Esempio n. 1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldBecomeLeaderInMajorityOf5() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldBecomeLeaderInMajorityOf5()
        {
            // Given
            FakeClock            fakeClock    = Clocks.fakeClock();
            OnDemandTimerService timerService = new OnDemandTimerService(fakeClock);
            RaftMachine          raft         = (new RaftMachineBuilder(_myself, 3, RaftTestMemberSetBuilder.INSTANCE)).timerService(timerService).clock(fakeClock).build();

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

            timerService.Invoke(ELECTION);

            raft.Handle(voteResponse().from(_member1).term(1).grant().build());
            assertThat(raft.Leader, @is(false));

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

            // Then
            assertThat(raft.Leader, @is(true));
        }
Esempio n. 2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldNotFinishIfRoundsNotExhausted()
        public virtual void ShouldNotFinishIfRoundsNotExhausted()
        {
            FakeClock clock = Clocks.fakeClock();
            StubLog   log   = new StubLog(this);

            long appendIndex = 10;

            log.AppendIndex = appendIndex;
            CatchupGoalTracker catchupGoalTracker = new CatchupGoalTracker(log, clock, ROUND_TIMEOUT, CATCHUP_TIMEOUT);

            for (int i = 0; i < CatchupGoalTracker.MAX_ROUNDS - 5; i++)
            {
                appendIndex    += 10;
                log.AppendIndex = appendIndex;
                clock.Forward(ROUND_TIMEOUT + 1, TimeUnit.MILLISECONDS);
                catchupGoalTracker.UpdateProgress((new FollowerState()).onSuccessResponse(appendIndex));
            }

            // then
            assertFalse(catchupGoalTracker.GoalAchieved);
            assertFalse(catchupGoalTracker.Finished);
        }
Esempio n. 3
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldProvideInterruptHandlerForSuspendedTransaction() throws org.neo4j.server.rest.transactional.error.TransactionLifecycleException
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldProvideInterruptHandlerForSuspendedTransaction()
        {
            // Given
            AssertableLogProvider logProvider = new AssertableLogProvider();
            FakeClock             clock       = Clocks.fakeClock();
            int timeoutLength = 123;

            TransactionHandleRegistry registry = new TransactionHandleRegistry(clock, timeoutLength, logProvider);
            TransactionHandle         handle   = mock(typeof(TransactionHandle));

            // Suspended Tx in Registry
            long id = registry.Begin(handle);

            registry.Release(id, handle);

            // When
            registry.Terminate(id);

            // Then
            verify(handle, times(1)).terminate();
            verifyNoMoreInteractions(handle);
        }
Esempio n. 4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void acquiringANonExistentTransactionShouldThrowErrorInvalidTransactionId() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void AcquiringANonExistentTransactionShouldThrowErrorInvalidTransactionId()
        {
            // Given
            AssertableLogProvider     logProvider = new AssertableLogProvider();
            TransactionHandleRegistry registry    = new TransactionHandleRegistry(Clocks.fakeClock(), 0, logProvider);

            long madeUpTransactionId = 1337;

            // When
            try
            {
                registry.Acquire(madeUpTransactionId);
                fail("Should have thrown exception");
            }
            catch (InvalidTransactionId)
            {
                // expected
            }

            // then
            logProvider.AssertNoLoggingOccurred();
        }
Esempio n. 5
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void leaderShouldSendHeartBeatsOnHeartbeatTimeout() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void LeaderShouldSendHeartBeatsOnHeartbeatTimeout()
        {
            // Given
            FakeClock                fakeClock    = Clocks.fakeClock();
            OnDemandTimerService     timerService = new OnDemandTimerService(fakeClock);
            OutboundMessageCollector messages     = new OutboundMessageCollector();

            RaftMachine raft = (new RaftMachineBuilder(_myself, 3, RaftTestMemberSetBuilder.INSTANCE)).timerService(timerService).outbound(messages).clock(fakeClock).build();

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

            timerService.Invoke(ELECTION);
            raft.Handle(voteResponse().from(_member1).term(1).grant().build());

            // When
            timerService.Invoke(RaftMachine.Timeouts.Heartbeat);

            // Then
            assertTrue(last(messages.SentTo(_member1)) is RaftMessages_Heartbeat);
            assertTrue(last(messages.SentTo(_member2)) is RaftMessages_Heartbeat);
        }
