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

The following examples show how to use org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse. 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: DecommissioningService.java    From crate with Apache License 2.0 6 votes vote down vote up
private CompletableFuture<ClusterHealthResponse> clusterHealthGet() {
    if (dataAvailability == DataAvailability.NONE) {
        return CompletableFuture.completedFuture(null);
    }

    // NOTE: it waits for ALL relocating shards, not just those that involve THIS node.
    ClusterHealthRequest request = new ClusterHealthRequest()
        .waitForNoRelocatingShards(true)
        .waitForEvents(Priority.LANGUID)
        .timeout(gracefulStopTimeout);

    if (dataAvailability == DataAvailability.FULL) {
        request = request.waitForGreenStatus();
    } else {
        request = request.waitForYellowStatus();
    }

    FutureActionListener<ClusterHealthResponse, ClusterHealthResponse> listener = FutureActionListener.newInstance();
    healthAction.execute(request, listener);
    return listener;
}
 
Example #2
Source File: RestHealthAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private Table buildTable(final ClusterHealthResponse health, final RestRequest request) {
    long time = System.currentTimeMillis();
    Table t = getTableWithHeader(request);
    t.startRow();
    t.addCell(TimeUnit.SECONDS.convert(time, TimeUnit.MILLISECONDS));
    t.addCell(dateFormat.print(time));
    t.addCell(health.getClusterName());
    t.addCell(health.getStatus().name().toLowerCase(Locale.ROOT));
    t.addCell(health.getNumberOfNodes());
    t.addCell(health.getNumberOfDataNodes());
    t.addCell(health.getActiveShards());
    t.addCell(health.getActivePrimaryShards());
    t.addCell(health.getRelocatingShards());
    t.addCell(health.getInitializingShards());
    t.addCell(health.getUnassignedShards());
    t.addCell(health.getNumberOfPendingTasks());
    t.addCell(health.getTaskMaxWaitingTime().millis() == 0 ? "-" : health.getTaskMaxWaitingTime());
    t.addCell(String.format(Locale.ROOT, "%1.1f%%", health.getActiveShardsPercent()));
    t.endRow();
    return t;
}
 
Example #3
Source File: PrometheusMetricsCollector.java    From elasticsearch-prometheus-exporter with Apache License 2.0 6 votes vote down vote up
private void updateClusterMetrics(ClusterHealthResponse chr) {
    if (chr != null) {
        catalog.setClusterGauge("cluster_status", chr.getStatus().value());

        catalog.setClusterGauge("cluster_nodes_number", chr.getNumberOfNodes());
        catalog.setClusterGauge("cluster_datanodes_number", chr.getNumberOfDataNodes());

        catalog.setClusterGauge("cluster_shards_active_percent", chr.getActiveShardsPercent());

        catalog.setClusterGauge("cluster_shards_number", chr.getActiveShards(), "active");
        catalog.setClusterGauge("cluster_shards_number", chr.getActivePrimaryShards(), "active_primary");
        catalog.setClusterGauge("cluster_shards_number", chr.getDelayedUnassignedShards(), "unassigned");
        catalog.setClusterGauge("cluster_shards_number", chr.getInitializingShards(), "initializing");
        catalog.setClusterGauge("cluster_shards_number", chr.getRelocatingShards(), "relocating");
        catalog.setClusterGauge("cluster_shards_number", chr.getUnassignedShards(), "unassigned");

        catalog.setClusterGauge("cluster_pending_tasks_number", chr.getNumberOfPendingTasks());
        catalog.setClusterGauge("cluster_task_max_waiting_time_seconds", chr.getTaskMaxWaitingTime().getSeconds());

        catalog.setClusterGauge("cluster_is_timedout_bool", chr.isTimedOut() ? 1 : 0);

        catalog.setClusterGauge("cluster_inflight_fetch_number", chr.getNumberOfInFlightFetch());
    }
}
 
