Skip to content

Commit d7c2ccf

Browse files
committed
ShardSearchFailure#readFrom to set index and shardId
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 elastic#32640
1 parent e1e8cf3 commit d7c2ccf

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
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+
super.index = shardTarget.getFullyQualifiedIndexName();
104+
super.shardId = shardTarget.getShardId().getId();
104105
}
105106
reason = in.readString();
106107
status = RestStatus.readFrom(in);

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
import org.elasticsearch.cluster.metadata.IndexMetaData;
2424
import org.elasticsearch.common.ParsingException;
2525
import org.elasticsearch.common.bytes.BytesReference;
26+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
2627
import org.elasticsearch.common.xcontent.ToXContent;
2728
import org.elasticsearch.common.xcontent.XContentParser;
2829
import org.elasticsearch.common.xcontent.XContentType;
2930
import org.elasticsearch.index.Index;
3031
import org.elasticsearch.index.shard.ShardId;
3132
import org.elasticsearch.search.SearchShardTarget;
3233
import org.elasticsearch.test.ESTestCase;
34+
import org.elasticsearch.test.VersionUtils;
3335

3436
import java.io.IOException;
3537

@@ -47,7 +49,7 @@ public static ShardSearchFailure createTestItem() {
4749
String indexName = randomAlphaOfLengthBetween(5, 10);
4850
String clusterAlias = randomBoolean() ? randomAlphaOfLengthBetween(5, 10) : null;
4951
searchShardTarget = new SearchShardTarget(nodeId,
50-
new ShardId(new Index(indexName, IndexMetaData.INDEX_UUID_NA_VALUE), randomInt()), clusterAlias, OriginalIndices.NONE);
52+
new ShardId(new Index(indexName, randomAlphaOfLength(12)), randomInt()), clusterAlias, OriginalIndices.NONE);
5153
}
5254
return new ShardSearchFailure(ex, searchShardTarget);
5355
}
@@ -134,4 +136,15 @@ public void testToXContentWithClusterAlias() throws IOException {
134136
+ "}",
135137
xContent.utf8ToString());
136138
}
139+
140+
public void testSerialization() throws IOException {
141+
ShardSearchFailure testItem = createTestItem();
142+
ShardSearchFailure deserializedInstance = copyStreamable(testItem, writableRegistry(),
143+
ShardSearchFailure::new, VersionUtils.randomVersion(random()));
144+
assertEquals(testItem.index(), deserializedInstance.index());
145+
assertEquals(testItem.shard(), deserializedInstance.shard());
146+
assertEquals(testItem.shardId(), deserializedInstance.shardId());
147+
assertEquals(testItem.reason(), deserializedInstance.reason());
148+
assertEquals(testItem.status(), deserializedInstance.status());
149+
}
137150
}

0 commit comments

Comments
 (0)