|
| 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.action.admin.cluster.shards; |
| 10 | + |
| 11 | +import org.opensearch.action.admin.cluster.state.ClusterStateResponse; |
| 12 | +import org.opensearch.action.admin.indices.stats.IndicesStatsResponse; |
| 13 | +import org.opensearch.cluster.metadata.IndexMetadata; |
| 14 | +import org.opensearch.cluster.routing.ShardRouting; |
| 15 | +import org.opensearch.common.settings.Settings; |
| 16 | +import org.opensearch.common.unit.TimeValue; |
| 17 | +import org.opensearch.core.action.ActionListener; |
| 18 | +import org.opensearch.core.common.Strings; |
| 19 | +import org.opensearch.core.tasks.TaskCancelledException; |
| 20 | +import org.opensearch.test.InternalTestCluster; |
| 21 | +import org.opensearch.test.OpenSearchIntegTestCase; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.List; |
| 25 | +import java.util.concurrent.CountDownLatch; |
| 26 | + |
| 27 | +import static org.opensearch.cluster.routing.UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING; |
| 28 | +import static org.opensearch.common.unit.TimeValue.timeValueMillis; |
| 29 | +import static org.opensearch.search.SearchService.NO_TIMEOUT; |
| 30 | + |
| 31 | +@OpenSearchIntegTestCase.ClusterScope(numDataNodes = 0, scope = OpenSearchIntegTestCase.Scope.TEST) |
| 32 | +public class TransportCatShardsActionIT extends OpenSearchIntegTestCase { |
| 33 | + |
| 34 | + public void testCatShardsWithSuccessResponse() throws InterruptedException { |
| 35 | + internalCluster().startClusterManagerOnlyNodes(1); |
| 36 | + List<String> nodes = internalCluster().startDataOnlyNodes(3); |
| 37 | + createIndex( |
| 38 | + "test", |
| 39 | + Settings.builder() |
| 40 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) |
| 41 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 2) |
| 42 | + .put(INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), "60m") |
| 43 | + .build() |
| 44 | + ); |
| 45 | + ensureGreen("test"); |
| 46 | + |
| 47 | + final CatShardsRequest shardsRequest = new CatShardsRequest(); |
| 48 | + shardsRequest.setCancelAfterTimeInterval(NO_TIMEOUT); |
| 49 | + shardsRequest.setIndices(Strings.EMPTY_ARRAY); |
| 50 | + CountDownLatch latch = new CountDownLatch(1); |
| 51 | + client().execute(CatShardsAction.INSTANCE, shardsRequest, new ActionListener<CatShardsResponse>() { |
| 52 | + @Override |
| 53 | + public void onResponse(CatShardsResponse catShardsResponse) { |
| 54 | + ClusterStateResponse clusterStateResponse = catShardsResponse.getClusterStateResponse(); |
| 55 | + IndicesStatsResponse indicesStatsResponse = catShardsResponse.getIndicesStatsResponse(); |
| 56 | + for (ShardRouting shard : clusterStateResponse.getState().routingTable().allShards()) { |
| 57 | + assertEquals("test", shard.getIndexName()); |
| 58 | + assertNotNull(indicesStatsResponse.asMap().get(shard)); |
| 59 | + } |
| 60 | + latch.countDown(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void onFailure(Exception e) { |
| 65 | + fail(); |
| 66 | + latch.countDown(); |
| 67 | + } |
| 68 | + }); |
| 69 | + latch.await(); |
| 70 | + } |
| 71 | + |
| 72 | + public void testCatShardsWithTimeoutException() throws IOException, AssertionError, InterruptedException { |
| 73 | + List<String> masterNodes = internalCluster().startClusterManagerOnlyNodes(1); |
| 74 | + List<String> nodes = internalCluster().startDataOnlyNodes(3); |
| 75 | + createIndex( |
| 76 | + "test", |
| 77 | + Settings.builder() |
| 78 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) |
| 79 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 2) |
| 80 | + .put(INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), "60m") |
| 81 | + .build() |
| 82 | + ); |
| 83 | + ensureGreen("test"); |
| 84 | + |
| 85 | + Settings clusterManagerDataPathSettings = internalCluster().dataPathSettings(masterNodes.get(0)); |
| 86 | + // Dropping master node to delay in cluster state call. |
| 87 | + internalCluster().stopRandomNode(InternalTestCluster.nameFilter(masterNodes.get(0))); |
| 88 | + |
| 89 | + CountDownLatch latch = new CountDownLatch(2); |
| 90 | + new Thread(() -> { |
| 91 | + try { |
| 92 | + // Ensures the cancellation timeout expires. |
| 93 | + Thread.sleep(2000); |
| 94 | + // Starting master node to proceed in cluster state call. |
| 95 | + internalCluster().startClusterManagerOnlyNode( |
| 96 | + Settings.builder().put("node.name", masterNodes.get(0)).put(clusterManagerDataPathSettings).build() |
| 97 | + ); |
| 98 | + latch.countDown(); |
| 99 | + } catch (InterruptedException e) { |
| 100 | + throw new RuntimeException(e); |
| 101 | + } |
| 102 | + }).start(); |
| 103 | + |
| 104 | + final CatShardsRequest shardsRequest = new CatShardsRequest(); |
| 105 | + TimeValue timeoutInterval = timeValueMillis(1000); |
| 106 | + shardsRequest.setCancelAfterTimeInterval(timeoutInterval); |
| 107 | + shardsRequest.clusterManagerNodeTimeout(timeValueMillis(2500)); |
| 108 | + shardsRequest.setIndices(Strings.EMPTY_ARRAY); |
| 109 | + client().execute(CatShardsAction.INSTANCE, shardsRequest, new ActionListener<CatShardsResponse>() { |
| 110 | + @Override |
| 111 | + public void onResponse(CatShardsResponse catShardsResponse) { |
| 112 | + // onResponse should not be called. |
| 113 | + latch.countDown(); |
| 114 | + throw new AssertionError( |
| 115 | + "The cat shards action is expected to fail with a TaskCancelledException, but it received a successful response instead." |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void onFailure(Exception e) { |
| 121 | + assertSame(e.getClass(), TaskCancelledException.class); |
| 122 | + assertEquals(e.getMessage(), "Cancellation timeout of " + timeoutInterval + " is expired"); |
| 123 | + latch.countDown(); |
| 124 | + } |
| 125 | + }); |
| 126 | + latch.await(); |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments