Beispiel #1
0
 private void InitializeInstanceFields()
 {
     _fileNames    = new FileNames(_baseDirectory);
     _readerPool   = new ReaderPool(0, Instance, _fileNames, _fsa, Clocks.fakeClock());
     _fileA        = spy(new SegmentFile(_fsa, _fileNames.getForVersion(0), _readerPool, 0, _contentMarshal, _logProvider, _header));
     _fileB        = spy(new SegmentFile(_fsa, _fileNames.getForVersion(1), _readerPool, 1, _contentMarshal, _logProvider, _header));
     _segmentFiles = new IList <SegmentFile> {
         _fileA, _fileB
     };
 }
Beispiel #2
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)));
        }
Beispiel #3
0
//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()));
            }
        }
Beispiel #4
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private synchronized SegmentFile createNext(long prevFileLastIndex, long prevIndex, long prevTerm) throws java.io.IOException
        private SegmentFile CreateNext(long prevFileLastIndex, long prevIndex, long prevTerm)
        {
            lock (this)
            {
                _currentVersion++;
                SegmentHeader header = new SegmentHeader(prevFileLastIndex, _currentVersion, prevIndex, prevTerm);

                File        file    = _fileNames.getForVersion(_currentVersion);
                SegmentFile segment = SegmentFile.Create(_fileSystem, file, _readerPool, _currentVersion, _contentMarshal, _logProvider, header);
                // TODO: Force base directory... probably not possible using fsa.
                segment.Flush();

                _allSegments.Add(segment);
                _rangeMap.replaceFrom(prevIndex + 1, segment);

                return(segment);
            }
        }
Beispiel #5
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();
            }
        }
Beispiel #6
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);
        }
Beispiel #7
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private Reader createFor(long version) throws java.io.IOException
        private Reader CreateFor(long version)
        {
            return(new Reader(_fsa, _fileNames.getForVersion(version), version));
        }