Skip to content

Commit

Permalink
fix unit test
Browse files Browse the repository at this point in the history
Signed-off-by: kkewwei <[email protected]>
  • Loading branch information
kkewwei committed Jul 28, 2024
1 parent 5b0ecfa commit 3c76993
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void testdoCatRequest() throws IOException, ParseException {
try (RestClient restClient = getRestClient()) {
int nodesCount = restClient.getNodes().size();
for (int i = 0; i < 20; i++) {
Request nodesRequest = new Request("GET", "/_cat/nodes?timeout=" + randomInt(300) + "ms");
Request nodesRequest = new Request("GET", "/_cat/nodes?timeout=" + randomInt(80) + "ms");
try {
Response response = restClient.performRequest(nodesRequest);
assertEquals(SC_OK, response.getStatusLine().getStatusCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@ public RestChannelConsumer doCatRequest(final RestRequest request, final NodeCli
);
parseDeprecatedMasterTimeoutParameter(clusterStateRequest, request, deprecationLogger, getName());
final boolean fullId = request.paramAsBoolean("full_id", false);
long beginTime = System.nanoTime();
final long timeout = request.hasParam("timeout")
final long beginTimeNano = System.nanoTime();
final long timeoutNano = request.hasParam("timeout")
? TimeValue.parseTimeValue(request.param("timeout"), "timeout").nanos()
: Long.MAX_VALUE;
return channel -> client.admin().cluster().state(clusterStateRequest, new RestActionListener<>(channel) {
@Override
public void processResponse(final ClusterStateResponse clusterStateResponse) {
long leftTime = timeout - System.nanoTime() + beginTime;
if (leftTime < TIMEOUT_THRESHOLD_NANO) {
long leftTimeNano = timeoutNano - System.nanoTime() + beginTimeNano;
if (leftTimeNano < TIMEOUT_THRESHOLD_NANO) {
onFailure(
new OpenSearchTimeoutException(
"There is not enough time to obtain nodesInfo metric from the cluster manager:"
Expand All @@ -154,16 +154,16 @@ public void processResponse(final ClusterStateResponse clusterStateResponse) {
ConcurrentMap<String, FailedNodeException> failNodeStats = new ConcurrentHashMap<>(nodeIds.length);
AtomicInteger counter = new AtomicInteger();
for (String nodeId : nodeIds) {
NodesInfoRequest nodesInfoRequest = createNodesInfoRequest(timeout, leftTime, nodeId);
NodesInfoRequest nodesInfoRequest = createNodesInfoRequest(timeoutNano, leftTimeNano, nodeId);
nodesInfoRequest.setIncludeDiscoveryNodes(false);
client.admin().cluster().nodesInfo(nodesInfoRequest, new ActionListener<>() {
@Override
public void onResponse(NodesInfoResponse nodesInfoResponse) {
assert nodesInfoResponse.getNodes().size() + nodesInfoResponse.failures().size() == 1;
NodesStatsRequest nodesStatsRequest = checkAndCreateNodesStatsRequest(
nodesInfoResponse.failures(),
timeout,
beginTime,
timeoutNano,
beginTimeNano,
nodeId,
this::onFailure,
clusterStateResponse.getState().nodes().get(nodeId).getName()
Expand Down Expand Up @@ -223,10 +223,10 @@ private void onOperation() {
});
}

private NodesInfoRequest createNodesInfoRequest(long timeout, long leftTime, String nodeId) {
private NodesInfoRequest createNodesInfoRequest(long timeoutNano, long leftTimeNano, String nodeId) {
NodesInfoRequest nodesInfoRequest = new NodesInfoRequest();
if (timeout != Long.MAX_VALUE) {
nodesInfoRequest.timeout(TimeValue.timeValueMillis(leftTime));
if (timeoutNano != Long.MAX_VALUE) {
nodesInfoRequest.timeout(TimeValue.timeValueNanos(leftTimeNano));
}
nodesInfoRequest.clear()
.nodesIds(nodeId)
Expand All @@ -241,8 +241,8 @@ private NodesInfoRequest createNodesInfoRequest(long timeout, long leftTime, Str

private NodesStatsRequest checkAndCreateNodesStatsRequest(
List<FailedNodeException> failedNodeExceptions,
long timeout,
long beginTime,
long timeoutNano,
long beginTimeNano,
String nodeId,
Consumer<FailedNodeException> failedConsumer,
String nodeName
Expand All @@ -251,15 +251,15 @@ private NodesStatsRequest checkAndCreateNodesStatsRequest(
failedConsumer.accept(failedNodeExceptions.get(0));
return null;
}
long leftTime = timeout - System.nanoTime() + beginTime;
long leftTime = timeoutNano - System.nanoTime() + beginTimeNano;
if (leftTime < TIMEOUT_THRESHOLD_NANO) {
failedConsumer.accept(
new FailedNodeException(nodeId, "There is not enough time to obtain nodesStats metric from " + nodeName, null)
);
return null;
}
NodesStatsRequest nodesStatsRequest = new NodesStatsRequest();
if (timeout != Long.MAX_VALUE) {
if (timeoutNano != Long.MAX_VALUE) {
nodesStatsRequest.timeout(TimeValue.timeValueMillis(leftTime));
}
nodesStatsRequest.setIncludeDiscoveryNodes(false);
Expand Down

0 comments on commit 3c76993

Please sign in to comment.