org.elasticsearch.cluster.health.ClusterHealthStatus Java Examples

The following examples show how to use org.elasticsearch.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: ClusterHealthRequest.java    From crate with Apache License 2.0 6 votes vote down vote up
public ClusterHealthRequest(StreamInput in) throws IOException {
    super(in);
    int size = in.readVInt();
    if (size == 0) {
        indices = Strings.EMPTY_ARRAY;
    } else {
        indices = new String[size];
        for (int i = 0; i < indices.length; i++) {
            indices[i] = in.readString();
        }
    }
    timeout = in.readTimeValue();
    if (in.readBoolean()) {
        waitForStatus = ClusterHealthStatus.fromValue(in.readByte());
    }
    waitForNoRelocatingShards = in.readBoolean();
    waitForActiveShards = ActiveShardCount.readFrom(in);
    waitForNodes = in.readString();
    if (in.readBoolean()) {
        waitForEvents = Priority.readFrom(in);
    }
    waitForNoInitializingShards = in.readBoolean();
}
 
Example #2
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 #3
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 #4
Source File: ElasticsearchPersistWriterIT.java    From streams with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public void prepareTestPersistWriter() throws Exception {

  testConfiguration = new ComponentConfigurator<>(ElasticsearchWriterConfiguration.class).detectConfiguration("ElasticsearchPersistWriterIT");
  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();
  if(indicesExistsResponse.isExists()) {
    DeleteIndexRequest deleteIndexRequest = Requests.deleteIndexRequest(testConfiguration.getIndex());
    DeleteIndexResponse deleteIndexResponse = testClient.admin().indices().delete(deleteIndexRequest).actionGet();
    assertTrue(deleteIndexResponse.isAcknowledged());
  }

}
 
Example #5
Source File: ElasticsearchReindexChildIT.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("ElasticsearchReindexChildIT");
  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 #6
Source File: TransportClusterStatsAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected ClusterStatsNodeResponse nodeOperation(ClusterStatsNodeRequest nodeRequest) {
    NodeInfo nodeInfo = nodeService.info(false, true, false, true, false, true, false, true);
    NodeStats nodeStats = nodeService.stats(CommonStatsFlags.NONE, true, true, true, false, true, false, false, false, false);
    List<ShardStats> shardsStats = new ArrayList<>();
    for (IndexService indexService : indicesService) {
        for (IndexShard indexShard : indexService) {
            if (indexShard.routingEntry() != null && indexShard.routingEntry().active()) {
                // only report on fully started shards
                shardsStats.add(new ShardStats(indexShard.routingEntry(), indexShard.shardPath(), new CommonStats(indexShard, SHARD_STATS_FLAGS), indexShard.commitStats()));
            }
        }
    }

    ClusterHealthStatus clusterStatus = null;
    if (clusterService.state().nodes().localNodeMaster()) {
        clusterStatus = new ClusterStateHealth(clusterService.state()).getStatus();
    }

    return new ClusterStatsNodeResponse(nodeInfo.getNode(), clusterStatus, nodeInfo, nodeStats, shardsStats.toArray(new ShardStats[shardsStats.size()]));

}
 
Example #7
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 #8
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 #9
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 #10
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 #11
Source File: TransportClusterHealthAction.java    From crate with Apache License 2.0 6 votes vote down vote up
private ClusterHealthResponse clusterHealth(ClusterHealthRequest request, ClusterState clusterState, int numberOfPendingTasks, int numberOfInFlightFetch,
                                            TimeValue pendingTaskTimeInQueue) {
    if (logger.isTraceEnabled()) {
        logger.trace("Calculating health based on state version [{}]", clusterState.version());
    }

    String[] concreteIndices;
    try {
        concreteIndices = indexNameExpressionResolver.concreteIndexNames(clusterState, request);
    } catch (IndexNotFoundException e) {
        // one of the specified indices is not there - treat it as RED.
        ClusterHealthResponse response = new ClusterHealthResponse(clusterState.getClusterName().value(), Strings.EMPTY_ARRAY, clusterState,
                numberOfPendingTasks, numberOfInFlightFetch, UnassignedInfo.getNumberOfDelayedUnassigned(clusterState),
                pendingTaskTimeInQueue);
        response.setStatus(ClusterHealthStatus.RED);
        return response;
    }

    return new ClusterHealthResponse(clusterState.getClusterName().value(), concreteIndices, clusterState, numberOfPendingTasks,
            numberOfInFlightFetch, UnassignedInfo.getNumberOfDelayedUnassigned(clusterState), pendingTaskTimeInQueue);
}
 
