Search in sources :

Example 1 with JobHandle

use of org.neo4j.scheduler.JobHandle in project neo4j by neo4j.

the class ScheduledJobHandle method waitTermination.

@Override
public void waitTermination() throws ExecutionException, InterruptedException {
    handleRelease.await();
    RuntimeException runtimeException = null;
    try {
        JobHandle handleDelegate = this.latestHandle;
        if (handleDelegate != null) {
            handleDelegate.waitTermination();
        }
    } catch (RuntimeException t) {
        runtimeException = t;
    }
    if (state.get() == FAILED) {
        Throwable exception = this.lastException;
        if (exception != null) {
            var executionException = new ExecutionException(exception);
            if (runtimeException != null) {
                executionException.addSuppressed(runtimeException);
            }
            throw executionException;
        } else {
            throw Exceptions.chain(new CancellationException(), runtimeException);
        }
    }
}
Also used : JobHandle(org.neo4j.scheduler.JobHandle) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with JobHandle

use of org.neo4j.scheduler.JobHandle in project neo4j by neo4j.

the class CentralJobSchedulerTest method longRunningScheduledJobsMustNotDelayOtherLongRunningJobs.

@Test
void longRunningScheduledJobsMustNotDelayOtherLongRunningJobs() {
    life.start();
    List<JobHandle<?>> handles = new ArrayList<>(30);
    AtomicLong startedCounter = new AtomicLong();
    BinaryLatch blockLatch = new BinaryLatch();
    Runnable task = () -> {
        startedCounter.incrementAndGet();
        blockLatch.await();
    };
    for (int i = 0; i < 10; i++) {
        handles.add(scheduler.schedule(Group.INDEX_POPULATION, NOT_MONITORED, task, 0, TimeUnit.MILLISECONDS));
    }
    for (int i = 0; i < 10; i++) {
        handles.add(scheduler.scheduleRecurring(Group.INDEX_POPULATION, NOT_MONITORED, task, Integer.MAX_VALUE, TimeUnit.MILLISECONDS));
    }
    for (int i = 0; i < 10; i++) {
        handles.add(scheduler.scheduleRecurring(Group.INDEX_POPULATION, NOT_MONITORED, task, 0, Integer.MAX_VALUE, TimeUnit.MILLISECONDS));
    }
    long deadline = TimeUnit.SECONDS.toNanos(10) + System.nanoTime();
    do {
        if (startedCounter.get() == handles.size()) {
            // All jobs got started. We're good!
            blockLatch.release();
            for (JobHandle<?> handle : handles) {
                handle.cancel();
            }
            return;
        }
    } while (System.nanoTime() < deadline);
    fail("Only managed to start " + startedCounter.get() + " tasks in 10 seconds, when " + handles.size() + " was expected.");
}
Also used : JobHandle(org.neo4j.scheduler.JobHandle) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) BinaryLatch(org.neo4j.util.concurrent.BinaryLatch) Test(org.junit.jupiter.api.Test)

Example 3 with JobHandle

use of org.neo4j.scheduler.JobHandle in project neo4j by neo4j.

the class GenericBlockBasedIndexPopulatorTest method setup.

@BeforeEach
void setup() {
    IndexProviderDescriptor providerDescriptor = new IndexProviderDescriptor("test", "v1");
    IndexDirectoryStructure directoryStructure = directoriesByProvider(directory.homePath()).forProvider(providerDescriptor);
    indexFiles = new IndexFiles.Directory(fs, directoryStructure, INDEX_DESCRIPTOR.getId());
    databaseIndexContext = DatabaseIndexContext.builder(pageCache, fs, DEFAULT_DATABASE_NAME).build();
    jobScheduler = JobSchedulerFactory.createInitialisedScheduler();
    populationWorkScheduler = new IndexPopulator.PopulationWorkScheduler() {

        @Override
        public <T> JobHandle<T> schedule(IndexPopulator.JobDescriptionSupplier descriptionSupplier, Callable<T> job) {
            return jobScheduler.schedule(Group.INDEX_POPULATION_WORK, new JobMonitoringParams(null, null, null), job);
        }
    };
}
Also used : IndexPopulator(org.neo4j.kernel.api.index.IndexPopulator) JobHandle(org.neo4j.scheduler.JobHandle) IndexDirectoryStructure(org.neo4j.kernel.api.index.IndexDirectoryStructure) NULL_CONTEXT(org.neo4j.internal.kernel.api.QueryContext.NULL_CONTEXT) IndexProviderDescriptor(org.neo4j.internal.schema.IndexProviderDescriptor) JobMonitoringParams(org.neo4j.scheduler.JobMonitoringParams) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with JobHandle

