Search in sources :

Example 6 with DoubleLatch

use of org.neo4j.test.DoubleLatch in project neo4j by neo4j.

the class IndexSamplingJobTrackerTest method shouldNotRunASampleJobWhichIsAlreadyRunning.

@Test
public void shouldNotRunASampleJobWhichIsAlreadyRunning() throws Throwable {
    // given
    when(config.jobLimit()).thenReturn(2);
    JobScheduler jobScheduler = new Neo4jJobScheduler();
    jobScheduler.init();
    IndexSamplingJobTracker jobTracker = new IndexSamplingJobTracker(config, jobScheduler);
    final DoubleLatch latch = new DoubleLatch();
    // when
    final AtomicInteger count = new AtomicInteger(0);
    assertTrue(jobTracker.canExecuteMoreSamplingJobs());
    IndexSamplingJob job = new IndexSamplingJob() {

        @Override
        public void run() {
            count.incrementAndGet();
            latch.waitForAllToStart();
            latch.finish();
        }

        @Override
        public long indexId() {
            return indexId12;
        }
    };
    jobTracker.scheduleSamplingJob(job);
    jobTracker.scheduleSamplingJob(job);
    latch.startAndWaitForAllToStart();
    latch.waitForAllToFinish();
    assertEquals(1, count.get());
}
Also used : JobScheduler(org.neo4j.kernel.impl.util.JobScheduler) Neo4jJobScheduler(org.neo4j.kernel.impl.util.Neo4jJobScheduler) Neo4jJobScheduler(org.neo4j.kernel.impl.util.Neo4jJobScheduler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DoubleLatch(org.neo4j.test.DoubleLatch) Test(org.junit.Test)

Example 7 with DoubleLatch

use of org.neo4j.test.DoubleLatch in project neo4j by neo4j.

the class IndexSamplingJobTrackerTest method shouldNotAcceptMoreJobsThanAllowed.

@Test
public void shouldNotAcceptMoreJobsThanAllowed() throws Throwable {
    // given
    when(config.jobLimit()).thenReturn(1);
    JobScheduler jobScheduler = new Neo4jJobScheduler();
    jobScheduler.init();
    final IndexSamplingJobTracker jobTracker = new IndexSamplingJobTracker(config, jobScheduler);
    final DoubleLatch latch = new DoubleLatch();
    final DoubleLatch waitingLatch = new DoubleLatch();
    // when
    assertTrue(jobTracker.canExecuteMoreSamplingJobs());
    jobTracker.scheduleSamplingJob(new IndexSamplingJob() {

        @Override
        public void run() {
            latch.startAndWaitForAllToStart();
            latch.waitForAllToFinish();
        }

        @Override
        public long indexId() {
            return indexId12;
        }
    });
    // then
    latch.waitForAllToStart();
    assertFalse(jobTracker.canExecuteMoreSamplingJobs());
    final AtomicBoolean waiting = new AtomicBoolean(false);
    new Thread(() -> {
        waiting.set(true);
        waitingLatch.startAndWaitForAllToStart();
        jobTracker.waitUntilCanExecuteMoreSamplingJobs();
        waiting.set(false);
        waitingLatch.finish();
    }).start();
    waitingLatch.waitForAllToStart();
    assertTrue(waiting.get());
    latch.finish();
    waitingLatch.waitForAllToFinish();
    assertFalse(waiting.get());
    // eventually we accept new jobs
    while (!jobTracker.canExecuteMoreSamplingJobs()) {
        Thread.yield();
    }
}
Also used : JobScheduler(org.neo4j.kernel.impl.util.JobScheduler) Neo4jJobScheduler(org.neo4j.kernel.impl.util.Neo4jJobScheduler) Neo4jJobScheduler(org.neo4j.kernel.impl.util.Neo4jJobScheduler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DoubleLatch(org.neo4j.test.DoubleLatch) Test(org.junit.Test)

Example 8 with DoubleLatch

use of org.neo4j.test.DoubleLatch in project neo4j by neo4j.

the class IndexingServiceTest method shouldWaitForRecoveredUniquenessConstraintIndexesToBeFullyPopulated.

@Test
public void shouldWaitForRecoveredUniquenessConstraintIndexesToBeFullyPopulated() throws Exception {
    // I.e. when a uniqueness constraint is created, but database crashes before that schema record
    // ends up in the store, so that next start have no choice but to rebuild it.
    // GIVEN
    final DoubleLatch latch = new DoubleLatch();
    ControlledIndexPopulator populator = new ControlledIndexPopulator(latch);
    final AtomicLong indexId = new AtomicLong(-1);
    IndexingService.Monitor monitor = new IndexingService.MonitorAdapter() {

        @Override
        public void awaitingPopulationOfRecoveredIndex(long index, NewIndexDescriptor descriptor) {
            // When we see that we start to await the index to populate, notify the slow-as-heck
            // populator that it can actually go and complete its job.
            indexId.set(index);
            latch.startAndWaitForAllToStart();
        }
    };
    // leaving out the IndexRule here will have the index being populated from scratch
    IndexingService indexing = newIndexingServiceWithMockedDependencies(populator, accessor, withData(addNodeUpdate(0, "value", 1)), monitor);
    // WHEN initializing, i.e. preparing for recovery
    life.init();
    // simulating an index being created as part of applying recovered transactions
    long fakeOwningConstraintRuleId = 1;
    indexing.createIndexes(constraintIndexRule(2, labelId, propertyKeyId, PROVIDER_DESCRIPTOR, fakeOwningConstraintRuleId));
    // and then starting, i.e. considering recovery completed
    life.start();
    // THEN afterwards the index should be ONLINE
    assertEquals(2, indexId.get());
    assertEquals(ONLINE, indexing.getIndexProxy(indexId.get()).getState());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) DoubleLatch(org.neo4j.test.DoubleLatch) Test(org.junit.Test)

Example 9 with DoubleLatch

use of org.neo4j.test.DoubleLatch in project neo4j by neo4j.

the class ControlledPopulationSchemaIndexProvider method installPopulationJobCompletionLatch.

public DoubleLatch installPopulationJobCompletionLatch() {
    final DoubleLatch populationCompletionLatch = new DoubleLatch();
    mockedPopulator = new IndexPopulator.Adapter() {

        @Override
        public void create() throws IOException {
            populationCompletionLatch.startAndWaitForAllToStartAndFinish();
            super.create();
        }

        @Override
        public IndexSample sampleResult() {
            return new IndexSample();
        }
    };
    return populationCompletionLatch;
}
Also used : IndexPopulator(org.neo4j.kernel.api.index.IndexPopulator) IndexSample(org.neo4j.storageengine.api.schema.IndexSample) DoubleLatch(org.neo4j.test.DoubleLatch) IOException(java.io.IOException)

Example 10 with DoubleLatch

use of org.neo4j.test.DoubleLatch in project neo4j by neo4j.

the class ContractCheckingIndexProxyTest method shouldNotCloseWhileForcing.

@Test(expected = /* THEN */
IllegalStateException.class)
public void shouldNotCloseWhileForcing() throws IOException {
    // GIVEN
    final DoubleLatch latch = new DoubleLatch();
    final IndexProxy inner = new IndexProxyAdapter() {

        @Override
        public void force() {
            latch.startAndWaitForAllToStartAndFinish();
        }
    };
    final IndexProxy outer = newContractCheckingIndexProxy(inner);
    outer.start();
    // WHEN
    runInSeparateThread(() -> outer.force());
    try {
        latch.waitForAllToStart();
        outer.close();
    } finally {
        latch.finish();
    }
}
Also used : SchemaIndexTestHelper.mockIndexProxy(org.neo4j.kernel.impl.api.index.SchemaIndexTestHelper.mockIndexProxy) DoubleLatch(org.neo4j.test.DoubleLatch) Test(org.junit.Test)

Aggregations

DoubleLatch (org.neo4j.test.DoubleLatch)48 Test (org.junit.Test)37 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)15 Collections.emptyMap (java.util.Collections.emptyMap)9 Map (java.util.Map)9 MapUtil.stringMap (org.neo4j.helpers.collection.MapUtil.stringMap)9 OffsetDateTime (java.time.OffsetDateTime)5 JobScheduler (org.neo4j.kernel.impl.util.JobScheduler)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Server (org.eclipse.jetty.server.Server)3 SchemaIndexTestHelper.mockIndexProxy (org.neo4j.kernel.impl.api.index.SchemaIndexTestHelper.mockIndexProxy)3 Neo4jJobScheduler (org.neo4j.kernel.impl.util.Neo4jJobScheduler)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.never (org.mockito.Mockito.never)2 Mockito.times (org.mockito.Mockito.times)2 Mockito.verify (org.mockito.Mockito.verify)2