//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldAllowOutOfBoundsPruneIndex() throws Exception //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldAllowOutOfBoundsPruneIndex() { //Given a prune index of n, if the smallest value for a segment file is n+c, the pruning should not remove // any files and not result in a failure. Segments segments = new Segments(_fsa, _fileNames, _readerPool, _segmentFiles, _contentMarshal, _logProvider, -1); 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(); segments.Prune(11); segments.Rotate(20, 20, 3); // we will truncate this whole file away segments.Last().closeWriter(); //when SegmentFile oldestNotDisposed = segments.Prune(-1); //then SegmentHeader header = oldestNotDisposed.Header(); assertEquals(10, header.PrevFileLastIndex()); assertEquals(10, header.PrevIndex()); assertEquals(2, header.PrevTerm()); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldCreateNext() throws Exception //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldCreateNext() { // Given using (Segments segments = new Segments(_fsa, _fileNames, _readerPool, _segmentFiles, _contentMarshal, _logProvider, -1)) { // When segments.Rotate(10, 10, 12); segments.Last().closeWriter(); SegmentFile last = segments.Last(); // Then assertEquals(10, last.Header().prevFileLastIndex()); assertEquals(10, last.Header().prevIndex()); assertEquals(12, last.Header().prevTerm()); } }
//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()); }
//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(); } }
public override long Prune(long safeIndex) { long pruneIndex = _pruner.getIndexToPruneFrom(safeIndex, _state.segments); SegmentFile oldestNotDisposed = _state.segments.prune(pruneIndex); long newPrevIndex = oldestNotDisposed.Header().prevIndex(); long newPrevTerm = oldestNotDisposed.Header().prevTerm(); if (newPrevIndex > _state.prevIndex) { _state.prevIndex = newPrevIndex; } if (newPrevTerm > _state.prevTerm) { _state.prevTerm = newPrevTerm; } _state.terms.prune(_state.prevIndex); return(_state.prevIndex); }
internal virtual List <SegmentFile> CreateSegmentFiles(int size) { List <SegmentFile> list = new List <SegmentFile>(size); for (int i = 0; i < size; i++) { SegmentFile file = mock(typeof(SegmentFile)); when(file.Header()).thenReturn(TestSegmentHeader(i)); when(file.Size()).thenReturn(1L); list.Add(file); } return(list); }
public virtual SegmentFile Prune(long pruneIndex) { lock (this) { IEnumerator <SegmentFile> itr = _allSegments.GetEnumerator(); //JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops: SegmentFile notDisposed = itr.next(); // we should always leave at least one segment int firstRemaining = 0; while (itr.MoveNext()) { SegmentFile current = itr.Current; if (current.Header().prevFileLastIndex() > pruneIndex) { break; } if (!notDisposed.TryClose()) { break; } _log.info("Pruning %s", notDisposed); if (!notDisposed.Delete()) { _log.error("Failed to delete %s", notDisposed); break; } // TODO: Sync the parent directory. Also consider handling fs operations under its own lock. firstRemaining++; notDisposed = current; } _rangeMap.remove(notDisposed.Header().prevIndex() + 1); _allSegments.RemoveRange(0, firstRemaining); return(notDisposed); } }
//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); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldDeleteTruncatedFilesOnPrune() throws Exception //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldDeleteTruncatedFilesOnPrune() { // Given using (Segments segments = new Segments(_fsa, _fileNames, _readerPool, _segmentFiles, _contentMarshal, _logProvider, -1)) { SegmentFile toBePruned = segments.Rotate(-1, -1, -1); segments.Last().closeWriter(); // need to close writer otherwise dispose will not be called // we will truncate this whole file away SegmentFile toBeTruncated = segments.Rotate(10, 10, 2); segments.Last().closeWriter(); // When // We truncate a whole file segments.Truncate(20, 9, 4); // And we prune all files before that file segments.Prune(10); // Then // the truncate file is part of the deletes that happen while pruning verify(_fsa, times(_segmentFiles.Count)).deleteFile(_fileNames.getForVersion(toBePruned.Header().version())); } }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldDeleteOnPrune() throws Exception //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldDeleteOnPrune() { verifyZeroInteractions(_fsa); // Given using (Segments segments = new Segments(_fsa, _fileNames, _readerPool, _segmentFiles, _contentMarshal, _logProvider, -1)) { // this is version 0 and will be deleted on prune later SegmentFile toPrune = segments.Rotate(-1, -1, -1); segments.Last().closeWriter(); // need to close writer otherwise dispose will not be called segments.Rotate(10, 10, 2); segments.Last().closeWriter(); // ditto segments.Rotate(20, 20, 2); // When segments.Prune(11); verify(_fsa, times(_segmentFiles.Count)).deleteFile(_fileNames.getForVersion(toPrune.Header().version())); } }