Search in sources :

Example 16 with Lifecycle

use of io.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class TaskMaster method stopLeading.

/**
   * Relinquish leadership. May be called multiple times, even when not currently the leader.
   */
private void stopLeading() {
    giant.lock();
    try {
        if (leading) {
            leading = false;
            mayBeStopped.signalAll();
            final Lifecycle leaderLifecycle = leaderLifecycleRef.getAndSet(null);
            if (leaderLifecycle != null) {
                leaderLifecycle.stop();
            }
        }
    } finally {
        giant.unlock();
    }
}
Also used : Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle)

Example 17 with Lifecycle

use of io.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class NamespaceExtractionCacheManagerExecutorsTest method setUp.

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

        @Override
        public CacheScheduler.VersionedCache populateCache(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.<Class<? extends ExtractionNamespace>, ExtractionNamespaceCacheFactory<?>>of(URIExtractionNamespace.class, cachePopulator), cacheManager);
    tmpFile = Files.createTempFile(tmpDir, "druidTestURIExtractionNS", ".dat").toFile();
    try (OutputStream ostream = new FileOutputStream(tmpFile)) {
        try (OutputStreamWriter out = new OutputStreamWriter(ostream)) {
            // 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) Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) NoopServiceEmitter(io.druid.server.metrics.NoopServiceEmitter) ExtractionNamespaceCacheFactory(io.druid.query.lookup.namespace.ExtractionNamespaceCacheFactory) URIExtractionNamespace(io.druid.query.lookup.namespace.URIExtractionNamespace) FileOutputStream(java.io.FileOutputStream) ExtractionNamespace(io.druid.query.lookup.namespace.ExtractionNamespace) URIExtractionNamespace(io.druid.query.lookup.namespace.URIExtractionNamespace) OutputStreamWriter(java.io.OutputStreamWriter) Before(org.junit.Before)

Example 18 with Lifecycle

use of io.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class ChainedExecutionQueryRunnerTest method testQueryCancellation.

@Test(timeout = 60000)
public void testQueryCancellation() throws Exception {
    ExecutorService exec = PrioritizedExecutorService.create(new Lifecycle(), new DruidProcessingConfig() {

        @Override
        public String getFormatString() {
            return "test";
        }

        @Override
        public int getNumThreads() {
            return 2;
        }
    });
    final CountDownLatch queriesStarted = new CountDownLatch(2);
    final CountDownLatch queriesInterrupted = new CountDownLatch(2);
    final CountDownLatch queryIsRegistered = new CountDownLatch(1);
    Capture<ListenableFuture> capturedFuture = EasyMock.newCapture();
    QueryWatcher watcher = EasyMock.createStrictMock(QueryWatcher.class);
    watcher.registerQuery(EasyMock.<Query>anyObject(), EasyMock.and(EasyMock.<ListenableFuture>anyObject(), EasyMock.capture(capturedFuture)));
    EasyMock.expectLastCall().andAnswer(new IAnswer<Void>() {

        @Override
        public Void answer() throws Throwable {
            queryIsRegistered.countDown();
            return null;
        }
    }).once();
    EasyMock.replay(watcher);
    ArrayBlockingQueue<DyingQueryRunner> interrupted = new ArrayBlockingQueue<>(3);
    Set<DyingQueryRunner> runners = Sets.newHashSet(new DyingQueryRunner(queriesStarted, queriesInterrupted, interrupted), new DyingQueryRunner(queriesStarted, queriesInterrupted, interrupted), new DyingQueryRunner(queriesStarted, queriesInterrupted, interrupted));
    ChainedExecutionQueryRunner chainedRunner = new ChainedExecutionQueryRunner<>(exec, watcher, Lists.<QueryRunner<Integer>>newArrayList(runners));
    Map<String, Object> context = ImmutableMap.<String, Object>of();
    final Sequence seq = chainedRunner.run(Druids.newTimeseriesQueryBuilder().dataSource("test").intervals("2014/2015").aggregators(Lists.<AggregatorFactory>newArrayList(new CountAggregatorFactory("count"))).build(), context);
    Future resultFuture = Executors.newFixedThreadPool(1).submit(new Runnable() {

        @Override
        public void run() {
            Sequences.toList(seq, Lists.newArrayList());
        }
    });
    // wait for query to register and start
    queryIsRegistered.await();
    queriesStarted.await();
    // cancel the query
    Assert.assertTrue(capturedFuture.hasCaptured());
    ListenableFuture future = capturedFuture.getValue();
    future.cancel(true);
    QueryInterruptedException cause = null;
    try {
        resultFuture.get();
    } catch (ExecutionException e) {
        Assert.assertTrue(e.getCause() instanceof QueryInterruptedException);
        cause = (QueryInterruptedException) e.getCause();
    }
    queriesInterrupted.await();
    Assert.assertNotNull(cause);
    Assert.assertTrue(future.isCancelled());
    DyingQueryRunner interrupted1 = interrupted.poll();
    synchronized (interrupted1) {
        Assert.assertTrue("runner 1 started", interrupted1.hasStarted);
        Assert.assertTrue("runner 1 interrupted", interrupted1.interrupted);
    }
    DyingQueryRunner interrupted2 = interrupted.poll();
    synchronized (interrupted2) {
        Assert.assertTrue("runner 2 started", interrupted2.hasStarted);
        Assert.assertTrue("runner 2 interrupted", interrupted2.interrupted);
    }
    runners.remove(interrupted1);
    runners.remove(interrupted2);
    DyingQueryRunner remainingRunner = runners.iterator().next();
    synchronized (remainingRunner) {
        Assert.assertTrue("runner 3 should be interrupted or not have started", !remainingRunner.hasStarted || remainingRunner.interrupted);
    }
    Assert.assertFalse("runner 1 not completed", interrupted1.hasCompleted);
    Assert.assertFalse("runner 2 not completed", interrupted2.hasCompleted);
    Assert.assertFalse("runner 3 not completed", remainingRunner.hasCompleted);
    EasyMock.verify(watcher);
}
Also used : Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) Sequence(io.druid.java.util.common.guava.Sequence) CountDownLatch(java.util.concurrent.CountDownLatch) IAnswer(org.easymock.IAnswer) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) CountAggregatorFactory(io.druid.query.aggregation.CountAggregatorFactory) ExecutorService(java.util.concurrent.ExecutorService) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 19 with Lifecycle

