From 99495aa171aa9deca3ad586172abf2fe87e08261 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Wed, 19 Jun 2019 12:06:20 +0200 Subject: [PATCH] Replace Streamable w/ Writeable in SingleShardRequest and subclasses (#43222) This commit replaces usages of Streamable with Writeable for the SingleShardRequest / TransportSingleShardAction classes and subclasses of these classes. Note that where possible response fields were made final and default constructors were removed. Relates to #34389 --- .../action/PainlessExecuteAction.java | 26 +++--- .../action/PainlessExecuteRequestTests.java | 9 +- .../admin/indices/analyze/AnalyzeAction.java | 27 +++--- .../get/GetFieldMappingsIndexRequest.java | 27 +++--- .../action/explain/ExplainRequest.java | 27 +++--- .../FieldCapabilitiesIndexRequest.java | 17 ++-- .../elasticsearch/action/get/GetRequest.java | 31 ++++--- .../action/get/MultiGetShardRequest.java | 35 ++++---- .../single/shard/SingleShardRequest.java | 19 ++-- .../shard/TransportSingleShardAction.java | 7 +- .../termvectors/MultiTermVectorsRequest.java | 2 +- .../MultiTermVectorsShardRequest.java | 25 +++--- .../termvectors/TermVectorsRequest.java | 90 +++++++++---------- .../index/seqno/RetentionLeaseActions.java | 44 ++++----- .../indices/analyze/AnalyzeRequestTests.java | 3 +- .../action/explain/ExplainRequestTests.java | 3 +- .../action/get/MultiGetShardRequestTests.java | 4 +- .../termvectors/TermVectorsUnitTests.java | 6 +- .../xpack/ccr/action/ShardChangesAction.java | 30 +++---- .../PutCcrRestoreSessionRequest.java | 16 ++-- .../ccr/action/ShardChangesRequestTests.java | 9 +- 21 files changed, 202 insertions(+), 255 deletions(-) diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessExecuteAction.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessExecuteAction.java index 7c8a368d38a58..32b61f9c38ba4 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessExecuteAction.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessExecuteAction.java @@ -257,9 +257,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } - private Script script; - private ScriptContext context = PainlessTestScript.CONTEXT; - private ContextSetup contextSetup; + private final Script script; + private final ScriptContext context; + private final ContextSetup contextSetup; static Request parse(XContentParser parser) throws IOException { return PARSER.parse(parser, null); @@ -267,16 +267,20 @@ static Request parse(XContentParser parser) throws IOException { Request(Script script, String scriptContextName, ContextSetup setup) { this.script = Objects.requireNonNull(script); - if (scriptContextName != null) { - this.context = fromScriptContextName(scriptContextName); - } + this.context = scriptContextName != null ? fromScriptContextName(scriptContextName) : PainlessTestScript.CONTEXT; if (setup != null) { this.contextSetup = setup; index(contextSetup.index); + } else { + contextSetup = null; } } - Request() { + Request(StreamInput in) throws IOException { + super(in); + script = new Script(in); + context = fromScriptContextName(in.readString()); + contextSetup = in.readOptionalWriteable(ContextSetup::new); } public Script getScript() { @@ -308,14 +312,6 @@ public ActionRequestValidationException validate() { return validationException; } - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - script = new Script(in); - context = fromScriptContextName(in.readString()); - contextSetup = in.readOptionalWriteable(ContextSetup::new); - } - @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/PainlessExecuteRequestTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/PainlessExecuteRequestTests.java index 016e8f4fe4f61..d0996c4c2591c 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/PainlessExecuteRequestTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/PainlessExecuteRequestTests.java @@ -21,6 +21,7 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; import org.elasticsearch.common.xcontent.NamedXContentRegistry; @@ -35,7 +36,7 @@ import org.elasticsearch.script.ScriptContext; import org.elasticsearch.script.ScriptType; import org.elasticsearch.search.SearchModule; -import org.elasticsearch.test.AbstractStreamableTestCase; +import org.elasticsearch.test.AbstractWireSerializingTestCase; import java.io.IOException; import java.io.UncheckedIOException; @@ -43,7 +44,7 @@ import static org.hamcrest.Matchers.equalTo; -public class PainlessExecuteRequestTests extends AbstractStreamableTestCase { +public class PainlessExecuteRequestTests extends AbstractWireSerializingTestCase { // Testing XContent serialization manually here, because the xContentType field in ContextSetup determines // how the request needs to parse and the xcontent serialization framework randomizes that. The XContentType @@ -89,8 +90,8 @@ protected PainlessExecuteAction.Request createTestInstance() { } @Override - protected PainlessExecuteAction.Request createBlankInstance() { - return new PainlessExecuteAction.Request(); + protected Writeable.Reader instanceReader() { + return PainlessExecuteAction.Request::new; } public void testValidate() { diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/analyze/AnalyzeAction.java b/server/src/main/java/org/elasticsearch/action/admin/indices/analyze/AnalyzeAction.java index 0c75be6da1bab..27b623776fa9c 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/analyze/AnalyzeAction.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/analyze/AnalyzeAction.java @@ -83,6 +83,19 @@ public static class Request extends SingleShardRequest { public Request() { } + Request(StreamInput in) throws IOException { + super(in); + text = in.readStringArray(); + analyzer = in.readOptionalString(); + tokenizer = in.readOptionalWriteable(NameOrDefinition::new); + tokenFilters.addAll(in.readList(NameOrDefinition::new)); + charFilters.addAll(in.readList(NameOrDefinition::new)); + field = in.readOptionalString(); + explain = in.readBoolean(); + attributes = in.readStringArray(); + normalizer = in.readOptionalString(); + } + /** * Constructs a new analyzer request for the provided index. * @@ -240,20 +253,6 @@ public ActionRequestValidationException validate() { return validationException; } - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - text = in.readStringArray(); - analyzer = in.readOptionalString(); - tokenizer = in.readOptionalWriteable(NameOrDefinition::new); - tokenFilters.addAll(in.readList(NameOrDefinition::new)); - charFilters.addAll(in.readList(NameOrDefinition::new)); - field = in.readOptionalString(); - explain = in.readBoolean(); - attributes = in.readStringArray(); - normalizer = in.readOptionalString(); - } - @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetFieldMappingsIndexRequest.java b/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetFieldMappingsIndexRequest.java index 149cba9a33e17..576d3812c0cf4 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetFieldMappingsIndexRequest.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetFieldMappingsIndexRequest.java @@ -23,7 +23,6 @@ import org.elasticsearch.action.OriginalIndices; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.single.shard.SingleShardRequest; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -31,14 +30,20 @@ public class GetFieldMappingsIndexRequest extends SingleShardRequest { - private boolean probablySingleFieldRequest; - private boolean includeDefaults; - private String[] fields = Strings.EMPTY_ARRAY; - private String[] types = Strings.EMPTY_ARRAY; + private final boolean probablySingleFieldRequest; + private final boolean includeDefaults; + private final String[] fields; + private final String[] types; private OriginalIndices originalIndices; - public GetFieldMappingsIndexRequest() { + GetFieldMappingsIndexRequest(StreamInput in) throws IOException { + super(in); + types = in.readStringArray(); + fields = in.readStringArray(); + includeDefaults = in.readBoolean(); + probablySingleFieldRequest = in.readBoolean(); + originalIndices = OriginalIndices.readOriginalIndices(in); } GetFieldMappingsIndexRequest(GetFieldMappingsRequest other, String index, boolean probablySingleFieldRequest) { @@ -92,14 +97,4 @@ public void writeTo(StreamOutput out) throws IOException { OriginalIndices.writeOriginalIndices(originalIndices, out); } - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - types = in.readStringArray(); - fields = in.readStringArray(); - includeDefaults = in.readBoolean(); - probablySingleFieldRequest = in.readBoolean(); - originalIndices = OriginalIndices.readOriginalIndices(in); - } - } diff --git a/server/src/main/java/org/elasticsearch/action/explain/ExplainRequest.java b/server/src/main/java/org/elasticsearch/action/explain/ExplainRequest.java index 260c63d340a6f..bbb9d24469c9e 100644 --- a/server/src/main/java/org/elasticsearch/action/explain/ExplainRequest.java +++ b/server/src/main/java/org/elasticsearch/action/explain/ExplainRequest.java @@ -74,6 +74,19 @@ public ExplainRequest(String index, String id) { this.id = id; } + ExplainRequest(StreamInput in) throws IOException { + super(in); + type = in.readString(); + id = in.readString(); + routing = in.readOptionalString(); + preference = in.readOptionalString(); + query = in.readNamedWriteable(QueryBuilder.class); + filteringAlias = new AliasFilter(in); + storedFields = in.readOptionalStringArray(); + fetchSourceContext = in.readOptionalWriteable(FetchSourceContext::new); + nowInMillis = in.readVLong(); + } + /** * @deprecated Types are in the process of being removed. */ @@ -184,20 +197,6 @@ public ActionRequestValidationException validate() { return validationException; } - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - type = in.readString(); - id = in.readString(); - routing = in.readOptionalString(); - preference = in.readOptionalString(); - query = in.readNamedWriteable(QueryBuilder.class); - filteringAlias = new AliasFilter(in); - storedFields = in.readOptionalStringArray(); - fetchSourceContext = in.readOptionalWriteable(FetchSourceContext::new); - nowInMillis = in.readVLong(); - } - @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); diff --git a/server/src/main/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesIndexRequest.java b/server/src/main/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesIndexRequest.java index 81266f12e37b3..6be8daacd51e8 100644 --- a/server/src/main/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesIndexRequest.java +++ b/server/src/main/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesIndexRequest.java @@ -30,11 +30,15 @@ public class FieldCapabilitiesIndexRequest extends SingleShardRequest { - private String[] fields; - private OriginalIndices originalIndices; + private final String[] fields; + private final OriginalIndices originalIndices; // For serialization - FieldCapabilitiesIndexRequest() {} + FieldCapabilitiesIndexRequest(StreamInput in) throws IOException { + super(in); + fields = in.readStringArray(); + originalIndices = OriginalIndices.readOriginalIndices(in); + } FieldCapabilitiesIndexRequest(String[] fields, String index, OriginalIndices originalIndices) { super(index); @@ -61,13 +65,6 @@ public IndicesOptions indicesOptions() { return originalIndices.indicesOptions(); } - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - fields = in.readStringArray(); - originalIndices = OriginalIndices.readOriginalIndices(in); - } - @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); diff --git a/server/src/main/java/org/elasticsearch/action/get/GetRequest.java b/server/src/main/java/org/elasticsearch/action/get/GetRequest.java index 390553fd8b55b..932602b3a2d22 100644 --- a/server/src/main/java/org/elasticsearch/action/get/GetRequest.java +++ b/server/src/main/java/org/elasticsearch/action/get/GetRequest.java @@ -69,6 +69,21 @@ public GetRequest() { type = MapperService.SINGLE_MAPPING_NAME; } + GetRequest(StreamInput in) throws IOException { + super(in); + type = in.readString(); + id = in.readString(); + routing = in.readOptionalString(); + preference = in.readOptionalString(); + refresh = in.readBoolean(); + storedFields = in.readOptionalStringArray(); + realtime = in.readBoolean(); + + this.versionType = VersionType.fromValue(in.readByte()); + this.version = in.readLong(); + fetchSourceContext = in.readOptionalWriteable(FetchSourceContext::new); + } + /** * Constructs a new get request against the specified index. The {@link #id(String)} must also be set. */ @@ -261,22 +276,6 @@ public VersionType versionType() { return this.versionType; } - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - type = in.readString(); - id = in.readString(); - routing = in.readOptionalString(); - preference = in.readOptionalString(); - refresh = in.readBoolean(); - storedFields = in.readOptionalStringArray(); - realtime = in.readBoolean(); - - this.versionType = VersionType.fromValue(in.readByte()); - this.version = in.readLong(); - fetchSourceContext = in.readOptionalWriteable(FetchSourceContext::new); - } - @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); diff --git a/server/src/main/java/org/elasticsearch/action/get/MultiGetShardRequest.java b/server/src/main/java/org/elasticsearch/action/get/MultiGetShardRequest.java index 55f1fc6f5b9dd..1e66766aaabfa 100644 --- a/server/src/main/java/org/elasticsearch/action/get/MultiGetShardRequest.java +++ b/server/src/main/java/org/elasticsearch/action/get/MultiGetShardRequest.java @@ -33,14 +33,26 @@ public class MultiGetShardRequest extends SingleShardRequest items; - public MultiGetShardRequest() { + MultiGetShardRequest(StreamInput in) throws IOException { + super(in); + int size = in.readVInt(); + locations = new IntArrayList(size); + items = new ArrayList<>(size); + for (int i = 0; i < size; i++) { + locations.add(in.readVInt()); + items.add(MultiGetRequest.Item.readItem(in)); + } + + preference = in.readOptionalString(); + refresh = in.readBoolean(); + realtime = in.readBoolean(); } MultiGetShardRequest(MultiGetRequest multiGetRequest, String index, int shardId) { @@ -108,23 +120,6 @@ public String[] indices() { return indices; } - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - int size = in.readVInt(); - locations = new IntArrayList(size); - items = new ArrayList<>(size); - - for (int i = 0; i < size; i++) { - locations.add(in.readVInt()); - items.add(MultiGetRequest.Item.readItem(in)); - } - - preference = in.readOptionalString(); - refresh = in.readBoolean(); - realtime = in.readBoolean(); - } - @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); diff --git a/server/src/main/java/org/elasticsearch/action/support/single/shard/SingleShardRequest.java b/server/src/main/java/org/elasticsearch/action/support/single/shard/SingleShardRequest.java index 15b10832b4eff..747b6c54ee8a2 100644 --- a/server/src/main/java/org/elasticsearch/action/support/single/shard/SingleShardRequest.java +++ b/server/src/main/java/org/elasticsearch/action/support/single/shard/SingleShardRequest.java @@ -48,6 +48,15 @@ public abstract class SingleShardRequest request, + IndexNameExpressionResolver indexNameExpressionResolver, Writeable.Reader request, String executor) { super(actionName, actionFilters, transportService.getTaskManager()); this.threadPool = threadPool; @@ -85,9 +84,9 @@ protected TransportSingleShardAction(String actionName, ThreadPool threadPool, C this.executor = executor; if (!isSubAction()) { - transportService.registerRequestHandler(actionName, request, ThreadPool.Names.SAME, new TransportHandler()); + transportService.registerRequestHandler(actionName, ThreadPool.Names.SAME, request, new TransportHandler()); } - transportService.registerRequestHandler(transportShardAction, request, ThreadPool.Names.SAME, new ShardTransportHandler()); + transportService.registerRequestHandler(transportShardAction, ThreadPool.Names.SAME, request, new ShardTransportHandler()); } /** diff --git a/server/src/main/java/org/elasticsearch/action/termvectors/MultiTermVectorsRequest.java b/server/src/main/java/org/elasticsearch/action/termvectors/MultiTermVectorsRequest.java index 4b2a3bd2fe6a5..96a207ca1a595 100644 --- a/server/src/main/java/org/elasticsearch/action/termvectors/MultiTermVectorsRequest.java +++ b/server/src/main/java/org/elasticsearch/action/termvectors/MultiTermVectorsRequest.java @@ -139,7 +139,7 @@ public void readFrom(StreamInput in) throws IOException { int size = in.readVInt(); requests = new ArrayList<>(size); for (int i = 0; i < size; i++) { - requests.add(TermVectorsRequest.readTermVectorsRequest(in)); + requests.add(new TermVectorsRequest(in)); } } diff --git a/server/src/main/java/org/elasticsearch/action/termvectors/MultiTermVectorsShardRequest.java b/server/src/main/java/org/elasticsearch/action/termvectors/MultiTermVectorsShardRequest.java index 81f1b023578a7..2255b3243f123 100644 --- a/server/src/main/java/org/elasticsearch/action/termvectors/MultiTermVectorsShardRequest.java +++ b/server/src/main/java/org/elasticsearch/action/termvectors/MultiTermVectorsShardRequest.java @@ -37,8 +37,17 @@ public class MultiTermVectorsShardRequest extends SingleShardRequest requests; - public MultiTermVectorsShardRequest() { + MultiTermVectorsShardRequest(StreamInput in) throws IOException { + super(in); + int size = in.readVInt(); + locations = new IntArrayList(size); + requests = new ArrayList<>(size); + for (int i = 0; i < size; i++) { + locations.add(in.readVInt()); + requests.add(new TermVectorsRequest(in)); + } + preference = in.readOptionalString(); } MultiTermVectorsShardRequest(String index, int shardId) { @@ -86,20 +95,6 @@ public String[] indices() { return indices; } - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - int size = in.readVInt(); - locations = new IntArrayList(size); - requests = new ArrayList<>(size); - for (int i = 0; i < size; i++) { - locations.add(in.readVInt()); - requests.add(TermVectorsRequest.readTermVectorsRequest(in)); - } - - preference = in.readOptionalString(); - } - @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); diff --git a/server/src/main/java/org/elasticsearch/action/termvectors/TermVectorsRequest.java b/server/src/main/java/org/elasticsearch/action/termvectors/TermVectorsRequest.java index 0cf5f78f17a5e..09f7a15dff795 100644 --- a/server/src/main/java/org/elasticsearch/action/termvectors/TermVectorsRequest.java +++ b/server/src/main/java/org/elasticsearch/action/termvectors/TermVectorsRequest.java @@ -133,6 +133,47 @@ public void writeTo(StreamOutput out) throws IOException { public TermVectorsRequest() { } + TermVectorsRequest(StreamInput in) throws IOException { + super(in); + if (in.getVersion().before(Version.V_8_0_0)) { + // types no longer relevant so ignore + in.readString(); + } + id = in.readString(); + + if (in.readBoolean()) { + doc = in.readBytesReference(); + xContentType = in.readEnum(XContentType.class); + } + routing = in.readOptionalString(); + preference = in.readOptionalString(); + long flags = in.readVLong(); + + flagsEnum.clear(); + for (Flag flag : Flag.values()) { + if ((flags & (1 << flag.ordinal())) != 0) { + flagsEnum.add(flag); + } + } + int numSelectedFields = in.readVInt(); + if (numSelectedFields > 0) { + selectedFields = new HashSet<>(); + for (int i = 0; i < numSelectedFields; i++) { + selectedFields.add(in.readString()); + } + } + if (in.readBoolean()) { + perFieldAnalyzer = readPerFieldAnalyzer(in.readMap()); + } + if (in.readBoolean()) { + filterSettings = new FilterSettings(); + filterSettings.readFrom(in); + } + realtime = in.readBoolean(); + versionType = VersionType.fromValue(in.readByte()); + version = in.readLong(); + } + /** * Constructs a new term vector request for a document that will be fetch * from the provided index. Use and {@link #id(String)} to specify the @@ -440,55 +481,6 @@ public ActionRequestValidationException validate() { return validationException; } - public static TermVectorsRequest readTermVectorsRequest(StreamInput in) throws IOException { - TermVectorsRequest termVectorsRequest = new TermVectorsRequest(); - termVectorsRequest.readFrom(in); - return termVectorsRequest; - } - - - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - if (in.getVersion().before(Version.V_8_0_0)) { - // types no longer relevant so ignore - in.readString(); - } - id = in.readString(); - - if (in.readBoolean()) { - doc = in.readBytesReference(); - xContentType = in.readEnum(XContentType.class); - } - routing = in.readOptionalString(); - preference = in.readOptionalString(); - long flags = in.readVLong(); - - flagsEnum.clear(); - for (Flag flag : Flag.values()) { - if ((flags & (1 << flag.ordinal())) != 0) { - flagsEnum.add(flag); - } - } - int numSelectedFields = in.readVInt(); - if (numSelectedFields > 0) { - selectedFields = new HashSet<>(); - for (int i = 0; i < numSelectedFields; i++) { - selectedFields.add(in.readString()); - } - } - if (in.readBoolean()) { - perFieldAnalyzer = readPerFieldAnalyzer(in.readMap()); - } - if (in.readBoolean()) { - filterSettings = new FilterSettings(); - filterSettings.readFrom(in); - } - realtime = in.readBoolean(); - versionType = VersionType.fromValue(in.readByte()); - version = in.readLong(); - } - @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); diff --git a/server/src/main/java/org/elasticsearch/index/seqno/RetentionLeaseActions.java b/server/src/main/java/org/elasticsearch/index/seqno/RetentionLeaseActions.java index 74c98bf3dca19..3f292a7c8d156 100644 --- a/server/src/main/java/org/elasticsearch/index/seqno/RetentionLeaseActions.java +++ b/server/src/main/java/org/elasticsearch/index/seqno/RetentionLeaseActions.java @@ -44,7 +44,6 @@ import java.io.IOException; import java.util.Objects; -import java.util.function.Supplier; /** * This class holds all actions related to retention leases. Note carefully that these actions are executed under a primary permit. Care is @@ -70,7 +69,7 @@ abstract static class TransportRetentionLeaseAction> extend final ActionFilters actionFilters, final IndexNameExpressionResolver indexNameExpressionResolver, final IndicesService indicesService, - final Supplier requestSupplier) { + final Writeable.Reader requestSupplier) { super( name, threadPool, @@ -272,19 +271,22 @@ public Response newResponse() { private abstract static class Request> extends SingleShardRequest { - private ShardId shardId; + private final ShardId shardId; public ShardId getShardId() { return shardId; } - private String id; + private final String id; public String getId() { return id; } - Request() { + Request(StreamInput in) throws IOException { + super(in); + shardId = new ShardId(in); + id = in.readString(); } Request(final ShardId shardId, final String id) { @@ -298,13 +300,6 @@ public ActionRequestValidationException validate() { return null; } - @Override - public void readFrom(final StreamInput in) throws IOException { - super.readFrom(in); - shardId = new ShardId(in); - id = in.readString(); - } - @Override public void writeTo(final StreamOutput out) throws IOException { super.writeTo(out); @@ -316,19 +311,22 @@ public void writeTo(final StreamOutput out) throws IOException { private abstract static class AddOrRenewRequest> extends Request { - private long retainingSequenceNumber; + private final long retainingSequenceNumber; public long getRetainingSequenceNumber() { return retainingSequenceNumber; } - private String source; + private final String source; public String getSource() { return source; } - AddOrRenewRequest() { + AddOrRenewRequest(StreamInput in) throws IOException { + super(in); + retainingSequenceNumber = in.readZLong(); + source = in.readString(); } AddOrRenewRequest(final ShardId shardId, final String id, final long retainingSequenceNumber, final String source) { @@ -340,13 +338,6 @@ public String getSource() { this.source = Objects.requireNonNull(source); } - @Override - public void readFrom(final StreamInput in) throws IOException { - super.readFrom(in); - retainingSequenceNumber = in.readZLong(); - source = in.readString(); - } - @Override public void writeTo(final StreamOutput out) throws IOException { super.writeTo(out); @@ -358,7 +349,8 @@ public void writeTo(final StreamOutput out) throws IOException { public static class AddRequest extends AddOrRenewRequest { - public AddRequest() { + AddRequest(StreamInput in) throws IOException { + super(in); } public AddRequest(final ShardId shardId, final String id, final long retainingSequenceNumber, final String source) { @@ -369,7 +361,8 @@ public AddRequest(final ShardId shardId, final String id, final long retainingSe public static class RenewRequest extends AddOrRenewRequest { - public RenewRequest() { + RenewRequest(StreamInput in) throws IOException { + super(in); } public RenewRequest(final ShardId shardId, final String id, final long retainingSequenceNumber, final String source) { @@ -380,7 +373,8 @@ public RenewRequest(final ShardId shardId, final String id, final long retaining public static class RemoveRequest extends Request { - public RemoveRequest() { + RemoveRequest(StreamInput in) throws IOException { + super(in); } public RemoveRequest(final ShardId shardId, final String id) { diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/analyze/AnalyzeRequestTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/analyze/AnalyzeRequestTests.java index 2c4c9212fb53a..6d6eef7a99882 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/analyze/AnalyzeRequestTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/analyze/AnalyzeRequestTests.java @@ -109,8 +109,7 @@ public void testSerialization() throws IOException { try (BytesStreamOutput output = new BytesStreamOutput()) { request.writeTo(output); try (StreamInput in = output.bytes().streamInput()) { - AnalyzeAction.Request serialized = new AnalyzeAction.Request(); - serialized.readFrom(in); + AnalyzeAction.Request serialized = new AnalyzeAction.Request(in); assertArrayEquals(request.text(), serialized.text()); assertEquals(request.tokenizer().name, serialized.tokenizer().name); assertEquals(request.tokenFilters().get(0).name, serialized.tokenFilters().get(0).name); diff --git a/server/src/test/java/org/elasticsearch/action/explain/ExplainRequestTests.java b/server/src/test/java/org/elasticsearch/action/explain/ExplainRequestTests.java index ae9d1cb0a39be..390d06a5058a1 100644 --- a/server/src/test/java/org/elasticsearch/action/explain/ExplainRequestTests.java +++ b/server/src/test/java/org/elasticsearch/action/explain/ExplainRequestTests.java @@ -64,8 +64,7 @@ public void testSerialize() throws IOException { request.routing("some_routing"); request.writeTo(output); try (StreamInput in = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), namedWriteableRegistry)) { - ExplainRequest readRequest = new ExplainRequest(); - readRequest.readFrom(in); + ExplainRequest readRequest = new ExplainRequest(in); assertEquals(request.filteringAlias(), readRequest.filteringAlias()); assertArrayEquals(request.storedFields(), readRequest.storedFields()); assertEquals(request.preference(), readRequest.preference()); diff --git a/server/src/test/java/org/elasticsearch/action/get/MultiGetShardRequestTests.java b/server/src/test/java/org/elasticsearch/action/get/MultiGetShardRequestTests.java index 782edb517cb13..4d43454e8dd25 100644 --- a/server/src/test/java/org/elasticsearch/action/get/MultiGetShardRequestTests.java +++ b/server/src/test/java/org/elasticsearch/action/get/MultiGetShardRequestTests.java @@ -71,9 +71,7 @@ public void testSerialization() throws IOException { StreamInput in = out.bytes().streamInput(); in.setVersion(out.getVersion()); - MultiGetShardRequest multiGetShardRequest2 = new MultiGetShardRequest(); - multiGetShardRequest2.readFrom(in); - + MultiGetShardRequest multiGetShardRequest2 = new MultiGetShardRequest(in); assertThat(multiGetShardRequest2.index(), equalTo(multiGetShardRequest.index())); assertThat(multiGetShardRequest2.preference(), equalTo(multiGetShardRequest.preference())); assertThat(multiGetShardRequest2.realtime(), equalTo(multiGetShardRequest.realtime())); diff --git a/server/src/test/java/org/elasticsearch/action/termvectors/TermVectorsUnitTests.java b/server/src/test/java/org/elasticsearch/action/termvectors/TermVectorsUnitTests.java index 06d3fbf67f87d..b14ccb82738ed 100644 --- a/server/src/test/java/org/elasticsearch/action/termvectors/TermVectorsUnitTests.java +++ b/server/src/test/java/org/elasticsearch/action/termvectors/TermVectorsUnitTests.java @@ -246,8 +246,7 @@ public void testStreamRequest() throws IOException { // read ByteArrayInputStream esInBuffer = new ByteArrayInputStream(outBuffer.toByteArray()); InputStreamStreamInput esBuffer = new InputStreamStreamInput(esInBuffer); - TermVectorsRequest req2 = new TermVectorsRequest(null, null); - req2.readFrom(esBuffer); + TermVectorsRequest req2 = new TermVectorsRequest(esBuffer); assertThat(request.offsets(), equalTo(req2.offsets())); assertThat(request.fieldStatistics(), equalTo(req2.fieldStatistics())); @@ -293,8 +292,7 @@ public void testStreamRequestLegacyVersion() throws IOException { esInBuffer = new ByteArrayInputStream(outBuffer.toByteArray()); esBuffer = new InputStreamStreamInput(esInBuffer); esBuffer.setVersion(Version.V_7_2_0); - TermVectorsRequest req2 = new TermVectorsRequest(null, null); - req2.readFrom(esBuffer); + TermVectorsRequest req2 = new TermVectorsRequest(esBuffer); assertThat(request.offsets(), equalTo(req2.offsets())); assertThat(request.fieldStatistics(), equalTo(req2.fieldStatistics())); diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardChangesAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardChangesAction.java index 521a1eaae829d..03495d34f2914 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardChangesAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardChangesAction.java @@ -75,8 +75,8 @@ public static class Request extends SingleShardRequest { private long fromSeqNo; private int maxOperationCount; - private ShardId shardId; - private String expectedHistoryUUID; + private final ShardId shardId; + private final String expectedHistoryUUID; private TimeValue pollTimeout = TransportResumeFollowAction.DEFAULT_READ_POLL_TIMEOUT; private ByteSizeValue maxBatchSize = TransportResumeFollowAction.DEFAULT_MAX_READ_REQUEST_SIZE; @@ -88,7 +88,17 @@ public Request(ShardId shardId, String expectedHistoryUUID) { this.expectedHistoryUUID = expectedHistoryUUID; } - Request() { + Request(StreamInput in) throws IOException { + super(in); + fromSeqNo = in.readVLong(); + maxOperationCount = in.readVInt(); + shardId = new ShardId(in); + expectedHistoryUUID = in.readString(); + pollTimeout = in.readTimeValue(); + maxBatchSize = new ByteSizeValue(in); + + // Starting the clock in order to know how much time is spent on fetching operations: + relativeStartNanos = System.nanoTime(); } public ShardId getShard() { @@ -148,20 +158,6 @@ public ActionRequestValidationException validate() { return validationException; } - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - fromSeqNo = in.readVLong(); - maxOperationCount = in.readVInt(); - shardId = new ShardId(in); - expectedHistoryUUID = in.readString(); - pollTimeout = in.readTimeValue(); - maxBatchSize = new ByteSizeValue(in); - - // Starting the clock in order to know how much time is spent on fetching operations: - relativeStartNanos = System.nanoTime(); - } - @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/PutCcrRestoreSessionRequest.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/PutCcrRestoreSessionRequest.java index e348df79223a7..15db362c43c24 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/PutCcrRestoreSessionRequest.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/PutCcrRestoreSessionRequest.java @@ -16,10 +16,13 @@ public class PutCcrRestoreSessionRequest extends SingleShardRequest { - private String sessionUUID; - private ShardId shardId; + private final String sessionUUID; + private final ShardId shardId; - PutCcrRestoreSessionRequest() { + PutCcrRestoreSessionRequest(StreamInput in) throws IOException { + super(in); + sessionUUID = in.readString(); + shardId = new ShardId(in); } public PutCcrRestoreSessionRequest(String sessionUUID, ShardId shardId) { @@ -33,13 +36,6 @@ public ActionRequestValidationException validate() { return null; } - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - sessionUUID = in.readString(); - shardId = new ShardId(in); - } - @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardChangesRequestTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardChangesRequestTests.java index 2ea2086990b32..5b2f06652b007 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardChangesRequestTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardChangesRequestTests.java @@ -5,13 +5,14 @@ */ package org.elasticsearch.xpack.ccr.action; +import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.index.shard.ShardId; -import org.elasticsearch.test.AbstractStreamableTestCase; +import org.elasticsearch.test.AbstractWireSerializingTestCase; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.nullValue; -public class ShardChangesRequestTests extends AbstractStreamableTestCase { +public class ShardChangesRequestTests extends AbstractWireSerializingTestCase { @Override protected ShardChangesAction.Request createTestInstance() { @@ -23,8 +24,8 @@ protected ShardChangesAction.Request createTestInstance() { } @Override - protected ShardChangesAction.Request createBlankInstance() { - return new ShardChangesAction.Request(); + protected Writeable.Reader instanceReader() { + return ShardChangesAction.Request::new; } public void testValidate() {