Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Close Index Response #39687

Merged
merged 11 commits into from
May 23, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,40 @@
- is_true: acknowledged
- match: { acknowledged: true }
- match: { shards_acknowledged: true }
---
"Close index response with result per index":
- skip:
version: " - 7.99.99"
reason: "close index response reports result per index starting version 8.0.0"

- do:
indices.create:
index: index_1
body:
settings:
number_of_replicas: 0

- do:
indices.create:
index: index_2
body:
settings:
number_of_replicas: 0

- do:
indices.create:
index: index_3
body:
settings:
number_of_replicas: 0

- do:
indices.close:
index: "index_*"

- match: { acknowledged: true }
- match: { shards_acknowledged: true }
- match: { indices.index_1.closed: true }
- match: { indices.index_2.closed: true }
- match: { indices.index_3.closed: true }

Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,40 @@
*/
package org.elasticsearch.action.admin.indices.close;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.Version;
import org.elasticsearch.action.support.DefaultShardOperationFailedException;
import org.elasticsearch.action.support.master.ShardsAcknowledgedResponse;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.Index;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

import static java.util.Collections.emptyList;
import static java.util.Collections.unmodifiableList;

public class CloseIndexResponse extends ShardsAcknowledgedResponse {

private List<IndexResult> indices;

CloseIndexResponse() {
}

public CloseIndexResponse(final boolean acknowledged, final boolean shardsAcknowledged) {
public CloseIndexResponse(final boolean acknowledged, final boolean shardsAcknowledged, final List<IndexResult> indices) {
super(acknowledged, shardsAcknowledged);
this.indices = unmodifiableList(Objects.requireNonNull(indices));
}

public List<IndexResult> getIndices() {
return indices;
}

@Override
Expand All @@ -40,6 +60,11 @@ public void readFrom(StreamInput in) throws IOException {
if (in.getVersion().onOrAfter(Version.V_7_2_0)) {
readShardsAcknowledged(in);
}
if (in.getVersion().onOrAfter(Version.V_8_0_0)) {
indices = unmodifiableList(in.readList(IndexResult::new));
} else {
indices = unmodifiableList(emptyList());
}
}

@Override
Expand All @@ -48,5 +73,225 @@ public void writeTo(StreamOutput out) throws IOException {
if (out.getVersion().onOrAfter(Version.V_7_2_0)) {
writeShardsAcknowledged(out);
}
if (out.getVersion().onOrAfter(Version.V_8_0_0)) {
out.writeList(indices);
}
}

protected void addCustomFields(final XContentBuilder builder, final Params params) throws IOException {
super.addCustomFields(builder, params);
builder.startObject("indices");
for (IndexResult index : indices) {
index.toXContent(builder, params);
}
builder.endObject();
}

@Override
public String toString() {
return Strings.toString(this);
}

public static class IndexResult implements Writeable, ToXContentFragment {

private final Index index;
private final @Nullable Exception exception;
private final @Nullable ShardResult[] shards;

public IndexResult(final Index index) {
this(index, null, null);
}

public IndexResult(final Index index, final Exception failure) {
this(index, Objects.requireNonNull(failure), null);
}

public IndexResult(final Index index, final ShardResult[] shards) {
this(index, null, Objects.requireNonNull(shards));
}

private IndexResult(final Index index, @Nullable final Exception exception, @Nullable final ShardResult[] shards) {
this.index = Objects.requireNonNull(index);
this.exception = exception;
this.shards = shards;
}

IndexResult(final StreamInput in) throws IOException {
this.index = new Index(in);
this.exception = in.readException();
this.shards = in.readOptionalArray(ShardResult::new, ShardResult[]::new);
}

@Override
public void writeTo(final StreamOutput out) throws IOException {
index.writeTo(out);
out.writeException(exception);
out.writeOptionalArray(shards);
}

public Index getIndex() {
return index;
}

public Exception getException() {
return exception;
}

public ShardResult[] getShards() {
return shards;
}

public boolean hasFailures() {
if (exception != null) {
return true;
}
if (shards != null) {
for (ShardResult shard : shards) {
if (shard.hasFailures()) {
return true;
}
}
}
return false;
}

@Override
public XContentBuilder toXContent(final XContentBuilder builder, final Params params) throws IOException {
builder.startObject(index.getName());
{
if (hasFailures()) {
builder.field("closed", false);
if (exception != null) {
builder.startObject("exception");
ElasticsearchException.generateFailureXContent(builder, params, exception, true);
builder.endObject();
} else {
builder.startObject("failedShards");
for (ShardResult shard : shards) {
if (shard.hasFailures()) {
shard.toXContent(builder, params);
}
}
builder.endObject();
}
} else {
builder.field("closed", true);
}
}
return builder.endObject();
}

@Override
public String toString() {
return Strings.toString(this);
}
}

public static class ShardResult implements Writeable, ToXContentFragment {

private final int id;
private final ShardResult.Failure[] failures;

public ShardResult(final int id, final Failure[] failures) {
this.id = id;
this.failures = failures;
}

ShardResult(final StreamInput in) throws IOException {
this.id = in.readVInt();
this.failures = in.readOptionalArray(Failure::readFailure, ShardResult.Failure[]::new);
}

@Override
public void writeTo(final StreamOutput out) throws IOException {
out.writeVInt(id);
out.writeOptionalArray(failures);
}

public boolean hasFailures() {
return failures != null && failures.length > 0;
}

public int getId() {
return id;
}

public Failure[] getFailures() {
return failures;
}

@Override
public XContentBuilder toXContent(final XContentBuilder builder, final Params params) throws IOException {
builder.startObject(String.valueOf(id));
{
builder.startArray("failures");
if (failures != null) {
for (Failure failure : failures) {
builder.startObject();
failure.toXContent(builder, params);
builder.endObject();
}
}
builder.endArray();
}
return builder.endObject();
}

@Override
public String toString() {
return Strings.toString(this);
}

public static class Failure extends DefaultShardOperationFailedException implements Writeable {

private @Nullable String nodeId;

private Failure() {
}

public Failure(final String index, final int shardId, final Throwable reason) {
this(index, shardId, reason, null);
}

public Failure(final String index, final int shardId, final Throwable reason, final String nodeId) {
super(index, shardId, reason);
this.nodeId = nodeId;
}

public String getNodeId() {
return nodeId;
}

@Override
public void readFrom(final StreamInput in) throws IOException {
super.readFrom(in);
nodeId = in.readOptionalString();
}

@Override
public void writeTo(final StreamOutput out) throws IOException {
super.writeTo(out);
out.writeOptionalString(nodeId);
}

@Override
public XContentBuilder toXContent(final XContentBuilder builder, final Params params) throws IOException {
if (nodeId != null) {
builder.field("node", nodeId);
}
return super.toXContent(builder, params);
}

@Override
public String toString() {
return Strings.toString(this);
}

static Failure readFailure(final StreamInput in) throws IOException {
final Failure failure = new Failure();
failure.readFrom(in);
return failure;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;

import java.util.Collections;

/**
* Close index action
*/
Expand Down Expand Up @@ -109,7 +111,7 @@ protected void masterOperation(final Task task,
final ActionListener<CloseIndexResponse> listener) throws Exception {
final Index[] concreteIndices = indexNameExpressionResolver.concreteIndices(state, request);
if (concreteIndices == null || concreteIndices.length == 0) {
listener.onResponse(new CloseIndexResponse(true, false));
listener.onResponse(new CloseIndexResponse(true, false, Collections.emptyList()));
return;
}

Expand Down
Loading