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

Replace Streamable w/ Writeable in SingleShardRequest and subclasses #43222

Merged
merged 8 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -257,26 +257,30 @@ 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);
}

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() {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,15 +36,15 @@
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;
import java.util.Collections;

import static org.hamcrest.Matchers.equalTo;

public class PainlessExecuteRequestTests extends AbstractStreamableTestCase<PainlessExecuteAction.Request> {
public class PainlessExecuteRequestTests extends AbstractWireSerializingTestCase<PainlessExecuteAction.Request> {

// 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
Expand Down Expand Up @@ -89,8 +90,8 @@ protected PainlessExecuteAction.Request createTestInstance() {
}

@Override
protected PainlessExecuteAction.Request createBlankInstance() {
return new PainlessExecuteAction.Request();
protected Writeable.Reader<PainlessExecuteAction.Request> instanceReader() {
return PainlessExecuteAction.Request::new;
}

public void testValidate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ public static class Request extends SingleShardRequest<Request> {
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.
*
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,27 @@
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;

import java.io.IOException;

public class GetFieldMappingsIndexRequest extends SingleShardRequest<GetFieldMappingsIndexRequest> {

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) {
Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@

public class FieldCapabilitiesIndexRequest extends SingleShardRequest<FieldCapabilitiesIndexRequest> {

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);
Expand All @@ -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);
Expand Down
31 changes: 15 additions & 16 deletions server/src/main/java/org/elasticsearch/action/get/GetRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,26 @@ public class MultiGetShardRequest extends SingleShardRequest<MultiGetShardReques

private int shardId;
private String preference;
boolean realtime = true;
boolean refresh;
private boolean realtime;
private boolean refresh;

IntArrayList locations;
List<MultiGetRequest.Item> 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) {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ public abstract class SingleShardRequest<Request extends SingleShardRequest<Requ
public SingleShardRequest() {
}

public SingleShardRequest(StreamInput in) throws IOException {
super(in);
if (in.readBoolean()) {
internalShardId = new ShardId(in);
}
index = in.readOptionalString();
// no need to pass threading over the network, they are always false when coming throw a thread pool
}

protected SingleShardRequest(String index) {
this.index = index;
}
Expand Down Expand Up @@ -93,16 +102,6 @@ public IndicesOptions indicesOptions() {
return INDICES_OPTIONS;
}

@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
if (in.readBoolean()) {
internalShardId = new ShardId(in);
}
index = in.readOptionalString();
// no need to pass threading over the network, they are always false when coming throw a thread pool
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
Expand Down
Loading