org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus Java Examples

The following examples show how to use org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EsInstanceStore.java    From soundwave with Apache License 2.0 6 votes vote down vote up
@Override
public String checkStatus() throws Exception {

  final ClusterHealthResponse healthResponse = esClient
      .admin().cluster().prepareHealth().execute().actionGet();

  if (healthResponse.isTimedOut()) {
    return EsStatus.TIMEOUT.toString();

  } else if (ClusterHealthStatus.RED.equals(healthResponse.getStatus())) {
    logger.warn("Elastic search health status is reported RED");
    return EsStatus.ERROR.toString();

  } else if (ClusterHealthStatus.GREEN.equals(healthResponse.getStatus())) {
    return EsStatus.SUCCESS.toString();

  } else {
    logger.warn("Elastic search health status is unknown");
    return EsStatus.UNKNOWN.toString();
  }
}
 
Example #2
Source File: AuthService.java    From elasticsearch-auth with Apache License 2.0 6 votes vote down vote up
public void init(final ActionListener<Void> listener) {
    client.admin().cluster().prepareHealth().setWaitForYellowStatus()
            .execute(new ActionListener<ClusterHealthResponse>() {
                @Override
                public void onResponse(final ClusterHealthResponse response) {
                    if (response.getStatus() == ClusterHealthStatus.RED) {
                        listener.onFailure(new AuthException(
                                RestStatus.SERVICE_UNAVAILABLE,
                                "This cluster is not ready."));
                    } else {
                        createConstraintIndexIfNotExist(listener);
                    }
                }

                @Override
                public void onFailure(final Throwable e) {
                    listener.onFailure(e);
                }
            });
}
 
Example #3
Source File: AbstractElasticSearchTest.java    From camunda-bpm-elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
   * Waits for all relocating shards to become active and the cluster has reached the given health status
   * using the cluster health API.
   */
  public ClusterHealthStatus waitForRelocation(ClusterHealthStatus status) {
    ClusterHealthRequest request = Requests.clusterHealthRequest().waitForRelocatingShards(0);
    if (status != null) {
      request.waitForStatus(status);
    }
    ClusterHealthResponse actionGet = adminClient.cluster()
        .health(request).actionGet();
    if (actionGet.isTimedOut()) {
//      logger.info("waitForRelocation timed out (status={}), cluster state:\n{}\n{}", status, adminClient.cluster().prepareState().get().getState().prettyPrint(), adminClient.cluster().preparePendingClusterTasks().get().prettyPrint());
      assertThat("timed out waiting for relocation", actionGet.isTimedOut(), equalTo(false));
    }
    if (status != null) {
      assertThat(actionGet.getStatus(), equalTo(status));
    }
    return actionGet.getStatus();
  }
 
Example #4
Source File: AbstractSearchJUnit4SpringContextTests.java    From elasticsearch-tutorial with MIT License 5 votes vote down vote up
protected void checkIndexHealthStatus(String indexName)
{
    ClusterHealthRequest request = new ClusterHealthRequest(indexName);
    ClusterHealthStatus clusterHealthStatus = searchClientService.getClient().admin().cluster().health(request).actionGet().getStatus();

    assertTrue(clusterHealthStatus.equals(ClusterHealthStatus.GREEN));
}
 
Example #5
Source File: AbstractSearchJUnit4SpringContextTests.java    From searchanalytics-bigdata with MIT License 5 votes vote down vote up
protected void checkIndexHealthStatus(String indexName) {
	ClusterHealthRequest request = new ClusterHealthRequest(indexName);
	ClusterHealthStatus clusterHealthStatus = searchClientService
			.getClient().admin().cluster().health(request).actionGet()
			.getStatus();

	assertTrue(clusterHealthStatus.equals(ClusterHealthStatus.GREEN));
}
 
Example #6
Source File: ElasticSearchEngine.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isSearchEngineReady() {
	ClusterHealthStatus status = new ClusterHealthRequestBuilder(client, ClusterHealthAction.INSTANCE)
			.setIndices(configuration.getIndexName())
			.setTimeout(new TimeValue(configuration.getTimeoutMillis(), TimeUnit.MILLISECONDS))
			.request()
			.waitForStatus();
	return status != ClusterHealthStatus.RED;
}
 
