Skip to content

Commit

Permalink
Change plugin instantiation from createComponents to Guice
Browse files Browse the repository at this point in the history
Signed-off-by: David Zane <[email protected]>
  • Loading branch information
dzane17 committed Oct 1, 2024
1 parent e4c6b8f commit 90c8e74
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
import java.util.List;
import java.util.function.Supplier;
import org.opensearch.action.ActionRequest;
import org.opensearch.client.Client;
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
import org.opensearch.cluster.node.DiscoveryNodes;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.lifecycle.LifecycleComponent;
import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.IndexScopedSettings;
import org.opensearch.common.settings.Setting;
Expand All @@ -24,12 +23,7 @@
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.util.concurrent.OpenSearchExecutors;
import org.opensearch.core.action.ActionResponse;
import org.opensearch.core.common.io.stream.NamedWriteableRegistry;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.env.Environment;
import org.opensearch.env.NodeEnvironment;
import org.opensearch.plugin.insights.core.listener.QueryInsightsListener;
import org.opensearch.plugin.insights.core.metrics.OperationalMetricsCounter;
import org.opensearch.plugin.insights.core.service.QueryInsightsService;
import org.opensearch.plugin.insights.rules.action.health_stats.HealthStatsAction;
import org.opensearch.plugin.insights.rules.action.top_queries.TopQueriesAction;
Expand All @@ -42,16 +36,10 @@
import org.opensearch.plugins.ActionPlugin;
import org.opensearch.plugins.Plugin;
import org.opensearch.plugins.TelemetryAwarePlugin;
import org.opensearch.repositories.RepositoriesService;
import org.opensearch.rest.RestController;
import org.opensearch.rest.RestHandler;
import org.opensearch.script.ScriptService;
import org.opensearch.telemetry.metrics.MetricsRegistry;
import org.opensearch.telemetry.tracing.Tracer;
import org.opensearch.threadpool.ExecutorBuilder;
import org.opensearch.threadpool.ScalingExecutorBuilder;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.watcher.ResourceWatcherService;

