-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
Signed-off-by: kkewwei <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* 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.http; | ||
|
||
import org.apache.hc.core5.http.ParseException; | ||
import org.apache.hc.core5.http.io.entity.EntityUtils; | ||
import org.opensearch.client.Request; | ||
import org.opensearch.client.Response; | ||
import org.opensearch.client.RestClient; | ||
import org.opensearch.test.OpenSearchIntegTestCase.ClusterScope; | ||
import org.opensearch.test.OpenSearchIntegTestCase.Scope; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.apache.hc.core5.http.HttpStatus.SC_OK; | ||
import static org.hamcrest.Matchers.containsString; | ||
|
||
@ClusterScope(scope = Scope.SUITE, supportsDedicatedMasters = false, numDataNodes = 5, numClientNodes = 0) | ||
public class HttpCatIT extends HttpSmokeTestCase { | ||
|
||
public void testdoCatRequest() throws IOException { | ||
try (RestClient restClient = getRestClient()) { | ||
int nodesCount = restClient.getNodes().size(); | ||
assertEquals(5, nodesCount); | ||
|
||
// to make sure the timeout is working | ||
for (int i = 0; i < 5; i++) { | ||
sendRequest(restClient, 30, nodesCount); | ||
} | ||
|
||
// no timeout | ||
for (int i = 0; i < 5; i++) { | ||
sendRequest(restClient, -1, nodesCount); | ||
} | ||
|
||
for (int i = 1; i < 5; i++) { | ||
long timeout = randomInt(300); | ||
sendRequest(restClient, timeout, nodesCount); | ||
} | ||
} | ||
} | ||
|
||
private void sendRequest(RestClient restClient, long timeout, int nodesCount) { | ||
Request nodesRequest; | ||
if (timeout < 0) { | ||
nodesRequest = new Request("GET", "/_cat/nodes"); | ||
} else { | ||
nodesRequest = new Request("GET", "/_cat/nodes?timeout=" + timeout + "ms"); | ||
} | ||
try { | ||
Response response = restClient.performRequest(nodesRequest); | ||
assertEquals(SC_OK, response.getStatusLine().getStatusCode()); | ||
String result = EntityUtils.toString(response.getEntity()); | ||
String[] NodeInfos = result.split("\n"); | ||
assertEquals(nodesCount, NodeInfos.length); | ||
} catch (IOException | ParseException e) { | ||
// it means that it costs too long to get ClusterState from the master. | ||
assertThat(e.getMessage(), containsString("There is not enough time to obtain nodesInfo metric from the cluster manager")); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* 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.action.admin.cluster.node.Nodes; | ||
|
||
import org.opensearch.action.ActionType; | ||
|
||
/** | ||
* Transport action for cat nodes | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class CatNodesAction extends ActionType<CatNodesResponse> { | ||
public static final CatNodesAction INSTANCE = new CatNodesAction(); | ||
public static final String NAME = "cluster:monitor/nodes/cat"; | ||
|
||
public CatNodesAction() { | ||
super(NAME, CatNodesResponse::new); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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.action.admin.cluster.node.Nodes; | ||
|
||
import org.opensearch.action.ActionRequestValidationException; | ||
import org.opensearch.action.support.clustermanager.ClusterManagerNodeReadRequest; | ||
import org.opensearch.common.unit.TimeValue; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.tasks.TaskId; | ||
import org.opensearch.rest.action.admin.cluster.ClusterAdminTask; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
/** | ||
* A request of _cat/nodes. | ||
* | ||
* @opensearch.api | ||
*/ | ||
public class CatNodesRequest extends ClusterManagerNodeReadRequest<CatNodesRequest> { | ||
|
||
private TimeValue cancelAfterTimeInterval; | ||
private long timeout = -1; | ||
|
||
public CatNodesRequest() {} | ||
|
||
public CatNodesRequest(StreamInput in) throws IOException { | ||
super(in); | ||
} | ||
Check warning on line 35 in server/src/main/java/org/opensearch/action/admin/cluster/node/Nodes/CatNodesRequest.java Codecov / codecov/patchserver/src/main/java/org/opensearch/action/admin/cluster/node/Nodes/CatNodesRequest.java#L34-L35
|
||
|
||
public void setCancelAfterTimeInterval(TimeValue timeout) { | ||
this.cancelAfterTimeInterval = timeout; | ||
} | ||
|
||
public TimeValue getCancelAfterTimeInterval() { | ||
return cancelAfterTimeInterval; | ||
Check warning on line 42 in server/src/main/java/org/opensearch/action/admin/cluster/node/Nodes/CatNodesRequest.java Codecov / codecov/patchserver/src/main/java/org/opensearch/action/admin/cluster/node/Nodes/CatNodesRequest.java#L42
|
||
} | ||
|
||
public void setTimeout(long timeout) { | ||
this.timeout = timeout; | ||
} | ||
Check warning on line 47 in server/src/main/java/org/opensearch/action/admin/cluster/node/Nodes/CatNodesRequest.java Codecov / codecov/patchserver/src/main/java/org/opensearch/action/admin/cluster/node/Nodes/CatNodesRequest.java#L46-L47
|
||
|
||
public long getTimeout() { | ||
return timeout; | ||
Check warning on line 50 in server/src/main/java/org/opensearch/action/admin/cluster/node/Nodes/CatNodesRequest.java Codecov / codecov/patchserver/src/main/java/org/opensearch/action/admin/cluster/node/Nodes/CatNodesRequest.java#L50
|
||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
return null; | ||
Check warning on line 55 in server/src/main/java/org/opensearch/action/admin/cluster/node/Nodes/CatNodesRequest.java Codecov / codecov/patchserver/src/main/java/org/opensearch/action/admin/cluster/node/Nodes/CatNodesRequest.java#L55
|
||
} | ||
|
||
@Override | ||
public ClusterAdminTask createTask(long id, String type, String action, TaskId parentTaskId, Map<String, String> headers) { | ||
return new ClusterAdminTask(id, type, action, parentTaskId, headers, this.cancelAfterTimeInterval); | ||
Check warning on line 60 in server/src/main/java/org/opensearch/action/admin/cluster/node/Nodes/CatNodesRequest.java Codecov / codecov/patchserver/src/main/java/org/opensearch/action/admin/cluster/node/Nodes/CatNodesRequest.java#L60
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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.action.admin.cluster.node.Nodes; | ||
|
||
import org.opensearch.action.admin.cluster.node.info.NodesInfoResponse; | ||
import org.opensearch.action.admin.cluster.node.stats.NodesStatsResponse; | ||
import org.opensearch.action.admin.cluster.state.ClusterStateResponse; | ||
import org.opensearch.core.action.ActionResponse; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* A response of a cat shards request. | ||
* | ||
* @opensearch.api | ||
*/ | ||
public class CatNodesResponse extends ActionResponse { | ||
|
||
private ClusterStateResponse clusterStateResponse; | ||
private NodesInfoResponse nodesInfoResponse; | ||
private NodesStatsResponse nodesStatsResponse; | ||
|
||
public CatNodesResponse( | ||
ClusterStateResponse clusterStateResponse, | ||
NodesInfoResponse nodesInfoResponse, | ||
NodesStatsResponse nodesStatsResponse | ||
) { | ||
this.clusterStateResponse = clusterStateResponse; | ||
this.nodesInfoResponse = nodesInfoResponse; | ||
this.nodesStatsResponse = nodesStatsResponse; | ||
} | ||
|
||
public CatNodesResponse(StreamInput in) throws IOException { | ||
super(in); | ||
} | ||
Check warning on line 43 in server/src/main/java/org/opensearch/action/admin/cluster/node/Nodes/CatNodesResponse.java Codecov / codecov/patchserver/src/main/java/org/opensearch/action/admin/cluster/node/Nodes/CatNodesResponse.java#L42-L43
|
||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
clusterStateResponse.writeTo(out); | ||
nodesInfoResponse.writeTo(out); | ||
nodesStatsResponse.writeTo(out); | ||
} | ||
Check warning on line 50 in server/src/main/java/org/opensearch/action/admin/cluster/node/Nodes/CatNodesResponse.java Codecov / codecov/patchserver/src/main/java/org/opensearch/action/admin/cluster/node/Nodes/CatNodesResponse.java#L47-L50
|
||
|
||
public NodesStatsResponse getNodesStatsResponse() { | ||
return nodesStatsResponse; | ||
} | ||
|
||
public NodesInfoResponse getNodesInfoResponse() { | ||
return nodesInfoResponse; | ||
} | ||
|
||
public ClusterStateResponse getClusterStateResponse() { | ||
return clusterStateResponse; | ||
} | ||
} |