Beispiel #1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void verifyTransaction(TransactionIdStore transactionIdStore, TransactionMetadataCache positionCache, byte[] additionalHeader, int masterId, int authorId, long timeStarted, long latestCommittedTxWhenStarted, long timeCommitted, LogicalTransactionStore store) throws java.io.IOException
        private void VerifyTransaction(TransactionIdStore transactionIdStore, TransactionMetadataCache positionCache, sbyte[] additionalHeader, int masterId, int authorId, long timeStarted, long latestCommittedTxWhenStarted, long timeCommitted, LogicalTransactionStore store)
        {
            TransactionMetadata expectedMetadata;

            using (TransactionCursor cursor = store.GetTransactions(TransactionIdStore_Fields.BASE_TX_ID + 1))
            {
                bool hasNext = cursor.next();
                assertTrue(hasNext);
                CommittedTransactionRepresentation tx          = cursor.get();
                TransactionRepresentation          transaction = tx.TransactionRepresentation;
                assertArrayEquals(additionalHeader, transaction.AdditionalHeader());
                assertEquals(masterId, transaction.MasterId);
                assertEquals(authorId, transaction.AuthorId);
                assertEquals(timeStarted, transaction.TimeStarted);
                assertEquals(timeCommitted, transaction.TimeCommitted);
                assertEquals(latestCommittedTxWhenStarted, transaction.LatestCommittedTxWhenStarted);
                expectedMetadata = new TransactionMetadata(masterId, authorId, tx.StartEntry.StartPosition, tx.StartEntry.checksum(), timeCommitted);
            }

            positionCache.Clear();

            TransactionMetadata actualMetadata = store.GetMetadataFor(transactionIdStore.LastCommittedTransactionId);

            assertEquals(expectedMetadata, actualMetadata);
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldNotCallTransactionClosedOnFailedAppendedTransaction() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldNotCallTransactionClosedOnFailedAppendedTransaction()
        {
            // GIVEN
            long   txId           = 3;
            string failureMessage = "Forces a failure";
            FlushablePositionAwareChannel channel = spy(new PositionAwarePhysicalFlushableChannel(mock(typeof(PhysicalLogVersionedStoreChannel))));
            IOException failure = new IOException(failureMessage);

            when(channel.PutInt(anyInt())).thenThrow(failure);
            when(_logFile.Writer).thenReturn(channel);
            when(_transactionIdStore.nextCommittingTransactionId()).thenReturn(txId);
            Mockito.reset(_databaseHealth);
            TransactionAppender appender = Life.add(CreateTransactionAppender());

            // WHEN
            TransactionRepresentation transaction = mock(typeof(TransactionRepresentation));

            when(transaction.AdditionalHeader()).thenReturn(new sbyte[0]);
            try
            {
                appender.Append(new TransactionToApply(transaction), _logAppendEvent);
                fail("Expected append to fail. Something is wrong with the test itself");
            }
            catch (IOException e)
            {
                // THEN
                assertSame(failure, e);
                verify(_transactionIdStore, times(1)).nextCommittingTransactionId();
                verify(_transactionIdStore, never()).transactionClosed(eq(txId), anyLong(), anyLong());
                verify(_databaseHealth).panic(failure);
            }
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldAppendSingleTransaction() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldAppendSingleTransaction()
        {
            // GIVEN
            when(_logFile.Writer).thenReturn(_channel);
            long txId = 15;

            when(_transactionIdStore.nextCommittingTransactionId()).thenReturn(txId);
            TransactionAppender appender = Life.add(CreateTransactionAppender());

            // WHEN
            TransactionRepresentation transaction = transaction(SingleCreateNodeCommand(0), new sbyte[] { 1, 2, 5 }, 2, 1, 12345, 4545, 12345 + 10);

            appender.Append(new TransactionToApply(transaction), _logAppendEvent);

            // THEN
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.kernel.impl.transaction.log.entry.LogEntryReader<ReadableLogChannel> logEntryReader = new org.neo4j.kernel.impl.transaction.log.entry.VersionAwareLogEntryReader<>();
            LogEntryReader <ReadableLogChannel> logEntryReader = new VersionAwareLogEntryReader <ReadableLogChannel>();

            using (PhysicalTransactionCursor <ReadableLogChannel> reader = new PhysicalTransactionCursor <ReadableLogChannel>(_channel, logEntryReader))
            {
                reader.Next();
                TransactionRepresentation tx = reader.Get().TransactionRepresentation;
                assertArrayEquals(transaction.AdditionalHeader(), tx.AdditionalHeader());
                assertEquals(transaction.MasterId, tx.MasterId);
                assertEquals(transaction.AuthorId, tx.AuthorId);
                assertEquals(transaction.TimeStarted, tx.TimeStarted);
                assertEquals(transaction.TimeCommitted, tx.TimeCommitted);
                assertEquals(transaction.LatestCommittedTxWhenStarted, tx.LatestCommittedTxWhenStarted);
            }
        }
Beispiel #4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldSerializeAndDeserializeTransactionRepresentation() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldSerializeAndDeserializeTransactionRepresentation()
        {
            // GIVEN
            PhysicalTransactionRepresentation transaction = new PhysicalTransactionRepresentation(JustOneNode());

            sbyte[] additionalHeader  = "extra".GetBytes();
            int     masterId          = 1;
            int     authorId          = 2;
            long    timeStarted       = 12345;
            long    lastTxWhenStarted = 12;
            long    timeCommitted     = timeStarted + 10;

            transaction.SetHeader(additionalHeader, masterId, authorId, timeStarted, lastTxWhenStarted, timeCommitted, -1);
            Protocol.TransactionSerializer serializer = new Protocol.TransactionSerializer(transaction);
            ChannelBuffer buffer = new ChannelBufferWrapper(new InMemoryClosableChannel());

            // WHEN serializing the transaction
            serializer.Write(buffer);

            // THEN deserializing the same transaction should yield the same data.
            // ... remember that this deserializer doesn't read the data source name string. Read it manually here
            assertEquals(NeoStoreDataSource.DEFAULT_DATA_SOURCE_NAME, Protocol.ReadString(buffer));
            VersionAwareLogEntryReader <ReadableClosablePositionAwareChannel> reader = new VersionAwareLogEntryReader <ReadableClosablePositionAwareChannel>();
            TransactionRepresentation readTransaction = (new Protocol.TransactionRepresentationDeserializer(reader)).Read(buffer, ByteBuffer.allocate(1000));

            assertArrayEquals(additionalHeader, readTransaction.AdditionalHeader());
            assertEquals(masterId, readTransaction.MasterId);
            assertEquals(authorId, readTransaction.AuthorId);
            assertEquals(timeStarted, readTransaction.TimeStarted);
            assertEquals(lastTxWhenStarted, readTransaction.LatestCommittedTxWhenStarted);
            assertEquals(timeCommitted, readTransaction.TimeCommitted);
        }
Beispiel #5
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void assertMarshalingEquality(io.netty.buffer.ByteBuf buffer, org.neo4j.causalclustering.core.state.machines.tx.TransactionRepresentationReplicatedTransaction replicatedTx) throws java.io.IOException, org.neo4j.causalclustering.messaging.EndOfStreamException
        private void AssertMarshalingEquality(ByteBuf buffer, TransactionRepresentationReplicatedTransaction replicatedTx)
        {
            _marshal.marshal(replicatedTx, new NetworkWritableChannel(buffer));

            ReplicatedContent unmarshal = _marshal.unmarshal(new NetworkReadableClosableChannelNetty4(buffer));

            TransactionRepresentation tx = replicatedTx.Tx();

            sbyte[] extraHeader = tx.AdditionalHeader();
            if (extraHeader == null)
            {
                // hackishly set additional header to empty array...
                (( PhysicalTransactionRepresentation )tx).setHeader(new sbyte[0], tx.MasterId, tx.AuthorId, tx.TimeStarted, tx.LatestCommittedTxWhenStarted, tx.TimeCommitted, tx.LockSessionId);
                extraHeader = tx.AdditionalHeader();
            }
            TransactionRepresentation representation = ReplicatedTransactionFactory.extractTransactionRepresentation(( ReplicatedTransaction )unmarshal, extraHeader);

            assertThat(representation, equalTo(tx));
        }
Beispiel #6
0
            public override bool Visit(CommittedTransactionRepresentation tx)
            {
                TransactionRepresentation transaction = tx.TransactionRepresentation;

                assertArrayEquals(AdditionalHeader, transaction.AdditionalHeader());
                assertEquals(MasterId, transaction.MasterId);
                assertEquals(AuthorId, transaction.AuthorId);
                assertEquals(TimeStarted, transaction.TimeStarted);
                assertEquals(TimeCommitted, transaction.TimeCommitted);
                assertEquals(LatestCommittedTxWhenStarted, transaction.LatestCommittedTxWhenStarted);
                VisitedTransactionsConflict++;
                return(false);
            }
Beispiel #7
0
        public override string ToString()
        {
            TransactionRepresentation tr = this._transactionRepresentation;

            return("Transaction #" + _transactionId + (_logPosition != null ? " at log position " + _logPosition : " (no log position)") +
                   " {started " + date(tr.TimeStarted) +
                   ", committed " + date(tr.TimeCommitted) +
                   ", with " + CountCommands() + " commands in this transaction" +
                   ", authored by " + tr.AuthorId +
                   ", with master id " + tr.MasterId +
                   ", lock session " + tr.LockSessionId +
                   ", latest committed transaction id when started was " + tr.LatestCommittedTxWhenStarted +
                   ", additional header bytes: " + HexPrinter.hex(tr.AdditionalHeader(), int.MaxValue, "") + "}");
        }
Beispiel #8
0
        private CommittedTransactionRepresentation CreateTxWithId(long txId)
        {
            CommittedTransactionRepresentation tx = mock(typeof(CommittedTransactionRepresentation));
            LogEntryCommit commitEntry            = mock(typeof(LogEntryCommit));

            when(commitEntry.TxId).thenReturn(txId);
            TransactionRepresentation txRep = mock(typeof(TransactionRepresentation));

            sbyte[] encodedRaftLogIndex = encodeLogIndexAsTxHeader(txId - 5);                 // just some arbitrary offset
            when(txRep.AdditionalHeader()).thenReturn(encodedRaftLogIndex);
            when(tx.TransactionRepresentation).thenReturn(txRep);
            when(tx.CommitEntry).thenReturn(commitEntry);
            return(tx);
        }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private static org.neo4j.kernel.impl.api.TransactionToApply newTransactionThatFailsWith(Exception error) throws java.io.IOException
        private static TransactionToApply NewTransactionThatFailsWith(Exception error)
        {
            TransactionRepresentation transaction = mock(typeof(TransactionRepresentation));

            when(transaction.AdditionalHeader()).thenReturn(new sbyte[0]);
            // allow to build validated index updates but fail on actual tx application
            doThrow(error).when(transaction).accept(any());

            long txId = ThreadLocalRandom.current().nextLong(0, 1000);
            TransactionToApply txToApply  = new TransactionToApply(transaction);
            FakeCommitment     commitment = new FakeCommitment(txId, mock(typeof(TransactionIdStore)));

            commitment.HasExplicitIndexChanges = false;
            txToApply.Commitment(commitment, txId);
            return(txToApply);
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldNotCallTransactionClosedOnFailedForceLogToDisk() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldNotCallTransactionClosedOnFailedForceLogToDisk()
        {
            // GIVEN
            long   txId           = 3;
            string failureMessage = "Forces a failure";
            FlushablePositionAwareChannel channel = spy(new InMemoryClosableChannel());
            IOException failure = new IOException(failureMessage);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.io.Flushable flushable = mock(java.io.Flushable.class);
            Flushable flushable = mock(typeof(Flushable));

            doAnswer(invocation =>
            {
                invocation.callRealMethod();
                return(flushable);
            }).when(channel).prepareForFlush();
            doThrow(failure).when(flushable).flush();
            when(_logFile.Writer).thenReturn(channel);
            TransactionMetadataCache metadataCache      = new TransactionMetadataCache();
            TransactionIdStore       transactionIdStore = mock(typeof(TransactionIdStore));

            when(transactionIdStore.NextCommittingTransactionId()).thenReturn(txId);
            Mockito.reset(_databaseHealth);
            TransactionAppender appender = Life.add(new BatchingTransactionAppender(_logFiles, NO_ROTATION, metadataCache, transactionIdStore, BYPASS, _databaseHealth));

            // WHEN
            TransactionRepresentation transaction = mock(typeof(TransactionRepresentation));

            when(transaction.AdditionalHeader()).thenReturn(new sbyte[0]);
            try
            {
                appender.Append(new TransactionToApply(transaction), _logAppendEvent);
                fail("Expected append to fail. Something is wrong with the test itself");
            }
            catch (IOException e)
            {
                // THEN
                assertSame(failure, e);
                verify(transactionIdStore, times(1)).nextCommittingTransactionId();
                verify(transactionIdStore, never()).transactionClosed(eq(txId), anyLong(), anyLong());
                verify(_databaseHealth).panic(failure);
            }
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldAppendCommittedTransactions() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldAppendCommittedTransactions()
        {
            // GIVEN
            when(_logFile.Writer).thenReturn(_channel);
            long nextTxId = 15;

            when(_transactionIdStore.nextCommittingTransactionId()).thenReturn(nextTxId);
            TransactionAppender appender = Life.add(new BatchingTransactionAppender(_logFiles, NO_ROTATION, _positionCache, _transactionIdStore, BYPASS, _databaseHealth));

            // WHEN
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final byte[] additionalHeader = new byte[]{1, 2, 5};
            sbyte[]    additionalHeader             = new sbyte[] { 1, 2, 5 };
            const int  masterId                     = 2;
            int        authorId                     = 1;
            const long timeStarted                  = 12345;
            long       latestCommittedTxWhenStarted = nextTxId - 5;
            long       timeCommitted                = timeStarted + 10;
            PhysicalTransactionRepresentation transactionRepresentation = new PhysicalTransactionRepresentation(SingleCreateNodeCommand(0));

            transactionRepresentation.SetHeader(additionalHeader, masterId, authorId, timeStarted, latestCommittedTxWhenStarted, timeCommitted, -1);

            LogEntryStart  start  = new LogEntryStart(0, 0, 0L, latestCommittedTxWhenStarted, null, LogPosition.UNSPECIFIED);
            LogEntryCommit commit = new LogEntryCommit(nextTxId, 0L);
            CommittedTransactionRepresentation transaction = new CommittedTransactionRepresentation(start, transactionRepresentation, commit);

            appender.Append(new TransactionToApply(transactionRepresentation, transaction.CommitEntry.TxId), _logAppendEvent);

            // THEN
            LogEntryReader <ReadableLogChannel> logEntryReader = new VersionAwareLogEntryReader <ReadableLogChannel>();

            using (PhysicalTransactionCursor <ReadableLogChannel> reader = new PhysicalTransactionCursor <ReadableLogChannel>(_channel, logEntryReader))
            {
                reader.Next();
                TransactionRepresentation result = reader.Get().TransactionRepresentation;
                assertArrayEquals(additionalHeader, result.AdditionalHeader());
                assertEquals(masterId, result.MasterId);
                assertEquals(authorId, result.AuthorId);
                assertEquals(timeStarted, result.TimeStarted);
                assertEquals(timeCommitted, result.TimeCommitted);
                assertEquals(latestCommittedTxWhenStarted, result.LatestCommittedTxWhenStarted);
            }
        }
        /// <returns> A TransactionCommitment instance with metadata about the committed transaction, such as whether or not
        /// this transaction contains any explicit index changes. </returns>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private TransactionCommitment appendToLog(org.neo4j.kernel.impl.transaction.TransactionRepresentation transaction, long transactionId) throws java.io.IOException
        private TransactionCommitment AppendToLog(TransactionRepresentation transaction, long transactionId)
        {
            // Reset command writer so that we, after we've written the transaction, can ask it whether or
            // not any explicit index command was written. If so then there's additional ordering to care about below.
            _indexCommandDetector.reset();

            // The outcome of this try block is either of:
            // a) transaction successfully appended, at which point we return a Commitment to be used after force
            // b) transaction failed to be appended, at which point a kernel panic is issued
            // The reason that we issue a kernel panic on failure in here is that at this point we're still
            // holding the logFile monitor, and a failure to append needs to be communicated with potential
            // log rotation, which will wait for all transactions closed or fail on kernel panic.
            try
            {
                LogPosition logPositionBeforeCommit = _writer.getCurrentPosition(_positionMarker).newPosition();
                _transactionLogWriter.append(transaction, transactionId);
                LogPosition logPositionAfterCommit = _writer.getCurrentPosition(_positionMarker).newPosition();

                long transactionChecksum = checksum(transaction.AdditionalHeader(), transaction.MasterId, transaction.AuthorId);
                _transactionMetadataCache.cacheTransactionMetadata(transactionId, logPositionBeforeCommit, transaction.MasterId, transaction.AuthorId, transactionChecksum, transaction.TimeCommitted);

                transaction.Accept(_indexCommandDetector);
                bool hasExplicitIndexChanges = _indexCommandDetector.hasWrittenAnyExplicitIndexCommand();
                if (hasExplicitIndexChanges)
                {
                    // Offer this transaction id to the queue so that the explicit index applier can take part in the ordering
                    _explicitIndexTransactionOrdering.offer(transactionId);
                }
                return(new TransactionCommitment(hasExplicitIndexChanges, transactionId, transactionChecksum, transaction.TimeCommitted, logPositionAfterCommit, _transactionIdStore));
            }
//JAVA TO C# CONVERTER WARNING: 'final' catch parameters are not available in C#:
//ORIGINAL LINE: catch (final Throwable panic)
            catch (Exception panic)
            {
                _databaseHealth.panic(panic);
                throw panic;
            }
        }
Beispiel #13
0
            internal TransactionRepresentationWriter(TransactionRepresentation tx)
            {
                NextJob = channel =>
                {
                    channel.putInt(tx.AuthorId);
                    channel.putInt(tx.MasterId);
                    channel.putLong(tx.LatestCommittedTxWhenStarted);
                    channel.putLong(tx.TimeStarted);
                    channel.putLong(tx.TimeCommitted);
                    channel.putInt(tx.LockSessionId);

                    sbyte[] additionalHeader = tx.AdditionalHeader();
                    if (additionalHeader != null)
                    {
                        channel.putInt(additionalHeader.Length);
                        channel.put(additionalHeader, additionalHeader.Length);
                    }
                    else
                    {
                        channel.putInt(0);
                    }
                };
                Commands = tx.GetEnumerator();
            }
Beispiel #14
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public void append(org.neo4j.kernel.impl.transaction.TransactionRepresentation transaction, long transactionId) throws java.io.IOException
        public virtual void Append(TransactionRepresentation transaction, long transactionId)
        {
            _writer.writeStartEntry(transaction.MasterId, transaction.AuthorId, transaction.TimeStarted, transaction.LatestCommittedTxWhenStarted, transaction.AdditionalHeader());

            // Write all the commands to the log channel
            _writer.serialize(transaction);

            // Write commit record
            _writer.writeCommitEntry(transactionId, transaction.TimeCommitted);
        }