Skip to content

Commit

Permalink
Fix tests related to CommandHandler refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
spacecowboy committed Dec 16, 2015
1 parent 922a7e3 commit c6d4ada
Show file tree
Hide file tree
Showing 17 changed files with 1,611 additions and 257 deletions.
Expand Up @@ -32,10 +32,10 @@
import org.neo4j.helpers.collection.Visitor; import org.neo4j.helpers.collection.Visitor;
import org.neo4j.io.fs.DefaultFileSystemAbstraction; import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction; import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.impl.api.CommandVisitor;
import org.neo4j.kernel.impl.store.NeoStores; import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.transaction.DeadSimpleLogVersionRepository; import org.neo4j.kernel.impl.transaction.DeadSimpleLogVersionRepository;
import org.neo4j.kernel.impl.transaction.DeadSimpleTransactionIdStore; import org.neo4j.kernel.impl.transaction.DeadSimpleTransactionIdStore;
import org.neo4j.kernel.impl.transaction.command.CommandHandler;
import org.neo4j.kernel.impl.transaction.log.LogPosition; import org.neo4j.kernel.impl.transaction.log.LogPosition;
import org.neo4j.kernel.impl.transaction.log.LogPositionMarker; import org.neo4j.kernel.impl.transaction.log.LogPositionMarker;
import org.neo4j.kernel.impl.transaction.log.LogVersionRepository; import org.neo4j.kernel.impl.transaction.log.LogVersionRepository;
Expand Down Expand Up @@ -279,7 +279,7 @@ private void writeSomeData( File file, Visitor<Pair<LogEntryWriter,Consumer<LogP
throw new RuntimeException( e ); throw new RuntimeException( e );
} }
}; };
LogEntryWriter first = new LogEntryWriter( writableLogChannel, CommandHandler.EMPTY ); LogEntryWriter first = new LogEntryWriter( writableLogChannel, new CommandVisitor.Adapter() );
visitor.visit( Pair.of( first, consumer ) ); visitor.visit( Pair.of( first, consumer ) );
} }
} }
Expand Down
Expand Up @@ -38,7 +38,7 @@
import org.neo4j.kernel.impl.index.IndexConfigStore; import org.neo4j.kernel.impl.index.IndexConfigStore;
import org.neo4j.kernel.impl.index.IndexDefineCommand; import org.neo4j.kernel.impl.index.IndexDefineCommand;
import org.neo4j.kernel.impl.locking.LockGroup; import org.neo4j.kernel.impl.locking.LockGroup;
import org.neo4j.kernel.impl.transaction.command.CommandHandler; import org.neo4j.kernel.impl.transaction.log.Commitment;
import org.neo4j.kernel.impl.transaction.log.FakeCommitment; import org.neo4j.kernel.impl.transaction.log.FakeCommitment;
import org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation; import org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation;
import org.neo4j.kernel.impl.transaction.log.TransactionIdStore; import org.neo4j.kernel.impl.transaction.log.TransactionIdStore;
Expand Down Expand Up @@ -74,18 +74,25 @@ public void shouldOnlyCreateOneApplierPerProvider() throws Exception
Map<String,Integer> names = MapUtil.genericMap( "first", 0, "second", 1 ); Map<String,Integer> names = MapUtil.genericMap( "first", 0, "second", 1 );
Map<String,Integer> keys = MapUtil.genericMap( "key", 0 ); Map<String,Integer> keys = MapUtil.genericMap( "key", 0 );
String applierName = "test-applier"; String applierName = "test-applier";
Commitment commitment = mock( Commitment.class );
when( commitment.hasLegacyIndexChanges() ).thenReturn( true );
IndexConfigStore config = newIndexConfigStore( names, applierName ); IndexConfigStore config = newIndexConfigStore( names, applierName );
LegacyIndexApplierLookup applierLookup = mock( LegacyIndexApplierLookup.class ); LegacyIndexApplierLookup applierLookup = mock( LegacyIndexApplierLookup.class );
when( applierLookup.newApplier( anyString(), anyBoolean() ) ).thenReturn( mock( CommandHandler.class ) ); when( applierLookup.newApplier( anyString(), anyBoolean() ) ).thenReturn( mock( CommandVisitor.class ) );
try ( LegacyBatchIndexApplier applier = new LegacyBatchIndexApplier( config, applierLookup, BYPASS, INTERNAL ) ) try ( LegacyBatchIndexApplier applier = new LegacyBatchIndexApplier( config, applierLookup, BYPASS, INTERNAL ) )
{ {
// WHEN TransactionToApply tx = new TransactionToApply( null, 2 );
IndexDefineCommand definitions = definitions( names, keys ); tx.commitment( commitment, 2 );
applier.visitIndexDefineCommand( definitions ); try ( TransactionApplier txApplier = applier.startTx( tx ) )
applier.visitIndexAddNodeCommand( addNodeToIndex( definitions, "first" ) ); {
applier.visitIndexAddNodeCommand( addNodeToIndex( definitions, "second" ) ); // WHEN
applier.visitIndexAddRelationshipCommand( addRelationshipToIndex( definitions, "second" ) ); IndexDefineCommand definitions = definitions( names, keys );
applier.apply(); txApplier.visitIndexDefineCommand( definitions );
txApplier.visitIndexAddNodeCommand( addNodeToIndex( definitions, "first" ) );
txApplier.visitIndexAddNodeCommand( addNodeToIndex( definitions, "second" ) );
txApplier.visitIndexAddRelationshipCommand( addRelationshipToIndex( definitions, "second" ) );
//applier.apply();
}
} }


