Search in sources :

Example 6 with JobScheduler

use of org.neo4j.kernel.impl.util.JobScheduler in project neo4j by neo4j.

the class IndexSamplingJobTrackerTest method shouldDoNothingWhenUsedAfterBeingStopped.

@Test(timeout = 5_000)
public void shouldDoNothingWhenUsedAfterBeingStopped() {
    // Given
    JobScheduler scheduler = mock(JobScheduler.class);
    IndexSamplingJobTracker jobTracker = new IndexSamplingJobTracker(config, scheduler);
    jobTracker.stopAndAwaitAllJobs();
    // When
    jobTracker.scheduleSamplingJob(mock(IndexSamplingJob.class));
    // Then
    verifyZeroInteractions(scheduler);
}
Also used : JobScheduler(org.neo4j.kernel.impl.util.JobScheduler) Neo4jJobScheduler(org.neo4j.kernel.impl.util.Neo4jJobScheduler) Test(org.junit.Test)

Example 7 with JobScheduler

use of org.neo4j.kernel.impl.util.JobScheduler in project neo4j by neo4j.

the class IndexSamplingJobTrackerTest method shouldWaitForAllJobsToFinish.

@Test(timeout = 5_000)
public void shouldWaitForAllJobsToFinish() throws Throwable {
    // Given
    when(config.jobLimit()).thenReturn(2);
    JobScheduler jobScheduler = new Neo4jJobScheduler();
    jobScheduler.init();
    final IndexSamplingJobTracker jobTracker = new IndexSamplingJobTracker(config, jobScheduler);
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    WaitingIndexSamplingJob job1 = new WaitingIndexSamplingJob(indexId11, latch1);
    WaitingIndexSamplingJob job2 = new WaitingIndexSamplingJob(indexId22, latch1);
    jobTracker.scheduleSamplingJob(job1);
    jobTracker.scheduleSamplingJob(job2);
    Future<?> stopping = Executors.newSingleThreadExecutor().submit(() -> {
        latch2.countDown();
        try {
            jobTracker.awaitAllJobs(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    });
    // When
    latch2.await();
    assertFalse(stopping.isDone());
    latch1.countDown();
    stopping.get(10, SECONDS);
    // Then
    assertTrue(stopping.isDone());
    assertNull(stopping.get());
    assertTrue(job1.executed);
    assertTrue(job2.executed);
}
Also used : JobScheduler(org.neo4j.kernel.impl.util.JobScheduler) Neo4jJobScheduler(org.neo4j.kernel.impl.util.Neo4jJobScheduler) Neo4jJobScheduler(org.neo4j.kernel.impl.util.Neo4jJobScheduler) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 8 with JobScheduler

use of org.neo4j.kernel.impl.util.JobScheduler in project neo4j by neo4j.

the class IndexSamplingJobTrackerTest method shouldStopAndWaitForAllJobsToFinish.

@Test(timeout = 5_000)
public void shouldStopAndWaitForAllJobsToFinish() throws Throwable {
    // Given
    when(config.jobLimit()).thenReturn(2);
    JobScheduler jobScheduler = new Neo4jJobScheduler();
    jobScheduler.init();
    final IndexSamplingJobTracker jobTracker = new IndexSamplingJobTracker(config, jobScheduler);
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    WaitingIndexSamplingJob job1 = new WaitingIndexSamplingJob(indexId11, latch1);
    WaitingIndexSamplingJob job2 = new WaitingIndexSamplingJob(indexId22, latch1);
    jobTracker.scheduleSamplingJob(job1);
    jobTracker.scheduleSamplingJob(job2);
    Future<?> stopping = Executors.newSingleThreadExecutor().submit(() -> {
        latch2.countDown();
        jobTracker.stopAndAwaitAllJobs();
    });
    // When
    latch2.await();
    assertFalse(stopping.isDone());
    latch1.countDown();
    stopping.get(10, SECONDS);
    // Then
    assertTrue(stopping.isDone());
    assertNull(stopping.get());
    assertTrue(job1.executed);
    assertTrue(job2.executed);
}
Also used : JobScheduler(org.neo4j.kernel.impl.util.JobScheduler) Neo4jJobScheduler(org.neo4j.kernel.impl.util.Neo4jJobScheduler) Neo4jJobScheduler(org.neo4j.kernel.impl.util.Neo4jJobScheduler) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 9 with JobScheduler

use of org.neo4j.kernel.impl.util.JobScheduler 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 10 with JobScheduler

use of org.neo4j.kernel.impl.util.JobScheduler in project neo4j by neo4j.

the class QueryLoggerKernelExtension method newInstance.

@Override
public Lifecycle newInstance(@SuppressWarnings("unused") KernelContext context, final Dependencies dependencies) throws Throwable {
    final Config config = dependencies.config();
    boolean queryLogEnabled = config.get(GraphDatabaseSettings.log_queries);
    final File queryLogFile = config.get(GraphDatabaseSettings.log_queries_filename);
    final FileSystemAbstraction fileSystem = dependencies.fileSystem();
    final JobScheduler jobScheduler = dependencies.jobScheduler();
    final Monitors monitoring = dependencies.monitoring();
    if (!queryLogEnabled) {
        return createEmptyAdapter();
    }
    return new LifecycleAdapter() {

        Closeable closable;

        @Override
        public void init() throws Throwable {
            Long thresholdMillis = config.get(GraphDatabaseSettings.log_queries_threshold);
            Long rotationThreshold = config.get(GraphDatabaseSettings.log_queries_rotation_threshold);
            int maxArchives = config.get(GraphDatabaseSettings.log_queries_max_archives);
            boolean logQueryParameters = config.get(GraphDatabaseSettings.log_queries_parameter_logging_enabled);
            FormattedLog.Builder logBuilder = FormattedLog.withUTCTimeZone();
            Log log;
            if (rotationThreshold == 0) {
                OutputStream logOutputStream = createOrOpenAsOuputStream(fileSystem, queryLogFile, true);
                log = logBuilder.toOutputStream(logOutputStream);
                closable = logOutputStream;
            } else {
                RotatingFileOutputStreamSupplier rotatingSupplier = new RotatingFileOutputStreamSupplier(fileSystem, queryLogFile, rotationThreshold, 0, maxArchives, jobScheduler.executor(JobScheduler.Groups.queryLogRotation));
                log = logBuilder.toOutputStream(rotatingSupplier);
                closable = rotatingSupplier;
            }
            QueryLogger logger = new QueryLogger(Clocks.systemClock(), log, thresholdMillis, logQueryParameters);
            monitoring.addMonitorListener(logger);
        }

        @Override
        public void shutdown() throws Throwable {
            closable.close();
        }
    };
}
Also used : JobScheduler(org.neo4j.kernel.impl.util.JobScheduler) FormattedLog(org.neo4j.logging.FormattedLog) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) Log(org.neo4j.logging.Log) FormattedLog(org.neo4j.logging.FormattedLog) Config(org.neo4j.kernel.configuration.Config) Closeable(java.io.Closeable) OutputStream(java.io.OutputStream) LifecycleAdapter(org.neo4j.kernel.lifecycle.LifecycleAdapter) RotatingFileOutputStreamSupplier(org.neo4j.logging.RotatingFileOutputStreamSupplier) Monitors(org.neo4j.kernel.monitoring.Monitors) File(java.io.File)

Aggregations

JobScheduler (org.neo4j.kernel.impl.util.JobScheduler)18 Test (org.junit.Test)9 Config (org.neo4j.kernel.configuration.Config)7 Neo4jJobScheduler (org.neo4j.kernel.impl.util.Neo4jJobScheduler)7 LifeSupport (org.neo4j.kernel.lifecycle.LifeSupport)6 File (java.io.File)5 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)4 Monitors (org.neo4j.kernel.monitoring.Monitors)4 LabelScanStoreProvider (org.neo4j.kernel.impl.api.scan.LabelScanStoreProvider)3 LogService (org.neo4j.kernel.impl.logging.LogService)3 Procedures (org.neo4j.kernel.impl.proc.Procedures)3 Log (org.neo4j.logging.Log)3 LogProvider (org.neo4j.logging.LogProvider)3 DoubleLatch (org.neo4j.test.DoubleLatch)3 IOException (java.io.IOException)2 URI (java.net.URI)2 Clock (java.time.Clock)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CountDownLatch (java.util.concurrent.CountDownLatch)2