Search in sources :

Example 1 with NeoStoreBatchTransactionApplier

use of org.neo4j.kernel.impl.transaction.command.NeoStoreBatchTransactionApplier in project neo4j by neo4j.

the class RecordStorageEngine method applier.

/**
     * Creates a {@link BatchTransactionApplierFacade} that is to be used for all transactions
     * in a batch. Each transaction is handled by a {@link TransactionApplierFacade} which wraps the
     * individual {@link TransactionApplier}s returned by the wrapped {@link BatchTransactionApplier}s.
     *
     * After all transactions have been applied the appliers are closed.
     */
protected BatchTransactionApplierFacade applier(TransactionApplicationMode mode) {
    ArrayList<BatchTransactionApplier> appliers = new ArrayList<>();
    // Graph store application. The order of the decorated store appliers is irrelevant
    appliers.add(new NeoStoreBatchTransactionApplier(neoStores, cacheAccess, lockService));
    if (mode.needsHighIdTracking()) {
        appliers.add(new HighIdBatchTransactionApplier(neoStores));
    }
    if (mode.needsCacheInvalidationOnUpdates()) {
        appliers.add(new CacheInvalidationBatchTransactionApplier(neoStores, cacheAccess));
    }
    // Schema index application
    appliers.add(new IndexBatchTransactionApplier(indexingService, labelScanStoreSync, indexUpdatesSync, neoStores.getNodeStore(), new PropertyLoader(neoStores), indexUpdatesConverter, mode));
    // Legacy index application
    appliers.add(new LegacyBatchIndexApplier(indexConfigStore, legacyIndexApplierLookup, legacyIndexTransactionOrdering, mode));
    // Counts store application
    appliers.add(new CountsStoreBatchTransactionApplier(neoStores.getCounts(), mode));
    // Perform the application
    return new BatchTransactionApplierFacade(appliers.toArray(new BatchTransactionApplier[appliers.size()]));
}
Also used : HighIdBatchTransactionApplier(org.neo4j.kernel.impl.transaction.command.HighIdBatchTransactionApplier) ArrayList(java.util.ArrayList) NeoStoreBatchTransactionApplier(org.neo4j.kernel.impl.transaction.command.NeoStoreBatchTransactionApplier) BatchTransactionApplierFacade(org.neo4j.kernel.impl.api.BatchTransactionApplierFacade) PropertyLoader(org.neo4j.kernel.impl.transaction.state.PropertyLoader) CountsStoreBatchTransactionApplier(org.neo4j.kernel.impl.api.CountsStoreBatchTransactionApplier) HighIdBatchTransactionApplier(org.neo4j.kernel.impl.transaction.command.HighIdBatchTransactionApplier) BatchTransactionApplier(org.neo4j.kernel.impl.api.BatchTransactionApplier) IndexBatchTransactionApplier(org.neo4j.kernel.impl.transaction.command.IndexBatchTransactionApplier) CacheInvalidationBatchTransactionApplier(org.neo4j.kernel.impl.transaction.command.CacheInvalidationBatchTransactionApplier) NeoStoreBatchTransactionApplier(org.neo4j.kernel.impl.transaction.command.NeoStoreBatchTransactionApplier) IndexBatchTransactionApplier(org.neo4j.kernel.impl.transaction.command.IndexBatchTransactionApplier) LegacyBatchIndexApplier(org.neo4j.kernel.impl.api.LegacyBatchIndexApplier) CacheInvalidationBatchTransactionApplier(org.neo4j.kernel.impl.transaction.command.CacheInvalidationBatchTransactionApplier) CountsStoreBatchTransactionApplier(org.neo4j.kernel.impl.api.CountsStoreBatchTransactionApplier)

Example 2 with NeoStoreBatchTransactionApplier

use of org.neo4j.kernel.impl.transaction.command.NeoStoreBatchTransactionApplier in project neo4j by neo4j.

the class ApplyRecoveredTransactionsTest method applyExternalTransaction.

