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 attemptsPruningUntilOpenFileIsFound() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void AttemptsPruningUntilOpenFileIsFound()
        {
            /// <summary>
            /// prune stops attempting to prune files after finding one that is open.
            /// </summary>

            // Given
            Segments segments = new Segments(_fsa, _fileNames, _readerPool, Collections.emptyList(), _contentMarshal, _logProvider, -1);

            /*
             * create 0
             * create 1
             * create 2
             * create 3
             *
             * closeWriter on all
             * create reader on 1
             * prune on 3
             *
             * only 0 should be deleted
             */

            segments.Rotate(-1, -1, -1);
            segments.Last().closeWriter();              // need to close writer otherwise dispose will not be called

            segments.Rotate(10, 10, 2);                 // we will truncate this whole file away
            segments.Last().closeWriter();              // need to close writer otherwise dispose will not be called
            IOCursor <EntryRecord> reader = segments.Last().getCursor(11);

            segments.Rotate(20, 20, 3);                 // we will truncate this whole file away
            segments.Last().closeWriter();

            segments.Rotate(30, 30, 4);                 // we will truncate this whole file away
            segments.Last().closeWriter();

            segments.Prune(31);

            //when
            OpenEndRangeMap.ValueRange <long, SegmentFile> shouldBePruned        = segments.GetForIndex(5);
            OpenEndRangeMap.ValueRange <long, SegmentFile> shouldNotBePruned     = segments.GetForIndex(15);
            OpenEndRangeMap.ValueRange <long, SegmentFile> shouldAlsoNotBePruned = segments.GetForIndex(25);

            //then
            assertFalse(shouldBePruned.Value().Present);
            assertTrue(shouldNotBePruned.Value().Present);
            assertTrue(shouldAlsoNotBePruned.Value().Present);

            //when
            reader.close();
            segments.Prune(31);

            shouldBePruned        = segments.GetForIndex(5);
            shouldNotBePruned     = segments.GetForIndex(15);
            shouldAlsoNotBePruned = segments.GetForIndex(25);

            //then
            assertFalse(shouldBePruned.Value().Present);
            assertFalse(shouldNotBePruned.Value().Present);
            assertFalse(shouldAlsoNotBePruned.Value().Present);
        }
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 shouldBeAbleToRepeatedlyReadWrittenValues() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldBeAbleToRepeatedlyReadWrittenValues()
        {
            using (SegmentFile segment = create(FsRule.get(), _fileNames.getForVersion(0), _readerPool, 0, _contentMarshal, _logProvider, _segmentHeader))
            {
                // given
                segment.Write(0, _entry1);
                segment.Write(1, _entry2);
                segment.Write(2, _entry3);
                segment.Flush();

                for (int i = 0; i < 3; i++)
                {
                    // when
                    IOCursor <EntryRecord> cursor = segment.GetCursor(0);

                    // then
                    assertTrue(cursor.next());
                    assertEquals(_entry1, cursor.get().logEntry());
                    assertTrue(cursor.next());
                    assertEquals(_entry2, cursor.get().logEntry());
                    assertTrue(cursor.next());
                    assertEquals(_entry3, cursor.get().logEntry());
                    assertFalse(cursor.next());

                    cursor.close();
                }
            }
        }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public org.neo4j.kernel.impl.transaction.log.TransactionMetadataCache.TransactionMetadata getMetadataFor(long transactionId) throws java.io.IOException
        public override TransactionMetadata GetMetadataFor(long transactionId)
        {
            if (transactionId <= BASE_TX_ID)
            {
                return(_metadataForEmptyStore);
            }

            TransactionMetadata transactionMetadata = _transactionMetadataCache.getTransactionMetadata(transactionId);

            if (transactionMetadata == null)
            {
                using (IOCursor <CommittedTransactionRepresentation> cursor = GetTransactions(transactionId))
                {
                    while (cursor.next())
                    {
                        CommittedTransactionRepresentation tx = cursor.get();
                        LogEntryCommit      commitEntry       = tx.CommitEntry;
                        long                committedTxId     = commitEntry.TxId;
                        long                timeWritten       = commitEntry.TimeWritten;
                        TransactionMetadata metadata          = _transactionMetadataCache.cacheTransactionMetadata(committedTxId, tx.StartEntry.StartPosition, tx.StartEntry.MasterId, tx.StartEntry.LocalId, LogEntryStart.checksum(tx.StartEntry), timeWritten);
                        if (committedTxId == transactionId)
                        {
                            transactionMetadata = metadata;
                        }
                    }
                }
                if (transactionMetadata == null)
                {
                    throw new NoSuchTransactionException(transactionId);
                }
            }

            return(transactionMetadata);
        }
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 shouldNotReturnReaderExperiencingErrorToPool() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldNotReturnReaderExperiencingErrorToPool()
        {
            // given
            StoreChannel channel    = mock(typeof(StoreChannel));
            Reader       reader     = mock(typeof(Reader));
            ReaderPool   readerPool = mock(typeof(ReaderPool));

            when(channel.read(any(typeof(ByteBuffer)))).thenThrow(new IOException());
            when(reader.Channel()).thenReturn(channel);
            when(readerPool.Acquire(anyLong(), anyLong())).thenReturn(reader);

            using (SegmentFile segment = create(FsRule.get(), _fileNames.getForVersion(0), readerPool, 0, _contentMarshal, _logProvider, _segmentHeader))
            {
                // given
                IOCursor <EntryRecord> cursor = segment.GetCursor(0);

                try
                {
                    cursor.next();
                    fail();
                }
                catch (IOException)
                {
                    // expected from mocking
                }

                // when
                cursor.close();

                // then
                verify(readerPool, never()).release(reader);
                verify(reader).close();
            }
        }
