Skip to content

Commit ed0571e

Browse files
authored
ShardSearchFailure#readFrom to set index and shardId (#33161)
As part of recent changes made to `ShardOperationFailedException` we introduced `index` and `shardId` members to the base class, but the subclasses are entirely responsible for the serialization of such fields. In the case of `ShardSearchFailure`, we have an additional `SearchShardTarget` instance member which also holds the index and the shardId, hence they get serialized as part of `SearchShardTarget` itself. When de-serializing a `ShardSearchFailure` though, we need to remember to also set the parent class `index` and `shardId` fields otherwise they get lost Relates to #32640
1 parent 318df2a commit ed0571e

File tree

6 files changed

+23
-9
lines changed

6 files changed

+23
-9
lines changed

server/src/main/java/org/elasticsearch/action/ShardOperationFailedException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
public abstract class ShardOperationFailedException implements Streamable, ToXContent {
3434

3535
protected String index;
36-
protected int shardId;
36+
protected int shardId = -1;
3737
protected String reason;
3838
protected RestStatus status;
3939
protected Throwable cause;

server/src/main/java/org/elasticsearch/action/search/ShardSearchFailure.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ public class ShardSearchFailure extends ShardOperationFailedException {
5454

5555
private SearchShardTarget shardTarget;
5656

57-
private ShardSearchFailure() {
58-
57+
ShardSearchFailure() {
5958
}
6059

6160
public ShardSearchFailure(Exception e) {
@@ -101,6 +100,8 @@ public static ShardSearchFailure readShardSearchFailure(StreamInput in) throws I
101100
public void readFrom(StreamInput in) throws IOException {
102101
if (in.readBoolean()) {
103102
shardTarget = new SearchShardTarget(in);
103+
index = shardTarget.getFullyQualifiedIndexName();
104+
shardId = shardTarget.getShardId().getId();
104105
}
105106
reason = in.readString();
106107
status = RestStatus.readFrom(in);

server/src/main/java/org/elasticsearch/action/support/replication/ReplicationResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public boolean primary() {
271271
public void readFrom(StreamInput in) throws IOException {
272272
shardId = ShardId.readShardId(in);
273273
super.shardId = shardId.getId();
274-
super.index = shardId.getIndexName();
274+
index = shardId.getIndexName();
275275
nodeId = in.readOptionalString();
276276
cause = in.readException();
277277
status = RestStatus.readFrom(in);

server/src/main/java/org/elasticsearch/snapshots/SnapshotShardFailure.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void readFrom(StreamInput in) throws IOException {
102102
nodeId = in.readOptionalString();
103103
shardId = ShardId.readShardId(in);
104104
super.shardId = shardId.getId();
105-
super.index = shardId.getIndexName();
105+
index = shardId.getIndexName();
106106
reason = in.readString();
107107
status = RestStatus.readFrom(in);
108108
}

server/src/test/java/org/elasticsearch/action/search/SearchResponseTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.action.search;
2121

22+
import org.elasticsearch.cluster.metadata.IndexMetaData;
2223
import org.elasticsearch.common.Strings;
2324
import org.elasticsearch.common.bytes.BytesReference;
2425
import org.elasticsearch.common.io.stream.BytesStreamOutput;
@@ -180,7 +181,7 @@ public void testFromXContentWithFailures() throws IOException {
180181
int numFailures = randomIntBetween(1, 5);
181182
ShardSearchFailure[] failures = new ShardSearchFailure[numFailures];
182183
for (int i = 0; i < failures.length; i++) {
183-
failures[i] = ShardSearchFailureTests.createTestItem();
184+
failures[i] = ShardSearchFailureTests.createTestItem(IndexMetaData.INDEX_UUID_NA_VALUE);
184185
}
185186
SearchResponse response = createTestItem(failures);
186187
XContentType xcontentType = randomFrom(XContentType.values());

server/src/test/java/org/elasticsearch/action/search/ShardSearchFailureTests.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.elasticsearch.index.shard.ShardId;
3131
import org.elasticsearch.search.SearchShardTarget;
3232
import org.elasticsearch.test.ESTestCase;
33+
import org.elasticsearch.test.VersionUtils;
3334

3435
import java.io.IOException;
3536

@@ -38,7 +39,7 @@
3839

3940
public class ShardSearchFailureTests extends ESTestCase {
4041

41-
public static ShardSearchFailure createTestItem() {
42+
public static ShardSearchFailure createTestItem(String indexUuid) {
4243
String randomMessage = randomAlphaOfLengthBetween(3, 20);
4344
Exception ex = new ParsingException(0, 0, randomMessage , new IllegalArgumentException("some bad argument"));
4445
SearchShardTarget searchShardTarget = null;
@@ -47,7 +48,7 @@ public static ShardSearchFailure createTestItem() {
4748
String indexName = randomAlphaOfLengthBetween(5, 10);
4849
String clusterAlias = randomBoolean() ? randomAlphaOfLengthBetween(5, 10) : null;
4950
searchShardTarget = new SearchShardTarget(nodeId,
50-
new ShardId(new Index(indexName, IndexMetaData.INDEX_UUID_NA_VALUE), randomInt()), clusterAlias, OriginalIndices.NONE);
51+
new ShardId(new Index(indexName, indexUuid), randomInt()), clusterAlias, OriginalIndices.NONE);
5152
}
5253
return new ShardSearchFailure(ex, searchShardTarget);
5354
}
@@ -66,7 +67,7 @@ public void testFromXContentWithRandomFields() throws IOException {
6667
}
6768

6869
private void doFromXContentTestWithRandomFields(boolean addRandomFields) throws IOException {
69-
ShardSearchFailure response = createTestItem();
70+
ShardSearchFailure response = createTestItem(IndexMetaData.INDEX_UUID_NA_VALUE);
7071
XContentType xContentType = randomFrom(XContentType.values());
7172
boolean humanReadable = randomBoolean();
7273
BytesReference originalBytes = toShuffledXContent(response, xContentType, ToXContent.EMPTY_PARAMS, humanReadable);
@@ -134,4 +135,15 @@ public void testToXContentWithClusterAlias() throws IOException {
134135
+ "}",
135136
xContent.utf8ToString());
136137
}
138+
139+
public void testSerialization() throws IOException {
140+
ShardSearchFailure testItem = createTestItem(randomAlphaOfLength(12));
141+
ShardSearchFailure deserializedInstance = copyStreamable(testItem, writableRegistry(),
142+
ShardSearchFailure::new, VersionUtils.randomVersion(random()));
143+
assertEquals(testItem.index(), deserializedInstance.index());
144+
assertEquals(testItem.shard(), deserializedInstance.shard());
145+
assertEquals(testItem.shardId(), deserializedInstance.shardId());
146+
assertEquals(testItem.reason(), deserializedInstance.reason());
147+
assertEquals(testItem.status(), deserializedInstance.status());
148+
}
137149
}

0 commit comments

Comments
 (0)