예제 #1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldThrowExceptionWhenReadingIncompleteHeader() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldThrowExceptionWhenReadingIncompleteHeader()
        {
            // given
            long prevFileLastIndex = 1;
            long version           = 2;
            long prevIndex         = 3;
            long prevTerm          = 4;

            SegmentHeader           writtenHeader = new SegmentHeader(prevFileLastIndex, version, prevIndex, prevTerm);
            InMemoryClosableChannel channel       = new InMemoryClosableChannel();

            channel.PutLong(writtenHeader.Version());
            channel.PutLong(writtenHeader.PrevIndex());

            // when
            try
            {
                _marshal.unmarshal(channel);
                fail();
            }
            catch (EndOfStreamException)
            {
                // expected
            }
        }
예제 #2
0
//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());
        }
예제 #3
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private static void writeHeader(org.neo4j.io.fs.FileSystemAbstraction fileSystem, java.io.File file, SegmentHeader header) throws java.io.IOException
        private static void WriteHeader(FileSystemAbstraction fileSystem, File file, SegmentHeader header)
        {
            using (StoreChannel channel = fileSystem.Open(file, OpenMode.READ_WRITE))
            {
                channel.Position(0);
                PhysicalFlushableChannel writer = new PhysicalFlushableChannel(channel, SegmentHeader.Size);
                _headerMarshal.marshal(header, writer);
                writer.PrepareForFlush().flush();
            }
        }
예제 #4
0
        internal SegmentFile(FileSystemAbstraction fileSystem, File file, ReaderPool readerPool, long version, ChannelMarshal <ReplicatedContent> contentMarshal, LogProvider logProvider, SegmentHeader header)
        {
            this._fileSystem     = fileSystem;
            this._file           = file;
            this._readerPool     = readerPool;
            this._contentMarshal = contentMarshal;
            this._header         = header;
            this._version        = version;

            this._positionCache = new PositionCache();
            this._refCount      = new ReferenceCounter();

            this._log = logProvider.getLog(this.GetType());
        }
예제 #5
0
        public override bool Equals(object o)
        {
            if (this == o)
            {
                return(true);
            }
            if (o == null || this.GetType() != o.GetType())
            {
                return(false);
            }
            SegmentHeader that = ( SegmentHeader )o;

            return(_prevFileLastIndex == that._prevFileLastIndex && _version == that._version && _prevIndex == that._prevIndex && _prevTerm == that._prevTerm);
        }
예제 #6
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);
            }
        }
예제 #7
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldWriteAndReadHeader() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldWriteAndReadHeader()
        {
            // given
            long prevFileLastIndex = 1;
            long version           = 2;
            long prevIndex         = 3;
            long prevTerm          = 4;

            SegmentHeader writtenHeader = new SegmentHeader(prevFileLastIndex, version, prevIndex, prevTerm);

            InMemoryClosableChannel channel = new InMemoryClosableChannel();

            // when
            _marshal.marshal(writtenHeader, channel);
            SegmentHeader readHeader = _marshal.unmarshal(channel);

            // then
            assertEquals(writtenHeader, readHeader);
        }
예제 #8
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]);
        }
예제 #9
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: static SegmentFile create(org.neo4j.io.fs.FileSystemAbstraction fileSystem, java.io.File file, ReaderPool readerPool, long version, org.neo4j.causalclustering.messaging.marshalling.ChannelMarshal<org.neo4j.causalclustering.core.replication.ReplicatedContent> contentMarshal, org.neo4j.logging.LogProvider logProvider, SegmentHeader header) throws java.io.IOException
        internal static SegmentFile Create(FileSystemAbstraction fileSystem, File file, ReaderPool readerPool, long version, ChannelMarshal <ReplicatedContent> contentMarshal, LogProvider logProvider, SegmentHeader header)
        {
            if (fileSystem.FileExists(file))
            {
                throw new System.InvalidOperationException("File was not expected to exist");
            }

            SegmentFile segment = new SegmentFile(fileSystem, file, readerPool, version, contentMarshal, logProvider, header);

            _headerMarshal.marshal(header, segment.OrCreateWriter);
            segment.Flush();

            return(segment);
        }
예제 #10
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);
        }