Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize cluster health request #3

Draft
wants to merge 1 commit into
base: 7.17
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.util.CollectionUtils;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.IndexNotFoundException;
import org.elasticsearch.node.NodeClosedException;
Expand Down Expand Up @@ -211,9 +212,9 @@ private void executeHealth(
return;
}

final Predicate<ClusterState> validationPredicate = newState -> validateRequest(request, newState, waitCount);
if (validationPredicate.test(currentState)) {
listener.onResponse(getResponse(request, currentState, waitCount, TimeoutState.OK));
ClusterHealthResponse validResponse = validateRequestAndGetResponse(request, currentState, waitCount);
if (validResponse != null) {
listener.onResponse(validResponse);
} else {
final ClusterStateObserver observer = new ClusterStateObserver(
currentState,
Expand All @@ -238,6 +239,7 @@ public void onTimeout(TimeValue timeout) {
listener.onResponse(getResponse(request, observer.setAndGetObservedState(), waitCount, TimeoutState.TIMED_OUT));
}
};
Predicate<ClusterState> validationPredicate = newState -> validateRequest(request, newState, waitCount);
observer.waitForNextChange(stateListener, validationPredicate, request.timeout());
}
}
Expand Down Expand Up @@ -276,6 +278,22 @@ private boolean validateRequest(final ClusterHealthRequest request, ClusterState
return prepareResponse(request, response, clusterState, indexNameExpressionResolver) == waitCount;
}

@Nullable
private ClusterHealthResponse validateRequestAndGetResponse(final ClusterHealthRequest request, ClusterState clusterState, final int waitCount) {
ClusterHealthResponse response = clusterHealth(
request,
clusterState,
clusterService.getMasterService().numberOfPendingTasks(),
allocationService.getNumberOfInFlightFetches(),
clusterService.getMasterService().getMaxTaskWaitTime()
);
if (prepareResponse(request, response, clusterState, indexNameExpressionResolver) == waitCount) {
return response;
} else {
return null;
}
}

private enum TimeoutState {
OK,
TIMED_OUT,
Expand Down