Add stream serialization to ReloadAnalyzersResponse (#44420)

This change adds Writeable support to ReloadAnalyzersResponse that
is required when using the transport client in 7.x.

Closes #44383
This commit is contained in:
Christoph Büscher 2019-07-18 11:11:26 +02:00
parent 1375cc93a8
commit eed2db8947
3 changed files with 122 additions and 3 deletions

View File

@ -9,6 +9,8 @@ import org.elasticsearch.action.support.DefaultShardOperationFailedException;
import org.elasticsearch.action.support.broadcast.BroadcastResponse;
import org.elasticsearch.common.ParseField;
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.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
@ -21,6 +23,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
@ -31,6 +34,7 @@ import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constru
public class ReloadAnalyzersResponse extends BroadcastResponse {
private final Map<String, ReloadDetails> reloadDetails;
private static final ParseField RELOAD_DETAILS_FIELD = new ParseField("reload_details");
private static final ParseField INDEX_FIELD = new ParseField("index");
private static final ParseField RELOADED_ANALYZERS_FIELD = new ParseField("reloaded_analyzers");
@ -38,8 +42,7 @@ public class ReloadAnalyzersResponse extends BroadcastResponse {
public ReloadAnalyzersResponse(StreamInput in) throws IOException {
super(in);
reloadDetails = null;
// TODO: this needs to deserialize reloadDetails, see https://github.com/elastic/elasticsearch/issues/44383
this.reloadDetails = in.readMap(StreamInput::readString, ReloadDetails::new);
}
public ReloadAnalyzersResponse(int totalShards, int successfulShards, int failedShards,
@ -100,7 +103,30 @@ public class ReloadAnalyzersResponse extends BroadcastResponse {
return PARSER.apply(parser, null);
}
public static class ReloadDetails {
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeMap(reloadDetails, StreamOutput::writeString, (stream, details) -> details.writeTo(stream));
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ReloadAnalyzersResponse that = (ReloadAnalyzersResponse) o;
return Objects.equals(reloadDetails, that.reloadDetails);
}
@Override
public int hashCode() {
return Objects.hash(reloadDetails);
}
public static class ReloadDetails implements Writeable {
private final String indexName;
private final Set<String> reloadedIndicesNodes;
@ -112,6 +138,19 @@ public class ReloadAnalyzersResponse extends BroadcastResponse {
this.reloadedAnalyzers = reloadedAnalyzers;
}
ReloadDetails(StreamInput in) throws IOException {
this.indexName = in.readString();
this.reloadedIndicesNodes = new HashSet<>(in.readList(StreamInput::readString));
this.reloadedAnalyzers = new HashSet<>(in.readList(StreamInput::readString));
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(indexName);
out.writeStringCollection(reloadedIndicesNodes);
out.writeStringCollection(reloadedAnalyzers);
}
public String getIndexName() {
return indexName;
}
@ -129,5 +168,24 @@ public class ReloadAnalyzersResponse extends BroadcastResponse {
this.reloadedAnalyzers.addAll(other.reloadedSearchAnalyzers);
this.reloadedIndicesNodes.add(other.nodeId);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ReloadDetails that = (ReloadDetails) o;
return Objects.equals(indexName, that.indexName)
&& Objects.equals(reloadedIndicesNodes, that.reloadedIndicesNodes)
&& Objects.equals(reloadedAnalyzers, that.reloadedAnalyzers);
}
@Override
public int hashCode() {
return Objects.hash(indexName, reloadedIndicesNodes, reloadedAnalyzers);
}
}
}

View File

@ -9,6 +9,7 @@ import org.elasticsearch.action.support.DefaultShardOperationFailedException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.test.AbstractBroadcastResponseTestCase;
import org.elasticsearch.test.VersionUtils;
import org.elasticsearch.xpack.core.action.ReloadAnalyzersResponse.ReloadDetails;
import java.io.IOException;
@ -53,4 +54,12 @@ public class ReloadAnalyzersResponseTests extends AbstractBroadcastResponseTestC
+ "}",
output);
}
public void testSerialization() throws IOException {
ReloadAnalyzersResponse response = createTestInstance();
ReloadAnalyzersResponse copy = copyWriteable(response, writableRegistry(), ReloadAnalyzersResponse::new,
VersionUtils.randomVersion(random()));
assertEquals(response.getReloadDetails(), copy.getReloadDetails());
}
}

View File

@ -0,0 +1,52 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.core.action;
import org.elasticsearch.common.io.stream.Writeable.Reader;
import org.elasticsearch.test.AbstractWireSerializingTestCase;
import org.elasticsearch.xpack.core.action.ReloadAnalyzersResponse.ReloadDetails;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
public class ReloadDetailsTests extends AbstractWireSerializingTestCase<ReloadDetails> {
@Override
protected ReloadDetails createTestInstance() {
return new ReloadDetails(randomAlphaOfLengthBetween(5, 10), Set.of(generateRandomStringArray(5, 5, false)),
Set.of(generateRandomStringArray(5, 5, false)));
}
@Override
protected Reader<ReloadDetails> instanceReader() {
return ReloadDetails::new;
}
@Override
protected ReloadDetails mutateInstance(ReloadDetails instance) throws IOException {
String indexName = instance.getIndexName();
Set<String> reloadedAnalyzers = new HashSet<>(instance.getReloadedAnalyzers());
Set<String> reloadedIndicesNodes = new HashSet<>(instance.getReloadedIndicesNodes());
int mutate = randomIntBetween(0, 2);
switch (mutate) {
case 0:
indexName = indexName + randomAlphaOfLength(2);
break;
case 1:
reloadedAnalyzers.add(randomAlphaOfLength(10));
break;
case 2:
reloadedIndicesNodes.add(randomAlphaOfLength(10));
break;
default:
throw new IllegalStateException("Requested to modify more than available parameters.");
}
return new ReloadDetails(indexName, reloadedIndicesNodes, reloadedAnalyzers);
}
}