|
| 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.support.nodes; |
| 10 | + |
| 11 | +import org.opensearch.action.admin.cluster.node.stats.NodesStatsRequest; |
| 12 | +import org.opensearch.action.admin.cluster.stats.ClusterStatsRequest; |
| 13 | +import org.opensearch.action.admin.cluster.stats.TransportClusterStatsAction; |
| 14 | +import org.opensearch.action.support.ActionFilters; |
| 15 | +import org.opensearch.action.support.PlainActionFuture; |
| 16 | +import org.opensearch.cluster.node.DiscoveryNode; |
| 17 | +import org.opensearch.cluster.service.ClusterService; |
| 18 | +import org.opensearch.common.io.stream.BytesStreamOutput; |
| 19 | +import org.opensearch.core.common.io.stream.StreamInput; |
| 20 | +import org.opensearch.indices.IndicesService; |
| 21 | +import org.opensearch.node.NodeService; |
| 22 | +import org.opensearch.test.transport.CapturingTransport; |
| 23 | +import org.opensearch.threadpool.ThreadPool; |
| 24 | +import org.opensearch.transport.TransportService; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.HashMap; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Map; |
| 33 | + |
| 34 | +public class TransportClusterStatsActionTests extends TransportNodesActionTests { |
| 35 | + |
| 36 | + /** |
| 37 | + * By default, we send discovery nodes list to each request that is sent across from the coordinator node. This |
| 38 | + * behavior is asserted in this test. |
| 39 | + */ |
| 40 | + public void testClusterStatsActionWithRetentionOfDiscoveryNodesList() { |
| 41 | + ClusterStatsRequest request = new ClusterStatsRequest(); |
| 42 | + request.setIncludeDiscoveryNodes(true); |
| 43 | + Map<String, List<MockClusterStatsNodeRequest>> combinedSentRequest = performNodesInfoAction(request); |
| 44 | + |
| 45 | + assertNotNull(combinedSentRequest); |
| 46 | + combinedSentRequest.forEach((node, capturedRequestList) -> { |
| 47 | + assertNotNull(capturedRequestList); |
| 48 | + capturedRequestList.forEach(sentRequest -> { |
| 49 | + assertNotNull(sentRequest.getDiscoveryNodes()); |
| 50 | + assertEquals(sentRequest.getDiscoveryNodes().length, clusterService.state().nodes().getSize()); |
| 51 | + }); |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + public void testClusterStatsActionWithPreFilledConcreteNodesAndWithRetentionOfDiscoveryNodesList() { |
| 56 | + ClusterStatsRequest request = new ClusterStatsRequest(); |
| 57 | + Collection<DiscoveryNode> discoveryNodes = clusterService.state().getNodes().getNodes().values(); |
| 58 | + request.setConcreteNodes(discoveryNodes.toArray(DiscoveryNode[]::new)); |
| 59 | + Map<String, List<MockClusterStatsNodeRequest>> combinedSentRequest = performNodesInfoAction(request); |
| 60 | + |
| 61 | + assertNotNull(combinedSentRequest); |
| 62 | + combinedSentRequest.forEach((node, capturedRequestList) -> { |
| 63 | + assertNotNull(capturedRequestList); |
| 64 | + capturedRequestList.forEach(sentRequest -> { |
| 65 | + assertNotNull(sentRequest.getDiscoveryNodes()); |
| 66 | + assertEquals(sentRequest.getDiscoveryNodes().length, clusterService.state().nodes().getSize()); |
| 67 | + }); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * In the optimized ClusterStats Request, we do not send the DiscoveryNodes List to each node. This behavior is |
| 73 | + * asserted in this test. |
| 74 | + */ |
| 75 | + public void testClusterStatsActionWithoutRetentionOfDiscoveryNodesList() { |
| 76 | + ClusterStatsRequest request = new ClusterStatsRequest(); |
| 77 | + request.setIncludeDiscoveryNodes(false); |
| 78 | + Map<String, List<MockClusterStatsNodeRequest>> combinedSentRequest = performNodesInfoAction(request); |
| 79 | + |
| 80 | + assertNotNull(combinedSentRequest); |
| 81 | + combinedSentRequest.forEach((node, capturedRequestList) -> { |
| 82 | + assertNotNull(capturedRequestList); |
| 83 | + capturedRequestList.forEach(sentRequest -> { assertNull(sentRequest.getDiscoveryNodes()); }); |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + public void testClusterStatsActionWithPreFilledConcreteNodesAndWithoutRetentionOfDiscoveryNodesList() { |
| 88 | + ClusterStatsRequest request = new ClusterStatsRequest(); |
| 89 | + Collection<DiscoveryNode> discoveryNodes = clusterService.state().getNodes().getNodes().values(); |
| 90 | + request.setConcreteNodes(discoveryNodes.toArray(DiscoveryNode[]::new)); |
| 91 | + request.setIncludeDiscoveryNodes(false); |
| 92 | + Map<String, List<MockClusterStatsNodeRequest>> combinedSentRequest = performNodesInfoAction(request); |
| 93 | + |
| 94 | + assertNotNull(combinedSentRequest); |
| 95 | + combinedSentRequest.forEach((node, capturedRequestList) -> { |
| 96 | + assertNotNull(capturedRequestList); |
| 97 | + capturedRequestList.forEach(sentRequest -> { assertNull(sentRequest.getDiscoveryNodes()); }); |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + private Map<String, List<MockClusterStatsNodeRequest>> performNodesInfoAction(ClusterStatsRequest request) { |
| 102 | + TransportNodesAction action = getTestTransportClusterStatsAction(); |
| 103 | + PlainActionFuture<NodesStatsRequest> listener = new PlainActionFuture<>(); |
| 104 | + action.new AsyncAction(null, request, listener).start(); |
| 105 | + Map<String, List<CapturingTransport.CapturedRequest>> capturedRequests = transport.getCapturedRequestsByTargetNodeAndClear(); |
| 106 | + Map<String, List<MockClusterStatsNodeRequest>> combinedSentRequest = new HashMap<>(); |
| 107 | + |
| 108 | + capturedRequests.forEach((node, capturedRequestList) -> { |
| 109 | + List<MockClusterStatsNodeRequest> sentRequestList = new ArrayList<>(); |
| 110 | + |
| 111 | + capturedRequestList.forEach(preSentRequest -> { |
| 112 | + BytesStreamOutput out = new BytesStreamOutput(); |
| 113 | + try { |
| 114 | + TransportClusterStatsAction.ClusterStatsNodeRequest clusterStatsNodeRequestFromCoordinator = |
| 115 | + (TransportClusterStatsAction.ClusterStatsNodeRequest) preSentRequest.request; |
| 116 | + clusterStatsNodeRequestFromCoordinator.writeTo(out); |
| 117 | + StreamInput in = out.bytes().streamInput(); |
| 118 | + MockClusterStatsNodeRequest mockClusterStatsNodeRequest = new MockClusterStatsNodeRequest(in); |
| 119 | + sentRequestList.add(mockClusterStatsNodeRequest); |
| 120 | + } catch (IOException e) { |
| 121 | + throw new RuntimeException(e); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + combinedSentRequest.put(node, sentRequestList); |
| 126 | + }); |
| 127 | + |
| 128 | + return combinedSentRequest; |
| 129 | + } |
| 130 | + |
| 131 | + private TestTransportClusterStatsAction getTestTransportClusterStatsAction() { |
| 132 | + return new TestTransportClusterStatsAction( |
| 133 | + THREAD_POOL, |
| 134 | + clusterService, |
| 135 | + transportService, |
| 136 | + nodeService, |
| 137 | + indicesService, |
| 138 | + new ActionFilters(Collections.emptySet()) |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + private static class TestTransportClusterStatsAction extends TransportClusterStatsAction { |
| 143 | + public TestTransportClusterStatsAction( |
| 144 | + ThreadPool threadPool, |
| 145 | + ClusterService clusterService, |
| 146 | + TransportService transportService, |
| 147 | + NodeService nodeService, |
| 148 | + IndicesService indicesService, |
| 149 | + ActionFilters actionFilters |
| 150 | + ) { |
| 151 | + super(threadPool, clusterService, transportService, nodeService, indicesService, actionFilters); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private static class MockClusterStatsNodeRequest extends TransportClusterStatsAction.ClusterStatsNodeRequest { |
| 156 | + |
| 157 | + public MockClusterStatsNodeRequest(StreamInput in) throws IOException { |
| 158 | + super(in); |
| 159 | + } |
| 160 | + |
| 161 | + public DiscoveryNode[] getDiscoveryNodes() { |
| 162 | + return this.request.concreteNodes(); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments