Search in sources :

Example 1 with JDBCExtractionNamespace

use of io.druid.query.lookup.namespace.JDBCExtractionNamespace in project druid by druid-io.

the class JDBCExtractionNamespaceTest method testMapping.

@Test(timeout = 10_000L)
public void testMapping() throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, ExecutionException, InterruptedException, TimeoutException {
    final JDBCExtractionNamespace extractionNamespace = new JDBCExtractionNamespace(derbyConnectorRule.getMetadataConnectorConfig(), tableName, keyName, valName, tsColumn, new Period(0));
    try (CacheScheduler.Entry entry = scheduler.schedule(extractionNamespace)) {
        NamespaceExtractionCacheManagerExecutorsTest.waitFor(entry);
        final Map<String, String> map = entry.getCache();
        for (Map.Entry<String, String> e : renames.entrySet()) {
            String key = e.getKey();
            String val = e.getValue();
            Assert.assertEquals("non-null check", Strings.emptyToNull(val), Strings.emptyToNull(map.get(key)));
        }
        Assert.assertEquals("null check", null, map.get("baz"));
    }
}
Also used : Period(org.joda.time.Period) JDBCExtractionNamespace(io.druid.query.lookup.namespace.JDBCExtractionNamespace) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 2 with JDBCExtractionNamespace

use of io.druid.query.lookup.namespace.JDBCExtractionNamespace in project druid by druid-io.

the class JDBCExtractionNamespaceCacheFactory method ensureDBI.

private DBI ensureDBI(CacheScheduler.EntryImpl<JDBCExtractionNamespace> id, JDBCExtractionNamespace namespace) {
    final CacheScheduler.EntryImpl<JDBCExtractionNamespace> key = id;
    DBI dbi = null;
    if (dbiCache.containsKey(key)) {
        dbi = dbiCache.get(key);
    }
    if (dbi == null) {
        final DBI newDbi = new DBI(namespace.getConnectorConfig().getConnectURI(), namespace.getConnectorConfig().getUser(), namespace.getConnectorConfig().getPassword());
        dbiCache.putIfAbsent(key, newDbi);
        dbi = dbiCache.get(key);
    }
    return dbi;
}
Also used : JDBCExtractionNamespace(io.druid.query.lookup.namespace.JDBCExtractionNamespace) DBI(org.skife.jdbi.v2.DBI) CacheScheduler(io.druid.server.lookup.namespace.cache.CacheScheduler)

Example 3 with JDBCExtractionNamespace

use of io.druid.query.lookup.namespace.JDBCExtractionNamespace in project druid by druid-io.

the class JDBCExtractionNamespaceTest method setup.

