forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPitMultiNodeTests.java
483 lines (452 loc) · 24.7 KB
/
PitMultiNodeTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.search;
import com.carrotsearch.hppc.cursors.ObjectCursor;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.opensearch.action.ActionFuture;
import org.opensearch.action.ActionListener;
import org.opensearch.action.LatchedActionListener;
import org.opensearch.action.admin.cluster.state.ClusterStateRequest;
import org.opensearch.action.admin.cluster.state.ClusterStateResponse;
import org.opensearch.action.search.CreatePitAction;
import org.opensearch.action.search.CreatePitRequest;
import org.opensearch.action.search.CreatePitResponse;
import org.opensearch.action.search.DeletePitAction;
import org.opensearch.action.search.DeletePitRequest;
import org.opensearch.action.search.DeletePitResponse;
import org.opensearch.action.search.GetAllPitNodesRequest;
import org.opensearch.action.search.GetAllPitNodesResponse;
import org.opensearch.action.search.GetAllPitsAction;
import org.opensearch.action.search.PitTestsUtil;
import org.opensearch.action.search.SearchResponse;
import org.opensearch.client.Requests;
import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.search.builder.PointInTimeBuilder;
import org.opensearch.test.InternalTestCluster;
import org.opensearch.test.OpenSearchIntegTestCase;
import org.opensearch.threadpool.TestThreadPool;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.action.admin.indices.flush.FlushRequest;
import org.opensearch.action.admin.indices.stats.IndicesStatsRequest;
import org.opensearch.action.admin.indices.stats.IndicesStatsResponse;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import static org.hamcrest.Matchers.containsString;
import static org.opensearch.action.search.PitTestsUtil.assertSegments;
import static org.opensearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE;
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked;
/**
* Multi node integration tests for PIT creation and search operation with PIT ID.
*/
@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.SUITE, numDataNodes = 2)
public class PitMultiNodeTests extends OpenSearchIntegTestCase {
@Before
public void setupIndex() throws ExecutionException, InterruptedException {
createIndex("index", Settings.builder().put("index.number_of_shards", 2).put("index.number_of_replicas", 0).build());
client().prepareIndex("index").setId("1").setSource("field", "value").setRefreshPolicy(IMMEDIATE).execute().get();
ensureGreen();
}
@After
public void clearIndex() {
client().admin().indices().prepareDelete("index").get();
}
public void testPit() throws Exception {
CreatePitRequest request = new CreatePitRequest(TimeValue.timeValueDays(1), true);
request.setIndices(new String[] { "index" });
ActionFuture<CreatePitResponse> execute = client().execute(CreatePitAction.INSTANCE, request);
CreatePitResponse pitResponse = execute.get();
SearchResponse searchResponse = client().prepareSearch("index")
.setSize(2)
.setPointInTime(new PointInTimeBuilder(pitResponse.getId()).setKeepAlive(TimeValue.timeValueDays(1)))
.get();
assertEquals(2, searchResponse.getSuccessfulShards());
assertEquals(2, searchResponse.getTotalShards());
validatePitStats("index", 2, 2);
PitTestsUtil.assertUsingGetAllPits(client(), pitResponse.getId(), pitResponse.getCreationTime());
assertSegments(false, client(), pitResponse.getId());
}
public void testCreatePitWhileNodeDropWithAllowPartialCreationFalse() throws Exception {
CreatePitRequest request = new CreatePitRequest(TimeValue.timeValueDays(1), false);
request.setIndices(new String[] { "index" });
internalCluster().restartRandomDataNode(new InternalTestCluster.RestartCallback() {
@Override
public Settings onNodeStopped(String nodeName) throws Exception {
ActionFuture<CreatePitResponse> execute = client().execute(CreatePitAction.INSTANCE, request);
ExecutionException ex = expectThrows(ExecutionException.class, execute::get);
assertTrue(ex.getMessage().contains("Failed to execute phase [create_pit]"));
validatePitStats("index", 0, 0);
return super.onNodeStopped(nodeName);
}
});
}
public void testCreatePitWhileNodeDropWithAllowPartialCreationTrue() throws Exception {
CreatePitRequest request = new CreatePitRequest(TimeValue.timeValueDays(1), true);
request.setIndices(new String[] { "index" });
internalCluster().restartRandomDataNode(new InternalTestCluster.RestartCallback() {
@Override
public Settings onNodeStopped(String nodeName) throws Exception {
ActionFuture<CreatePitResponse> execute = client().execute(CreatePitAction.INSTANCE, request);
CreatePitResponse pitResponse = execute.get();
PitTestsUtil.assertUsingGetAllPits(client(), pitResponse.getId(), pitResponse.getCreationTime());
assertSegments(false, "index", 1, client(), pitResponse.getId());
assertEquals(1, pitResponse.getSuccessfulShards());
assertEquals(2, pitResponse.getTotalShards());
SearchResponse searchResponse = client().prepareSearch("index")
.setSize(2)
.setPointInTime(new PointInTimeBuilder(pitResponse.getId()).setKeepAlive(TimeValue.timeValueDays(1)))
.get();
assertEquals(1, searchResponse.getSuccessfulShards());
assertEquals(1, searchResponse.getTotalShards());
validatePitStats("index", 1, 1);
return super.onNodeStopped(nodeName);
}
});
}
public void testPitSearchWithNodeDrop() throws Exception {
CreatePitRequest request = new CreatePitRequest(TimeValue.timeValueDays(1), true);
request.setIndices(new String[] { "index" });
ActionFuture<CreatePitResponse> execute = client().execute(CreatePitAction.INSTANCE, request);
CreatePitResponse pitResponse = execute.get();
internalCluster().restartRandomDataNode(new InternalTestCluster.RestartCallback() {
@Override
public Settings onNodeStopped(String nodeName) throws Exception {
SearchResponse searchResponse = client().prepareSearch()
.setSize(2)
.setPointInTime(new PointInTimeBuilder(pitResponse.getId()).setKeepAlive(TimeValue.timeValueDays(1)))
.get();
assertEquals(1, searchResponse.getSuccessfulShards());
assertEquals(1, searchResponse.getFailedShards());
assertEquals(0, searchResponse.getSkippedShards());
assertEquals(2, searchResponse.getTotalShards());
validatePitStats("index", 1, 1);
PitTestsUtil.assertUsingGetAllPits(client(), pitResponse.getId(), pitResponse.getCreationTime());
return super.onNodeStopped(nodeName);
}
});
}
public void testPitSearchWithNodeDropWithPartialSearchResultsFalse() throws Exception {
CreatePitRequest request = new CreatePitRequest(TimeValue.timeValueDays(1), true);
request.setIndices(new String[] { "index" });
ActionFuture<CreatePitResponse> execute = client().execute(CreatePitAction.INSTANCE, request);
CreatePitResponse pitResponse = execute.get();
internalCluster().restartRandomDataNode(new InternalTestCluster.RestartCallback() {
@Override
public Settings onNodeStopped(String nodeName) throws Exception {
ActionFuture<SearchResponse> execute = client().prepareSearch()
.setSize(2)
.setPointInTime(new PointInTimeBuilder(pitResponse.getId()).setKeepAlive(TimeValue.timeValueDays(1)))
.setAllowPartialSearchResults(false)
.execute();
ExecutionException ex = expectThrows(ExecutionException.class, execute::get);
assertTrue(ex.getMessage().contains("Partial shards failure"));
return super.onNodeStopped(nodeName);
}
});
}
public void testPitInvalidDefaultKeepAlive() {
IllegalArgumentException exc = expectThrows(
IllegalArgumentException.class,
() -> client().admin()
.cluster()
.prepareUpdateSettings()
.setPersistentSettings(Settings.builder().put("point_in_time.max_keep_alive", "1m").put("search.default_keep_alive", "2m"))
.get()
);
assertThat(exc.getMessage(), containsString("was (2m > 1m)"));
assertAcked(
client().admin()
.cluster()
.prepareUpdateSettings()
.setPersistentSettings(Settings.builder().put("search.default_keep_alive", "5m").put("point_in_time.max_keep_alive", "5m"))
.get()
);
assertAcked(
client().admin()
.cluster()
.prepareUpdateSettings()
.setPersistentSettings(Settings.builder().put("search.default_keep_alive", "2m"))
.get()
);
assertAcked(
client().admin()
.cluster()
.prepareUpdateSettings()
.setPersistentSettings(Settings.builder().put("point_in_time.max_keep_alive", "2m"))
.get()
);
exc = expectThrows(
IllegalArgumentException.class,
() -> client().admin()
.cluster()
.prepareUpdateSettings()
.setPersistentSettings(Settings.builder().put("search.default_keep_alive", "3m"))
.get()
);
assertThat(exc.getMessage(), containsString("was (3m > 2m)"));
assertAcked(
client().admin()
.cluster()
.prepareUpdateSettings()
.setPersistentSettings(Settings.builder().put("search.default_keep_alive", "1m"))
.get()
);
exc = expectThrows(
IllegalArgumentException.class,
() -> client().admin()
.cluster()
.prepareUpdateSettings()
.setPersistentSettings(Settings.builder().put("point_in_time.max_keep_alive", "30s"))
.get()
);
assertThat(exc.getMessage(), containsString("was (1m > 30s)"));
assertAcked(
client().admin()
.cluster()
.prepareUpdateSettings()
.setPersistentSettings(Settings.builder().putNull("*"))
.setTransientSettings(Settings.builder().putNull("*"))
);
}
public void testConcurrentCreates() throws InterruptedException {
CreatePitRequest createPitRequest = new CreatePitRequest(TimeValue.timeValueDays(1), true);
createPitRequest.setIndices(new String[] { "index" });
int concurrentRuns = randomIntBetween(20, 50);
AtomicInteger numSuccess = new AtomicInteger();
TestThreadPool testThreadPool = null;
try {
testThreadPool = new TestThreadPool(DeletePitMultiNodeTests.class.getName());
List<Runnable> operationThreads = new ArrayList<>();
CountDownLatch countDownLatch = new CountDownLatch(concurrentRuns);
Set<String> createSet = new HashSet<>();
for (int i = 0; i < concurrentRuns; i++) {
Runnable thread = () -> {
logger.info("Triggering pit create --->");
LatchedActionListener listener = new LatchedActionListener<>(new ActionListener<CreatePitResponse>() {
@Override
public void onResponse(CreatePitResponse createPitResponse) {
if (createSet.add(createPitResponse.getId())) {
numSuccess.incrementAndGet();
}
}
@Override
public void onFailure(Exception e) {}
}, countDownLatch);
client().execute(CreatePitAction.INSTANCE, createPitRequest, listener);
};
operationThreads.add(thread);
}
TestThreadPool finalTestThreadPool = testThreadPool;
operationThreads.forEach(runnable -> finalTestThreadPool.executor("generic").execute(runnable));
countDownLatch.await();
assertEquals(concurrentRuns, numSuccess.get());
} finally {
ThreadPool.terminate(testThreadPool, 500, TimeUnit.MILLISECONDS);
}
}
public void testConcurrentCreatesWithDeletes() throws InterruptedException, ExecutionException {
CreatePitRequest createPitRequest = new CreatePitRequest(TimeValue.timeValueDays(1), true);
createPitRequest.setIndices(new String[] { "index" });
List<String> pitIds = new ArrayList<>();
String id = client().execute(CreatePitAction.INSTANCE, createPitRequest).get().getId();
pitIds.add(id);
DeletePitRequest deletePITRequest = new DeletePitRequest(pitIds);
Set<String> createSet = new HashSet<>();
AtomicInteger numSuccess = new AtomicInteger();
TestThreadPool testThreadPool = null;
try {
testThreadPool = new TestThreadPool(PitMultiNodeTests.class.getName());
int concurrentRuns = randomIntBetween(20, 50);
List<Runnable> operationThreads = new ArrayList<>();
CountDownLatch countDownLatch = new CountDownLatch(concurrentRuns);
long randomDeleteThread = randomLongBetween(0, concurrentRuns - 1);
for (int i = 0; i < concurrentRuns; i++) {
int currentThreadIteration = i;
Runnable thread = () -> {
if (currentThreadIteration == randomDeleteThread) {
LatchedActionListener listener = new LatchedActionListener<>(new ActionListener<CreatePitResponse>() {
@Override
public void onResponse(CreatePitResponse createPitResponse) {
if (createSet.add(createPitResponse.getId())) {
numSuccess.incrementAndGet();
}
}
@Override
public void onFailure(Exception e) {}
}, countDownLatch);
client().execute(CreatePitAction.INSTANCE, createPitRequest, listener);
} else {
LatchedActionListener listener = new LatchedActionListener<>(new ActionListener<DeletePitResponse>() {
@Override
public void onResponse(DeletePitResponse deletePitResponse) {
if (deletePitResponse.getDeletePitResults().get(0).isSuccessful()) {
numSuccess.incrementAndGet();
}
}
@Override
public void onFailure(Exception e) {}
}, countDownLatch);
client().execute(DeletePitAction.INSTANCE, deletePITRequest, listener);
}
};
operationThreads.add(thread);
}
TestThreadPool finalTestThreadPool = testThreadPool;
operationThreads.forEach(runnable -> finalTestThreadPool.executor("generic").execute(runnable));
countDownLatch.await();
assertEquals(concurrentRuns, numSuccess.get());
} finally {
ThreadPool.terminate(testThreadPool, 500, TimeUnit.MILLISECONDS);
}
}
public void validatePitStats(String index, long expectedPitCurrent, long expectedOpenContexts) throws ExecutionException,
InterruptedException {
// Clear the index transaction log
FlushRequest flushRequest = Requests.flushRequest(index);
client().admin().indices().flush(flushRequest).get();
// Test stats
IndicesStatsRequest indicesStatsRequest = new IndicesStatsRequest();
indicesStatsRequest.indices(index);
indicesStatsRequest.all();
IndicesStatsResponse indicesStatsResponse = client().admin().indices().stats(indicesStatsRequest).get();
long pitCurrent = indicesStatsResponse.getIndex(index).getTotal().search.getTotal().getPitCurrent();
long openContexts = indicesStatsResponse.getIndex(index).getTotal().search.getOpenContexts();
assertEquals(expectedPitCurrent, pitCurrent);
assertEquals(expectedOpenContexts, openContexts);
}
public void testGetAllPits() throws Exception {
client().admin().indices().prepareCreate("index1").get();
CreatePitRequest request = new CreatePitRequest(TimeValue.timeValueDays(1), true);
request.setIndices(new String[] { "index", "index1" });
ActionFuture<CreatePitResponse> execute = client().execute(CreatePitAction.INSTANCE, request);
CreatePitResponse pitResponse = execute.get();
CreatePitResponse pitResponse1 = client().execute(CreatePitAction.INSTANCE, request).get();
CreatePitResponse pitResponse2 = client().execute(CreatePitAction.INSTANCE, request).get();
final ClusterStateRequest clusterStateRequest = new ClusterStateRequest();
clusterStateRequest.local(false);
clusterStateRequest.clear().nodes(true).routingTable(true).indices("*");
ClusterStateResponse clusterStateResponse = client().admin().cluster().state(clusterStateRequest).get();
final List<DiscoveryNode> nodes = new LinkedList<>();
for (ObjectCursor<DiscoveryNode> cursor : clusterStateResponse.getState().nodes().getDataNodes().values()) {
DiscoveryNode node = cursor.value;
nodes.add(node);
}
DiscoveryNode[] disNodesArr = new DiscoveryNode[nodes.size()];
nodes.toArray(disNodesArr);
GetAllPitNodesRequest getAllPITNodesRequest = new GetAllPitNodesRequest(disNodesArr);
ActionFuture<GetAllPitNodesResponse> execute1 = client().execute(GetAllPitsAction.INSTANCE, getAllPITNodesRequest);
GetAllPitNodesResponse getPitResponse = execute1.get();
assertEquals(3, getPitResponse.getPitInfos().size());
List<String> resultPitIds = getPitResponse.getPitInfos().stream().map(p -> p.getPitId()).collect(Collectors.toList());
// asserting that we get all unique PIT IDs
Assert.assertTrue(resultPitIds.contains(pitResponse.getId()));
Assert.assertTrue(resultPitIds.contains(pitResponse1.getId()));
Assert.assertTrue(resultPitIds.contains(pitResponse2.getId()));
client().admin().indices().prepareDelete("index1").get();
}
public void testGetAllPitsDuringNodeDrop() throws Exception {
CreatePitRequest request = new CreatePitRequest(TimeValue.timeValueDays(1), true);
request.setIndices(new String[] { "index" });
ActionFuture<CreatePitResponse> execute = client().execute(CreatePitAction.INSTANCE, request);
CreatePitResponse pitResponse = execute.get();
GetAllPitNodesRequest getAllPITNodesRequest = new GetAllPitNodesRequest(getDiscoveryNodes());
internalCluster().restartRandomDataNode(new InternalTestCluster.RestartCallback() {
@Override
public Settings onNodeStopped(String nodeName) throws Exception {
ActionFuture<GetAllPitNodesResponse> execute1 = client().execute(GetAllPitsAction.INSTANCE, getAllPITNodesRequest);
GetAllPitNodesResponse getPitResponse = execute1.get();
// we still get a pit id from the data node which is up
assertEquals(1, getPitResponse.getPitInfos().size());
// failure for node drop
assertEquals(1, getPitResponse.failures().size());
assertTrue(getPitResponse.failures().get(0).getMessage().contains("Failed node"));
return super.onNodeStopped(nodeName);
}
});
}
private DiscoveryNode[] getDiscoveryNodes() throws ExecutionException, InterruptedException {
final ClusterStateRequest clusterStateRequest = new ClusterStateRequest();
clusterStateRequest.local(false);
clusterStateRequest.clear().nodes(true).routingTable(true).indices("*");
ClusterStateResponse clusterStateResponse = client().admin().cluster().state(clusterStateRequest).get();
final List<DiscoveryNode> nodes = new LinkedList<>();
for (ObjectCursor<DiscoveryNode> cursor : clusterStateResponse.getState().nodes().getDataNodes().values()) {
DiscoveryNode node = cursor.value;
nodes.add(node);
}
DiscoveryNode[] disNodesArr = new DiscoveryNode[nodes.size()];
nodes.toArray(disNodesArr);
return disNodesArr;
}
public void testConcurrentGetWithDeletes() throws InterruptedException, ExecutionException {
CreatePitRequest createPitRequest = new CreatePitRequest(TimeValue.timeValueDays(1), true);
createPitRequest.setIndices(new String[] { "index" });
List<String> pitIds = new ArrayList<>();
String id = client().execute(CreatePitAction.INSTANCE, createPitRequest).get().getId();
pitIds.add(id);
DeletePitRequest deletePITRequest = new DeletePitRequest(pitIds);
GetAllPitNodesRequest getAllPITNodesRequest = new GetAllPitNodesRequest(getDiscoveryNodes());
AtomicInteger numSuccess = new AtomicInteger();
TestThreadPool testThreadPool = null;
try {
testThreadPool = new TestThreadPool(PitMultiNodeTests.class.getName());
int concurrentRuns = randomIntBetween(20, 50);
List<Runnable> operationThreads = new ArrayList<>();
CountDownLatch countDownLatch = new CountDownLatch(concurrentRuns);
long randomDeleteThread = randomLongBetween(0, concurrentRuns - 1);
for (int i = 0; i < concurrentRuns; i++) {
int currentThreadIteration = i;
Runnable thread = () -> {
if (currentThreadIteration == randomDeleteThread) {
LatchedActionListener listener = new LatchedActionListener<>(new ActionListener<GetAllPitNodesResponse>() {
@Override
public void onResponse(GetAllPitNodesResponse getAllPitNodesResponse) {
if (getAllPitNodesResponse.failures().isEmpty()) {
numSuccess.incrementAndGet();
}
}
@Override
public void onFailure(Exception e) {}
}, countDownLatch);
client().execute(GetAllPitsAction.INSTANCE, getAllPITNodesRequest, listener);
} else {
LatchedActionListener listener = new LatchedActionListener<>(new ActionListener<DeletePitResponse>() {
@Override
public void onResponse(DeletePitResponse deletePitResponse) {
if (deletePitResponse.getDeletePitResults().get(0).isSuccessful()) {
numSuccess.incrementAndGet();
}
}
@Override
public void onFailure(Exception e) {}
}, countDownLatch);
client().execute(DeletePitAction.INSTANCE, deletePITRequest, listener);
}
};
operationThreads.add(thread);
}
TestThreadPool finalTestThreadPool = testThreadPool;
operationThreads.forEach(runnable -> finalTestThreadPool.executor("generic").execute(runnable));
countDownLatch.await();
assertEquals(concurrentRuns, numSuccess.get());
} finally {
ThreadPool.terminate(testThreadPool, 500, TimeUnit.MILLISECONDS);
}
}
}