Convert ShardOperationFailedException to Writeable (#44532) (#44580)

This commit converts subclasses of ShardOperationFailedException to
implement ctors with StreamInput instead of readFrom. It also simplifies
IndicesShardStoresResponse.Failure to serialize its shardId after the
super data.

relates #34389
This commit is contained in:
Ryan Ernst 2019-07-18 13:29:19 -07:00 committed by GitHub
parent 3477f5ae04
commit af093a4095
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 61 additions and 94 deletions

View File

@ -20,7 +20,7 @@
package org.elasticsearch.action; package org.elasticsearch.action;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.io.stream.Streamable; import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.RestStatus;
@ -30,7 +30,7 @@ import java.util.Objects;
* An exception indicating that a failure occurred performing an operation on the shard. * An exception indicating that a failure occurred performing an operation on the shard.
* *
*/ */
public abstract class ShardOperationFailedException implements Streamable, ToXContentObject { public abstract class ShardOperationFailedException implements Writeable, ToXContentObject {
protected String index; protected String index;
protected int shardId = -1; protected int shardId = -1;

View File

@ -238,7 +238,9 @@ public class CloseIndexResponse extends ShardsAcknowledgedResponse {
private @Nullable String nodeId; private @Nullable String nodeId;
private Failure() { private Failure(StreamInput in) throws IOException {
super(in);
nodeId = in.readOptionalString();
} }
public Failure(final String index, final int shardId, final Throwable reason) { public Failure(final String index, final int shardId, final Throwable reason) {
@ -254,12 +256,6 @@ public class CloseIndexResponse extends ShardsAcknowledgedResponse {
return nodeId; return nodeId;
} }
@Override
public void readFrom(final StreamInput in) throws IOException {
super.readFrom(in);
nodeId = in.readOptionalString();
}
@Override @Override
public void writeTo(final StreamOutput out) throws IOException { public void writeTo(final StreamOutput out) throws IOException {
super.writeTo(out); super.writeTo(out);
@ -280,9 +276,7 @@ public class CloseIndexResponse extends ShardsAcknowledgedResponse {
} }
static Failure readFailure(final StreamInput in) throws IOException { static Failure readFailure(final StreamInput in) throws IOException {
final Failure failure = new Failure(); return new Failure(in);
failure.readFrom(in);
return failure;
} }
} }
} }

View File

@ -241,7 +241,14 @@ public class IndicesShardStoresResponse extends ActionResponse implements ToXCon
this.nodeId = nodeId; this.nodeId = nodeId;
} }
private Failure() { private Failure(StreamInput in) throws IOException {
if (in.getVersion().before(Version.V_7_4_0)) {
nodeId = in.readString();
}
readFrom(in, this);
if (in.getVersion().onOrAfter(Version.V_7_4_0)) {
nodeId = in.readString();
}
} }
public String nodeId() { public String nodeId() {
@ -249,21 +256,18 @@ public class IndicesShardStoresResponse extends ActionResponse implements ToXCon
} }
static Failure readFailure(StreamInput in) throws IOException { static Failure readFailure(StreamInput in) throws IOException {
Failure failure = new Failure(); return new Failure(in);
failure.readFrom(in);
return failure;
}
@Override
public void readFrom(StreamInput in) throws IOException {
nodeId = in.readString();
super.readFrom(in);
} }
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
out.writeString(nodeId); if (out.getVersion().before(Version.V_7_4_0)) {
out.writeString(nodeId);
}
super.writeTo(out); super.writeTo(out);
if (out.getVersion().onOrAfter(Version.V_7_4_0)) {
out.writeString(nodeId);
}
} }
@Override @Override

View File