Ejemplo n.º 5
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private boolean nextSegment() throws java.io.IOException
        private bool NextSegment()
        {
            _segmentRange = _segments.getForIndex(_currentIndex);
            Optional <SegmentFile> optionalFile = _segmentRange.value();

            if (!optionalFile.Present)
            {
                _currentRecord.invalidate();
                return(false);
            }

            SegmentFile file = optionalFile.get();

            /* Open new reader before closing old, so that pruner cannot overtake us. */
            IOCursor <EntryRecord> oldCursor = _cursor;

            try
            {
                _cursor = file.GetCursor(_currentIndex);
            }
            catch (DisposedException)
            {
                _currentRecord.invalidate();
                return(false);
            }

            if (oldCursor != null)
            {
                oldCursor.close();
            }

            _limit = _segmentRange.limit().GetValueOrDefault(long.MaxValue);

            return(true);
        }
Ejemplo n.º 6
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: protected void extractTransactions(long startingAtTransactionId, org.neo4j.helpers.collection.Visitor<org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation,Exception> visitor) throws Exception
        protected internal virtual void ExtractTransactions(long startingAtTransactionId, Visitor <CommittedTransactionRepresentation, Exception> visitor)
        {
            using (IOCursor <CommittedTransactionRepresentation> cursor = TransactionStore.getTransactions(startingAtTransactionId))
            {
                while (cursor.next() && !visitor.Visit(cursor.get()))
                {
                }
            }
        }
Ejemplo n.º 7
0
 internal ChunkedTransactionStream(Log log, StoreId storeId, long firstTxId, long txIdPromise, IOCursor <CommittedTransactionRepresentation> txCursor, CatchupServerProtocol protocol)
 {
     this._log          = log;
     this._storeId      = storeId;
     this._expectedTxId = firstTxId;
     this._txIdPromise  = txIdPromise;
     this._txCursor     = txCursor;
     this._protocol     = protocol;
 }
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 shouldReportCorrectInitialValues() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldReportCorrectInitialValues()
        {
            using (SegmentFile segment = create(FsRule.get(), _fileNames.getForVersion(0), _readerPool, _version, _contentMarshal, _logProvider, _segmentHeader))
            {
                assertEquals(0, segment.Header().version());

                IOCursor <EntryRecord> cursor = segment.GetCursor(0);
                assertFalse(cursor.next());

                cursor.close();
            }
        }
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 shouldPruneReaderPoolOnClose() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldPruneReaderPoolOnClose()
        {
            using (SegmentFile segment = create(FsRule.get(), _fileNames.getForVersion(0), _readerPool, 0, _contentMarshal, _logProvider, _segmentHeader))
            {
                segment.Write(0, _entry1);
                segment.Flush();
                segment.CloseWriter();

                IOCursor <EntryRecord> cursor = segment.GetCursor(0);
                cursor.next();
                cursor.close();
            }

            verify(_readerPool).prune(0);
        }