Esempio n. 6
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldDelayFreeingOfAggressivelyReusedIdsConsideringTimeAsWell()
        public virtual void ShouldDelayFreeingOfAggressivelyReusedIdsConsideringTimeAsWell()
        {
            // GIVEN
            MockedIdGeneratorFactory actual = new MockedIdGeneratorFactory();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.time.FakeClock clock = org.neo4j.time.Clocks.fakeClock();
            FakeClock clock = Clocks.fakeClock();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final long safeZone = MINUTES.toMillis(1);
            long safeZone = MINUTES.toMillis(1);
            ControllableSnapshotSupplier boundaries = new ControllableSnapshotSupplier();
            BufferingIdGeneratorFactory  bufferingIdGeneratorFactory = new BufferingIdGeneratorFactory(actual, t => clock.Millis() - t.snapshotTime() >= safeZone, new CommunityIdTypeConfigurationProvider());

            bufferingIdGeneratorFactory.Initialize(boundaries);

            IdGenerator idGenerator = bufferingIdGeneratorFactory.Open(new File("doesnt-matter"), 10, IdType.StringBlock, () => 0L, int.MaxValue);

            // WHEN
            idGenerator.FreeId(7);
            verifyNoMoreInteractions(actual.Get(IdType.StringBlock));

            // after some maintenance and transaction still not closed
            bufferingIdGeneratorFactory.Maintenance();
            verifyNoMoreInteractions(actual.Get(IdType.StringBlock));

            // although after transactions have all closed
            boundaries.SetMostRecentlyReturnedSnapshotToAllClosed();
            bufferingIdGeneratorFactory.Maintenance();
            // ... the clock would still say "nope" so no interaction
            verifyNoMoreInteractions(actual.Get(IdType.StringBlock));

            // then finally after time has passed as well
            clock.Forward(70, SECONDS);
            bufferingIdGeneratorFactory.Maintenance();

            // THEN
            verify(actual.Get(IdType.StringBlock)).freeId(7);
        }
Esempio n. 7
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldMonitorLeaderNotFound() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldMonitorLeaderNotFound()
        {
            // Given
            FakeClock            fakeClock    = Clocks.fakeClock();
            OnDemandTimerService timerService = new OnDemandTimerService(fakeClock);

            RaftMachine raft = (new RaftMachineBuilder(_myself, 3, RaftTestMemberSetBuilder.INSTANCE)).timerService(timerService).build();

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

            try
            {
                // When
                // There is no leader
                raft.Leader;
                fail("Should have thrown exception");
            }
            // Then
            catch (NoLeaderFoundException)
            {
                // expected
            }
        }
Esempio n. 8
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void transactionsShouldBeEvictedWhenUnusedLongerThanTimeout() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void TransactionsShouldBeEvictedWhenUnusedLongerThanTimeout()
        {
            // Given
            FakeClock                 clock       = Clocks.fakeClock();
            AssertableLogProvider     logProvider = new AssertableLogProvider();
            TransactionHandleRegistry registry    = new TransactionHandleRegistry(clock, 0, logProvider);
            TransactionHandle         oldTx       = mock(typeof(TransactionHandle));
            TransactionHandle         newTx       = mock(typeof(TransactionHandle));
            TransactionHandle         handle      = mock(typeof(TransactionHandle));

            long txId1 = registry.Begin(handle);
            long txId2 = registry.Begin(handle);

            // And given one transaction was stored one minute ago, and another was stored just now
            registry.Release(txId1, oldTx);
            clock.Forward(1, TimeUnit.MINUTES);
            registry.Release(txId2, newTx);

            // When
            registry.RollbackSuspendedTransactionsIdleSince(clock.Millis() - 1000);

            // Then
            assertThat(registry.Acquire(txId2), equalTo(newTx));

            // And then the other should have been evicted
            try
            {
                registry.Acquire(txId1);
                fail("Should have thrown exception");
            }
            catch (InvalidTransactionId)
            {
                // ok
            }

            logProvider.AssertExactly(inLog(typeof(TransactionHandleRegistry)).info("Transaction with id 1 has been automatically rolled " + "back due to transaction timeout."));
        }
