Search in sources :

Example 46 with ClusterHealthResponse

use of org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse in project spring-boot by spring-projects.

the class ElasticsearchHealthIndicator method doHealthCheck.

@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
    List<String> indices = this.properties.getIndices();
    ClusterHealthResponse response = this.client.admin().cluster().health(Requests.clusterHealthRequest(indices.isEmpty() ? allIndices : indices.toArray(new String[indices.size()]))).actionGet(this.properties.getResponseTimeout());
    switch(response.getStatus()) {
        case GREEN:
        case YELLOW:
            builder.up();
            break;
        case RED:
        default:
            builder.down();
            break;
    }
    builder.withDetail("clusterName", response.getClusterName());
    builder.withDetail("numberOfNodes", response.getNumberOfNodes());
    builder.withDetail("numberOfDataNodes", response.getNumberOfDataNodes());
    builder.withDetail("activePrimaryShards", response.getActivePrimaryShards());
    builder.withDetail("activeShards", response.getActiveShards());
    builder.withDetail("relocatingShards", response.getRelocatingShards());
    builder.withDetail("initializingShards", response.getInitializingShards());
    builder.withDetail("unassignedShards", response.getUnassignedShards());
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)

Example 47 with ClusterHealthResponse

use of org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse in project elasticsearch by elastic.

the class ClusterStatsIT method waitForNodes.

private void waitForNodes(int numNodes) {
    ClusterHealthResponse actionGet = client().admin().cluster().health(Requests.clusterHealthRequest().waitForEvents(Priority.LANGUID).waitForNodes(Integer.toString(numNodes))).actionGet();
    assertThat(actionGet.isTimedOut(), is(false));
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)

Example 48 with ClusterHealthResponse

use of org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse in project elasticsearch by elastic.

the class RestIndicesActionTests method testBuildTable.

public void testBuildTable() {
    final Settings settings = Settings.EMPTY;
    final RestController restController = new RestController(settings, Collections.emptySet(), null, null, null);
    final RestIndicesAction action = new RestIndicesAction(settings, restController, new IndexNameExpressionResolver(settings));
    // build a (semi-)random table
    final int numIndices = randomIntBetween(0, 5);
    Index[] indices = new Index[numIndices];
    for (int i = 0; i < numIndices; i++) {
        indices[i] = new Index(randomAsciiOfLength(5), UUIDs.randomBase64UUID());
    }
    final MetaData.Builder metaDataBuilder = MetaData.builder();
    for (final Index index : indices) {
        metaDataBuilder.put(IndexMetaData.builder(index.getName()).settings(Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetaData.SETTING_INDEX_UUID, index.getUUID())).creationDate(System.currentTimeMillis()).numberOfShards(1).numberOfReplicas(1).state(IndexMetaData.State.OPEN));
    }
    final MetaData metaData = metaDataBuilder.build();
    final ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).build();
    final String[] indicesStr = new String[indices.length];
    for (int i = 0; i < indices.length; i++) {
        indicesStr[i] = indices[i].getName();
    }
    final ClusterHealthResponse clusterHealth = new ClusterHealthResponse(clusterState.getClusterName().value(), indicesStr, clusterState, 0, 0, 0, TimeValue.timeValueMillis(1000L));
    final Table table = action.buildTable(new FakeRestRequest(), indices, clusterHealth, randomIndicesStatsResponse(indices), metaData);
    // now, verify the table is correct
    int count = 0;
    List<Table.Cell> headers = table.getHeaders();
    assertThat(headers.get(count++).value, equalTo("health"));
    assertThat(headers.get(count++).value, equalTo("status"));
    assertThat(headers.get(count++).value, equalTo("index"));
    assertThat(headers.get(count++).value, equalTo("uuid"));
    List<List<Table.Cell>> rows = table.getRows();
    assertThat(rows.size(), equalTo(indices.length));
    // TODO: more to verify (e.g. randomize cluster health, num primaries, num replicas, etc)
    for (int i = 0; i < rows.size(); i++) {
        count = 0;
        final List<Table.Cell> row = rows.get(i);
        // all are red because cluster state doesn't have routing entries
        assertThat(row.get(count++).value, equalTo("red*"));
        // all are OPEN for now
        assertThat(row.get(count++).value, equalTo("open"));
        assertThat(row.get(count++).value, equalTo(indices[i].getName()));
        assertThat(row.get(count++).value, equalTo(indices[i].getUUID()));
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) Table(org.elasticsearch.common.Table) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) RestController(org.elasticsearch.rest.RestController) Index(org.elasticsearch.index.Index) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Settings(org.elasticsearch.common.settings.Settings)

Example 49 with ClusterHealthResponse

use of org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse in project elasticsearch by elastic.

the class QuorumGatewayIT method testQuorumRecovery.

