Skip to content

Commit

Permalink
fix naming and simplifying code
Browse files Browse the repository at this point in the history
Signed-off-by: Pranshu Shukla <[email protected]>
  • Loading branch information
Pranshu-S committed Jul 18, 2024
1 parent b53c772 commit 5fffc47
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public abstract class BaseNodesRequest<Request extends BaseNodesRequest<Request>
*
* Setting default behavior as `true` but can be explicitly changed in requests that do not require.
*/
private boolean populateDiscoveryNodesInTransportRequest = true;
private boolean retainDiscoveryNodes = true;
private final TimeValue DEFAULT_TIMEOUT_SECS = TimeValue.timeValueSeconds(30);

private TimeValue timeout;
Expand Down Expand Up @@ -127,12 +127,12 @@ public void setConcreteNodes(DiscoveryNode[] concreteNodes) {
this.concreteNodes = concreteNodes;
}

public void populateDiscoveryNodesInTransportRequest(boolean value) {
populateDiscoveryNodesInTransportRequest = value;
public void retainDiscoveryNodes(boolean value) {
retainDiscoveryNodes = value;
}

public boolean populateDiscoveryNodesInTransportRequest() {
return populateDiscoveryNodesInTransportRequest;
public boolean retainDiscoveryNodes() {
return retainDiscoveryNodes;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,32 +246,20 @@ class AsyncAction {
this.task = task;
this.request = request;
this.listener = listener;

// Check if concrete nodes are already available
if (request.concreteNodes() != null) {
this.responses = new AtomicReferenceArray<>(request.concreteNodes().length);

if (request.populateDiscoveryNodesInTransportRequest()) {
this.concreteNodes = null;
} else {
this.concreteNodes = request.concreteNodes();
request.setConcreteNodes(null);
assert request.concreteNodes() == null;
}
return;
}

// Check if we want to populate the DiscoveryNodes in the transport Request and accordingly backfill the
// concrete nodes.
if (request.populateDiscoveryNodesInTransportRequest()) {
if (request.concreteNodes() == null) {
resolveRequest(request, clusterService.state());
assert request.concreteNodes() != null;
this.responses = new AtomicReferenceArray<>(request.concreteNodes().length);
this.concreteNodes = null;
}
this.responses = new AtomicReferenceArray<>(request.concreteNodes().length);

if (request.retainDiscoveryNodes() == false) {
// We transfer the ownership of discovery nodes to route the request to into the AsyncAction class.
// This reduces the payload of the request and improves the number of concrete nodes in the memory
this.concreteNodes = request.concreteNodes();
request.setConcreteNodes(null);
} else {
this.concreteNodes = getConcreteNodes(request, clusterService.state());
assert request.concreteNodes() == null;
this.responses = new AtomicReferenceArray<>(concreteNodes.length);
// initializing it separately as we keep the `concreteNodes` as final since we want it to be immutable.
this.concreteNodes = null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public String getName() {
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
ClusterStatsRequest clusterStatsRequest = new ClusterStatsRequest().nodesIds(request.paramAsStringArray("nodeId", null));
clusterStatsRequest.timeout(request.param("timeout"));
clusterStatsRequest.populateDiscoveryNodesInTransportRequest(false);
clusterStatsRequest.retainDiscoveryNodes(false);
return channel -> client.admin().cluster().clusterStats(clusterStatsRequest, new NodesResponseRestListener<>(channel));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
final NodesInfoRequest nodesInfoRequest = prepareRequest(request);
nodesInfoRequest.timeout(request.param("timeout"));
settingsFilter.addFilterSettingParams(request);
nodesInfoRequest.populateDiscoveryNodesInTransportRequest(false);
nodesInfoRequest.retainDiscoveryNodes(false);
return channel -> client.admin().cluster().nodesInfo(nodesInfoRequest, new NodesResponseRestListener<>(channel));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
// If no levels are passed in this results in an empty array.
String[] levels = Strings.splitStringByCommaToArray(request.param("level"));
nodesStatsRequest.indices().setLevels(levels);
nodesStatsRequest.populateDiscoveryNodesInTransportRequest(false);
nodesStatsRequest.retainDiscoveryNodes(false);

return channel -> client.admin().cluster().nodesStats(nodesStatsRequest, new NodesResponseRestListener<>(channel));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public RestChannelConsumer doCatRequest(final RestRequest request, final NodeCli
public void processResponse(final ClusterStateResponse clusterStateResponse) {
NodesInfoRequest nodesInfoRequest = new NodesInfoRequest();
nodesInfoRequest.timeout(request.param("timeout"));
nodesInfoRequest.populateDiscoveryNodesInTransportRequest(false);
nodesInfoRequest.retainDiscoveryNodes(false);
nodesInfoRequest.clear()
.addMetrics(
NodesInfoRequest.Metric.JVM.metricName(),
Expand All @@ -138,7 +138,7 @@ public void processResponse(final ClusterStateResponse clusterStateResponse) {
public void processResponse(final NodesInfoResponse nodesInfoResponse) {
NodesStatsRequest nodesStatsRequest = new NodesStatsRequest();
nodesStatsRequest.timeout(request.param("timeout"));
nodesStatsRequest.populateDiscoveryNodesInTransportRequest(false);
nodesStatsRequest.retainDiscoveryNodes(false);
nodesStatsRequest.clear()
.indices(true)
.addMetrics(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.opensearch.threadpool.TestThreadPool;
import org.junit.After;

import java.io.IOException;
import java.util.Collections;

public class RestStatsActionTests extends OpenSearchTestCase {
Expand All @@ -31,18 +30,30 @@ public void terminateThreadPool() {
terminate(threadPool);
}

public void testClusterStatsActionPrepareRequestNoError() throws IOException {
public void testClusterStatsActionPrepareRequestNoError() {
RestClusterStatsAction action = new RestClusterStatsAction();
action.prepareRequest(new FakeRestRequest(), client);
try {
action.prepareRequest(new FakeRestRequest(), client);
} catch (Throwable t) {
fail(t.getMessage());
}
}

public void testNodesStatsActionPrepareRequestNoError() throws IOException {
public void testNodesStatsActionPrepareRequestNoError() {
RestNodesStatsAction action = new RestNodesStatsAction();
action.prepareRequest(new FakeRestRequest(), client);
try {
action.prepareRequest(new FakeRestRequest(), client);
} catch (Throwable t) {
fail(t.getMessage());
}
}

public void testNodesInfoActionPrepareRequestNoError() throws IOException {
public void testNodesInfoActionPrepareRequestNoError() {
RestNodesInfoAction action = new RestNodesInfoAction(new SettingsFilter(Collections.singleton("foo.filtered")));
action.prepareRequest(new FakeRestRequest(), client);
try {
action.prepareRequest(new FakeRestRequest(), client);
} catch (Throwable t) {
fail(t.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public class TransportClusterStatsActionTests extends TransportNodesActionTests
* By default, we send discovery nodes list to each request that is sent across from the coordinator node. This
* behavior is asserted in this test.
*/
public void testClusterStatsActionWithDiscoveryNodesListPopulated() {
public void testClusterStatsActionWithRetentionOfDiscoveryNodesList() {
ClusterStatsRequest request = new ClusterStatsRequest();
request.populateDiscoveryNodesInTransportRequest(true);
request.retainDiscoveryNodes(true);
Map<String, List<MockClusterStatsNodeRequest>> combinedSentRequest = performNodesInfoAction(request);

assertNotNull(combinedSentRequest);
Expand All @@ -52,7 +52,7 @@ public void testClusterStatsActionWithDiscoveryNodesListPopulated() {
});
}

public void testClusterStatsActionWithDiscoveryNodesListInRestRequestPopulated() {
public void testClusterStatsActionWithPreFilledConcreteNodesAndWithRetentionOfDiscoveryNodesList() {
ClusterStatsRequest request = new ClusterStatsRequest();
Collection<DiscoveryNode> discoveryNodes = clusterService.state().getNodes().getNodes().values();
request.setConcreteNodes(discoveryNodes.toArray(DiscoveryNode[]::new));
Expand All @@ -72,9 +72,9 @@ public void testClusterStatsActionWithDiscoveryNodesListInRestRequestPopulated()
* In the optimized ClusterStats Request, we do not send the DiscoveryNodes List to each node. This behavior is
* asserted in this test.
*/
public void testClusterStatsActionWithDiscoveryNodesListNotPopulated() {
public void testClusterStatsActionWithoutRetentionOfDiscoveryNodesList() {
ClusterStatsRequest request = new ClusterStatsRequest();
request.populateDiscoveryNodesInTransportRequest(false);
request.retainDiscoveryNodes(false);
Map<String, List<MockClusterStatsNodeRequest>> combinedSentRequest = performNodesInfoAction(request);

assertNotNull(combinedSentRequest);
Expand All @@ -84,11 +84,11 @@ public void testClusterStatsActionWithDiscoveryNodesListNotPopulated() {
});
}

public void testClusterStatsWithDiscoveryNodesListInRestRequestNotPopulated() {
public void testClusterStatsActionWithPreFilledConcreteNodesAndWithoutRetentionOfDiscoveryNodesList() {
ClusterStatsRequest request = new ClusterStatsRequest();
Collection<DiscoveryNode> discoveryNodes = clusterService.state().getNodes().getNodes().values();
request.setConcreteNodes(discoveryNodes.toArray(DiscoveryNode[]::new));
request.populateDiscoveryNodesInTransportRequest(false);
request.retainDiscoveryNodes(false);
Map<String, List<MockClusterStatsNodeRequest>> combinedSentRequest = performNodesInfoAction(request);

assertNotNull(combinedSentRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public class TransportNodesInfoActionTests extends TransportNodesActionTests {
* By default, we send discovery nodes list to each request that is sent across from the coordinator node. This
* behavior is asserted in this test.
*/
public void testNodesInfoActionWithDiscoveryNodesListPopulated() {
public void testNodesInfoActionWithRetentionOfDiscoveryNodesList() {
NodesInfoRequest request = new NodesInfoRequest();
request.populateDiscoveryNodesInTransportRequest(true);
request.retainDiscoveryNodes(true);
Map<String, List<MockNodesInfoRequest>> combinedSentRequest = performNodesInfoAction(request);

assertNotNull(combinedSentRequest);
Expand All @@ -54,9 +54,9 @@ public void testNodesInfoActionWithDiscoveryNodesListPopulated() {
* In the optimized ClusterStats Request, we do not send the DiscoveryNodes List to each node. This behavior is
* asserted in this test.
*/
public void testNodesInfoActionWithDiscoveryNodesListNotPopulated() {
public void testNodesInfoActionWithoutRetentionOfDiscoveryNodesList() {
NodesInfoRequest request = new NodesInfoRequest();
request.populateDiscoveryNodesInTransportRequest(false);
request.retainDiscoveryNodes(false);
Map<String, List<MockNodesInfoRequest>> combinedSentRequest = performNodesInfoAction(request);

assertNotNull(combinedSentRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public class TransportNodesStatsActionTests extends TransportNodesActionTests {
* By default, we send discovery nodes list to each request that is sent across from the coordinator node. This
* behavior is asserted in this test.
*/
public void testNodesStatsActionWithDiscoveryNodesListPopulated() {
public void testNodesStatsActionWithRetentionOfDiscoveryNodesList() {
NodesStatsRequest request = new NodesStatsRequest();
request.populateDiscoveryNodesInTransportRequest(true);
request.retainDiscoveryNodes(true);
Map<String, List<MockNodeStatsRequest>> combinedSentRequest = performNodesStatsAction(request);

assertNotNull(combinedSentRequest);
Expand All @@ -53,9 +53,9 @@ public void testNodesStatsActionWithDiscoveryNodesListPopulated() {
* By default, we send discovery nodes list to each request that is sent across from the coordinator node. This
* behavior is asserted in this test.
*/
public void testNodesStatsActionWithDiscoveryNodesListNotPopulated() {
public void testNodesStatsActionWithoutRetentionOfDiscoveryNodesList() {
NodesStatsRequest request = new NodesStatsRequest();
request.populateDiscoveryNodesInTransportRequest(false);
request.retainDiscoveryNodes(false);
Map<String, List<MockNodeStatsRequest>> combinedSentRequest = performNodesStatsAction(request);

assertNotNull(combinedSentRequest);
Expand Down

0 comments on commit 5fffc47

Please sign in to comment.