Search in sources :

Example 66 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project neo4j by neo4j.

the class EncodeGroupsStepTest method shouldEncodeGroupChains.

@SuppressWarnings("unchecked")
@Test
public void shouldEncodeGroupChains() throws Throwable {
    // GIVEN
    StageControl control = mock(StageControl.class);
    final AtomicLong nextId = new AtomicLong();
    RecordStore<RelationshipGroupRecord> store = mock(RecordStore.class);
    when(store.nextId()).thenAnswer(invocation -> nextId.incrementAndGet());
    doAnswer(invocation -> {
        invocation.getArgumentAt(0, RelationshipGroupRecord.class).setFirstOut(1);
        return null;
    }).when(store).prepareForCommit(any(RelationshipGroupRecord.class));
    Configuration config = Configuration.withBatchSize(DEFAULT, 10);
    EncodeGroupsStep encoder = new EncodeGroupsStep(control, config, store);
    // WHEN
    encoder.start(Step.ORDER_SEND_DOWNSTREAM);
    Catcher catcher = new Catcher();
    encoder.process(batch(new Group(1, 3), new Group(2, 3), new Group(3, 4)), catcher);
    encoder.process(batch(new Group(4, 2), new Group(5, 10)), catcher);
    encoder.process(batch(new Group(6, 35)), catcher);
    encoder.process(batch(new Group(7, 2)), catcher);
    encoder.endOfUpstream();
    while (!encoder.isCompleted()) {
        Thread.sleep(10);
    }
    encoder.close();
    // THEN
    assertEquals(4, catcher.batches.size());
    long lastOwningNodeLastBatch = -1;
    for (RelationshipGroupRecord[] batch : catcher.batches) {
        assertBatch(batch, lastOwningNodeLastBatch);
        lastOwningNodeLastBatch = batch[batch.length - 1].getOwningNode();
    }
}
Also used : Group(org.neo4j.unsafe.impl.batchimport.ReadGroupsFromCacheStepTest.Group) AtomicLong(java.util.concurrent.atomic.AtomicLong) StageControl(org.neo4j.unsafe.impl.batchimport.staging.StageControl) RelationshipGroupRecord(org.neo4j.kernel.impl.store.record.RelationshipGroupRecord) Test(org.junit.Test)

Example 67 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project neo4j by neo4j.

the class CsvInputBatchImportIT method buildUpExpectedData.

private void buildUpExpectedData(List<InputNode> nodeData, List<InputRelationship> relationshipData, Map<String, InputNode> expectedNodes, Map<String, String[]> expectedNodeNames, Map<String, Map<String, Map<String, AtomicInteger>>> expectedRelationships, Map<String, AtomicLong> nodeCounts, Map<String, Map<String, Map<String, AtomicLong>>> relationshipCounts) {
    for (InputNode node : nodeData) {
        expectedNodes.put((String) node.id(), node);
        expectedNodeNames.put(nameOf(node), node.labels());
        countNodeLabels(nodeCounts, node.labels());
    }
    for (InputRelationship relationship : relationshipData) {
        // Expected relationship counts per node, type and direction
        InputNode startNode = expectedNodes.get(relationship.startNode());
        InputNode endNode = expectedNodes.get(relationship.endNode());
        {
            expectedRelationships.get(nameOf(startNode)).get(nameOf(endNode)).get(relationship.type()).incrementAndGet();
        }
        // Expected counts per start/end node label ids
        // Let's do what CountsState#addRelationship does, roughly
        relationshipCounts.get(null).get(null).get(null).incrementAndGet();
        relationshipCounts.get(null).get(relationship.type()).get(null).incrementAndGet();
        for (String startNodeLabelName : asSet(startNode.labels())) {
            Map<String, Map<String, AtomicLong>> startLabelCounts = relationshipCounts.get(startNodeLabelName);
            startLabelCounts.get(null).get(null).incrementAndGet();
            Map<String, AtomicLong> typeCounts = startLabelCounts.get(relationship.type());
            typeCounts.get(null).incrementAndGet();
            if (COMPUTE_DOUBLE_SIDED_RELATIONSHIP_COUNTS) {
                for (String endNodeLabelName : asSet(endNode.labels())) {
                    startLabelCounts.get(null).get(endNodeLabelName).incrementAndGet();
                    typeCounts.get(endNodeLabelName).incrementAndGet();
                }
            }
        }
        for (String endNodeLabelName : asSet(endNode.labels())) {
            relationshipCounts.get(null).get(null).get(endNodeLabelName).incrementAndGet();
            relationshipCounts.get(null).get(relationship.type()).get(endNodeLabelName).incrementAndGet();
        }
    }
}
Also used : InputNode(org.neo4j.unsafe.impl.batchimport.input.InputNode) AtomicLong(java.util.concurrent.atomic.AtomicLong) InputRelationship(org.neo4j.unsafe.impl.batchimport.input.InputRelationship) Map(java.util.Map) HashMap(java.util.HashMap) AutoCreatingHashMap(org.neo4j.kernel.impl.util.AutoCreatingHashMap)

