Esempio n. 1
0
 public LeaderRole(
     IRoleCoordinator <TOperation> coordinator,
     ILogger logger,
     IPersistentLog <TOperation> journal,
     IStateMachine <TOperation> stateMachine,
     IRaftPersistentState persistentState,
     IRaftVolatileState volatileState,
     IGrainFactory grainFactory,
     RegisterTimerDelegate registerTimer,
     IServerIdentity identity,
     IMembershipProvider membershipProvider,
     ISettings settings)
 {
     this.coordinator        = coordinator;
     this.logger             = logger;
     this.journal            = journal;
     this.stateMachine       = stateMachine;
     this.persistentState    = persistentState;
     this.volatileState      = volatileState;
     this.grainFactory       = grainFactory;
     this.registerTimer      = registerTimer;
     this.identity           = identity;
     this.membershipProvider = membershipProvider;
     this.settings           = settings;
     this.heartbeatTimeout   = TimeSpan.FromMilliseconds(this.settings.HeartbeatTimeoutMilliseconds);
 }
 public CandidateRole(
     IRoleCoordinator <TOperation> local,
     ILogger logger,
     IPersistentLog <TOperation> journal,
     IRaftPersistentState persistentState,
     IRaftVolatileState volatileState,
     IRandom random,
     IGrainFactory grainFactory,
     RegisterTimerDelegate registerTimer,
     IServerIdentity identity,
     IMembershipProvider membershipProvider,
     ISettings settings)
 {
     this.local              = local;
     this.logger             = logger;
     this.journal            = journal;
     this.persistentState    = persistentState;
     this.volatileState      = volatileState;
     this.random             = random;
     this.grainFactory       = grainFactory;
     this.registerTimer      = registerTimer;
     this.identity           = identity;
     this.membershipProvider = membershipProvider;
     this.settings           = settings;
 }
 public RoleCoordinator(
     IRaftPersistentState persistentState,
     ILogger logger,
     Func <IFollowerRole <TOperation> > createFollowerRole,
     Func <ICandidateRole <TOperation> > createCandidateRole,
     Func <ILeaderRole <TOperation> > createLeaderRole)
 {
     this.persistentState     = persistentState;
     this.logger              = logger;
     this.createFollowerRole  = createFollowerRole;
     this.createCandidateRole = createCandidateRole;
     this.createLeaderRole    = createLeaderRole;
 }
Esempio n. 4
0
 public FollowerRole(
     ILogger logger,
     IRoleCoordinator <TOperation> coordinator,
     IPersistentLog <TOperation> journal,
     IStateMachine <TOperation> stateMachine,
     IRaftPersistentState persistentState,
     IRaftVolatileState volatileState,
     IRandom random,
     RegisterTimerDelegate registerTimer,
     ISettings settings)
 {
     this.logger          = logger;
     this.coordinator     = coordinator;
     this.journal         = journal;
     this.stateMachine    = stateMachine;
     this.persistentState = persistentState;
     this.volatileState   = volatileState;
     this.random          = random;
     this.registerTimer   = registerTimer;
     this.settings        = settings;
 }
Esempio n. 5
0
        /// <summary>
        /// This method is called at the end of the process of activating a grain.
        ///             It is called before any messages have been dispatched to the grain.
        ///             For grains with declared persistent state, this method is called after the State property has been populated.
        /// </summary>
        public override async Task OnActivateAsync()
        {
            this.log = this.GetLogger($"{this.GetPrimaryKeyString()}");
            this.log.Info("Activating");

            // TODO: Get servers from Orleans' memberhsip provider.
            var allServers = new[] { "one", "two", "three" };

            var applicationContainerBuilder = new ContainerBuilder();

            // TODO: Move these registrations into a module.
            applicationContainerBuilder.RegisterType <Settings>().As <ISettings>().SingleInstance().PreserveExistingDefaults();
            applicationContainerBuilder.RegisterType <StaticMembershipProvider>()
            .OnActivated(_ => _.Instance.SetServers(allServers))
            .InstancePerLifetimeScope()
            .AsImplementedInterfaces()
            .PreserveExistingDefaults();
            applicationContainerBuilder.RegisterType <VolatileState>()
            .SingleInstance()
            .AsImplementedInterfaces()
            .PreserveExistingDefaults();
            applicationContainerBuilder.RegisterType <RoleCoordinator <TOperation> >()
            .InstancePerLifetimeScope()
            .AsImplementedInterfaces()
            .PreserveExistingDefaults();
            applicationContainerBuilder.Register <IRandom>(_ => ConcurrentRandom.Instance)
            .SingleInstance()
            .PreserveExistingDefaults();

            // Register roles.
            applicationContainerBuilder.RegisterType <FollowerRole <TOperation> >().AsImplementedInterfaces().PreserveExistingDefaults();
            applicationContainerBuilder.RegisterType <CandidateRole <TOperation> >().AsImplementedInterfaces().PreserveExistingDefaults();
            applicationContainerBuilder.RegisterType <LeaderRole <TOperation> >().AsImplementedInterfaces().PreserveExistingDefaults();

            var applicationContainer = applicationContainerBuilder.Build();

            // Build the container for this grain's scope.
            this.container = applicationContainer.BeginLifetimeScope(
                builder =>
            {
                builder.RegisterInstance(this.GrainFactory).SingleInstance().PreserveExistingDefaults();
                builder.Register <IServerIdentity>(_ => new ServerIdentity {
                    Id = this.GetPrimaryKeyString()
                })
                .SingleInstance()
                .PreserveExistingDefaults();

                builder.Register(_ => new OrleansLogger(this.log)
                {
                    FormatMessage = this.GetLogMessage
                })
                .SingleInstance()
                .AsImplementedInterfaces()
                .PreserveExistingDefaults();

                builder.RegisterInstance <RegisterTimerDelegate>(this.RegisterTimer)
                .SingleInstance()
                .PreserveExistingDefaults();

                // By default, all persistent state is stored using the configured grain state storage provider.
                builder.Register <IRaftPersistentState>(
                    _ => new OrleansStorageRaftPersistentState <TOperation>(this.State, this.LogAndWriteState))
                .SingleInstance()
                .PreserveExistingDefaults();
                builder.Register(_ => this.State.Log)
                .OnActivated(_ => _.Instance.WriteCallback = this.LogAndWriteJournal)
                .SingleInstance()
                .As <IPersistentLog <TOperation> >()
                .PreserveExistingDefaults();

                // The consumer typically provides their own state machine, so register the method used to retrieve
                // it.
                builder.Register(this.GetStateMachine);
            });

            // Resolve services.
            this.persistentState = this.container.Resolve <IRaftPersistentState>();
            this.coordinator     = this.container.Resolve <IRoleCoordinator <TOperation> >();
            this.journal         = this.container.Resolve <IPersistentLog <TOperation> >();
            this.logger          = this.container.Resolve <ILogger>();

            await this.coordinator.Initialize();

            await base.OnActivateAsync();
        }