Search in sources :

Example 1 with BACKGROUND_REBUILD_UPDATED

use of org.neo4j.kernel.impl.api.index.sampling.IndexSamplingMode.BACKGROUND_REBUILD_UPDATED in project neo4j by neo4j.

the class IndexSamplingControllerTest method shouldNotEmptyQueueConcurrently.

@Test
public void shouldNotEmptyQueueConcurrently() {
    // given
    final AtomicInteger totalCount = new AtomicInteger(0);
    final AtomicInteger concurrentCount = new AtomicInteger(0);
    final DoubleLatch jobLatch = new DoubleLatch();
    final DoubleLatch testLatch = new DoubleLatch();
    final ThreadLocal<Boolean> hasRun = new ThreadLocal<Boolean>() {

        @Override
        protected Boolean initialValue() {
            return false;
        }
    };
    IndexSamplingJobFactory jobFactory = (indexId, proxy) -> {
        // make sure we execute this once per thread
        if (hasRun.get()) {
            return null;
        }
        hasRun.set(true);
        if (!concurrentCount.compareAndSet(0, 1)) {
            throw new IllegalStateException("count !== 0 on create");
        }
        totalCount.incrementAndGet();
        jobLatch.waitForAllToStart();
        testLatch.startAndWaitForAllToStart();
        jobLatch.waitForAllToFinish();
        concurrentCount.decrementAndGet();
        testLatch.finish();
        return null;
    };
    final IndexSamplingController controller = new IndexSamplingController(samplingConfig, jobFactory, jobQueue, tracker, snapshotProvider, scheduler, always(false));
    when(tracker.canExecuteMoreSamplingJobs()).thenReturn(true);
    when(indexProxy.getState()).thenReturn(ONLINE);
    // when running once
    new Thread(runController(controller, BACKGROUND_REBUILD_UPDATED)).start();
    jobLatch.startAndWaitForAllToStart();
    testLatch.waitForAllToStart();
    // then blocking on first job
    assertEquals(1, concurrentCount.get());
    assertEquals(1, totalCount.get());
    // when running a second time
    controller.sampleIndexes(BACKGROUND_REBUILD_UPDATED);
    // then no concurrent job execution
    jobLatch.finish();
    testLatch.waitForAllToFinish();
    // and finally exactly one job has run to completion
    assertEquals(0, concurrentCount.get());
    assertEquals(1, totalCount.get());
}
Also used : IndexMap(org.neo4j.kernel.impl.api.index.IndexMap) IndexProxy(org.neo4j.kernel.impl.api.index.IndexProxy) Predicates(org.neo4j.function.Predicates) NewIndexDescriptorFactory(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptorFactory) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) DoubleLatch(org.neo4j.test.DoubleLatch) Mockito.when(org.mockito.Mockito.when) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) JobScheduler(org.neo4j.kernel.impl.util.JobScheduler) ONLINE(org.neo4j.kernel.api.index.InternalIndexState.ONLINE) TRIGGER_REBUILD_UPDATED(org.neo4j.kernel.impl.api.index.sampling.IndexSamplingMode.TRIGGER_REBUILD_UPDATED) IndexMapSnapshotProvider(org.neo4j.kernel.impl.api.index.IndexMapSnapshotProvider) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) FAILED(org.neo4j.kernel.api.index.InternalIndexState.FAILED) BACKGROUND_REBUILD_UPDATED(org.neo4j.kernel.impl.api.index.sampling.IndexSamplingMode.BACKGROUND_REBUILD_UPDATED) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) POPULATING(org.neo4j.kernel.api.index.InternalIndexState.POPULATING) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DoubleLatch(org.neo4j.test.DoubleLatch) Test(org.junit.Test)

Example 2 with BACKGROUND_REBUILD_UPDATED

use of org.neo4j.kernel.impl.api.index.sampling.IndexSamplingMode.BACKGROUND_REBUILD_UPDATED in project neo4j by neo4j.

the class IndexSamplingControllerTest method shouldNotStartOtherSamplingWhenSamplingAllTheIndexes.

