Exemple #1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void incompleteEntriesAtTheEndShouldNotCauseFailures() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void IncompleteEntriesAtTheEndShouldNotCauseFailures()
        {
            // Given
            // we use a RaftLog to create a raft log file and then we will start chopping bits off from the end
            SegmentedRaftLog raftLog = CreateRaftLog(100_000);

            raftLog.Start();

            // Add a bunch of entries, preferably one of each available kind.
            raftLog.Append(new RaftLogEntry(4, new NewLeaderBarrier()));
            raftLog.Append(new RaftLogEntry(4, new ReplicatedIdAllocationRequest(new MemberId(System.Guid.randomUUID()), IdType.RELATIONSHIP, 1, 1024)));
            raftLog.Append(new RaftLogEntry(4, new ReplicatedIdAllocationRequest(new MemberId(System.Guid.randomUUID()), IdType.RELATIONSHIP, 1025, 1024)));
            raftLog.Append(new RaftLogEntry(4, new ReplicatedLockTokenRequest(new MemberId(System.Guid.randomUUID()), 1)));
            raftLog.Append(new RaftLogEntry(4, new NewLeaderBarrier()));
            raftLog.Append(new RaftLogEntry(5, new ReplicatedTokenRequest(TokenType.LABEL, "labelToken", new sbyte[] { 1, 2, 3 })));
            raftLog.Append(new RaftLogEntry(5, ReplicatedTransaction.from(new sbyte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 })));

            raftLog.Stop();

            // We use a temporary RecoveryProtocol to get the file to chop
            RecoveryProtocol recovery      = CreateRecoveryProtocol();
            State            recoveryState = Recovery.run();
            string           logFilename   = recoveryState.Segments.last().Filename;

            recoveryState.Segments.close();
            File logFile = new File(_logDirectory, logFilename);

            // When
            // We remove any number of bytes from the end (up to but not including the header) and try to recover
            // Then
            // No exceptions should be thrown
            TruncateAndRecover(logFile, SegmentHeader.Size);
        }
Exemple #2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void incompleteHeaderOfLastOfMoreThanOneLogFilesShouldNotCauseFailure() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void IncompleteHeaderOfLastOfMoreThanOneLogFilesShouldNotCauseFailure()
        {
            // Given
            // we use a RaftLog to create two log files, in order to chop the header of the second
            SegmentedRaftLog raftLog = CreateRaftLog(1);

            raftLog.Start();

            raftLog.Append(new RaftLogEntry(4, new NewLeaderBarrier()));                   // will cause rotation

            raftLog.Stop();

            // We use a temporary RecoveryProtocol to get the file to chop
            RecoveryProtocol recovery      = CreateRecoveryProtocol();
            State            recoveryState = Recovery.run();
            string           logFilename   = recoveryState.Segments.last().Filename;

            recoveryState.Segments.close();
            File logFile = new File(_logDirectory, logFilename);

            // When
            // We remove any number of bytes from the end of the second file and try to recover
            // Then
            // No exceptions should be thrown
            TruncateAndRecover(logFile, 0);
        }
Exemple #3
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldRotateOnAppendWhenRotateSizeIsReached() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldRotateOnAppendWhenRotateSizeIsReached()
        {
            // When
            SegmentedRaftLog log = _life.add(CreateRaftLog(ROTATE_AT_SIZE_IN_BYTES));

            log.Append(new RaftLogEntry(0, ReplicatedStringOfBytes(ROTATE_AT_SIZE_IN_BYTES)));

            // Then
            File[] files = _fileSystemRule.get().listFiles(_testDirectory.directory(), (dir, name) => name.StartsWith("raft"));
            assertEquals(2, Files.Length);
        }
Exemple #4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldNotAppendAtTheEndOfLogFileWithIncompleteEntries() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldNotAppendAtTheEndOfLogFileWithIncompleteEntries()
        {
            // Given
            // we use a RaftLog to create a raft log file and then we will chop some bits off the end
            SegmentedRaftLog raftLog = CreateRaftLog(100_000);

            raftLog.Start();

            raftLog.Append(new RaftLogEntry(4, new NewLeaderBarrier()));

            raftLog.Stop();

            // We use a temporary RecoveryProtocol to get the file to chop
            RecoveryProtocol recovery      = CreateRecoveryProtocol();
            State            recoveryState = Recovery.run();
            string           logFilename   = recoveryState.Segments.last().Filename;

            recoveryState.Segments.close();
            File         logFile     = new File(_logDirectory, logFilename);
            StoreChannel lastFile    = FsRule.get().open(logFile, OpenMode.READ_WRITE);
            long         currentSize = lastFile.size();

            lastFile.close();

            // When
            // We induce an incomplete entry at the end of the last file
            lastFile = FsRule.get().open(logFile, OpenMode.READ_WRITE);
            lastFile.Truncate(currentSize - 1);
            lastFile.close();

            // We start the raft log again, on the previous log file with truncated last entry.
            raftLog = CreateRaftLog(100_000);

            //  Recovery will run here
            raftLog.Start();

            // Append an entry
            raftLog.Append(new RaftLogEntry(4, new NewLeaderBarrier()));

            // Then
            // The log should report as containing only the last entry we've appended
            using (RaftLogCursor entryCursor = raftLog.GetEntryCursor(0))
            {
                // There should be exactly one entry, of type NewLeaderBarrier
                assertTrue(entryCursor.Next());
                RaftLogEntry raftLogEntry = entryCursor.get();
                assertEquals(typeof(NewLeaderBarrier), raftLogEntry.Content().GetType());
                assertFalse(entryCursor.Next());
            }
            raftLog.Stop();
        }
