|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.rest; |
| 10 | + |
| 11 | +import org.opensearch.cluster.ClusterState; |
| 12 | +import org.opensearch.cluster.routing.IndexRoutingTable; |
| 13 | +import org.opensearch.cluster.routing.IndexShardRoutingTable; |
| 14 | +import org.opensearch.cluster.routing.RoutingTable; |
| 15 | +import org.opensearch.common.settings.ClusterSettings; |
| 16 | +import org.opensearch.common.settings.Setting; |
| 17 | +import org.opensearch.common.settings.Settings; |
| 18 | +import org.opensearch.rest.action.cat.RestIndicesAction; |
| 19 | +import org.opensearch.rest.action.cat.RestSegmentsAction; |
| 20 | +import org.opensearch.rest.action.cat.RestShardsAction; |
| 21 | + |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Objects; |
| 24 | +import java.util.function.Supplier; |
| 25 | + |
| 26 | +/** |
| 27 | + * Class to define dynamic settings for putting circuit breakers on the actions and functions to evaluate if block is required. |
| 28 | + */ |
| 29 | +public class RequestLimitSettings { |
| 30 | + |
| 31 | + /** |
| 32 | + * Enum to represent action names against whom we need to perform limit checks. |
| 33 | + */ |
| 34 | + public enum BlockAction { |
| 35 | + CAT_INDICES, |
| 36 | + CAT_SHARDS, |
| 37 | + CAT_SEGMENTS |
| 38 | + } |
| 39 | + |
| 40 | + private volatile int catIndicesLimit; |
| 41 | + private volatile int catShardsLimit; |
| 42 | + private volatile int catSegmentsLimit; |
| 43 | + |
| 44 | + /** |
| 45 | + * Setting to enable circuit breaker on {@link RestIndicesAction}. The limit will be applied on number of indices. |
| 46 | + */ |
| 47 | + public static final Setting<Integer> CAT_INDICES_LIMIT_SETTING = Setting.intSetting( |
| 48 | + "cat.indices.limit", |
| 49 | + -1, |
| 50 | + Setting.Property.NodeScope, |
| 51 | + Setting.Property.Dynamic |
| 52 | + ); |
| 53 | + |
| 54 | + /** |
| 55 | + * Setting to enable circuit breaker on {@link RestShardsAction}. The limit will be applied on number of shards. |
| 56 | + */ |
| 57 | + public static final Setting<Integer> CAT_SHARDS_LIMIT_SETTING = Setting.intSetting( |
| 58 | + "cat.shards.limit", |
| 59 | + -1, |
| 60 | + Setting.Property.NodeScope, |
| 61 | + Setting.Property.Dynamic |
| 62 | + ); |
| 63 | + |
| 64 | + /** |
| 65 | + * Setting to enable circuit breaker on {@link RestSegmentsAction}. The limit will be applied on number of indices. |
| 66 | + */ |
| 67 | + public static final Setting<Integer> CAT_SEGMENTS_LIMIT_SETTING = Setting.intSetting( |
| 68 | + "cat.segments.limit", |
| 69 | + -1, |
| 70 | + Setting.Property.NodeScope, |
| 71 | + Setting.Property.Dynamic |
| 72 | + ); |
| 73 | + |
| 74 | + public RequestLimitSettings(ClusterSettings clusterSettings, Settings settings) { |
| 75 | + setCatShardsLimitSetting(CAT_SHARDS_LIMIT_SETTING.get(settings)); |
| 76 | + setCatIndicesLimitSetting(CAT_INDICES_LIMIT_SETTING.get(settings)); |
| 77 | + setCatSegmentsLimitSetting(CAT_SEGMENTS_LIMIT_SETTING.get(settings)); |
| 78 | + |
| 79 | + clusterSettings.addSettingsUpdateConsumer(CAT_SHARDS_LIMIT_SETTING, this::setCatShardsLimitSetting); |
| 80 | + clusterSettings.addSettingsUpdateConsumer(CAT_INDICES_LIMIT_SETTING, this::setCatIndicesLimitSetting); |
| 81 | + clusterSettings.addSettingsUpdateConsumer(CAT_SEGMENTS_LIMIT_SETTING, this::setCatSegmentsLimitSetting); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Method to check if the circuit breaker limit has reached for an action. |
| 86 | + * The limits are controlled via dynamic settings. |
| 87 | + * |
| 88 | + * @param clusterState {@link ClusterState} |
| 89 | + * @param actionToCheck {@link BlockAction} |
| 90 | + * @return True/False |
| 91 | + */ |
| 92 | + public boolean isCircuitLimitBreached(final ClusterState clusterState, final BlockAction actionToCheck) { |
| 93 | + if (Objects.isNull(clusterState)) return false; |
| 94 | + switch (actionToCheck) { |
| 95 | + case CAT_INDICES: |
| 96 | + if (catIndicesLimit <= 0) return false; |
| 97 | + int indicesCount = getTotalIndices(clusterState); |
| 98 | + if (indicesCount > catIndicesLimit) return true; |
| 99 | + break; |
| 100 | + case CAT_SHARDS: |
| 101 | + if (catShardsLimit <= 0) return false; |
| 102 | + int totalShards = getTotalShards(clusterState); |
| 103 | + if (totalShards > catShardsLimit) return true; |
| 104 | + break; |
| 105 | + case CAT_SEGMENTS: |
| 106 | + if (catSegmentsLimit <= 0) return false; |
| 107 | + if (getTotalIndices(clusterState) > catSegmentsLimit) return true; |
| 108 | + break; |
| 109 | + } |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + private void setCatShardsLimitSetting(final int catShardsLimit) { |
| 114 | + this.catShardsLimit = catShardsLimit; |
| 115 | + } |
| 116 | + |
| 117 | + private void setCatIndicesLimitSetting(final int catIndicesLimit) { |
| 118 | + this.catIndicesLimit = catIndicesLimit; |
| 119 | + } |
| 120 | + |
| 121 | + private void setCatSegmentsLimitSetting(final int catSegmentsLimit) { |
| 122 | + this.catSegmentsLimit = catSegmentsLimit; |
| 123 | + } |
| 124 | + |
| 125 | + private static int getTotalIndices(final ClusterState clusterState) { |
| 126 | + return chainWalk(() -> clusterState.getMetadata().getIndices().size(), 0); |
| 127 | + } |
| 128 | + |
| 129 | + private static int getTotalShards(final ClusterState clusterState) { |
| 130 | + final RoutingTable routingTable = clusterState.getRoutingTable(); |
| 131 | + final Map<String, IndexRoutingTable> indexRoutingTableMap = routingTable.getIndicesRouting(); |
| 132 | + int totalShards = 0; |
| 133 | + for (final Map.Entry<String, IndexRoutingTable> entry : indexRoutingTableMap.entrySet()) { |
| 134 | + for (final Map.Entry<Integer, IndexShardRoutingTable> indexShardRoutingTableEntry : entry.getValue().getShards().entrySet()) { |
| 135 | + totalShards += indexShardRoutingTableEntry.getValue().getShards().size(); |
| 136 | + } |
| 137 | + } |
| 138 | + return totalShards; |
| 139 | + } |
| 140 | + |
| 141 | + // TODO: Evaluate if we can move this to common util. |
| 142 | + private static <T> T chainWalk(Supplier<T> supplier, T defaultValue) { |
| 143 | + try { |
| 144 | + return supplier.get(); |
| 145 | + } catch (NullPointerException e) { |
| 146 | + return defaultValue; |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments