18
18
import org .elasticsearch .search .SearchHit ;
19
19
import org .elasticsearch .search .SearchHits ;
20
20
import org .elasticsearch .test .ESTestCase ;
21
+ import org .junit .Before ;
21
22
22
23
import java .io .IOException ;
23
24
import java .util .Collections ;
34
35
public class AsyncTwoPhaseIndexerTests extends ESTestCase {
35
36
36
37
AtomicBoolean isFinished = new AtomicBoolean (false );
38
+ AtomicBoolean isStopped = new AtomicBoolean (false );
39
+
40
+ @ Before
41
+ public void reset () {
42
+ isFinished .set (false );
43
+ isStopped .set (false );
44
+ }
37
45
38
46
private class MockIndexer extends AsyncTwoPhaseIndexer <Integer , MockJobStats > {
39
47
40
48
private final CountDownLatch latch ;
41
49
// test the execution order
42
50
private volatile int step ;
51
+ private final boolean stoppedBeforeFinished ;
43
52
44
53
protected MockIndexer (Executor executor , AtomicReference <IndexerState > initialState , Integer initialPosition ,
45
- CountDownLatch latch ) {
54
+ CountDownLatch latch , boolean stoppedBeforeFinished ) {
46
55
super (executor , initialState , initialPosition , new MockJobStats ());
47
56
this .latch = latch ;
57
+ this .stoppedBeforeFinished = stoppedBeforeFinished ;
48
58
}
49
59
50
60
@ Override
@@ -57,7 +67,7 @@ protected IterationResult<Integer> doProcess(SearchResponse searchResponse) {
57
67
awaitForLatch ();
58
68
assertThat (step , equalTo (3 ));
59
69
++step ;
60
- return new IterationResult <Integer >(Collections .emptyList (), 3 , true );
70
+ return new IterationResult <>(Collections .emptyList (), 3 , true );
61
71
}
62
72
63
73
private void awaitForLatch () {
@@ -99,7 +109,8 @@ protected void doNextBulk(BulkRequest request, ActionListener<BulkResponse> next
99
109
100
110
@ Override
101
111
protected void doSaveState (IndexerState state , Integer position , Runnable next ) {
102
- assertThat (step , equalTo (5 ));
112
+ int expectedStep = stoppedBeforeFinished ? 3 : 5 ;
113
+ assertThat (step , equalTo (expectedStep ));
103
114
++step ;
104
115
next .run ();
105
116
}
@@ -114,7 +125,12 @@ protected void onFinish(ActionListener<Void> listener) {
114
125
assertThat (step , equalTo (4 ));
115
126
++step ;
116
127
listener .onResponse (null );
117
- isFinished .set (true );
128
+ assertTrue (isFinished .compareAndSet (false , true ));
129
+ }
130
+
131
+ @ Override
132
+ protected void onStop () {
133
+ assertTrue (isStopped .compareAndSet (false , true ));
118
134
}
119
135
120
136
@ Override
@@ -180,7 +196,7 @@ protected void doSaveState(IndexerState state, Integer position, Runnable next)
180
196
protected void onFailure (Exception exc ) {
181
197
assertThat (step , equalTo (2 ));
182
198
++step ;
183
- isFinished .set ( true );
199
+ assertTrue ( isFinished .compareAndSet ( false , true ) );
184
200
}
185
201
186
202
@ Override
@@ -209,18 +225,18 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
209
225
public void testStateMachine () throws Exception {
210
226
AtomicReference <IndexerState > state = new AtomicReference <>(IndexerState .STOPPED );
211
227
final ExecutorService executor = Executors .newFixedThreadPool (1 );
212
- isFinished .set (false );
213
228
try {
214
229
CountDownLatch countDownLatch = new CountDownLatch (1 );
215
- MockIndexer indexer = new MockIndexer (executor , state , 2 , countDownLatch );
230
+ MockIndexer indexer = new MockIndexer (executor , state , 2 , countDownLatch , false );
216
231
indexer .start ();
217
232
assertThat (indexer .getState (), equalTo (IndexerState .STARTED ));
218
233
assertTrue (indexer .maybeTriggerAsyncJob (System .currentTimeMillis ()));
219
234
assertThat (indexer .getState (), equalTo (IndexerState .INDEXING ));
220
235
countDownLatch .countDown ();
221
236
222
237
assertThat (indexer .getPosition (), equalTo (2 ));
223
- ESTestCase .awaitBusy (() -> isFinished .get ());
238
+ assertTrue (awaitBusy (() -> isFinished .get ()));
239
+ assertFalse (isStopped .get ());
224
240
assertThat (indexer .getStep (), equalTo (6 ));
225
241
assertThat (indexer .getStats ().getNumInvocations (), equalTo (1L ));
226
242
assertThat (indexer .getStats ().getNumPages (), equalTo (1L ));
@@ -234,18 +250,57 @@ public void testStateMachine() throws Exception {
234
250
public void testStateMachineBrokenSearch () throws InterruptedException {
235
251
AtomicReference <IndexerState > state = new AtomicReference <>(IndexerState .STOPPED );
236
252
final ExecutorService executor = Executors .newFixedThreadPool (1 );
237
- isFinished .set (false );
238
253
try {
239
254
240
255
MockIndexerThrowsFirstSearch indexer = new MockIndexerThrowsFirstSearch (executor , state , 2 );
241
256
indexer .start ();
242
257
assertThat (indexer .getState (), equalTo (IndexerState .STARTED ));
243
258
assertTrue (indexer .maybeTriggerAsyncJob (System .currentTimeMillis ()));
244
- assertTrue (ESTestCase . awaitBusy (() -> isFinished .get (), 10000 , TimeUnit .SECONDS ));
259
+ assertTrue (awaitBusy (() -> isFinished .get (), 10000 , TimeUnit .SECONDS ));
245
260
assertThat (indexer .getStep (), equalTo (3 ));
246
261
247
262
} finally {
248
263
executor .shutdownNow ();
249
264
}
250
265
}
266
+
267
+ public void testStop_AfterIndexerIsFinished () throws InterruptedException {
268
+ AtomicReference <IndexerState > state = new AtomicReference <>(IndexerState .STOPPED );
269
+ final ExecutorService executor = Executors .newFixedThreadPool (1 );
270
+ try {
271
+ CountDownLatch countDownLatch = new CountDownLatch (1 );
272
+ MockIndexer indexer = new MockIndexer (executor , state , 2 , countDownLatch , false );
273
+ indexer .start ();
274
+ assertTrue (indexer .maybeTriggerAsyncJob (System .currentTimeMillis ()));
275
+ countDownLatch .countDown ();
276
+ assertTrue (awaitBusy (() -> isFinished .get ()));
277
+
278
+ indexer .stop ();
279
+ assertTrue (isStopped .get ());
280
+ assertThat (indexer .getState (), equalTo (IndexerState .STOPPED ));
281
+ } finally {
282
+ executor .shutdownNow ();
283
+ }
284
+ }
285
+
286
+ public void testStop_WhileIndexing () throws InterruptedException {
287
+ AtomicReference <IndexerState > state = new AtomicReference <>(IndexerState .STOPPED );
288
+ final ExecutorService executor = Executors .newFixedThreadPool (1 );
289
+ try {
290
+ CountDownLatch countDownLatch = new CountDownLatch (1 );
291
+ MockIndexer indexer = new MockIndexer (executor , state , 2 , countDownLatch , true );
292
+ indexer .start ();
293
+ assertThat (indexer .getState (), equalTo (IndexerState .STARTED ));
294
+ assertTrue (indexer .maybeTriggerAsyncJob (System .currentTimeMillis ()));
295
+ assertThat (indexer .getState (), equalTo (IndexerState .INDEXING ));
296
+ indexer .stop ();
297
+ countDownLatch .countDown ();
298
+
299
+ assertThat (indexer .getPosition (), equalTo (2 ));
300
+ assertTrue (awaitBusy (() -> isStopped .get ()));
301
+ assertFalse (isFinished .get ());
302
+ } finally {
303
+ executor .shutdownNow ();
304
+ }
305
+ }
251
306
}
0 commit comments