-
Notifications
You must be signed in to change notification settings - Fork 13
caching-resolvers: add cache reloader and reloading service #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
d462da2
e6e50c7
69c0813
abe84de
cc0c8e4
c36346d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,13 @@ | |
import co.elastic.logstash.api.FilterMatchListener; | ||
import co.elastic.logstash.filters.elasticintegration.ingest.SetSecurityUserProcessor; | ||
import co.elastic.logstash.filters.elasticintegration.ingest.SingleProcessorIngestPlugin; | ||
import co.elastic.logstash.filters.elasticintegration.resolver.CacheReloadService; | ||
import co.elastic.logstash.filters.elasticintegration.resolver.SimpleResolverCache; | ||
import co.elastic.logstash.filters.elasticintegration.resolver.ResolverCache; | ||
import co.elastic.logstash.filters.elasticintegration.util.PluginContext; | ||
import com.google.common.util.concurrent.AbstractScheduledService; | ||
import com.google.common.util.concurrent.Service; | ||
import com.google.common.util.concurrent.ServiceManager; | ||
import org.elasticsearch.client.RestClient; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.core.IOUtils; | ||
|
@@ -34,20 +39,19 @@ | |
import org.elasticsearch.threadpool.ThreadPool; | ||
|
||
import java.io.Closeable; | ||
import java.nio.file.Path; | ||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Consumer; | ||
import java.util.function.Supplier; | ||
|
||
import static com.google.common.util.concurrent.AbstractScheduledService.Scheduler.newFixedRateSchedule; | ||
|
||
@SuppressWarnings("UnusedReturnValue") | ||
public class EventProcessorBuilder { | ||
|
||
|
@@ -59,9 +63,10 @@ public static EventProcessorBuilder fromElasticsearch(final RestClient elasticse | |
final EventProcessorBuilder builder = new EventProcessorBuilder(); | ||
|
||
builder.setEventPipelineNameResolver(new DatastreamEventToPipelineNameResolver(elasticsearchRestClient, new SimpleResolverCache<>("datastream-to-pipeline", | ||
new SimpleResolverCache.Configuration(Duration.ofMinutes(60), Duration.ofSeconds(60))))); | ||
new SimpleResolverCache.Configuration(Duration.ofHours(24), Duration.ofHours(24))))); | ||
|
||
builder.setPipelineConfigurationResolver(new ElasticsearchPipelineConfigurationResolver(elasticsearchRestClient)); | ||
builder.setPipelineResolverCacheConfig(Duration.ofMinutes(60), Duration.ofSeconds(60)); | ||
builder.setPipelineResolverCacheConfig(Duration.ofHours(24), Duration.ofHours(24)); | ||
yaauie marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return builder; | ||
} | ||
|
||
|
@@ -126,21 +131,17 @@ public synchronized EventProcessorBuilder addProcessorsFromPlugin(Supplier<Inges | |
return this; | ||
} | ||
|
||
EventProcessor build(final String nodeName) { | ||
final Settings defaultSettings = Settings.builder() | ||
synchronized EventProcessor build(final PluginContext pluginContext) { | ||
Objects.requireNonNull(this.pipelineConfigurationResolver, "pipeline configuration resolver is REQUIRED"); | ||
Objects.requireNonNull(this.eventToPipelineNameResolver, "event to pipeline name resolver is REQUIRED"); | ||
|
||
final Settings settings = Settings.builder() | ||
.put("path.home", "/") | ||
.put("node.name", nodeName) | ||
.put("node.name", "logstash.filter.elastic_integration." + pluginContext.pluginId()) | ||
.put("ingest.grok.watchdog.interval", "1s") | ||
.put("ingest.grok.watchdog.max_execution_time", "1s") | ||
.build(); | ||
|
||
return build(defaultSettings); | ||
} | ||
|
||
synchronized EventProcessor build(final Settings settings) { | ||
Objects.requireNonNull(this.pipelineConfigurationResolver, "pipeline configuration resolver is REQUIRED"); | ||
Objects.requireNonNull(this.eventToPipelineNameResolver, "event to pipeline name resolver is REQUIRED"); | ||
|
||
final List<Closeable> resourcesToClose = new ArrayList<>(); | ||
|
||
try { | ||
|
@@ -175,12 +176,27 @@ synchronized EventProcessor build(final Settings settings) { | |
final ResolverCache<String, IngestPipeline> ingestPipelineCache = Optional.ofNullable(pipelineResolverCacheSupplier) | ||
.orElse(defaultCacheSupplier("ingest-pipeline")) | ||
.get(); | ||
|
||
final SimpleCachingIngestPipelineResolver cachingInternalPipelineResolver = | ||
new SimpleIngestPipelineResolver(this.pipelineConfigurationResolver, ingestPipelineFactory).withCache(ingestPipelineCache); | ||
|
||
final FilterMatchListener filterMatchListener = Objects.requireNonNullElse(this.filterMatchListener, (event) -> {}); | ||
|
||
// start reload services for our resolvers | ||
final ArrayList<Service> services = new ArrayList<>(); | ||
eventToPipelineNameResolver.innerCacheReloader().ifPresent(cacheReloader -> { | ||
final AbstractScheduledService.Scheduler pipelineNameReloadSchedule = newFixedRateSchedule(Duration.ofSeconds(60), Duration.ofSeconds(60)); | ||
|
||
services.add(CacheReloadService.newManaged(pluginContext, cacheReloader, pipelineNameReloadSchedule)); | ||
}); | ||
final AbstractScheduledService.Scheduler pipelineDefinitionReloadSchedule = newFixedRateSchedule(Duration.ofSeconds(60), Duration.ofSeconds(60)); | ||
services.add(CacheReloadService.newManaged(pluginContext, cachingInternalPipelineResolver.getReloader(), pipelineDefinitionReloadSchedule)); | ||
|
||
final ServiceManager serviceManager = new ServiceManager(services); | ||
serviceManager.startAsync(); | ||
resourcesToClose.add(() -> { | ||
serviceManager.stopAsync(); | ||
serviceManager.awaitStopped(); | ||
}); | ||
|
||
return new EventProcessor(filterMatchListener, cachingInternalPipelineResolver, eventToPipelineNameResolver, resourcesToClose); | ||
} catch (Exception e) { | ||
IOUtils.closeWhileHandlingException(resourcesToClose); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. | ||
* under one or more contributor license agreements. Licensed under the | ||
* Elastic License 2.0; you may not use this file except in compliance | ||
* with the Elastic License 2.0. | ||
*/ | ||
package co.elastic.logstash.filters.elasticintegration.resolver; | ||
|
||
import co.elastic.logstash.filters.elasticintegration.util.PluginContext; | ||
import com.google.common.util.concurrent.AbstractScheduledService; | ||
import com.google.common.util.concurrent.MoreExecutors; | ||
import com.google.common.util.concurrent.Service; | ||
|
||
import java.util.concurrent.ScheduledExecutorService; | ||
|
||
/** | ||
* A {@link CacheReloadService} is a service for scheduled reloading of resolver caches via {@link CacheReloader}. | ||
*/ | ||
public class CacheReloadService extends AbstractScheduledService { | ||
|
||
/** | ||
* Creates a new cache reload service, wholly managing the lifecycle of the internal | ||
* scheduled executor service to ensure that it is shut down when this service is terminated | ||
* or transitions into a failed state. | ||
* | ||
* @param pluginContext | ||
* @param reloader | ||
* @param scheduler | ||
* @return | ||
*/ | ||
public static CacheReloadService newManaged(final PluginContext pluginContext, | ||
final CacheReloader reloader, | ||
final Scheduler scheduler) { | ||
final String threadPurpose = String.format("cache-reloader(%s)", reloader.type()); | ||
final ScheduledExecutorService executor = pluginContext.newSingleThreadScheduledExecutor(threadPurpose); | ||
|
||
final CacheReloadService cacheReloadService = new CacheReloadService(reloader, executor, scheduler); | ||
cacheReloadService.addListener(new Service.Listener() { | ||
public void terminated(Service.State from) { | ||
executor.shutdown(); | ||
} | ||
|
||
public void failed(Service.State from, Throwable failure) { | ||
executor.shutdown(); | ||
} | ||
}, MoreExecutors.directExecutor()); | ||
|
||
return cacheReloadService; | ||
} | ||
|
||
final CacheReloader reloader; | ||
final ScheduledExecutorService executor; | ||
|
||
final Scheduler scheduler; | ||
|
||
private CacheReloadService(CacheReloader reloader, | ||
ScheduledExecutorService executor, | ||
Scheduler scheduler) { | ||
this.reloader = reloader; | ||
this.executor = executor; | ||
this.scheduler = scheduler; | ||
} | ||
|
||
@Override | ||
protected void runOneIteration() throws Exception { | ||
reloader.reloadOnce(); | ||
} | ||
|
||
@Override | ||
protected Scheduler scheduler() { | ||
return scheduler; | ||
} | ||
|
||
@Override | ||
protected ScheduledExecutorService executor() { | ||
return executor; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. | ||
* under one or more contributor license agreements. Licensed under the | ||
* Elastic License 2.0; you may not use this file except in compliance | ||
* with the Elastic License 2.0. | ||
*/ | ||
package co.elastic.logstash.filters.elasticintegration.resolver; | ||
|
||
public interface CacheReloader { | ||
String type(); | ||
|
||
void reloadOnce(); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.