use of io.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class StaticMapExtractionNamespaceCacheFactoryTest method setup.

@Before
public void setup() throws Exception {
    lifecycle = new Lifecycle();
    lifecycle.start();
    NoopServiceEmitter noopServiceEmitter = new NoopServiceEmitter();
    scheduler = new CacheScheduler(noopServiceEmitter, Collections.<Class<? extends ExtractionNamespace>, ExtractionNamespaceCacheFactory<?>>emptyMap(), new OnHeapNamespaceExtractionCacheManager(lifecycle, noopServiceEmitter));
}
Also used : ExtractionNamespaceCacheFactory(io.druid.query.lookup.namespace.ExtractionNamespaceCacheFactory) OnHeapNamespaceExtractionCacheManager(io.druid.server.lookup.namespace.cache.OnHeapNamespaceExtractionCacheManager) Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) StaticMapExtractionNamespace(io.druid.query.lookup.namespace.StaticMapExtractionNamespace) ExtractionNamespace(io.druid.query.lookup.namespace.ExtractionNamespace) NoopServiceEmitter(io.druid.server.metrics.NoopServiceEmitter) CacheScheduler(io.druid.server.lookup.namespace.cache.CacheScheduler) Before(org.junit.Before)

Example 20 with Lifecycle

use of io.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class URIExtractionNamespaceCacheFactoryTest method getParameters.

