Skip to content

Commit 923b33c

Browse files
committed
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 00d07ca commit 923b33c

File tree

6 files changed

+37
-9
lines changed

6 files changed

+37
-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
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.action.search;
2121

2222
import org.elasticsearch.Version;
23+
import org.elasticsearch.cluster.metadata.IndexMetaData;
2324
import org.elasticsearch.common.Strings;
2425
import org.elasticsearch.common.bytes.BytesReference;
2526
import org.elasticsearch.common.io.stream.BytesStreamOutput;
@@ -183,7 +184,7 @@ public void testFromXContentWithFailures() throws IOException {
183184
int numFailures = randomIntBetween(1, 5);
184185
ShardSearchFailure[] failures = new ShardSearchFailure[numFailures];
185186
for (int i = 0; i < failures.length; i++) {
186-
failures[i] = ShardSearchFailureTests.createTestItem();
187+
failures[i] = ShardSearchFailureTests.createTestItem(IndexMetaData.INDEX_UUID_NA_VALUE);
187188
}
188189
SearchResponse response = createTestItem(failures);
189190
XContentType xcontentType = randomFrom(XContentType.values());

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.action.search;
2121

22+
import org.elasticsearch.Version;
2223
import org.elasticsearch.action.OriginalIndices;
2324
import org.elasticsearch.cluster.metadata.IndexMetaData;
2425
import org.elasticsearch.common.ParsingException;
@@ -30,6 +31,7 @@
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

@@ -38,7 +40,7 @@
3840

3941
public class ShardSearchFailureTests extends ESTestCase {
4042

41-
public static ShardSearchFailure createTestItem() {
43+
public static ShardSearchFailure createTestItem(String indexUuid) {
4244
String randomMessage = randomAlphaOfLengthBetween(3, 20);
4345
Exception ex = new ParsingException(0, 0, randomMessage , new IllegalArgumentException("some bad argument"));
4446
SearchShardTarget searchShardTarget = null;
@@ -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, indexUuid), randomInt()), clusterAlias, OriginalIndices.NONE);
5153
}
5254
return new ShardSearchFailure(ex, searchShardTarget);
5355
}
@@ -66,7 +68,7 @@ public void testFromXContentWithRandomFields() throws IOException {
6668
}
6769

6870
private void doFromXContentTestWithRandomFields(boolean addRandomFields) throws IOException {
69-
ShardSearchFailure response = createTestItem();
71+
ShardSearchFailure response = createTestItem(IndexMetaData.INDEX_UUID_NA_VALUE);
7072
XContentType xContentType = randomFrom(XContentType.values());
7173
boolean humanReadable = randomBoolean();
7274
BytesReference originalBytes = toShuffledXContent(response, xContentType, ToXContent.EMPTY_PARAMS, humanReadable);
@@ -134,4 +136,28 @@ public void testToXContentWithClusterAlias() throws IOException {
134136
+ "}",
135137
xContent.utf8ToString());
136138
}
139+
140+
public void testSerialization() throws IOException {
141+
ShardSearchFailure testItem = createTestItem(randomAlphaOfLength(12));
142+
Version version = VersionUtils.randomVersion(random());
143+
ShardSearchFailure deserializedInstance = copyStreamable(testItem, writableRegistry(),
144+
ShardSearchFailure::new, version);
145+
if (version.onOrAfter(Version.V_5_6_0)) {
146+
assertEquals(testItem.index(), deserializedInstance.index());
147+
assertEquals(testItem.shard(), deserializedInstance.shard());
148+
} else {
149+
//clusterAlias does not get serialized prior to 5.6
150+
if (testItem.shard() != null && testItem.shard().getClusterAlias() != null) {
151+
SearchShardTarget shard = testItem.shard();
152+
assertEquals(shard.getIndex(), deserializedInstance.index());
153+
SearchShardTarget newShard = new SearchShardTarget(shard.getNodeId(), shard.getShardId(), null, shard.getOriginalIndices());
154+
assertEquals(newShard, deserializedInstance.shard());
155+
} else {
156+
assertEquals(testItem.shard(), deserializedInstance.shard());
157+
}
158+
}
159+
assertEquals(testItem.shardId(), deserializedInstance.shardId());
160+
assertEquals(testItem.reason(), deserializedInstance.reason());
161+
assertEquals(testItem.status(), deserializedInstance.status());
162+
}
137163
}

0 commit comments

Comments
 (0)