Search in sources :

Example 1 with LockGroup

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

the class ConsistencyCheckingApplierTest method setUp.

@BeforeEach
void setUp() {
    Config config = Config.defaults(neo4j_home, directory.homePath());
    DatabaseLayout layout = DatabaseLayout.of(config);
    neoStores = new StoreFactory(layout, config, new DefaultIdGeneratorFactory(directory.getFileSystem(), immediate(), DEFAULT_DATABASE_NAME), pageCache, directory.getFileSystem(), NullLogProvider.getInstance(), PageCacheTracer.NULL, writable()).openAllNeoStores(true);
    RelationshipStore relationshipStore = neoStores.getRelationshipStore();
    checker = new ConsistencyCheckingApplier(relationshipStore, CursorContext.NULL);
    BatchContext batchContext = mock(BatchContext.class);
    when(batchContext.getLockGroup()).thenReturn(new LockGroup());
    applier = new NeoStoreTransactionApplier(CommandVersion.AFTER, neoStores, mock(CacheAccessBackDoor.class), LockService.NO_LOCK_SERVICE, 0, batchContext, CursorContext.NULL);
    appliers = new TransactionApplier[] { checker, applier };
}
Also used : LockGroup(org.neo4j.lock.LockGroup) Config(org.neo4j.configuration.Config) DatabaseLayout(org.neo4j.io.layout.DatabaseLayout) DefaultIdGeneratorFactory(org.neo4j.internal.id.DefaultIdGeneratorFactory) RelationshipStore(org.neo4j.kernel.impl.store.RelationshipStore) StoreFactory(org.neo4j.kernel.impl.store.StoreFactory) ConsistencyCheckingApplier(org.neo4j.internal.recordstorage.ConsistencyCheckingApplierFactory.ConsistencyCheckingApplier) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with LockGroup

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

the class IndexTransactionApplierFactoryTest method shouldRegisterIndexesToActivateIntoTheActivator.

@Test
void shouldRegisterIndexesToActivateIntoTheActivator() throws Exception {
    // given
    IndexUpdateListener indexUpdateListener = mock(IndexUpdateListener.class);
    IndexActivator indexActivator = new IndexActivator(indexUpdateListener);
    long indexId1 = 1;
    long indexId2 = 2;
    long indexId3 = 3;
    long constraintId1 = 10;
    long constraintId2 = 11;
    long constraintId3 = 12;
    String providerKey = "index-key";
    String providerVersion = "v1";
    IndexDescriptor rule1 = uniqueForSchema(forLabel(1, 1), providerKey, providerVersion, indexId1, constraintId1);
    IndexDescriptor rule2 = uniqueForSchema(forLabel(2, 1), providerKey, providerVersion, indexId2, constraintId2);
    IndexDescriptor rule3 = uniqueForSchema(forLabel(3, 1), providerKey, providerVersion, indexId3, constraintId3);
    IndexTransactionApplierFactory applier = new IndexTransactionApplierFactory(indexUpdateListener);
    var batchContext = mock(BatchContext.class);
    when(batchContext.getLockGroup()).thenReturn(new LockGroup());
    when(batchContext.indexUpdates()).thenReturn(mock(IndexUpdates.class));
    when(batchContext.getIndexActivator()).thenReturn(indexActivator);
    try (var txApplier = applier.startTx(new GroupOfCommands(), batchContext)) {
        // activate index 1
        txApplier.visitSchemaRuleCommand(new Command.SchemaRuleCommand(new SchemaRecord(rule1.getId()), asSchemaRecord(rule1, true), rule1));
        // activate index 2
        txApplier.visitSchemaRuleCommand(new Command.SchemaRuleCommand(new SchemaRecord(rule2.getId()), asSchemaRecord(rule2, true), rule2));
        // activate index 3
        txApplier.visitSchemaRuleCommand(new Command.SchemaRuleCommand(new SchemaRecord(rule3.getId()), asSchemaRecord(rule3, true), rule3));
        // drop index 2
        txApplier.visitSchemaRuleCommand(new Command.SchemaRuleCommand(asSchemaRecord(rule2, true), asSchemaRecord(rule2, false), rule2));
    }
    verify(indexUpdateListener).dropIndex(rule2);
    indexActivator.close();
    verify(indexUpdateListener).activateIndex(rule1);
    verify(indexUpdateListener).activateIndex(rule3);
    verifyNoMoreInteractions(indexUpdateListener);
}
Also used : LockGroup(org.neo4j.lock.LockGroup) IndexUpdateListener(org.neo4j.storageengine.api.IndexUpdateListener) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) NodeCommand(org.neo4j.internal.recordstorage.Command.NodeCommand) SchemaRecord(org.neo4j.kernel.impl.store.record.SchemaRecord) Test(org.junit.jupiter.api.Test)