@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> getParameters() throws NoSuchMethodException {
    final List<Object[]> compressionParams = ImmutableList.of(new Object[] { ".dat", new Function<File, OutputStream>() {

        @Nullable
        @Override
        public OutputStream apply(@Nullable File outFile) {
            try {
                return new FileOutputStream(outFile);
            } catch (IOException ex) {
                throw Throwables.propagate(ex);
            }
        }
    } }, new Object[] { ".gz", new Function<File, OutputStream>() {

        @Nullable
        @Override
        public OutputStream apply(@Nullable File outFile) {
            try {
                final FileOutputStream fos = new FileOutputStream(outFile);
                return new GZIPOutputStream(fos) {

                    @Override
                    public void close() throws IOException {
                        try {
                            super.close();
                        } finally {
                            fos.close();
                        }
                    }
                };
            } catch (IOException ex) {
                throw Throwables.propagate(ex);
            }
        }
    } });
    final List<Function<Lifecycle, NamespaceExtractionCacheManager>> cacheManagerCreators = ImmutableList.of(new Function<Lifecycle, NamespaceExtractionCacheManager>() {

        @Override
        public NamespaceExtractionCacheManager apply(Lifecycle lifecycle) {
            return new OnHeapNamespaceExtractionCacheManager(lifecycle, new NoopServiceEmitter());
        }
    }, new Function<Lifecycle, NamespaceExtractionCacheManager>() {

        @Override
        public NamespaceExtractionCacheManager apply(Lifecycle lifecycle) {
            return new OffHeapNamespaceExtractionCacheManager(lifecycle, new NoopServiceEmitter());
        }
    });
    return new Iterable<Object[]>() {

        @Override
        public Iterator<Object[]> iterator() {
            return new Iterator<Object[]>() {

                Iterator<Object[]> compressionIt = compressionParams.iterator();

                Iterator<Function<Lifecycle, NamespaceExtractionCacheManager>> cacheManagerCreatorsIt = cacheManagerCreators.iterator();

                Object[] compressions = compressionIt.next();

                @Override
                public boolean hasNext() {
                    return compressionIt.hasNext() || cacheManagerCreatorsIt.hasNext();
                }

                @Override
                public Object[] next() {
                    if (cacheManagerCreatorsIt.hasNext()) {
                        Function<Lifecycle, NamespaceExtractionCacheManager> cacheManagerCreator = cacheManagerCreatorsIt.next();
                        return new Object[] { compressions[0], compressions[1], cacheManagerCreator };
                    } else {
                        cacheManagerCreatorsIt = cacheManagerCreators.iterator();
                        compressions = compressionIt.next();
                        return next();
                    }
                }

                @Override
                public void remove() {
                    throw new UOE("Cannot remove");
                }
            };
        }
    };
}
Also used : OnHeapNamespaceExtractionCacheManager(io.druid.server.lookup.namespace.cache.OnHeapNamespaceExtractionCacheManager) GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) NoopServiceEmitter(io.druid.server.metrics.NoopServiceEmitter) UOE(io.druid.java.util.common.UOE) IOException(java.io.IOException) OffHeapNamespaceExtractionCacheManager(io.druid.server.lookup.namespace.cache.OffHeapNamespaceExtractionCacheManager) Function(com.google.common.base.Function) NamespaceExtractionCacheManager(io.druid.server.lookup.namespace.cache.NamespaceExtractionCacheManager) OffHeapNamespaceExtractionCacheManager(io.druid.server.lookup.namespace.cache.OffHeapNamespaceExtractionCacheManager) OnHeapNamespaceExtractionCacheManager(io.druid.server.lookup.namespace.cache.OnHeapNamespaceExtractionCacheManager) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) Iterator(java.util.Iterator) File(java.io.File) Nullable(javax.annotation.Nullable)

Aggregations

Lifecycle (io.druid.java.util.common.lifecycle.Lifecycle)25 Injector (com.google.inject.Injector)10 Before (org.junit.Before)9 Test (org.junit.Test)8 Module (com.google.inject.Module)6 Binder (com.google.inject.Binder)5 NoopServiceEmitter (io.druid.server.metrics.NoopServiceEmitter)5 ExtractionNamespaceCacheFactory (io.druid.query.lookup.namespace.ExtractionNamespaceCacheFactory)4 IOException (java.io.IOException)4 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)3 ManageLifecycle (io.druid.guice.ManageLifecycle)3 ExtractionNamespace (io.druid.query.lookup.namespace.ExtractionNamespace)3 OnHeapNamespaceExtractionCacheManager (io.druid.server.lookup.namespace.cache.OnHeapNamespaceExtractionCacheManager)3 ExecutionException (java.util.concurrent.ExecutionException)3 Provides (com.google.inject.Provides)2 Self (io.druid.guice.annotations.Self)2 Sequence (io.druid.java.util.common.guava.Sequence)2 CountAggregatorFactory (io.druid.query.aggregation.CountAggregatorFactory)2 JDBCExtractionNamespace (io.druid.query.lookup.namespace.JDBCExtractionNamespace)2 URIExtractionNamespace (io.druid.query.lookup.namespace.URIExtractionNamespace)2