8
8
9
9
package org .opensearch .remotestore ;
10
10
11
+ import org .opensearch .action .support .WriteRequest ;
12
+ import org .opensearch .cluster .routing .ShardRouting ;
11
13
import org .opensearch .common .settings .Settings ;
14
+ import org .opensearch .index .shard .IndexShard ;
12
15
import org .opensearch .indices .replication .SegmentReplicationIT ;
16
+ import org .opensearch .test .InternalTestCluster ;
13
17
import org .opensearch .test .OpenSearchIntegTestCase ;
14
18
import org .junit .After ;
15
19
import org .junit .Before ;
16
20
17
21
import java .nio .file .Path ;
18
22
23
+ import static org .opensearch .test .hamcrest .OpenSearchAssertions .assertHitCount ;
24
+
19
25
/**
20
26
* This class runs Segment Replication Integ test suite with remote store enabled.
21
27
*/
@@ -40,6 +46,82 @@ protected boolean segmentReplicationWithRemoteEnabled() {
40
46
return true ;
41
47
}
42
48
49
+ public void testRestartPrimary_NoReplicas () throws Exception {
50
+ final String primary = internalCluster ().startDataOnlyNode ();
51
+ createIndex (INDEX_NAME );
52
+ ensureYellow (INDEX_NAME );
53
+
54
+ assertEquals (getNodeContainingPrimaryShard ().getName (), primary );
55
+
56
+ client ().prepareIndex (INDEX_NAME ).setId ("1" ).setSource ("foo" , "bar" ).setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE ).get ();
57
+ if (randomBoolean ()) {
58
+ flush (INDEX_NAME );
59
+ } else {
60
+ refresh (INDEX_NAME );
61
+ }
62
+
63
+ internalCluster ().restartNode (primary );
64
+ ensureYellow (INDEX_NAME );
65
+ assertDocCounts (1 , primary );
66
+ }
67
+
68
+ public void testReplicationPostDeleteAndForceMerge () throws Exception {
69
+ final String primary = internalCluster ().startDataOnlyNode ();
70
+ createIndex (INDEX_NAME );
71
+ final String replica = internalCluster ().startDataOnlyNode ();
72
+ ensureGreen (INDEX_NAME );
73
+ final int initialDocCount = scaledRandomIntBetween (1 , 10 );
74
+ for (int i = 0 ; i < initialDocCount ; i ++) {
75
+ client ().prepareIndex (INDEX_NAME ).setId (String .valueOf (i )).setSource ("foo" , "bar" ).get ();
76
+ }
77
+ refresh (INDEX_NAME );
78
+ waitForSearchableDocs (initialDocCount , primary , replica );
79
+
80
+ final int deletedDocCount = randomIntBetween (1 , initialDocCount );
81
+ for (int i = 0 ; i < deletedDocCount ; i ++) {
82
+ client (primary ).prepareDelete (INDEX_NAME , String .valueOf (i )).setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE ).get ();
83
+ }
84
+ client ().admin ().indices ().prepareForceMerge (INDEX_NAME ).setMaxNumSegments (1 ).setFlush (false ).get ();
85
+
86
+ // randomly flush here after the force merge to wipe any old segments.
87
+ if (randomBoolean ()) {
88
+ flush (INDEX_NAME );
89
+ }
90
+
91
+ final IndexShard primaryShard = getIndexShard (primary , INDEX_NAME );
92
+ final IndexShard replicaShard = getIndexShard (replica , INDEX_NAME );
93
+ assertBusy (
94
+ () -> assertEquals (
95
+ primaryShard .getLatestReplicationCheckpoint ().getSegmentInfosVersion (),
96
+ replicaShard .getLatestReplicationCheckpoint ().getSegmentInfosVersion ()
97
+ )
98
+ );
99
+
100
+ // add some docs to the xlog and drop primary.
101
+ final int additionalDocs = randomIntBetween (1 , 5 );
102
+ for (int i = initialDocCount ; i < initialDocCount + additionalDocs ; i ++) {
103
+ client ().prepareIndex (INDEX_NAME ).setId (String .valueOf (i )).setSource ("foo" , "bar" ).get ();
104
+ }
105
+ // Drop the primary and wait until replica is promoted.
106
+ internalCluster ().stopRandomNode (InternalTestCluster .nameFilter (primary ));
107
+ ensureYellowAndNoInitializingShards (INDEX_NAME );
108
+
109
+ final ShardRouting replicaShardRouting = getShardRoutingForNodeName (replica );
110
+ assertNotNull (replicaShardRouting );
111
+ assertTrue (replicaShardRouting + " should be promoted as a primary" , replicaShardRouting .primary ());
112
+ refresh (INDEX_NAME );
113
+ final long expectedHitCount = initialDocCount + additionalDocs - deletedDocCount ;
114
+ assertHitCount (client (replica ).prepareSearch (INDEX_NAME ).setSize (0 ).setPreference ("_only_local" ).get (), expectedHitCount );
115
+
116
+ int expectedMaxSeqNo = initialDocCount + deletedDocCount + additionalDocs - 1 ;
117
+ assertEquals (expectedMaxSeqNo , replicaShard .seqNoStats ().getMaxSeqNo ());
118
+
119
+ // index another doc.
120
+ client ().prepareIndex (INDEX_NAME ).setId (String .valueOf (expectedMaxSeqNo + 1 )).setSource ("another" , "doc" ).get ();
121
+ refresh (INDEX_NAME );
122
+ assertHitCount (client (replica ).prepareSearch (INDEX_NAME ).setSize (0 ).setPreference ("_only_local" ).get (), expectedHitCount + 1 );
123
+ }
124
+
43
125
@ Before
44
126
public void setup () {
45
127
internalCluster ().startClusterManagerOnlyNode ();
0 commit comments