Example 3 with LockGroup

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

the class ParallelRecoveryVisitor method visit.

@Override
public boolean visit(CommittedTransactionRepresentation transaction) throws Exception {
    checkFailure();
    // We need to know the starting point for the "is it my turn yet?" check below that each thread needs to do before acquiring the locks
    prevLockedTxId.compareAndSet(-1, transaction.getCommitEntry().getTxId() - stride);
    // TODO Also consider the memory usage of all active transaction instances and apply back-pressure if surpassing it
    appliers.submit(() -> {
        long txId = transaction.getCommitEntry().getTxId();
        while (prevLockedTxId.get() != txId - stride) {
            Thread.onSpinWait();
            checkFailure();
        }
        try (LockGroup locks = new LockGroup()) {
            storageEngine.lockRecoveryCommands(transaction.getTransactionRepresentation(), lockService, locks, mode);
            boolean myTurn = prevLockedTxId.compareAndSet(txId - stride, txId);
            checkState(myTurn, "Something wrong with the algorithm, I thought it was my turn, but apparently it wasn't %d", txId);
            apply(transaction);
        } catch (Throwable e) {
            failure.compareAndSet(null, e);
        }
        return null;
    });
    return false;
}
Also used : LockGroup(org.neo4j.lock.LockGroup)

Example 4 with LockGroup

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

the class CommandHandlerContract method apply.

/**
 * Simply calls through to the {@link CommandStream#accept(Visitor)} method for each {@link
 * CommandsToApply} given. This assumes that the {@link TransactionApplierFactory} will return {@link
 * TransactionApplier}s which actually do the work and that the transaction has all the relevant data.
 *
 * @param applier to use
 * @param transactions to apply
 */
public static void apply(TransactionApplierFactory applier, CommandsToApply... transactions) throws Exception {
    var batchContext = mock(BatchContext.class);
    when(batchContext.getLockGroup()).thenReturn(new LockGroup());
    when(batchContext.getIdUpdateListener()).thenReturn(IdUpdateListener.IGNORE);
    when(batchContext.getIndexActivator()).thenReturn(mock(IndexActivator.class));
    for (CommandsToApply tx : transactions) {
        try (TransactionApplier txApplier = applier.startTx(tx, batchContext)) {
            tx.accept(txApplier);
        }
    }
}
Also used : CommandsToApply(org.neo4j.storageengine.api.CommandsToApply) LockGroup(org.neo4j.lock.LockGroup)

Example 5 with LockGroup

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

the class CommandHandlerContract method apply.

/**
 * In case the transactions do not have the commands to apply, use this method to apply any commands you want with a
 * given {@link ApplyFunction} instead.
 *
 * @param applier to use
 * @param function which knows what to do with the {@link TransactionApplier}.
 * @param transactions are only used to create {@link TransactionApplier}s. The actual work is delegated to the
 * function.
 * @return the boolean-and result of all function operations.
 */
public static boolean apply(TransactionApplierFactory applier, ApplyFunction function, CommandsToApply... transactions) throws Exception {
    BatchContext batchContext = mock(BatchContext.class);
    when(batchContext.getLockGroup()).thenReturn(new LockGroup());
    when(batchContext.getIdUpdateListener()).thenReturn(IdUpdateListener.DIRECT);
    when(batchContext.getIndexActivator()).thenReturn(new IndexActivator(mock(IndexUpdateListener.class)));
    return apply(applier, function, batchContext, transactions);
}
Also used : LockGroup(org.neo4j.lock.LockGroup)

Aggregations

LockGroup (org.neo4j.lock.LockGroup)6 Test (org.junit.jupiter.api.Test)2 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Config (org.neo4j.configuration.Config)1 DefaultIdGeneratorFactory (org.neo4j.internal.id.DefaultIdGeneratorFactory)1 NodeCommand (org.neo4j.internal.recordstorage.Command.NodeCommand)1 ConsistencyCheckingApplier (org.neo4j.internal.recordstorage.ConsistencyCheckingApplierFactory.ConsistencyCheckingApplier)1 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)1 DatabaseLayout (org.neo4j.io.layout.DatabaseLayout)1 RelationshipStore (org.neo4j.kernel.impl.store.RelationshipStore)1 StoreFactory (org.neo4j.kernel.impl.store.StoreFactory)1 SchemaRecord (org.neo4j.kernel.impl.store.record.SchemaRecord)1 Lock (org.neo4j.lock.Lock)1 CommandsToApply (org.neo4j.storageengine.api.CommandsToApply)1 IndexUpdateListener (org.neo4j.storageengine.api.IndexUpdateListener)1