Java Code Examples for org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse#isTimedOut()

The following examples show how to use org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse#isTimedOut() . 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: 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 2
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 3
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 4
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 5
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 6
Source File: ESIntegTestCase.java    From crate with Apache License 2.0 6 votes vote down vote up
protected void ensureStableCluster(int nodeCount, TimeValue timeValue, boolean local, @Nullable String viaNode) {
    if (viaNode == null) {
        viaNode = randomFrom(internalCluster().getNodeNames());
    }
    logger.debug("ensuring cluster is stable with [{}] nodes. access node: [{}]. timeout: [{}]", nodeCount, viaNode, timeValue);
    ClusterHealthResponse clusterHealthResponse = client(viaNode).admin().cluster().prepareHealth()
        .setWaitForEvents(Priority.LANGUID)
        .setWaitForNodes(Integer.toString(nodeCount))
        .setTimeout(timeValue)
        .setLocal(local)
        .setWaitForNoRelocatingShards(true)
        .get();
    if (clusterHealthResponse.isTimedOut()) {
        ClusterStateResponse stateResponse = client(viaNode).admin().cluster().prepareState().get();
        fail("failed to reach a stable cluster of [" + nodeCount + "] nodes. Tried via [" + viaNode + "]. last cluster state:\n"
             + stateResponse.getState());
    }
    assertThat(clusterHealthResponse.isTimedOut(), is(false));
    ensureFullyConnectedCluster();
}
 
Example 7
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 8
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 9
Source File: RestHighLevelClientCase.java    From skywalking with Apache License 2.0 5 votes vote down vote up
public boolean healthcheck() throws Exception {
    ClusterHealthRequest request = new ClusterHealthRequest();
    request.timeout(TimeValue.timeValueSeconds(10));
    request.waitForStatus(ClusterHealthStatus.GREEN);

    ClusterHealthResponse response = client.cluster().health(request, RequestOptions.DEFAULT);
    if (response.isTimedOut()) {
        String message = "elastic search node start fail!";
        logger.error(message);
        throw new RuntimeException(message);
    }
    return true;
}
 
Example 10
Source File: ElasticsearchClusterRunner.java    From elasticsearch-cluster-runner with Apache License 2.0 5 votes vote down vote up
public ClusterHealthStatus waitForRelocation() {
    final ClusterHealthRequest request = Requests.clusterHealthRequest().waitForNoRelocatingShards(true);
    final ClusterHealthResponse actionGet = client().admin().cluster().health(request).actionGet();
    if (actionGet.isTimedOut()) {
        onFailure("waitForRelocation timed out, cluster state:\n" + "\n" + client().admin().cluster().prepareState().get().getState()
                + "\n" + client().admin().cluster().preparePendingClusterTasks().get(), actionGet);
    }
    return actionGet.getStatus();
}
 
Example 11
Source File: ElasticsearchClusterRunner.java    From elasticsearch-cluster-runner with Apache License 2.0 5 votes vote down vote up
/**
 * Wait for yellow state of a cluster.
 *
 * @param indices indices to check status
 * @return cluster health status
 */
public ClusterHealthStatus ensureYellow(final String... indices) {
    final ClusterHealthResponse actionGet = client().admin().cluster().health(Requests.clusterHealthRequest(indices)
            .waitForNoRelocatingShards(true).waitForYellowStatus().waitForEvents(Priority.LANGUID)).actionGet();
    if (actionGet.isTimedOut()) {
        onFailure("ensureYellow timed out, cluster state:\n" + "\n" + client().admin().cluster().prepareState().get().getState() + "\n"
                + client().admin().cluster().preparePendingClusterTasks().get(), actionGet);
    }
    return actionGet.getStatus();
}
 
Example 12
Source File: ElasticsearchClusterRunner.java    From elasticsearch-cluster-runner with Apache License 2.0 5 votes vote down vote up
/**
 * Wait for green state of a cluster.
 *
 * @param indices indices to check status
 * @return cluster health status
 */
public ClusterHealthStatus ensureGreen(final String... indices) {
    final ClusterHealthResponse actionGet = client().admin().cluster().health(
            Requests.clusterHealthRequest(indices).waitForGreenStatus().waitForEvents(Priority.LANGUID).waitForNoRelocatingShards(true))
            .actionGet();
    if (actionGet.isTimedOut()) {
        onFailure("ensureGreen timed out, cluster state:\n" + client().admin().cluster().prepareState().get().getState() + "\n"
                + client().admin().cluster().preparePendingClusterTasks().get(), actionGet);
    }
    return actionGet.getStatus();
}
 
Example 13
Source File: ESSingleNodeTestCase.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Ensures the cluster has a green state via the cluster health API. This method will also wait for relocations.
 * It is useful to ensure that all action on the cluster have finished and all shards that were currently relocating
 * are now allocated and started.
 *
 * @param timeout time out value to set on {@link org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest}
 */
