Skip to content

Commit

Permalink
refactor the code to make it more maintainable
Browse files Browse the repository at this point in the history
Signed-off-by: Chenyang Ji <cyji@amazon.com>
  • Loading branch information
ansjcy committed Jun 4, 2024
1 parent 64c55f7 commit 14294b1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,9 @@
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.TOP_N_CPU_QUERIES_ENABLED;
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.TOP_N_CPU_QUERIES_SIZE;
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.TOP_N_CPU_QUERIES_WINDOW_SIZE;
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.TOP_N_LATENCY_QUERIES_ENABLED;
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.TOP_N_LATENCY_QUERIES_SIZE;
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.TOP_N_LATENCY_QUERIES_WINDOW_SIZE;
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.TOP_N_MEMORY_QUERIES_ENABLED;
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.TOP_N_MEMORY_QUERIES_SIZE;
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.TOP_N_MEMORY_QUERIES_WINDOW_SIZE;
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.getTopNEnabledSetting;
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.getTopNSizeSetting;
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.getTopNWindowSizeSetting;

/**
* The listener for query insights services.
Expand All @@ -66,63 +60,26 @@ public final class QueryInsightsListener extends SearchRequestOperationsListener
public QueryInsightsListener(final ClusterService clusterService, final QueryInsightsService queryInsightsService) {
this.clusterService = clusterService;
this.queryInsightsService = queryInsightsService;
clusterService.getClusterSettings()
.addSettingsUpdateConsumer(TOP_N_LATENCY_QUERIES_ENABLED, v -> this.setEnableTopQueries(MetricType.LATENCY, v));
clusterService.getClusterSettings()
.addSettingsUpdateConsumer(
TOP_N_LATENCY_QUERIES_SIZE,
v -> this.queryInsightsService.getTopQueriesService(MetricType.LATENCY).setTopNSize(v),
v -> this.queryInsightsService.getTopQueriesService(MetricType.LATENCY).validateTopNSize(v)
);
clusterService.getClusterSettings()
.addSettingsUpdateConsumer(
TOP_N_LATENCY_QUERIES_WINDOW_SIZE,
v -> this.queryInsightsService.getTopQueriesService(MetricType.LATENCY).setWindowSize(v),
v -> this.queryInsightsService.getTopQueriesService(MetricType.LATENCY).validateWindowSize(v)
);
clusterService.getClusterSettings()
.addSettingsUpdateConsumer(TOP_N_CPU_QUERIES_ENABLED, v -> this.setEnableTopQueries(MetricType.CPU, v));
clusterService.getClusterSettings()
.addSettingsUpdateConsumer(
TOP_N_CPU_QUERIES_SIZE,
v -> this.queryInsightsService.getTopQueriesService(MetricType.CPU).setTopNSize(v),
v -> this.queryInsightsService.getTopQueriesService(MetricType.CPU).validateTopNSize(v)
);
clusterService.getClusterSettings()
.addSettingsUpdateConsumer(
TOP_N_CPU_QUERIES_WINDOW_SIZE,
v -> this.queryInsightsService.getTopQueriesService(MetricType.CPU).setWindowSize(v),
v -> this.queryInsightsService.getTopQueriesService(MetricType.CPU).validateWindowSize(v)
);
clusterService.getClusterSettings()
.addSettingsUpdateConsumer(TOP_N_MEMORY_QUERIES_ENABLED, v -> this.setEnableTopQueries(MetricType.MEMORY, v));
clusterService.getClusterSettings()
.addSettingsUpdateConsumer(
TOP_N_MEMORY_QUERIES_SIZE,
v -> this.queryInsightsService.getTopQueriesService(MetricType.MEMORY).setTopNSize(v),
v -> this.queryInsightsService.getTopQueriesService(MetricType.MEMORY).validateTopNSize(v)
);
clusterService.getClusterSettings()
.addSettingsUpdateConsumer(
TOP_N_MEMORY_QUERIES_WINDOW_SIZE,
v -> this.queryInsightsService.getTopQueriesService(MetricType.MEMORY).setWindowSize(v),
v -> this.queryInsightsService.getTopQueriesService(MetricType.MEMORY).validateWindowSize(v)
);
this.setEnableTopQueries(MetricType.LATENCY, clusterService.getClusterSettings().get(TOP_N_LATENCY_QUERIES_ENABLED));
this.queryInsightsService.getTopQueriesService(MetricType.LATENCY)
.setTopNSize(clusterService.getClusterSettings().get(TOP_N_LATENCY_QUERIES_SIZE));
this.queryInsightsService.getTopQueriesService(MetricType.LATENCY)
.setWindowSize(clusterService.getClusterSettings().get(TOP_N_LATENCY_QUERIES_WINDOW_SIZE));
this.setEnableTopQueries(MetricType.CPU, clusterService.getClusterSettings().get(TOP_N_CPU_QUERIES_ENABLED));
this.queryInsightsService.getTopQueriesService(MetricType.CPU)
.setTopNSize(clusterService.getClusterSettings().get(TOP_N_CPU_QUERIES_SIZE));
this.queryInsightsService.getTopQueriesService(MetricType.CPU)
.setWindowSize(clusterService.getClusterSettings().get(TOP_N_CPU_QUERIES_WINDOW_SIZE));
this.setEnableTopQueries(MetricType.MEMORY, clusterService.getClusterSettings().get(TOP_N_MEMORY_QUERIES_ENABLED));
this.queryInsightsService.getTopQueriesService(MetricType.MEMORY)
.setTopNSize(clusterService.getClusterSettings().get(TOP_N_MEMORY_QUERIES_SIZE));
this.queryInsightsService.getTopQueriesService(MetricType.MEMORY)
.setWindowSize(clusterService.getClusterSettings().get(TOP_N_MEMORY_QUERIES_WINDOW_SIZE));
for (MetricType type : MetricType.allMetricTypes()) {
clusterService.getClusterSettings()
.addSettingsUpdateConsumer(getTopNEnabledSetting(type), v -> this.setEnableTopQueries(type, v));
clusterService.getClusterSettings()
.addSettingsUpdateConsumer(
getTopNSizeSetting(type),
v -> this.queryInsightsService.setTopNSize(type, v),
v -> this.queryInsightsService.validateTopNSize(type, v)
);
clusterService.getClusterSettings()
.addSettingsUpdateConsumer(
getTopNWindowSizeSetting(type),
v -> this.queryInsightsService.setWindowSize(type, v),
v -> this.queryInsightsService.validateWindowSize(type, v)
);

this.setEnableTopQueries(type, clusterService.getClusterSettings().get(getTopNEnabledSetting(type)));
this.queryInsightsService.setTopNSize(type, clusterService.getClusterSettings().get(getTopNSizeSetting(type)));
this.queryInsightsService.setWindowSize(type, clusterService.getClusterSettings().get(getTopNWindowSizeSetting(type)));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.opensearch.common.inject.Inject;
import org.opensearch.common.lifecycle.AbstractLifecycleComponent;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.plugin.insights.rules.model.MetricType;
import org.opensearch.plugin.insights.rules.model.SearchQueryRecord;
import org.opensearch.plugin.insights.settings.QueryInsightsSettings;
Expand Down Expand Up @@ -157,6 +158,28 @@ public boolean isEnabled() {
return false;
}

public void validateWindowSize(MetricType type, TimeValue windowSize) {
if (topQueriesServices.containsKey(type)) {
topQueriesServices.get(type).validateWindowSize(windowSize);
}
}
public void setWindowSize(MetricType type, TimeValue windowSize) {
if (topQueriesServices.containsKey(type)) {
topQueriesServices.get(type).setWindowSize(windowSize);
}
}

public void validateTopNSize(MetricType type, int topNSize) {
if (topQueriesServices.containsKey(type)) {
topQueriesServices.get(type).validateTopNSize(topNSize);
}
}
public void setTopNSize(MetricType type, int topNSize) {
if (topQueriesServices.containsKey(type)) {
topQueriesServices.get(type).setTopNSize(topNSize);
}
}

@Override
protected void doStart() {
if (isEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.opensearch.common.settings.Setting;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.plugin.insights.rules.model.MetricType;

import java.util.Arrays;
import java.util.HashSet;
Expand Down Expand Up @@ -173,6 +174,53 @@ public class QueryInsightsSettings {
Setting.Property.Dynamic
);

/**
* Get the enabled setting based on type
* @param type MetricType
* @return enabled setting
*/
public static Setting<Boolean> getTopNEnabledSetting(MetricType type) {
switch (type) {
case CPU:
return TOP_N_CPU_QUERIES_ENABLED;
case MEMORY:
return TOP_N_MEMORY_QUERIES_ENABLED;
default:
return TOP_N_LATENCY_QUERIES_ENABLED;
}
}

/**
* Get the top n size setting based on type
* @param type MetricType
* @return top n size setting
*/
public static Setting<Integer> getTopNSizeSetting(MetricType type) {
switch (type) {
case CPU:
return TOP_N_CPU_QUERIES_SIZE;
case MEMORY:
return TOP_N_MEMORY_QUERIES_SIZE;
default:
return TOP_N_LATENCY_QUERIES_SIZE;
}
}

/**
* Get the window size setting based on type
* @param type MetricType
* @return top n queries window size setting
*/
public static Setting<TimeValue> getTopNWindowSizeSetting(MetricType type) {
switch (type) {
case CPU:
return TOP_N_CPU_QUERIES_WINDOW_SIZE;
case MEMORY:
return TOP_N_MEMORY_QUERIES_WINDOW_SIZE;
default:
return TOP_N_LATENCY_QUERIES_WINDOW_SIZE;
}
}
/**
* Default constructor
*/
Expand Down

0 comments on commit 14294b1

Please sign in to comment.