use of org.neo4j.scheduler.JobHandle in project neo4j by neo4j.

the class PartMergerTest method setUp.

@BeforeEach
void setUp() {
    scheduler = new ThreadPoolJobScheduler();
    populationWorkScheduler = new PopulationWorkScheduler() {

        @Override
        public <T> JobHandle<T> schedule(IndexPopulator.JobDescriptionSupplier descriptionSupplier, Callable<T> job) {
            return scheduler.schedule(Group.INDEX_POPULATION_WORK, NOT_MONITORED, job);
        }
    };
}
Also used : IndexPopulator(org.neo4j.kernel.api.index.IndexPopulator) JobHandle(org.neo4j.scheduler.JobHandle) PopulationWorkScheduler(org.neo4j.kernel.api.index.IndexPopulator.PopulationWorkScheduler) ThreadPoolJobScheduler(org.neo4j.test.scheduler.ThreadPoolJobScheduler) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with JobHandle

use of org.neo4j.scheduler.JobHandle in project neo4j by neo4j.

the class TimeBasedTaskSchedulerTest method delayedTasksMustNotRunIfCancelledFirst.

@Test
void delayedTasksMustNotRunIfCancelledFirst() {
    MonitoredCancelListener cancelListener = new MonitoredCancelListener();
    JobHandle handle = scheduler.submit(Group.STORAGE_MAINTENANCE, counter::incrementAndGet, 100, 0);
    handle.registerCancelListener(cancelListener);
    clock.forward(90, TimeUnit.NANOSECONDS);
    scheduler.tick();
    handle.cancel();
    clock.forward(10, TimeUnit.NANOSECONDS);
    scheduler.tick();
    pools.getThreadPool(Group.STORAGE_MAINTENANCE).shutDown();
    assertThat(counter.get()).isEqualTo(0);
    assertTrue(cancelListener.isCanceled());
    assertThrows(CancellationException.class, handle::waitTermination);
}
Also used : JobHandle(org.neo4j.scheduler.JobHandle) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Aggregations

JobHandle (org.neo4j.scheduler.JobHandle)20 Test (org.junit.jupiter.api.Test)14 RepeatedTest (org.junit.jupiter.api.RepeatedTest)9 TimeUnit (java.util.concurrent.TimeUnit)3 JobMonitoringParams (org.neo4j.scheduler.JobMonitoringParams)3 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 IndexPopulator (org.neo4j.kernel.api.index.IndexPopulator)2 CancellationException (java.util.concurrent.CancellationException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Dependencies (org.neo4j.collection.Dependencies)1 NULL_CONTEXT (org.neo4j.internal.kernel.api.QueryContext.NULL_CONTEXT)1 IndexProviderDescriptor (org.neo4j.internal.schema.IndexProviderDescriptor)1 IndexDirectoryStructure (org.neo4j.kernel.api.index.IndexDirectoryStructure)1 PopulationWorkScheduler (org.neo4j.kernel.api.index.IndexPopulator.PopulationWorkScheduler)1 TransactionMonitorScheduler (org.neo4j.kernel.impl.api.transaction.monitor.TransactionMonitorScheduler)1 CentralJobScheduler (org.neo4j.kernel.impl.scheduler.CentralJobScheduler)1