Skip to content

Commit

Permalink
Refactoring UTs to use ClusterServiceUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
Harsh Garg committed Mar 1, 2024
1 parent 07f2cc1 commit ac76c80
Show file tree
Hide file tree
Showing 34 changed files with 111 additions and 204 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.opensearch.plugin.insights.settings.QueryInsightsSettings;
import org.opensearch.plugins.ActionPlugin;
import org.opensearch.rest.RestHandler;
import org.opensearch.telemetry.metrics.MetricsRegistry;
import org.opensearch.test.ClusterServiceUtils;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.threadpool.ExecutorBuilder;
import org.opensearch.threadpool.ScalingExecutorBuilder;
Expand Down Expand Up @@ -51,8 +51,7 @@ public void setup() {
clusterSettings.registerSetting(QueryInsightsSettings.TOP_N_LATENCY_QUERIES_SIZE);
clusterSettings.registerSetting(QueryInsightsSettings.TOP_N_LATENCY_QUERIES_WINDOW_SIZE);

clusterService = new ClusterService(settings, clusterSettings, threadPool, mock(MetricsRegistry.class));

clusterService = ClusterServiceUtils.createClusterService(settings, clusterSettings, threadPool);
}

public void testGetSettings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.opensearch.search.aggregations.support.ValueType;
import org.opensearch.search.builder.SearchSourceBuilder;
import org.opensearch.telemetry.metrics.MetricsRegistry;
import org.opensearch.test.ClusterServiceUtils;
import org.opensearch.test.OpenSearchTestCase;
import org.junit.Before;

