Search in sources :

Example 1 with LockService

use of org.neo4j.lock.LockService 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(LockType.class))).thenReturn(LockService.NO_LOCK);
    when(lockService.acquireRelationshipLock(anyLong(), any(LockType.class))).thenReturn(LockService.NO_LOCK);
    Map<IdType, WorkSync<IdGenerator, IdGeneratorUpdateWork>> idGeneratorWorkSyncs = new EnumMap<>(IdType.class);
    for (IdType idType : IdType.values()) {
        idGeneratorWorkSyncs.put(idType, new WorkSync<>(idGeneratorFactory.get(idType)));
    }
    NeoStoreTransactionApplierFactory applier = new NeoStoreTransactionApplierFactory(INTERNAL, neoStores, mock(CacheAccessBackDoor.class), lockService);
    CommandsToApply tx = new GroupOfCommands(transactionId, commands);
    CommandHandlerContract.apply(applier, txApplier -> {
        tx.accept(txApplier);
        return false;
    }, new GroupOfCommands(transactionId, commands));
}
Also used : CommandsToApply(org.neo4j.storageengine.api.CommandsToApply) WorkSync(org.neo4j.util.concurrent.WorkSync) LockService(org.neo4j.lock.LockService) LockType(org.neo4j.lock.LockType) EnumMap(java.util.EnumMap) IdType(org.neo4j.internal.id.IdType)

Example 2 with LockService

use of org.neo4j.lock.LockService in project neo4j by neo4j.

the class MultiIndexPopulationConcurrentUpdatesIT method dynamicIndexStoreViewWrapper.

private DynamicIndexStoreView dynamicIndexStoreViewWrapper(Runnable customAction, Supplier<StorageReader> readerSupplier, IndexingService.IndexProxyProvider indexProxies, Config config, JobScheduler scheduler) {
    LockService lockService = LockService.NO_LOCK_SERVICE;
    Locks locks = org.neo4j.kernel.impl.locking.Locks.NO_LOCKS;
    FullScanStoreView fullScanStoreView = new FullScanStoreView(lockService, readerSupplier, Config.defaults(), scheduler);
    return new DynamicIndexStoreViewWrapper(fullScanStoreView, indexProxies, lockService, locks, readerSupplier, customAction, config, scheduler);
}
Also used : LockService(org.neo4j.lock.LockService) FullScanStoreView(org.neo4j.kernel.impl.transaction.state.storeview.FullScanStoreView) Locks(org.neo4j.kernel.impl.locking.Locks)

Example 3 with LockService

use of org.neo4j.lock.LockService in project neo4j by neo4j.

the class TransactionRecordStateTest method shouldLockUpdatedNodes.

@Test
void shouldLockUpdatedNodes() throws Exception {
    neoStores = createStores();
    // given
    LockService locks = mock(LockService.class, RETURNS_MOCKS);
    NodeStore nodeStore = neoStores.getNodeStore();
    long[] nodes = { // allocate ids
    nodeStore.nextId(NULL), nodeStore.nextId(NULL), nodeStore.nextId(NULL), nodeStore.nextId(NULL), nodeStore.nextId(NULL), nodeStore.nextId(NULL), nodeStore.nextId(NULL) };
    {
        // create the node records that we will modify in our main tx.
        TransactionRecordState tx = newTransactionRecordState();
        for (int i = 1; i < nodes.length - 1; i++) {
            tx.nodeCreate(nodes[i]);
        }
        tx.nodeAddProperty(nodes[3], 0, Values.of("old"));
        tx.nodeAddProperty(nodes[4], 0, Values.of("old"));
        TransactionApplierFactory applier = buildApplier(locks);
        apply(applier, transaction(tx));
    }
    reset(locks);
    // These are the changes we want to assert locking on
    TransactionRecordState tx = newTransactionRecordState();
    tx.nodeCreate(nodes[0]);
    tx.addLabelToNode(0, nodes[1]);
    tx.nodeAddProperty(nodes[2], 0, Values.of("value"));
    tx.nodeChangeProperty(nodes[3], 0, Values.of("value"));
    tx.nodeRemoveProperty(nodes[4], 0);
    tx.nodeDelete(nodes[5]);
    tx.nodeCreate(nodes[6]);
    tx.addLabelToNode(0, nodes[6]);
    tx.nodeAddProperty(nodes[6], 0, Values.of("value"));
    // commit( tx );
    TransactionApplierFactory applier = buildApplier(locks);
    apply(applier, transaction(tx));
    // then
    // create node, NodeCommand == 1 update
    verify(locks).acquireNodeLock(nodes[0], EXCLUSIVE);
    // add label, NodeCommand == 1 update
    verify(locks).acquireNodeLock(nodes[1], EXCLUSIVE);
    // add property, NodeCommand and PropertyCommand == 2 updates
    verify(locks, times(2)).acquireNodeLock(nodes[2], EXCLUSIVE);
    // update property, in place, PropertyCommand == 1 update
    verify(locks).acquireNodeLock(nodes[3], EXCLUSIVE);
    // remove property, updates the Node and the Property == 2 updates
    verify(locks, times(2)).acquireNodeLock(nodes[4], EXCLUSIVE);
    // delete node, single NodeCommand == 1 update
    verify(locks).acquireNodeLock(nodes[5], EXCLUSIVE);
    // create and add-label goes into the NodeCommand, add property is a PropertyCommand == 2 updates
    verify(locks, times(2)).acquireNodeLock(nodes[6], EXCLUSIVE);
}
Also used : NodeStore(org.neo4j.kernel.impl.store.NodeStore) LockService(org.neo4j.lock.LockService) Test(org.junit.jupiter.api.Test)