Exemple #5
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);
        }
Exemple #6
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldReturnFalseOnCursorForEntryThatWasPruned() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldReturnFalseOnCursorForEntryThatWasPruned()
        {
            //given
            SegmentedRaftLog segmentedRaftLog = CreateRaftLog(1, "keep_none");
            long             firstIndex       = segmentedRaftLog.Append(new RaftLogEntry(1, ReplicatedInteger.valueOf(1)));

            segmentedRaftLog.Append(new RaftLogEntry(2, ReplicatedInteger.valueOf(2)));
            long lastIndex = segmentedRaftLog.Append(new RaftLogEntry(3, ReplicatedInteger.valueOf(3)));

            //when
            segmentedRaftLog.Prune(firstIndex);
            RaftLogCursor entryCursor = segmentedRaftLog.GetEntryCursor(firstIndex);
            bool          next        = entryCursor.Next();

            //then
            assertFalse(next);
        }
Exemple #7
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: protected org.neo4j.causalclustering.core.consensus.log.RaftLog createRaftLog() throws Throwable
        protected internal override RaftLog CreateRaftLog()
        {
            FileSystemAbstraction fsa = FsRule.get();

            File directory = new File(RAFT_LOG_DIRECTORY_NAME);

            fsa.Mkdir(directory);

            long rotateAtSizeBytes = 128;
            int  readerPoolSize    = 8;

            LogProvider            logProvider     = Instance;
            CoreLogPruningStrategy pruningStrategy = (new CoreLogPruningStrategyFactory(raft_log_pruning_strategy.DefaultValue, logProvider)).newInstance();
            SegmentedRaftLog       newRaftLog      = new SegmentedRaftLog(fsa, directory, rotateAtSizeBytes, new DummyRaftableContentSerializer(), logProvider, readerPoolSize, Clocks.systemClock(), new OnDemandJobScheduler(), pruningStrategy);

            newRaftLog.Init();
            newRaftLog.Start();

            return(newRaftLog);
        }
Exemple #8
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldBeAbleToRecoverToLatestStateAfterRotation() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldBeAbleToRecoverToLatestStateAfterRotation()
        {
            // Given
            int  term = 0;
            long indexToRestoreTo;

            using (Lifespan lifespan = new Lifespan())
            {
                SegmentedRaftLog log = lifespan.Add(CreateRaftLog(ROTATE_AT_SIZE_IN_BYTES));
                log.Append(new RaftLogEntry(term, ReplicatedStringOfBytes(ROTATE_AT_SIZE_IN_BYTES - 40)));
                indexToRestoreTo = log.Append(new RaftLogEntry(term, ReplicatedInteger.valueOf(1)));
            }

            // When
            SegmentedRaftLog log = _life.add(CreateRaftLog(ROTATE_AT_SIZE_IN_BYTES));

            // Then
            assertEquals(indexToRestoreTo, log.AppendIndex());
            assertEquals(term, log.ReadEntryTerm(indexToRestoreTo));
        }
Exemple #9
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldReturnFalseOnCursorForEntryThatDoesntExist() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldReturnFalseOnCursorForEntryThatDoesntExist()
        {
            //given
            SegmentedRaftLog segmentedRaftLog = CreateRaftLog(1);

            segmentedRaftLog.Append(new RaftLogEntry(1, ReplicatedInteger.valueOf(1)));
            segmentedRaftLog.Append(new RaftLogEntry(2, ReplicatedInteger.valueOf(2)));
            long lastIndex = segmentedRaftLog.Append(new RaftLogEntry(3, ReplicatedInteger.valueOf(3)));

            //when
            bool next;

            using (RaftLogCursor entryCursor = segmentedRaftLog.GetEntryCursor(lastIndex + 1))
            {
                next = entryCursor.Next();
            }

            //then
            assertFalse(next);
        }
Exemple #10
0
        private SegmentedRaftLog CreateRaftLog(long rotateAtSize, string pruneStrategy)
        {
            if (_fileSystem == null)
            {
                _fileSystem = new EphemeralFileSystemAbstraction();
            }

            File directory = new File(RAFT_LOG_DIRECTORY_NAME);

            _fileSystem.mkdir(directory);

            LogProvider            logProvider     = Instance;
            CoreLogPruningStrategy pruningStrategy = (new CoreLogPruningStrategyFactory(pruneStrategy, logProvider)).NewInstance();
            SegmentedRaftLog       newRaftLog      = new SegmentedRaftLog(_fileSystem, directory, rotateAtSize, new DummyRaftableContentSerializer(), logProvider, 8, Clocks.systemClock(), new OnDemandJobScheduler(), pruningStrategy);

            _life.add(newRaftLog);
            _life.init();
            _life.start();

            return(newRaftLog);
        }