Beispiel #1
0
 public CoreSnapshotService(CommandApplicationProcess applicationProcess, CoreState coreState, RaftLog raftLog, RaftMachine raftMachine)
 {
     this._applicationProcess = applicationProcess;
     this._coreState          = coreState;
     this._raftLog            = raftLog;
     this._raftMachine        = raftMachine;
 }
Beispiel #2
0
        private Callable <Void> SharedClientStarter(CoreTopologyService topologyService, ISet <MemberId> expectedTargetSet)
        {
            return(() =>
            {
                try
                {
                    RaftMachine raftMock = mock(typeof(RaftMachine));
                    RaftCoreTopologyConnector tc = new RaftCoreTopologyConnector(topologyService, raftMock, CausalClusteringSettings.database.DefaultValue);
                    topologyService.init();
                    topologyService.start();
                    tc.start();

                    assertEventually("should discover complete target set", () =>
                    {
                        ArgumentCaptor <ISet <MemberId> > targetMembers = ArgumentCaptor.forClass((Type <ISet <MemberId> >)expectedTargetSet.GetType());
                        verify(raftMock, atLeastOnce()).TargetMembershipSet = targetMembers.capture();
                        return targetMembers.Value;
                    }, equalTo(expectedTargetSet), TIMEOUT_MS, MILLISECONDS);
                }
                catch (Exception throwable)
                {
                    fail(throwable.Message);
                }
                return null;
            });
        }
Beispiel #3
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void candidateShouldWinElectionAndBecomeLeader() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void CandidateShouldWinElectionAndBecomeLeader()
        {
            // given
            FakeClock    fakeClock = Clocks.fakeClock();
            TimerService timeouts  = new OnDemandTimerService(fakeClock);
            RaftMachine  raft      = (new RaftMachineBuilder(_myself, 3, RaftTestMemberSetBuilder.INSTANCE)).outbound(_outbound).timerService(timeouts).clock(fakeClock).build();

            raft.InstallCoreState(new RaftCoreState(new MembershipEntry(0, asSet(_myself, _member1, _member2))));
            raft.PostRecoveryActions();

            timeouts.Invoke(RaftMachine.Timeouts.ELECTION);

            // when
            raft.Handle(voteResponse().from(_member1).term(1).grant().build());
            raft.Handle(voteResponse().from(_member2).term(1).grant().build());

            // then
            assertEquals(1, raft.Term());
            assertEquals(LEADER, raft.CurrentRole());

            /*
             * We require atLeast here because RaftMachine has its own scheduled service, which can spuriously wake up and
             * send empty entries. These are fine and have no bearing on the correctness of this test, but can cause it
             * fail if we expect exactly 2 of these messages
             */
            verify(_outbound, atLeast(1)).send(eq(_member1), isA(typeof(Org.Neo4j.causalclustering.core.consensus.RaftMessages_AppendEntries_Request)));
            verify(_outbound, atLeast(1)).send(eq(_member2), isA(typeof(Org.Neo4j.causalclustering.core.consensus.RaftMessages_AppendEntries_Request)));
        }
Beispiel #4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void candidateShouldLoseElectionAndRemainCandidate() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void CandidateShouldLoseElectionAndRemainCandidate()
        {
            // Note the etcd implementation seems to diverge from the paper here, since the paper suggests that it should
            // remain as a candidate

            // given
            FakeClock    fakeClock = Clocks.fakeClock();
            TimerService timeouts  = new OnDemandTimerService(fakeClock);
            RaftMachine  raft      = (new RaftMachineBuilder(_myself, 3, RaftTestMemberSetBuilder.INSTANCE)).outbound(_outbound).timerService(timeouts).clock(fakeClock).build();

            raft.InstallCoreState(new RaftCoreState(new MembershipEntry(0, asSet(_myself, _member1, _member2))));
            raft.PostRecoveryActions();

            timeouts.Invoke(RaftMachine.Timeouts.ELECTION);

            // when
            raft.Handle(voteResponse().from(_member1).term(1).deny().build());
            raft.Handle(voteResponse().from(_member2).term(1).deny().build());

            // then
            assertEquals(1, raft.Term());
            assertEquals(CANDIDATE, raft.CurrentRole());

            verify(_outbound, never()).send(eq(_member1), isA(typeof(Org.Neo4j.causalclustering.core.consensus.RaftMessages_AppendEntries_Request)));
            verify(_outbound, never()).send(eq(_member2), isA(typeof(Org.Neo4j.causalclustering.core.consensus.RaftMessages_AppendEntries_Request)));
        }
