Skip to content

Commit f7da8c3

Browse files
Fix flaky test SegmentReplicationTargetServiceTests.testStartReplicationListenerSuccess (#17969)
* fix flaky test SegmentReplicationTargetServiceTests.testStartReplicationListenerSuccess Signed-off-by: guojialiang <[email protected]> * add UT Signed-off-by: guojialiang <[email protected]> * add comment Signed-off-by: guojialiang <[email protected]> * add tests Signed-off-by: guojialiang <[email protected]> --------- Signed-off-by: guojialiang <[email protected]>
1 parent 3638c13 commit f7da8c3

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

server/src/main/java/org/opensearch/index/shard/IndexShard.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,16 +1716,24 @@ public void releaseLockOnCommitData(String snapshotId, long primaryTerm, long ge
17161716
}
17171717

17181718
public Optional<NRTReplicationEngine> getReplicationEngine() {
1719-
if (getEngine() instanceof NRTReplicationEngine) {
1720-
return Optional.of((NRTReplicationEngine) getEngine());
1721-
} else {
1719+
try {
1720+
if (getEngine() instanceof NRTReplicationEngine) {
1721+
return Optional.of((NRTReplicationEngine) getEngine());
1722+
} else {
1723+
return Optional.empty();
1724+
}
1725+
} catch (AlreadyClosedException e) {
1726+
// If shard already closed, return empty. The logic related to segment replication will not continue to execute after judging
1727+
// that the return value is empty, and there will be no side effects.
1728+
logger.debug("failed to get ReplicationEngine", e);
17221729
return Optional.empty();
17231730
}
17241731
}
17251732

17261733
public void finalizeReplication(SegmentInfos infos) throws IOException {
1727-
if (getReplicationEngine().isPresent()) {
1728-
getReplicationEngine().get().updateSegments(infos);
1734+
Optional<NRTReplicationEngine> engineOptional = getReplicationEngine();
1735+
if (engineOptional.isPresent()) {
1736+
engineOptional.get().updateSegments(infos);
17291737
}
17301738
}
17311739

server/src/test/java/org/opensearch/index/shard/SegmentReplicationIndexShardTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ public void testReplication() throws Exception {
146146

147147
// Assertions
148148
shards.assertAllEqual(numDocs);
149+
assertTrue(primaryShard.getReplicationEngine().isEmpty());
150+
assertFalse(replicaShard.getReplicationEngine().isEmpty());
151+
replicaShard.close("test", false, false);
152+
assertTrue(replicaShard.getReplicationEngine().isEmpty());
149153
}
150154
}
151155

0 commit comments

Comments
 (0)