Ejemplo n.º 10
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldHaveIdempotentCloseMethods() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldHaveIdempotentCloseMethods()
        {
            // given
            SegmentFile            segment = create(FsRule.get(), _fileNames.getForVersion(0), _readerPool, 0, _contentMarshal, _logProvider, _segmentHeader);
            IOCursor <EntryRecord> cursor  = segment.GetCursor(0);

            // when
            segment.CloseWriter();
            cursor.close();

            // then
            assertTrue(segment.TryClose());
            segment.Close();
            assertTrue(segment.TryClose());
            segment.Close();
        }
Ejemplo n.º 11
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private long applyTransactions(java.io.File fromPath, org.neo4j.kernel.internal.GraphDatabaseAPI toDb, org.neo4j.kernel.configuration.Config toConfig, long fromTxExclusive, long toTxInclusive, java.io.PrintStream out) throws Exception
		 private long ApplyTransactions( File fromPath, GraphDatabaseAPI toDb, Config toConfig, long fromTxExclusive, long toTxInclusive, PrintStream @out )
		 {
			  DependencyResolver resolver = toDb.DependencyResolver;
			  TransactionRepresentationCommitProcess commitProcess = new TransactionRepresentationCommitProcess( resolver.ResolveDependency( typeof( TransactionAppender ) ), resolver.ResolveDependency( typeof( StorageEngine ) ) );
			  LifeSupport life = new LifeSupport();
			  try
			  {
					  using ( DefaultFileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction(), JobScheduler jobScheduler = createInitialisedScheduler(), PageCache pageCache = StandalonePageCacheFactory.createPageCache(fileSystem, jobScheduler) )
					  {
						LogicalTransactionStore source = life.Add( new ReadOnlyTransactionStore( pageCache, fileSystem, DatabaseLayout.of( fromPath ), Config.defaults(), new Monitors() ) );
						life.Start();
						long lastAppliedTx = fromTxExclusive;
						// Some progress if there are more than a couple of transactions to apply
						ProgressListener progress = toTxInclusive - fromTxExclusive >= 100 ? textual( @out ).singlePart( "Application progress", toTxInclusive - fromTxExclusive ) : Org.Neo4j.Helpers.progress.ProgressListener_Fields.None;
						using ( IOCursor<CommittedTransactionRepresentation> cursor = source.GetTransactions( fromTxExclusive + 1 ) )
						{
							 while ( cursor.next() )
							 {
								  CommittedTransactionRepresentation transaction = cursor.get();
								  TransactionRepresentation transactionRepresentation = transaction.TransactionRepresentation;
								  try
								  {
										commitProcess.Commit( new TransactionToApply( transactionRepresentation ), NULL, EXTERNAL );
										progress.Add( 1 );
								  }
//JAVA TO C# CONVERTER WARNING: 'final' catch parameters are not available in C#:
//ORIGINAL LINE: catch (final Throwable e)
								  catch ( Exception e )
								  {
										Console.Error.WriteLine( "ERROR applying transaction " + transaction.CommitEntry.TxId );
										throw e;
								  }
								  lastAppliedTx = transaction.CommitEntry.TxId;
								  if ( lastAppliedTx == toTxInclusive )
								  {
										break;
								  }
							 }
						}
						return lastAppliedTx;
					  }
			  }
			  finally
			  {
					life.Shutdown();
			  }
		 }