@Test
public void shouldNotStartOtherSamplingWhenSamplingAllTheIndexes() {
    // given
    final AtomicInteger totalCount = new AtomicInteger(0);
    final AtomicInteger concurrentCount = new AtomicInteger(0);
    final DoubleLatch jobLatch = new DoubleLatch();
    final DoubleLatch testLatch = new DoubleLatch();
    IndexSamplingJobFactory jobFactory = (indexId, proxy) -> {
        if (!concurrentCount.compareAndSet(0, 1)) {
            throw new IllegalStateException("count !== 0 on create");
        }
        totalCount.incrementAndGet();
        jobLatch.waitForAllToStart();
        testLatch.startAndWaitForAllToStart();
        jobLatch.waitForAllToFinish();
        concurrentCount.decrementAndGet();
        testLatch.finish();
        return null;
    };
    final IndexSamplingController controller = new IndexSamplingController(samplingConfig, jobFactory, jobQueue, tracker, snapshotProvider, scheduler, always(true));
    when(tracker.canExecuteMoreSamplingJobs()).thenReturn(true);
    when(indexProxy.getState()).thenReturn(ONLINE);
    // when running once
    new Thread(runController(controller, TRIGGER_REBUILD_UPDATED)).start();
    jobLatch.startAndWaitForAllToStart();
    testLatch.waitForAllToStart();
    // then blocking on first job
    assertEquals(1, concurrentCount.get());
    // when running a second time
    controller.sampleIndexes(BACKGROUND_REBUILD_UPDATED);
    // then no concurrent job execution
    jobLatch.finish();
    testLatch.waitForAllToFinish();
    // and finally exactly one job has run to completion
    assertEquals(0, concurrentCount.get());
    assertEquals(1, totalCount.get());
}
Also used : IndexMap(org.neo4j.kernel.impl.api.index.IndexMap) IndexProxy(org.neo4j.kernel.impl.api.index.IndexProxy) Predicates(org.neo4j.function.Predicates) NewIndexDescriptorFactory(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptorFactory) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) DoubleLatch(org.neo4j.test.DoubleLatch) Mockito.when(org.mockito.Mockito.when) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) JobScheduler(org.neo4j.kernel.impl.util.JobScheduler) ONLINE(org.neo4j.kernel.api.index.InternalIndexState.ONLINE) TRIGGER_REBUILD_UPDATED(org.neo4j.kernel.impl.api.index.sampling.IndexSamplingMode.TRIGGER_REBUILD_UPDATED) IndexMapSnapshotProvider(org.neo4j.kernel.impl.api.index.IndexMapSnapshotProvider) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) FAILED(org.neo4j.kernel.api.index.InternalIndexState.FAILED) BACKGROUND_REBUILD_UPDATED(org.neo4j.kernel.impl.api.index.sampling.IndexSamplingMode.BACKGROUND_REBUILD_UPDATED) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) POPULATING(org.neo4j.kernel.api.index.InternalIndexState.POPULATING) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DoubleLatch(org.neo4j.test.DoubleLatch) Test(org.junit.Test)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Test (org.junit.Test)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 Mockito.verifyNoMoreInteractions (org.mockito.Mockito.verifyNoMoreInteractions)2 Mockito.when (org.mockito.Mockito.when)2 Predicates (org.neo4j.function.Predicates)2 FAILED (org.neo4j.kernel.api.index.InternalIndexState.FAILED)2 ONLINE (org.neo4j.kernel.api.index.InternalIndexState.ONLINE)2 POPULATING (org.neo4j.kernel.api.index.InternalIndexState.POPULATING)2 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)2 NewIndexDescriptorFactory (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptorFactory)2 IndexMap (org.neo4j.kernel.impl.api.index.IndexMap)2 IndexMapSnapshotProvider (org.neo4j.kernel.impl.api.index.IndexMapSnapshotProvider)2 IndexProxy (org.neo4j.kernel.impl.api.index.IndexProxy)2 BACKGROUND_REBUILD_UPDATED (org.neo4j.kernel.impl.api.index.sampling.IndexSamplingMode.BACKGROUND_REBUILD_UPDATED)2 TRIGGER_REBUILD_UPDATED (org.neo4j.kernel.impl.api.index.sampling.IndexSamplingMode.TRIGGER_REBUILD_UPDATED)2