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