@ -54,7 +54,15 @@ public class ShardSearchFailure extends ShardOperationFailedException {
private SearchShardTarget shardTarget; private SearchShardTarget shardTarget;
ShardSearchFailure() { ShardSearchFailure(StreamInput in) throws IOException {
shardTarget = in.readOptionalWriteable(SearchShardTarget::new);
if (shardTarget != null) {
index = shardTarget.getFullyQualifiedIndexName();
shardId = shardTarget.getShardId().getId();
}
reason = in.readString();
status = RestStatus.readFrom(in);
cause = in.readException();
} }
public ShardSearchFailure(Exception e) { public ShardSearchFailure(Exception e) {
@ -91,21 +99,8 @@ public class ShardSearchFailure extends ShardOperationFailedException {
} }
public static ShardSearchFailure readShardSearchFailure(StreamInput in) throws IOException { public static ShardSearchFailure readShardSearchFailure(StreamInput in) throws IOException {
ShardSearchFailure shardSearchFailure = new ShardSearchFailure(); return new ShardSearchFailure(in);
shardSearchFailure.readFrom(in);
return shardSearchFailure;
}
@Override
public void readFrom(StreamInput in) throws IOException {
shardTarget = in.readOptionalWriteable(SearchShardTarget::new);
if (shardTarget != null) {
index = shardTarget.getFullyQualifiedIndexName();
shardId = shardTarget.getShardId().getId();
}
reason = in.readString();
status = RestStatus.readFrom(in);
cause = in.readException();
} }
@Override @Override

View File

@ -51,7 +51,10 @@ public class DefaultShardOperationFailedException extends ShardOperationFailedEx
PARSER.declareObject(constructorArg(), (p, c) -> ElasticsearchException.fromXContent(p), new ParseField(REASON)); PARSER.declareObject(constructorArg(), (p, c) -> ElasticsearchException.fromXContent(p), new ParseField(REASON));
} }
protected DefaultShardOperationFailedException() { protected DefaultShardOperationFailedException() {}
protected DefaultShardOperationFailedException(StreamInput in) throws IOException {
readFrom(in, this);
} }
public DefaultShardOperationFailedException(ElasticsearchException e) { public DefaultShardOperationFailedException(ElasticsearchException e) {
@ -64,17 +67,14 @@ public class DefaultShardOperationFailedException extends ShardOperationFailedEx
} }
public static DefaultShardOperationFailedException readShardOperationFailed(StreamInput in) throws IOException { public static DefaultShardOperationFailedException readShardOperationFailed(StreamInput in) throws IOException {
DefaultShardOperationFailedException exp = new DefaultShardOperationFailedException(); return new DefaultShardOperationFailedException(in);
exp.readFrom(in);
return exp;
} }
@Override public static void readFrom(StreamInput in, DefaultShardOperationFailedException f) throws IOException {
public void readFrom(StreamInput in) throws IOException { f.index = in.readOptionalString();
index = in.readOptionalString(); f.shardId = in.readVInt();
shardId = in.readVInt(); f.cause = in.readException();
cause = in.readException(); f.status = RestStatus.readFrom(in);
status = RestStatus.readFrom(in);
} }
@Override @Override

View File

