Search in sources :

Example 1 with LookupCache

use of org.graylog2.plugin.lookup.LookupCache in project graylog2-server by Graylog2.

the class LookupTableService method handleCacheUpdate.

@Subscribe
public void handleCacheUpdate(CachesUpdated updated) {
    scheduler.schedule(() -> {
        // first we create the new cache instance and start it
        // then we retrieve the old one so we can safely stop it later
        // then we build a new lookup table instance with the new cache instance
        // last we can remove the old lookup table instance and stop the original cache
        // collect old cache instances
        final ImmutableSet.Builder<LookupCache> existingCaches = ImmutableSet.builder();
        // create new cache and lookup table instances
        final Map<CacheDto, LookupCache> newCaches = createCaches(configService.findCachesForIds(updated.ids()));
        final CountDownLatch runningLatch = new CountDownLatch(newCaches.size());
        newCaches.forEach((cacheDto, cache) -> {
            cache.addListener(new CacheListener(cacheDto, cache, runningLatch, existingCaches::add), scheduler);
            cache.startAsync();
        });
        // wait until everything is either running or failed before starting the
        awaitUninterruptibly(runningLatch);
        // when a cache is updated, the lookup tables that use it need to be updated as well
        final Collection<LookupTableDto> tablesToUpdate = configService.findTablesForCacheIds(updated.ids());
        tablesToUpdate.forEach(this::createLookupTable);
        // stop old caches
        existingCaches.build().forEach(AbstractIdleService::stopAsync);
    }, 0, TimeUnit.SECONDS);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) LookupCache(org.graylog2.plugin.lookup.LookupCache) LookupTableDto(org.graylog2.lookup.dto.LookupTableDto) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) CountDownLatch(java.util.concurrent.CountDownLatch) CacheDto(org.graylog2.lookup.dto.CacheDto) Subscribe(com.google.common.eventbus.Subscribe)

Example 2 with LookupCache

use of org.graylog2.plugin.lookup.LookupCache in project graylog2-server by Graylog2.

the class LookupTableService method createAndStartCaches.

private CountDownLatch createAndStartCaches() {
    final Map<CacheDto, LookupCache> caches = createCaches(configService.loadAllCaches());
    final CountDownLatch latch = new CountDownLatch(toIntExact(caches.size()));
    caches.forEach((cacheDto, lookupCache) -> {
        lookupCache.addListener(new CacheListener(cacheDto, lookupCache, latch), scheduler);
        lookupCache.startAsync();
    });
    return latch;
}
Also used : LookupCache(org.graylog2.plugin.lookup.LookupCache) CountDownLatch(java.util.concurrent.CountDownLatch) CacheDto(org.graylog2.lookup.dto.CacheDto)

Example 3 with LookupCache

use of org.graylog2.plugin.lookup.LookupCache in project graylog2-server by Graylog2.

the class LookupTableService method createCache.

private LookupCache createCache(CacheDto dto) {
    try {
        final LookupCache.Factory<? extends LookupCache> factory = cacheFactories.get(dto.config().type());
        if (factory == null) {
            LOG.warn("Unable to load cache {} of type {}, missing a factory. Is a required plugin missing?", dto.name(), dto.config().type());
            // TODO system notification
            return null;
        }
        final LookupCache cache = factory.create(dto.id(), dto.name(), dto.config());
        cache.addListener(new LoggingServiceListener("Cache", String.format(Locale.ENGLISH, "%s/%s [@%s]", dto.name(), dto.id(), objectId(cache)), LOG), scheduler);
        return cache;
    } catch (Exception e) {
        LOG.error("Couldn't create cache <{}/{}>", dto.name(), dto.id(), e);
        return null;
    }
}
Also used : LookupCache(org.graylog2.plugin.lookup.LookupCache) LoggingServiceListener(org.graylog2.utilities.LoggingServiceListener)

Example 4 with LookupCache

use of org.graylog2.plugin.lookup.LookupCache in project graylog2-server by Graylog2.

the class LookupTableService method createLookupTable.

private LookupTable createLookupTable(LookupTableDto dto) {
    final LookupCache cache = idToCache.get(dto.cacheId());
    if (cache == null) {
        LOG.warn("Lookup table {} is referencing a missing cache {}, check if it started properly.", dto.name(), dto.cacheId());
        return null;
    }
    final LookupDataAdapter adapter = idToAdapter.get(dto.dataAdapterId());
    if (adapter == null) {
        LOG.warn("Lookup table {} is referencing a missing data adapter {}, check if it started properly.", dto.name(), dto.dataAdapterId());
        return null;
    }
    final LookupDefaultSingleValue defaultSingleValue;
    try {
        defaultSingleValue = LookupDefaultSingleValue.create(dto.defaultSingleValue(), dto.defaultSingleValueType());
    } catch (Exception e) {
        LOG.error("Could not create default single value object for lookup table {}/{}: {}", dto.name(), dto.id(), e.getMessage());
        return null;
    }
    final LookupDefaultMultiValue defaultMultiValue;
    try {
        defaultMultiValue = LookupDefaultMultiValue.create(dto.defaultMultiValue(), dto.defaultMultiValueType());
    } catch (Exception e) {
        LOG.error("Could not create default multi value object for lookup table {}/{}: {}", dto.name(), dto.id(), e.getMessage());
        return null;
    }
    final LookupTable table = LookupTable.builder().id(dto.id()).name(dto.name()).description(dto.description()).title(dto.title()).cache(cache).dataAdapter(adapter).defaultSingleValue(defaultSingleValue).defaultMultiValue(defaultMultiValue).build();
    final LookupCache newCache = table.cache();
    final LookupDataAdapter newAdapter = table.dataAdapter();
    LOG.info("Starting lookup table {}/{} [@{}] using cache {}/{} [@{}], data adapter {}/{} [@{}]", table.name(), table.id(), objectId(table), newCache.name(), newCache.id(), objectId(newCache), newAdapter.name(), newAdapter.id(), objectId(newAdapter));
    final LookupTable previous = liveTables.put(dto.name(), table);
    if (previous != null) {
        LOG.info("Replaced previous lookup table {} [@{}]", previous.name(), objectId(previous));
    }
    return table;
}
Also used : LookupDataAdapter(org.graylog2.plugin.lookup.LookupDataAdapter) LookupCache(org.graylog2.plugin.lookup.LookupCache)

Aggregations

LookupCache (org.graylog2.plugin.lookup.LookupCache)4 CountDownLatch (java.util.concurrent.CountDownLatch)2 CacheDto (org.graylog2.lookup.dto.CacheDto)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Subscribe (com.google.common.eventbus.Subscribe)1 AbstractIdleService (com.google.common.util.concurrent.AbstractIdleService)1 LookupTableDto (org.graylog2.lookup.dto.LookupTableDto)1 LookupDataAdapter (org.graylog2.plugin.lookup.LookupDataAdapter)1 LoggingServiceListener (org.graylog2.utilities.LoggingServiceListener)1