Esempio n. 9
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldRequestVotesOnElectionTimeout() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldRequestVotesOnElectionTimeout()
        {
            // Given
            FakeClock                fakeClock    = Clocks.fakeClock();
            OnDemandTimerService     timerService = new OnDemandTimerService(fakeClock);
            OutboundMessageCollector messages     = new OutboundMessageCollector();

            RaftMachine raft = (new RaftMachineBuilder(_myself, 3, RaftTestMemberSetBuilder.INSTANCE)).timerService(timerService).electionTimeout(_electionTimeout).clock(fakeClock).outbound(messages).build();

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

            // When
            timerService.Invoke(ELECTION);

            // Then
            assertThat(messages.SentTo(_myself).Count, equalTo(0));

            assertThat(messages.SentTo(_member1).Count, equalTo(1));
            assertThat(messages.SentTo(_member1)[0], instanceOf(typeof(RaftMessages_Vote_Request)));

            assertThat(messages.SentTo(_member2).Count, equalTo(1));
            assertThat(messages.SentTo(_member2)[0], instanceOf(typeof(RaftMessages_Vote_Request)));
        }
Esempio n. 10
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void newMembersShouldBeIncludedInHeartbeatMessages() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void NewMembersShouldBeIncludedInHeartbeatMessages()
        {
            // Given
            DirectNetworking network = new DirectNetworking();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.causalclustering.identity.MemberId newMember = member(99);
            MemberId newMember = member(99);

            DirectNetworking.Inbound <RaftMessages_RaftMessage> newMemberInbound = new Org.Neo4j.causalclustering.core.consensus.DirectNetworking.Inbound <RaftMessages_RaftMessage>(network, newMember);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final OutboundMessageCollector messages = new OutboundMessageCollector();
            OutboundMessageCollector messages = new OutboundMessageCollector();

            newMemberInbound.RegisterHandler((Org.Neo4j.causalclustering.messaging.Inbound_MessageHandler <RaftMessages_RaftMessage>)message => messages.send(newMember, message));

            FakeClock            fakeClock    = Clocks.fakeClock();
            OnDemandTimerService timerService = new OnDemandTimerService(fakeClock);
            RaftMachine          raft         = (new RaftMachineBuilder(_myself, 3, RaftTestMemberSetBuilder.INSTANCE)).timerService(timerService).outbound(messages).clock(fakeClock).build();

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

            // We make ourselves the leader
            timerService.Invoke(ELECTION);
            raft.Handle(voteResponse().from(_member1).term(1).grant().build());

            // When
            raft.TargetMembershipSet = asSet(_myself, _member1, _member2, newMember);
            network.ProcessMessages();

            timerService.Invoke(RaftMachine.Timeouts.Heartbeat);
            network.ProcessMessages();

            // Then
            assertEquals(typeof(RaftMessages_AppendEntries_Request), messages.SentTo(newMember)[0].GetType());
        }
Esempio n. 11
0
 private void InitializeInstanceFields()
 {
     _fileNames  = new FileNames(_baseDir);
     _readerPool = spy(new ReaderPool(0, _logProvider, _fileNames, FsRule.get(), Clocks.fakeClock()));
 }
