Skip to content

Commit

Permalink
optimize _cat/nodes api
Browse files Browse the repository at this point in the history
  • Loading branch information
kkewwei committed Jul 26, 2024
1 parent 074115a commit 46509de
Showing 1 changed file with 8 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
* @opensearch.api
*/
public class RestNodesAction extends AbstractCatAction {
public static final long TIMEOUT_THRESHOLD_MILLIS = 5;
public static final long TIMEOUT_THRESHOLD_NANO = 5_000_000;
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestNodesAction.class);
static final String LOCAL_DEPRECATED_MESSAGE = "Deprecated parameter [local] used. This parameter does not cause this API to act "
+ "locally, and should not be used. It will be unsupported in version 8.0.";
Expand Down Expand Up @@ -132,19 +132,18 @@ public RestChannelConsumer doCatRequest(final RestRequest request, final NodeCli
);
parseDeprecatedMasterTimeoutParameter(clusterStateRequest, request, deprecationLogger, getName());
final boolean fullId = request.paramAsBoolean("full_id", false);
ThreadPool threadPool = client.admin().cluster().threadPool();
long beginTime = threadPool.relativeTimeInMillis();
long beginTime = System.nanoTime();
final long timeout = request.hasParam("timeout")
? TimeValue.parseTimeValue(request.param("timeout"), "timeout").millis()
: Long.MAX_VALUE;
return channel -> client.admin().cluster().state(clusterStateRequest, new RestActionListener<>(channel) {
@Override
public void processResponse(final ClusterStateResponse clusterStateResponse) {
long leftTime = timeout - threadPool.relativeTimeInMillis() + beginTime;
if (leftTime < TIMEOUT_THRESHOLD_MILLIS) {
long leftTime = timeout - System.nanoTime() + beginTime;
if (leftTime < TIMEOUT_THRESHOLD_NANO) {
onFailure(
new OpenSearchTimeoutException(
"costs too much time to get ClusterState from the cluster manager:"
"There is not enough time to obtain nodesInfo metric from the cluster manager:"
+ clusterStateResponse.getState().nodes().getMasterNode().getName()
)
);
Expand All @@ -169,7 +168,6 @@ public void onResponse(NodesInfoResponse nodesInfoResponse) {
beginTime,
nodeId,
this::onFailure,
threadPool::relativeTimeInMillis,
clusterStateResponse.getState().nodes().get(nodeId).getName()
);
if (nodesStatsRequest == null) {
Expand Down Expand Up @@ -246,17 +244,16 @@ private NodesStatsRequest checkAndCreateNodesStatsRequest(
long beginTime,
String nodeId,
Consumer<FailedNodeException> failedConsumer,
Supplier<Long> currentTimeSupplier,
String nodeName
) {
if (failedNodeExceptions.isEmpty() == false) {
failedConsumer.accept(failedNodeExceptions.get(0));
return null;
}
long leftTime = timeout - currentTimeSupplier.get() + beginTime;
if (leftTime < TIMEOUT_THRESHOLD_MILLIS) {
long leftTime = timeout - System.nanoTime() + beginTime;
if (leftTime < TIMEOUT_THRESHOLD_NANO) {
failedConsumer.accept(
new FailedNodeException(nodeId, "There is not enough time to obtain nodesInfo metric from " + nodeName, null)
new FailedNodeException(nodeId, "There is not enough time to obtain nodesStats metric from " + nodeName, null)
);
return null;
}
Expand Down

0 comments on commit 46509de

Please sign in to comment.