Skip to content

Commit 0b85ec3

Browse files
author
Aman Khare
committed
Move common functionalities to BaseShardResponse
Signed-off-by: Aman Khare <[email protected]>
1 parent 69d15d8 commit 0b85ec3

File tree

4 files changed

+55
-31
lines changed

4 files changed

+55
-31
lines changed

server/src/internalClusterTest/java/org/opensearch/gateway/RecoveryFromGatewayIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,15 +812,15 @@ public void testShardFetchCorruptedShardsUsingBatchAction() throws Exception {
812812
.get(discoveryNodes[0].getId())
813813
.getNodeGatewayStartedShardsBatch()
814814
.get(shardId);
815-
assertNotNull(nodeGatewayStartedShards.storeException());
815+
assertNotNull(nodeGatewayStartedShards.getException());
816816
assertNotNull(nodeGatewayStartedShards.allocationId());
817817
assertTrue(nodeGatewayStartedShards.primary());
818818
}
819819

820820
private void assertNodeGatewayStartedShardsHappyCase(
821821
TransportNodesListGatewayStartedShardsBatch.NodeGatewayStartedShard nodeGatewayStartedShards
822822
) {
823-
assertNull(nodeGatewayStartedShards.storeException());
823+
assertNull(nodeGatewayStartedShards.getException());
824824
assertNotNull(nodeGatewayStartedShards.allocationId());
825825
assertTrue(nodeGatewayStartedShards.primary());
826826
}

server/src/main/java/org/opensearch/gateway/BaseShardResponse.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,34 @@
2121
* @opensearch.internal
2222
*/
2323
public abstract class BaseShardResponse extends TransportResponse {
24-
public BaseShardResponse() {}
24+
25+
private Exception storeException;
26+
27+
public BaseShardResponse(Exception storeException) {
28+
this.storeException = storeException;
29+
}
2530

2631
public abstract boolean isEmpty();
2732

28-
public abstract Exception getException();
33+
public Exception getException() {
34+
return storeException;
35+
}
2936

3037
public BaseShardResponse(StreamInput in) throws IOException {
31-
super(in);
38+
if (in.readBoolean()) {
39+
storeException = in.readException();
40+
} else {
41+
storeException = null;
42+
}
3243
}
3344

3445
@Override
35-
public void writeTo(StreamOutput out) throws IOException {}
46+
public void writeTo(StreamOutput out) throws IOException {
47+
if (storeException != null) {
48+
out.writeBoolean(true);
49+
out.writeException(storeException);
50+
} else {
51+
out.writeBoolean(false);
52+
}
53+
}
3654
}