Example #4
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 #5
Source File: ESClient.java    From Gather-Platform with GNU General Public License v3.0 6 votes vote down vote up
public Client getClient() {
    if (!staticValue.isNeedEs()) {
        LOG.info("已在配置文件中声明不需要ES,如需要ES,请在配置文件中进行配置");
        return null;
    }
    if (client != null) return client;
    try {
        LOG.info("正在初始化ElasticSearch客户端," + staticValue.getEsHost());
        Settings settings = Settings.builder()
                .put("cluster.name", staticValue.getEsClusterName()).build();
        client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(staticValue.getEsHost()), 9300));
        final ClusterHealthResponse healthResponse = client.admin().cluster().prepareHealth()
                .setTimeout(TimeValue.timeValueMinutes(1)).execute().actionGet();
        if (healthResponse.isTimedOut()) {
            LOG.error("ES客户端初始化失败");
        } else {
            LOG.info("ES客户端初始化成功");
        }
    } catch (IOException e) {
        LOG.fatal("构建ElasticSearch客户端失败!");
    }
    return client;
}
 
Example #6
Source File: ClientFactory.java    From act-platform with ISC License 6 votes vote down vote up
private boolean waitForConnection(RestHighLevelClient client) {
  long timeout = System.currentTimeMillis() + INITIALIZATION_TIMEOUT;
  while (System.currentTimeMillis() < timeout) {
    try {
      ClusterHealthResponse response = client.cluster().health(new ClusterHealthRequest(), RequestOptions.DEFAULT);
      LOGGER.debug("ElasticSearch cluster (%s) status is %s.", response.getClusterName(), response.getStatus());
      // If ElasticSearch is reachable and its status is at least 'yellow' return immediately.
      if (response.status() == RestStatus.OK && response.getStatus() != ClusterHealthStatus.RED) return true;
    } catch (ElasticsearchException | IOException ex) {
      LOGGER.debug(ex, "Could not fetch ElasticSearch cluster health information.");
    }

    try {
      Thread.sleep(INITIALIZATION_RETRY_WAIT);
    } catch (InterruptedException ignored) {
      // Re-interrupt thread and return immediately in order to trigger a component shutdown.
      Thread.currentThread().interrupt();
      return false;
    }

    LOGGER.warning("ElasticSearch cluster is not available. Trying again.");
  }

  return false;
}
 
Example #7
Source File: IMAPImporter.java    From elasticsearch-imap with Apache License 2.0 6 votes vote down vote up
public static void waitForYellowCluster(Client client) throws IOException {

        ClusterHealthStatus status = ClusterHealthStatus.YELLOW;
        
        try {
            logger.debug("waiting for cluster state {}", status.name());
            final ClusterHealthResponse healthResponse = client.admin().cluster().prepareHealth().setWaitForStatus(status)
                    .setTimeout(TimeValue.timeValueSeconds(30)).execute().actionGet();
            if (healthResponse.isTimedOut()) {
                logger.error("Timeout while waiting for cluster state: {}, current cluster state is: {}", status.name(), healthResponse.getStatus().name());
                throw new IOException("cluster state is " + healthResponse.getStatus().name() + " and not " + status.name()
                       + ", cowardly refusing to continue with operations");
            } else {
                logger.debug("... cluster state ok");
            }
        } catch (final Exception e) {
            logger.error("Exception while waiting for cluster state: {} due to ", e, status.name(), e.toString());
            throw new IOException("timeout, cluster does not respond to health request, cowardly refusing to continue with operations", e);
        }
    }
 
Example #8
Source File: BaseClient.java    From elasticsearch-helper with Apache License 2.0 6 votes vote down vote up
public void waitForCluster(String statusString, TimeValue timeout) throws IOException {
    if (client() == null) {
        return;
    }
    try {
        ClusterHealthStatus status = ClusterHealthStatus.fromString(statusString);
        ClusterHealthResponse healthResponse =
                client().execute(ClusterHealthAction.INSTANCE, new ClusterHealthRequest().waitForStatus(status).timeout(timeout)).actionGet();
        if (healthResponse != null && healthResponse.isTimedOut()) {
            throw new IOException("cluster state is " + healthResponse.getStatus().name()
                    + " and not " + status.name()
                    + ", from here on, everything will fail!");
        }
    } catch (ElasticsearchTimeoutException e) {
        throw new IOException("timeout, cluster does not respond to health request, cowardly refusing to continue with operations");
    }
}
 
Example #9
Source File: NodeTestUtils.java    From elasticsearch-helper with Apache License 2.0 6 votes vote down vote up
@Before
public void startNodes() {
    try {
        logger.info("starting");
        setClusterName();
        startNode("1");
        findNodeAddress();
        try {
            ClusterHealthResponse healthResponse = client("1").execute(ClusterHealthAction.INSTANCE,
                            new ClusterHealthRequest().waitForStatus(ClusterHealthStatus.GREEN).timeout(TimeValue.timeValueSeconds(30))).actionGet();
            if (healthResponse != null && healthResponse.isTimedOut()) {
                throw new IOException("cluster state is " + healthResponse.getStatus().name()
                        + ", from here on, everything will fail!");
            }
        } catch (ElasticsearchTimeoutException e) {
            throw new IOException("timeout, cluster does not respond to health request, cowardly refusing to continue with operations");
        }
    } catch (Throwable t) {
        logger.error("startNodes failed", t);
    }
}
 
Example #10
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 #11
Source File: EsEntityIndexImpl.java    From usergrid with Apache License 2.0 6 votes vote down vote up
/**
 * Check health of cluster.
 */
@Override
public Health getClusterHealth() {

    try {
        ClusterHealthResponse chr = esProvider.getClient().admin()
            .cluster().health(new ClusterHealthRequest()).get();
        return Health.valueOf( chr.getStatus().name() );
    }
    catch ( Exception ex ) {
        ex.printStackTrace();
        logger.error( "Error connecting to ElasticSearch", ex.getMessage() );
    }

    // this is bad, red alert!
    return Health.RED;
}
 
Example #12
Source File: PrometheusMetricsCollector.java    From elasticsearch-prometheus-exporter with Apache License 2.0 6 votes vote down vote up
private void updatePerIndexMetrics(ClusterHealthResponse chr, IndicesStatsResponse isr) {

        if (chr != null && isr != null) {
            for (Map.Entry<String, IndexStats> entry : isr.getIndices().entrySet()) {
                String indexName = entry.getKey();
                ClusterIndexHealth cih = chr.getIndices().get(indexName);
                catalog.setClusterGauge("index_status", cih.getStatus().value(), indexName);
                catalog.setClusterGauge("index_replicas_number", cih.getNumberOfReplicas(), indexName);
                catalog.setClusterGauge("index_shards_number", cih.getActiveShards(), "active", indexName);
                catalog.setClusterGauge("index_shards_number", cih.getNumberOfShards(), "shards", indexName);
                catalog.setClusterGauge("index_shards_number", cih.getActivePrimaryShards(), "active_primary", indexName);
                catalog.setClusterGauge("index_shards_number", cih.getInitializingShards(), "initializing", indexName);
                catalog.setClusterGauge("index_shards_number", cih.getRelocatingShards(), "relocating", indexName);
                catalog.setClusterGauge("index_shards_number", cih.getUnassignedShards(), "unassigned", indexName);
                IndexStats indexStats = entry.getValue();
                updatePerIndexContextMetrics(indexName, "total", indexStats.getTotal());
                updatePerIndexContextMetrics(indexName, "primaries", indexStats.getPrimaries());
            }
        }
    }
 
Example #13
Source File: SQLTransportExecutor.java    From crate with Apache License 2.0 6 votes vote down vote up
private ClusterHealthStatus ensureState(ClusterHealthStatus state) {
    Client client = clientProvider.client();
    ClusterHealthResponse actionGet = client.admin().cluster().health(
        Requests.clusterHealthRequest()
            .waitForStatus(state)
            .waitForEvents(Priority.LANGUID).waitForNoRelocatingShards(false)
    ).actionGet();

    if (actionGet.isTimedOut()) {
        LOGGER.info("ensure state timed out, cluster state:\n{}\n{}",
            client.admin().cluster().prepareState().get().getState(),
            client.admin().cluster().preparePendingClusterTasks().get());
        assertThat("timed out waiting for state", actionGet.isTimedOut(), equalTo(false));
    }
    if (state == ClusterHealthStatus.YELLOW) {
        assertThat(actionGet.getStatus(), Matchers.anyOf(equalTo(state), equalTo(ClusterHealthStatus.GREEN)));
    } else {
        assertThat(actionGet.getStatus(), equalTo(state));
    }
    return actionGet.getStatus();
}
 
Example #14
Source File: ElasticsearchHdfsIT.java    From streams with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public void prepareTest() throws Exception {

  testConfiguration = new StreamsConfigurator<>(ElasticsearchHdfsConfiguration.class).detectCustomConfiguration("ElasticsearchHdfsIT");
  testClient = ElasticsearchClientManager.getInstance(testConfiguration.getSource()).client();

  ClusterHealthRequest clusterHealthRequest = Requests.clusterHealthRequest();
  ClusterHealthResponse clusterHealthResponse = testClient.admin().cluster().health(clusterHealthRequest).actionGet();
  assertNotEquals(clusterHealthResponse.getStatus(), ClusterHealthStatus.RED);

  IndicesExistsRequest indicesExistsRequest = Requests.indicesExistsRequest(testConfiguration.getSource().getIndexes().get(0));
  IndicesExistsResponse indicesExistsResponse = testClient.admin().indices().exists(indicesExistsRequest).actionGet();
  assertThat(indicesExistsResponse.isExists(), is(true));

  SearchRequestBuilder countRequest = testClient
      .prepareSearch(testConfiguration.getSource().getIndexes().get(0))
      .setTypes(testConfiguration.getSource().getTypes().get(0));
  SearchResponse countResponse = countRequest.execute().actionGet();

  count = (int)countResponse.getHits().getTotalHits();

  assertNotEquals(count, 0);
}
 
Example #15
Source File: ElasticsearchReindexIT.java    From streams with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public void prepareTest() throws Exception {

  testConfiguration = new StreamsConfigurator<>(ElasticsearchReindexConfiguration.class).detectCustomConfiguration("ElasticsearchReindexIT");
  testClient = ElasticsearchClientManager.getInstance(testConfiguration.getSource()).client();

  ClusterHealthRequest clusterHealthRequest = Requests.clusterHealthRequest();
  ClusterHealthResponse clusterHealthResponse = testClient.admin().cluster().health(clusterHealthRequest).actionGet();
  assertThat(clusterHealthResponse.getStatus(), not(ClusterHealthStatus.RED));

  IndicesExistsRequest indicesExistsRequest = Requests.indicesExistsRequest(testConfiguration.getSource().getIndexes().get(0));
  IndicesExistsResponse indicesExistsResponse = testClient.admin().indices().exists(indicesExistsRequest).actionGet();
  assertThat(indicesExistsResponse.isExists(), is(true));

  SearchRequestBuilder countRequest = testClient
      .prepareSearch(testConfiguration.getSource().getIndexes().get(0))
      .setTypes(testConfiguration.getSource().getTypes().get(0));
  SearchResponse countResponse = countRequest.execute().actionGet();

  count = (int)countResponse.getHits().getTotalHits();

  assertThat(count, not(0));

}
 
Example #16
Source File: EsEntityIndexImpl.java    From usergrid with Apache License 2.0 6 votes vote down vote up
/**
 * Check health of this specific index.
 */
@Override
public Health getIndexHealth() {

    try {
        String[] indexNames = this.getIndexes();
        final ActionFuture<ClusterHealthResponse> future =  esProvider.getClient().admin().cluster().health(
            new ClusterHealthRequest( indexNames  ) );

        //only wait 2 seconds max
        ClusterHealthResponse chr = future.actionGet(2000);
        return Health.valueOf( chr.getStatus().name() );
    }
    catch ( Exception ex ) {
        logger.error( "Error connecting to ElasticSearch", ex.getMessage() );
    }

    // this is bad, red alert!
    return Health.RED;
}
 
Example #17
Source File: TwitterUserstreamElasticsearchIT.java    From streams with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public void prepareTest() throws Exception {

  testConfiguration = new StreamsConfigurator<>(TwitterUserstreamElasticsearchConfiguration.class).detectCustomConfiguration();

  testClient = ElasticsearchClientManager.getInstance(testConfiguration.getElasticsearch()).client();

  ClusterHealthRequest clusterHealthRequest = Requests.clusterHealthRequest();
  ClusterHealthResponse clusterHealthResponse = testClient.admin().cluster().health(clusterHealthRequest).actionGet();
  assertNotEquals(clusterHealthResponse.getStatus(), ClusterHealthStatus.RED);

  IndicesExistsRequest indicesExistsRequest = Requests.indicesExistsRequest(testConfiguration.getElasticsearch().getIndex());
  IndicesExistsResponse indicesExistsResponse = testClient.admin().indices().exists(indicesExistsRequest).actionGet();

  if(indicesExistsResponse.isExists()) {
    DeleteIndexRequest deleteIndexRequest = Requests.deleteIndexRequest(testConfiguration.getElasticsearch().getIndex());
    DeleteIndexResponse deleteIndexResponse = testClient.admin().indices().delete(deleteIndexRequest).actionGet();
    assertTrue(deleteIndexResponse.isAcknowledged());
  };

  CreateIndexRequest createIndexRequest = Requests.createIndexRequest(testConfiguration.getElasticsearch().getIndex());
  CreateIndexResponse createIndexResponse = testClient.admin().indices().create(createIndexRequest).actionGet();
  assertTrue(createIndexResponse.isAcknowledged());

}
 
Example #18
Source File: ESIntegTestCase.java    From crate 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().waitForNoRelocatingShards(true);
    if (status != null) {
        request.waitForStatus(status);
    }
    ClusterHealthResponse actionGet = client().admin().cluster()
        .health(request).actionGet();
    if (actionGet.isTimedOut()) {
        logger.info("waitForRelocation timed out (status={}), cluster state:\n{}\n{}", status,
            client().admin().cluster().prepareState().get().getState(), client().admin().cluster().preparePendingClusterTasks().get());
        assertThat("timed out waiting for relocation", actionGet.isTimedOut(), equalTo(false));
    }
    if (status != null) {
        assertThat(actionGet.getStatus(), equalTo(status));
    }
    return actionGet.getStatus();
}
 
Example #19
Source File: CustomRealmIT.java    From shield-custom-realm-example with Apache License 2.0 6 votes vote down vote up
public void testTransportClient() throws Exception {
    NodesInfoResponse nodeInfos = client().admin().cluster().prepareNodesInfo().get();
    List<NodeInfo>  nodes = nodeInfos.getNodes();
    assertTrue(nodes.size() > 0);
    TransportAddress publishAddress = randomFrom(nodes).getTransport().address().publishAddress();
    String clusterName = nodeInfos.getClusterName().value();

    Settings settings = Settings.builder()
            .put("cluster.name", clusterName)
            .put(ThreadContext.PREFIX + "." + CustomRealm.USER_HEADER, randomFrom(KNOWN_USERS))
            .put(ThreadContext.PREFIX + "." + CustomRealm.PW_HEADER, PASSWORD)
            .build();
    try (TransportClient client = new PreBuiltXPackTransportClient(settings)) {
        client.addTransportAddress(publishAddress);
        ClusterHealthResponse response = client.admin().cluster().prepareHealth().execute().actionGet();
        assertThat(response.isTimedOut(), is(false));
    }
}
 
Example #20
Source File: FessEsClient.java    From fess with Apache License 2.0 6 votes vote down vote up
protected void waitForYellowStatus(final FessConfig fessConfig) {
    Exception cause = null;
    final long startTime = System.currentTimeMillis();
    for (int i = 0; i < maxEsStatusRetry; i++) {
        try {
            final ClusterHealthResponse response =
                    client.admin().cluster().prepareHealth().setWaitForYellowStatus().execute()
                            .actionGet(fessConfig.getIndexHealthTimeout());
            if (logger.isDebugEnabled()) {
                logger.debug("Elasticsearch Cluster Status: {}", response.getStatus());
            }
            return;
        } catch (final Exception e) {
            cause = e;
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to access to Elasticsearch:{}", i, cause);
        }
        ThreadUtil.sleep(1000L);
    }
    final String message =
            "Elasticsearch (" + System.getProperty(Constants.FESS_ES_HTTP_ADDRESS)
                    + ") is not available. Check the state of your Elasticsearch cluster (" + clusterName + ") in "
                    + (System.currentTimeMillis() - startTime) + "ms.";
    throw new ContainerInitFailureException(message, cause);
}
 
