Search in sources :

Example 1 with ClusterHealthRequest

use of org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest in project crate by crate.

the class DecommissioningService method decommission.

private void decommission() {
    // fail on new requests so that clients don't use this node anymore
    sqlOperations.disable();
    /*
         * setting this setting will cause the {@link DecommissionAllocationDecider} to prevent allocations onto this node
         *
         * nodeIds are part of the key to prevent conflicts if other nodes are being decommissioned in parallel
         */
    Settings settings = Settings.builder().put(DECOMMISSION_PREFIX + clusterService.localNode().getId(), true).build();
    updateSettingsAction.execute(new ClusterUpdateSettingsRequest().transientSettings(settings), new ActionListener<ClusterUpdateSettingsResponse>() {

        @Override
        public void onResponse(ClusterUpdateSettingsResponse clusterUpdateSettingsResponse) {
            // changing settings triggers AllocationService.reroute -> shards will be relocated
            // NOTE: it waits for ALL relocating shards, not just those that involve THIS node.
            ClusterHealthRequest request = new ClusterHealthRequest().waitForRelocatingShards(0).waitForEvents(Priority.LANGUID).timeout(gracefulStopTimeout);
            if (dataAvailability == DataAvailability.FULL) {
                request = request.waitForGreenStatus();
            } else {
                request = request.waitForYellowStatus();
            }
            final long startTime = System.nanoTime();
            healthAction.execute(request, new ActionListener<ClusterHealthResponse>() {

                @Override
                public void onResponse(ClusterHealthResponse clusterHealthResponse) {
                    exitIfNoActiveRequests(startTime);
                }

                @Override
                public void onFailure(Throwable e) {
                    forceStopOrAbort(e);
                }
            });
        }

        @Override
        public void onFailure(Throwable e) {
            logger.error("Couldn't set settings. Graceful shutdown failed", e);
        }
    });
}
Also used : ActionListener(org.elasticsearch.action.ActionListener) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) ClusterUpdateSettingsResponse(org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse) CrateSettings(io.crate.metadata.settings.CrateSettings) Settings(org.elasticsearch.common.settings.Settings) ClusterUpdateSettingsRequest(org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest)

Example 2 with ClusterHealthRequest

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

the class ESIntegTestCase method waitForRelocation.

