Search in sources :

Example 1 with JdbcExtractionNamespace

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

the class JdbcExtractionNamespaceTest method ensureEntry.

private CacheScheduler.Entry ensureEntry() throws InterruptedException {
    final JdbcExtractionNamespace extractionNamespace = new JdbcExtractionNamespace(derbyConnectorRule.getMetadataConnectorConfig(), TABLE_NAME, KEY_NAME, VAL_NAME, tsColumn, null, new Period(10), null, new JdbcAccessSecurityConfig());
    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 : JdbcAccessSecurityConfig(org.apache.druid.server.initialization.JdbcAccessSecurityConfig) Period(org.joda.time.Period) JdbcExtractionNamespace(org.apache.druid.query.lookup.namespace.JdbcExtractionNamespace)

Example 2 with JdbcExtractionNamespace

use of org.apache.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(StringUtils.format("CREATE TABLE %s (%s TIMESTAMP, %s VARCHAR(64), %s VARCHAR(64), %s VARCHAR(64))", TABLE_NAME, TS_COLUMN, FILTER_COLUMN, KEY_NAME, VAL_NAME)).setQueryTimeout(1).execute());
            handle.createStatement(StringUtils.format("TRUNCATE TABLE %s", TABLE_NAME)).setQueryTimeout(1).execute();
            handle.commit();
            closer.register(new Closeable() {

                @Override
                public void close() throws IOException {
                    handle.createStatement("DROP TABLE " + TABLE_NAME).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() {
                            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() {
                    if (scheduler == null) {
                        return;
                    }
                    Assert.assertEquals(0, scheduler.getActiveEntries());
                }
            });
            for (Map.Entry<String, String[]> entry : RENAMES.entrySet()) {
                try {
                    String key = entry.getKey();
                    String value = entry.getValue()[0];
                    String filter = entry.getValue()[1];
                    insertValues(handle, key, value, filter, "2015-01-01 00:00:00");
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    throw new RuntimeException(e);
                }
            }
            NoopServiceEmitter noopServiceEmitter = new NoopServiceEmitter();
            scheduler = new CacheScheduler(noopServiceEmitter, ImmutableMap.of(JdbcExtractionNamespace.class, new CacheGenerator<JdbcExtractionNamespace>() {

                private final JdbcCacheGenerator delegate = new JdbcCacheGenerator();

                @Override
                public CacheScheduler.VersionedCache generateCache(final JdbcExtractionNamespace namespace, final CacheScheduler.EntryImpl<JdbcExtractionNamespace> id, final String lastVersion, final CacheScheduler scheduler) throws InterruptedException {
                    updateLock.lockInterruptibly();
                    try {
                        log.debug("Running cache generator");
                        try {
                            return delegate.generateCache(namespace, id, lastVersion, scheduler);
                        } finally {
                            updates.incrementAndGet();
                        }
                    } finally {
                        updateLock.unlock();
                    }
                }
            }), new OnHeapNamespaceExtractionCacheManager(lifecycle, noopServiceEmitter, new NamespaceExtractionConfig()));
            try {
                lifecycle.start();
            } catch (Exception e) {
                throw new RuntimeException(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() {
                            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 ignore = () -> setupFuture.cancel(true)) {
        handleRef = setupFuture.get(10, TimeUnit.SECONDS);
    }
    Assert.assertNotNull(handleRef);
}
Also used : JdbcCacheGenerator(org.apache.druid.server.lookup.namespace.JdbcCacheGenerator) CacheGenerator(org.apache.druid.query.lookup.namespace.CacheGenerator) Closeable(java.io.Closeable) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) ReentrantLock(java.util.concurrent.locks.ReentrantLock) JdbcCacheGenerator(org.apache.druid.server.lookup.namespace.JdbcCacheGenerator) NamespaceExtractionConfig(org.apache.druid.server.lookup.namespace.NamespaceExtractionConfig) Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) NoopServiceEmitter(org.apache.druid.server.metrics.NoopServiceEmitter) 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) JdbcExtractionNamespace(org.apache.druid.query.lookup.namespace.JdbcExtractionNamespace) Before(org.junit.Before)

Example 3 with JdbcExtractionNamespace

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

the class JdbcExtractionNamespaceTest method testSerde.