server/src/main/java/org/opensearch/gateway/TransportNodesListGatewayStartedShards.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ protected NodeGatewayStartedShards nodeOperation(NodeRequest request) {
169169
shardInfo.allocationId(),
170170
shardInfo.primary(),
171171
shardInfo.replicationCheckpoint(),
172-
shardInfo.storeException()
172+
shardInfo.getException()
173173
);
174174
} catch (Exception e) {
175175
throw new OpenSearchException("failed to load started shards", e);

server/src/main/java/org/opensearch/gateway/TransportNodesListGatewayStartedShardsBatch.java

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -260,20 +260,20 @@ public void writeTo(StreamOutput out) throws IOException {
260260
*
261261
* @opensearch.internal
262262
*/
263-
public static class NodeGatewayStartedShard {
263+
public static class NodeGatewayStartedShard extends BaseShardResponse {
264264
private final String allocationId;
265265
private final boolean primary;
266-
private final Exception storeException;
267266
private final ReplicationCheckpoint replicationCheckpoint;
268267

268+
@Override
269+
public boolean isEmpty() {
270+
return allocationId == null && primary == false && getException() == null && replicationCheckpoint == null;
271+
}
272+
269273
public NodeGatewayStartedShard(StreamInput in) throws IOException {
274+
super(in);
270275
allocationId = in.readOptionalString();
271276
primary = in.readBoolean();
272-
if (in.readBoolean()) {
273-
storeException = in.readException();
274-
} else {
275-
storeException = null;
276-
}
277277
if (in.readBoolean()) {
278278
replicationCheckpoint = new ReplicationCheckpoint(in);
279279
} else {
@@ -291,10 +291,10 @@ public NodeGatewayStartedShard(
291291
ReplicationCheckpoint replicationCheckpoint,
292292
Exception storeException
293293
) {
294+
super(storeException);
294295
this.allocationId = allocationId;
295296
this.primary = primary;
296297
this.replicationCheckpoint = replicationCheckpoint;
297-
this.storeException = storeException;
298298
}
299299

300300
public String allocationId() {
@@ -309,19 +309,10 @@ public ReplicationCheckpoint replicationCheckpoint() {
309309
return this.replicationCheckpoint;
310310
}
311311

312-
public Exception storeException() {
313-
return this.storeException;
314-
}
315-
316312
public void writeTo(StreamOutput out) throws IOException {
313+
super.writeTo(out);
317314
out.writeOptionalString(allocationId);
318315
out.writeBoolean(primary);
319-
if (storeException != null) {
320-
out.writeBoolean(true);
321-
out.writeException(storeException);
322-
} else {
323-
out.writeBoolean(false);
324-
}
325316
if (replicationCheckpoint != null) {
326317
out.writeBoolean(true);
327318
replicationCheckpoint.writeTo(out);
@@ -343,15 +334,15 @@ public boolean equals(Object o) {
343334

344335
return primary == that.primary
345336
&& Objects.equals(allocationId, that.allocationId)
346-
&& Objects.equals(storeException, that.storeException)
337+
&& Objects.equals(getException(), that.getException())
347338
&& Objects.equals(replicationCheckpoint, that.replicationCheckpoint);
348339
}
349340

350341
@Override
351342
public int hashCode() {
352343
int result = (allocationId != null ? allocationId.hashCode() : 0);
353344
result = 31 * result + (primary ? 1 : 0);
354-
result = 31 * result + (storeException != null ? storeException.hashCode() : 0);
345+
result = 31 * result + (getException() != null ? getException().hashCode() : 0);
355346
result = 31 * result + (replicationCheckpoint != null ? replicationCheckpoint.hashCode() : 0);
356347
return result;
357348
}
@@ -360,8 +351,8 @@ public int hashCode() {
360351
public String toString() {
361352
StringBuilder buf = new StringBuilder();
362353
buf.append("NodeGatewayStartedShards[").append("allocationId=").append(allocationId).append(",primary=").append(primary);
363-
if (storeException != null) {
364-
buf.append(",storeException=").append(storeException);
354+
if (getException() != null) {
355+
buf.append(",storeException=").append(getException());
365356
}
366357
if (replicationCheckpoint != null) {
367358
buf.append(",ReplicationCheckpoint=").append(replicationCheckpoint.toString());
@@ -387,13 +378,28 @@ public Map<ShardId, NodeGatewayStartedShard> getNodeGatewayStartedShardsBatch()
387378

388379
public NodeGatewayStartedShardsBatch(StreamInput in) throws IOException {
389380
super(in);
390-
this.nodeGatewayStartedShardsBatch = in.readMap(ShardId::new, NodeGatewayStartedShard::new);
381+
this.nodeGatewayStartedShardsBatch = in.readMap(ShardId::new,
382+
i -> {
383+
if (i.readBoolean()) {
384+
return new NodeGatewayStartedShard(i);
385+
} else {
386+
return null;
387+
}
388+
});
391389
}
392390

393391
@Override
394392
public void writeTo(StreamOutput out) throws IOException {
395393
super.writeTo(out);
396-
out.writeMap(nodeGatewayStartedShardsBatch, (o, k) -> k.writeTo(o), (o, v) -> v.writeTo(o));
394+
out.writeMap(nodeGatewayStartedShardsBatch, (o, k) -> k.writeTo(o),
395+
(o, v) -> {
396+
if (v != null) {
397+
o.writeBoolean(true);
398+
v.writeTo(o);
399+
} else {
400+
o.writeBoolean(false);
401+
}
402+
});
397403
}
398404

399405
public NodeGatewayStartedShardsBatch(DiscoveryNode node, Map<ShardId, NodeGatewayStartedShard> nodeGatewayStartedShardsBatch) {

0 commit comments

Comments
 (0)