Example #21
Source File: ElasticsearchParentChildUpdaterIT.java    From streams with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public void prepareTestParentChildPersistUpdater() throws Exception {

  testConfiguration = new ComponentConfigurator<>(ElasticsearchWriterConfiguration.class).detectConfiguration( "ElasticsearchParentChildUpdaterIT");
  testClient = ElasticsearchClientManager.getInstance(testConfiguration).client();

  ClusterHealthRequest clusterHealthRequest = Requests.clusterHealthRequest();
  ClusterHealthResponse clusterHealthResponse = testClient.admin().cluster().health(clusterHealthRequest).actionGet();
  assertNotEquals(clusterHealthResponse.getStatus(), ClusterHealthStatus.RED);

  IndicesExistsRequest indicesExistsRequest = Requests.indicesExistsRequest(testConfiguration.getIndex());
  IndicesExistsResponse indicesExistsResponse = testClient.admin().indices().exists(indicesExistsRequest).actionGet();
  assertTrue(indicesExistsResponse.isExists());

  Reflections reflections = new Reflections(new ConfigurationBuilder()
      .setUrls(ClasspathHelper.forPackage("org.apache.streams.pojo.json"))
      .setScanners(new SubTypesScanner()));
  objectTypes = reflections.getSubTypesOf(ActivityObject.class);

  Path testdataDir = Paths.get("target/dependency/activitystreams-testdata");
  files = Files.list(testdataDir).collect(Collectors.toList());

}
 
Example #22
Source File: ElasticsearchContainerTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test
public void transportClientClusterHealth() {
    // transportClientContainer {
    // Create the elasticsearch container.
    try (ElasticsearchContainer container = new ElasticsearchContainer()) {
        // Start the container. This step might take some time...
        container.start();

        // Do whatever you want with the transport client
        TransportAddress transportAddress = new TransportAddress(container.getTcpHost());
        String expectedClusterName = "docker-cluster";
        Settings settings = Settings.builder().put("cluster.name", expectedClusterName).build();
        try (TransportClient transportClient = new PreBuiltTransportClient(settings)
            .addTransportAddress(transportAddress)) {
            ClusterHealthResponse healths = transportClient.admin().cluster().prepareHealth().get();
            String clusterName = healths.getClusterName();
            // }}}
            assertThat(clusterName, is(expectedClusterName));
            // transportClientContainer {{{
        }
    }
    // }
}
 
Example #23
Source File: InternalEsClient.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * Clusterの状態取得.
 * @return 状態Map
 */
public Map<String, Object> checkHealth() {
    ClusterHealthResponse clusterHealth;
    clusterHealth = esTransportClient.admin().cluster().health(new ClusterHealthRequest()).actionGet();
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("cluster_name", clusterHealth.getClusterName());
    map.put("status", clusterHealth.getStatus().name());
    map.put("timed_out", clusterHealth.isTimedOut());
    map.put("number_of_nodes", clusterHealth.getNumberOfNodes());
    map.put("number_of_data_nodes", clusterHealth.getNumberOfDataNodes());
    map.put("active_primary_shards", clusterHealth.getActivePrimaryShards());
    map.put("active_shards", clusterHealth.getActiveShards());
    map.put("relocating_shards", clusterHealth.getRelocatingShards());
    map.put("initializing_shards", clusterHealth.getInitializingShards());
    map.put("unassigned_shards", clusterHealth.getUnassignedShards());
    return map;
}
 
Example #24
Source File: NodeTestUtils.java    From elasticsearch-analysis-baseform with Apache License 2.0 6 votes vote down vote up
@Before
public void startNodes() {
    try {
        logger.info("starting");
        setClusterName();
        startNode("1");
        findNodeAddress();
        try {
            ClusterHealthResponse healthResponse = client("1").execute(ClusterHealthAction.INSTANCE,
                            new ClusterHealthRequest().waitForStatus(ClusterHealthStatus.GREEN).timeout(TimeValue.timeValueSeconds(30))).actionGet();
            if (healthResponse != null && healthResponse.isTimedOut()) {
                throw new IOException("cluster state is " + healthResponse.getStatus().name()
                        + ", from here on, everything will fail!");
            }
        } catch (ElasticsearchTimeoutException e) {
            throw new IOException("timeout, cluster does not respond to health request, cowardly refusing to continue with operations");
        }
    } catch (Throwable t) {
        logger.error("startNodes failed", t);
    }
}
 
Example #25
Source File: InternalEsClient.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * Clusterの状態取得.
 * @return 状態Map
 */
