Skip to content

baryvn/Orleans.Oracle

Repository files navigation

Orleans Oracle Providers

Orleans is a framework that provides a straight-forward approach to building distributed high-scale computing applications, without the need to learn and apply complex concurrency or other scaling patterns.

Orleans.Oracle

is a package that use Oracle as a backend for Orleans providers like Cluster Membership, Grain State storage.

Installation

Nuget Packages are provided:

  • Orleans.Oracle.Core
  • Orleans.Clustering.Oracle
  • Orleans.Persistence.Oracle
  • Orleans.Reminders.Oracle

Note

In development environment sometimes you will have to delete member in cluster's Member table. the reason for this issue is when you suddenly stop the application while running test or debug orleans can't update the state down to member table properly and will show error when starting cluster

Silo


IHostBuilder builder = Host.CreateDefaultBuilder(args)
    .UseOrleans(silo =>
    {
        silo.Configure<ClusterOptions>(options =>
        {
            options.ClusterId = "ORLEANS_ORACLE_DC";
            options.ServiceId = "ORLEANS_ORACLE";

        });

        // Add Oracle DbContext, this db context is used in, Clustering,GrainStorage and Reminder
        var conn = "******************";
        silo.Services.AddDbContext<OracleDbContext>(options => options.UseOracle(conn, o =>
        {
            o.UseOracleSQLCompatibility(OracleSQLCompatibility.DatabaseVersion19);
        }), ServiceLifetime.Scoped);]
        // Add clustering
        silo.UseOracleClustering();

        // Add Persitend storage
        silo.AddOracleGrainStorage("Storage", option =>
        {
            option.Tables = new List<Type> { typeof(TestModel) };
        });

        // Add Reminder
        silo.UseOracleReminder();

        
        silo.ConfigureLogging(logging => logging.AddConsole());

        silo.ConfigureEndpoints(
            siloPort: 11111,
            gatewayPort: 30001,
            advertisedIP: IPAddress.Parse(bindAdress),
            listenOnAnyHostAddress: true
            );

        silo.Configure<ClusterMembershipOptions>(options =>
        {
            options.EnableIndirectProbes = true;
            options.UseLivenessGossip = true;
        });
    })
    .UseConsoleLifetime();

using IHost host = builder.Build();

await host.RunAsync();

Client


var builder = WebApplication.CreateBuilder(args);
var conn = "****************";
builder.Services.AddDbContext<OracleDbContext>(options => options.UseOracle(conn, o =>
{
    o.UseOracleSQLCompatibility(OracleSQLCompatibility.DatabaseVersion19);
}), ServiceLifetime.Scoped);

builder.Host.UseOrleansClient(client =>
{
    client.Configure<ClusterOptions>(options =>
    {
        options.ClusterId = "ORLEANS_ORACLE_DC";
        options.ServiceId = "ORLEANS_ORACLE";
    });
    client.UseOracleClustering();
});

Define Persistence

  • BaseEntity is require
  • property name is uppercase
  • [Description("TEST_TABLE")] of class is table name
  • [Description("VARCHAR2(50)")] of properties is oracle data type
  • [Key] is GrainKey type GuidKey
  • [Key] and [GroupKey] of properties is set this properties is primarykey in oracle

BaseEntity

[GenerateSerializer]
public class BaseEntity
{
    [Description("VARCHAR2(128)")]
    [Id(0)]
    [Key]
    public string ID { get; set; } = Guid.NewGuid().ToString();
}

Table

[Description("TEST_TABLE")]
[GenerateSerializer]
public class TestModel : BaseEntity
{
    [Description("VARCHAR2(128)")]
    [Id(1)]
    [GroupKey]
    public string FORENKEY { get; set; } = Guid.NewGuid().ToString();

    [Description("VARCHAR2(50)")]
    [Id(0)]
    public string MYCOLUM { get; set; }
}

Use Persistence & Remider

interface grain

public interface IHelloGrain : IGrainWithGuidKey
{
    ValueTask<string> SayHello(string greeting);
    Task<string> GetMyColumn();

    void SaveColumn();
}

impliment grain

using Microsoft.Extensions.Logging;
using Orleans.Oracle.Core;
using Orleans.Timers;
public class HelloGrain : Grain, IHelloGrain, IRemindable
{
    private readonly ILogger _logger;

    private readonly IReminderRegistry _reminderRegistry;
    private readonly IPersistentState<BaseState<TestModel>> _test;

    private IGrainReminder? _rTest;
    private bool _taskDone = false;

    public HelloGrain(ILogger<HelloGrain> logger, IReminderRegistry reminderRegistry, [PersistentState("test", "Storage")] IPersistentState<BaseState<TestModel>> test)
    {
        _logger = logger;
        _test = test;
        _reminderRegistry = reminderRegistry;
    }

    public override Task OnActivateAsync(CancellationToken cancellationToken)
    {
        return Task.WhenAll(_test.ReadStateAsync());
    }


    public async Task<string> GetCount()
    {
        return _test.State.Items.Count.ToString();
    }

    public async Task AddItem(TestModel model)
    {
        // items is a list
        _test.State.Items.Add(model);
        await _test.WriteStateAsync();
    }
    public async Task ReceiveReminder(string reminderName, TickStatus status)
    {
        try
        {
            if (reminderName == "TEST_REMIDER")
            {
                // Excute task
                if (_taskDone)
                {
                    if (_rTest == null)
                    {
                        _rTest = await _reminderRegistry.GetReminder(GrainContext.GrainId, "TEST_REMIDER");
                    }
                    if (_rTest != null)
                        await _reminderRegistry.UnregisterReminder(GrainContext.GrainId, _rTest);
                }
            }
        }
        catch (Exception ex)
        {
            //log
        }
    }
    public async Task RegisterRemider()
    {
        if (_rTest == null)
        {
            _rTest = await _reminderRegistry.GetReminder(GrainContext.GrainId, "TEST_REMIDER");
        }
        if (_rTest == null)
        {
            _rTest = await _reminderRegistry.RegisterOrUpdateReminder(
            callingGrainId: GrainContext.GrainId,
            reminderName: "TEST_REMIDER",
            dueTime: TimeSpan.Zero,
            period: TimeSpan.FromMinutes(1));
        }
    }

}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published