@Before
public void setup() throws Exception {
    lifecycle = new Lifecycle();
    updates = new AtomicLong(0L);
    updateLock = new ReentrantLock(true);
    closer = Closer.create();
    setupTeardownService = MoreExecutors.listeningDecorator(Execs.multiThreaded(2, "JDBCExtractionNamespaceTeardown--%s"));
    final ListenableFuture<Handle> setupFuture = setupTeardownService.submit(new Callable<Handle>() {

        @Override
        public Handle call() {
            final Handle handle = derbyConnectorRule.getConnector().getDBI().open();
            Assert.assertEquals(0, handle.createStatement(String.format("CREATE TABLE %s (%s TIMESTAMP, %s VARCHAR(64), %s VARCHAR(64))", tableName, tsColumn_, keyName, valName)).setQueryTimeout(1).execute());
            handle.createStatement(String.format("TRUNCATE TABLE %s", tableName)).setQueryTimeout(1).execute();
            handle.commit();
            closer.register(new Closeable() {

                @Override
                public void close() throws IOException {
                    handle.createStatement("DROP TABLE " + tableName).setQueryTimeout(1).execute();
                    final ListenableFuture future = setupTeardownService.submit(new Runnable() {

                        @Override
                        public void run() {
                            handle.close();
                        }
                    });
                    try (Closeable closeable = new Closeable() {

                        @Override
                        public void close() throws IOException {
                            future.cancel(true);
                        }
                    }) {
                        future.get(10, TimeUnit.SECONDS);
                    } catch (InterruptedException | ExecutionException | TimeoutException e) {
                        throw new IOException("Error closing handle", e);
                    }
                }
            });
            closer.register(new Closeable() {

                @Override
                public void close() throws IOException {
                    if (scheduler == null) {
                        return;
                    }
                    Assert.assertEquals(0, scheduler.getActiveEntries());
                }
            });
            for (Map.Entry<String, String> entry : renames.entrySet()) {
                try {
                    insertValues(handle, entry.getKey(), entry.getValue(), "2015-01-01 00:00:00");
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    throw Throwables.propagate(e);
                }
            }
            NoopServiceEmitter noopServiceEmitter = new NoopServiceEmitter();
            scheduler = new CacheScheduler(noopServiceEmitter, ImmutableMap.<Class<? extends ExtractionNamespace>, ExtractionNamespaceCacheFactory<?>>of(JDBCExtractionNamespace.class, new ExtractionNamespaceCacheFactory<JDBCExtractionNamespace>() {

                private final JDBCExtractionNamespaceCacheFactory delegate = new JDBCExtractionNamespaceCacheFactory();

                @Override
                public CacheScheduler.VersionedCache populateCache(final JDBCExtractionNamespace namespace, final CacheScheduler.EntryImpl<JDBCExtractionNamespace> id, final String lastVersion, final CacheScheduler scheduler) throws InterruptedException {
                    updateLock.lockInterruptibly();
                    try {
                        log.debug("Running cache populator");
                        try {
                            return delegate.populateCache(namespace, id, lastVersion, scheduler);
                        } finally {
                            updates.incrementAndGet();
                        }
                    } finally {
                        updateLock.unlock();
                    }
                }
            }), new OnHeapNamespaceExtractionCacheManager(lifecycle, noopServiceEmitter));
            try {
                lifecycle.start();
            } catch (Exception e) {
                throw Throwables.propagate(e);
            }
            closer.register(new Closeable() {

                @Override
                public void close() throws IOException {
                    final ListenableFuture future = setupTeardownService.submit(new Runnable() {

                        @Override
                        public void run() {
                            lifecycle.stop();
                        }
                    });
                    try (final Closeable closeable = new Closeable() {

                        @Override
                        public void close() throws IOException {
                            future.cancel(true);
                        }
                    }) {
                        future.get(30, TimeUnit.SECONDS);
                    } catch (InterruptedException | ExecutionException | TimeoutException e) {
                        throw new IOException("Error stopping lifecycle", e);
                    }
                }
            });
            return handle;
        }
    });
    try (final Closeable closeable = new Closeable() {

        @Override
        public void close() throws IOException {
            if (!setupFuture.isDone() && !setupFuture.cancel(true) && !setupFuture.isDone()) {
                throw new IOException("Unable to stop future");
            }
        }
    }) {
        handleRef = setupFuture.get(10, TimeUnit.SECONDS);
    }
    Assert.assertNotNull(handleRef);
}
Also used : Closeable(java.io.Closeable) ExtractionNamespaceCacheFactory(io.druid.query.lookup.namespace.ExtractionNamespaceCacheFactory) JDBCExtractionNamespaceCacheFactory(io.druid.server.lookup.namespace.JDBCExtractionNamespaceCacheFactory) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) ReentrantLock(java.util.concurrent.locks.ReentrantLock) JDBCExtractionNamespaceCacheFactory(io.druid.server.lookup.namespace.JDBCExtractionNamespaceCacheFactory) Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) NoopServiceEmitter(io.druid.server.metrics.NoopServiceEmitter) JDBCExtractionNamespace(io.druid.query.lookup.namespace.JDBCExtractionNamespace) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Handle(org.skife.jdbi.v2.Handle) AtomicLong(java.util.concurrent.atomic.AtomicLong) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Before(org.junit.Before)

Example 4 with JDBCExtractionNamespace

use of io.druid.query.lookup.namespace.JDBCExtractionNamespace in project druid by druid-io.

the class JDBCExtractionNamespaceTest method ensureEntry.

private CacheScheduler.Entry ensureEntry() throws NoSuchFieldException, IllegalAccessException, InterruptedException {
    final JDBCExtractionNamespace extractionNamespace = new JDBCExtractionNamespace(derbyConnectorRule.getMetadataConnectorConfig(), tableName, keyName, valName, tsColumn, new Period(10));
    CacheScheduler.Entry entry = scheduler.schedule(extractionNamespace);
    waitForUpdates(1_000L, 2L);
    Assert.assertEquals("sanity check not correct", "bar", entry.getCache().get("foo"));
    return entry;
}
Also used : Period(org.joda.time.Period) JDBCExtractionNamespace(io.druid.query.lookup.namespace.JDBCExtractionNamespace)

Aggregations

JDBCExtractionNamespace (io.druid.query.lookup.namespace.JDBCExtractionNamespace)4 Period (org.joda.time.Period)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 Lifecycle (io.druid.java.util.common.lifecycle.Lifecycle)1 ExtractionNamespaceCacheFactory (io.druid.query.lookup.namespace.ExtractionNamespaceCacheFactory)1 JDBCExtractionNamespaceCacheFactory (io.druid.server.lookup.namespace.JDBCExtractionNamespaceCacheFactory)1 CacheScheduler (io.druid.server.lookup.namespace.cache.CacheScheduler)1 NoopServiceEmitter (io.druid.server.metrics.NoopServiceEmitter)1 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Before (org.junit.Before)1 Test (org.junit.Test)1 DBI (org.skife.jdbi.v2.DBI)1 Handle (org.skife.jdbi.v2.Handle)1