From cdfa025c275f9b300e5222c99ab0357182632848 Mon Sep 17 00:00:00 2001 From: Zack King <91903901+kingzacko1@users.noreply.github.com> Date: Fri, 31 Jan 2025 08:18:15 -0600 Subject: [PATCH 1/2] Add SystemEntity caches and new forceUpdate method to ScopedEntityMongoUtils (#21439) --- .../org/graylog2/lookup/LookupModule.java | 2 ++ .../graylog2/lookup/LookupTableService.java | 20 ++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/graylog2-server/src/main/java/org/graylog2/lookup/LookupModule.java b/graylog2-server/src/main/java/org/graylog2/lookup/LookupModule.java index c2bbd1b2b483..15f3e99f68cb 100644 --- a/graylog2-server/src/main/java/org/graylog2/lookup/LookupModule.java +++ b/graylog2-server/src/main/java/org/graylog2/lookup/LookupModule.java @@ -27,6 +27,7 @@ import org.graylog2.lookup.caches.NullCache; import org.graylog2.lookup.db.DBLookupTableConfigService; import org.graylog2.plugin.inject.Graylog2Module; +import org.graylog2.plugin.lookup.LookupCache; import org.graylog2.plugin.lookup.LookupDataAdapter; import org.graylog2.system.SystemEntity; import org.graylog2.system.urlwhitelist.UrlWhitelistNotificationService; @@ -48,6 +49,7 @@ protected void configure() { bind(LookupTableConfigService.class).to(DBLookupTableConfigService.class).asEagerSingleton(); // Triggering map binder once, so it does not break injection when no instance is bound. MapBinder.newMapBinder(binder(), String.class, LookupDataAdapter.Factory2.class, SystemEntity.class); + MapBinder.newMapBinder(binder(), String.class, LookupCache.Factory.class, SystemEntity.class); serviceBinder().addBinding().to(LookupTableService.class).asEagerSingleton(); installLookupCache(NullCache.NAME, diff --git a/graylog2-server/src/main/java/org/graylog2/lookup/LookupTableService.java b/graylog2-server/src/main/java/org/graylog2/lookup/LookupTableService.java index 8e1806d49f6d..05b3a21fac59 100644 --- a/graylog2-server/src/main/java/org/graylog2/lookup/LookupTableService.java +++ b/graylog2-server/src/main/java/org/graylog2/lookup/LookupTableService.java @@ -23,6 +23,9 @@ import com.google.common.eventbus.Subscribe; import com.google.common.util.concurrent.AbstractIdleService; import com.google.common.util.concurrent.Service; +import jakarta.inject.Inject; +import jakarta.inject.Named; +import jakarta.inject.Singleton; import org.graylog2.lookup.dto.CacheDto; import org.graylog2.lookup.dto.DataAdapterDto; import org.graylog2.lookup.dto.LookupTableDto; @@ -43,11 +46,6 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; - -import jakarta.inject.Inject; -import jakarta.inject.Named; -import jakarta.inject.Singleton; - import java.util.Collection; import java.util.Collections; import java.util.List; @@ -84,6 +82,7 @@ public class LookupTableService extends AbstractIdleService { private final Map adapterFactories; private final Map adapterFactories2; private final Map systemAdapterFactories; + private final Map systemCacheFactories; private final ScheduledExecutorService scheduler; private final EventBus eventBus; private final LookupDataAdapterRefreshService adapterRefreshService; @@ -102,6 +101,7 @@ public LookupTableService(LookupTableConfigService configService, Map adapterFactories, Map adapterFactories2, @SystemEntity Map systemAdapterFactories, + @SystemEntity Map systemCacheFactories, @Named("daemonScheduler") ScheduledExecutorService scheduler, EventBus eventBus) { this.configService = configService; @@ -109,6 +109,7 @@ public LookupTableService(LookupTableConfigService configService, this.adapterFactories = adapterFactories; this.adapterFactories2 = adapterFactories2; this.systemAdapterFactories = systemAdapterFactories; + this.systemCacheFactories = systemCacheFactories; this.scheduler = scheduler; this.eventBus = eventBus; this.adapterRefreshService = new LookupDataAdapterRefreshService(scheduler, liveTables); @@ -451,12 +452,17 @@ private Map createCaches(Collection cacheDtos) private LookupCache createCache(CacheDto dto) { try { final LookupCache.Factory factory = cacheFactories.get(dto.config().type()); - if (factory == null) { + final LookupCache.Factory systemFactory = systemCacheFactories.get(dto.config().type()); + final LookupCache cache; + if (factory != null) { + cache = factory.create(dto.id(), dto.name(), dto.config()); + } else if (systemFactory != null) { + cache = systemFactory.create(dto.id(), dto.name(), dto.config()); + } else { 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)), From 10746218f459d0f5a36dd501d85263b625797bc2 Mon Sep 17 00:00:00 2001 From: Zack King Date: Mon, 3 Feb 2025 12:40:02 -0600 Subject: [PATCH 2/2] Add toBuilder method to CacheDto --- .../src/main/java/org/graylog2/lookup/dto/CacheDto.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/graylog2-server/src/main/java/org/graylog2/lookup/dto/CacheDto.java b/graylog2-server/src/main/java/org/graylog2/lookup/dto/CacheDto.java index 4d2e2554a3d4..f6c8a6275140 100644 --- a/graylog2-server/src/main/java/org/graylog2/lookup/dto/CacheDto.java +++ b/graylog2-server/src/main/java/org/graylog2/lookup/dto/CacheDto.java @@ -55,6 +55,8 @@ public static Builder builder() { return new AutoValue_CacheDto.Builder(); } + public abstract Builder toBuilder(); + @JsonAutoDetect @AutoValue.Builder public abstract static class Builder extends ScopedEntity.AbstractBuilder {