public Map<String, Object> checkHealth() {
    ClusterHealthResponse clusterHealth;
    clusterHealth = esTransportClient.admin().cluster().health(new ClusterHealthRequest()).actionGet();
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("cluster_name", clusterHealth.getClusterName());
    map.put("status", clusterHealth.getStatus().name());
    map.put("timed_out", clusterHealth.isTimedOut());
    map.put("number_of_nodes", clusterHealth.getNumberOfNodes());
    map.put("number_of_data_nodes", clusterHealth.getNumberOfDataNodes());
    map.put("active_primary_shards", clusterHealth.getActivePrimaryShards());
    map.put("active_shards", clusterHealth.getActiveShards());
    map.put("relocating_shards", clusterHealth.getRelocatingShards());
    map.put("initializing_shards", clusterHealth.getInitializingShards());
    map.put("unassigned_shards", clusterHealth.getUnassignedShards());
    return map;
}
 
Example #26
Source File: NodeTestUtils.java    From elasticsearch-xml with Apache License 2.0 6 votes vote down vote up
@Before
public void startNodes() {
    try {
        logger.info("starting");
        setClusterName();
        startNode("1");
        findNodeAddress();
        try {
            ClusterHealthResponse healthResponse = client("1").execute(ClusterHealthAction.INSTANCE,
                            new ClusterHealthRequest().waitForStatus(ClusterHealthStatus.GREEN).timeout(TimeValue.timeValueSeconds(30))).actionGet();
            if (healthResponse != null && healthResponse.isTimedOut()) {
                throw new IOException("cluster state is " + healthResponse.getStatus().name()
                        + ", from here on, everything will fail!");
            }
        } catch (ElasticsearchTimeoutException e) {
            throw new IOException("timeout, cluster does not respond to health request, cowardly refusing to continue with operations");
        }
    } catch (Throwable t) {
        logger.error("startNodes failed", t);
    }
}
 
Example #27
Source File: ElasticSearchComponent.java    From metron with Apache License 2.0 6 votes vote down vote up
public static void waitForCluster(Client client, ClusterHealthStatus statusThreshold,
    String timeout) throws UnableToStartException {
  try {
    ClusterHealthResponse healthResponse = (ClusterHealthResponse) client
        .execute(ClusterHealthAction.INSTANCE,
            new ClusterHealthRequest().waitForStatus(statusThreshold).timeout(timeout))
        .actionGet();
    if (healthResponse != null && healthResponse.isTimedOut()) {
      throw new UnableToStartException("cluster state is " + healthResponse.getStatus().name()
          + " and not " + statusThreshold.name()
          + ", from here on, everything will fail!");
    }
  } catch (ElasticsearchTimeoutException e) {
    throw new UnableToStartException(
        "timeout, cluster does not respond to health request, cowardly refusing to continue with operations");
  }
}
 
Example #28
Source File: HdfsElasticsearchIT.java    From streams with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public void prepareTest() throws Exception {

  testConfiguration = new StreamsConfigurator<>(HdfsElasticsearchConfiguration.class).detectCustomConfiguration("HdfsElasticsearchIT");
  testClient = ElasticsearchClientManager.getInstance(testConfiguration.getDestination()).client();

  ClusterHealthRequest clusterHealthRequest = Requests.clusterHealthRequest();
  ClusterHealthResponse clusterHealthResponse = testClient.admin().cluster().health(clusterHealthRequest).actionGet();
  assertNotEquals(clusterHealthResponse.getStatus(), ClusterHealthStatus.RED);

  IndicesExistsRequest indicesExistsRequest = Requests.indicesExistsRequest(testConfiguration.getDestination().getIndex());
  IndicesExistsResponse indicesExistsResponse = testClient.admin().indices().exists(indicesExistsRequest).actionGet();
  if(indicesExistsResponse.isExists()) {
    DeleteIndexRequest deleteIndexRequest = Requests.deleteIndexRequest(testConfiguration.getDestination().getIndex());
    DeleteIndexResponse deleteIndexResponse = testClient.admin().indices().delete(deleteIndexRequest).actionGet();
    assertTrue(deleteIndexResponse.isAcknowledged());
  };
}
 
Example #29
Source File: ElasticsearchQueryStore.java    From foxtrot with Apache License 2.0 5 votes vote down vote up
@Override
public ClusterHealthResponse getClusterHealth() throws ExecutionException, InterruptedException {
    //Bug as mentioned in https://github.com/elastic/elasticsearch/issues/10574
    return connection.getClient()
            .admin()
            .cluster()
            .prepareHealth()
            .execute()
            .get();
}
 
Example #30
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.");
    }
}