private void applyExternalTransaction(long transactionId, Command... commands) throws Exception {
    LockService lockService = mock(LockService.class);
    when(lockService.acquireNodeLock(anyLong(), any(LockService.LockType.class))).thenReturn(LockService.NO_LOCK);
    when(lockService.acquireRelationshipLock(anyLong(), any(LockService.LockType.class))).thenReturn(LockService.NO_LOCK);
    NeoStoreBatchTransactionApplier applier = new NeoStoreBatchTransactionApplier(neoStores, mock(CacheAccessBackDoor.class), lockService);
    TransactionRepresentation tx = new PhysicalTransactionRepresentation(Arrays.asList(commands));
    CommandHandlerContract.apply(applier, txApplier -> {
        tx.accept(txApplier);
        return false;
    }, new TransactionToApply(tx, transactionId));
}
Also used : TransactionToApply(org.neo4j.kernel.impl.api.TransactionToApply) LockService(org.neo4j.kernel.impl.locking.LockService) TransactionRepresentation(org.neo4j.kernel.impl.transaction.TransactionRepresentation) PhysicalTransactionRepresentation(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation) NeoStoreBatchTransactionApplier(org.neo4j.kernel.impl.transaction.command.NeoStoreBatchTransactionApplier) CacheAccessBackDoor(org.neo4j.kernel.impl.core.CacheAccessBackDoor) PhysicalTransactionRepresentation(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation)

Example 3 with NeoStoreBatchTransactionApplier

use of org.neo4j.kernel.impl.transaction.command.NeoStoreBatchTransactionApplier in project neo4j by neo4j.

the class TransactionRecordStateTest method shouldLockUpdatedNodes.

@Test
public void shouldLockUpdatedNodes() throws Exception {
    // given
    LockService locks = mock(LockService.class, new Answer<Object>() {

        @Override
        public synchronized Object answer(final InvocationOnMock invocation) throws Throwable {
            // This is necessary because finalize() will also be called
            String name = invocation.getMethod().getName();
            if (name.equals("acquireNodeLock") || name.equals("acquireRelationshipLock")) {
                return mock(Lock.class, (Answer) invocationOnMock -> null);
            }
            return null;
        }
    });
    NeoStores neoStores = neoStoresRule.open();
    NodeStore nodeStore = neoStores.getNodeStore();
    long[] nodes = { // allocate ids
    nodeStore.nextId(), nodeStore.nextId(), nodeStore.nextId(), nodeStore.nextId(), nodeStore.nextId(), nodeStore.nextId(), nodeStore.nextId() };
    {
        // create the node records that we will modify in our main tx.
        TransactionRecordState tx = newTransactionRecordState(neoStores);
        for (int i = 1; i < nodes.length - 1; i++) {
            tx.nodeCreate(nodes[i]);
        }
        tx.nodeAddProperty(nodes[3], 0, "old");
        tx.nodeAddProperty(nodes[4], 0, "old");
        BatchTransactionApplier applier = new NeoStoreBatchTransactionApplier(neoStores, mock(CacheAccessBackDoor.class), locks);
        apply(applier, transaction(tx));
    }
    reset(locks);
    // These are the changes we want to assert locking on
    TransactionRecordState tx = newTransactionRecordState(neoStores);
    tx.nodeCreate(nodes[0]);
    tx.addLabelToNode(0, nodes[1]);
    tx.nodeAddProperty(nodes[2], 0, "value");
    tx.nodeChangeProperty(nodes[3], 0, "value");
    tx.nodeRemoveProperty(nodes[4], 0);
    tx.nodeDelete(nodes[5]);
    tx.nodeCreate(nodes[6]);
    tx.addLabelToNode(0, nodes[6]);
    tx.nodeAddProperty(nodes[6], 0, "value");
    //commit( tx );
    BatchTransactionApplier applier = new NeoStoreBatchTransactionApplier(neoStores, mock(CacheAccessBackDoor.class), locks);
    apply(applier, transaction(tx));
    // then
    // create node, NodeCommand == 1 update
    verify(locks, times(1)).acquireNodeLock(nodes[0], LockService.LockType.WRITE_LOCK);
    // add label, NodeCommand == 1 update
    verify(locks, times(1)).acquireNodeLock(nodes[1], LockService.LockType.WRITE_LOCK);
    // add property, NodeCommand and PropertyCommand == 2 updates
    verify(locks, times(2)).acquireNodeLock(nodes[2], LockService.LockType.WRITE_LOCK);
    // update property, in place, PropertyCommand == 1 update
    verify(locks, times(1)).acquireNodeLock(nodes[3], LockService.LockType.WRITE_LOCK);
    // remove property, updates the Node and the Property == 2 updates
    verify(locks, times(2)).acquireNodeLock(nodes[4], LockService.LockType.WRITE_LOCK);
    // delete node, single NodeCommand == 1 update
    verify(locks, times(1)).acquireNodeLock(nodes[5], LockService.LockType.WRITE_LOCK);
    // create and add-label goes into the NodeCommand, add property is a PropertyCommand == 2 updates
    verify(locks, times(2)).acquireNodeLock(nodes[6], LockService.LockType.WRITE_LOCK);
}
Also used : LockService(org.neo4j.kernel.impl.locking.LockService) Lock(org.neo4j.kernel.impl.locking.Lock) Answer(org.mockito.stubbing.Answer) NodeStore(org.neo4j.kernel.impl.store.NodeStore) InvocationOnMock(org.mockito.invocation.InvocationOnMock) NeoStores(org.neo4j.kernel.impl.store.NeoStores) NeoStoreBatchTransactionApplier(org.neo4j.kernel.impl.transaction.command.NeoStoreBatchTransactionApplier) NeoStoreBatchTransactionApplier(org.neo4j.kernel.impl.transaction.command.NeoStoreBatchTransactionApplier) BatchTransactionApplier(org.neo4j.kernel.impl.api.BatchTransactionApplier) CacheAccessBackDoor(org.neo4j.kernel.impl.core.CacheAccessBackDoor) Test(org.junit.Test)