/**
     * 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();
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest)

Example 3 with ClusterHealthRequest

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

the class RestClusterHealthAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    ClusterHealthRequest clusterHealthRequest = clusterHealthRequest(Strings.splitStringByCommaToArray(request.param("index")));
    clusterHealthRequest.local(request.paramAsBoolean("local", clusterHealthRequest.local()));
    clusterHealthRequest.masterNodeTimeout(request.paramAsTime("master_timeout", clusterHealthRequest.masterNodeTimeout()));
    clusterHealthRequest.timeout(request.paramAsTime("timeout", clusterHealthRequest.timeout()));
    String waitForStatus = request.param("wait_for_status");
    if (waitForStatus != null) {
        clusterHealthRequest.waitForStatus(ClusterHealthStatus.valueOf(waitForStatus.toUpperCase(Locale.ROOT)));
    }
    clusterHealthRequest.waitForNoRelocatingShards(request.paramAsBoolean("wait_for_no_relocating_shards", clusterHealthRequest.waitForNoRelocatingShards()));
    if (request.hasParam("wait_for_relocating_shards")) {
        // wait_for_relocating_shards has been removed in favor of wait_for_no_relocating_shards
        throw new IllegalArgumentException("wait_for_relocating_shards has been removed, " + "use wait_for_no_relocating_shards [true/false] instead");
    }
    String waitForActiveShards = request.param("wait_for_active_shards");
    if (waitForActiveShards != null) {
        clusterHealthRequest.waitForActiveShards(ActiveShardCount.parseString(waitForActiveShards));
    }
    clusterHealthRequest.waitForNodes(request.param("wait_for_nodes", clusterHealthRequest.waitForNodes()));
    if (request.param("wait_for_events") != null) {
        clusterHealthRequest.waitForEvents(Priority.valueOf(request.param("wait_for_events").toUpperCase(Locale.ROOT)));
    }
    return channel -> client.admin().cluster().health(clusterHealthRequest, new RestStatusToXContentListener<>(channel));
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) Priority(org.elasticsearch.common.Priority) Requests.clusterHealthRequest(org.elasticsearch.client.Requests.clusterHealthRequest) RestStatusToXContentListener(org.elasticsearch.rest.action.RestStatusToXContentListener) Set(java.util.Set) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) ActiveShardCount(org.elasticsearch.action.support.ActiveShardCount) Settings(org.elasticsearch.common.settings.Settings) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) Locale(java.util.Locale) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) ClusterHealthStatus(org.elasticsearch.cluster.health.ClusterHealthStatus) Collections(java.util.Collections) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest)

Example 4 with ClusterHealthRequest

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

the class RestIndicesAction method doCatRequest.

@Override
public RestChannelConsumer doCatRequest(final RestRequest request, final NodeClient client) {
    final String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    final ClusterStateRequest clusterStateRequest = new ClusterStateRequest();
    clusterStateRequest.clear().indices(indices).metaData(true);
    clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local()));
    clusterStateRequest.masterNodeTimeout(request.paramAsTime("master_timeout", clusterStateRequest.masterNodeTimeout()));
    final IndicesOptions strictExpandIndicesOptions = IndicesOptions.strictExpand();
    clusterStateRequest.indicesOptions(strictExpandIndicesOptions);
    return channel -> client.admin().cluster().state(clusterStateRequest, new RestActionListener<ClusterStateResponse>(channel) {

        @Override
        public void processResponse(final ClusterStateResponse clusterStateResponse) {
            final ClusterState state = clusterStateResponse.getState();
            final Index[] concreteIndices = indexNameExpressionResolver.concreteIndices(state, strictExpandIndicesOptions, indices);
            assert concreteIndices.length == state.metaData().getIndices().size();
            ClusterHealthRequest clusterHealthRequest = Requests.clusterHealthRequest(indices);
            clusterHealthRequest.local(request.paramAsBoolean("local", clusterHealthRequest.local()));
            client.admin().cluster().health(clusterHealthRequest, new RestActionListener<ClusterHealthResponse>(channel) {

                @Override
                public void processResponse(final ClusterHealthResponse clusterHealthResponse) {
                    IndicesStatsRequest indicesStatsRequest = new IndicesStatsRequest();
                    indicesStatsRequest.indices(indices);
                    indicesStatsRequest.indicesOptions(strictExpandIndicesOptions);
                    indicesStatsRequest.all();
                    client.admin().indices().stats(indicesStatsRequest, new RestResponseListener<IndicesStatsResponse>(channel) {

                        @Override
                        public RestResponse buildResponse(IndicesStatsResponse indicesStatsResponse) throws Exception {
                            Table tab = buildTable(request, concreteIndices, clusterHealthResponse, indicesStatsResponse, state.metaData());
                            return RestTable.buildResponse(tab, channel);
                        }
                    });
                }
            });
        }
    });
}
Also used : MetaData(org.elasticsearch.cluster.metadata.MetaData) DateTimeZone(org.joda.time.DateTimeZone) Arrays(java.util.Arrays) GET(org.elasticsearch.rest.RestRequest.Method.GET) Table(org.elasticsearch.common.Table) IndexStats(org.elasticsearch.action.admin.indices.stats.IndexStats) Index(org.elasticsearch.index.Index) ClusterIndexHealth(org.elasticsearch.cluster.health.ClusterIndexHealth) Strings(org.elasticsearch.common.Strings) HashSet(java.util.HashSet) IndicesStatsResponse(org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse) ClusterState(org.elasticsearch.cluster.ClusterState) IndicesStatsRequest(org.elasticsearch.action.admin.indices.stats.IndicesStatsRequest) Settings(org.elasticsearch.common.settings.Settings) Locale(java.util.Locale) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) Requests(org.elasticsearch.client.Requests) RestResponseListener(org.elasticsearch.rest.action.RestResponseListener) RestResponse(org.elasticsearch.rest.RestResponse) DateTime(org.joda.time.DateTime) Set(java.util.Set) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) RestController(org.elasticsearch.rest.RestController) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ClusterStateRequest(org.elasticsearch.action.admin.cluster.state.ClusterStateRequest) ClusterHealthStatus(org.elasticsearch.cluster.health.ClusterHealthStatus) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Collections(java.util.Collections) RestActionListener(org.elasticsearch.rest.action.RestActionListener) ClusterState(org.elasticsearch.cluster.ClusterState) IndicesStatsResponse(org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse) Table(org.elasticsearch.common.Table) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) RestResponse(org.elasticsearch.rest.RestResponse) ClusterStateRequest(org.elasticsearch.action.admin.cluster.state.ClusterStateRequest) RestActionListener(org.elasticsearch.rest.action.RestActionListener) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) IndicesStatsRequest(org.elasticsearch.action.admin.indices.stats.IndicesStatsRequest)

Example 5 with ClusterHealthRequest

use of org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest in project YCSB by brianfrankcooper.

the class ElasticsearchClient method init.

/**
   * Initialize any state for this DB. Called once per DB instance; there is one
   * DB instance per client thread.
   */