Beispiel #5
0
        private static ThreadStart LeaderViewUpdatingListener(RaftMachine raft, IEnumerable <RaftMachine> validRafts, IDictionary <MemberId, MemberId> leaderViews, long viewCount, CompletableFuture <MemberId> futureAgreedLeader)
        {
            LeaderListener listener = newLeader =>
            {
                lock ( leaderViews )
                {
                    leaderViews[raft.Identity()] = newLeader.memberId();

                    bool leaderIsValid = false;
                    foreach (RaftMachine validRaft in validRafts)
                    {
                        if (validRaft.Identity().Equals(newLeader.memberId()))
                        {
                            leaderIsValid = true;
                        }
                    }

                    if (newLeader.memberId() != null && leaderIsValid && AllAgreeOnLeader(leaderViews, viewCount, newLeader.memberId()))
                    {
                        futureAgreedLeader.complete(newLeader.memberId());
                    }
                }
            };

            raft.RegisterListener(listener);
            return(() => raft.unregisterListener(listener));
        }
Beispiel #6
0
 public MembershipWaiterLifecycle(MembershipWaiter membershipWaiter, long joinCatchupTimeout, RaftMachine raft, LogProvider logProvider)
 {
     this._membershipWaiter   = membershipWaiter;
     this._joinCatchupTimeout = joinCatchupTimeout;
     this._raft = raft;
     this._log  = logProvider.getLog(this.GetType());
 }
Beispiel #7
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldTimeoutIfLeaderCommitIsNeverKnown() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldTimeoutIfLeaderCommitIsNeverKnown()
        {
            OnDemandJobScheduler jobScheduler = new OnDemandJobScheduler();
            MembershipWaiter     waiter       = new MembershipWaiter(member(0), jobScheduler, () => _dbHealth, 1, NullLogProvider.Instance, new Monitors());

            ExposedRaftState raftState = RaftStateBuilder.raftState().leaderCommit(-1).build().copy();

            RaftMachine raft = mock(typeof(RaftMachine));

            when(raft.State()).thenReturn(raftState);

            CompletableFuture <bool> future = waiter.WaitUntilCaughtUpMember(raft);

            jobScheduler.RunJob();

            try
            {
                future.get(10, MILLISECONDS);
                fail("Should have timed out.");
            }
            catch (TimeoutException)
            {
                // expected
            }
        }
Beispiel #8
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Before public void before() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void Before()
        {
            // given
            _testEntryLog = new InMemoryRaftLog();
            _testEntryLog.append(new RaftLogEntry(0, new RaftTestGroup(_myself)));

            _raft = (new RaftMachineBuilder(_myself, 3, RaftTestMemberSetBuilder.INSTANCE)).raftLog(_testEntryLog).commitListener(CommitListener).build();
        }
Beispiel #9
0
 internal Evaluator(MembershipWaiter outerInstance, RaftMachine raft, CompletableFuture <bool> catchUpFuture, System.Func <DatabaseHealth> dbHealthSupplier)
 {
     this._outerInstance   = outerInstance;
     this.Raft             = raft;
     this.CatchUpFuture    = catchUpFuture;
     this.LastLeaderCommit = raft.State().leaderCommit();
     this.DbHealthSupplier = dbHealthSupplier;
 }