Example 4 with NeoStoreBatchTransactionApplier

use of org.neo4j.kernel.impl.transaction.command.NeoStoreBatchTransactionApplier in project neo4j by neo4j.

the class TransactionRecordStateTest method shouldExtractUpdateCommandsInCorrectOrder.

@Test
public void shouldExtractUpdateCommandsInCorrectOrder() throws Throwable {
    // GIVEN
    NeoStores neoStores = neoStoresRule.open(GraphDatabaseSettings.dense_node_threshold.name(), "1");
    TransactionRecordState recordState = newTransactionRecordState(neoStores);
    long nodeId = 0, relId1 = 1, relId2 = 2, relId3 = 3;
    recordState.nodeCreate(nodeId);
    recordState.relCreate(relId1, 0, nodeId, nodeId);
    recordState.relCreate(relId2, 0, nodeId, nodeId);
    recordState.nodeAddProperty(nodeId, 0, 101);
    BatchTransactionApplier applier = new NeoStoreBatchTransactionApplier(neoStores, mock(CacheAccessBackDoor.class), LockService.NO_LOCK_SERVICE);
    apply(applier, transaction(recordState));
    recordState = newTransactionRecordState(neoStores);
    recordState.nodeChangeProperty(nodeId, 0, 102);
    recordState.relCreate(relId3, 0, nodeId, nodeId);
    recordState.relAddProperty(relId1, 0, 123);
    // WHEN
    Collection<StorageCommand> commands = new ArrayList<>();
    recordState.extractCommands(commands);
    // THEN
    Iterator<StorageCommand> commandIterator = commands.iterator();
    // added rel property
    assertCommand(commandIterator.next(), PropertyCommand.class);
    // created relationship relId3
    assertCommand(commandIterator.next(), RelationshipCommand.class);
    // rest is updates...
    assertCommand(commandIterator.next(), PropertyCommand.class);
    assertCommand(commandIterator.next(), RelationshipCommand.class);
    assertCommand(commandIterator.next(), RelationshipCommand.class);
    assertCommand(commandIterator.next(), Command.RelationshipGroupCommand.class);
    assertCommand(commandIterator.next(), NodeCommand.class);
    assertFalse(commandIterator.hasNext());
}
Also used : NodeCommand(org.neo4j.kernel.impl.transaction.command.Command.NodeCommand) PropertyCommand(org.neo4j.kernel.impl.transaction.command.Command.PropertyCommand) RelationshipGroupCommand(org.neo4j.kernel.impl.transaction.command.Command.RelationshipGroupCommand) StorageCommand(org.neo4j.storageengine.api.StorageCommand) Command(org.neo4j.kernel.impl.transaction.command.Command) RelationshipCommand(org.neo4j.kernel.impl.transaction.command.Command.RelationshipCommand) NeoStores(org.neo4j.kernel.impl.store.NeoStores) StorageCommand(org.neo4j.storageengine.api.StorageCommand) NeoStoreBatchTransactionApplier(org.neo4j.kernel.impl.transaction.command.NeoStoreBatchTransactionApplier) ArrayList(java.util.ArrayList) NeoStoreBatchTransactionApplier(org.neo4j.kernel.impl.transaction.command.NeoStoreBatchTransactionApplier) BatchTransactionApplier(org.neo4j.kernel.impl.api.BatchTransactionApplier) CacheAccessBackDoor(org.neo4j.kernel.impl.core.CacheAccessBackDoor) Test(org.junit.Test)

Example 5 with NeoStoreBatchTransactionApplier