@ -143,9 +143,7 @@ public class ReplicationResponse extends ActionResponse {
int size = in.readVInt(); int size = in.readVInt();
failures = new Failure[size]; failures = new Failure[size];
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
Failure failure = new Failure(); failures[i] = new Failure(in);
failure.readFrom(in);
failures[i] = failure;
} }
} }
@ -242,6 +240,16 @@ public class ReplicationResponse extends ActionResponse {
private String nodeId; private String nodeId;
private boolean primary; private boolean primary;
public Failure(StreamInput in) throws IOException {
shardId = new ShardId(in);
super.shardId = shardId.getId();
index = shardId.getIndexName();
nodeId = in.readOptionalString();
cause = in.readException();
status = RestStatus.readFrom(in);
primary = in.readBoolean();
}
public Failure(ShardId shardId, @Nullable String nodeId, Exception cause, RestStatus status, boolean primary) { public Failure(ShardId shardId, @Nullable String nodeId, Exception cause, RestStatus status, boolean primary) {
super(shardId.getIndexName(), shardId.getId(), ExceptionsHelper.detailedMessage(cause), status, cause); super(shardId.getIndexName(), shardId.getId(), ExceptionsHelper.detailedMessage(cause), status, cause);
this.shardId = shardId; this.shardId = shardId;
@ -272,17 +280,6 @@ public class ReplicationResponse extends ActionResponse {
return primary; return primary;
} }
@Override
public void readFrom(StreamInput in) throws IOException {
shardId = new ShardId(in);
super.shardId = shardId.getId();
index = shardId.getIndexName();
nodeId = in.readOptionalString();
cause = in.readException();
status = RestStatus.readFrom(in);
primary = in.readBoolean();
}
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
shardId.writeTo(out); shardId.writeTo(out);

View File

@ -303,7 +303,7 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
if (size > 0) { if (size > 0) {
List<SnapshotShardFailure> failureBuilder = new ArrayList<>(); List<SnapshotShardFailure> failureBuilder = new ArrayList<>();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
failureBuilder.add(SnapshotShardFailure.readSnapshotShardFailure(in)); failureBuilder.add(new SnapshotShardFailure(in));
} }
shardFailures = Collections.unmodifiableList(failureBuilder); shardFailures = Collections.unmodifiableList(failureBuilder);
} else { } else {

View File

@ -45,8 +45,13 @@ public class SnapshotShardFailure extends ShardOperationFailedException {
private String nodeId; private String nodeId;
private ShardId shardId; private ShardId shardId;
private SnapshotShardFailure() { SnapshotShardFailure(StreamInput in) throws IOException {
nodeId = in.readOptionalString();
shardId = new ShardId(in);
super.shardId = shardId.getId();
index = shardId.getIndexName();
reason = in.readString();
status = RestStatus.readFrom(in);
} }
/** /**
@ -84,28 +89,6 @@ public class SnapshotShardFailure extends ShardOperationFailedException {
return nodeId; return nodeId;
} }
/**
* Reads shard failure information from stream input
*
* @param in stream input
* @return shard failure information
*/
static SnapshotShardFailure readSnapshotShardFailure(StreamInput in) throws IOException {
SnapshotShardFailure exp = new SnapshotShardFailure();
exp.readFrom(in);
return exp;
}
@Override
public void readFrom(StreamInput in) throws IOException {
nodeId = in.readOptionalString();
shardId = new ShardId(in);
super.shardId = shardId.getId();
index = shardId.getIndexName();
reason = in.readString();
status = RestStatus.readFrom(in);
}
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalString(nodeId); out.writeOptionalString(nodeId);

View File

@ -20,7 +20,6 @@
package org.elasticsearch.action; package org.elasticsearch.action;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.RestStatus;
@ -58,11 +57,6 @@ public class ShardOperationFailedExceptionTests extends ESTestCase {
super(index, shardId, reason, status, cause); super(index, shardId, reason, status, cause);
} }
@Override
public void readFrom(StreamInput in) throws IOException {
}
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {

View File

@ -138,7 +138,7 @@ public class ShardSearchFailureTests extends ESTestCase {
public void testSerialization() throws IOException { public void testSerialization() throws IOException {
ShardSearchFailure testItem = createTestItem(randomAlphaOfLength(12)); ShardSearchFailure testItem = createTestItem(randomAlphaOfLength(12));
ShardSearchFailure deserializedInstance = copyStreamable(testItem, writableRegistry(), ShardSearchFailure deserializedInstance = copyWriteable(testItem, writableRegistry(),
ShardSearchFailure::new, VersionUtils.randomVersion(random())); ShardSearchFailure::new, VersionUtils.randomVersion(random()));
assertEquals(testItem.index(), deserializedInstance.index()); assertEquals(testItem.index(), deserializedInstance.index());
assertEquals(testItem.shard(), deserializedInstance.shard()); assertEquals(testItem.shard(), deserializedInstance.shard());