Example #7
Source File: ElasticsearchEmitter.java    From amazon-kinesis-connectors with Apache License 2.0 5 votes vote down vote up
private void printClusterStatus() {
    ClusterHealthRequestBuilder healthRequestBuilder = elasticsearchClient.admin().cluster().prepareHealth();
    ClusterHealthResponse response = healthRequestBuilder.execute().actionGet();
    if (response.getStatus().equals(ClusterHealthStatus.RED)) {
        LOG.error("Cluster health is RED. Indexing ability will be limited");
    } else if (response.getStatus().equals(ClusterHealthStatus.YELLOW)) {
        LOG.warn("Cluster health is YELLOW.");
    } else if (response.getStatus().equals(ClusterHealthStatus.GREEN)) {
        LOG.info("Cluster health is GREEN.");
    }
}
 
Example #8
Source File: AbstractElasticSearchTest.java    From camunda-bpm-elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Waits for all relocating shards to become active using the cluster health API.
 */
public ClusterHealthStatus waitForRelocation() {
  return waitForRelocation(null);
}
 
Example #9
Source File: ElasticsearchEmitterTest.java    From amazon-kinesis-connectors with Apache License 2.0 4 votes vote down vote up
/**
 * Set the 2nd record in the passed in list to fail.
 * 
 * Assert that only 1 record is returned, and that it is equal to the 2nd object in the list.
 * 
 * @throws IOException
 */
@Test
public void testRecordFails() throws IOException {

    List<ElasticsearchObject> records = new ArrayList<ElasticsearchObject>();
    ElasticsearchObject r1 = createMockRecordAndSetExpectations("sample-index", "type", "1", "{\"name\":\"Mike\"}");
    records.add(r1);
    // this will be the failing record.
    ElasticsearchObject r2 = createMockRecordAndSetExpectations("sample-index", "type", "2",
            "{\"name\":\"Mike\",\"badJson\":\"forgotendingquote}");
    records.add(r2);
    ElasticsearchObject r3 = createMockRecordAndSetExpectations("sample-index", "type", "3", "{\"name\":\"Mike\"}");
    records.add(r3);

    // mock building and executing the request
    mockBuildingAndExecutingRequest(records);

    // Verify that there is a response for each record, and that each gets touched.
    BulkItemResponse[] responses = new BulkItemResponse[records.size()];
    for (int i = 0; i < responses.length; i++) {
        responses[i] = createMock(BulkItemResponse.class);
        // define behavior for a failing record
        if (i == 1) {
            expect(responses[i].isFailed()).andReturn(true);
            expect(responses[i].getFailureMessage()).andReturn("bad json error message");
            Failure failure = new Failure("index", "type", "id", "foo failure message", RestStatus.BAD_REQUEST);
            expect(responses[i].getFailure()).andReturn(failure);
        } else {
            expect(responses[i].isFailed()).andReturn(false);
        }
        replay(responses[i]);
    }

    // verify admin client gets used to check cluster status. this case, yellow
    expect(mockBulkResponse.getItems()).andReturn(responses);
    AdminClient mockAdminClient = createMock(AdminClient.class);
    expect(elasticsearchClientMock.admin()).andReturn(mockAdminClient);
    ClusterAdminClient mockClusterAdminClient = createMock(ClusterAdminClient.class);
    expect(mockAdminClient.cluster()).andReturn(mockClusterAdminClient);
    ClusterHealthRequestBuilder mockHealthRequestBuilder = createMock(ClusterHealthRequestBuilder.class);
    expect(mockClusterAdminClient.prepareHealth()).andReturn(mockHealthRequestBuilder);
    ListenableActionFuture mockHealthFuture = createMock(ListenableActionFuture.class);
    expect(mockHealthRequestBuilder.execute()).andReturn(mockHealthFuture);
    ClusterHealthResponse mockResponse = createMock(ClusterHealthResponse.class);
    expect(mockHealthFuture.actionGet()).andReturn(mockResponse);
    expect(mockResponse.getStatus()).andReturn(ClusterHealthStatus.YELLOW);
    expectLastCall().times(2);

    replay(elasticsearchClientMock, r1, r2, r3, buffer, mockBulkBuilder, mockIndexBuilder, mockFuture,
            mockBulkResponse, mockAdminClient, mockClusterAdminClient, mockHealthRequestBuilder, mockHealthFuture,
            mockResponse);

    List<ElasticsearchObject> failures = emitter.emit(buffer);
    assertTrue(failures.size() == 1);
    // the emitter should return the exact object that failed
    assertEquals(failures.get(0), records.get(1));

    verify(elasticsearchClientMock, r1, r2, r3, buffer, mockBulkBuilder, mockIndexBuilder, mockFuture,
            mockBulkResponse, mockAdminClient, mockClusterAdminClient, mockHealthRequestBuilder, mockHealthFuture,
            mockResponse);
    verifyRecords(records);
    verifyResponses(responses);
}