Search in sources :

Example 1 with WorkSync

use of org.neo4j.util.concurrent.WorkSync 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 WorkSync

use of org.neo4j.util.concurrent.WorkSync in project neo4j by neo4j.

the class LargeFreelistCreationDeletionIT method shouldAlternateLargeCreationsAndDeletionsAndNotLoseIds.

@Test
void shouldAlternateLargeCreationsAndDeletionsAndNotLoseIds() throws Throwable {
    long[][] allocatedIds = new long[THREADS][];
    for (int i = 0; i < THREADS; i++) {
        allocatedIds[i] = new long[ALLOCATIONS_PER_THREAD];
    }
    for (int r = 0; r < 3; r++) {
        // Create
        try (var freelist = new IndexedIdGenerator(pageCache, directory.file("file.id"), immediate(), IdType.NODE, false, () -> 0, Long.MAX_VALUE, writable(), Config.defaults(), DEFAULT_DATABASE_NAME, NULL)) {
            // Make sure ID cache is filled so that initial allocations won't slide highId unnecessarily.
            freelist.maintenance(NULL);
            Race race = new Race();
            WorkSync<IndexedIdGenerator, Ids> workSync = new WorkSync<>(freelist);
            for (int t = 0; t < THREADS; t++) {
                int thread = t;
                race.addContestant(throwing(() -> {
                    int cursor = 0;
                    for (int i = 0; i < TRANSACTIONS_PER_THREAD; i++) {
                        long[] txIds = new long[ALLOCATIONS_PER_TRANSACTION];
                        for (int a = 0; a < txIds.length; a++) {
                            long id = freelist.nextId(NULL);
                            allocatedIds[thread][cursor++] = id;
                            txIds[a] = id;
                        }
                        workSync.apply(new Ids(txIds));
                        Thread.sleep(1);
                    }
                }), 1);
            }
            race.go();
            assertAllUnique(allocatedIds);
            // Delete
            try (Marker marker = freelist.marker(NULL)) {
                for (long[] perThread : allocatedIds) {
                    for (long id : perThread) {
                        marker.markDeleted(id);
                    }
                }
            }
            // Checkpoint
            freelist.checkpoint(NULL);
            System.out.println(freelist.getHighId());
        }
    }
}
Also used : WorkSync(org.neo4j.util.concurrent.WorkSync) Race(org.neo4j.test.Race) Marker(org.neo4j.internal.id.IdGenerator.Marker) Test(org.junit.jupiter.api.Test)

Example 3 with WorkSync

use of org.neo4j.util.concurrent.WorkSync in project neo4j by neo4j.

the class SchemaRuleCommandTest method setup.

@BeforeEach
void setup() {
    Map<IdType, WorkSync<IdGenerator, IdGeneratorUpdateWork>> idGeneratorWorkSyncs = new EnumMap<>(IdType.class);
    for (IdType idType : IdType.values()) {
        idGeneratorWorkSyncs.put(idType, new WorkSync<>(mock(IdGenerator.class)));
    }
    storeApplier = new NeoStoreTransactionApplierFactory(INTERNAL, neoStores, mock(CacheAccessBackDoor.class), LockService.NO_LOCK_SERVICE);
}
Also used : WorkSync(org.neo4j.util.concurrent.WorkSync) EnumMap(java.util.EnumMap) IdType(org.neo4j.internal.id.IdType) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

WorkSync (org.neo4j.util.concurrent.WorkSync)3 EnumMap (java.util.EnumMap)2 IdType (org.neo4j.internal.id.IdType)2 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1 Marker (org.neo4j.internal.id.IdGenerator.Marker)1 LockService (org.neo4j.lock.LockService)1 LockType (org.neo4j.lock.LockType)1 CommandsToApply (org.neo4j.storageengine.api.CommandsToApply)1 Race (org.neo4j.test.Race)1