Search in sources :

Example 1 with CacheScheduler

use of io.druid.server.lookup.namespace.cache.CacheScheduler in project druid by druid-io.

the class NamespacedExtractorModuleTest method setUp.

@Before
public void setUp() throws Exception {
    final Map<Class<? extends ExtractionNamespace>, ExtractionNamespaceCacheFactory<?>> factoryMap = ImmutableMap.<Class<? extends ExtractionNamespace>, ExtractionNamespaceCacheFactory<?>>of(URIExtractionNamespace.class, new URIExtractionNamespaceCacheFactory(ImmutableMap.<String, SearchableVersionedDataFinder>of("file", new LocalFileTimestampVersionFinder())), JDBCExtractionNamespace.class, new JDBCExtractionNamespaceCacheFactory());
    lifecycle = new Lifecycle();
    lifecycle.start();
    NoopServiceEmitter noopServiceEmitter = new NoopServiceEmitter();
    scheduler = new CacheScheduler(noopServiceEmitter, factoryMap, new OnHeapNamespaceExtractionCacheManager(lifecycle, noopServiceEmitter));
}
Also used : SearchableVersionedDataFinder(io.druid.data.SearchableVersionedDataFinder) OnHeapNamespaceExtractionCacheManager(io.druid.server.lookup.namespace.cache.OnHeapNamespaceExtractionCacheManager) Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) NoopServiceEmitter(io.druid.server.metrics.NoopServiceEmitter) ExtractionNamespaceCacheFactory(io.druid.query.lookup.namespace.ExtractionNamespaceCacheFactory) ExtractionNamespace(io.druid.query.lookup.namespace.ExtractionNamespace) URIExtractionNamespace(io.druid.query.lookup.namespace.URIExtractionNamespace) JDBCExtractionNamespace(io.druid.query.lookup.namespace.JDBCExtractionNamespace) LocalFileTimestampVersionFinder(io.druid.segment.loading.LocalFileTimestampVersionFinder) CacheScheduler(io.druid.server.lookup.namespace.cache.CacheScheduler) Before(org.junit.Before)

Example 2 with CacheScheduler

use of io.druid.server.lookup.namespace.cache.CacheScheduler in project druid by druid-io.

the class JDBCExtractionNamespaceCacheFactory method populateCache.

@Override
@Nullable
public CacheScheduler.VersionedCache populateCache(final JDBCExtractionNamespace namespace, final CacheScheduler.EntryImpl<JDBCExtractionNamespace> entryId, final String lastVersion, final CacheScheduler scheduler) {
    final long lastCheck = lastVersion == null ? JodaUtils.MIN_INSTANT : Long.parseLong(lastVersion);
    final Long lastDBUpdate = lastUpdates(entryId, namespace);
    if (lastDBUpdate != null && lastDBUpdate <= lastCheck) {
        return null;
    }
    final long dbQueryStart = System.currentTimeMillis();
    final DBI dbi = ensureDBI(entryId, namespace);
    final String table = namespace.getTable();
    final String valueColumn = namespace.getValueColumn();
    final String keyColumn = namespace.getKeyColumn();
    LOG.debug("Updating %s", entryId);
    final List<Pair<String, String>> pairs = dbi.withHandle(new HandleCallback<List<Pair<String, String>>>() {

        @Override
        public List<Pair<String, String>> withHandle(Handle handle) throws Exception {
            final String query;
            query = String.format("SELECT %s, %s FROM %s", keyColumn, valueColumn, table);
            return handle.createQuery(query).map(new ResultSetMapper<Pair<String, String>>() {

                @Override
                public Pair<String, String> map(final int index, final ResultSet r, final StatementContext ctx) throws SQLException {
                    return new Pair<>(r.getString(keyColumn), r.getString(valueColumn));
                }
            }).list();
        }
    });
    final String newVersion;
    if (lastDBUpdate != null) {
        newVersion = lastDBUpdate.toString();
    } else {
        newVersion = String.format("%d", dbQueryStart);
    }
    final CacheScheduler.VersionedCache versionedCache = scheduler.createVersionedCache(entryId, newVersion);
    try {
        final Map<String, String> cache = versionedCache.getCache();
        for (Pair<String, String> pair : pairs) {
            cache.put(pair.lhs, pair.rhs);
        }
        LOG.info("Finished loading %d values for %s", cache.size(), entryId);
        return versionedCache;
    } catch (Throwable t) {
        try {
            versionedCache.close();
        } catch (Exception e) {
            t.addSuppressed(e);
        }
        throw t;
    }
}
Also used : SQLException(java.sql.SQLException) DBI(org.skife.jdbi.v2.DBI) SQLException(java.sql.SQLException) Handle(org.skife.jdbi.v2.Handle) StatementContext(org.skife.jdbi.v2.StatementContext) ResultSet(java.sql.ResultSet) List(java.util.List) CacheScheduler(io.druid.server.lookup.namespace.cache.CacheScheduler) Pair(io.druid.java.util.common.Pair) Nullable(javax.annotation.Nullable)

Example 3 with CacheScheduler

use of io.druid.server.lookup.namespace.cache.CacheScheduler 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)

Aggregations

CacheScheduler (io.druid.server.lookup.namespace.cache.CacheScheduler)3 Lifecycle (io.druid.java.util.common.lifecycle.Lifecycle)2 ExtractionNamespace (io.druid.query.lookup.namespace.ExtractionNamespace)2 ExtractionNamespaceCacheFactory (io.druid.query.lookup.namespace.ExtractionNamespaceCacheFactory)2 OnHeapNamespaceExtractionCacheManager (io.druid.server.lookup.namespace.cache.OnHeapNamespaceExtractionCacheManager)2 NoopServiceEmitter (io.druid.server.metrics.NoopServiceEmitter)2 Before (org.junit.Before)2 SearchableVersionedDataFinder (io.druid.data.SearchableVersionedDataFinder)1 Pair (io.druid.java.util.common.Pair)1 JDBCExtractionNamespace (io.druid.query.lookup.namespace.JDBCExtractionNamespace)1 StaticMapExtractionNamespace (io.druid.query.lookup.namespace.StaticMapExtractionNamespace)1 URIExtractionNamespace (io.druid.query.lookup.namespace.URIExtractionNamespace)1 LocalFileTimestampVersionFinder (io.druid.segment.loading.LocalFileTimestampVersionFinder)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 List (java.util.List)1 Nullable (javax.annotation.Nullable)1 DBI (org.skife.jdbi.v2.DBI)1 Handle (org.skife.jdbi.v2.Handle)1 StatementContext (org.skife.jdbi.v2.StatementContext)1