/**
* Plugin class for Query Insights.
Expand All @@ -63,32 +51,8 @@ public class QueryInsightsPlugin extends Plugin implements ActionPlugin, Telemet
public QueryInsightsPlugin() {}

@Override
public Collection<Object> createComponents(
final Client client,
final ClusterService clusterService,
final ThreadPool threadPool,
final ResourceWatcherService resourceWatcherService,
final ScriptService scriptService,
final NamedXContentRegistry xContentRegistry,
final Environment environment,
final NodeEnvironment nodeEnvironment,
final NamedWriteableRegistry namedWriteableRegistry,
final IndexNameExpressionResolver indexNameExpressionResolver,
final Supplier<RepositoriesService> repositoriesServiceSupplier,
final Tracer tracer,
final MetricsRegistry metricsRegistry
) {
// initialize operational metrics counters
OperationalMetricsCounter.initialize(clusterService.getClusterName().toString(), metricsRegistry);
// create top n queries service
final QueryInsightsService queryInsightsService = new QueryInsightsService(
clusterService.getClusterSettings(),
threadPool,
client,
metricsRegistry,
xContentRegistry
);
return List.of(queryInsightsService, new QueryInsightsListener(clusterService, queryInsightsService, false));
public Collection<Class<? extends LifecycleComponent>> getGuiceServiceClasses() {
return List.of(QueryInsightsService.class, QueryInsightsListener.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@
import org.opensearch.action.search.SearchTask;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.inject.Inject;
import org.opensearch.common.lifecycle.Lifecycle;
import org.opensearch.common.lifecycle.LifecycleComponent;
import org.opensearch.common.lifecycle.LifecycleListener;
import org.opensearch.core.tasks.resourcetracker.TaskResourceInfo;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.indices.IndicesService;
import org.opensearch.plugin.insights.core.metrics.OperationalMetric;
import org.opensearch.plugin.insights.core.metrics.OperationalMetricsCounter;
import org.opensearch.plugin.insights.core.service.QueryInsightsService;
Expand All @@ -47,7 +51,7 @@
* It forwards query-related data to the appropriate query insights stores,
* either for each request or for each phase.
*/
public final class QueryInsightsListener extends SearchRequestOperationsListener {
public final class QueryInsightsListener extends SearchRequestOperationsListener implements LifecycleComponent {
private static final ToXContent.Params FORMAT_PARAMS = new ToXContent.MapParams(Collections.singletonMap("pretty", "false"));

private static final Logger log = LogManager.getLogger(QueryInsightsListener.class);
Expand All @@ -60,10 +64,15 @@ public final class QueryInsightsListener extends SearchRequestOperationsListener
*
* @param clusterService The Node's cluster service.
* @param queryInsightsService The topQueriesByLatencyService associated with this listener
* @param indicesService The domain's indices service
*/
@Inject
public QueryInsightsListener(final ClusterService clusterService, final QueryInsightsService queryInsightsService) {
this(clusterService, queryInsightsService, false);
public QueryInsightsListener(
final ClusterService clusterService,
final QueryInsightsService queryInsightsService,
final IndicesService indicesService
) {
this(clusterService, queryInsightsService, false, indicesService);
}

/**
Expand All @@ -72,11 +81,13 @@ public QueryInsightsListener(final ClusterService clusterService, final QueryIns
* @param clusterService The Node's cluster service.
* @param queryInsightsService The topQueriesByLatencyService associated with this listener
* @param initiallyEnabled Is the listener initially enabled/disabled
* @param indicesService The domain's indices service
*/
public QueryInsightsListener(
final ClusterService clusterService,
final QueryInsightsService queryInsightsService,
boolean initiallyEnabled
boolean initiallyEnabled,
final IndicesService indicesService
) {
super(initiallyEnabled);
this.clusterService = clusterService;
Expand Down Expand Up @@ -268,4 +279,33 @@ private void constructSearchQueryRecord(final SearchPhaseContext context, final
}
}

@Override
public Lifecycle.State lifecycleState() {
return null;
}

@Override
public void addLifecycleListener(LifecycleListener lifecycleListener) {

}

@Override
public void removeLifecycleListener(LifecycleListener lifecycleListener) {

}

@Override
public void start() {

}

@Override
public void stop() {

}

@Override
public void close() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.client.Client;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.inject.Inject;
import org.opensearch.common.lifecycle.AbstractLifecycleComponent;
import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.core.xcontent.NamedXContentRegistry;
Expand Down Expand Up @@ -102,15 +102,15 @@ public class QueryInsightsService extends AbstractLifecycleComponent {
/**
* Constructor of the QueryInsightsService
*
* @param clusterSettings OpenSearch cluster level settings
* @param clusterService OpenSearch cluster service
* @param threadPool The OpenSearch thread pool to run async tasks
* @param client OS client
* @param metricsRegistry Opentelemetry Metrics registry
* @param namedXContentRegistry NamedXContentRegistry for parsing purposes
*/
@Inject
public QueryInsightsService(
final ClusterSettings clusterSettings,
final ClusterService clusterService,
final ThreadPool threadPool,
final Client client,
final MetricsRegistry metricsRegistry,
Expand All @@ -132,16 +132,20 @@ public QueryInsightsService(
);
}
for (MetricType type : MetricType.allMetricTypes()) {
clusterSettings.addSettingsUpdateConsumer(
getExporterSettings(type),
(settings -> setExporterAndReader(type, settings)),
(settings -> validateExporterAndReaderConfig(type, settings))
);
clusterService.getClusterSettings()
.addSettingsUpdateConsumer(
getExporterSettings(type),
(settings -> setExporterAndReader(type, settings)),
(settings -> validateExporterAndReaderConfig(type, settings))
);
}

this.searchQueryCategorizer = SearchQueryCategorizer.getInstance(metricsRegistry);
this.enableSearchQueryMetricsFeature(false);
this.groupingType = DEFAULT_GROUPING_TYPE;

// initialize operational metrics counters
OperationalMetricsCounter.initialize(clusterService.getClusterName().toString(), metricsRegistry);
}

/**
Expand Down

0 comments on commit 90c8e74

Please sign in to comment.