@Override public TransactionApplier startTx( CommandsToApply transaction ) throws IOException { TransactionApplier[] txAppliers = new TransactionApplier[appliers.length]; for ( int i = 0; i < appliers.length; i++ ) { txAppliers[i] = appliers[i].startTx( transaction ); } return new TransactionApplierFacade( txAppliers ); }
@Override public TransactionApplier startTx( CommandsToApply transaction, LockGroup lockGroup ) throws IOException { TransactionApplier[] txAppliers = new TransactionApplier[appliers.length]; for ( int i = 0; i < appliers.length; i++ ) { txAppliers[i] = appliers[i].startTx( transaction, lockGroup ); } return new TransactionApplierFacade( txAppliers ); }
@Before public void setUp() throws Exception { txApplier1 = mock( TransactionApplier.class ); applier1 = mock( BatchTransactionApplier.class ); when( applier1.startTx( any( TransactionToApply.class ) ) ).thenReturn( txApplier1 ); when( applier1.startTx( any( TransactionToApply.class ), any( LockGroup.class ) ) ).thenReturn( txApplier1 ); txApplier2 = mock( TransactionApplier.class ); applier2 = mock( BatchTransactionApplier.class ); when( applier2.startTx( any( TransactionToApply.class ) ) ).thenReturn( txApplier2 ); when( applier2.startTx( any( TransactionToApply.class ), any( LockGroup.class ) ) ).thenReturn( txApplier2 ); txApplier3 = mock( TransactionApplier.class ); applier3 = mock( BatchTransactionApplier.class ); when( applier3.startTx( any( TransactionToApply.class ) ) ).thenReturn( txApplier3 ); when( applier3.startTx( any( TransactionToApply.class ), any( LockGroup.class ) ) ).thenReturn( txApplier3 ); facade = new BatchTransactionApplierFacade( applier1, applier2, applier3 ); }
@Test public void testStartTxCorrectOrder() throws Exception { // GIVEN TransactionToApply tx = mock( TransactionToApply.class ); // WHEN TransactionApplierFacade result = (TransactionApplierFacade) facade.startTx( tx ); // THEN InOrder inOrder = inOrder( applier1, applier2, applier3 ); inOrder.verify( applier1 ).startTx( tx ); inOrder.verify( applier2 ).startTx( tx ); inOrder.verify( applier3 ).startTx( tx ); assertEquals( txApplier1, result.appliers[0] ); assertEquals( txApplier2, result.appliers[1] ); assertEquals( txApplier3, result.appliers[2] ); assertEquals( 3, result.appliers.length ); }
@Test public void testStartTxCorrectOrderWithLockGroup() throws Exception { // GIVEN TransactionToApply tx = mock( TransactionToApply.class ); LockGroup lockGroup = mock( LockGroup.class ); // WHEN TransactionApplierFacade result = (TransactionApplierFacade) facade.startTx( tx, lockGroup ); // THEN InOrder inOrder = inOrder( applier1, applier2, applier3 ); inOrder.verify( applier1 ).startTx( tx, lockGroup ); inOrder.verify( applier2 ).startTx( tx, lockGroup ); inOrder.verify( applier3 ).startTx( tx, lockGroup ); assertEquals( txApplier1, result.appliers[0] ); assertEquals( txApplier2, result.appliers[1] ); assertEquals( txApplier3, result.appliers[2] ); assertEquals( 3, result.appliers.length ); }
/** * In case the transactions do not have the commands to apply, use this method to apply any commands you want with a * given {@link ApplyFunction} instead. * * @param applier to use * @param function which knows what to do with the {@link TransactionApplier}. * @param transactions are only used to create {@link TransactionApplier}s. The actual work is delegated to the * function. * @return the boolean-and result of all function operations. */ public static boolean apply( BatchTransactionApplier applier, ApplyFunction function, TransactionToApply... transactions ) throws Exception { boolean result = true; for ( TransactionToApply tx : transactions ) { try ( TransactionApplier txApplier = applier.startTx( tx, new LockGroup() ) ) { result &= function.apply( txApplier ); } } applier.close(); return result; } }
@Override public void apply( CommandsToApply batch, TransactionApplicationMode mode ) throws Exception { // Have these command appliers as separate try-with-resource to have better control over // point between closing this and the locks above try ( IndexActivator indexActivator = new IndexActivator( indexingService ); LockGroup locks = new LockGroup(); BatchTransactionApplier batchApplier = applier( mode, indexActivator ) ) { while ( batch != null ) { try ( TransactionApplier txApplier = batchApplier.startTx( batch, locks ) ) { batch.accept( txApplier ); } batch = batch.next(); } } catch ( Throwable cause ) { TransactionApplyKernelException kernelException = new TransactionApplyKernelException( cause, "Failed to apply transaction: %s", batch ); databaseHealth.panic( kernelException ); throw kernelException; } }
/** * Simply calls through to the {@link TransactionRepresentation#accept(Visitor)} method for each {@link * TransactionToApply} given. This assumes that the {@link BatchTransactionApplier} will return {@link * TransactionApplier}s which actually do the work and that the transaction has all the relevant data. * * @param applier to use * @param transactions to apply */ public static void apply( BatchTransactionApplier applier, TransactionToApply... transactions ) throws Exception { for ( TransactionToApply tx : transactions ) { try ( TransactionApplier txApplier = applier.startTx( tx, new LockGroup() ) ) { tx.transactionRepresentation().accept( txApplier ); } } applier.close(); }
@Override public TransactionApplier startTx( CommandsToApply transaction ) throws IOException { TransactionApplier[] txAppliers = new TransactionApplier[appliers.length]; for ( int i = 0; i < appliers.length; i++ ) { txAppliers[i] = appliers[i].startTx( transaction ); } return new TransactionApplierFacade( txAppliers ); }
@Override public TransactionApplier startTx( CommandsToApply transaction, LockGroup lockGroup ) throws IOException { TransactionApplier[] txAppliers = new TransactionApplier[appliers.length]; for ( int i = 0; i < appliers.length; i++ ) { txAppliers[i] = appliers[i].startTx( transaction, lockGroup ); } return new TransactionApplierFacade( txAppliers ); }
@Override public void apply( CommandsToApply batch, TransactionApplicationMode mode ) throws Exception { // Have these command appliers as separate try-with-resource to have better control over // point between closing this and the locks above try ( IndexActivator indexActivator = new IndexActivator( indexingService ); LockGroup locks = new LockGroup(); BatchTransactionApplier batchApplier = applier( mode, indexActivator ) ) { while ( batch != null ) { try ( TransactionApplier txApplier = batchApplier.startTx( batch, locks ) ) { batch.accept( txApplier ); } batch = batch.next(); } } catch ( Throwable cause ) { TransactionApplyKernelException kernelException = new TransactionApplyKernelException( cause, "Failed to apply transaction: %s", batch ); databaseHealth.panic( kernelException ); throw kernelException; } }