/** * Checks whether we can perform a write based on the required active shard count setting. * Returns **null* if OK to proceed, or a string describing the reason to stop */ protected String checkActiveShardCount() { final ShardId shardId = primary.routingEntry().shardId(); final ActiveShardCount waitForActiveShards = request.waitForActiveShards(); if (waitForActiveShards == ActiveShardCount.NONE) { return null; // not waiting for any shards } final IndexShardRoutingTable shardRoutingTable = primary.getReplicationGroup().getRoutingTable(); if (waitForActiveShards.enoughShardsActive(shardRoutingTable)) { return null; } else { final String resolvedShards = waitForActiveShards == ActiveShardCount.ALL ? Integer.toString(shardRoutingTable.shards().size()) : waitForActiveShards.toString(); logger.trace("[{}] not enough active copies to meet shard count of [{}] (have {}, needed {}), scheduling a retry. op [{}], " + "request [{}]", shardId, waitForActiveShards, shardRoutingTable.activeShards().size(), resolvedShards, opType, request); return "Not enough active copies to meet shard count of [" + waitForActiveShards + "] (have " + shardRoutingTable.activeShards().size() + ", needed " + resolvedShards + ")."; } }
private static ActiveShardCount get(final int value) { switch (value) { case ACTIVE_SHARD_COUNT_DEFAULT: return DEFAULT; case ALL_ACTIVE_SHARDS: return ALL; case 1: return ONE; case 0: return NONE; default: assert value > 1; return new ActiveShardCount(value); } }
/** * Sets the number of shard copies that must be active across all indices before getting the * health status. Defaults to {@link ActiveShardCount#NONE}, meaning we don't wait on any active shards. * Set this value to {@link ActiveShardCount#ALL} to wait for all shards (primary and * all replicas) to be active across all indices in the cluster. Otherwise, use * {@link ActiveShardCount#from(int)} to set this value to any non-negative integer, up to the * total number of shard copies to wait for. */ public ClusterHealthRequest waitForActiveShards(ActiveShardCount waitForActiveShards) { if (waitForActiveShards.equals(ActiveShardCount.DEFAULT)) { // the default for cluster health request is 0, not 1 this.waitForActiveShards = ActiveShardCount.NONE; } else { this.waitForActiveShards = waitForActiveShards; } return this; }
@Override public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { RolloverRequest rolloverIndexRequest = new RolloverRequest(request.param("index"), request.param("new_index")); request.applyContentParser(rolloverIndexRequest::fromXContent); rolloverIndexRequest.dryRun(request.paramAsBoolean("dry_run", false)); rolloverIndexRequest.timeout(request.paramAsTime("timeout", rolloverIndexRequest.timeout())); rolloverIndexRequest.masterNodeTimeout(request.paramAsTime("master_timeout", rolloverIndexRequest.masterNodeTimeout())); rolloverIndexRequest.getCreateIndexRequest().waitForActiveShards( ActiveShardCount.parseString(request.param("wait_for_active_shards"))); return channel -> client.admin().indices().rolloverIndex(rolloverIndexRequest, new RestToXContentListener<>(channel)); } }
@Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); waitForActiveShards.writeTo(out); out.writeVInt(requests.size()); for (DocWriteRequest<?> request : requests) { DocWriteRequest.writeDocumentRequest(out, request); } refreshPolicy.writeTo(out); out.writeTimeValue(timeout); }
/** * A shortcut for {@link #setWaitForActiveShards(ActiveShardCount)} where the numerical * shard count is passed in, instead of having to first call {@link ActiveShardCount#from(int)} * to get the ActiveShardCount. */ public void setWaitForActiveShards(final int waitForActiveShards) { setWaitForActiveShards(ActiveShardCount.from(waitForActiveShards)); }
@Override public void readFrom(StreamInput in) throws IOException { super.readFrom(in); waitForActiveShards = ActiveShardCount.readFrom(in); int size = in.readVInt(); for (int i = 0; i < size; i++) { requests.add(DocWriteRequest.readDocumentRequest(in)); } refreshPolicy = RefreshPolicy.readFrom(in); timeout = in.readTimeValue(); }
Params withWaitForActiveShards(ActiveShardCount activeShardCount, ActiveShardCount defaultActiveShardCount) { if (activeShardCount != null && activeShardCount != defaultActiveShardCount) { return putParam("wait_for_active_shards", activeShardCount.toString().toLowerCase(Locale.ROOT)); } return this; }
if (activeShardCount.enoughShardsActive(state, indexNames)) { onResult.accept(true); } else { final Predicate<ClusterState> shardsAllocatedPredicate = newState -> activeShardCount.enoughShardsActive(newState, indexNames);
/** * Get an ActiveShardCount instance for the given value. The value is first validated to ensure * it is a valid shard count and throws an IllegalArgumentException if validation fails. Valid * values are any non-negative number. Directly use {@link ActiveShardCount#DEFAULT} for the * default value (which is one shard copy) or {@link ActiveShardCount#ALL} to specify all the shards. */ public static ActiveShardCount from(final int value) { if (value < 0) { throw new IllegalArgumentException("shard count cannot be a negative value"); } return get(value); }
waitForCounter++; if (request.waitForActiveShards().equals(ActiveShardCount.NONE) == false) { ActiveShardCount waitForActiveShards = request.waitForActiveShards(); assert waitForActiveShards.equals(ActiveShardCount.DEFAULT) == false : "waitForActiveShards must not be DEFAULT on the request object, instead it should be NONE"; if (waitForActiveShards.equals(ActiveShardCount.ALL)) { if (response.getUnassignedShards() == 0 && response.getInitializingShards() == 0) { } else if (waitForActiveShards.enoughShardsActive(response.getActiveShards())) {
if (waitForActiveShards.validate(numberOfReplicas) == false) { throw new IllegalArgumentException("invalid " + SETTING_WAIT_FOR_ACTIVE_SHARDS.getKey() + "[" + waitForActiveShards + "]: cannot be greater than " +
@Override public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { OpenIndexRequest openIndexRequest = new OpenIndexRequest(Strings.splitStringByCommaToArray(request.param("index"))); openIndexRequest.timeout(request.paramAsTime("timeout", openIndexRequest.timeout())); openIndexRequest.masterNodeTimeout(request.paramAsTime("master_timeout", openIndexRequest.masterNodeTimeout())); openIndexRequest.indicesOptions(IndicesOptions.fromRequest(request, openIndexRequest.indicesOptions())); String waitForActiveShards = request.param("wait_for_active_shards"); if (waitForActiveShards != null) { openIndexRequest.waitForActiveShards(ActiveShardCount.parseString(waitForActiveShards)); } return channel -> client.admin().indices().open(openIndexRequest, new RestToXContentListener<>(channel)); } }
@Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); out.writeStringArray(indices); indicesOptions.writeIndicesOptions(out); if (out.getVersion().onOrAfter(Version.V_6_1_0)) { waitForActiveShards.writeTo(out); } } }
/** * A shortcut for {@link #setWaitForActiveShards(ActiveShardCount)} where the numerical * shard count is passed in, instead of having to first call {@link ActiveShardCount#from(int)} * to get the ActiveShardCount. */ public ResizeRequestBuilder setWaitForActiveShards(final int waitForActiveShards) { return setWaitForActiveShards(ActiveShardCount.from(waitForActiveShards)); }
@Override public void readFrom(StreamInput in) throws IOException { super.readFrom(in); indices = in.readStringArray(); indicesOptions = IndicesOptions.readIndicesOptions(in); if (in.getVersion().onOrAfter(Version.V_6_1_0)) { waitForActiveShards = ActiveShardCount.readFrom(in); } }
Params withWaitForActiveShards(ActiveShardCount currentActiveShardCount, ActiveShardCount defaultActiveShardCount) { if (currentActiveShardCount != null && currentActiveShardCount != defaultActiveShardCount) { return putParam("wait_for_active_shards", currentActiveShardCount.toString().toLowerCase(Locale.ROOT)); } return this; }
if (waitForActiveShards.enoughShardsActive(shardRouting.value) == false) {
public static ActiveShardCount readFrom(final StreamInput in) throws IOException { return get(in.readInt()); }
waitForActiveShards = tmpImd.getWaitForActiveShards(); if (waitForActiveShards.validate(tmpImd.getNumberOfReplicas()) == false) { throw new IllegalArgumentException("invalid wait_for_active_shards[" + request.waitForActiveShards() + "]: cannot be greater than number of shard copies [" +