Ejemplo n.º 12
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: protected void channelRead0(io.netty.channel.ChannelHandlerContext ctx, final TxPullRequest msg) throws Exception
//JAVA TO C# CONVERTER WARNING: 'final' parameters are ignored unless the option to convert to C# 7.2 'in' parameters is selected:
        protected internal override void ChannelRead0(ChannelHandlerContext ctx, TxPullRequest msg)
        {
            _monitor.increment();

            if (msg.PreviousTxId() <= 0)
            {
                _log.error("Illegal tx pull request");
                EndInteraction(ctx, E_INVALID_REQUEST, -1);
                return;
            }

            StoreId localStoreId    = _storeIdSupplier.get();
            StoreId expectedStoreId = msg.ExpectedStoreId();

            long firstTxId = msg.PreviousTxId() + 1;

            /*
             * This is the minimum transaction id we must send to consider our streaming operation successful. The kernel can
             * concurrently prune even future transactions while iterating and the cursor will silently fail on iteration, so
             * we need to add our own protection for this reason and also as a generally important sanity check for the fulfillment
             * of the consistent recovery contract which requires us to stream transactions at least as far as the time when the
             * file copy operation completed.
             */
            long txIdPromise = _transactionIdStore.LastCommittedTransactionId;
            IOCursor <CommittedTransactionRepresentation> txCursor = GetCursor(txIdPromise, ctx, firstTxId, localStoreId, expectedStoreId);

            if (txCursor != null)
            {
                ChunkedTransactionStream txStream = new ChunkedTransactionStream(_log, localStoreId, firstTxId, txIdPromise, txCursor, _protocol);
                // chunked transaction stream ends the interaction internally and closes the cursor
                ctx.writeAndFlush(txStream).addListener(f =>
                {
                    if (_log.DebugEnabled || !f.Success)
                    {
                        string message = format("Streamed transactions [%d--%d] to %s", firstTxId, txStream.LastTxId(), ctx.channel().remoteAddress());
                        if (f.Success)
                        {
                            _log.debug(message);
                        }
                        else
                        {
                            _log.warn(message, f.cause());
                        }
                    }
                });
            }
        }
Ejemplo n.º 13
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldBeAbleToWriteAndRead() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldBeAbleToWriteAndRead()
        {
            using (SegmentFile segment = create(FsRule.get(), _fileNames.getForVersion(0), _readerPool, 0, _contentMarshal, _logProvider, _segmentHeader))
            {
                // given
                segment.Write(0, _entry1);
                segment.Flush();

                // when
                IOCursor <EntryRecord> cursor = segment.GetCursor(0);

                // then
                assertTrue(cursor.next());
                assertEquals(_entry1, cursor.get().logEntry());

                cursor.close();
            }
        }
Ejemplo n.º 14
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldCatchDoubleCloseReaderErrors() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldCatchDoubleCloseReaderErrors()
        {
            try
            {
                using (SegmentFile segment = create(FsRule.get(), _fileNames.getForVersion(0), _readerPool, 0, _contentMarshal, _logProvider, _segmentHeader))
                {
                    // given
                    IOCursor <EntryRecord> cursor = segment.GetCursor(0);

                    cursor.close();
                    cursor.close();
                    fail("Should have caught double close error");
                }
            }
            catch (System.InvalidOperationException)
            {
                // expected
            }
        }
Ejemplo n.º 15
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldCallDisposeHandlerAfterLastReaderIsClosed() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldCallDisposeHandlerAfterLastReaderIsClosed()
        {
            using (SegmentFile segment = create(FsRule.get(), _fileNames.getForVersion(0), _readerPool, 0, _contentMarshal, _logProvider, _segmentHeader))
            {
                // given
                IOCursor <EntryRecord> cursor0 = segment.GetCursor(0);
                IOCursor <EntryRecord> cursor1 = segment.GetCursor(0);

                // when
                segment.CloseWriter();
                cursor0.close();

                // then
                assertFalse(segment.TryClose());

                // when
                cursor1.close();

                // then
                assertTrue(segment.TryClose());
            }
        }
