@Test public void shouldEmptyIfTooMany() throws Exception { // GIVEN Applier applier = mock( Applier.class ); int batchSize = 10; TransactionQueue queue = new TransactionQueue( batchSize, applier ); // WHEN for ( int i = 0; i < 9; i++ ) { queue.queue( mock( TransactionToApply.class ) ); verifyNoMoreInteractions( applier ); } queue.queue( mock( TransactionToApply.class ) ); verify( applier, times( 1 ) ).apply( any(), any() ); reset( applier ); // THEN queue.queue( mock( TransactionToApply.class ) ); // and WHEN emptying in the end for ( int i = 0; i < 2; i++ ) { queue.queue( mock( TransactionToApply.class ) ); verifyNoMoreInteractions( applier ); } queue.empty(); verify( applier, times( 1 ) ).apply( any(), any() ); }
@Test public void shouldLinkTogetherTransactions() throws Exception { // GIVEN Applier applier = mock( Applier.class ); int batchSize = 10; TransactionQueue queue = new TransactionQueue( batchSize, applier ); // WHEN TransactionToApply[] txs = new TransactionToApply[batchSize]; for ( int i = 0; i < batchSize; i++ ) { queue.queue( txs[i] = new TransactionToApply( mock( TransactionRepresentation.class ) ) ); } // THEN verify( applier, times( 1 ) ).apply( any(), any() ); for ( int i = 0; i < txs.length - 1; i++ ) { assertEquals( txs[i + 1], txs[i].next() ); } } }
void applyQueuedTransactions() throws Exception { queue.empty(); } }
BatchingResponseHandler( int maxBatchSize, TransactionQueue.Applier applier, TransactionObligationFulfiller obligationFulfiller, TxHandler txHandler, VersionContextSupplier versionContextSupplier, Log log ) { this.obligationFulfiller = obligationFulfiller; this.txHandler = txHandler; this.versionContextSupplier = versionContextSupplier; this.queue = new TransactionQueue( maxBatchSize, applier ); this.log = log; }
@Override public boolean visit( CommittedTransactionRepresentation transaction ) throws Exception { queue.queue( new TransactionToApply( transaction.getTransactionRepresentation(), transaction.getCommitEntry().getTxId(), versionContextSupplier.getVersionContext() ) { @Override public void commitment( Commitment commitment, long transactionId ) { // TODO Perhaps odd to override this method here just to be able to call txHandler? super.commitment( commitment, transactionId ); txHandler.accept( transactionId ); } } ); return false; }
public TransactionToApply first() { if ( isEmpty() ) { throw new IllegalStateException( "Nothing in queue" ); } return first; }
void applyBatch() throws Exception { txQueue.empty(); }
public synchronized void installCommitProcess( TransactionCommitProcess commitProcess, long lastCommittedIndex ) { this.lastCommittedIndex = lastCommittedIndex; log.info( format("Updated lastCommittedIndex to %d", lastCommittedIndex) ); this.queue = new TransactionQueue( maxBatchSize, (first, last) -> commitProcess.commit( first, CommitEvent.NULL, TransactionApplicationMode.EXTERNAL ) ); }
/** * Queues a transaction for application. * * @param tx The transaction to be queued for application. */ public void queue( CommittedTransactionRepresentation tx ) throws Exception { long receivedTxId = tx.getCommitEntry().getTxId(); long expectedTxId = lastQueuedTxId + 1; if ( receivedTxId != expectedTxId ) { log.warn( "Out of order transaction. Received: %d Expected: %d", receivedTxId, expectedTxId ); return; } txQueue.queue( new TransactionToApply( tx.getTransactionRepresentation(), receivedTxId, versionContextSupplier.getVersionContext() ) ); if ( !stopped ) { lastQueuedTxId = receivedTxId; monitor.txPullResponse( receivedTxId ); } }
public TransactionToApply last() { if ( isEmpty() ) { throw new IllegalStateException( "Nothing in queue" ); } return last; } }
@Override public void run() { try { TransactionQueue queue = new TransactionQueue( batchSize, ( tx, last ) -> { // Apply storageEngine.apply( tx, TransactionApplicationMode.EXTERNAL ); // And verify that all nodes are in the index verifyIndex( tx ); base += batchSize; } ); for ( ; !end.get(); i++ ) { queue.queue( createNodeAndProperty( i ) ); } queue.empty(); } catch ( Exception e ) { throw new RuntimeException( e ); } finally { commandCreationContext.close(); } }
public synchronized void ensuredApplied() { try { queue.empty(); } catch ( Exception e ) { throw panicException( e ); } }
@Override public void start() { stopped = false; refreshFromNewStore(); txBatcher = new TransactionQueue( maxBatchSize, ( first, last ) -> commitProcess.commit( first, NULL, EXTERNAL ) ); }
queue.queue( transaction );
public TransactionToApply first() { if ( isEmpty() ) { throw new IllegalStateException( "Nothing in queue" ); } return first; }
public synchronized void ensuredApplied() { try { queue.empty(); } catch ( Exception e ) { throw panicException( e ); } }
public synchronized void installCommitProcess( TransactionCommitProcess commitProcess, long lastCommittedIndex ) { this.lastCommittedIndex = lastCommittedIndex; log.info( format("Updated lastCommittedIndex to %d", lastCommittedIndex) ); this.queue = new TransactionQueue( maxBatchSize, ( first, last ) -> { commitProcess.commit( first, CommitEvent.NULL, TransactionApplicationMode.EXTERNAL ); pageCursorTracerSupplier.get().reportEvents(); // Report paging metrics for the commit } ); }