Example #12
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 #13
Source File: ClusterHealthRequest.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    int size = in.readVInt();
    if (size == 0) {
        indices = Strings.EMPTY_ARRAY;
    } else {
        indices = new String[size];
        for (int i = 0; i < indices.length; i++) {
            indices[i] = in.readString();
        }
    }
    timeout = readTimeValue(in);
    if (in.readBoolean()) {
        waitForStatus = ClusterHealthStatus.fromValue(in.readByte());
    }
    waitForRelocatingShards = in.readInt();
    waitForActiveShards = in.readInt();
    waitForNodes = in.readString();
    if (in.readBoolean()) {
        waitForEvents = Priority.readFrom(in);
    }
}
 
Example #14
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 #15
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 #16
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 #17
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 #18
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 #19
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 #20
Source File: TwitterHistoryElasticsearchIT.java    From streams with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public void prepareTest() throws Exception {

  testConfiguration = new StreamsConfigurator<>(TwitterHistoryElasticsearchConfiguration.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());
  };
}
 
Example #21
Source File: ElasticsearchParentChildWriterIT.java    From streams with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public void prepareTestParentChildPersistWriter() throws Exception {

  testConfiguration = new ComponentConfigurator<>(ElasticsearchWriterConfiguration.class).detectConfiguration("ElasticsearchParentChildWriterIT");
  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();
  if (indicesExistsResponse.isExists()) {
    DeleteIndexRequest deleteIndexRequest = Requests.deleteIndexRequest(testConfiguration.getIndex());
    DeleteIndexResponse deleteIndexResponse = testClient.admin().indices().delete(deleteIndexRequest).actionGet();
    assertTrue(deleteIndexResponse.isAcknowledged());
  }

  PutIndexTemplateRequestBuilder putTemplateRequestBuilder = testClient.admin().indices().preparePutTemplate("mappings");
  URL templateURL = ElasticsearchParentChildWriterIT.class.getResource("/ActivityChildObjectParent.json");
  ObjectNode template = MAPPER.readValue(templateURL, ObjectNode.class);
  String templateSource = MAPPER.writeValueAsString(template);
  putTemplateRequestBuilder.setSource(templateSource);

  testClient.admin().indices().putTemplate(putTemplateRequestBuilder.request()).actionGet();

  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());

  assert( files.size() > 0);
}
 
Example #22
Source File: ElasticsearchClientRest.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public boolean isClusterYellow() {
  try {
    byte status = getClusterHealth().getStatus().value();
    return status == ClusterHealthStatus.YELLOW.value() || status == ClusterHealthStatus.GREEN.value();
  } catch (IOException e) {
    log.info("Elasticsearch cluster not yet ready: {}", e.getMessage());
    log.trace("Elasticsearch cluster not yet ready: ", e);
  }
  return false;
}
 
Example #23
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 #24
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 #25
Source File: MongoElasticsearchSyncIT.java    From streams with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public void prepareTest() throws Exception {

  testConfiguration = new StreamsConfigurator<>(MongoElasticsearchSyncConfiguration.class).detectCustomConfiguration();
  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();
  assertFalse(indicesExistsResponse.isExists());
}
 
Example #26
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 #27
Source File: CaseController.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@GetMapping("/healthCheck")
public String 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 "Success";
}
 
Example #28
Source File: ElasticsearchReindexParentIT.java    From streams with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public void prepareTest() throws Exception {

  testConfiguration = new StreamsConfigurator<>(ElasticsearchReindexConfiguration.class).detectCustomConfiguration("ElasticsearchReindexParentIT");
  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();
  assertTrue(indicesExistsResponse.isExists());

  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();

  PutIndexTemplateRequestBuilder putTemplateRequestBuilder = testClient.admin().indices().preparePutTemplate("mappings");
  URL templateURL = ElasticsearchParentChildWriterIT.class.getResource("/ActivityChildObjectParent.json");
  ObjectNode template = MAPPER.readValue(templateURL, ObjectNode.class);
  String templateSource = MAPPER.writeValueAsString(template);
  putTemplateRequestBuilder.setSource(templateSource);

  testClient.admin().indices().putTemplate(putTemplateRequestBuilder.request()).actionGet();

  assertThat(count, not(0));

}
 
Example #29
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 #30
Source File: ElasticsearchPersistUpdaterIT.java    From streams with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public void prepareTestPersistUpdater() throws Exception {

  testConfiguration = new ComponentConfigurator<>(ElasticsearchWriterConfiguration.class).detectConfiguration("ElasticsearchPersistUpdaterIT");
  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());

}