Ejemplo n.º 16
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldHandleReaderPastEndCorrectly() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldHandleReaderPastEndCorrectly()
        {
            using (SegmentFile segment = create(FsRule.get(), _fileNames.getForVersion(0), _readerPool, 0, _contentMarshal, _logProvider, _segmentHeader))
            {
                // given
                segment.Write(0, _entry1);
                segment.Write(1, _entry2);
                segment.Flush();
                segment.CloseWriter();

                IOCursor <EntryRecord> cursor = segment.GetCursor(3);

                // then
                assertFalse(cursor.next());

                // when
                cursor.close();

                // then
                assertTrue(segment.TryClose());
            }
        }
Ejemplo n.º 17
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.º 18
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private static void read(org.neo4j.io.fs.FileSystemAbstraction fileSystem, String cmd, org.neo4j.kernel.impl.store.NeoStores neoStores, org.neo4j.io.layout.DatabaseLayout databaseLayout) throws java.io.IOException
        private static void Read(FileSystemAbstraction fileSystem, string cmd, NeoStores neoStores, DatabaseLayout databaseLayout)
        {
            Matcher matcher = _readCommandPattern.matcher(cmd);

            if (matcher.find())
            {
                string  lower   = matcher.group("lower");
                string  upper   = matcher.group("upper");
                string  fname   = matcher.group("fname");
                string  regex   = matcher.group("regex");
                Pattern pattern = !string.ReferenceEquals(regex, null) ? Pattern.compile(regex) : null;
                long    fromId  = !string.ReferenceEquals(lower, null) ? long.Parse(lower) : 0L;
                long    toId    = !string.ReferenceEquals(upper, null) ? long.Parse(upper) : long.MaxValue;

                RecordStore store = GetStore(fname, neoStores);
                if (store != null)
                {
                    ReadStore(fileSystem, store, fromId, toId, pattern);
                    return;
                }

                IOCursor <LogEntry> cursor = GetLogCursor(fileSystem, fname, databaseLayout);
                if (cursor != null)
                {
                    ReadLog(cursor, fromId, toId, pattern);
                    cursor.close();
                    return;
                }

                _console.printf("don't know how to read '%s'%n", fname);
            }
            else
            {
                _console.printf("bad read command format%n");
            }
        }
Ejemplo n.º 19
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private static void readLog(org.neo4j.cursor.IOCursor<org.neo4j.kernel.impl.transaction.log.entry.LogEntry> cursor, final long fromLine, final long toLine, final java.util.regex.Pattern pattern) throws java.io.IOException
//JAVA TO C# CONVERTER WARNING: 'final' parameters are ignored unless the option to convert to C# 7.2 'in' parameters is selected:
        private static void ReadLog(IOCursor <LogEntry> cursor, long fromLine, long toLine, Pattern pattern)
        {
            TimeZone timeZone  = TimeZone.Default;
            long     lineCount = -1;

            while (cursor.next())
            {
                LogEntry logEntry = cursor.get();
                lineCount++;
                if (lineCount > toLine)
                {
                    return;
                }
                if (lineCount < fromLine)
                {
                    continue;
                }
                string str = logEntry.ToString(timeZone);
                if (pattern == null || pattern.matcher(str).find())
                {
                    _console.printf("%s%n", str);
                }
            }
        }
Ejemplo n.º 20
0
		 public TransactionLogEntryCursor( IOCursor<LogEntry> @delegate )
		 {
			  this.@delegate = @delegate;
		 }
