예제 #1
0
 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;
 }
예제 #2
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);
 }
        private void TestEmptyLog(IPersistentLog <TestOperation> log)
        {
            log.Contains(new LogEntryId(1, 4)).Should().BeFalse();

            Assert.Throws <ArgumentOutOfRangeException>(() => log.Get(2));

            var entries = log.GetCursor(2).ToArray();

            entries.Should().HaveCount(0);

            var reverseEntries = log.GetReverseCursor().ToArray();

            reverseEntries.Should().HaveCount(0);
        }
        private void TestLog(IPersistentLog <TestOperation> log)
        {
            var operations = new LogEntry <TestOperation>[]
            {
                new LogEntry <TestOperation>(new LogEntryId(1, 1), new TestOperation {
                    StringValue = "operation1"
                }),
                new LogEntry <TestOperation>(new LogEntryId(1, 2), new TestOperation {
                    StringValue = "operation2"
                }),
                new LogEntry <TestOperation>(new LogEntryId(1, 3), new TestOperation {
                    StringValue = "operation3"
                })
            };

            log.AppendOrOverwrite(operations).Wait();

            log.Contains(operations[0].Id).Should().BeTrue();
            log.Contains(operations[1].Id).Should().BeTrue();
            log.Contains(operations[2].Id).Should().BeTrue();

            log.Contains(new LogEntryId(1, 4)).Should().BeFalse();
            log.Contains(new LogEntryId(2, 1)).Should().BeFalse();



            var entry2 = log.Get(2);

            entry2.Id.Should().Be(new LogEntryId(1, 2));
            entry2.Operation.StringValue.Should().Be("operation2");

            var entries = log.GetCursor(2).ToArray();

            entries.Should().HaveCount(2);
            entries[0].Id.Should().Be(new LogEntryId(1, 2));
            entries[1].Id.Should().Be(new LogEntryId(1, 3));

            var reverseEntries = log.GetReverseCursor().ToArray();

            reverseEntries.Should().HaveCount(3);
            reverseEntries[0].Id.Should().Be(new LogEntryId(1, 3));
            reverseEntries[1].Id.Should().Be(new LogEntryId(1, 2));
            reverseEntries[2].Id.Should().Be(new LogEntryId(1, 1));
        }
예제 #5
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;
 }
        void TestLog(IPersistentLog<TestOperation> log)
        {
            var operations = new LogEntry<TestOperation>[]
            {
                new LogEntry<TestOperation>(new LogEntryId(1, 1), new TestOperation { StringValue = "operation1" }),
                new LogEntry<TestOperation>(new LogEntryId(1, 2), new TestOperation { StringValue = "operation2" }),
                new LogEntry<TestOperation>(new LogEntryId(1, 3), new TestOperation { StringValue = "operation3" })
            };

            log.AppendOrOverwrite(operations).Wait();

            log.Contains(operations[0].Id).Should().BeTrue();
            log.Contains(operations[1].Id).Should().BeTrue();
            log.Contains(operations[2].Id).Should().BeTrue();

            log.Contains(new LogEntryId(1,4)).Should().BeFalse();
            log.Contains(new LogEntryId(2, 1)).Should().BeFalse();

          

            var entry2 = log.Get(2);
            entry2.Id.Should().Be(new LogEntryId(1, 2));
            entry2.Operation.StringValue.Should().Be("operation2");

            var entries = log.GetCursor(2).ToArray();
            entries.Should().HaveCount(2);
            entries[0].Id.Should().Be(new LogEntryId(1, 2));
            entries[1].Id.Should().Be(new LogEntryId(1, 3));

            var reverseEntries = log.GetReverseCursor().ToArray();
            reverseEntries.Should().HaveCount(3);
            reverseEntries[0].Id.Should().Be(new LogEntryId(1, 3));
            reverseEntries[1].Id.Should().Be(new LogEntryId(1, 2));
            reverseEntries[2].Id.Should().Be(new LogEntryId(1, 1));

        }
        void TestEmptyLog(IPersistentLog<TestOperation> log)
        {

            log.Contains(new LogEntryId(1, 4)).Should().BeFalse();

            Assert.Throws<ArgumentOutOfRangeException>(() => log.Get(2));

            var entries = log.GetCursor(2).ToArray();
            entries.Should().HaveCount(0);

            var reverseEntries = log.GetReverseCursor().ToArray();
            reverseEntries.Should().HaveCount(0);

        }
 public static string ProgressString <TOperation>(this IPersistentLog <TOperation> log)
 {
     return($"[{string.Join(", ", log.GetCursor(0).Select(_ => _.Id))}]");
 }
예제 #9
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();
        }