6060import org .opensearch .indices .replication .SegmentReplicationState ;
6161import org .opensearch .indices .replication .SegmentReplicationTarget ;
6262import org .opensearch .indices .replication .SegmentReplicationTargetService ;
63+ import org .opensearch .indices .replication .checkpoint .MergedSegmentPublisher ;
6364import org .opensearch .indices .replication .checkpoint .ReferencedSegmentsCheckpoint ;
6465import org .opensearch .indices .replication .checkpoint .ReplicationCheckpoint ;
6566import org .opensearch .indices .replication .checkpoint .SegmentReplicationCheckpointPublisher ;
7576import org .opensearch .snapshots .SnapshotInfoTests ;
7677import org .opensearch .snapshots .SnapshotShardsService ;
7778import org .opensearch .test .VersionUtils ;
79+ import org .opensearch .test .junit .annotations .TestLogging ;
7880import org .opensearch .threadpool .ThreadPool ;
7981import org .opensearch .transport .TransportService ;
8082import org .junit .Assert ;
@@ -156,9 +158,69 @@ public void testReplication() throws Exception {
156158 }
157159
158160 @ LockFeatureFlag (MERGED_SEGMENT_WARMER_EXPERIMENTAL_FLAG )
161+ @ TestLogging (reason = "Getting trace logs from MergedSegmentWarmer" , value = "org.opensearch.index.engine.MergedSegmentWarmer:TRACE" )
159162 public void testMergedSegmentReplication () throws Exception {
160163 // Test that the pre-copy merged segment logic does not block the merge process of the primary shard when there are 1 replica shard.
161- try (ReplicationGroup shards = createGroup (1 , getIndexSettings (), indexMapping , new NRTReplicationEngineFactory ());) {
164+ final RecoverySettings recoverySettings = new RecoverySettings (
165+ Settings .builder ().put (RecoverySettings .INDICES_MERGED_SEGMENT_REPLICATION_WARMER_ENABLED_SETTING .getKey (), true ).build (),
166+ new ClusterSettings (Settings .EMPTY , ClusterSettings .BUILT_IN_CLUSTER_SETTINGS )
167+ );
168+ try (
169+ ReplicationGroup shards = createGroup (
170+ 1 ,
171+ getIndexSettings (),
172+ indexMapping ,
173+ new NRTReplicationEngineFactory (),
174+ recoverySettings ,
175+ MergedSegmentPublisher .EMPTY
176+ )
177+ ) {
178+ shards .startAll ();
179+ final IndexShard primaryShard = shards .getPrimary ();
180+ final IndexShard replicaShard = shards .getReplicas ().get (0 );
181+
182+ // index and replicate segments to replica.
183+ int numDocs = randomIntBetween (10 , 20 );
184+ shards .indexDocs (numDocs );
185+ primaryShard .refresh ("test" );
186+ flushShard (primaryShard );
187+
188+ shards .indexDocs (numDocs );
189+ primaryShard .refresh ("test" );
190+ flushShard (primaryShard );
191+ replicateSegments (primaryShard , List .of (replicaShard ));
192+ shards .assertAllEqual (2 * numDocs );
193+
194+ primaryShard .forceMerge (new ForceMergeRequest ("test" ).maxNumSegments (1 ));
195+ replicateMergedSegments (primaryShard , List .of (replicaShard ));
196+ primaryShard .refresh ("test" );
197+ assertEquals (1 , primaryShard .segments (false ).size ());
198+ // After the pre-copy merged segment is completed, the merged segment is not visible in the replica, and the number of segments
199+ // in the replica shard is still 2.
200+ assertEquals (2 , replicaShard .segments (false ).size ());
201+ }
202+ }
203+
204+ @ LockFeatureFlag (MERGED_SEGMENT_WARMER_EXPERIMENTAL_FLAG )
205+ public void testMergedSegmentReplicationWithException () throws Exception {
206+ // Test that the pre-copy merged segment exception will not cause primary shard to fail
207+ MergedSegmentPublisher mergedSegmentPublisherWithException = new MergedSegmentPublisher ((indexShard , checkpoint ) -> {
208+ throw new RuntimeException ("mock exception" );
209+ });
210+ final RecoverySettings recoverySettings = new RecoverySettings (
211+ Settings .builder ().put (RecoverySettings .INDICES_MERGED_SEGMENT_REPLICATION_WARMER_ENABLED_SETTING .getKey (), true ).build (),
212+ new ClusterSettings (Settings .EMPTY , ClusterSettings .BUILT_IN_CLUSTER_SETTINGS )
213+ );
214+ try (
215+ ReplicationGroup shards = createGroup (
216+ 1 ,
217+ getIndexSettings (),
218+ indexMapping ,
219+ new NRTReplicationEngineFactory (),
220+ recoverySettings ,
221+ mergedSegmentPublisherWithException
222+ )
223+ ) {
162224 shards .startAll ();
163225 final IndexShard primaryShard = shards .getPrimary ();
164226 final IndexShard replicaShard = shards .getReplicas ().get (0 );
@@ -188,7 +250,20 @@ public void testMergedSegmentReplication() throws Exception {
188250 @ LockFeatureFlag (MERGED_SEGMENT_WARMER_EXPERIMENTAL_FLAG )
189251 public void testMergedSegmentReplicationWithZeroReplica () throws Exception {
190252 // Test that the pre-copy merged segment logic does not block the merge process of the primary shard when there are 0 replica shard.
191- try (ReplicationGroup shards = createGroup (0 , getIndexSettings (), indexMapping , new NRTReplicationEngineFactory ());) {
253+ final RecoverySettings recoverySettings = new RecoverySettings (
254+ Settings .builder ().put (RecoverySettings .INDICES_MERGED_SEGMENT_REPLICATION_WARMER_ENABLED_SETTING .getKey (), true ).build (),
255+ new ClusterSettings (Settings .EMPTY , ClusterSettings .BUILT_IN_CLUSTER_SETTINGS )
256+ );
257+ try (
258+ ReplicationGroup shards = createGroup (
259+ 0 ,
260+ getIndexSettings (),
261+ indexMapping ,
262+ new NRTReplicationEngineFactory (),
263+ recoverySettings ,
264+ MergedSegmentPublisher .EMPTY
265+ )
266+ ) {
192267 shards .startAll ();
193268 final IndexShard primaryShard = shards .getPrimary ();
194269
@@ -210,7 +285,20 @@ public void testMergedSegmentReplicationWithZeroReplica() throws Exception {
210285
211286 @ LockFeatureFlag (MERGED_SEGMENT_WARMER_EXPERIMENTAL_FLAG )
212287 public void testCleanupRedundantPendingMergeSegment () throws Exception {
213- try (ReplicationGroup shards = createGroup (1 , getIndexSettings (), indexMapping , new NRTReplicationEngineFactory ());) {
288+ final RecoverySettings recoverySettings = new RecoverySettings (
289+ Settings .builder ().put (RecoverySettings .INDICES_MERGED_SEGMENT_REPLICATION_WARMER_ENABLED_SETTING .getKey (), true ).build (),
290+ new ClusterSettings (Settings .EMPTY , ClusterSettings .BUILT_IN_CLUSTER_SETTINGS )
291+ );
292+ try (
293+ ReplicationGroup shards = createGroup (
294+ 1 ,
295+ getIndexSettings (),
296+ indexMapping ,
297+ new NRTReplicationEngineFactory (),
298+ recoverySettings ,
299+ MergedSegmentPublisher .EMPTY
300+ )
301+ ) {
214302 shards .startAll ();
215303 final IndexShard primaryShard = shards .getPrimary ();
216304 final IndexShard replicaShard = shards .getReplicas ().get (0 );
0 commit comments