// THEN // THEN
Expand All @@ -100,7 +107,7 @@ public void shouldOrderTransactionsMakingLegacyIndexChanges() throws Throwable
Map<String,Integer> keys = MapUtil.genericMap( "key", 0 ); Map<String,Integer> keys = MapUtil.genericMap( "key", 0 );
String applierName = "test-applier"; String applierName = "test-applier";
LegacyIndexApplierLookup applierLookup = mock( LegacyIndexApplierLookup.class ); LegacyIndexApplierLookup applierLookup = mock( LegacyIndexApplierLookup.class );
when( applierLookup.newApplier( anyString(), anyBoolean() ) ).thenReturn( mock( CommandHandler.class ) ); when( applierLookup.newApplier( anyString(), anyBoolean() ) ).thenReturn( mock( CommandVisitor.class ) );
IndexConfigStore config = newIndexConfigStore( names, applierName ); IndexConfigStore config = newIndexConfigStore( names, applierName );


// WHEN multiple legacy index transactions are running, they should be done in order // WHEN multiple legacy index transactions are running, they should be done in order
Expand All @@ -118,9 +125,12 @@ public void shouldOrderTransactionsMakingLegacyIndexChanges() throws Throwable
FakeCommitment commitment = new FakeCommitment( txId, mock( TransactionIdStore.class ) ); FakeCommitment commitment = new FakeCommitment( txId, mock( TransactionIdStore.class ) );
commitment.setHasLegacyIndexChanges( true ); commitment.setHasLegacyIndexChanges( true );
txToApply.commitment( commitment, txId ); txToApply.commitment( commitment, txId );
applier.begin( txToApply, new LockGroup() ); try ( TransactionApplier txApplier = applier.startTx( txToApply ) )
applier.end(); {
applier.apply(); //applier.begin( txToApply, new LockGroup() );
//txApplier.end();
//applier.apply();
}
// Make sure threads are unordered // Make sure threads are unordered
Thread.sleep( ThreadLocalRandom.current().nextInt( 5 ) ); Thread.sleep( ThreadLocalRandom.current().nextInt( 5 ) );
// THEN // THEN
Expand Down
Expand Up @@ -591,7 +591,7 @@ public void validatedUpdatesShouldFlush() throws Exception
IndexUpdateMode.ONLINE ) ) IndexUpdateMode.ONLINE ) )
{ {
Set<IndexDescriptor> affectedIndexes = new HashSet<>(); Set<IndexDescriptor> affectedIndexes = new HashSet<>();
updates.flush( affectedIndexes ); updates.flush( affectedIndexes::add );
assertEquals( asSet( new IndexDescriptor( labelId, propertyKeyId ) ), affectedIndexes ); assertEquals( asSet( new IndexDescriptor( labelId, propertyKeyId ) ), affectedIndexes );
} }