Expand Down Expand Up @@ -58,7 +58,7 @@ public void setup() {
clusterSettings.registerSetting(QueryInsightsSettings.TOP_N_LATENCY_QUERIES_ENABLED);
clusterSettings.registerSetting(QueryInsightsSettings.TOP_N_LATENCY_QUERIES_SIZE);
clusterSettings.registerSetting(QueryInsightsSettings.TOP_N_LATENCY_QUERIES_WINDOW_SIZE);
clusterService = new ClusterService(settings, clusterSettings, null, mock(MetricsRegistry.class));
clusterService = ClusterServiceUtils.createClusterService(settings, clusterSettings, null);
when(queryInsightsService.isCollectionEnabled(MetricType.LATENCY)).thenReturn(true);
when(queryInsightsService.getTopQueriesService(MetricType.LATENCY)).thenReturn(topQueriesService);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.opensearch.plugin.insights.rules.action.top_queries.TopQueriesResponse;
import org.opensearch.plugin.insights.rules.model.MetricType;
import org.opensearch.plugin.insights.settings.QueryInsightsSettings;
import org.opensearch.telemetry.metrics.MetricsRegistry;
import org.opensearch.test.ClusterServiceUtils;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.transport.TransportService;
Expand All @@ -34,7 +34,7 @@ public class TransportTopQueriesActionTests extends OpenSearchTestCase {
private final Settings.Builder settingsBuilder = Settings.builder();
private final Settings settings = settingsBuilder.build();
private final ClusterSettings clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
private final ClusterService clusterService = new ClusterService(settings, clusterSettings, threadPool, mock(MetricsRegistry.class));
private final ClusterService clusterService = ClusterServiceUtils.createClusterService(settings, clusterSettings, threadPool);
private final TransportService transportService = mock(TransportService.class);
private final QueryInsightsService topQueriesByLatencyService = mock(QueryInsightsService.class);
private final ActionFilters actionFilters = mock(ActionFilters.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,9 +633,9 @@ private void callClusterStateAppliers(
for (ClusterStateApplier applier : clusterStateAppliers) {
logger.trace("calling [{}] with change to version [{}]", applier, clusterChangedEvent.state().version());
try (TimingHandle ignored = stopWatch.timing("running applier [" + applier + "]")) {
long applierStartTimeMS = currentTimeInMillis();
long applierStartTimeMS = System.currentTimeMillis();
applier.applyClusterState(clusterChangedEvent);
double applierExecutionTimeMS = (double) Math.max(0, currentTimeInMillis() - applierStartTimeMS);
double applierExecutionTimeMS = (double) Math.max(0, System.currentTimeMillis() - applierStartTimeMS);
clusterStateAppliersHistogram.record(
applierExecutionTimeMS,
Tags.create().addTag(LATENCY_METRIC_OPERATION_TAG_KEY, applier.getClass().getSimpleName())
Expand All @@ -658,9 +658,9 @@ private void callClusterStateListener(
try {
logger.trace("calling [{}] with change to version [{}]", listener, clusterChangedEvent.state().version());
try (TimingHandle ignored = stopWatch.timing("notifying listener [" + listener + "]")) {
long listenerStartTimeMS = currentTimeInMillis();
long listenerStartTimeMS = System.currentTimeMillis();
listener.clusterChanged(clusterChangedEvent);
double listenerExecutionTimeMS = (double) Math.max(0, currentTimeInMillis() - listenerStartTimeMS);
double listenerExecutionTimeMS = (double) Math.max(0, System.currentTimeMillis() - listenerStartTimeMS);
clusterStateListenersHistogram.record(
listenerExecutionTimeMS,
Tags.create().addTag(LATENCY_METRIC_OPERATION_TAG_KEY, listener.getClass().getSimpleName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
import org.opensearch.index.VersionType;
import org.opensearch.indices.SystemIndices;
import org.opensearch.tasks.Task;
import org.opensearch.telemetry.metrics.MetricsRegistry;
import org.opensearch.telemetry.tracing.noop.NoopTracer;
import org.opensearch.test.ClusterServiceUtils;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.test.VersionUtils;
import org.opensearch.threadpool.ThreadPool;
Expand Down Expand Up @@ -154,11 +154,10 @@ private void indicesThatCannotBeCreatedTestCase(
null,
new IndexingPressureService(
Settings.EMPTY,
new ClusterService(
ClusterServiceUtils.createClusterService(
Settings.EMPTY,
new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS),
null,
mock(MetricsRegistry.class)
null
)
),
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@
import org.opensearch.indices.SystemIndices;
import org.opensearch.ingest.IngestService;
import org.opensearch.tasks.Task;
import org.opensearch.telemetry.metrics.NoopMetricsRegistryFactory;
import org.opensearch.telemetry.tracing.noop.NoopTracer;
import org.opensearch.test.ClusterServiceUtils;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.test.VersionUtils;
import org.opensearch.threadpool.ThreadPool;
Expand Down Expand Up @@ -171,11 +171,10 @@ class TestTransportBulkAction extends TransportBulkAction {
),
new IndexingPressureService(
SETTINGS,
new ClusterService(
ClusterServiceUtils.createClusterService(
SETTINGS,
new ClusterSettings(SETTINGS, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS),
null,
new NoopMetricsRegistryFactory().getMetricsRegistry()
null
)
),
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
import org.opensearch.common.unit.TimeValue;
import org.opensearch.index.query.QueryBuilders;
import org.opensearch.search.builder.SearchSourceBuilder;
import org.opensearch.telemetry.metrics.MetricsRegistryFactory;
import org.opensearch.telemetry.metrics.NoopMetricsRegistryFactory;
import org.opensearch.test.ClusterServiceUtils;
import org.opensearch.test.OpenSearchTestCase;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -74,14 +73,12 @@
public class SearchRequestSlowLogTests extends OpenSearchTestCase {
static MockAppender appender;
static Logger logger = LogManager.getLogger(SearchRequestSlowLog.CLUSTER_SEARCH_REQUEST_SLOWLOG_PREFIX + ".SearchRequestSlowLog");
static MetricsRegistryFactory metricsRegistryFactory;

@BeforeClass
public static void init() throws IllegalAccessException {
appender = new MockAppender("trace_appender");
appender.start();
Loggers.addAppender(logger, appender);
metricsRegistryFactory = new NoopMetricsRegistryFactory();
}

@AfterClass
Expand All @@ -94,21 +91,19 @@ public void testMultipleSlowLoggersUseSingleLog4jLogger() {
LoggerContext context = (LoggerContext) LogManager.getContext(false);

SearchPhaseContext searchPhaseContext1 = new MockSearchPhaseContext(1);
ClusterService clusterService1 = new ClusterService(
ClusterService clusterService1 = ClusterServiceUtils.createClusterService(
Settings.EMPTY,
new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS),
null,
metricsRegistryFactory.getMetricsRegistry()
null
);
SearchRequestSlowLog searchRequestSlowLog1 = new SearchRequestSlowLog(clusterService1);
int numberOfLoggersBefore = context.getLoggers().size();

SearchPhaseContext searchPhaseContext2 = new MockSearchPhaseContext(1);
ClusterService clusterService2 = new ClusterService(
ClusterService clusterService2 = ClusterServiceUtils.createClusterService(
Settings.EMPTY,
new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS),
null,
metricsRegistryFactory.getMetricsRegistry()
null
);
SearchRequestOperationsCompositeListenerFactory searchRequestListeners = new SearchRequestOperationsCompositeListenerFactory();
SearchRequestSlowLog searchRequestSlowLog2 = new SearchRequestSlowLog(clusterService2);
Expand All @@ -130,7 +125,7 @@ public void testOnRequestEnd() throws InterruptedException {
settingsBuilder.put(SearchRequestSlowLog.CLUSTER_SEARCH_REQUEST_SLOWLOG_THRESHOLD_TRACE_SETTING.getKey(), "0ms");
Settings settings = settingsBuilder.build();
ClusterSettings clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
ClusterService clusterService = new ClusterService(settings, clusterSettings, null, metricsRegistryFactory.getMetricsRegistry());
ClusterService clusterService = ClusterServiceUtils.createClusterService(settings, clusterSettings, null);
SearchRequestSlowLog searchRequestSlowLog = new SearchRequestSlowLog(clusterService, logger);
final List<SearchRequestOperationsListener> searchListenersList = new ArrayList<>(List.of(searchRequestSlowLog));

Expand Down Expand Up @@ -163,7 +158,7 @@ public void testConcurrentOnRequestEnd() throws InterruptedException {
settingsBuilder.put(SearchRequestSlowLog.CLUSTER_SEARCH_REQUEST_SLOWLOG_THRESHOLD_TRACE_SETTING.getKey(), "-1");
Settings settings = settingsBuilder.build();
ClusterSettings clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
ClusterService clusterService = new ClusterService(settings, clusterSettings, null, metricsRegistryFactory.getMetricsRegistry());
ClusterService clusterService = ClusterServiceUtils.createClusterService(settings, clusterSettings, null);
SearchRequestSlowLog searchRequestSlowLog = new SearchRequestSlowLog(clusterService, logger);
final List<SearchRequestOperationsListener> searchListenersList = new ArrayList<>(List.of(searchRequestSlowLog));

Expand Down Expand Up @@ -327,7 +322,7 @@ public void testLevelSettingWarn() {
settingsBuilder.put(SearchRequestSlowLog.CLUSTER_SEARCH_REQUEST_SLOWLOG_LEVEL.getKey(), level);
Settings settings = settingsBuilder.build();
ClusterSettings clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
ClusterService clusterService = new ClusterService(settings, clusterSettings, null, metricsRegistryFactory.getMetricsRegistry());
ClusterService clusterService = ClusterServiceUtils.createClusterService(settings, clusterSettings, null);
SearchRequestSlowLog searchRequestSlowLog = new SearchRequestSlowLog(clusterService);
assertEquals(level, searchRequestSlowLog.getLevel());
}
Expand All @@ -338,7 +333,7 @@ public void testLevelSettingDebug() {
settingsBuilder.put(SearchRequestSlowLog.CLUSTER_SEARCH_REQUEST_SLOWLOG_LEVEL.getKey(), level);
Settings settings = settingsBuilder.build();
ClusterSettings clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
ClusterService clusterService = new ClusterService(settings, clusterSettings, null, metricsRegistryFactory.getMetricsRegistry());
ClusterService clusterService = ClusterServiceUtils.createClusterService(settings, clusterSettings, null);
SearchRequestSlowLog searchRequestSlowLog = new SearchRequestSlowLog(clusterService);
assertEquals(level, searchRequestSlowLog.getLevel().toString());
}
Expand All @@ -349,7 +344,7 @@ public void testLevelSettingFail() {
settingsBuilder.put(SearchRequestSlowLog.CLUSTER_SEARCH_REQUEST_SLOWLOG_LEVEL.getKey(), level);
Settings settings = settingsBuilder.build();
ClusterSettings clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
ClusterService clusterService = new ClusterService(settings, clusterSettings, null, metricsRegistryFactory.getMetricsRegistry());
ClusterService clusterService = ClusterServiceUtils.createClusterService(settings, clusterSettings, null);

try {
new SearchRequestSlowLog(clusterService);
Expand All @@ -369,7 +364,7 @@ public void testSetThresholds() {
settingsBuilder.put(SearchRequestSlowLog.CLUSTER_SEARCH_REQUEST_SLOWLOG_THRESHOLD_TRACE_SETTING.getKey(), "100ms");
Settings settings = settingsBuilder.build();
ClusterSettings clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
ClusterService clusterService = new ClusterService(settings, clusterSettings, null, metricsRegistryFactory.getMetricsRegistry());
ClusterService clusterService = ClusterServiceUtils.createClusterService(settings, clusterSettings, null);
SearchRequestSlowLog searchRequestSlowLog = new SearchRequestSlowLog(clusterService);
assertEquals(TimeValue.timeValueMillis(400).nanos(), searchRequestSlowLog.getWarnThreshold());
assertEquals(TimeValue.timeValueMillis(300).nanos(), searchRequestSlowLog.getInfoThreshold());
Expand All @@ -386,7 +381,7 @@ public void testSetThresholdsUnits() {
settingsBuilder.put(SearchRequestSlowLog.CLUSTER_SEARCH_REQUEST_SLOWLOG_THRESHOLD_TRACE_SETTING.getKey(), "100nanos");
Settings settings = settingsBuilder.build();
ClusterSettings clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
ClusterService clusterService = new ClusterService(settings, clusterSettings, null, metricsRegistryFactory.getMetricsRegistry());
ClusterService clusterService = ClusterServiceUtils.createClusterService(settings, clusterSettings, null);
SearchRequestSlowLog searchRequestSlowLog = new SearchRequestSlowLog(clusterService);
assertEquals(TimeValue.timeValueSeconds(400).nanos(), searchRequestSlowLog.getWarnThreshold());
assertEquals(TimeValue.timeValueMillis(300).nanos(), searchRequestSlowLog.getInfoThreshold());
Expand All @@ -401,7 +396,7 @@ public void testSetThresholdsDefaults() {
settingsBuilder.put(SearchRequestSlowLog.CLUSTER_SEARCH_REQUEST_SLOWLOG_THRESHOLD_DEBUG_SETTING.getKey(), "200ms");
Settings settings = settingsBuilder.build();
ClusterSettings clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
ClusterService clusterService = new ClusterService(settings, clusterSettings, null, metricsRegistryFactory.getMetricsRegistry());
ClusterService clusterService = ClusterServiceUtils.createClusterService(settings, clusterSettings, null);
SearchRequestSlowLog searchRequestSlowLog = new SearchRequestSlowLog(clusterService);
assertEquals(TimeValue.timeValueMillis(400).nanos(), searchRequestSlowLog.getWarnThreshold());
assertEquals(TimeValue.timeValueMillis(-1).nanos(), searchRequestSlowLog.getInfoThreshold());
Expand All @@ -415,7 +410,7 @@ public void testSetThresholdsError() {
settingsBuilder.put(SearchRequestSlowLog.CLUSTER_SEARCH_REQUEST_SLOWLOG_THRESHOLD_WARN_SETTING.getKey(), "NOT A TIME VALUE");
Settings settings = settingsBuilder.build();
ClusterSettings clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
ClusterService clusterService = new ClusterService(settings, clusterSettings, null, metricsRegistryFactory.getMetricsRegistry());
ClusterService clusterService = ClusterServiceUtils.createClusterService(settings, clusterSettings, null);

try {
new SearchRequestSlowLog(clusterService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.gateway.GatewayAllocator;
import org.opensearch.plugins.ClusterPlugin;
import org.opensearch.telemetry.metrics.NoopMetricsRegistryFactory;
import org.opensearch.test.ClusterServiceUtils;
import org.opensearch.test.gateway.TestGatewayAllocator;

import java.util.Arrays;
Expand All @@ -90,11 +90,10 @@ public class ClusterModuleTests extends ModuleTestCase {
public void setUp() throws Exception {
super.setUp();
threadContext = new ThreadContext(Settings.EMPTY);
clusterService = new ClusterService(
clusterService = ClusterServiceUtils.createClusterService(
Settings.EMPTY,
new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS),
null,
new NoopMetricsRegistryFactory().getMetricsRegistry()
null
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.action.ActionResponse;
import org.opensearch.node.Node;
import org.opensearch.telemetry.metrics.MetricsRegistry;
import org.opensearch.telemetry.metrics.NoopMetricsRegistryFactory;
import org.opensearch.telemetry.metrics.noop.NoopMetricsRegistry;
import org.opensearch.test.ClusterServiceUtils;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.test.client.NoOpClient;
Expand Down Expand Up @@ -84,14 +83,13 @@ public void testScheduling() {
final ClusterSettings clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
final DeterministicTaskQueue deterministicTaskQueue = new DeterministicTaskQueue(settings, random());
final ThreadPool threadPool = deterministicTaskQueue.getThreadPool();
final MetricsRegistry metricsRegistry = new NoopMetricsRegistryFactory().getMetricsRegistry();

final ClusterApplierService clusterApplierService = new ClusterApplierService(
"test",
settings,
clusterSettings,
threadPool,
metricsRegistry
NoopMetricsRegistry.INSTANCE
) {
@Override
protected PrioritizedOpenSearchThreadPoolExecutor createThreadPoolExecutor() {
Expand Down
Loading

0 comments on commit ac76c80

Please sign in to comment.