use of org.graylog2.lookup.dto.CacheDto in project graylog2-server by Graylog2.
the class LookupTableResource method updateCache.
@PUT
@Path("caches/{idOrName}")
@AuditEvent(type = AuditEventTypes.LOOKUP_CACHE_UPDATE)
@ApiOperation(value = "Update the given cache settings")
public CacheApi updateCache(@ApiParam(name = "idOrName") @PathParam("idOrName") @NotEmpty String idOrName, @ApiParam CacheApi toUpdate) {
checkLookupCacheId(idOrName, toUpdate);
checkPermission(RestPermissions.LOOKUP_TABLES_EDIT, toUpdate.id());
CacheDto saved = dbCacheService.save(toUpdate.toDto());
return CacheApi.fromDto(saved);
}
use of org.graylog2.lookup.dto.CacheDto 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);
}
use of org.graylog2.lookup.dto.CacheDto 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;
}
use of org.graylog2.lookup.dto.CacheDto in project graylog2-server by Graylog2.
the class LookupCacheFacadeTest method createNativeEntity.
@Test
public void createNativeEntity() {
final Entity entity = EntityV1.builder().id(ModelId.of("1")).type(ModelTypes.LOOKUP_CACHE_V1).data(objectMapper.convertValue(LookupCacheEntity.create(ValueReference.of("no-op-cache"), ValueReference.of("No-op cache"), ValueReference.of("No-op cache"), ReferenceMapUtils.toReferenceMap(ImmutableMap.of("type", "none"))), JsonNode.class)).build();
assertThat(cacheService.findAll()).isEmpty();
final NativeEntity<CacheDto> nativeEntity = facade.createNativeEntity(entity, Collections.emptyMap(), Collections.emptyMap(), "username");
final NativeEntityDescriptor descriptor = nativeEntity.descriptor();
final CacheDto cacheDto = nativeEntity.entity();
assertThat(nativeEntity.descriptor().id()).isNotNull();
assertThat(descriptor.type()).isEqualTo(ModelTypes.LOOKUP_CACHE_V1);
assertThat(cacheDto.name()).isEqualTo("no-op-cache");
assertThat(cacheDto.title()).isEqualTo("No-op cache");
assertThat(cacheDto.description()).isEqualTo("No-op cache");
assertThat(cacheDto.config().type()).isEqualTo("none");
assertThat(cacheService.findAll()).hasSize(1);
}
use of org.graylog2.lookup.dto.CacheDto in project graylog2-server by Graylog2.
the class LookupCacheFacadeTest method exportNativeEntity.
@Test
public void exportNativeEntity() {
final CacheDto cacheDto = CacheDto.builder().id("1234567890").name("cache-name").title("Cache Title").description("Cache Description").config(new FallbackCacheConfig()).build();
final EntityDescriptor descriptor = EntityDescriptor.create(cacheDto.id(), ModelTypes.LOOKUP_CACHE_V1);
final EntityDescriptorIds entityDescriptorIds = EntityDescriptorIds.of(descriptor);
final Entity entity = facade.exportNativeEntity(cacheDto, entityDescriptorIds);
assertThat(entity).isInstanceOf(EntityV1.class);
assertThat(entity.id()).isEqualTo(ModelId.of(entityDescriptorIds.get(descriptor).orElse(null)));
assertThat(entity.type()).isEqualTo(ModelTypes.LOOKUP_CACHE_V1);
final EntityV1 entityV1 = (EntityV1) entity;
final LookupCacheEntity lookupCacheEntity = objectMapper.convertValue(entityV1.data(), LookupCacheEntity.class);
assertThat(lookupCacheEntity.name()).isEqualTo(ValueReference.of("cache-name"));
assertThat(lookupCacheEntity.title()).isEqualTo(ValueReference.of("Cache Title"));
assertThat(lookupCacheEntity.description()).isEqualTo(ValueReference.of("Cache Description"));
assertThat(lookupCacheEntity.configuration()).containsEntry("type", ValueReference.of("FallbackCacheConfig"));
}
Aggregations