15
15
import org .opensearch .cluster .metadata .IndexMetadata ;
16
16
import org .opensearch .common .concurrent .GatedCloseable ;
17
17
import org .opensearch .common .lucene .Lucene ;
18
- import org .opensearch .common .lucene .search .Queries ;
19
18
import org .opensearch .common .settings .Settings ;
20
19
import org .opensearch .index .IndexSettings ;
21
- import org .opensearch .index .mapper .ParsedDocument ;
22
20
import org .opensearch .index .seqno .LocalCheckpointTracker ;
23
21
import org .opensearch .index .seqno .SequenceNumbers ;
24
22
import org .opensearch .index .store .Store ;
36
34
import java .util .stream .Collectors ;
37
35
38
36
import static org .hamcrest .Matchers .equalTo ;
39
- import static org .hamcrest .Matchers .notNullValue ;
40
37
import static org .opensearch .index .seqno .SequenceNumbers .NO_OPS_PERFORMED ;
41
38
import static org .opensearch .index .seqno .SequenceNumbers .LOCAL_CHECKPOINT_KEY ;
42
39
import static org .opensearch .index .seqno .SequenceNumbers .MAX_SEQ_NO ;
43
40
44
41
public class NRTReplicationEngineTests extends EngineTestCase {
45
42
43
+ private static final IndexSettings INDEX_SETTINGS = IndexSettingsModule .newIndexSettings (
44
+ "index" ,
45
+ Settings .builder ().put (IndexMetadata .SETTING_REPLICATION_TYPE , ReplicationType .SEGMENT ).build ()
46
+ );
47
+
46
48
public void testCreateEngine () throws IOException {
47
49
final AtomicLong globalCheckpoint = new AtomicLong (SequenceNumbers .NO_OPS_PERFORMED );
48
50
try (
49
- final Store nrtEngineStore = createStore ();
51
+ final Store nrtEngineStore = createStore (INDEX_SETTINGS , newDirectory () );
50
52
final NRTReplicationEngine nrtEngine = buildNrtReplicaEngine (globalCheckpoint , nrtEngineStore )
51
53
) {
52
54
final SegmentInfos latestSegmentInfos = nrtEngine .getLatestSegmentInfos ();
@@ -70,7 +72,7 @@ public void testEngineWritesOpsToTranslog() throws Exception {
70
72
final AtomicLong globalCheckpoint = new AtomicLong (SequenceNumbers .NO_OPS_PERFORMED );
71
73
72
74
try (
73
- final Store nrtEngineStore = createStore ();
75
+ final Store nrtEngineStore = createStore (INDEX_SETTINGS , newDirectory () );
74
76
final NRTReplicationEngine nrtEngine = buildNrtReplicaEngine (globalCheckpoint , nrtEngineStore )
75
77
) {
76
78
List <Engine .Operation > operations = generateHistoryOnReplica (
@@ -104,88 +106,63 @@ public void testEngineWritesOpsToTranslog() throws Exception {
104
106
}
105
107
}
106
108
107
- public void testUpdateSegments () throws Exception {
109
+ public void testUpdateSegments_CommitOnGenIncrease () throws IOException {
108
110
final AtomicLong globalCheckpoint = new AtomicLong (SequenceNumbers .NO_OPS_PERFORMED );
109
111
110
112
try (
111
- final Store nrtEngineStore = createStore ();
113
+ final Store nrtEngineStore = createStore (INDEX_SETTINGS , newDirectory () );
112
114
final NRTReplicationEngine nrtEngine = buildNrtReplicaEngine (globalCheckpoint , nrtEngineStore )
113
115
) {
114
- // add docs to the primary engine.
115
- List <Engine .Operation > operations = generateHistoryOnReplica (
116
- between (1 , 500 ),
117
- randomBoolean (),
118
- randomBoolean (),
119
- randomBoolean (),
120
- Engine .Operation .TYPE .INDEX
121
- );
122
-
123
- for (Engine .Operation op : operations ) {
124
- applyOperation (engine , op );
125
- applyOperation (nrtEngine , op );
126
- }
127
-
128
- engine .refresh ("test" );
129
-
130
- final SegmentInfos latestPrimaryInfos = engine .getLatestSegmentInfos ();
131
- nrtEngine .updateSegments (latestPrimaryInfos , engine .getProcessedLocalCheckpoint ());
132
- assertMatchingSegmentsAndCheckpoints (nrtEngine , latestPrimaryInfos );
133
-
134
- // assert a doc from the operations exists.
135
- final ParsedDocument parsedDoc = createParsedDoc (operations .stream ().findFirst ().get ().id (), null );
136
- try (Engine .GetResult getResult = engine .get (newGet (true , parsedDoc ), engine ::acquireSearcher )) {
137
- assertThat (getResult .exists (), equalTo (true ));
138
- assertThat (getResult .docIdAndVersion (), notNullValue ());
139
- }
140
-
141
- try (Engine .GetResult getResult = nrtEngine .get (newGet (true , parsedDoc ), nrtEngine ::acquireSearcher )) {
142
- assertThat (getResult .exists (), equalTo (true ));
143
- assertThat (getResult .docIdAndVersion (), notNullValue ());
144
- }
116
+ // assume we start at the same gen.
117
+ assertEquals (2 , nrtEngine .getLatestSegmentInfos ().getGeneration ());
118
+ assertEquals (nrtEngine .getLatestSegmentInfos ().getGeneration (), nrtEngine .getLastCommittedSegmentInfos ().getGeneration ());
119
+ assertEquals (engine .getLatestSegmentInfos ().getGeneration (), nrtEngine .getLatestSegmentInfos ().getGeneration ());
120
+
121
+ // flush the primary engine - we don't need any segments, just force a new commit point.
122
+ engine .flush (true , true );
123
+ assertEquals (3 , engine .getLatestSegmentInfos ().getGeneration ());
124
+ nrtEngine .updateSegments (engine .getLatestSegmentInfos (), engine .getProcessedLocalCheckpoint ());
125
+ assertEquals (4 , nrtEngine .getLastCommittedSegmentInfos ().getGeneration ());
126
+ assertEquals (4 , nrtEngine .getLatestSegmentInfos ().getGeneration ());
127
+ }
128
+ }
145
129
146
- // Flush the primary and update the NRTEngine with the latest committed infos.
147
- engine .flush ();
148
- nrtEngine .translogManager ().syncTranslog (); // to advance persisted checkpoint
130
+ public void updateSegments_replicaAtLowerCommitGen () throws IOException {
131
+ final AtomicLong globalCheckpoint = new AtomicLong (SequenceNumbers .NO_OPS_PERFORMED );
149
132
150
- Set <Long > seqNos = operations .stream ().map (Engine .Operation ::seqNo ).collect (Collectors .toSet ());
133
+ try (
134
+ final Store nrtEngineStore = createStore (INDEX_SETTINGS , newDirectory ());
135
+ final NRTReplicationEngine nrtEngine = buildNrtReplicaEngine (globalCheckpoint , nrtEngineStore )
136
+ ) {
137
+ // commit the infos to push us to segments_3.
138
+ nrtEngine .commitSegmentInfos ();
151
139
152
- nrtEngine .ensureOpen ();
153
- try (
154
- Translog .Snapshot snapshot = assertAndGetInternalTranslogManager (nrtEngine .translogManager ()).getTranslog ().newSnapshot ()
155
- ) {
156
- assertThat (snapshot .totalOperations (), equalTo (operations .size ()));
157
- assertThat (
158
- TestTranslog .drainSnapshot (snapshot , false ).stream ().map (Translog .Operation ::seqNo ).collect (Collectors .toSet ()),
159
- equalTo (seqNos )
160
- );
161
- }
140
+ // update the replica with a lower gen.
141
+ assertEquals (2 , engine .getLatestSegmentInfos ().getGeneration ());
142
+ nrtEngine .updateSegments (engine .getLatestSegmentInfos (), engine .getProcessedLocalCheckpoint ());
143
+ assertEquals (2 , nrtEngine .getLastCommittedSegmentInfos ().getGeneration ());
144
+ assertEquals (2 , nrtEngine .getLatestSegmentInfos ().getGeneration ());
145
+ }
146
+ }
162
147
163
- final SegmentInfos primaryInfos = engine .getLastCommittedSegmentInfos ();
164
- nrtEngine .updateSegments (primaryInfos , engine .getProcessedLocalCheckpoint ());
165
- assertMatchingSegmentsAndCheckpoints (nrtEngine , primaryInfos );
148
+ public void updateSegments_ReplicaAlreadyAtCommitGen () throws IOException {
149
+ final AtomicLong globalCheckpoint = new AtomicLong (SequenceNumbers .NO_OPS_PERFORMED );
166
150
167
- assertEquals (
168
- assertAndGetInternalTranslogManager (nrtEngine .translogManager ()).getTranslog ().getGeneration ().translogFileGeneration ,
169
- assertAndGetInternalTranslogManager (engine .translogManager ()).getTranslog ().getGeneration ().translogFileGeneration
170
- );
151
+ try (
152
+ final Store nrtEngineStore = createStore (INDEX_SETTINGS , newDirectory ());
153
+ final NRTReplicationEngine nrtEngine = buildNrtReplicaEngine (globalCheckpoint , nrtEngineStore )
154
+ ) {
155
+ // commit the infos to push us to segments_3.
156
+ nrtEngine .commitSegmentInfos ();
171
157
172
- try (
173
- Translog .Snapshot snapshot = assertAndGetInternalTranslogManager (nrtEngine .translogManager ()).getTranslog ().newSnapshot ()
174
- ) {
175
- assertThat (snapshot .totalOperations (), equalTo (operations .size ()));
176
- assertThat (
177
- TestTranslog .drainSnapshot (snapshot , false ).stream ().map (Translog .Operation ::seqNo ).collect (Collectors .toSet ()),
178
- equalTo (seqNos )
179
- );
180
- }
158
+ // update the replica with a lower gen.
159
+ assertEquals (2 , engine .getLatestSegmentInfos ().getGeneration ());
160
+ nrtEngine .updateSegments (engine .getLatestSegmentInfos (), engine .getProcessedLocalCheckpoint ());
161
+ assertEquals (2 , nrtEngine .getLastCommittedSegmentInfos ().getGeneration ());
162
+ assertEquals (2 , nrtEngine .getLatestSegmentInfos ().getGeneration ());
181
163
182
- // Ensure the same hit count between engines.
183
- int expectedDocCount ;
184
- try (final Engine .Searcher test = engine .acquireSearcher ("test" )) {
185
- expectedDocCount = test .count (Queries .newMatchAllQuery ());
186
- assertSearcherHits (nrtEngine , expectedDocCount );
187
- }
188
- assertEngineCleanedUp (nrtEngine , assertAndGetInternalTranslogManager (nrtEngine .translogManager ()).getDeletionPolicy ());
164
+ nrtEngine .close ();
165
+ assertEquals (3 , nrtEngine .getLastCommittedSegmentInfos ().getGeneration ());
189
166
}
190
167
}
191
168
@@ -227,12 +204,9 @@ public void testCommitSegmentInfos() throws Exception {
227
204
// This test asserts that NRTReplication#commitSegmentInfos creates a new commit point with the latest checkpoints
228
205
// stored in user data.
229
206
final AtomicLong globalCheckpoint = new AtomicLong (SequenceNumbers .NO_OPS_PERFORMED );
230
- final IndexSettings indexSettings = IndexSettingsModule .newIndexSettings (
231
- "index" ,
232
- Settings .builder ().put (IndexMetadata .SETTING_REPLICATION_TYPE , ReplicationType .SEGMENT ).build ()
233
- );
207
+
234
208
try (
235
- final Store nrtEngineStore = createStore (indexSettings , newDirectory ());
209
+ final Store nrtEngineStore = createStore (INDEX_SETTINGS , newDirectory ());
236
210
final NRTReplicationEngine nrtEngine = buildNrtReplicaEngine (globalCheckpoint , nrtEngineStore )
237
211
) {
238
212
List <Engine .Operation > operations = generateHistoryOnReplica (between (1 , 500 ), randomBoolean (), randomBoolean (), randomBoolean ())
0 commit comments