public ClusterHealthStatus ensureGreen(TimeValue timeout, String... indices) {
    ClusterHealthResponse actionGet = client().admin().cluster()
            .health(Requests.clusterHealthRequest(indices).timeout(timeout).waitForGreenStatus().waitForEvents(Priority.LANGUID)
                    .waitForNoRelocatingShards(true)).actionGet();
    if (actionGet.isTimedOut()) {
        logger.info("ensureGreen timed out, cluster state:\n{}\n{}", client().admin().cluster().prepareState().get().getState(),
            client().admin().cluster().preparePendingClusterTasks().get());
        assertThat("timed out waiting for green state", actionGet.isTimedOut(), equalTo(false));
    }
    assertThat(actionGet.getStatus(), equalTo(ClusterHealthStatus.GREEN));
    logger.debug("indices {} are green", indices.length == 0 ? "[_all]" : indices);
    return actionGet.getStatus();
}
 
Example 14
Source File: ElasticsearchStateManager.java    From elasticsearch-imap with Apache License 2.0 5 votes vote down vote up
private void waitForCluster(final ClusterHealthStatus status, final TimeValue timeout) throws IOException {
    try {
        logger.debug("waiting for cluster state {}", status.name());
        final ClusterHealthResponse healthResponse = client.admin().cluster().prepareHealth().setWaitForStatus(status)
                .setTimeout(timeout).execute().actionGet();
        if (healthResponse.isTimedOut()) {
            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 ElasticsearchTimeoutException e) {
        throw new IOException("timeout, cluster does not respond to health request, cowardly refusing to continue with operations");
    }
}
 
Example 15
Source File: CaseController.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@GetMapping("/healthcheck")
public String healthcheck() throws Exception {
    Client client = initTransportClient();
    ClusterHealthResponse response = client.admin().cluster().prepareHealth().setWaitForYellowStatus().get();
    if (response.isTimedOut()) {
        String message = "elastic search node start fail!";
        logger.error(message);
        throw new RuntimeException(message);
    }
    client.close();
    return "Success";
}
 
Example 16
Source File: AwsRestHighLevelClient.java    From aws-athena-query-federation with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves cluster-health information for shards associated with the specified index. The request will time out
 * if no results are returned after a period of time indicated by timeout.
 * @param index is used to restrict the request to a specified index.
 * @param timeout is the command timeout period in seconds.
 * @return a set of shard ids for the specified index.
 * @throws IOException if an error occurs while sending the request to the Elasticsearch instance.
 * @throws RuntimeException if the request times out, or no active-primary shards are present.
 */
public Set<Integer> getShardIds(String index, long timeout)
        throws RuntimeException, IOException
{
    ClusterHealthRequest request = new ClusterHealthRequest(index)
            .timeout(new TimeValue(timeout, TimeUnit.SECONDS));
    // Set request to shard-level details
    request.level(ClusterHealthRequest.Level.SHARDS);

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

    if (response.isTimedOut()) {
        throw new RuntimeException("Request timed out for index (" + index + ").");
    }
    else if (response.getActiveShards() == 0) {
        throw new RuntimeException("There are no active shards for index (" + index + ").");
    }
    else if (response.getStatus() == ClusterHealthStatus.RED) {
        throw new RuntimeException("Request aborted for index (" + index +
                ") due to cluster's status (RED) - One or more primary shards are unassigned.");
    }
    else if (!response.getIndices().containsKey(index)) {
        throw new RuntimeException("Request has an invalid index (" + index + ").");
    }

    return response.getIndices().get(index).getShards().keySet();
}
 
Example 17
Source File: AbstractUnitTest.java    From elasticsearch-shield-kerberos-realm with Apache License 2.0 5 votes vote down vote up
protected void waitForCluster(final ClusterHealthStatus status, final TimeValue timeout) throws IOException {
    try {
        log.debug("waiting for cluster state {}", status.name());
        final ClusterHealthResponse healthResponse = client.admin().cluster().prepareHealth().setWaitForStatus(status)
                .setWaitForNodes(">2").setTimeout(timeout).execute().actionGet();
        if (healthResponse.isTimedOut()) {
            throw new IOException("cluster state is " + healthResponse.getStatus().name() + " and not " + status.name()
                    + ", cowardly refusing to continue with operations");
        } else {
            log.debug("... cluster state ok");
        }
    } catch (final ElasticsearchTimeoutException e) {
        throw new IOException("timeout, cluster does not respond to health request, cowardly refusing to continue with operations");
    }
}
 
Example 18
Source File: ESIntegTestCase.java    From crate with Apache License 2.0 5 votes vote down vote up
private ClusterHealthStatus ensureColor(ClusterHealthStatus clusterHealthStatus, TimeValue timeout, boolean waitForNoInitializingShards,
                                        String... indices) {
    String color = clusterHealthStatus.name().toLowerCase(Locale.ROOT);
    String method = "ensure" + Strings.capitalize(color);

    ClusterHealthRequest healthRequest = Requests.clusterHealthRequest(indices)
        .timeout(timeout)
        .waitForStatus(clusterHealthStatus)
        .waitForEvents(Priority.LANGUID)
        .waitForNoRelocatingShards(true)
        .waitForNoInitializingShards(waitForNoInitializingShards)
        // We currently often use ensureGreen or ensureYellow to check whether the cluster is back in a good state after shutting down
        // a node. If the node that is stopped is the master node, another node will become master and publish a cluster state where it
        // is master but where the node that was stopped hasn't been removed yet from the cluster state. It will only subsequently
        // publish a second state where the old master is removed. If the ensureGreen/ensureYellow is timed just right, it will get to
        // execute before the second cluster state update removes the old master and the condition ensureGreen / ensureYellow will
        // trivially hold if it held before the node was shut down. The following "waitForNodes" condition ensures that the node has
        // been removed by the master so that the health check applies to the set of nodes we expect to be part of the cluster.
        .waitForNodes(Integer.toString(cluster().size()));

    ClusterHealthResponse actionGet = client().admin().cluster().health(healthRequest).actionGet();
    if (actionGet.isTimedOut()) {
        logger.info("{} timed out, cluster state:\n{}\n{}",
            method,
            client().admin().cluster().prepareState().get().getState(),
            client().admin().cluster().preparePendingClusterTasks().get());
        fail("timed out waiting for " + color + " state");
    }
    assertThat("Expected at least " + clusterHealthStatus + " but got " + actionGet.getStatus(),
        actionGet.getStatus().value(), lessThanOrEqualTo(clusterHealthStatus.value()));
    logger.debug("indices {} are {}", indices.length == 0 ? "[_all]" : indices, color);
    return actionGet.getStatus();
}
 
Example 19
Source File: ElasticsearchClient.java    From yacy_grid_mcp with GNU Lesser General Public License v2.1 5 votes vote down vote up
@SuppressWarnings("unused")
private boolean wait_ready(long maxtimemillis, ClusterHealthStatus status) {
    // wait for yellow status
    long start = System.currentTimeMillis();
    boolean is_ready;
    do {
        // wait for yellow status
        ClusterHealthResponse health = elasticsearchClient.admin().cluster().prepareHealth().setWaitForStatus(status).execute().actionGet();
        is_ready = !health.isTimedOut();
        if (!is_ready && System.currentTimeMillis() - start > maxtimemillis) return false; 
    } while (!is_ready);
    return is_ready;
}
 
Example 20
Source File: UserRequestHandler.java    From elasticsearch-taste with Apache License 2.0 4 votes vote down vote up
private void doUserMappingCreation(final Params params,
        final RequestHandler.OnErrorListener listener,
        final Map<String, Object> requestMap,
        final Map<String, Object> paramMap, final RequestHandlerChain chain) {
    final String index = params.param(
            TasteConstants.REQUEST_PARAM_USER_INDEX, params.param("index"));
    final String type = params.param(
            TasteConstants.REQUEST_PARAM_USER_TYPE,
            TasteConstants.USER_TYPE);
    final String userIdField = params.param(
            TasteConstants.REQUEST_PARAM_USER_ID_FIELD,
            TasteConstants.USER_ID_FIELD);
    final String timestampField = params.param(
            TasteConstants.REQUEST_PARAM_TIMESTAMP_FIELD,
            TasteConstants.TIMESTAMP_FIELD);

    try (XContentBuilder jsonBuilder = XContentFactory.jsonBuilder()) {
        final ClusterHealthResponse healthResponse = client
                .admin()
                .cluster()
                .prepareHealth(index)
                .setWaitForYellowStatus()
                .setTimeout(
                        params.param("timeout",
                                DEFAULT_HEALTH_REQUEST_TIMEOUT)).execute()
                .actionGet();
        if (healthResponse.isTimedOut()) {
            listener.onError(new OperationFailedException(
                    "Failed to create index: " + index + "/" + type));
        }

        final XContentBuilder builder = jsonBuilder//
                .startObject()//
                .startObject(type)//
                .startObject("properties")//

                // @timestamp
                .startObject(timestampField)//
                .field("type", "date")//
                .field("format", "date_optional_time")//
                .endObject()//

                // user_id
                .startObject(userIdField)//
                .field("type", "long")//
                .endObject()//

                // system_id
                .startObject("system_id")//
                .field("type", "string")//
                .field("index", "not_analyzed")//
                .endObject()//

                .endObject()//
                .endObject()//
                .endObject();

        final PutMappingResponse mappingResponse = client.admin().indices()
                .preparePutMapping(index).setType(type).setSource(builder)
                .execute().actionGet();
        if (mappingResponse.isAcknowledged()) {
            fork(() -> execute(params, listener, requestMap, paramMap,
                    chain));
        } else {
            listener.onError(new OperationFailedException(
                    "Failed to create mapping for " + index + "/" + type));
        }
    } catch (final Exception e) {
        listener.onError(e);
    }
}