Skip to content

Commit eed2db8

Browse files
author
Christoph Büscher
committed
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
1 parent 1375cc9 commit eed2db8

File tree

3 files changed

+122
-3
lines changed

3 files changed

+122
-3
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/ReloadAnalyzersResponse.java

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.elasticsearch.action.support.broadcast.BroadcastResponse;
1010
import org.elasticsearch.common.ParseField;
1111
import org.elasticsearch.common.io.stream.StreamInput;
12+
import org.elasticsearch.common.io.stream.StreamOutput;
13+
import org.elasticsearch.common.io.stream.Writeable;
1214
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
1315
import org.elasticsearch.common.xcontent.XContentBuilder;
1416
import org.elasticsearch.common.xcontent.XContentParser;
@@ -21,6 +23,7 @@
2123
import java.util.List;
2224
import java.util.Map;
2325
import java.util.Map.Entry;
26+
import java.util.Objects;
2427
import java.util.Set;
2528

2629
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
@@ -31,15 +34,15 @@
3134
public class ReloadAnalyzersResponse extends BroadcastResponse {
3235

3336
private final Map<String, ReloadDetails> reloadDetails;
37+
3438
private static final ParseField RELOAD_DETAILS_FIELD = new ParseField("reload_details");
3539
private static final ParseField INDEX_FIELD = new ParseField("index");
3640
private static final ParseField RELOADED_ANALYZERS_FIELD = new ParseField("reloaded_analyzers");
3741
private static final ParseField RELOADED_NODE_IDS_FIELD = new ParseField("reloaded_node_ids");
3842

3943
public ReloadAnalyzersResponse(StreamInput in) throws IOException {
4044
super(in);
41-
reloadDetails = null;
42-
// TODO: this needs to deserialize reloadDetails, see https://github.com/elastic/elasticsearch/issues/44383
45+
this.reloadDetails = in.readMap(StreamInput::readString, ReloadDetails::new);
4346
}
4447

4548
public ReloadAnalyzersResponse(int totalShards, int successfulShards, int failedShards,
@@ -100,7 +103,30 @@ public static ReloadAnalyzersResponse fromXContent(XContentParser parser) {
100103
return PARSER.apply(parser, null);
101104
}
102105

103-
public static class ReloadDetails {
106+
@Override
107+
public void writeTo(StreamOutput out) throws IOException {
108+
super.writeTo(out);
109+
out.writeMap(reloadDetails, StreamOutput::writeString, (stream, details) -> details.writeTo(stream));
110+
}
111+
112+
@Override
113+
public boolean equals(Object o) {
114+
if (this == o) {
115+
return true;
116+
}
117+
if (o == null || getClass() != o.getClass()) {
118+
return false;
119+
}
120+
ReloadAnalyzersResponse that = (ReloadAnalyzersResponse) o;
121+
return Objects.equals(reloadDetails, that.reloadDetails);
122+
}
123+
124+
@Override
125+
public int hashCode() {
126+
return Objects.hash(reloadDetails);
127+
}
128+
129+
public static class ReloadDetails implements Writeable {
104130

105131
private final String indexName;
106132
private final Set<String> reloadedIndicesNodes;
@@ -112,6 +138,19 @@ public ReloadDetails(String name, Set<String> reloadedIndicesNodes, Set<String>
112138
this.reloadedAnalyzers = reloadedAnalyzers;
113139
}
114140

141+
ReloadDetails(StreamInput in) throws IOException {
142+
this.indexName = in.readString();
143+
this.reloadedIndicesNodes = new HashSet<>(in.readList(StreamInput::readString));
144+
this.reloadedAnalyzers = new HashSet<>(in.readList(StreamInput::readString));
145+
}
146+
147+
@Override
148+
public void writeTo(StreamOutput out) throws IOException {
149+
out.writeString(indexName);
150+
out.writeStringCollection(reloadedIndicesNodes);
151+
out.writeStringCollection(reloadedAnalyzers);
152+
}
153+
115154
public String getIndexName() {
116155
return indexName;
117156
}
@@ -129,5 +168,24 @@ void merge(ReloadResult other) {
129168
this.reloadedAnalyzers.addAll(other.reloadedSearchAnalyzers);
130169
this.reloadedIndicesNodes.add(other.nodeId);
131170
}
171+
172+
@Override
173+
public boolean equals(Object o) {
174+
if (this == o) {
175+
return true;
176+
}
177+
if (o == null || getClass() != o.getClass()) {
178+
return false;
179+
}
180+
ReloadDetails that = (ReloadDetails) o;
181+
return Objects.equals(indexName, that.indexName)
182+
&& Objects.equals(reloadedIndicesNodes, that.reloadedIndicesNodes)
183+
&& Objects.equals(reloadedAnalyzers, that.reloadedAnalyzers);
184+
}
185+
186+
@Override
187+
public int hashCode() {
188+
return Objects.hash(indexName, reloadedIndicesNodes, reloadedAnalyzers);
189+
}
132190
}
133191
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/ReloadAnalyzersResponseTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.elasticsearch.common.Strings;
1010
import org.elasticsearch.common.xcontent.XContentParser;
1111
import org.elasticsearch.test.AbstractBroadcastResponseTestCase;
12+
import org.elasticsearch.test.VersionUtils;
1213
import org.elasticsearch.xpack.core.action.ReloadAnalyzersResponse.ReloadDetails;
1314

1415
import java.io.IOException;
@@ -53,4 +54,12 @@ public void testToXContent() {
5354
+ "}",
5455
output);
5556
}
57+
58+
public void testSerialization() throws IOException {
59+
ReloadAnalyzersResponse response = createTestInstance();
60+
ReloadAnalyzersResponse copy = copyWriteable(response, writableRegistry(), ReloadAnalyzersResponse::new,
61+
VersionUtils.randomVersion(random()));
62+
assertEquals(response.getReloadDetails(), copy.getReloadDetails());
63+
}
64+
5665
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
package org.elasticsearch.xpack.core.action;
8+
9+
import org.elasticsearch.common.io.stream.Writeable.Reader;
10+
import org.elasticsearch.test.AbstractWireSerializingTestCase;
11+
import org.elasticsearch.xpack.core.action.ReloadAnalyzersResponse.ReloadDetails;
12+
13+
import java.io.IOException;
14+
import java.util.HashSet;
15+
import java.util.Set;
16+
17+
public class ReloadDetailsTests extends AbstractWireSerializingTestCase<ReloadDetails> {
18+
19+
@Override
20+
protected ReloadDetails createTestInstance() {
21+
return new ReloadDetails(randomAlphaOfLengthBetween(5, 10), Set.of(generateRandomStringArray(5, 5, false)),
22+
Set.of(generateRandomStringArray(5, 5, false)));
23+
}
24+
25+
@Override
26+
protected Reader<ReloadDetails> instanceReader() {
27+
return ReloadDetails::new;
28+
}
29+
30+
@Override
31+
protected ReloadDetails mutateInstance(ReloadDetails instance) throws IOException {
32+
String indexName = instance.getIndexName();
33+
Set<String> reloadedAnalyzers = new HashSet<>(instance.getReloadedAnalyzers());
34+
Set<String> reloadedIndicesNodes = new HashSet<>(instance.getReloadedIndicesNodes());
35+
int mutate = randomIntBetween(0, 2);
36+
switch (mutate) {
37+
case 0:
38+
indexName = indexName + randomAlphaOfLength(2);
39+
break;
40+
case 1:
41+
reloadedAnalyzers.add(randomAlphaOfLength(10));
42+
break;
43+
case 2:
44+
reloadedIndicesNodes.add(randomAlphaOfLength(10));
45+
break;
46+
default:
47+
throw new IllegalStateException("Requested to modify more than available parameters.");
48+
}
49+
return new ReloadDetails(indexName, reloadedIndicesNodes, reloadedAnalyzers);
50+
}
51+
52+
}

0 commit comments

Comments
 (0)