From ab80b7fb1f81f40c08c9e06610c066a36616f392 Mon Sep 17 00:00:00 2001 From: Sumit Bansal Date: Wed, 25 Sep 2024 21:28:37 +0530 Subject: [PATCH] Refactor code to handle review comments and introduce new exception Signed-off-by: Sumit Bansal --- .../org/opensearch/action/ActionModule.java | 12 +- .../shards/TransportCatShardsAction.java | 37 ++-- .../common/settings/ClusterSettings.java | 9 +- .../opensearch/rest/RequestLimitSettings.java | 144 --------------- .../rest/ResponseLimitBreachedException.java | 36 ++++ .../rest/ResponseLimitSettings.java | 172 ++++++++++++++++++ .../rest/action/cat/AbstractCatAction.java | 4 +- .../rest/action/cat/RestIndicesAction.java | 33 ++-- .../rest/action/cat/RestSegmentsAction.java | 30 +-- .../RenamedTimeoutRequestParameterTests.java | 10 +- ...s.java => ResponseLimitSettingsTests.java} | 138 +++++++++----- .../action/cat/RestIndicesActionTests.java | 6 +- 12 files changed, 388 insertions(+), 243 deletions(-) delete mode 100644 server/src/main/java/org/opensearch/rest/RequestLimitSettings.java create mode 100644 server/src/main/java/org/opensearch/rest/ResponseLimitBreachedException.java create mode 100644 server/src/main/java/org/opensearch/rest/ResponseLimitSettings.java rename server/src/test/java/org/opensearch/rest/{RequestLimitSettingsTests.java => ResponseLimitSettingsTests.java} (54%) diff --git a/server/src/main/java/org/opensearch/action/ActionModule.java b/server/src/main/java/org/opensearch/action/ActionModule.java index af0d8f1cfd5be..b07de05fbe3c4 100644 --- a/server/src/main/java/org/opensearch/action/ActionModule.java +++ b/server/src/main/java/org/opensearch/action/ActionModule.java @@ -325,7 +325,7 @@ import org.opensearch.plugins.ActionPlugin; import org.opensearch.plugins.ActionPlugin.ActionHandler; import org.opensearch.rest.NamedRoute; -import org.opensearch.rest.RequestLimitSettings; +import org.opensearch.rest.ResponseLimitSettings; import org.opensearch.rest.RestController; import org.opensearch.rest.RestHandler; import org.opensearch.rest.RestHeaderDefinition; @@ -529,7 +529,7 @@ public class ActionModule extends AbstractModule { private final RequestValidators indicesAliasesRequestRequestValidators; private final ThreadPool threadPool; private final ExtensionsManager extensionsManager; - private final RequestLimitSettings requestLimitSettings; + private final ResponseLimitSettings responseLimitSettings; public ActionModule( Settings settings, @@ -582,7 +582,7 @@ public ActionModule( ); restController = new RestController(headers, restWrapper, nodeClient, circuitBreakerService, usageService); - requestLimitSettings = new RequestLimitSettings(clusterSettings, settings); + responseLimitSettings = new ResponseLimitSettings(clusterSettings, settings); } public Map> getActions() { @@ -963,8 +963,8 @@ public void initRestHandlers(Supplier nodesInCluster) { registerHandler.accept(new RestClusterManagerAction()); registerHandler.accept(new RestNodesAction()); registerHandler.accept(new RestTasksAction(nodesInCluster)); - registerHandler.accept(new RestIndicesAction(requestLimitSettings)); - registerHandler.accept(new RestSegmentsAction(requestLimitSettings)); + registerHandler.accept(new RestIndicesAction(responseLimitSettings)); + registerHandler.accept(new RestSegmentsAction(responseLimitSettings)); // Fully qualified to prevent interference with rest.action.count.RestCountAction registerHandler.accept(new org.opensearch.rest.action.cat.RestCountAction()); // Fully qualified to prevent interference with rest.action.indices.RestRecoveryAction @@ -1052,7 +1052,7 @@ protected void configure() { // register dynamic ActionType -> transportAction Map used by NodeClient bind(DynamicActionRegistry.class).toInstance(dynamicActionRegistry); - bind(RequestLimitSettings.class).toInstance(requestLimitSettings); + bind(ResponseLimitSettings.class).toInstance(responseLimitSettings); } public ActionFilters getActionFilters() { diff --git a/server/src/main/java/org/opensearch/action/admin/cluster/shards/TransportCatShardsAction.java b/server/src/main/java/org/opensearch/action/admin/cluster/shards/TransportCatShardsAction.java index 7e21195e019e0..50ac9a67cddea 100644 --- a/server/src/main/java/org/opensearch/action/admin/cluster/shards/TransportCatShardsAction.java +++ b/server/src/main/java/org/opensearch/action/admin/cluster/shards/TransportCatShardsAction.java @@ -19,14 +19,15 @@ import org.opensearch.common.inject.Inject; import org.opensearch.core.action.ActionListener; import org.opensearch.core.action.NotifyOnceListener; -import org.opensearch.core.common.breaker.CircuitBreaker; -import org.opensearch.core.common.breaker.CircuitBreakingException; -import org.opensearch.rest.RequestLimitSettings; +import org.opensearch.rest.ResponseLimitBreachedException; +import org.opensearch.rest.ResponseLimitSettings; import org.opensearch.tasks.CancellableTask; import org.opensearch.tasks.Task; import org.opensearch.transport.TransportService; -import static org.opensearch.rest.RequestLimitSettings.BlockAction.CAT_SHARDS; +import java.util.Objects; + +import static org.opensearch.rest.ResponseLimitSettings.LimitEntity.SHARDS; /** * Perform cat shards action @@ -36,18 +37,18 @@ public class TransportCatShardsAction extends HandledTransportAction { private final NodeClient client; - private final RequestLimitSettings requestLimitSettings; + private final ResponseLimitSettings responseLimitSettings; @Inject public TransportCatShardsAction( NodeClient client, TransportService transportService, ActionFilters actionFilters, - RequestLimitSettings requestLimitSettings + ResponseLimitSettings responseLimitSettings ) { super(CatShardsAction.NAME, transportService, actionFilters, CatShardsRequest::new); this.client = client; - this.requestLimitSettings = requestLimitSettings; + this.responseLimitSettings = responseLimitSettings; } @Override @@ -85,10 +86,7 @@ protected void innerOnFailure(Exception e) { client.admin().cluster().state(clusterStateRequest, new ActionListener() { @Override public void onResponse(ClusterStateResponse clusterStateResponse) { - if (shardsRequest.isRequestLimitCheckSupported() - && requestLimitSettings.isCircuitLimitBreached(clusterStateResponse.getState(), CAT_SHARDS)) { - listener.onFailure(new CircuitBreakingException("Too many shards requested.", CircuitBreaker.Durability.TRANSIENT)); - } + validateRequestLimit(shardsRequest, clusterStateResponse, cancellableListener); catShardsResponse.setClusterStateResponse(clusterStateResponse); IndicesStatsRequest indicesStatsRequest = new IndicesStatsRequest(); indicesStatsRequest.setShouldCancelOnTimeout(true); @@ -123,4 +121,21 @@ public void onFailure(Exception e) { } } + + private void validateRequestLimit( + final CatShardsRequest shardsRequest, + final ClusterStateResponse clusterStateResponse, + final ActionListener listener + ) { + if (shardsRequest.isRequestLimitCheckSupported() + && Objects.nonNull(clusterStateResponse) + && Objects.nonNull(clusterStateResponse.getState())) { + int limit = responseLimitSettings.getCatShardsResponseLimit(); + if (ResponseLimitSettings.isResponseLimitBreached(clusterStateResponse.getState().getRoutingTable(), SHARDS, limit)) { + listener.onFailure( + new ResponseLimitBreachedException("Too many shards requested. Can not request shards beyond {" + limit + "}") + ); + } + } + } } diff --git a/server/src/main/java/org/opensearch/common/settings/ClusterSettings.java b/server/src/main/java/org/opensearch/common/settings/ClusterSettings.java index 66fcd4583ae2b..faa1af2a2fcb6 100644 --- a/server/src/main/java/org/opensearch/common/settings/ClusterSettings.java +++ b/server/src/main/java/org/opensearch/common/settings/ClusterSettings.java @@ -155,7 +155,7 @@ import org.opensearch.repositories.blobstore.BlobStoreRepository; import org.opensearch.repositories.fs.FsRepository; import org.opensearch.rest.BaseRestHandler; -import org.opensearch.rest.RequestLimitSettings; +import org.opensearch.rest.ResponseLimitSettings; import org.opensearch.script.ScriptService; import org.opensearch.search.SearchService; import org.opensearch.search.aggregations.MultiBucketConsumerService; @@ -799,9 +799,10 @@ public void apply(Settings value, Settings current, Settings previous) { WorkloadManagementSettings.NODE_LEVEL_MEMORY_REJECTION_THRESHOLD, WorkloadManagementSettings.NODE_LEVEL_MEMORY_CANCELLATION_THRESHOLD, - RequestLimitSettings.CAT_INDICES_LIMIT_SETTING, - RequestLimitSettings.CAT_SHARDS_LIMIT_SETTING, - RequestLimitSettings.CAT_SEGMENTS_LIMIT_SETTING + // Settings to be used for limiting rest requests + ResponseLimitSettings.CAT_INDICES_RESPONSE_LIMIT_SETTING, + ResponseLimitSettings.CAT_SHARDS_RESPONSE_LIMIT_SETTING, + ResponseLimitSettings.CAT_SEGMENTS_RESPONSE_LIMIT_SETTING ) ) ); diff --git a/server/src/main/java/org/opensearch/rest/RequestLimitSettings.java b/server/src/main/java/org/opensearch/rest/RequestLimitSettings.java deleted file mode 100644 index 8cc4199d302b9..0000000000000 --- a/server/src/main/java/org/opensearch/rest/RequestLimitSettings.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - -package org.opensearch.rest; - -import org.opensearch.cluster.ClusterState; -import org.opensearch.cluster.routing.IndexRoutingTable; -import org.opensearch.cluster.routing.IndexShardRoutingTable; -import org.opensearch.cluster.routing.RoutingTable; -import org.opensearch.common.settings.ClusterSettings; -import org.opensearch.common.settings.Setting; -import org.opensearch.common.settings.Settings; -import org.opensearch.rest.action.cat.RestIndicesAction; -import org.opensearch.rest.action.cat.RestSegmentsAction; -import org.opensearch.rest.action.cat.RestShardsAction; - -import java.util.Map; -import java.util.Objects; -import java.util.function.Supplier; - -/** - * Class to define dynamic settings for putting circuit breakers on the actions and functions to evaluate if block is required. - */ -public class RequestLimitSettings { - - /** - * Enum to represent action names against whom we need to perform limit checks. - */ - public enum BlockAction { - CAT_INDICES, - CAT_SHARDS, - CAT_SEGMENTS - } - - private volatile int catIndicesLimit; - private volatile int catShardsLimit; - private volatile int catSegmentsLimit; - - /** - * Setting to enable circuit breaker on {@link RestIndicesAction}. The limit will be applied on number of indices. - */ - public static final Setting CAT_INDICES_LIMIT_SETTING = Setting.intSetting( - "cat.indices.limit", - -1, - Setting.Property.NodeScope, - Setting.Property.Dynamic - ); - - /** - * Setting to enable circuit breaker on {@link RestShardsAction}. The limit will be applied on number of shards. - */ - public static final Setting CAT_SHARDS_LIMIT_SETTING = Setting.intSetting( - "cat.shards.limit", - -1, - Setting.Property.NodeScope, - Setting.Property.Dynamic - ); - - /** - * Setting to enable circuit breaker on {@link RestSegmentsAction}. The limit will be applied on number of indices. - */ - public static final Setting CAT_SEGMENTS_LIMIT_SETTING = Setting.intSetting( - "cat.segments.limit", - -1, - Setting.Property.NodeScope, - Setting.Property.Dynamic - ); - - public RequestLimitSettings(ClusterSettings clusterSettings, Settings settings) { - setCatShardsLimitSetting(CAT_SHARDS_LIMIT_SETTING.get(settings)); - setCatIndicesLimitSetting(CAT_INDICES_LIMIT_SETTING.get(settings)); - setCatSegmentsLimitSetting(CAT_SEGMENTS_LIMIT_SETTING.get(settings)); - - clusterSettings.addSettingsUpdateConsumer(CAT_SHARDS_LIMIT_SETTING, this::setCatShardsLimitSetting); - clusterSettings.addSettingsUpdateConsumer(CAT_INDICES_LIMIT_SETTING, this::setCatIndicesLimitSetting); - clusterSettings.addSettingsUpdateConsumer(CAT_SEGMENTS_LIMIT_SETTING, this::setCatSegmentsLimitSetting); - } - - /** - * Method to check if the circuit breaker limit has reached for an action. - * The limits are controlled via dynamic settings. - * - * @param clusterState {@link ClusterState} - * @param actionToCheck {@link BlockAction} - * @return True/False - */ - public boolean isCircuitLimitBreached(final ClusterState clusterState, final BlockAction actionToCheck) { - if (Objects.isNull(clusterState)) return false; - switch (actionToCheck) { - case CAT_INDICES: - if (catIndicesLimit <= 0) return false; - int indicesCount = chainWalk(() -> clusterState.getMetadata().getIndices().size(), 0); - if (indicesCount > catIndicesLimit) return true; - break; - case CAT_SHARDS: - if (catShardsLimit <= 0) return false; - final RoutingTable routingTable = clusterState.getRoutingTable(); - final Map indexRoutingTableMap = routingTable.getIndicesRouting(); - int totalShards = 0; - for (final Map.Entry entry : indexRoutingTableMap.entrySet()) { - for (final Map.Entry indexShardRoutingTableEntry : entry.getValue() - .getShards() - .entrySet()) { - totalShards += indexShardRoutingTableEntry.getValue().getShards().size(); - // Fail fast if catShardsLimit value is breached and avoid unnecessary computation. - if (totalShards > catShardsLimit) return true; - } - } - break; - case CAT_SEGMENTS: - if (catSegmentsLimit <= 0) return false; - int indicesCountForCatSegment = chainWalk(() -> clusterState.getRoutingTable().getIndicesRouting().size(), 0); - if (indicesCountForCatSegment > catSegmentsLimit) return true; - break; - } - return false; - } - - private void setCatShardsLimitSetting(final int catShardsLimit) { - this.catShardsLimit = catShardsLimit; - } - - private void setCatIndicesLimitSetting(final int catIndicesLimit) { - this.catIndicesLimit = catIndicesLimit; - } - - private void setCatSegmentsLimitSetting(final int catSegmentsLimit) { - this.catSegmentsLimit = catSegmentsLimit; - } - - // TODO: Evaluate if we can move this to common util. - private static T chainWalk(Supplier supplier, T defaultValue) { - try { - return supplier.get(); - } catch (NullPointerException e) { - return defaultValue; - } - } -} diff --git a/server/src/main/java/org/opensearch/rest/ResponseLimitBreachedException.java b/server/src/main/java/org/opensearch/rest/ResponseLimitBreachedException.java new file mode 100644 index 0000000000000..281e023a45a6b --- /dev/null +++ b/server/src/main/java/org/opensearch/rest/ResponseLimitBreachedException.java @@ -0,0 +1,36 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.rest; + +import org.opensearch.OpenSearchException; +import org.opensearch.core.common.io.stream.StreamInput; +import org.opensearch.core.rest.RestStatus; + +import java.io.IOException; + +/** + * Thrown when api response limit threshold exceeds + * + * @opensearch.internal + */ +public class ResponseLimitBreachedException extends OpenSearchException { + + public ResponseLimitBreachedException(String msg) { + super(msg); + } + + public ResponseLimitBreachedException(StreamInput in) throws IOException { + super(in); + } + + @Override + public RestStatus status() { + return RestStatus.TOO_MANY_REQUESTS; + } +} diff --git a/server/src/main/java/org/opensearch/rest/ResponseLimitSettings.java b/server/src/main/java/org/opensearch/rest/ResponseLimitSettings.java new file mode 100644 index 0000000000000..ac29331c1954f --- /dev/null +++ b/server/src/main/java/org/opensearch/rest/ResponseLimitSettings.java @@ -0,0 +1,172 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.rest; + +import org.opensearch.cluster.metadata.Metadata; +import org.opensearch.cluster.routing.IndexRoutingTable; +import org.opensearch.cluster.routing.RoutingTable; +import org.opensearch.common.settings.ClusterSettings; +import org.opensearch.common.settings.Setting; +import org.opensearch.common.settings.Settings; +import org.opensearch.rest.action.cat.RestIndicesAction; +import org.opensearch.rest.action.cat.RestSegmentsAction; +import org.opensearch.rest.action.cat.RestShardsAction; + +import java.util.Map; +import java.util.Objects; +import java.util.function.Function; + +/** + * Class to define dynamic settings for putting response limits on the actions and methods to evaluate if block is required. + */ +public class ResponseLimitSettings { + + /** + * Enum to represent entity against which we need to perform limit checks. + */ + public enum LimitEntity { + INDICES, + SHARDS + } + + private volatile int catIndicesResponseLimit; + private volatile int catShardsResponseLimit; + private volatile int catSegmentsResponseLimit; + + /** + * Setting to enable response limit on {@link RestIndicesAction}. The limit will be applied on number of indices. + */ + public static final Setting CAT_INDICES_RESPONSE_LIMIT_SETTING = Setting.intSetting( + "cat.indices.response.limit.number_of_indices", + -1, + Setting.Property.NodeScope, + Setting.Property.Dynamic + ); + + /** + * Setting to enable response limit on {@link RestShardsAction}. The limit will be applied on number of shards. + */ + public static final Setting CAT_SHARDS_RESPONSE_LIMIT_SETTING = Setting.intSetting( + "cat.shards.response.limit.number_of_shards", + -1, + Setting.Property.NodeScope, + Setting.Property.Dynamic + ); + + /** + * Setting to enable response limit on {@link RestSegmentsAction}. The limit will be applied on number of indices. + */ + public static final Setting CAT_SEGMENTS_RESPONSE_LIMIT_SETTING = Setting.intSetting( + "cat.segments.response.limit.number_of_indices", + -1, + Setting.Property.NodeScope, + Setting.Property.Dynamic + ); + + public ResponseLimitSettings(ClusterSettings clusterSettings, Settings settings) { + setCatShardsResponseLimit(CAT_SHARDS_RESPONSE_LIMIT_SETTING.get(settings)); + setCatIndicesResponseLimit(CAT_INDICES_RESPONSE_LIMIT_SETTING.get(settings)); + setCatSegmentsResponseLimit(CAT_SEGMENTS_RESPONSE_LIMIT_SETTING.get(settings)); + + clusterSettings.addSettingsUpdateConsumer(CAT_SHARDS_RESPONSE_LIMIT_SETTING, this::setCatShardsResponseLimit); + clusterSettings.addSettingsUpdateConsumer(CAT_INDICES_RESPONSE_LIMIT_SETTING, this::setCatIndicesResponseLimit); + clusterSettings.addSettingsUpdateConsumer(CAT_SEGMENTS_RESPONSE_LIMIT_SETTING, this::setCatSegmentsResponseLimit); + } + + /** + * Method to check if the response limit has reached for an action. + * The limits are controlled via dynamic settings. + * + * @param metadata {@link Metadata} + * @param limitEntity {@link LimitEntity} + * @param limit Integer limit on block entity + * @return True/False + */ + public static boolean isResponseLimitBreached(final Metadata metadata, final LimitEntity limitEntity, final int limit) { + if (Objects.isNull(metadata)) return false; + if (limit <= 0) return false; + if (Objects.requireNonNull(limitEntity) == LimitEntity.INDICES) { + int indicesCount = getTotalIndicesFromMetadata.apply(metadata); + return indicesCount > limit; + } + return false; + } + + /** + * Method to check if the response limit has reached for an action. + * The limits are controlled via dynamic settings. + * + * @param routingTable {@link RoutingTable} + * @param limitEntity {@link LimitEntity} + * @param limit Integer limit on block entity + * @return True/False + */ + public static boolean isResponseLimitBreached(final RoutingTable routingTable, final LimitEntity limitEntity, final int limit) { + if (Objects.isNull(routingTable)) return false; + if (limit <= 0) return false; + switch (limitEntity) { + case INDICES: + int indicesCount = getTotalIndicesFromRoutingTable.apply(routingTable); + if (indicesCount > limit) return true; + break; + case SHARDS: + final Map indexRoutingTableMap = routingTable.getIndicesRouting(); + int totalShards = 0; + for (final Map.Entry entry : indexRoutingTableMap.entrySet()) { + // In case routing table is corrupted. We will not block actions. + if (Objects.isNull(entry.getValue()) || Objects.isNull(entry.getValue().getShards())) { + return false; + } + totalShards += entry.getValue().getShards().size(); + if (totalShards > limit) return true; + } + System.out.println("Total Shards" + totalShards); + break; + } + return false; + } + + private void setCatShardsResponseLimit(final int catShardsResponseLimit) { + this.catShardsResponseLimit = catShardsResponseLimit; + } + + private void setCatIndicesResponseLimit(final int catIndicesResponseLimit) { + this.catIndicesResponseLimit = catIndicesResponseLimit; + } + + private void setCatSegmentsResponseLimit(final int catSegmentsResponseLimit) { + this.catSegmentsResponseLimit = catSegmentsResponseLimit; + } + + public int getCatShardsResponseLimit() { + return this.catShardsResponseLimit; + } + + public int getCatIndicesResponseLimit() { + return this.catIndicesResponseLimit; + } + + public int getCatSegmentsResponseLimit() { + return this.catSegmentsResponseLimit; + } + + static Function getTotalIndicesFromMetadata = (metadata) -> { + if (Objects.nonNull(metadata) && Objects.nonNull(metadata.getIndices())) { + return metadata.getIndices().size(); + } + return 0; + }; + + static Function getTotalIndicesFromRoutingTable = (routingTable) -> { + if (Objects.nonNull(routingTable) && Objects.nonNull(routingTable.getIndicesRouting())) { + return routingTable.getIndicesRouting().size(); + } + return 0; + }; +} diff --git a/server/src/main/java/org/opensearch/rest/action/cat/AbstractCatAction.java b/server/src/main/java/org/opensearch/rest/action/cat/AbstractCatAction.java index e502b5a110b5e..654624ca63faa 100644 --- a/server/src/main/java/org/opensearch/rest/action/cat/AbstractCatAction.java +++ b/server/src/main/java/org/opensearch/rest/action/cat/AbstractCatAction.java @@ -39,7 +39,7 @@ import org.opensearch.core.rest.RestStatus; import org.opensearch.rest.BaseRestHandler; import org.opensearch.rest.BytesRestResponse; -import org.opensearch.rest.RequestLimitSettings; +import org.opensearch.rest.ResponseLimitSettings; import org.opensearch.rest.RestRequest; import java.io.IOException; @@ -100,7 +100,7 @@ protected Set responseParams() { } /** - * Method to check if limits defined in {@link RequestLimitSettings} are applicable to an action. + * Method to check if limits defined in {@link ResponseLimitSettings} are applicable to an action. * * @return True / False status */ diff --git a/server/src/main/java/org/opensearch/rest/action/cat/RestIndicesAction.java b/server/src/main/java/org/opensearch/rest/action/cat/RestIndicesAction.java index 19e3470c1b449..a06df1e44dc4b 100644 --- a/server/src/main/java/org/opensearch/rest/action/cat/RestIndicesAction.java +++ b/server/src/main/java/org/opensearch/rest/action/cat/RestIndicesAction.java @@ -57,10 +57,9 @@ import org.opensearch.core.action.ActionListener; import org.opensearch.core.action.ActionResponse; import org.opensearch.core.common.Strings; -import org.opensearch.core.common.breaker.CircuitBreaker; -import org.opensearch.core.common.breaker.CircuitBreakingException; import org.opensearch.index.IndexSettings; -import org.opensearch.rest.RequestLimitSettings; +import org.opensearch.rest.ResponseLimitBreachedException; +import org.opensearch.rest.ResponseLimitSettings; import org.opensearch.rest.RestRequest; import org.opensearch.rest.RestResponse; import org.opensearch.rest.action.RestResponseListener; @@ -74,6 +73,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.Spliterators; import java.util.function.Function; @@ -83,7 +83,7 @@ import static java.util.Arrays.asList; import static java.util.Collections.unmodifiableList; import static org.opensearch.action.support.clustermanager.ClusterManagerNodeRequest.DEFAULT_CLUSTER_MANAGER_NODE_TIMEOUT; -import static org.opensearch.rest.RequestLimitSettings.BlockAction.CAT_INDICES; +import static org.opensearch.rest.ResponseLimitSettings.LimitEntity.INDICES; import static org.opensearch.rest.RestRequest.Method.GET; /** @@ -100,10 +100,10 @@ public class RestIndicesAction extends AbstractCatAction { private static final String DUPLICATE_PARAMETER_ERROR_MESSAGE = "Please only use one of the request parameters [master_timeout, cluster_manager_timeout]."; - private final RequestLimitSettings requestLimitSettings; + private final ResponseLimitSettings responseLimitSettings; - public RestIndicesAction(RequestLimitSettings requestLimitSettings) { - this.requestLimitSettings = requestLimitSettings; + public RestIndicesAction(ResponseLimitSettings responseLimitSettings) { + this.responseLimitSettings = responseLimitSettings; } @Override @@ -171,7 +171,6 @@ public void onResponse(final GetSettingsResponse getSettingsResponse) { // type of request in the presence of security plugins (looking at you, ClusterHealthRequest), so // force the IndicesOptions for all the sub-requests to be as inclusive as possible. final IndicesOptions subRequestIndicesOptions = IndicesOptions.lenientExpandHidden(); - // Indices that were successfully resolved during the get settings request might be deleted when the // subsequent cluster state, cluster health and indices stats requests execute. We have to distinguish two cases: // 1) the deleted index was explicitly passed as parameter to the /_cat/indices request. In this case we @@ -190,12 +189,7 @@ public void onResponse(final GetSettingsResponse getSettingsResponse) { new ActionListener() { @Override public void onResponse(ClusterStateResponse clusterStateResponse) { - if (isRequestLimitCheckSupported() - && requestLimitSettings.isCircuitLimitBreached(clusterStateResponse.getState(), CAT_INDICES)) { - listener.onFailure( - new CircuitBreakingException("Too many indices requested.", CircuitBreaker.Durability.TRANSIENT) - ); - } + validateRequestLimit(clusterStateResponse, listener); final GroupedActionListener groupedListener = createGroupedListener( request, 4, @@ -239,6 +233,17 @@ public void onFailure(final Exception e) { }; } + private void validateRequestLimit(final ClusterStateResponse clusterStateResponse, final ActionListener listener) { + if (isRequestLimitCheckSupported() && Objects.nonNull(clusterStateResponse) && Objects.nonNull(clusterStateResponse.getState())) { + int limit = responseLimitSettings.getCatIndicesResponseLimit(); + if (ResponseLimitSettings.isResponseLimitBreached(clusterStateResponse.getState().getMetadata(), INDICES, limit)) { + listener.onFailure( + new ResponseLimitBreachedException("Too many indices requested. Can not request indices beyond {" + limit + "}") + ); + } + } + } + /** * We're using the Get Settings API here to resolve the authorized indices for the user. * This is because the Cluster State and Cluster Health APIs do not filter output based diff --git a/server/src/main/java/org/opensearch/rest/action/cat/RestSegmentsAction.java b/server/src/main/java/org/opensearch/rest/action/cat/RestSegmentsAction.java index d608c87cc2b85..8b78897aee3ad 100644 --- a/server/src/main/java/org/opensearch/rest/action/cat/RestSegmentsAction.java +++ b/server/src/main/java/org/opensearch/rest/action/cat/RestSegmentsAction.java @@ -44,10 +44,9 @@ import org.opensearch.common.Table; import org.opensearch.common.logging.DeprecationLogger; import org.opensearch.core.common.Strings; -import org.opensearch.core.common.breaker.CircuitBreaker; -import org.opensearch.core.common.breaker.CircuitBreakingException; import org.opensearch.index.engine.Segment; -import org.opensearch.rest.RequestLimitSettings; +import org.opensearch.rest.ResponseLimitBreachedException; +import org.opensearch.rest.ResponseLimitSettings; import org.opensearch.rest.RestRequest; import org.opensearch.rest.RestResponse; import org.opensearch.rest.action.RestActionListener; @@ -55,10 +54,11 @@ import java.util.List; import java.util.Map; +import java.util.Objects; import static java.util.Arrays.asList; import static java.util.Collections.unmodifiableList; -import static org.opensearch.rest.RequestLimitSettings.BlockAction.CAT_SEGMENTS; +import static org.opensearch.rest.ResponseLimitSettings.LimitEntity.INDICES; import static org.opensearch.rest.RestRequest.Method.GET; /** @@ -70,10 +70,10 @@ public class RestSegmentsAction extends AbstractCatAction { private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestSegmentsAction.class); - private final RequestLimitSettings requestLimitSettings; + private final ResponseLimitSettings responseLimitSettings; - public RestSegmentsAction(RequestLimitSettings requestLimitSettings) { - this.requestLimitSettings = requestLimitSettings; + public RestSegmentsAction(ResponseLimitSettings responseLimitSettings) { + this.responseLimitSettings = responseLimitSettings; } @Override @@ -111,10 +111,7 @@ public RestChannelConsumer doCatRequest(final RestRequest request, final NodeCli return channel -> client.admin().cluster().state(clusterStateRequest, new RestActionListener(channel) { @Override public void processResponse(final ClusterStateResponse clusterStateResponse) { - if (isRequestLimitCheckSupported() - && requestLimitSettings.isCircuitLimitBreached(clusterStateResponse.getState(), CAT_SEGMENTS)) { - throw new CircuitBreakingException("Segments from too many indices requested.", CircuitBreaker.Durability.TRANSIENT); - } + validateRequestLimit(clusterStateResponse); final IndicesSegmentsRequest indicesSegmentsRequest = new IndicesSegmentsRequest(); indicesSegmentsRequest.indices(indices); client.admin().indices().segments(indicesSegmentsRequest, new RestResponseListener(channel) { @@ -129,6 +126,17 @@ public RestResponse buildResponse(final IndicesSegmentResponse indicesSegmentRes }); } + private void validateRequestLimit(final ClusterStateResponse clusterStateResponse) { + if (isRequestLimitCheckSupported() && Objects.nonNull(clusterStateResponse) && Objects.nonNull(clusterStateResponse.getState())) { + int limit = responseLimitSettings.getCatSegmentsResponseLimit(); + if (ResponseLimitSettings.isResponseLimitBreached(clusterStateResponse.getState().getRoutingTable(), INDICES, limit)) { + throw new ResponseLimitBreachedException( + "Segments from too many indices requested. Can not request indices beyond {" + limit + "}" + ); + } + } + } + @Override protected void documentation(StringBuilder sb) { sb.append("/_cat/segments\n"); diff --git a/server/src/test/java/org/opensearch/action/RenamedTimeoutRequestParameterTests.java b/server/src/test/java/org/opensearch/action/RenamedTimeoutRequestParameterTests.java index 088b4d2824f1e..d7eaf326939cb 100644 --- a/server/src/test/java/org/opensearch/action/RenamedTimeoutRequestParameterTests.java +++ b/server/src/test/java/org/opensearch/action/RenamedTimeoutRequestParameterTests.java @@ -19,7 +19,7 @@ import org.opensearch.core.xcontent.MediaTypeRegistry; import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.rest.BaseRestHandler; -import org.opensearch.rest.RequestLimitSettings; +import org.opensearch.rest.ResponseLimitSettings; import org.opensearch.rest.action.admin.cluster.RestCleanupRepositoryAction; import org.opensearch.rest.action.admin.cluster.RestCloneSnapshotAction; import org.opensearch.rest.action.admin.cluster.RestClusterGetSettingsAction; @@ -159,8 +159,8 @@ public void testCatAllocation() { public void testCatIndices() { ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); Settings settings = Settings.builder().build(); - RequestLimitSettings requestLimitSettings = new RequestLimitSettings(clusterSettings, settings); - RestIndicesAction action = new RestIndicesAction(requestLimitSettings); + ResponseLimitSettings responseLimitSettings = new ResponseLimitSettings(clusterSettings, settings); + RestIndicesAction action = new RestIndicesAction(responseLimitSettings); Exception e = assertThrows(OpenSearchParseException.class, () -> action.doCatRequest(getRestRequestWithBothParams(), client)); assertThat(e.getMessage(), containsString(DUPLICATE_PARAMETER_ERROR_MESSAGE)); assertWarnings(MASTER_TIMEOUT_DEPRECATED_MESSAGE); @@ -246,8 +246,8 @@ public void testCatThreadPool() { public void testCatSegments() { final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); final Settings settings = Settings.builder().build(); - final RequestLimitSettings requestLimitSettings = new RequestLimitSettings(clusterSettings, settings); - RestSegmentsAction action = new RestSegmentsAction(requestLimitSettings); + final ResponseLimitSettings responseLimitSettings = new ResponseLimitSettings(clusterSettings, settings); + RestSegmentsAction action = new RestSegmentsAction(responseLimitSettings); Exception e = assertThrows(OpenSearchParseException.class, () -> action.doCatRequest(getRestRequestWithBothParams(), client)); assertThat(e.getMessage(), containsString(DUPLICATE_PARAMETER_ERROR_MESSAGE)); assertWarnings(MASTER_TIMEOUT_DEPRECATED_MESSAGE); diff --git a/server/src/test/java/org/opensearch/rest/RequestLimitSettingsTests.java b/server/src/test/java/org/opensearch/rest/ResponseLimitSettingsTests.java similarity index 54% rename from server/src/test/java/org/opensearch/rest/RequestLimitSettingsTests.java rename to server/src/test/java/org/opensearch/rest/ResponseLimitSettingsTests.java index c7ef256592eb1..815633a25a31c 100644 --- a/server/src/test/java/org/opensearch/rest/RequestLimitSettingsTests.java +++ b/server/src/test/java/org/opensearch/rest/ResponseLimitSettingsTests.java @@ -27,115 +27,167 @@ import java.util.HashMap; import java.util.Map; -import static org.opensearch.rest.RequestLimitSettings.CAT_INDICES_LIMIT_SETTING; -import static org.opensearch.rest.RequestLimitSettings.CAT_SEGMENTS_LIMIT_SETTING; -import static org.opensearch.rest.RequestLimitSettings.CAT_SHARDS_LIMIT_SETTING; +import static org.opensearch.rest.ResponseLimitSettings.CAT_INDICES_RESPONSE_LIMIT_SETTING; +import static org.opensearch.rest.ResponseLimitSettings.CAT_SEGMENTS_RESPONSE_LIMIT_SETTING; +import static org.opensearch.rest.ResponseLimitSettings.CAT_SHARDS_RESPONSE_LIMIT_SETTING; -public class RequestLimitSettingsTests extends OpenSearchTestCase { +public class ResponseLimitSettingsTests extends OpenSearchTestCase { - public void testIsCircuitLimitBreached_forNullClusterState_expectNotBreached() { + public void testIsResponseLimitBreached_forNullMetadata_expectNotBreached() { final Settings settings = Settings.builder().build(); final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); - final RequestLimitSettings requestLimitSettings = new RequestLimitSettings(clusterSettings, settings); - final boolean breached = requestLimitSettings.isCircuitLimitBreached(null, RequestLimitSettings.BlockAction.CAT_INDICES); + final ResponseLimitSettings responseLimitSettings = new ResponseLimitSettings(clusterSettings, settings); + final boolean breached = ResponseLimitSettings.isResponseLimitBreached( + (Metadata) null, + ResponseLimitSettings.LimitEntity.INDICES, + 0 + ); assertFalse(breached); } - public void testIsCircuitLimitBreached_forCatIndicesWithSettingDisabled_expectNotBreached() { + public void testIsResponseLimitBreached_forNullRoutingTable_expectNotBreached() { + final Settings settings = Settings.builder().build(); + final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); + final ResponseLimitSettings responseLimitSettings = new ResponseLimitSettings(clusterSettings, settings); + final boolean breached = ResponseLimitSettings.isResponseLimitBreached( + (RoutingTable) null, + ResponseLimitSettings.LimitEntity.INDICES, + 0 + ); + assertFalse(breached); + } + + public void testIsResponseLimitBreached_forCatIndicesWithSettingDisabled_expectNotBreached() { // Don't enable limit - final Settings settings = Settings.builder().put(CAT_INDICES_LIMIT_SETTING.getKey(), -1).build(); + final Settings settings = Settings.builder().put(CAT_INDICES_RESPONSE_LIMIT_SETTING.getKey(), -1).build(); final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); - final RequestLimitSettings requestLimitSettings = new RequestLimitSettings(clusterSettings, settings); + final ResponseLimitSettings responseLimitSettings = new ResponseLimitSettings(clusterSettings, settings); final ClusterState clusterState = buildClusterState("test-index-1", "test-index-2", "test-index-3"); - final boolean breached = requestLimitSettings.isCircuitLimitBreached(clusterState, RequestLimitSettings.BlockAction.CAT_INDICES); + final boolean breached = ResponseLimitSettings.isResponseLimitBreached( + clusterState.getMetadata(), + ResponseLimitSettings.LimitEntity.INDICES, + responseLimitSettings.getCatIndicesResponseLimit() + ); assertFalse(breached); } - public void testIsCircuitLimitBreached_forCatIndicesWithSettingEnabled_expectBreached() { + public void testIsResponseLimitBreached_forCatIndicesWithSettingEnabled_expectBreached() { // Set limit of 1 index - final Settings settings = Settings.builder().put(CAT_INDICES_LIMIT_SETTING.getKey(), 1).build(); + final Settings settings = Settings.builder().put(CAT_INDICES_RESPONSE_LIMIT_SETTING.getKey(), 1).build(); final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); - final RequestLimitSettings requestLimitSettings = new RequestLimitSettings(clusterSettings, settings); + final ResponseLimitSettings responseLimitSettings = new ResponseLimitSettings(clusterSettings, settings); // Pass cluster state with 3 indices final ClusterState clusterState = buildClusterState("test-index-1", "test-index-2", "test-index-3"); - final boolean breached = requestLimitSettings.isCircuitLimitBreached(clusterState, RequestLimitSettings.BlockAction.CAT_INDICES); + final boolean breached = ResponseLimitSettings.isResponseLimitBreached( + clusterState.getMetadata(), + ResponseLimitSettings.LimitEntity.INDICES, + responseLimitSettings.getCatIndicesResponseLimit() + ); assertTrue(breached); } - public void testIsCircuitLimitBreached_forCatIndicesWithSettingEnabled_expectNotBreached() { + public void testIsResponseLimitBreached_forCatIndicesWithSettingEnabled_expectNotBreached() { // Set limit of 5 indices - final Settings settings = Settings.builder().put(CAT_INDICES_LIMIT_SETTING.getKey(), 5).build(); + final Settings settings = Settings.builder().put(CAT_INDICES_RESPONSE_LIMIT_SETTING.getKey(), 5).build(); final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); - final RequestLimitSettings requestLimitSettings = new RequestLimitSettings(clusterSettings, settings); + final ResponseLimitSettings responseLimitSettings = new ResponseLimitSettings(clusterSettings, settings); // Pass cluster state with 3 indices final ClusterState clusterState = buildClusterState("test-index-1", "test-index-2", "test-index-3"); - final boolean breached = requestLimitSettings.isCircuitLimitBreached(clusterState, RequestLimitSettings.BlockAction.CAT_INDICES); + final boolean breached = ResponseLimitSettings.isResponseLimitBreached( + clusterState.getMetadata(), + ResponseLimitSettings.LimitEntity.INDICES, + responseLimitSettings.getCatIndicesResponseLimit() + ); assertFalse(breached); } - public void testIsCircuitLimitBreached_forCatShardsWithSettingDisabled_expectNotBreached() { + public void testIsResponseLimitBreached_forCatShardsWithSettingDisabled_expectNotBreached() { // Don't enable limit - final Settings settings = Settings.builder().put(CAT_SHARDS_LIMIT_SETTING.getKey(), -1).build(); + final Settings settings = Settings.builder().put(CAT_SHARDS_RESPONSE_LIMIT_SETTING.getKey(), -1).build(); final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); - final RequestLimitSettings requestLimitSettings = new RequestLimitSettings(clusterSettings, settings); + final ResponseLimitSettings responseLimitSettings = new ResponseLimitSettings(clusterSettings, settings); // Build cluster state with 3 shards final ClusterState clusterState = buildClusterState("test-index-1", "test-index-2", "test-index-3"); - final boolean breached = requestLimitSettings.isCircuitLimitBreached(clusterState, RequestLimitSettings.BlockAction.CAT_SHARDS); + final boolean breached = ResponseLimitSettings.isResponseLimitBreached( + clusterState.getRoutingTable(), + ResponseLimitSettings.LimitEntity.SHARDS, + responseLimitSettings.getCatShardsResponseLimit() + ); assertFalse(breached); } - public void testIsCircuitLimitBreached_forCatShardsWithSettingEnabled_expectBreached() { + public void testIsResponseLimitBreached_forCatShardsWithSettingEnabled_expectBreached() { // Set limit of 2 shards - final Settings settings = Settings.builder().put(CAT_SHARDS_LIMIT_SETTING.getKey(), 2).build(); + final Settings settings = Settings.builder().put(CAT_SHARDS_RESPONSE_LIMIT_SETTING.getKey(), 2).build(); final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); - final RequestLimitSettings requestLimitSettings = new RequestLimitSettings(clusterSettings, settings); + final ResponseLimitSettings responseLimitSettings = new ResponseLimitSettings(clusterSettings, settings); // Build cluster state with 3 shards final ClusterState clusterState = buildClusterState("test-index-1", "test-index-2", "test-index-3"); - final boolean breached = requestLimitSettings.isCircuitLimitBreached(clusterState, RequestLimitSettings.BlockAction.CAT_SHARDS); + final boolean breached = ResponseLimitSettings.isResponseLimitBreached( + clusterState.getRoutingTable(), + ResponseLimitSettings.LimitEntity.SHARDS, + responseLimitSettings.getCatShardsResponseLimit() + ); assertTrue(breached); } - public void testIsCircuitLimitBreached_forCatShardsWithSettingEnabled_expectNotBreached() { + public void testIsResponseLimitBreached_forCatShardsWithSettingEnabled_expectNotBreached() { // Set limit of 3 shards - final Settings settings = Settings.builder().put(CAT_SHARDS_LIMIT_SETTING.getKey(), 3).build(); + final Settings settings = Settings.builder().put(CAT_SHARDS_RESPONSE_LIMIT_SETTING.getKey(), 3).build(); final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); - final RequestLimitSettings requestLimitSettings = new RequestLimitSettings(clusterSettings, settings); + final ResponseLimitSettings responseLimitSettings = new ResponseLimitSettings(clusterSettings, settings); // Build cluster state with 3 shards final ClusterState clusterState = buildClusterState("test-index-1", "test-index-2", "test-index-3"); - final boolean breached = requestLimitSettings.isCircuitLimitBreached(clusterState, RequestLimitSettings.BlockAction.CAT_SHARDS); + final boolean breached = ResponseLimitSettings.isResponseLimitBreached( + clusterState.getRoutingTable(), + ResponseLimitSettings.LimitEntity.SHARDS, + responseLimitSettings.getCatShardsResponseLimit() + ); assertFalse(breached); } - public void testIsCircuitLimitBreached_forCatSegmentsWithSettingDisabled_expectNotBreached() { + public void testIsResponseLimitBreached_forCatSegmentsWithSettingDisabled_expectNotBreached() { // Don't enable limit - final Settings settings = Settings.builder().put(CAT_SEGMENTS_LIMIT_SETTING.getKey(), -1).build(); + final Settings settings = Settings.builder().put(CAT_SEGMENTS_RESPONSE_LIMIT_SETTING.getKey(), -1).build(); final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); - final RequestLimitSettings requestLimitSettings = new RequestLimitSettings(clusterSettings, settings); + final ResponseLimitSettings responseLimitSettings = new ResponseLimitSettings(clusterSettings, settings); // Build cluster state with 3 indices final ClusterState clusterState = buildClusterState("test-index-1", "test-index-2", "test-index-3"); - final boolean breached = requestLimitSettings.isCircuitLimitBreached(clusterState, RequestLimitSettings.BlockAction.CAT_SEGMENTS); + final boolean breached = ResponseLimitSettings.isResponseLimitBreached( + clusterState.getRoutingTable(), + ResponseLimitSettings.LimitEntity.INDICES, + responseLimitSettings.getCatSegmentsResponseLimit() + ); assertFalse(breached); } - public void testIsCircuitLimitBreached_forCatSegmentsWithSettingEnabled_expectBreached() { + public void testIsResponseLimitBreached_forCatSegmentsWithSettingEnabled_expectBreached() { // Set limit of 1 index - final Settings settings = Settings.builder().put(CAT_SEGMENTS_LIMIT_SETTING.getKey(), 1).build(); + final Settings settings = Settings.builder().put(CAT_SEGMENTS_RESPONSE_LIMIT_SETTING.getKey(), 1).build(); final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); - final RequestLimitSettings requestLimitSettings = new RequestLimitSettings(clusterSettings, settings); + final ResponseLimitSettings responseLimitSettings = new ResponseLimitSettings(clusterSettings, settings); // Build cluster state with 3 indices final ClusterState clusterState = buildClusterState("test-index-1", "test-index-2", "test-index-3"); - final boolean breached = requestLimitSettings.isCircuitLimitBreached(clusterState, RequestLimitSettings.BlockAction.CAT_SEGMENTS); + final boolean breached = ResponseLimitSettings.isResponseLimitBreached( + clusterState.getRoutingTable(), + ResponseLimitSettings.LimitEntity.INDICES, + responseLimitSettings.getCatSegmentsResponseLimit() + ); assertTrue(breached); } - public void testIsCircuitLimitBreached_forCatSegmentsWithSettingEnabled_expectNotBreached() { + public void testIsResponseLimitBreached_forCatSegmentsWithSettingEnabled_expectNotBreached() { // Set limit of 3 indices - final Settings settings = Settings.builder().put(CAT_SEGMENTS_LIMIT_SETTING.getKey(), 5).build(); + final Settings settings = Settings.builder().put(CAT_SEGMENTS_RESPONSE_LIMIT_SETTING.getKey(), 5).build(); final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); - final RequestLimitSettings requestLimitSettings = new RequestLimitSettings(clusterSettings, settings); + final ResponseLimitSettings responseLimitSettings = new ResponseLimitSettings(clusterSettings, settings); // Build cluster state with 3 indices final ClusterState clusterState = buildClusterState("test-index-1", "test-index-2", "test-index-3"); - final boolean breached = requestLimitSettings.isCircuitLimitBreached(clusterState, RequestLimitSettings.BlockAction.CAT_SEGMENTS); + final boolean breached = ResponseLimitSettings.isResponseLimitBreached( + clusterState.getRoutingTable(), + ResponseLimitSettings.LimitEntity.INDICES, + responseLimitSettings.getCatSegmentsResponseLimit() + ); assertFalse(breached); } diff --git a/server/src/test/java/org/opensearch/rest/action/cat/RestIndicesActionTests.java b/server/src/test/java/org/opensearch/rest/action/cat/RestIndicesActionTests.java index b6666136e2379..51cf432f0e986 100644 --- a/server/src/test/java/org/opensearch/rest/action/cat/RestIndicesActionTests.java +++ b/server/src/test/java/org/opensearch/rest/action/cat/RestIndicesActionTests.java @@ -48,7 +48,7 @@ import org.opensearch.core.index.Index; import org.opensearch.core.index.shard.ShardId; import org.opensearch.index.IndexSettings; -import org.opensearch.rest.RequestLimitSettings; +import org.opensearch.rest.ResponseLimitSettings; import org.opensearch.test.OpenSearchTestCase; import org.opensearch.test.rest.FakeRestRequest; @@ -141,8 +141,8 @@ public void testBuildTable() { final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); final Settings settings = Settings.builder().build(); - final RequestLimitSettings requestLimitSettings = new RequestLimitSettings(clusterSettings, settings); - final RestIndicesAction action = new RestIndicesAction(requestLimitSettings); + final ResponseLimitSettings responseLimitSettings = new ResponseLimitSettings(clusterSettings, settings); + final RestIndicesAction action = new RestIndicesAction(responseLimitSettings); final Table table = action.buildTable(new FakeRestRequest(), indicesSettings, indicesHealths, indicesStats, indicesMetadatas); // now, verify the table is correct