Beispiel #10
0
 public RaftMessageApplier(LocalDatabase localDatabase, LogProvider logProvider, RaftMachine raftMachine, CoreStateDownloaderService downloadService, CommandApplicationProcess applicationProcess, Org.Neo4j.causalclustering.catchup.CatchupAddressProvider_PrioritisingUpstreamStrategyBasedAddressProvider catchupAddressProvider)
 {
     this._localDatabase          = localDatabase;
     this._log                    = logProvider.getLog(this.GetType());
     this._raftMachine            = raftMachine;
     this._downloadService        = downloadService;
     this._applicationProcess     = applicationProcess;
     this._catchupAddressProvider = catchupAddressProvider;
 }
Beispiel #11
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Before public void setup() throws java.io.IOException
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void Setup()
        {
            // given
            RaftLog raftLog = new InMemoryRaftLog();

            raftLog.Append(new RaftLogEntry(0, new RaftTestGroup(0)));

            _raft = (new RaftMachineBuilder(_myself, 3, RaftTestMemberSetBuilder.INSTANCE)).raftLog(raftLog).outbound(_outbound).build();
        }
Beispiel #12
0
        internal virtual CompletableFuture <bool> WaitUntilCaughtUpMember(RaftMachine raft)
        {
            CompletableFuture <bool> catchUpFuture = new CompletableFuture <bool>();

            Evaluator evaluator = new Evaluator(this, raft, catchUpFuture, _dbHealthSupplier);

            JobHandle jobHandle = _jobScheduler.schedule(Group.MEMBERSHIP_WAITER, evaluator, _currentCatchupDelayInMs, MILLISECONDS);

            catchUpFuture.whenComplete((result, e) => jobHandle.cancel(true));

            return(catchUpFuture);
        }
Beispiel #13
0
        internal CoreStatus(OutputFormat output, CoreGraphDatabase db) : base(output)
        {
            this._output = output;
            this._db     = db;

            DependencyResolver dependencyResolver = Db.DependencyResolver;

            this._raftMembershipManager        = dependencyResolver.ResolveDependency(typeof(RaftMembershipManager));
            this._databaseHealth               = dependencyResolver.ResolveDependency(typeof(DatabaseHealth));
            this._topologyService              = dependencyResolver.ResolveDependency(typeof(TopologyService));
            this._raftMachine                  = dependencyResolver.ResolveDependency(typeof(RaftMachine));
            this._raftMessageTimerResetMonitor = dependencyResolver.ResolveDependency(typeof(DurationSinceLastMessageMonitor));
            _commandIndexTracker               = dependencyResolver.ResolveDependency(typeof(CommandIndexTracker));
        }
Beispiel #14
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void candidateShouldVoteForTheSameCandidateInTheSameTerm() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void CandidateShouldVoteForTheSameCandidateInTheSameTerm()
        {
            // given
            FakeClock    fakeClock = Clocks.fakeClock();
            TimerService timeouts  = new OnDemandTimerService(fakeClock);
            RaftMachine  raft      = (new RaftMachineBuilder(_myself, 3, RaftTestMemberSetBuilder.INSTANCE)).outbound(_outbound).timerService(timeouts).clock(fakeClock).build();

            raft.InstallCoreState(new RaftCoreState(new MembershipEntry(0, asSet(_myself, _member1, _member2))));

            // when
            raft.Handle(voteRequest().from(_member1).candidate(_member1).term(1).build());
            raft.Handle(voteRequest().from(_member1).candidate(_member1).term(1).build());

            // then
            verify(_outbound, times(2)).send(_member1, voteResponse().term(1).grant().build());
        }
Beispiel #15
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldWaitUntilLeaderCommitIsAvailable() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldWaitUntilLeaderCommitIsAvailable()
        {
            OnDemandJobScheduler jobScheduler = new OnDemandJobScheduler();
            MembershipWaiter     waiter       = new MembershipWaiter(member(0), jobScheduler, () => _dbHealth, 500, NullLogProvider.Instance, new Monitors());

            InMemoryRaftLog raftLog = new InMemoryRaftLog();

            raftLog.Append(new RaftLogEntry(0, valueOf(0)));
            ExposedRaftState raftState = RaftStateBuilder.raftState().votingMembers(member(0)).leaderCommit(0).entryLog(raftLog).commitIndex(0L).build().copy();

            RaftMachine raft = mock(typeof(RaftMachine));

            when(raft.State()).thenReturn(raftState);

            CompletableFuture <bool> future = waiter.WaitUntilCaughtUpMember(raft);

            jobScheduler.RunJob();

            future.get(1, TimeUnit.SECONDS);
        }
