Skip to content

Commit

Permalink
Include secondary unit ids in high id tracking
Browse files Browse the repository at this point in the history
when applying transactions. Previously those secondary unit ids weren't
tracked and so might be lost after a crash or master switch. This might
result in such a record being reused if it was a secondary unit id and the
highest one in the store.
  • Loading branch information
tinwelint committed Apr 20, 2016
1 parent 299453b commit fd34650
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
Expand Up @@ -35,6 +35,7 @@
import org.neo4j.kernel.impl.store.record.DynamicRecord;
import org.neo4j.kernel.impl.store.record.PropertyBlock;
import org.neo4j.kernel.impl.store.record.TokenRecord;
import org.neo4j.kernel.impl.transaction.command.Command.BaseCommand;
import org.neo4j.kernel.impl.transaction.command.Command.LabelTokenCommand;
import org.neo4j.kernel.impl.transaction.command.Command.NodeCommand;
import org.neo4j.kernel.impl.transaction.command.Command.PropertyCommand;
Expand All @@ -46,6 +47,8 @@
import org.neo4j.kernel.impl.transaction.command.Command.TokenCommand;
import org.neo4j.storageengine.api.Token;

import static java.lang.Math.max;

public class HighIdTransactionApplier extends TransactionApplier.Adapter
{
private final NeoStores neoStores;
Expand Down Expand Up @@ -129,7 +132,7 @@ public boolean visitSchemaRuleCommand( SchemaRuleCommand command ) throws IOExce
SchemaStore schemaStore = neoStores.getSchemaStore();
for ( DynamicRecord record : command.getRecordsAfter() )
{
track( schemaStore, record.getId() );
track( schemaStore, record );
}
return false;
}
Expand All @@ -145,8 +148,9 @@ public void close() throws Exception
}
}

private void track( RecordStore<?> store, long id )
private void track( RecordStore<?> store, AbstractBaseRecord record )
{
long id = max( record.getId(), record.requiresSecondaryUnit() ? record.getSecondaryUnitId() : -1 );
HighId highId = highIds.get( store );
if ( highId == null )
{
Expand All @@ -158,23 +162,23 @@ private void track( RecordStore<?> store, long id )
}
}

private void track( RecordStore<?> store, Command command )
private <RECORD extends AbstractBaseRecord> void track( RecordStore<RECORD> store, BaseCommand<RECORD> command )
{
track( store, command.getKey() );
track( store, command.getAfter() );
}

private void track( RecordStore<?> store, Collection<? extends AbstractBaseRecord> records )
{
for ( AbstractBaseRecord record : records )
{
track( store, record.getId() );
track( store, record );
}
}

private <RECORD extends TokenRecord, TOKEN extends Token>
void trackToken( TokenStore<RECORD, TOKEN> tokenStore, TokenCommand<RECORD> tokenCommand )
{
track( tokenStore, tokenCommand );
track( tokenStore, tokenCommand.getAfter() );
track( tokenStore.getNameStore(), tokenCommand.getAfter().getNameRecords() );
}

Expand Down
Expand Up @@ -24,6 +24,12 @@

import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.store.PropertyType;
import org.neo4j.kernel.impl.store.record.NodeRecord;
import org.neo4j.kernel.impl.store.record.RelationshipGroupRecord;
import org.neo4j.kernel.impl.store.record.RelationshipRecord;
import org.neo4j.kernel.impl.transaction.command.Command.NodeCommand;
import org.neo4j.kernel.impl.transaction.command.Command.RelationshipCommand;
import org.neo4j.kernel.impl.transaction.command.Command.RelationshipGroupCommand;
import org.neo4j.test.NeoStoresRule;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -95,4 +101,39 @@ public void shouldUpdateHighIdsOnExternalTransaction() throws Exception
assertEquals( "PropertyStore DynamicArrayStore", 9 + 1, neoStores.getPropertyStore().getArrayStore().getHighId() );
assertEquals( "SchemaStore", 20 + 1, neoStores.getSchemaStore().getHighId() );
}

@Test
public void shouldTrackSecondaryUnitIdsAsWell() throws Exception
{
// GIVEN
NeoStores neoStores = neoStoresRule.open();
HighIdTransactionApplier tracker = new HighIdTransactionApplier( neoStores );

NodeRecord node = new NodeRecord( 5 ).initialize( true, 123, true, 456, 0 );
node.setSecondaryUnitId( 6 );
node.setRequiresSecondaryUnit( true );

RelationshipRecord relationship = new RelationshipRecord( 10 )
.initialize( true, 1, 2, 3, 4, 5, 6, 7, 8, true, true );
relationship.setSecondaryUnitId( 12 );
relationship.setRequiresSecondaryUnit( true );

RelationshipGroupRecord relationshipGroup = new RelationshipGroupRecord( 8 )
.initialize( true, 0, 1, 2, 3, 4, 5 );
relationshipGroup.setSecondaryUnitId( 20 );
relationshipGroup.setRequiresSecondaryUnit( true );

// WHEN
tracker.visitNodeCommand( new NodeCommand( new NodeRecord( node.getId() ), node ) );
tracker.visitRelationshipCommand( new RelationshipCommand(
new RelationshipRecord( relationship.getId() ), relationship ) );
tracker.visitRelationshipGroupCommand( new RelationshipGroupCommand(
new RelationshipGroupRecord( relationshipGroup.getId() ), relationshipGroup ) );
tracker.close();

// THEN
assertEquals( node.getSecondaryUnitId()+1, neoStores.getNodeStore().getHighId() );
assertEquals( relationship.getSecondaryUnitId()+1, neoStores.getRelationshipStore().getHighId() );
assertEquals( relationshipGroup.getSecondaryUnitId()+1, neoStores.getRelationshipGroupStore().getHighId() );
}
}

0 comments on commit fd34650

Please sign in to comment.