Expand Down Expand Up @@ -624,7 +624,7 @@ public void closingOfValidatedUpdatesShouldRemoveReservation() throws Exception
IndexUpdateMode.ONLINE ) ) IndexUpdateMode.ONLINE ) )
{ {
verifyZeroInteractions( reservation ); verifyZeroInteractions( reservation );
updates.flush( new HashSet<>() ); updates.flush( indexDescriptor -> {} );
verifyZeroInteractions( reservation ); verifyZeroInteractions( reservation );
} }


Expand Down Expand Up @@ -669,7 +669,7 @@ public void closingOfValidatedUpdatesShouldCloseUpdaters() throws Exception
NodePropertyUpdate.add( 1, propertyKeyId, "foo", new long[]{labelId1} ), NodePropertyUpdate.add( 1, propertyKeyId, "foo", new long[]{labelId1} ),
NodePropertyUpdate.add( 2, propertyKeyId, "bar", new long[]{labelId2} ) ), IndexUpdateMode.ONLINE ) ) NodePropertyUpdate.add( 2, propertyKeyId, "bar", new long[]{labelId2} ) ), IndexUpdateMode.ONLINE ) )
{ {
updates.flush( new HashSet<>() ); updates.flush( indexDescriptor -> {} );
} }


// Then // Then
Expand Down
Expand Up @@ -47,6 +47,7 @@
import org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation; import org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation;
import org.neo4j.kernel.impl.transaction.state.LazyIndexUpdates; import org.neo4j.kernel.impl.transaction.state.LazyIndexUpdates;
import org.neo4j.kernel.impl.transaction.state.PropertyLoader; import org.neo4j.kernel.impl.transaction.state.PropertyLoader;
import org.neo4j.kernel.internal.DatabaseHealth;


import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
Expand Down Expand Up @@ -172,10 +173,10 @@ public void shouldRethrowExceptionThrownByIndexUpdatersValidationProcedure() thr
validator.validate( tx ); validator.validate( tx );
fail( "Should have thrown " + UnderlyingStorageException.class.getSimpleName() ); fail( "Should have thrown " + UnderlyingStorageException.class.getSimpleName() );
} }
catch ( UnderlyingStorageException e ) catch ( IOException e )
{ {
// Then // Then
assertSame( error, e.getCause() ); assertSame( error, e.getCause().getCause() );
} }
} }


Expand Down Expand Up @@ -239,7 +240,8 @@ private IndexUpdatesValidator newIndexUpdatesValidatorWithMockedDependencies()
{ {
when( neoStores.getNodeStore() ).thenReturn( nodeStore ); when( neoStores.getNodeStore() ).thenReturn( nodeStore );
when( neoStores.getPropertyStore() ).thenReturn( propertyStore ); when( neoStores.getPropertyStore() ).thenReturn( propertyStore );
return new OnlineIndexUpdatesValidator( neoStores, null, propertyLoader, indexingService, ONLINE ); return new OnlineIndexUpdatesValidator( neoStores, mock( DatabaseHealth.class ), propertyLoader,
indexingService, ONLINE );
} }


private static NodePropertyCommands createNodeWithLabelAndPropertyCommands( long nodeId, int label, int property ) private static NodePropertyCommands createNodeWithLabelAndPropertyCommands( long nodeId, int label, int property )
Expand Down
Expand Up @@ -63,7 +63,7 @@ public void recoveredValidatedUpdatesShouldFlushRecoveredNodeIds() throws Except
// When // When
try ( ValidatedIndexUpdates updates = validator.validate( tx ) ) try ( ValidatedIndexUpdates updates = validator.validate( tx ) )
{ {
updates.flush( new HashSet<>() ); updates.flush( indexDescriptor -> {} );
} }


// Then // Then
Expand Down
Expand Up @@ -19,6 +19,12 @@
*/ */
package org.neo4j.kernel.impl.transaction.command; package org.neo4j.kernel.impl.transaction.command;


import java.io.IOException;
import java.util.function.Function;

import org.neo4j.helpers.collection.Visitor;
import org.neo4j.kernel.impl.api.BatchTransactionApplier;
import org.neo4j.kernel.impl.api.TransactionApplier;
import org.neo4j.kernel.impl.api.TransactionToApply; import org.neo4j.kernel.impl.api.TransactionToApply;
import org.neo4j.kernel.impl.locking.LockGroup; import org.neo4j.kernel.impl.locking.LockGroup;
import org.neo4j.kernel.impl.transaction.TransactionRepresentation; import org.neo4j.kernel.impl.transaction.TransactionRepresentation;
Expand All @@ -32,48 +38,57 @@ public class CommandHandlerContract
@FunctionalInterface @FunctionalInterface
public interface ApplyFunction public interface ApplyFunction
{ {
boolean apply( CommandHandler applier, TransactionRepresentation tx ) throws Exception; boolean apply( TransactionApplier applier ) throws Exception;
} }