Example 4 with LockService

use of org.neo4j.lock.LockService in project neo4j by neo4j.

the class RecordStorageEngineTest method shouldCloseLockGroupAfterAppliers.

@Test
void shouldCloseLockGroupAfterAppliers() throws Exception {
    // given
    long nodeId = 5;
    LockService lockService = mock(LockService.class);
    Lock nodeLock = mock(Lock.class);
    when(lockService.acquireNodeLock(nodeId, EXCLUSIVE)).thenReturn(nodeLock);
    // <-- simply so that we can use InOrder mockito construct
    Consumer<Boolean> applierCloseCall = mock(Consumer.class);
    CapturingTransactionApplierFactoryChain applier = new CapturingTransactionApplierFactoryChain(applierCloseCall);
    RecordStorageEngine engine = recordStorageEngineBuilder().lockService(lockService).transactionApplierTransformer(applier::wrapAroundActualApplier).build();
    CommandsToApply commandsToApply = mock(CommandsToApply.class);
    when(commandsToApply.cursorContext()).thenReturn(NULL);
    when(commandsToApply.accept(any())).thenAnswer(invocationOnMock -> {
        // Visit one node command
        Visitor<StorageCommand, IOException> visitor = invocationOnMock.getArgument(0);
        NodeRecord after = new NodeRecord(nodeId);
        after.setInUse(true);
        visitor.visit(new Command.NodeCommand(new NodeRecord(nodeId), after));
        return null;
    });
    // when
    engine.apply(commandsToApply, TransactionApplicationMode.INTERNAL);
    // then
    InOrder inOrder = inOrder(lockService, applierCloseCall, nodeLock);
    inOrder.verify(lockService).acquireNodeLock(nodeId, EXCLUSIVE);
    inOrder.verify(applierCloseCall).accept(true);
    inOrder.verify(nodeLock).release();
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) LockService(org.neo4j.lock.LockService) StorageCommand(org.neo4j.storageengine.api.StorageCommand) IOException(java.io.IOException) Lock(org.neo4j.lock.Lock) CommandsToApply(org.neo4j.storageengine.api.CommandsToApply) NodeRecord(org.neo4j.kernel.impl.store.record.NodeRecord) StorageCommand(org.neo4j.storageengine.api.StorageCommand) Test(org.junit.jupiter.api.Test)

Aggregations

LockService (org.neo4j.lock.LockService)4 Test (org.junit.jupiter.api.Test)2 CommandsToApply (org.neo4j.storageengine.api.CommandsToApply)2 IOException (java.io.IOException)1 EnumMap (java.util.EnumMap)1 InOrder (org.mockito.InOrder)1 IdType (org.neo4j.internal.id.IdType)1 Locks (org.neo4j.kernel.impl.locking.Locks)1 NodeStore (org.neo4j.kernel.impl.store.NodeStore)1 NodeRecord (org.neo4j.kernel.impl.store.record.NodeRecord)1 FullScanStoreView (org.neo4j.kernel.impl.transaction.state.storeview.FullScanStoreView)1 Lock (org.neo4j.lock.Lock)1 LockType (org.neo4j.lock.LockType)1 StorageCommand (org.neo4j.storageengine.api.StorageCommand)1 WorkSync (org.neo4j.util.concurrent.WorkSync)1