Esempio n. 12
0
        public override RaftLog CreateRaftLog()
        {
            File directory = new File(RAFT_LOG_DIRECTORY_NAME);
            FileSystemAbstraction fileSystem = _fsRule.get();

            fileSystem.Mkdir(directory);

            LogProvider            logProvider     = Instance;
            CoreLogPruningStrategy pruningStrategy = (new CoreLogPruningStrategyFactory("1 entries", logProvider)).NewInstance();

            return(_life.add(new SegmentedRaftLog(fileSystem, directory, 1024, new DummyRaftableContentSerializer(), logProvider, 8, Clocks.fakeClock(), new OnDemandJobScheduler(), pruningStrategy)));
        }
Esempio n. 13
0
        public override SegmentedRaftLog CreateRaftLog(FileSystemAbstraction fsa, File dir)
        {
            long                   rotateAtSize    = ByteUnit.mebiBytes(8);
            LogProvider            logProvider     = Instance;
            int                    readerPoolSize  = 8;
            CoreLogPruningStrategy pruningStrategy = (new CoreLogPruningStrategyFactory(raft_log_pruning_strategy.DefaultValue, logProvider)).newInstance();

            return(new SegmentedRaftLog(fsa, dir, rotateAtSize, new DummyRaftableContentSerializer(), logProvider, readerPoolSize, Clocks.fakeClock(), new OnDemandJobScheduler(), pruningStrategy));
        }
Esempio n. 14
0
        private SegmentedRaftLog CreateRaftLog(long rotateAtSize)
        {
            LogProvider            logProvider     = Instance;
            CoreLogPruningStrategy pruningStrategy = (new CoreLogPruningStrategyFactory(raft_log_pruning_strategy.DefaultValue, logProvider)).newInstance();

            return(new SegmentedRaftLog(_fileSystemRule.get(), _testDirectory.directory(), rotateAtSize, new DummyRaftableContentSerializer(), logProvider, 0, Clocks.fakeClock(), new OnDemandJobScheduler(), pruningStrategy));
        }
Esempio n. 15
0
 private void InitializeInstanceFields()
 {
     _fsa        = FileSystemRule.get();
     _fileNames  = new FileNames(_root);
     _readerPool = new ReaderPool(0, Instance, _fileNames, _fsa, Clocks.fakeClock());
 }
Esempio n. 16
0
        private RecoveryProtocol CreateRecoveryProtocol()
        {
            FileNames fileNames = new FileNames(_logDirectory);

            return(new RecoveryProtocol(FsRule.get(), fileNames, new ReaderPool(8, Instance, fileNames, FsRule.get(), Clocks.fakeClock()), CoreReplicatedContentMarshal.marshaller(), Instance));
        }
Esempio n. 17
0
        private SegmentedRaftLog CreateRaftLog(long rotateAtSize)
        {
            File directory = new File(RAFT_LOG_DIRECTORY_NAME);

            _logDirectory = Dir.directory(directory.Name);

            LogProvider            logProvider     = Instance;
            CoreLogPruningStrategy pruningStrategy = (new CoreLogPruningStrategyFactory("100 entries", logProvider)).NewInstance();

            return(new SegmentedRaftLog(FsRule.get(), _logDirectory, rotateAtSize, CoreReplicatedContentMarshal.marshaller(), logProvider, 8, Clocks.fakeClock(), new OnDemandJobScheduler(), pruningStrategy));
        }
Esempio n. 18
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private org.neo4j.causalclustering.core.consensus.log.RaftLog createRaftLog() throws Exception
        private RaftLog CreateRaftLog()
        {
            File directory = new File(RAFT_LOG_DIRECTORY_NAME);
            FileSystemAbstraction fileSystem = new EphemeralFileSystemAbstraction();

            fileSystem.Mkdir(directory);

            LogProvider            logProvider     = Instance;
            CoreLogPruningStrategy pruningStrategy = (new CoreLogPruningStrategyFactory("1 entries", logProvider)).NewInstance();
            SegmentedRaftLog       newRaftLog      = new SegmentedRaftLog(fileSystem, directory, 1, new DummyRaftableContentSerializer(), logProvider, 8, Clocks.fakeClock(), new OnDemandJobScheduler(), pruningStrategy);

            newRaftLog.Start();
            return(newRaftLog);
        }