@Test
public void testSerde() throws IOException {
    final JdbcAccessSecurityConfig securityConfig = new JdbcAccessSecurityConfig();
    final JdbcExtractionNamespace extractionNamespace = new JdbcExtractionNamespace(derbyConnectorRule.getMetadataConnectorConfig(), TABLE_NAME, KEY_NAME, VAL_NAME, tsColumn, "some filter", new Period(10), null, securityConfig);
    final ObjectMapper mapper = new DefaultObjectMapper();
    mapper.setInjectableValues(new Std().addValue(JdbcAccessSecurityConfig.class, securityConfig));
    final ExtractionNamespace extractionNamespace2 = mapper.readValue(mapper.writeValueAsBytes(extractionNamespace), ExtractionNamespace.class);
    Assert.assertEquals(extractionNamespace, extractionNamespace2);
}
Also used : Std(com.fasterxml.jackson.databind.InjectableValues.Std) JdbcAccessSecurityConfig(org.apache.druid.server.initialization.JdbcAccessSecurityConfig) Period(org.joda.time.Period) ExtractionNamespace(org.apache.druid.query.lookup.namespace.ExtractionNamespace) JdbcExtractionNamespace(org.apache.druid.query.lookup.namespace.JdbcExtractionNamespace) DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) JdbcExtractionNamespace(org.apache.druid.query.lookup.namespace.JdbcExtractionNamespace) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) Test(org.junit.Test)

Example 4 with JdbcExtractionNamespace

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

the class JdbcCacheGeneratorTest method indicatesMissingJdbcJarsWithTsColumn.

@Test
public void indicatesMissingJdbcJarsWithTsColumn() {
    String tsColumn = "tsColumn";
    JdbcExtractionNamespace missingJarNamespace = createJdbcExtractionNamespace(MISSING_METADATA_STORAGE_CONNECTOR_CONFIG, tsColumn);
    exception.expect(IllegalStateException.class);
    exception.expectMessage(MISSING_JDB_DRIVER_JAR_MSG);
    target.generateCache(missingJarNamespace, KEY, LAST_VERSION, SCHEDULER);
}
Also used : JdbcExtractionNamespace(org.apache.druid.query.lookup.namespace.JdbcExtractionNamespace) Test(org.junit.Test)

Example 5 with JdbcExtractionNamespace

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

the class JdbcCacheGeneratorTest method indicatesMissingJdbcJarsWithoutTsColumn.

@Test
public void indicatesMissingJdbcJarsWithoutTsColumn() {
    String missingTsColumn = null;
    // for missingTsColumn
    @SuppressWarnings("ConstantConditions") JdbcExtractionNamespace missingJarNamespace = createJdbcExtractionNamespace(MISSING_METADATA_STORAGE_CONNECTOR_CONFIG, missingTsColumn);
    exception.expect(IllegalStateException.class);
    exception.expectMessage(MISSING_JDB_DRIVER_JAR_MSG);
    target.generateCache(missingJarNamespace, KEY, LAST_VERSION, SCHEDULER);
}
Also used : JdbcExtractionNamespace(org.apache.druid.query.lookup.namespace.JdbcExtractionNamespace) Test(org.junit.Test)

Aggregations

JdbcExtractionNamespace (org.apache.druid.query.lookup.namespace.JdbcExtractionNamespace)7 Test (org.junit.Test)5 JdbcAccessSecurityConfig (org.apache.druid.server.initialization.JdbcAccessSecurityConfig)4 Period (org.joda.time.Period)4 ImmutableMap (com.google.common.collect.ImmutableMap)2 Map (java.util.Map)2 Std (com.fasterxml.jackson.databind.InjectableValues.Std)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 Closeable (java.io.Closeable)1 IOException (java.io.IOException)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 DefaultObjectMapper (org.apache.druid.jackson.DefaultObjectMapper)1 Lifecycle (org.apache.druid.java.util.common.lifecycle.Lifecycle)1 CacheGenerator (org.apache.druid.query.lookup.namespace.CacheGenerator)1 ExtractionNamespace (org.apache.druid.query.lookup.namespace.ExtractionNamespace)1 JdbcCacheGenerator (org.apache.druid.server.lookup.namespace.JdbcCacheGenerator)1