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

The following examples show how to use org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest. 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: TransportNodePrometheusMetricsAction.java    From elasticsearch-prometheus-exporter with Apache License 2.0 6 votes vote down vote up
private AsyncAction(ActionListener<NodePrometheusMetricsResponse> listener) {
    this.listener = listener;

    // Note: when using ClusterHealthRequest in Java, it pulls data at the shards level, according to ES source
    // code comment this is "so it is backward compatible with the transport client behaviour".
    // hence we are explicit about ClusterHealthRequest level and do not rely on defaults.
    // https://www.elastic.co/guide/en/elasticsearch/reference/6.4/cluster-health.html#request-params
    this.healthRequest = Requests.clusterHealthRequest().local(true);
    this.healthRequest.level(ClusterHealthRequest.Level.SHARDS);

    this.nodesStatsRequest = Requests.nodesStatsRequest("_local").clear().all();

    // Indices stats request is not "node-specific", it does not support any "_local" notion
    // it is broad-casted to all cluster nodes.
    this.indicesStatsRequest = isPrometheusIndices ? new IndicesStatsRequest() : null;

    // Cluster settings are get via ClusterStateRequest (see elasticsearch RestClusterGetSettingsAction for details)
    // We prefer to send it to master node (hence local=false; it should be set by default but we want to be sure).
    this.clusterStateRequest = isPrometheusClusterSettings ? Requests.clusterStateRequest()
            .clear().metadata(true).local(false) : null;
}
 
Example #2
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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #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: 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 #14
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 #15
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 #16
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 #17
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 #18
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 #19
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 #20
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 #21
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 #22
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 #23
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 #24
Source File: AuditlogTest.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
@Test
public void testClusterHealthRequest() {
    Settings settings = Settings.builder()
            .put("opendistro_security.audit.type", TestAuditlogImpl.class.getName())
            .put(ConfigConstants.OPENDISTRO_SECURITY_AUDIT_CONFIG_DISABLED_TRANSPORT_CATEGORIES, "NONE")
            .put("opendistro_security.audit.threadpool.size", 0)
            .build();
    AbstractAuditLog al = new AuditLogImpl(settings, null, null, AbstractSecurityUnitTest.MOCK_POOL, null, cs);
    TestAuditlogImpl.clear();
    al.logGrantedPrivileges("indices:data/read/search", new ClusterHealthRequest(), null);
    Assert.assertEquals(1, TestAuditlogImpl.messages.size());
}
 
Example #25
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 #26
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());

}
 
Example #27
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 #28
Source File: InitializerCommand.java    From foxtrot with Apache License 2.0 5 votes vote down vote up
@Override
protected void run(Bootstrap<FoxtrotServerConfiguration> bootstrap, Namespace namespace, FoxtrotServerConfiguration configuration)
        throws Exception {
    ElasticsearchConfig esConfig = configuration.getElasticsearch();
    ElasticsearchConnection connection = new ElasticsearchConnection(esConfig);
    connection.start();

    ClusterHealthResponse clusterHealth = connection.getClient()
            .admin()
            .cluster()
            .health(new ClusterHealthRequest())
            .actionGet();
    int numDataNodes = clusterHealth.getNumberOfDataNodes();
    int numReplicas = (numDataNodes < 2) ? 0 : 1;

    logger.info("# data nodes: {}, Setting replica count to: {}", numDataNodes, numReplicas);

    createMetaIndex(connection, ElasticsearchConsolePersistence.INDEX, numReplicas);
    createMetaIndex(connection, ElasticsearchConsolePersistence.INDEX_V2, numReplicas);
    createMetaIndex(connection, TableMapStore.TABLE_META_INDEX, numReplicas);
    createMetaIndex(connection, ElasticsearchConsolePersistence.INDEX_HISTORY, numReplicas);
    createMetaIndex(connection, FqlStoreServiceImpl.FQL_STORE_INDEX, numReplicas);

    logger.info("Creating mapping");
    PutIndexTemplateRequest putIndexTemplateRequest = ElasticsearchUtils.getClusterTemplateMapping();
    PutIndexTemplateResponse response = connection.getClient()
            .admin()
            .indices()
            .putTemplate(putIndexTemplateRequest)
            .actionGet();
    logger.info("Created mapping: {}", response.isAcknowledged());

    logger.info("Creating hbase table");
    HBaseUtil.createTable(configuration.getHbase(), configuration.getHbase()
            .getTableName());
}
 
Example #29
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 #30
Source File: LogstashIT.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private static RestHighLevelClient createClient() throws IOException {

        // Instantiate the client.
        LOGGER.info("instantiating the ES client");
        final HttpHost httpHost = new HttpHost(HOST_NAME, MavenHardcodedConstants.ES_PORT);
        final RestClientBuilder clientBuilder =
                RestClient.builder(httpHost);
        final RestHighLevelClient client = new RestHighLevelClient(clientBuilder);

        // Verify the connection.
        LOGGER.info("verifying the ES connection");
        final ClusterHealthResponse healthResponse = client
                .cluster()
                .health(new ClusterHealthRequest(), RequestOptions.DEFAULT);
        Assertions
                .assertThat(healthResponse.getStatus())
                .isNotEqualTo(ClusterHealthStatus.RED);

        // Delete the index.
        LOGGER.info("deleting the ES index");
        final DeleteIndexRequest deleteRequest =
                new DeleteIndexRequest(MavenHardcodedConstants.ES_INDEX_NAME);
        try {
            final AcknowledgedResponse deleteResponse = client
                    .indices()
                    .delete(deleteRequest, RequestOptions.DEFAULT);
            Assertions
                    .assertThat(deleteResponse.isAcknowledged())
                    .isTrue();
        } catch (ElasticsearchStatusException error) {
            Assertions.assertThat(error)
                    .satisfies(ignored -> Assertions
                            .assertThat(error.status())
                            .isEqualTo(RestStatus.NOT_FOUND));
        }

        return client;

    }