public static boolean apply( CommandHandler applier, TransactionToApply... transactions ) public static void apply( BatchTransactionApplier applier, TransactionToApply... transactions ) throws Exception
throws Exception
{ {
return apply( new CommandApplierFacade( applier ), transactions ); for ( TransactionToApply tx : transactions )
{
try ( TransactionApplier txApplier = applier.startTx( tx, new LockGroup() ) )
{
tx.transactionRepresentation().accept( txApplier );
}
}
applier.close();
} }


public static boolean apply( CommandApplierFacade applier, TransactionToApply... transactions ) public static boolean apply( BatchTransactionApplier applier, ApplyFunction function,
throws Exception TransactionToApply... transactions ) throws Exception
{ {
return apply( applier, (handler,tx) -> { boolean result = true;
tx.accept( applier ); for ( TransactionToApply tx : transactions )
return false; {
}, transactions ); try ( TransactionApplier txApplier = applier.startTx( tx, new LockGroup() ) )
{
result &= function.apply( txApplier );
}
}
applier.close();
return result;
} }


public static boolean apply( CommandHandler applier, ApplyFunction function, TransactionToApply... transactions ) /*public static boolean apply( BatchTransactionApplier applier, TransactionToApply... transactions )
throws Exception throws Exception
{ {
boolean result = true;
for ( TransactionToApply tx : transactions ) for ( TransactionToApply tx : transactions )
{ {
applier.begin( tx, new LockGroup() ); try (TransactionApplier txApplier = applier.startTx( tx, new LockGroup() ))
try
{ {
result &= function.apply( applier, tx.transactionRepresentation() ); tx.transactionRepresentation().accept( txApplier );
} //function.apply( applier, tx.transactionRepresentation() );
finally
{
applier.end();
} }
} }
if ( !(applier instanceof CommandApplierFacade) ) if ( !(applier instanceof CommandApplierFacade) )
{ {
// This is really odd... the whole apply/close bit. CommandApplierFacade is apparently // This is really odd... the whole apply/close bit. CommandApplierFacade is apparently
// owning the call of apply itself. We'll just have to figure out why this is and then // owning the call of apply itself. We'll just have to figure out why this is and then
// merge apply/close. We can't have it like this. // merge apply/close. We can't have it like this.
applier.apply(); applier.apply();
} }
applier.close(); applier.close();
return result; return false;
} }*/
} }
Expand Up @@ -22,6 +22,7 @@
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;


import org.neo4j.kernel.impl.api.CommandVisitor;
import org.neo4j.kernel.impl.api.TransactionToApply; import org.neo4j.kernel.impl.api.TransactionToApply;
import org.neo4j.kernel.impl.locking.LockGroup; import org.neo4j.kernel.impl.locking.LockGroup;
import org.neo4j.kernel.impl.store.NeoStores; import org.neo4j.kernel.impl.store.NeoStores;
Expand All @@ -31,8 +32,6 @@
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;


import static org.neo4j.kernel.impl.transaction.command.CommandHandler.EMPTY;