Beispiel #16
0
        internal Fixture(ISet <MemberId> memberIds, TestNetwork net, long electionTimeout, long heartbeatInterval)
        {
            this.Net = net;

            foreach (MemberId member in memberIds)
            {
                TestNetwork.Inbound  inbound  = new TestNetwork.Inbound(net, member);
                TestNetwork.Outbound outbound = new TestNetwork.Outbound(net, member);

                _members.Add(member);

                TimerService timerService = CreateTimerService();

                BootstrapWaiter waiter = new BootstrapWaiter();
                _bootstrapWaiters.Add(waiter);

                InMemoryRaftLog raftLog     = new InMemoryRaftLog();
                RaftMachine     raftMachine = (new RaftMachineBuilder(member, memberIds.Count, RaftTestMemberSetBuilder.INSTANCE)).electionTimeout(electionTimeout).heartbeatInterval(heartbeatInterval).inbound(inbound).outbound(outbound).timerService(timerService).raftLog(raftLog).commitListener(waiter).build();

                Rafts.Add(new RaftFixture(this, raftMachine, raftLog));
            }
        }
Beispiel #17
0
 public RaftCoreTopologyConnector(CoreTopologyService coreTopologyService, RaftMachine raftMachine, string dbName)
 {
     this._coreTopologyService = coreTopologyService;
     this._raftMachine         = raftMachine;
     this._dbName = dbName;
 }
Beispiel #18
0
 internal RaftFixture(Fixture outerInstance, RaftMachine raftMachine, InMemoryRaftLog raftLog)
 {
     this._outerInstance      = outerInstance;
     this.RaftMachineConflict = raftMachine;
     this.RaftLogConflict     = raftLog;
 }
