Search in sources :

Example 6 with CacheDto

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);
}
Also used : CacheDto(org.graylog2.lookup.dto.CacheDto) Path(javax.ws.rs.Path) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT)

Example 7 with CacheDto

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);
}
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 8 with CacheDto

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;
}
Also used : LookupCache(org.graylog2.plugin.lookup.LookupCache) CountDownLatch(java.util.concurrent.CountDownLatch) CacheDto(org.graylog2.lookup.dto.CacheDto)

Example 9 with CacheDto

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);
}
Also used : NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) LookupCacheEntity(org.graylog2.contentpacks.model.entities.LookupCacheEntity) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) CacheDto(org.graylog2.lookup.dto.CacheDto) Test(org.junit.Test)

Example 10 with CacheDto

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"));
}
Also used : EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) LookupCacheEntity(org.graylog2.contentpacks.model.entities.LookupCacheEntity) EntityDescriptorIds(org.graylog2.contentpacks.EntityDescriptorIds) CacheDto(org.graylog2.lookup.dto.CacheDto) LookupCacheEntity(org.graylog2.contentpacks.model.entities.LookupCacheEntity) FallbackCacheConfig(org.graylog2.plugin.lookup.FallbackCacheConfig) Test(org.junit.Test)

Aggregations

CacheDto (org.graylog2.lookup.dto.CacheDto)15 LookupCacheEntity (org.graylog2.contentpacks.model.entities.LookupCacheEntity)8 Test (org.junit.Test)6 ApiOperation (io.swagger.annotations.ApiOperation)5 Path (javax.ws.rs.Path)5 Entity (org.graylog2.contentpacks.model.entities.Entity)5 NativeEntity (org.graylog2.contentpacks.model.entities.NativeEntity)5 NativeEntityDescriptor (org.graylog2.contentpacks.model.entities.NativeEntityDescriptor)4 LookupTableDto (org.graylog2.lookup.dto.LookupTableDto)4 BadRequestException (javax.ws.rs.BadRequestException)3 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)3 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)3 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)3 FallbackCacheConfig (org.graylog2.plugin.lookup.FallbackCacheConfig)3 LookupCache (org.graylog2.plugin.lookup.LookupCache)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 GET (javax.ws.rs.GET)2 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)2 AuditEvent (org.graylog2.audit.jersey.AuditEvent)2