public class HighIdTransactionApplierTest public class HighIdTransactionApplierTest
{ {
@Rule @Rule
Expand All @@ -43,9 +42,9 @@ public void shouldUpdateHighIdsOnExternalTransaction() throws Exception
{ {
// GIVEN // GIVEN
NeoStores neoStores = neoStoresRule.open(); NeoStores neoStores = neoStoresRule.open();
HighIdTransactionApplier tracker = new HighIdTransactionApplier( EMPTY, neoStores ); HighIdTransactionApplier tracker = new HighIdTransactionApplier( neoStores );


tracker.begin( mock( TransactionToApply.class ), new LockGroup() ); //tracker.begin( mock( TransactionToApply.class ), new LockGroup() );
// WHEN // WHEN
// Nodes // Nodes
tracker.visitNodeCommand( Commands.createNode( 10, 2, 3 ) ); tracker.visitNodeCommand( Commands.createNode( 10, 2, 3 ) );
Expand Down Expand Up @@ -79,9 +78,9 @@ public void shouldUpdateHighIdsOnExternalTransaction() throws Exception
tracker.visitPropertyCommand( Commands.createProperty( 10, PropertyType.STRING, 0, 6, 7 ) ); tracker.visitPropertyCommand( Commands.createProperty( 10, PropertyType.STRING, 0, 6, 7 ) );
tracker.visitPropertyCommand( Commands.createProperty( 20, PropertyType.ARRAY, 1, 8, 9 ) ); tracker.visitPropertyCommand( Commands.createProperty( 20, PropertyType.ARRAY, 1, 8, 9 ) );


tracker.end(); //tracker.end();


tracker.apply(); //tracker.apply();
tracker.close(); tracker.close();


// THEN // THEN
Expand Down
Expand Up @@ -27,7 +27,10 @@
import org.neo4j.concurrent.WorkSync; import org.neo4j.concurrent.WorkSync;
import org.neo4j.kernel.api.exceptions.index.IndexCapacityExceededException; import org.neo4j.kernel.api.exceptions.index.IndexCapacityExceededException;
import org.neo4j.kernel.api.labelscan.NodeLabelUpdate; import org.neo4j.kernel.api.labelscan.NodeLabelUpdate;
import org.neo4j.kernel.impl.api.TransactionApplier;
import org.neo4j.kernel.impl.api.TransactionToApply;
import org.neo4j.kernel.impl.api.index.IndexingService; import org.neo4j.kernel.impl.api.index.IndexingService;
import org.neo4j.kernel.impl.api.index.ValidatedIndexUpdates;
import org.neo4j.kernel.impl.store.NodeLabelsField; import org.neo4j.kernel.impl.store.NodeLabelsField;
import org.neo4j.kernel.impl.store.record.NodeRecord; import org.neo4j.kernel.impl.store.record.NodeRecord;
import org.neo4j.kernel.impl.transaction.command.Command.NodeCommand; import org.neo4j.kernel.impl.transaction.command.Command.NodeCommand;
Expand All @@ -36,6 +39,7 @@
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;


import static org.mockito.Mockito.when;
import static org.neo4j.kernel.impl.store.record.Record.NO_NEXT_PROPERTY; import static org.neo4j.kernel.impl.store.record.Record.NO_NEXT_PROPERTY;
import static org.neo4j.kernel.impl.store.record.Record.NO_NEXT_RELATIONSHIP; import static org.neo4j.kernel.impl.store.record.Record.NO_NEXT_RELATIONSHIP;


Expand All @@ -49,13 +53,19 @@ public void shouldProvideLabelScanStoreUpdatesSortedByNodeId() throws Exception
LabelScanWriter writer = new OrderVerifyingLabelScanWriter( 10, 15, 20 ); LabelScanWriter writer = new OrderVerifyingLabelScanWriter( 10, 15, 20 );
WorkSync<Supplier<LabelScanWriter>,LabelUpdateWork> labelScanSync = WorkSync<Supplier<LabelScanWriter>,LabelUpdateWork> labelScanSync =
new WorkSync<>( singletonProvider( writer ) ); new WorkSync<>( singletonProvider( writer ) );
TransactionToApply tx = mock( TransactionToApply.class );
ValidatedIndexUpdates indexUpdates = mock( ValidatedIndexUpdates.class );
when( tx.validatedIndexUpdates() ).thenReturn( indexUpdates );
try ( IndexBatchTransactionApplier applier = new IndexBatchTransactionApplier( indexing, labelScanSync ) ) try ( IndexBatchTransactionApplier applier = new IndexBatchTransactionApplier( indexing, labelScanSync ) )
{ {
// WHEN try ( TransactionApplier txApplier = applier.startTx( tx ))
applier.visitNodeCommand( node( 15 ) ); {
applier.visitNodeCommand( node( 20 ) ); // WHEN
applier.visitNodeCommand( node( 10 ) ); txApplier.visitNodeCommand( node( 15 ) );
applier.apply(); txApplier.visitNodeCommand( node( 20 ) );
txApplier.visitNodeCommand( node( 10 ) );
//applier.apply();
}
} }
// THEN all assertions happen inside the LabelScanWriter#write and #close // THEN all assertions happen inside the LabelScanWriter#write and #close
} }
Expand Down

0 comments on commit c6d4ada

Please sign in to comment.