Beispiel #19
0
        public CoreStateMachinesModule(MemberId myself, PlatformModule platformModule, File clusterStateDirectory, Config config, RaftReplicator replicator, RaftMachine raftMachine, Dependencies dependencies, LocalDatabase localDatabase)
        {
            StateStorage <IdAllocationState>        idAllocationState;
            StateStorage <ReplicatedLockTokenState> lockTokenState;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.kernel.lifecycle.LifeSupport life = platformModule.life;
            LifeSupport life = platformModule.Life;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.io.fs.FileSystemAbstraction fileSystem = platformModule.fileSystem;
            FileSystemAbstraction fileSystem  = platformModule.FileSystem;
            LogService            logging     = platformModule.Logging;
            LogProvider           logProvider = logging.InternalLogProvider;

            lockTokenState = life.Add(new DurableStateStorage <>(fileSystem, clusterStateDirectory, LOCK_TOKEN_NAME, new ReplicatedLockTokenState.Marshal(new MemberId.Marshal()), config.Get(replicated_lock_token_state_size), logProvider));

            idAllocationState = life.Add(new DurableStateStorage <>(fileSystem, clusterStateDirectory, ID_ALLOCATION_NAME, new IdAllocationState.Marshal(), config.Get(id_alloc_state_size), logProvider));

            ReplicatedIdAllocationStateMachine idAllocationStateMachine = new ReplicatedIdAllocationStateMachine(idAllocationState);

            IDictionary <IdType, int> allocationSizes = GetIdTypeAllocationSizeFromConfig(config);

            ReplicatedIdRangeAcquirer idRangeAcquirer = new ReplicatedIdRangeAcquirer(replicator, idAllocationStateMachine, allocationSizes, myself, logProvider);

            IdTypeConfigurationProvider = new EnterpriseIdTypeConfigurationProvider(config);
            CommandIndexTracker commandIndexTracker = dependencies.SatisfyDependency(new CommandIndexTracker());

            FreeIdCondition         = new IdReusabilityCondition(commandIndexTracker, raftMachine, myself);
            this.IdGeneratorFactory = CreateIdGeneratorFactory(fileSystem, idRangeAcquirer, logProvider, IdTypeConfigurationProvider);

            TokenRegistry relationshipTypeTokenRegistry = new TokenRegistry(Org.Neo4j.Kernel.impl.core.TokenHolder_Fields.TYPE_RELATIONSHIP_TYPE);

            System.Func <StorageEngine>           storageEngineSupplier       = () => localDatabase.DataSource().DependencyResolver.resolveDependency(typeof(StorageEngine));
            ReplicatedRelationshipTypeTokenHolder relationshipTypeTokenHolder = new ReplicatedRelationshipTypeTokenHolder(relationshipTypeTokenRegistry, replicator, this.IdGeneratorFactory, storageEngineSupplier);

            TokenRegistry propertyKeyTokenRegistry = new TokenRegistry(Org.Neo4j.Kernel.impl.core.TokenHolder_Fields.TYPE_PROPERTY_KEY);
            ReplicatedPropertyKeyTokenHolder propertyKeyTokenHolder = new ReplicatedPropertyKeyTokenHolder(propertyKeyTokenRegistry, replicator, this.IdGeneratorFactory, storageEngineSupplier);

            TokenRegistry labelTokenRegistry            = new TokenRegistry(Org.Neo4j.Kernel.impl.core.TokenHolder_Fields.TYPE_LABEL);
            ReplicatedLabelTokenHolder labelTokenHolder = new ReplicatedLabelTokenHolder(labelTokenRegistry, replicator, this.IdGeneratorFactory, storageEngineSupplier);

            ReplicatedLockTokenStateMachine replicatedLockTokenStateMachine = new ReplicatedLockTokenStateMachine(lockTokenState);

            VersionContextSupplier      versionContextSupplier = platformModule.VersionContextSupplier;
            ReplicatedTokenStateMachine labelTokenStateMachine = new ReplicatedTokenStateMachine(labelTokenRegistry, logProvider, versionContextSupplier);

            ReplicatedTokenStateMachine propertyKeyTokenStateMachine = new ReplicatedTokenStateMachine(propertyKeyTokenRegistry, logProvider, versionContextSupplier);

            ReplicatedTokenStateMachine relationshipTypeTokenStateMachine = new ReplicatedTokenStateMachine(relationshipTypeTokenRegistry, logProvider, versionContextSupplier);

            PageCursorTracerSupplier          cursorTracerSupplier     = platformModule.Tracers.pageCursorTracerSupplier;
            ReplicatedTransactionStateMachine replicatedTxStateMachine = new ReplicatedTransactionStateMachine(commandIndexTracker, replicatedLockTokenStateMachine, config.Get(state_machine_apply_max_batch_size), logProvider, cursorTracerSupplier, versionContextSupplier);

            dependencies.SatisfyDependencies(replicatedTxStateMachine);

            LocksFactory lockFactory = createLockFactory(config, logging);

            LocksSupplier = () => CreateLockManager(lockFactory, config, platformModule.Clock, replicator, myself, raftMachine, replicatedLockTokenStateMachine);

            RecoverConsensusLogIndex consensusLogIndexRecovery = new RecoverConsensusLogIndex(localDatabase, logProvider);

            CoreStateMachines = new CoreStateMachines(replicatedTxStateMachine, labelTokenStateMachine, relationshipTypeTokenStateMachine, propertyKeyTokenStateMachine, replicatedLockTokenStateMachine, idAllocationStateMachine, new DummyMachine(), localDatabase, consensusLogIndexRecovery);

            CommitProcessFactory = (appender, applier, ignored) =>
            {
                localDatabase.RegisterCommitProcessDependencies(appender, applier);
                return(new ReplicatedTransactionCommitProcess(replicator));
            };

            this.TokenHolders = new TokenHolders(propertyKeyTokenHolder, labelTokenHolder, relationshipTypeTokenHolder);
            dependencies.SatisfyDependencies(TokenHolders);
        }