Ejemplo n.º 21
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: State run() throws java.io.IOException, DamagedLogStorageException, DisposedException
        internal virtual State Run()
        {
            State state = new State();
            SortedDictionary <long, File> files = _fileNames.getAllFiles(_fileSystem, _log);

            if (Files.SetOfKeyValuePairs().Empty)
            {
                state.Segments = new Segments(_fileSystem, _fileNames, _readerPool, emptyList(), _contentMarshal, _logProvider, -1);
                state.Segments.rotate(-1, -1, -1);
                state.Terms = new Terms(-1, -1);
                return(state);
            }

            IList <SegmentFile> segmentFiles = new List <SegmentFile>();
            SegmentFile         segment      = null;

            long expectedVersion       = Files.firstKey();
            bool mustRecoverLastHeader = false;
            bool skip = true;               // the first file is treated the same as a skip

            foreach (KeyValuePair <long, File> entry in Files.SetOfKeyValuePairs())
            {
                long          fileNameVersion = entry.Key;
                File          file            = entry.Value;
                SegmentHeader header;

                CheckVersionSequence(fileNameVersion, expectedVersion);

                try
                {
                    header = LoadHeader(_fileSystem, file);
                    CheckVersionMatches(header.Version(), fileNameVersion);
                }
                catch (EndOfStreamException e)
                {
                    if (Files.lastKey() != fileNameVersion)
                    {
                        throw new DamagedLogStorageException(e, "Intermediate file with incomplete or no header found: %s", file);
                    }
                    else if (Files.Count == 1)
                    {
                        throw new DamagedLogStorageException(e, "Single file with incomplete or no header found: %s", file);
                    }

                    /* Last file header must be recovered by scanning next-to-last file and writing a new header based on that. */
                    mustRecoverLastHeader = true;
                    break;
                }

                segment = new SegmentFile(_fileSystem, file, _readerPool, fileNameVersion, _contentMarshal, _logProvider, header);
                segmentFiles.Add(segment);

                if (segment.Header().prevIndex() != segment.Header().prevFileLastIndex())
                {
                    _log.info(format("Skipping from index %d to %d.", segment.Header().prevFileLastIndex(), segment.Header().prevIndex() + 1));
                    skip = true;
                }

                if (skip)
                {
                    state.PrevIndex = segment.Header().prevIndex();
                    state.PrevTerm  = segment.Header().prevTerm();
                    skip            = false;
                }

                expectedVersion++;
            }

            Debug.Assert(segment != null);

            state.AppendIndex = segment.Header().prevIndex();
            state.Terms       = new Terms(segment.Header().prevIndex(), segment.Header().prevTerm());

            using (IOCursor <EntryRecord> cursor = segment.GetCursor(segment.Header().prevIndex() + 1))
            {
                while (cursor.next())
                {
                    EntryRecord entry = cursor.get();
                    state.AppendIndex = entry.LogIndex();
                    state.Terms.append(state.AppendIndex, entry.LogEntry().term());
                }
            }

            if (mustRecoverLastHeader)
            {
                SegmentHeader header = new SegmentHeader(state.AppendIndex, expectedVersion, state.AppendIndex, state.Terms.latest());
                _log.warn("Recovering last file based on next-to-last file. " + header);

                File file = _fileNames.getForVersion(expectedVersion);
                WriteHeader(_fileSystem, file, header);

                segment = new SegmentFile(_fileSystem, file, _readerPool, expectedVersion, _contentMarshal, _logProvider, header);
                segmentFiles.Add(segment);
            }

            state.Segments = new Segments(_fileSystem, _fileNames, _readerPool, segmentFiles, _contentMarshal, _logProvider, segment.Header().version());

            return(state);
        }
Ejemplo n.º 22
0
 internal SegmentedRaftLogCursor(long fromIndex, IOCursor <EntryRecord> inner)
 {
     this._inner   = inner;
     this._current = new CursorValue <RaftLogEntry>();
     this._index   = fromIndex - 1;
 }
Ejemplo n.º 23
0
 public FilteringIOCursor(IOCursor <T> @delegate, System.Predicate <T> toKeep)
 {
     this.@delegate = @delegate;
     this._toKeep   = toKeep;
 }
Ejemplo n.º 24
0
 public IOCursorAsResourceIterable(IOCursor <T> cursor)
 {
     this._cursor = cursor;
 }