8
8
9
9
package org .opensearch .remotemigration ;
10
10
11
+ import com .carrotsearch .randomizedtesting .generators .RandomNumbers ;
12
+
13
+ import org .opensearch .action .DocWriteResponse ;
11
14
import org .opensearch .action .admin .cluster .health .ClusterHealthResponse ;
12
15
import org .opensearch .action .admin .cluster .settings .ClusterUpdateSettingsRequest ;
13
- import org .opensearch .action .admin .indices .replication .SegmentReplicationStatsResponse ;
14
16
import org .opensearch .action .admin .indices .settings .put .UpdateSettingsRequest ;
17
+ import org .opensearch .action .delete .DeleteResponse ;
18
+ import org .opensearch .action .index .IndexResponse ;
15
19
import org .opensearch .cluster .metadata .IndexMetadata ;
16
20
import org .opensearch .cluster .routing .allocation .command .MoveAllocationCommand ;
17
21
import org .opensearch .common .Priority ;
18
22
import org .opensearch .common .settings .Settings ;
19
23
import org .opensearch .common .unit .TimeValue ;
20
- import org .opensearch .index .SegmentReplicationPerGroupStats ;
21
24
import org .opensearch .index .query .QueryBuilders ;
22
25
import org .opensearch .test .OpenSearchIntegTestCase ;
23
26
import org .opensearch .test .hamcrest .OpenSearchAssertions ;
24
27
25
- import java .util .concurrent .TimeUnit ;
28
+ import java .util .concurrent .atomic .AtomicBoolean ;
29
+ import java .util .concurrent .atomic .AtomicInteger ;
26
30
27
31
import static org .opensearch .node .remotestore .RemoteStoreNodeService .MIGRATION_DIRECTION_SETTING ;
28
32
import static org .opensearch .node .remotestore .RemoteStoreNodeService .REMOTE_STORE_COMPATIBILITY_MODE_SETTING ;
29
33
import static org .opensearch .test .hamcrest .OpenSearchAssertions .assertAcked ;
30
34
31
35
@ OpenSearchIntegTestCase .ClusterScope (scope = OpenSearchIntegTestCase .Scope .TEST , numDataNodes = 0 , autoManageMasterNodes = false )
36
+
32
37
public class RemoteReplicaRecoveryIT extends MigrationBaseTestCase {
33
38
34
39
protected int maximumNumberOfShards () {
@@ -58,8 +63,10 @@ public void testReplicaRecovery() throws Exception {
58
63
client ().admin ().indices ().prepareCreate ("test" ).setSettings (indexSettings ()).setMapping ("field" , "type=text" ).get ();
59
64
String replicaNode = internalCluster ().startNode ();
60
65
ensureGreen ("test" );
61
- AsyncIndexingService asyncIndexingService = new AsyncIndexingService ("test" );
62
- asyncIndexingService .startIndexing ();
66
+
67
+ AtomicInteger numAutoGenDocs = new AtomicInteger ();
68
+ final AtomicBoolean finished = new AtomicBoolean (false );
69
+ Thread indexingThread = getThread (finished , numAutoGenDocs );
63
70
64
71
refresh ("test" );
65
72
@@ -71,10 +78,12 @@ public void testReplicaRecovery() throws Exception {
71
78
updateSettingsRequest .persistentSettings (Settings .builder ().put (MIGRATION_DIRECTION_SETTING .getKey (), "remote_store" ));
72
79
assertAcked (client ().admin ().cluster ().updateSettings (updateSettingsRequest ).actionGet ());
73
80
74
- internalCluster ().startNode ();
81
+ String remoteNode2 = internalCluster ().startNode ();
75
82
internalCluster ().validateClusterFormed ();
76
83
77
84
// identify the primary
85
+
86
+ Thread .sleep (RandomNumbers .randomIntBetween (random (), 0 , 2000 ));
78
87
logger .info ("--> relocating primary from {} to {} " , primaryNode , remoteNode );
79
88
client ().admin ()
80
89
.cluster ()
@@ -93,6 +102,7 @@ public void testReplicaRecovery() throws Exception {
93
102
94
103
assertEquals (0 , clusterHealthResponse .getRelocatingShards ());
95
104
logger .info ("--> relocation of primary from docrep to remote complete" );
105
+ Thread .sleep (RandomNumbers .randomIntBetween (random (), 0 , 2000 ));
96
106
97
107
logger .info ("--> getting up the new replicas now to doc rep node as well as remote node " );
98
108
// Increase replica count to 3
@@ -119,33 +129,52 @@ public void testReplicaRecovery() throws Exception {
119
129
logger .info ("--> replica is up now on another docrep now as well as remote node" );
120
130
121
131
assertEquals (0 , clusterHealthResponse .getRelocatingShards ());
122
- asyncIndexingService .stopIndexing ();
123
- refresh ("test" );
124
132
125
- // segrep lag should be zero
126
- assertBusy (() -> {
127
- SegmentReplicationStatsResponse segmentReplicationStatsResponse = dataNodeClient ().admin ()
128
- .indices ()
129
- .prepareSegmentReplicationStats ("test" )
130
- .setDetailed (true )
131
- .execute ()
132
- .actionGet ();
133
- SegmentReplicationPerGroupStats perGroupStats = segmentReplicationStatsResponse .getReplicationStats ().get ("test" ).get (0 );
134
- assertEquals (segmentReplicationStatsResponse .getReplicationStats ().size (), 1 );
135
- perGroupStats .getReplicaStats ().stream ().forEach (e -> assertEquals (e .getCurrentReplicationLagMillis (), 0 ));
136
- }, 20 , TimeUnit .SECONDS );
133
+ Thread .sleep (RandomNumbers .randomIntBetween (random (), 0 , 2000 ));
137
134
138
- OpenSearchAssertions .assertHitCount (
139
- client ().prepareSearch ("test" ).setTrackTotalHits (true ).get (),
140
- asyncIndexingService .getIndexedDocs ()
141
- );
135
+ // Stop replicas on docrep now.
136
+ // ToDo : Remove once we have dual replication enabled
137
+ client ().admin ()
138
+ .indices ()
139
+ .updateSettings (
140
+ new UpdateSettingsRequest ("test" ).settings (
141
+ Settings .builder ()
142
+ .put (IndexMetadata .SETTING_NUMBER_OF_REPLICAS , 1 )
143
+ .put ("index.routing.allocation.exclude._name" , primaryNode + "," + replicaNode )
144
+ .build ()
145
+ )
146
+ )
147
+ .get ();
148
+
149
+ finished .set (true );
150
+ indexingThread .join ();
151
+ refresh ("test" );
152
+ OpenSearchAssertions .assertHitCount (client ().prepareSearch ("test" ).setTrackTotalHits (true ).get (), numAutoGenDocs .get ());
142
153
OpenSearchAssertions .assertHitCount (
143
154
client ().prepareSearch ("test" )
144
155
.setTrackTotalHits (true )// extra paranoia ;)
145
156
.setQuery (QueryBuilders .termQuery ("auto" , true ))
157
+ // .setPreference("_prefer_nodes:" + (remoteNode+ "," + remoteNode2))
146
158
.get (),
147
- asyncIndexingService . getIndexedDocs ()
159
+ numAutoGenDocs . get ()
148
160
);
149
161
150
162
}
163
+
164
+ private Thread getThread (AtomicBoolean finished , AtomicInteger numAutoGenDocs ) {
165
+ Thread indexingThread = new Thread (() -> {
166
+ while (finished .get () == false && numAutoGenDocs .get () < 100 ) {
167
+ IndexResponse indexResponse = client ().prepareIndex ("test" ).setId ("id" ).setSource ("field" , "value" ).get ();
168
+ assertEquals (DocWriteResponse .Result .CREATED , indexResponse .getResult ());
169
+ DeleteResponse deleteResponse = client ().prepareDelete ("test" , "id" ).get ();
170
+ assertEquals (DocWriteResponse .Result .DELETED , deleteResponse .getResult ());
171
+ client ().prepareIndex ("test" ).setSource ("auto" , true ).get ();
172
+ numAutoGenDocs .incrementAndGet ();
173
+ logger .info ("Indexed {} docs here" , numAutoGenDocs .get ());
174
+ }
175
+ });
176
+ indexingThread .start ();
177
+ return indexingThread ;
178
+ }
179
+
151
180
}
0 commit comments