@Override
public void init() throws DBException {
    final Properties props = getProperties();
    // Check if transport client needs to be used (To connect to multiple
    // elasticsearch nodes)
    remoteMode = Boolean.parseBoolean(props.getProperty("es.remote", "false"));
    final String pathHome = props.getProperty("path.home");
    // when running in embedded mode, require path.home
    if (!remoteMode && (pathHome == null || pathHome.isEmpty())) {
        throw new IllegalArgumentException("path.home must be specified when running in embedded mode");
    }
    this.indexKey = props.getProperty("es.index.key", DEFAULT_INDEX_KEY);
    int numberOfShards = parseIntegerProperty(props, "es.number_of_shards", NUMBER_OF_SHARDS);
    int numberOfReplicas = parseIntegerProperty(props, "es.number_of_replicas", NUMBER_OF_REPLICAS);
    Boolean newdb = Boolean.parseBoolean(props.getProperty("es.newdb", "false"));
    Builder settings = Settings.settingsBuilder().put("cluster.name", DEFAULT_CLUSTER_NAME).put("node.local", Boolean.toString(!remoteMode)).put("path.home", pathHome);
    // if properties file contains elasticsearch user defined properties
    // add it to the settings file (will overwrite the defaults).
    settings.put(props);
    final String clusterName = settings.get("cluster.name");
    System.err.println("Elasticsearch starting node = " + clusterName);
    System.err.println("Elasticsearch node path.home = " + settings.get("path.home"));
    System.err.println("Elasticsearch Remote Mode = " + remoteMode);
    // Remote mode support for connecting to remote elasticsearch cluster
    if (remoteMode) {
        settings.put("client.transport.sniff", true).put("client.transport.ignore_cluster_name", false).put("client.transport.ping_timeout", "30s").put("client.transport.nodes_sampler_interval", "30s");
        // Default it to localhost:9300
        String[] nodeList = props.getProperty("es.hosts.list", DEFAULT_REMOTE_HOST).split(",");
        System.out.println("Elasticsearch Remote Hosts = " + props.getProperty("es.hosts.list", DEFAULT_REMOTE_HOST));
        TransportClient tClient = TransportClient.builder().settings(settings).build();
        for (String h : nodeList) {
            String[] nodes = h.split(":");
            try {
                tClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(nodes[0]), Integer.parseInt(nodes[1])));
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("Unable to parse port number.", e);
            } catch (UnknownHostException e) {
                throw new IllegalArgumentException("Unable to Identify host.", e);
            }
        }
        client = tClient;
    } else {
        // Start node only if transport client mode is disabled
        node = nodeBuilder().clusterName(clusterName).settings(settings).node();
        node.start();
        client = node.client();
    }
    final boolean exists = client.admin().indices().exists(Requests.indicesExistsRequest(indexKey)).actionGet().isExists();
    if (exists && newdb) {
        client.admin().indices().prepareDelete(indexKey).execute().actionGet();
    }
    if (!exists || newdb) {
        client.admin().indices().create(new CreateIndexRequest(indexKey).settings(Settings.builder().put("index.number_of_shards", numberOfShards).put("index.number_of_replicas", numberOfReplicas).put("index.mapping._id.indexed", true))).actionGet();
    }
    client.admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet();
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) UnknownHostException(java.net.UnknownHostException) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) NodeBuilder.nodeBuilder(org.elasticsearch.node.NodeBuilder.nodeBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) Builder(org.elasticsearch.common.settings.Settings.Builder) Properties(java.util.Properties) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest)

Aggregations

ClusterHealthRequest (org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest)11 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)8 IOException (java.io.IOException)3 Settings (org.elasticsearch.common.settings.Settings)3 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2 Locale (java.util.Locale)2 Set (java.util.Set)2 NodeClient (org.elasticsearch.client.node.NodeClient)2 ClusterState (org.elasticsearch.cluster.ClusterState)2 ClusterHealthStatus (org.elasticsearch.cluster.health.ClusterHealthStatus)2 Strings (org.elasticsearch.common.Strings)2 RestController (org.elasticsearch.rest.RestController)2 RestRequest (org.elasticsearch.rest.RestRequest)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 CrateSettings (io.crate.metadata.settings.CrateSettings)1 URI (java.net.URI)1 UnknownHostException (java.net.UnknownHostException)1 Arrays (java.util.Arrays)1