Example 68 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project neo4j by neo4j.

the class DelayedRenewableTimeoutServiceTest method shouldTimeOutAfterTimeoutPeriod.

@Test
public void shouldTimeOutAfterTimeoutPeriod() throws Throwable {
    // given
    final AtomicLong timeoutCount = new AtomicLong();
    DelayedRenewableTimeoutService timeoutService = new DelayedRenewableTimeoutService(Clocks.systemClock(), getInstance());
    long startTime = System.currentTimeMillis();
    timeoutService.create(Timeouts.FOOBAR, TIMEOUT_MS, 0, timeout -> timeoutCount.incrementAndGet());
    life.add(timeoutService);
    Predicates.await(timeoutCount::get, count -> count == 1, LONG_TIME_MS, MILLISECONDS, 1, MILLISECONDS);
    long runTime = System.currentTimeMillis() - startTime;
    assertThat(runTime, greaterThanOrEqualTo(TIMEOUT_MS - ERROR_MS));
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 69 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project neo4j by neo4j.

the class CrashGenerationCleaner method clean.

// === Methods about the execution and threading ===
public void clean() throws IOException {
    assert unstableGeneration > stableGeneration;
    assert unstableGeneration - stableGeneration > 1;
    long startTime = currentTimeMillis();
    int threads = availableProcessors;
    ExecutorService executor = Executors.newFixedThreadPool(threads);
    AtomicLong nextId = new AtomicLong(lowTreeNodeId);
    AtomicReference<Throwable> error = new AtomicReference<>();
    AtomicInteger cleanedPointers = new AtomicInteger();
    for (int i = 0; i < threads; i++) {
        executor.submit(cleaner(nextId, error, cleanedPointers));
    }
    executor.shutdown();
    try {
        long lastProgression = nextId.get();
        // I/O congestion spikes w/o failing in vain.
        while (!executor.awaitTermination(30, SECONDS)) {
            if (lastProgression == nextId.get()) {
                // No progression at all, abort?
                error.compareAndSet(null, new IOException("No progress, so forcing abort"));
            }
            lastProgression = nextId.get();
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    Throwable finalError = error.get();
    if (finalError != null) {
        throw launderedException(IOException.class, finalError);
    }
    long endTime = currentTimeMillis();
    monitor.recoveryCompleted(highTreeNodeId - lowTreeNodeId, cleanedPointers.get(), endTime - startTime);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException)

Example 70 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project neo4j by neo4j.

the class TransactionRecordStateTest method shouldDeleteDynamicLabelsForDeletedNode.

@Test
public void shouldDeleteDynamicLabelsForDeletedNode() throws Throwable {
    // GIVEN a store that has got a node with a dynamic label record
    NeoStores store = neoStoresRule.open();
    BatchTransactionApplier applier = new NeoStoreBatchTransactionApplier(store, mock(CacheAccessBackDoor.class), LockService.NO_LOCK_SERVICE);
    AtomicLong nodeId = new AtomicLong();
    AtomicLong dynamicLabelRecordId = new AtomicLong();
    apply(applier, transaction(nodeWithDynamicLabelRecord(store, nodeId, dynamicLabelRecordId)));
    assertDynamicLabelRecordInUse(store, dynamicLabelRecordId.get(), true);
    // WHEN applying a transaction where the node is deleted
    apply(applier, transaction(deleteNode(store, nodeId.get())));
    // THEN the dynamic label record should also be deleted
    assertDynamicLabelRecordInUse(store, dynamicLabelRecordId.get(), false);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) 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)

Aggregations

AtomicLong (java.util.concurrent.atomic.AtomicLong)2292 Test (org.junit.Test)986 ArrayList (java.util.ArrayList)300 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)273 IOException (java.io.IOException)254 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)250 List (java.util.List)222 HashMap (java.util.HashMap)212 Map (java.util.Map)209 CountDownLatch (java.util.concurrent.CountDownLatch)185 AtomicReference (java.util.concurrent.atomic.AtomicReference)174 HashSet (java.util.HashSet)106 Arrays (java.util.Arrays)101 File (java.io.File)99 Test (org.junit.jupiter.api.Test)98 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)95 Set (java.util.Set)94 TimeUnit (java.util.concurrent.TimeUnit)91 Collections (java.util.Collections)88 Random (java.util.Random)85