Cluster Health API

edit

The Cluster Health API allows getting cluster health.

Cluster Health Request

edit

A ClusterHealthRequest:

ClusterHealthRequest request = new ClusterHealthRequest();

There are no required parameters. By default, the client will check all indices and will not wait for any events.

Indices

edit

Indices which should be checked can be passed in the constructor:

ClusterHealthRequest request = new ClusterHealthRequest("index1", "index2");

Or using the corresponding setter method:

ClusterHealthRequest request = new ClusterHealthRequest();
request.indices("index1", "index2");

Other parameters

edit

Other parameters can be passed only through setter methods:

request.timeout(TimeValue.timeValueSeconds(50)); 
request.timeout("50s"); 

Timeout for the request as a TimeValue. Defaults to 30 seconds

As a String

request.masterNodeTimeout(TimeValue.timeValueSeconds(20)); 
request.masterNodeTimeout("20s"); 

Timeout to connect to the master node as a TimeValue. Defaults to the same as timeout

As a String

request.waitForStatus(ClusterHealthStatus.YELLOW); 
request.waitForYellowStatus(); 

The status to wait (e.g. green, yellow, or red). Accepts a ClusterHealthStatus value.

Using predefined method

request.waitForEvents(Priority.NORMAL); 

The priority of the events to wait for. Accepts a Priority value.

request.level(ClusterHealthRequest.Level.SHARDS); 

The level of detail of the returned health information. Accepts a ClusterHealthRequest.Level value.

request.waitForNoRelocatingShards(true); 

Wait for 0 relocating shards. Defaults to false

request.waitForNoInitializingShards(true); 

Wait for 0 initializing shards. Defaults to false

request.waitForNodes("2"); 
request.waitForNodes(">=2"); 
request.waitForNodes("le(2)"); 

Wait for N nodes in the cluster. Defaults to 0

Using >=N, <=N, >N and <N notation

Using ge(N), le(N), gt(N), lt(N) notation

request.waitForActiveShards(ActiveShardCount.ALL); 
request.waitForActiveShards(1); 

Wait for all shards to be active in the cluster

Wait for N shards to be active in the cluster

request.local(true); 

Non-master node can be used for this request. Defaults to false

Synchronous Execution

edit

When executing a ClusterHealthRequest in the following manner, the client waits for the ClusterHealthResponse to be returned before continuing with code execution:

ClusterHealthResponse response = client.cluster().health(request, RequestOptions.DEFAULT);

Synchronous calls may throw an IOException in case of either failing to parse the REST response in the high-level REST client, the request times out or similar cases where there is no response coming back from the server.

In cases where the server returns a 4xx or 5xx error code, the high-level client tries to parse the response body error details instead and then throws a generic ElasticsearchException and adds the original ResponseException as a suppressed exception to it.

Asynchronous Execution

edit

Executing a ClusterHealthRequest can also be done in an asynchronous fashion so that the client can return directly. Users need to specify how the response or potential failures will be handled by passing the request and a listener to the asynchronous health method:

client.cluster().healthAsync(request, RequestOptions.DEFAULT, listener); 

The ClusterHealthRequest to execute and the ActionListener to use when the execution completes

The asynchronous method does not block and returns immediately. Once it is completed the ActionListener is called back using the onResponse method if the execution successfully completed or using the onFailure method if it failed. Failure scenarios and expected exceptions are the same as in the synchronous execution case.

A typical listener for health looks like:

ActionListener<ClusterHealthResponse> listener =
    new ActionListener<ClusterHealthResponse>() {
        @Override
        public void onResponse(ClusterHealthResponse response) {
            
        }

        @Override
        public void onFailure(Exception e) {
            
        }
    };

Called when the execution is successfully completed.

Called when the whole ClusterHealthRequest fails.

Cluster Health Response

edit

The returned ClusterHealthResponse contains the next information about the cluster:

String clusterName = response.getClusterName(); 
ClusterHealthStatus status = response.getStatus(); 

Name of the cluster

Cluster status (green, yellow or red)

boolean timedOut = response.isTimedOut(); 
RestStatus restStatus = response.status(); 

Whether request was timed out while processing

Status of the request (OK or REQUEST_TIMEOUT). Other errors will be thrown as exceptions

int numberOfNodes = response.getNumberOfNodes(); 
int numberOfDataNodes = response.getNumberOfDataNodes(); 

Number of nodes in the cluster

Number of data nodes in the cluster

int activeShards = response.getActiveShards(); 
int activePrimaryShards = response.getActivePrimaryShards(); 
int relocatingShards = response.getRelocatingShards(); 
int initializingShards = response.getInitializingShards(); 
int unassignedShards = response.getUnassignedShards(); 
int delayedUnassignedShards = response.getDelayedUnassignedShards(); 
double activeShardsPercent = response.getActiveShardsPercent(); 

Number of active shards

Number of primary active shards

Number of relocating shards

Number of initializing shards

Number of unassigned shards

Number of unassigned shards that are currently being delayed

Percent of active shards

TimeValue taskMaxWaitingTime = response.getTaskMaxWaitingTime(); 
int numberOfPendingTasks = response.getNumberOfPendingTasks(); 
int numberOfInFlightFetch = response.getNumberOfInFlightFetch(); 

Maximum wait time of all tasks in the queue

Number of currently pending tasks

Number of async fetches that are currently ongoing

Map<String, ClusterIndexHealth> indices = response.getIndices(); 

Detailed information about indices in the cluster

ClusterIndexHealth index = indices.get("index"); 
ClusterHealthStatus indexStatus = index.getStatus();
int numberOfShards = index.getNumberOfShards();
int numberOfReplicas = index.getNumberOfReplicas();
int activeShards = index.getActiveShards();
int activePrimaryShards = index.getActivePrimaryShards();
int initializingShards = index.getInitializingShards();
int relocatingShards = index.getRelocatingShards();
int unassignedShards = index.getUnassignedShards();

Detailed information about a specific index

Map<Integer, ClusterShardHealth> shards = index.getShards(); 
ClusterShardHealth shardHealth = shards.get(0);
int shardId = shardHealth.getShardId();
ClusterHealthStatus shardStatus = shardHealth.getStatus();
int active = shardHealth.getActiveShards();
int initializing = shardHealth.getInitializingShards();
int unassigned = shardHealth.getUnassignedShards();
int relocating = shardHealth.getRelocatingShards();
boolean primaryActive = shardHealth.isPrimaryActive();

Detailed information about a specific shard