Search in sources :

Example 16 with UriExtractionNamespace

use of org.apache.druid.query.lookup.namespace.UriExtractionNamespace in project druid by druid-io.

the class CacheSchedulerTest method setUp.

@Before
public void setUp() throws Exception {
    lifecycle = new Lifecycle();
    lifecycle.start();
    cacheManager = createCacheManager.apply(lifecycle);
    final Path tmpDir = temporaryFolder.newFolder().toPath();
    final CacheGenerator<UriExtractionNamespace> cacheGenerator = new CacheGenerator<UriExtractionNamespace>() {

        @Override
        public CacheScheduler.VersionedCache generateCache(final UriExtractionNamespace extractionNamespace, final CacheScheduler.EntryImpl<UriExtractionNamespace> id, final String lastVersion, final CacheScheduler scheduler) throws InterruptedException {
            // To make absolutely sure there is a unique currentTimeMillis
            Thread.sleep(2);
            String version = Long.toString(System.currentTimeMillis());
            CacheScheduler.VersionedCache versionedCache = scheduler.createVersionedCache(id, version);
            // Don't actually read off disk because TravisCI doesn't like that
            versionedCache.getCache().put(KEY, VALUE);
            return versionedCache;
        }
    };
    scheduler = new CacheScheduler(new NoopServiceEmitter(), ImmutableMap.of(UriExtractionNamespace.class, cacheGenerator), cacheManager);
    tmpFile = Files.createTempFile(tmpDir, "druidTestURIExtractionNS", ".dat").toFile();
    try (OutputStream ostream = new FileOutputStream(tmpFile)) {
        try (OutputStreamWriter out = new OutputStreamWriter(ostream, StandardCharsets.UTF_8)) {
            // Since Travis sucks with disk related stuff, we override the disk reading part above.
            // This is safe and should shake out any problem areas that accidentally read the file.
            out.write("SHOULDN'T TRY TO PARSE");
            out.flush();
        }
    }
}
Also used : Path(java.nio.file.Path) CacheGenerator(org.apache.druid.query.lookup.namespace.CacheGenerator) Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) NoopServiceEmitter(org.apache.druid.server.metrics.NoopServiceEmitter) FileOutputStream(java.io.FileOutputStream) UriExtractionNamespace(org.apache.druid.query.lookup.namespace.UriExtractionNamespace) OutputStreamWriter(java.io.OutputStreamWriter) Before(org.junit.Before)

Example 17 with UriExtractionNamespace

use of org.apache.druid.query.lookup.namespace.UriExtractionNamespace in project druid by druid-io.

the class CacheSchedulerTest method testDelete.

private void testDelete() throws InterruptedException {
    // Give it some time between attempts to update
    final long period = 1_000L;
    final UriExtractionNamespace namespace = getUriExtractionNamespace(period);
    CacheScheduler.Entry entry = scheduler.scheduleAndWait(namespace, 10_000);
    Assert.assertNotNull(entry);
    final Future<?> future = entry.getUpdaterFuture();
    Assert.assertFalse(future.isCancelled());
    Assert.assertFalse(future.isDone());
    entry.awaitTotalUpdates(1);
    Assert.assertEquals(VALUE, entry.getCache().get(KEY));
    entry.close();
    try {
        Assert.assertNull(future.get());
    } catch (CancellationException e) {
    // Ignore
    } catch (ExecutionException e) {
        if (!future.isCancelled()) {
            throw new RuntimeException(e);
        }
    }
    Assert.assertTrue(future.isCancelled());
    Assert.assertTrue(future.isDone());
}
Also used : CancellationException(java.util.concurrent.CancellationException) UriExtractionNamespace(org.apache.druid.query.lookup.namespace.UriExtractionNamespace) ExecutionException(java.util.concurrent.ExecutionException)

Example 18 with UriExtractionNamespace

use of org.apache.druid.query.lookup.namespace.UriExtractionNamespace in project druid by druid-io.

the class CacheSchedulerTest method testShutdown.

@Test(timeout = 60_000L)
public void testShutdown() throws InterruptedException {
    final long period = 5L;
    try {
        final UriExtractionNamespace namespace = getUriExtractionNamespace(period);
        try (CacheScheduler.Entry entry = scheduler.schedule(namespace)) {
            final Future<?> future = entry.getUpdaterFuture();
            entry.awaitNextUpdates(1);
            Assert.assertFalse(future.isCancelled());
            Assert.assertFalse(future.isDone());
            final long prior = scheduler.updatesStarted();
            entry.awaitNextUpdates(1);
            Assert.assertTrue(scheduler.updatesStarted() > prior);
        }
    } finally {
        lifecycle.stop();
    }
    while (!cacheManager.waitForServiceToEnd(1_000, TimeUnit.MILLISECONDS)) {
    // keep waiting
    }
    checkNoMoreRunning();
    Assert.assertTrue(cacheManager.scheduledExecutorService().isShutdown());
    Assert.assertTrue(cacheManager.scheduledExecutorService().isTerminated());
}
Also used : UriExtractionNamespace(org.apache.druid.query.lookup.namespace.UriExtractionNamespace) UriExtractionNamespaceTest(org.apache.druid.query.lookup.namespace.UriExtractionNamespaceTest) Test(org.junit.Test)

Aggregations

UriExtractionNamespace (org.apache.druid.query.lookup.namespace.UriExtractionNamespace)18 Test (org.junit.Test)15 UriExtractionNamespaceTest (org.apache.druid.query.lookup.namespace.UriExtractionNamespaceTest)14 File (java.io.File)7 DefaultObjectMapper (org.apache.druid.jackson.DefaultObjectMapper)7 Period (org.joda.time.Period)7 CacheScheduler (org.apache.druid.server.lookup.namespace.cache.CacheScheduler)6 CacheSchedulerTest (org.apache.druid.server.lookup.namespace.cache.CacheSchedulerTest)6 Writer (java.io.Writer)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 URI (java.net.URI)2 Before (org.junit.Before)2 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 CancellationException (java.util.concurrent.CancellationException)1 ExecutionException (java.util.concurrent.ExecutionException)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1