Skip to content

Commit

Permalink
Adds SystemEntity Annotated Caches (6.0) (#21530)
Browse files Browse the repository at this point in the history
* Add SystemEntity caches and new forceUpdate method to ScopedEntityMongoUtils (#21439)

* Add toBuilder method to CacheDto

---------

Co-authored-by: Dan Torrey <[email protected]>
  • Loading branch information
kingzacko1 and danotorrey authored Feb 6, 2025
1 parent 5104619 commit cd6f342
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -84,6 +82,7 @@ public class LookupTableService extends AbstractIdleService {
private final Map<String, LookupDataAdapter.Factory> adapterFactories;
private final Map<String, LookupDataAdapter.Factory2> adapterFactories2;
private final Map<String, LookupDataAdapter.Factory2> systemAdapterFactories;
private final Map<String, LookupCache.Factory> systemCacheFactories;
private final ScheduledExecutorService scheduler;
private final EventBus eventBus;
private final LookupDataAdapterRefreshService adapterRefreshService;
Expand All @@ -102,13 +101,15 @@ public LookupTableService(LookupTableConfigService configService,
Map<String, LookupDataAdapter.Factory> adapterFactories,
Map<String, LookupDataAdapter.Factory2> adapterFactories2,
@SystemEntity Map<String, LookupDataAdapter.Factory2> systemAdapterFactories,
@SystemEntity Map<String, LookupCache.Factory> systemCacheFactories,
@Named("daemonScheduler") ScheduledExecutorService scheduler,
EventBus eventBus) {
this.configService = configService;
this.cacheFactories = cacheFactories;
this.adapterFactories = adapterFactories;
this.adapterFactories2 = adapterFactories2;
this.systemAdapterFactories = systemAdapterFactories;
this.systemCacheFactories = systemCacheFactories;
this.scheduler = scheduler;
this.eventBus = eventBus;
this.adapterRefreshService = new LookupDataAdapterRefreshService(scheduler, liveTables);
Expand Down Expand Up @@ -451,12 +452,17 @@ private Map<CacheDto, LookupCache> createCaches(Collection<CacheDto> cacheDtos)
private LookupCache createCache(CacheDto dto) {
try {
final LookupCache.Factory<? extends LookupCache> factory = cacheFactories.get(dto.config().type());
if (factory == null) {
final LookupCache.Factory<? extends LookupCache> 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)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Builder> {
Expand Down

0 comments on commit cd6f342

Please sign in to comment.