@@ -522,7 +522,7 @@ public void testRecoveryWithDerivedSourceEnabled() throws Exception {
522
522
assertAcked (
523
523
prepareCreate (
524
524
"test" ,
525
- 2 ,
525
+ 1 ,
526
526
Settings .builder ()
527
527
.put (SETTING_NUMBER_OF_SHARDS , numberOfShards )
528
528
.put (SETTING_NUMBER_OF_REPLICAS , 1 )
@@ -541,7 +541,7 @@ public void testRecoveryWithDerivedSourceEnabled() throws Exception {
541
541
}
542
542
}
543
543
544
- logger .info ("--> allow 2 nodes for index [test] ..." );
544
+ logger .info ("--> allow 2 nodes for index [test] with replica ..." );
545
545
allowNodes ("test" , 2 );
546
546
547
547
logger .info ("--> waiting for GREEN health status ..." );
@@ -585,6 +585,7 @@ public void testReplicaRecoveryWithDerivedSourceBeforeRefresh() throws Exception
585
585
.put (SETTING_NUMBER_OF_REPLICAS , 0 )
586
586
.put (IndexSettings .INDEX_DERIVED_SOURCE_SETTING .getKey (), true )
587
587
.put (IndexSettings .INDEX_TRANSLOG_DURABILITY_SETTING .getKey (), Translog .Durability .ASYNC )
588
+ .put ("index.refresh_interval" , -1 )
588
589
).setMapping (mapping )
589
590
);
590
591
@@ -668,7 +669,9 @@ public void testReplicaRecoveryWithDerivedSourceFromTranslog() throws Exception
668
669
669
670
// Kill replica node and index more documents
670
671
final String replicaNode = ensureReplicaNode ("test" );
671
- internalCluster ().stopRandomNode (InternalTestCluster .nameFilter (replicaNode ));
672
+ if (replicaNode != null ) {
673
+ internalCluster ().stopRandomNode (InternalTestCluster .nameFilter (replicaNode ));
674
+ }
672
675
673
676
int additionalDocs = randomIntBetween (50 , 100 );
674
677
for (int i = docCount ; i < docCount + additionalDocs ; i ++) {
@@ -722,7 +725,7 @@ public void testRecoverWhileUnderLoadWithDerivedSource() throws Exception {
722
725
assertAcked (
723
726
prepareCreate (
724
727
"test" ,
725
- 2 ,
728
+ 1 ,
726
729
Settings .builder ()
727
730
.put (SETTING_NUMBER_OF_SHARDS , numberOfShards )
728
731
.put (SETTING_NUMBER_OF_REPLICAS , 1 )
@@ -781,18 +784,37 @@ protected XContentBuilder generateSource(long id, Random random) throws IOExcept
781
784
logger .info ("--> verifying indexed content" );
782
785
783
786
// Verify docs on primary
784
- assertPrimaryDocCount (totalNumDocs );
787
+ SearchResponse primaryResponse = client ().prepareSearch ("test" ).setPreference ("_primary" ).setTrackTotalHits (true ).get ();
788
+ assertHitCount (primaryResponse , totalNumDocs );
785
789
786
790
// Verify docs and derived source on replica
787
- assertReplicaDocsAndSource (totalNumDocs );
791
+ assertBusy (() -> {
792
+ SearchResponse replicaResponse = client ().prepareSearch ("test" )
793
+ .setPreference ("_replica" )
794
+ .setTrackTotalHits (true )
795
+ .setSize (totalNumDocs )
796
+ .addSort ("value" , SortOrder .ASC )
797
+ .get ();
798
+
799
+ assertHitCount (replicaResponse , totalNumDocs );
800
+
801
+ // Verify source reconstruction on replica
802
+ for (SearchHit hit : replicaResponse .getHits ()) {
803
+ assertNotNull (hit .getSourceAsMap ());
804
+ assertEquals (3 , hit .getSourceAsMap ().size ());
805
+ int id = (Integer ) hit .getSourceAsMap ().get ("value" );
806
+ assertEquals ("name_" + id , hit .getSourceAsMap ().get ("name" ));
807
+ assertNotNull (hit .getSourceAsMap ().get ("timestamp" ));
808
+ }
809
+ }, 30 , TimeUnit .SECONDS );
788
810
789
811
// Additional source verification with random sampling
790
812
assertRandomDocsSource (50 );
791
813
}
792
814
}
793
815
794
816
public void testRecoverWithRelocationAndDerivedSource () throws Exception {
795
- final int numShards = between (2 , 5 );
817
+ final int numShards = between (3 , 5 );
796
818
logger .info ("--> creating test index with derived source enabled..." );
797
819
798
820
String mapping = """
@@ -813,7 +835,7 @@ public void testRecoverWithRelocationAndDerivedSource() throws Exception {
813
835
assertAcked (
814
836
prepareCreate (
815
837
"test" ,
816
- 3 ,
838
+ 1 ,
817
839
Settings .builder ()
818
840
.put (SETTING_NUMBER_OF_SHARDS , numShards )
819
841
.put (SETTING_NUMBER_OF_REPLICAS , 0 )
@@ -822,9 +844,8 @@ public void testRecoverWithRelocationAndDerivedSource() throws Exception {
822
844
.put (IndexService .RETENTION_LEASE_SYNC_INTERVAL_SETTING .getKey (), "100ms" )
823
845
).setMapping (mapping )
824
846
);
825
-
826
- final int numDocs = scaledRandomIntBetween (2000 , 10000 );
827
- int allowNodes = 2 ;
847
+ int numNodes = 1 ;
848
+ final int numDocs = scaledRandomIntBetween (1500 , 2000 );
828
849
829
850
try (BackgroundIndexer indexer = new BackgroundIndexer ("test" , null , client (), numDocs ) {
830
851
@ Override
@@ -836,14 +857,13 @@ protected XContentBuilder generateSource(long id, Random random) throws IOExcept
836
857
.endObject ();
837
858
}
838
859
}) {
839
- for (int i = 0 ; i < numDocs ; i += scaledRandomIntBetween (100 , Math .min (1000 , numDocs ))) {
860
+
861
+ for (int i = 0 ; i < numDocs ; i += scaledRandomIntBetween (500 , Math .min (1000 , numDocs ))) {
840
862
indexer .assertNoFailures ();
841
863
logger .info ("--> waiting for {} docs to be indexed ..." , i );
842
864
waitForDocs (i , indexer );
843
-
844
- // Alternate between 1 and 2 nodes to force relocation
845
- allowNodes = 3 - allowNodes ; // Toggle between 1 and 2
846
- allowNodes ("test" , allowNodes );
865
+ internalCluster ().startDataOnlyNode ();
866
+ numNodes ++;
847
867
848
868
logger .info ("--> waiting for GREEN health status ..." );
849
869
ensureGreen (TimeValue .timeValueMinutes (2 ));
@@ -854,12 +874,11 @@ protected XContentBuilder generateSource(long id, Random random) throws IOExcept
854
874
855
875
// Add replicas after stopping indexing
856
876
logger .info ("--> adding replicas ..." );
857
- allowNodes ("test" , 3 );
858
877
assertAcked (
859
878
client ().admin ()
860
879
.indices ()
861
880
.prepareUpdateSettings ("test" )
862
- .setSettings (Settings .builder ().put (SETTING_NUMBER_OF_REPLICAS , 1 ))
881
+ .setSettings (Settings .builder ().put (SETTING_NUMBER_OF_REPLICAS , numNodes - 1 ))
863
882
.get ()
864
883
);
865
884
ensureGreen (TimeValue .timeValueMinutes (2 ));
@@ -868,37 +887,31 @@ protected XContentBuilder generateSource(long id, Random random) throws IOExcept
868
887
client ().admin ().indices ().prepareRefresh ().get ();
869
888
870
889
// Verify final doc count and derived source reconstruction
871
- assertPrimaryDocCount (numDocs );
872
- assertReplicaDocsAndSource (numDocs );
873
- assertRandomDocsSource (100 );
874
- }
875
- }
876
-
877
- private void assertPrimaryDocCount (int expectedCount ) {
878
- SearchResponse primaryResponse = client ().prepareSearch ("test" ).setPreference ("_primary" ).setTrackTotalHits (true ).get ();
879
- assertHitCount (primaryResponse , expectedCount );
880
- }
890
+ SearchResponse primaryResponse = client ().prepareSearch ("test" ).setPreference ("_primary" ).setTrackTotalHits (true ).get ();
891
+ assertHitCount (primaryResponse , numDocs );
881
892
882
- private void assertReplicaDocsAndSource (int expectedCount ) throws Exception {
883
- assertBusy (() -> {
884
- SearchResponse replicaResponse = client ().prepareSearch ("test" )
885
- .setPreference ("_replica" )
886
- .setTrackTotalHits (true )
887
- .setSize (expectedCount )
888
- .addSort ("value" , SortOrder .ASC )
889
- .get ();
890
-
891
- assertHitCount (replicaResponse , expectedCount );
893
+ assertBusy (() -> {
894
+ SearchResponse replicaResponse = client ().prepareSearch ("test" )
895
+ .setPreference ("_replica" )
896
+ .setTrackTotalHits (true )
897
+ .setSize (numDocs )
898
+ .addSort ("value" , SortOrder .ASC )
899
+ .get ();
900
+
901
+ assertHitCount (replicaResponse , numDocs );
902
+
903
+ // Verify source reconstruction on replica
904
+ for (SearchHit hit : replicaResponse .getHits ()) {
905
+ assertNotNull (hit .getSourceAsMap ());
906
+ assertEquals (3 , hit .getSourceAsMap ().size ());
907
+ int id = (Integer ) hit .getSourceAsMap ().get ("value" );
908
+ assertEquals ("name_" + id , hit .getSourceAsMap ().get ("name" ));
909
+ assertNotNull (hit .getSourceAsMap ().get ("timestamp" ));
910
+ }
911
+ }, 30 , TimeUnit .SECONDS );
892
912
893
- // Verify source reconstruction on replica
894
- for (SearchHit hit : replicaResponse .getHits ()) {
895
- assertNotNull (hit .getSourceAsMap ());
896
- assertEquals (3 , hit .getSourceAsMap ().size ());
897
- int id = (Integer ) hit .getSourceAsMap ().get ("value" );
898
- assertEquals ("name_" + id , hit .getSourceAsMap ().get ("name" ));
899
- assertNotNull (hit .getSourceAsMap ().get ("timestamp" ));
900
- }
901
- }, 30 , TimeUnit .SECONDS );
913
+ assertRandomDocsSource (100 );
914
+ }
902
915
}
903
916
904
917
private void assertRandomDocsSource (int sampleSize ) {
@@ -921,7 +934,11 @@ private void assertRandomDocsSource(int sampleSize) {
921
934
private String ensureReplicaNode (String index ) {
922
935
ClusterState state = client ().admin ().cluster ().prepareState ().get ().getState ();
923
936
Index idx = state .metadata ().index (index ).getIndex ();
924
- String nodeId = state .routingTable ().index (idx ).shard (0 ).replicaShards ().get (0 ).currentNodeId ();
925
- return state .nodes ().get (nodeId ).getName ();
937
+ String replicaNode = state .routingTable ().index (idx ).shard (0 ).replicaShards ().get (0 ).currentNodeId ();
938
+ String clusterManagerNode = internalCluster ().getClusterManagerName ();
939
+ if (!replicaNode .equals (clusterManagerNode )) {
940
+ state .nodes ().get (replicaNode ).getName ();
941
+ }
942
+ return null ;
926
943
}
927
944
}
0 commit comments