use of org.neo4j.kernel.impl.locking.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(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));
}
use of org.neo4j.kernel.impl.locking.LockService 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);
}
use of org.neo4j.kernel.impl.locking.LockService in project neo4j by neo4j.
the class RecordStorageEngine method storeStatementSupplier.
private Supplier<StorageStatement> storeStatementSupplier(NeoStores neoStores) {
Supplier<IndexReaderFactory> indexReaderFactory = () -> new IndexReaderFactory.Caching(indexingService);
LockService lockService = takePropertyReadLocks ? this.lockService : NO_LOCK_SERVICE;
return () -> new StoreStatement(neoStores, indexReaderFactory, labelScanStore::newReader, lockService);
}
use of org.neo4j.kernel.impl.locking.LockService in project neo4j by neo4j.
the class BatchInserterImpl method repopulateAllIndexes.
private void repopulateAllIndexes() throws IOException, IndexEntryConflictException {
if (!labelsTouched) {
return;
}
final IndexRule[] rules = getIndexesNeedingPopulation();
final IndexPopulator[] populators = new IndexPopulator[rules.length];
// the store is uncontended at this point, so creating a local LockService is safe.
final NewIndexDescriptor[] descriptors = new NewIndexDescriptor[rules.length];
for (int i = 0; i < rules.length; i++) {
IndexRule rule = rules[i];
descriptors[i] = rule.getIndexDescriptor();
populators[i] = schemaIndexProviders.apply(rule.getProviderDescriptor()).getPopulator(rule.getId(), descriptors[i], new IndexSamplingConfig(config));
populators[i].create();
}
Visitor<NodeUpdates, IOException> propertyUpdateVisitor = updates -> {
for (int i = 0; i < descriptors.length; i++) {
Optional<IndexEntryUpdate> update = updates.forIndex(descriptors[i].schema());
if (update.isPresent()) {
try {
populators[i].add(Collections.singletonList(update.get()));
} catch (IndexEntryConflictException conflict) {
throw conflict.notAllowed(descriptors[i]);
}
}
}
return true;
};
List<NewIndexDescriptor> descriptorList = Arrays.asList(descriptors);
int[] labelIds = descriptorList.stream().mapToInt(index -> index.schema().getLabelId()).toArray();
int[] propertyKeyIds = descriptorList.stream().flatMapToInt(d -> Arrays.stream(d.schema().getPropertyIds())).toArray();
InitialNodeLabelCreationVisitor labelUpdateVisitor = new InitialNodeLabelCreationVisitor();
StoreScan<IOException> storeScan = indexStoreView.visitNodes(labelIds, (propertyKeyId) -> PrimitiveIntCollections.contains(propertyKeyIds, propertyKeyId), propertyUpdateVisitor, labelUpdateVisitor, true);
storeScan.run();
for (IndexPopulator populator : populators) {
populator.verifyDeferredConstraints(indexStoreView);
populator.close(true);
}
labelUpdateVisitor.close();
}
Aggregations