Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldRecoverSeveralSkips() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldRecoverSeveralSkips()
        {
            // given
            CreateLogFile(_fsa, 10, 1, 1, 20, 9);
            CreateLogFile(_fsa, 100, 2, 2, 200, 99);
            CreateLogFile(_fsa, 1000, 3, 3, 2000, 999);

            RecoveryProtocol protocol = new RecoveryProtocol(_fsa, _fileNames, _readerPool, _contentMarshal, NullLogProvider.Instance);

            // when
            State state = protocol.Run();

            // then
            assertEquals(2000, state.PrevIndex);
            assertEquals(999, state.PrevTerm);

            assertEquals(-1, state.Terms.get(20));
            assertEquals(-1, state.Terms.get(200));
            assertEquals(-1, state.Terms.get(1999));

            assertEquals(999, state.Terms.get(2000));
            assertEquals(-1, state.Terms.get(2001));

            assertEquals(999, state.Terms.latest());
        }
Ejemplo n.º 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();
        }
Ejemplo n.º 5
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldRecoverEvenIfLastHeaderIsMissing() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldRecoverEvenIfLastHeaderIsMissing()
        {
            // given
            CreateLogFile(_fsa, -1, 0, 0, -1, -1);
            CreateEmptyLogFile(_fsa, 1);

            RecoveryProtocol protocol = new RecoveryProtocol(_fsa, _fileNames, _readerPool, _contentMarshal, NullLogProvider.Instance);

            // when
            protocol.Run();

            // then
            assertNotEquals(0, _fsa.getFileSize(_fileNames.getForVersion(1)));
        }
Ejemplo n.º 6
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldReturnEmptyStateOnEmptyDirectory() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldReturnEmptyStateOnEmptyDirectory()
        {
            // given
            RecoveryProtocol protocol = new RecoveryProtocol(_fsa, _fileNames, _readerPool, _contentMarshal, NullLogProvider.Instance);

            // when
            State state = protocol.Run();

            // then
            assertEquals(-1, state.AppendIndex);
            assertEquals(-1, state.Terms.latest());
            assertEquals(-1, state.PrevIndex);
            assertEquals(-1, state.PrevTerm);
            assertEquals(0, state.Segments.last().header().version());
        }
Ejemplo n.º 7
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldFailIfTheVersionNumberInTheHeaderAndFileNameDiffer() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldFailIfTheVersionNumberInTheHeaderAndFileNameDiffer()
        {
            // given
            CreateLogFile(_fsa, -1, 0, 1, -1, -1);

            RecoveryProtocol protocol = new RecoveryProtocol(_fsa, _fileNames, _readerPool, _contentMarshal, NullLogProvider.Instance);

            try
            {
                // when
                protocol.Run();
                fail("Expected an exception");
            }
            catch (DamagedLogStorageException)
            {
                // expected
            }
        }
Ejemplo n.º 8
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldRecoverAndBeAbleToSkip() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldRecoverAndBeAbleToSkip()
        {
            // given
            CreateLogFile(_fsa, -1, 0, 0, -1, -1);
            CreateLogFile(_fsa, 10, 1, 1, 10, 0);
            CreateLogFile(_fsa, 20, 2, 2, 20, 1);

            RecoveryProtocol protocol = new RecoveryProtocol(_fsa, _fileNames, _readerPool, _contentMarshal, NullLogProvider.Instance);

            // when
            State       state   = protocol.Run();
            SegmentFile newFile = state.Segments.skip(20, 40, 2);

            // then
            assertEquals(20, newFile.Header().prevFileLastIndex());
            assertEquals(3, newFile.Header().version());
            assertEquals(40, newFile.Header().prevIndex());
            assertEquals(2, newFile.Header().prevTerm());
        }
Ejemplo n.º 9
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldFailIfANonLastFileIsMissingHeader() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldFailIfANonLastFileIsMissingHeader()
        {
            // given
            CreateLogFile(_fsa, -1, 0, 0, -1, -1);
            CreateEmptyLogFile(_fsa, 1);
            CreateLogFile(_fsa, -1, 2, 2, -1, -1);

            RecoveryProtocol protocol = new RecoveryProtocol(_fsa, _fileNames, _readerPool, _contentMarshal, NullLogProvider.Instance);

            try
            {
                // when
                protocol.Run();
                fail("Expected an exception");
            }
            catch (DamagedLogStorageException)
            {
                // expected
            }
        }
Ejemplo n.º 10
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private int dump(String filenameOrDirectory, java.io.PrintStream out) throws java.io.IOException, DamagedLogStorageException, DisposedException
        private int Dump(string filenameOrDirectory, PrintStream @out)
        {
            LogProvider logProvider = NullLogProvider.Instance;

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int[] logsFound = {0};
            int[]            logsFound        = new int[] { 0 };
            FileNames        fileNames        = new FileNames(new File(filenameOrDirectory));
            ReaderPool       readerPool       = new ReaderPool(0, logProvider, fileNames, _fileSystem, Clocks.systemClock());
            RecoveryProtocol recoveryProtocol = new RecoveryProtocol(_fileSystem, fileNames, readerPool, _marshal, logProvider);
            Segments         segments         = recoveryProtocol.Run().Segments;

            segments.Visit(segment =>
            {
                logsFound[0]++;
                @out.println("=== " + segment.Filename + " ===");
                SegmentHeader header = segment.header();
                @out.println(header.ToString());
                try
                {
                    using (IOCursor <EntryRecord> cursor = segment.getCursor(header.PrevIndex() + 1))
                    {
                        while (cursor.next())
                        {
                            @out.println(cursor.get().ToString());
                        }
                    }
                }
                catch (Exception e) when(e is DisposedException || e is IOException)
                {
                    e.printStackTrace();
                    Environment.Exit(-1);
                    return(true);
                }
                return(false);
            });

            return(logsFound[0]);
        }
Ejemplo n.º 11
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void testRecoveryOfBootstrappedEntry(long bootstrapIndex, long bootstrapTerm) throws java.io.IOException, DamagedLogStorageException, DisposedException
        private void TestRecoveryOfBootstrappedEntry(long bootstrapIndex, long bootstrapTerm)
        {
            // given
            CreateLogFile(_fsa, -1, 0, 0, -1, -1);
            CreateLogFile(_fsa, -1, 1, 1, bootstrapIndex, bootstrapTerm);

            RecoveryProtocol protocol = new RecoveryProtocol(_fsa, _fileNames, _readerPool, _contentMarshal, NullLogProvider.Instance);

            // when
            State state = protocol.Run();

            // then
            assertEquals(bootstrapIndex, state.PrevIndex);
            assertEquals(bootstrapTerm, state.PrevTerm);

            assertEquals(-1, state.Terms.get(-1));
            assertEquals(-1, state.Terms.get(bootstrapIndex - 1));
            assertEquals(bootstrapTerm, state.Terms.get(bootstrapIndex));
            assertEquals(-1, state.Terms.get(bootstrapIndex + 1));

            assertEquals(bootstrapTerm, state.Terms.latest());
        }