use of org.neo4j.kernel.impl.transaction.command.NeoStoreBatchTransactionApplier in project neo4j by neo4j.

the class TransactionRecordStateTest method movingBilaterallyOfTheDenseNodeThresholdIsConsistent.

@Test
public void movingBilaterallyOfTheDenseNodeThresholdIsConsistent() throws Exception {
    // GIVEN
    NeoStores neoStores = neoStoresRule.open(GraphDatabaseSettings.dense_node_threshold.name(), "10");
    TransactionRecordState tx = newTransactionRecordState(neoStores);
    long nodeId = neoStores.getNodeStore().nextId();
    tx.nodeCreate(nodeId);
    int typeA = (int) neoStores.getRelationshipTypeTokenStore().nextId();
    tx.createRelationshipTypeToken("A", typeA);
    createRelationships(neoStores, tx, nodeId, typeA, INCOMING, 20);
    BatchTransactionApplier applier = new NeoStoreBatchTransactionApplier(neoStores, mock(CacheAccessBackDoor.class), LockService.NO_LOCK_SERVICE);
    apply(applier, transaction(tx));
    tx = newTransactionRecordState(neoStores);
    int typeB = 1;
    tx.createRelationshipTypeToken("B", typeB);
    // WHEN
    // i remove enough relationships to become dense and remove enough to become not dense
    long[] relationshipsOfTypeB = createRelationships(neoStores, tx, nodeId, typeB, OUTGOING, 5);
    for (long relationshipToDelete : relationshipsOfTypeB) {
        tx.relDelete(relationshipToDelete);
    }
    PhysicalTransactionRepresentation ptx = transactionRepresentationOf(tx);
    apply(applier, ptx);
    // THEN
    // The dynamic label record in before should be the same id as in after, and should be in use
    final AtomicBoolean foundRelationshipGroupInUse = new AtomicBoolean();
    ptx.accept(command -> ((Command) command).handle(new CommandVisitor.Adapter() {

        @Override
        public boolean visitRelationshipGroupCommand(Command.RelationshipGroupCommand command) throws IOException {
            if (command.getAfter().inUse()) {
                if (!foundRelationshipGroupInUse.get()) {
                    foundRelationshipGroupInUse.set(true);
                } else {
                    fail();
                }
            }
            return false;
        }
    }));
    assertTrue("Did not create relationship group command", foundRelationshipGroupInUse.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NeoStores(org.neo4j.kernel.impl.store.NeoStores) NeoStoreBatchTransactionApplier(org.neo4j.kernel.impl.transaction.command.NeoStoreBatchTransactionApplier) RelationshipGroupCommand(org.neo4j.kernel.impl.transaction.command.Command.RelationshipGroupCommand) NeoStoreBatchTransactionApplier(org.neo4j.kernel.impl.transaction.command.NeoStoreBatchTransactionApplier) BatchTransactionApplier(org.neo4j.kernel.impl.api.BatchTransactionApplier) CacheAccessBackDoor(org.neo4j.kernel.impl.core.CacheAccessBackDoor) PhysicalTransactionRepresentation(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation) Test(org.junit.Test)

Aggregations

NeoStoreBatchTransactionApplier (org.neo4j.kernel.impl.transaction.command.NeoStoreBatchTransactionApplier)11 BatchTransactionApplier (org.neo4j.kernel.impl.api.BatchTransactionApplier)10 CacheAccessBackDoor (org.neo4j.kernel.impl.core.CacheAccessBackDoor)9 Test (org.junit.Test)7 NeoStores (org.neo4j.kernel.impl.store.NeoStores)7 ArrayList (java.util.ArrayList)3 RelationshipGroupCommand (org.neo4j.kernel.impl.transaction.command.Command.RelationshipGroupCommand)3 PhysicalTransactionRepresentation (org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 LockService (org.neo4j.kernel.impl.locking.LockService)2 TransactionRepresentation (org.neo4j.kernel.impl.transaction.TransactionRepresentation)2 Command (org.neo4j.kernel.impl.transaction.command.Command)2 NodeCommand (org.neo4j.kernel.impl.transaction.command.Command.NodeCommand)2 PropertyCommand (org.neo4j.kernel.impl.transaction.command.Command.PropertyCommand)2 RelationshipCommand (org.neo4j.kernel.impl.transaction.command.Command.RelationshipCommand)2 StorageCommand (org.neo4j.storageengine.api.StorageCommand)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1 BatchTransactionApplierFacade (org.neo4j.kernel.impl.api.BatchTransactionApplierFacade)1