public void testQuorumRecovery() throws Exception {
    logger.info("--> starting 3 nodes");
    // we are shutting down nodes - make sure we don't have 2 clusters if we test network
    internalCluster().startNodes(3);
    createIndex("test");
    ensureGreen();
    final NumShards test = getNumShards("test");
    logger.info("--> indexing...");
    client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("field", "value1").endObject()).get();
    //We don't check for failures in the flush response: if we do we might get the following:
    // FlushNotAllowedEngineException[[test][1] recovery is in progress, flush [COMMIT_TRANSLOG] is not allowed]
    flush();
    client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject().field("field", "value2").endObject()).get();
    refresh();
    for (int i = 0; i < 10; i++) {
        assertHitCount(client().prepareSearch().setSize(0).setQuery(matchAllQuery()).get(), 2L);
    }
    logger.info("--> restart all nodes");
    internalCluster().fullRestart(new RestartCallback() {

        @Override
        public Settings onNodeStopped(String nodeName) throws Exception {
            return null;
        }

        @Override
        public void doAfterNodes(int numNodes, final Client activeClient) throws Exception {
            if (numNodes == 1) {
                assertTrue(awaitBusy(() -> {
                    logger.info("--> running cluster_health (wait for the shards to startup)");
                    ClusterHealthResponse clusterHealth = activeClient.admin().cluster().health(clusterHealthRequest().waitForYellowStatus().waitForNodes("2").waitForActiveShards(test.numPrimaries * 2)).actionGet();
                    logger.info("--> done cluster_health, status {}", clusterHealth.getStatus());
                    return (!clusterHealth.isTimedOut()) && clusterHealth.getStatus() == ClusterHealthStatus.YELLOW;
                }, 30, TimeUnit.SECONDS));
                logger.info("--> one node is closed -- index 1 document into the remaining nodes");
                activeClient.prepareIndex("test", "type1", "3").setSource(jsonBuilder().startObject().field("field", "value3").endObject()).get();
                assertNoFailures(activeClient.admin().indices().prepareRefresh().get());
                for (int i = 0; i < 10; i++) {
                    assertHitCount(activeClient.prepareSearch().setSize(0).setQuery(matchAllQuery()).get(), 3L);
                }
            }
        }
    });
    logger.info("--> all nodes are started back, verifying we got the latest version");
    logger.info("--> running cluster_health (wait for the shards to startup)");
    ensureGreen();
    for (int i = 0; i < 10; i++) {
        assertHitCount(client().prepareSearch().setSize(0).setQuery(matchAllQuery()).get(), 3L);
    }
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) RestartCallback(org.elasticsearch.test.InternalTestCluster.RestartCallback) Client(org.elasticsearch.client.Client) Settings(org.elasticsearch.common.settings.Settings)

Example 50 with ClusterHealthResponse

use of org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse in project elasticsearch by elastic.

the class RelocationIT method testSimpleRelocationNoIndexing.

public void testSimpleRelocationNoIndexing() {
    logger.info("--> starting [node1] ...");
    final String node_1 = internalCluster().startNode();
    logger.info("--> creating test index ...");
    prepareCreate("test", Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0)).get();
    logger.info("--> index 10 docs");
    for (int i = 0; i < 10; i++) {
        client().prepareIndex("test", "type", Integer.toString(i)).setSource("field", "value" + i).execute().actionGet();
    }
    logger.info("--> flush so we have an actual index");
    client().admin().indices().prepareFlush().execute().actionGet();
    logger.info("--> index more docs so we have something in the translog");
    for (int i = 10; i < 20; i++) {
        client().prepareIndex("test", "type", Integer.toString(i)).setSource("field", "value" + i).execute().actionGet();
    }
    logger.info("--> verifying count");
    client().admin().indices().prepareRefresh().execute().actionGet();
    assertThat(client().prepareSearch("test").setSize(0).execute().actionGet().getHits().getTotalHits(), equalTo(20L));
    logger.info("--> start another node");
    final String node_2 = internalCluster().startNode();
    ClusterHealthResponse clusterHealthResponse = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForNodes("2").execute().actionGet();
    assertThat(clusterHealthResponse.isTimedOut(), equalTo(false));
    logger.info("--> relocate the shard from node1 to node2");
    client().admin().cluster().prepareReroute().add(new MoveAllocationCommand("test", 0, node_1, node_2)).execute().actionGet();
    clusterHealthResponse = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForNoRelocatingShards(true).setTimeout(ACCEPTABLE_RELOCATION_TIME).execute().actionGet();
    assertThat(clusterHealthResponse.isTimedOut(), equalTo(false));
    logger.info("--> verifying count again...");
    client().admin().indices().prepareRefresh().execute().actionGet();
    assertThat(client().prepareSearch("test").setSize(0).execute().actionGet().getHits().getTotalHits(), equalTo(20L));
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand)

Aggregations

ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)93 Settings (org.elasticsearch.common.settings.Settings)25 Client (org.elasticsearch.client.Client)22 ClusterState (org.elasticsearch.cluster.ClusterState)16 IOException (java.io.IOException)11 SearchResponse (org.elasticsearch.action.search.SearchResponse)10 MoveAllocationCommand (org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand)10 CloseIndexResponse (org.elasticsearch.action.admin.indices.close.CloseIndexResponse)9 OpenIndexResponse (org.elasticsearch.action.admin.indices.open.OpenIndexResponse)9 ClusterHealthRequest (org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest)8 ClusterStateResponse (org.elasticsearch.action.admin.cluster.state.ClusterStateResponse)7 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)7 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)7 Index (org.elasticsearch.index.Index)7 ArrayList (java.util.ArrayList)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 Path (java.nio.file.Path)5 ExecutionException (java.util.concurrent.ExecutionException)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 NodesInfoResponse (org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse)4