11
11
import org .elasticsearch .Version ;
12
12
import org .elasticsearch .action .admin .cluster .repositories .put .PutRepositoryRequest ;
13
13
import org .elasticsearch .action .admin .cluster .snapshots .restore .RestoreSnapshotRequest ;
14
- import org .elasticsearch .action .search .SearchRequest ;
15
14
import org .elasticsearch .action .search .SearchResponse ;
16
15
import org .elasticsearch .client .Request ;
17
16
import org .elasticsearch .client .RequestOptions ;
18
17
import org .elasticsearch .client .Response ;
19
18
import org .elasticsearch .client .ResponseException ;
20
19
import org .elasticsearch .client .RestClient ;
21
- import org .elasticsearch .client .RestHighLevelClient ;
22
- import org .elasticsearch .client .core .ShardsAcknowledgedResponse ;
23
20
import org .elasticsearch .cluster .routing .Murmur3HashFunction ;
24
21
import org .elasticsearch .common .Strings ;
25
22
import org .elasticsearch .common .document .DocumentField ;
26
23
import org .elasticsearch .common .settings .SecureString ;
27
24
import org .elasticsearch .common .settings .Settings ;
28
25
import org .elasticsearch .common .util .concurrent .ThreadContext ;
29
26
import org .elasticsearch .core .Booleans ;
27
+ import org .elasticsearch .core .Nullable ;
30
28
import org .elasticsearch .core .PathUtils ;
31
29
import org .elasticsearch .index .IndexVersion ;
32
30
import org .elasticsearch .index .query .QueryBuilders ;
43
41
44
42
import java .io .IOException ;
45
43
import java .util .Arrays ;
46
- import java .util .Collections ;
47
44
import java .util .Comparator ;
48
45
import java .util .HashSet ;
49
46
import java .util .List ;
@@ -75,12 +72,6 @@ protected Settings restClientSettings() {
75
72
return Settings .builder ().put (ThreadContext .PREFIX + ".Authorization" , token ).build ();
76
73
}
77
74
78
- @ SuppressWarnings ("removal" )
79
- protected static RestHighLevelClient highLevelClient (RestClient client ) {
80
- return new RestHighLevelClient (client , ignore -> {}, Collections .emptyList ()) {
81
- };
82
- }
83
-
84
75
public void testOldRepoAccess () throws IOException {
85
76
runTest (false );
86
77
}
@@ -113,10 +104,7 @@ public void runTest(boolean sourceOnlyRepository) throws IOException {
113
104
int numDocs = 10 ;
114
105
int extraDocs = 1 ;
115
106
final Set <String > expectedIds = new HashSet <>();
116
- try (
117
- RestHighLevelClient client = highLevelClient (adminClient ());
118
- RestClient oldEs = RestClient .builder (new HttpHost ("127.0.0.1" , oldEsPort )).build ()
119
- ) {
107
+ try (RestClient oldEs = RestClient .builder (new HttpHost ("127.0.0.1" , oldEsPort )).build ()) {
120
108
if (afterRestart == false ) {
121
109
beforeRestart (
122
110
sourceOnlyRepository ,
@@ -126,7 +114,6 @@ public void runTest(boolean sourceOnlyRepository) throws IOException {
126
114
numDocs ,
127
115
extraDocs ,
128
116
expectedIds ,
129
- client ,
130
117
oldEs ,
131
118
indexName
132
119
);
@@ -151,7 +138,6 @@ private void beforeRestart(
151
138
int numDocs ,
152
139
int extraDocs ,
153
140
Set <String > expectedIds ,
154
- RestHighLevelClient client ,
155
141
RestClient oldEs ,
156
142
String indexName
157
143
) throws IOException {
@@ -262,35 +248,15 @@ private void beforeRestart(
262
248
assertThat (getResp .evaluate ("snapshots.0.stats.total.file_count" ), greaterThan (0 ));
263
249
264
250
// restore / mount and check whether searches work
265
- restoreMountAndVerify (
266
- numDocs ,
267
- expectedIds ,
268
- client ,
269
- numberOfShards ,
270
- sourceOnlyRepository ,
271
- oldVersion ,
272
- indexName ,
273
- repoName ,
274
- snapshotName
275
- );
251
+ restoreMountAndVerify (numDocs , expectedIds , numberOfShards , sourceOnlyRepository , oldVersion , indexName , repoName , snapshotName );
276
252
277
253
// close indices
278
- assertTrue ( closeIndex (client (), "restored_" + indexName ). isShardsAcknowledged () );
279
- assertTrue ( closeIndex (client (), "mounted_full_copy_" + indexName ). isShardsAcknowledged () );
280
- assertTrue ( closeIndex (client (), "mounted_shared_cache_" + indexName ). isShardsAcknowledged () );
254
+ closeIndex (client (), "restored_" + indexName );
255
+ closeIndex (client (), "mounted_full_copy_" + indexName );
256
+ closeIndex (client (), "mounted_shared_cache_" + indexName );
281
257
282
258
// restore / mount again
283
- restoreMountAndVerify (
284
- numDocs ,
285
- expectedIds ,
286
- client ,
287
- numberOfShards ,
288
- sourceOnlyRepository ,
289
- oldVersion ,
290
- indexName ,
291
- repoName ,
292
- snapshotName
293
- );
259
+ restoreMountAndVerify (numDocs , expectedIds , numberOfShards , sourceOnlyRepository , oldVersion , indexName , repoName , snapshotName );
294
260
}
295
261
296
262
private String getType (Version oldVersion , String id ) {
@@ -305,7 +271,6 @@ private static String sourceForDoc(int i) {
305
271
private void restoreMountAndVerify (
306
272
int numDocs ,
307
273
Set <String > expectedIds ,
308
- RestHighLevelClient client ,
309
274
int numberOfShards ,
310
275
boolean sourceOnlyRepository ,
311
276
Version oldVersion ,
@@ -358,7 +323,7 @@ private void restoreMountAndVerify(
358
323
}
359
324
360
325
// run a search against the index
361
- assertDocs ("restored_" + indexName , numDocs , expectedIds , client , sourceOnlyRepository , oldVersion , numberOfShards );
326
+ assertDocs ("restored_" + indexName , numDocs , expectedIds , sourceOnlyRepository , oldVersion , numberOfShards );
362
327
363
328
// mount as full copy searchable snapshot
364
329
Request mountRequest = new Request ("POST" , "/_snapshot/" + repoName + "/" + snapshotName + "/_mount" );
@@ -378,7 +343,7 @@ private void restoreMountAndVerify(
378
343
ensureGreen ("mounted_full_copy_" + indexName );
379
344
380
345
// run a search against the index
381
- assertDocs ("mounted_full_copy_" + indexName , numDocs , expectedIds , client , sourceOnlyRepository , oldVersion , numberOfShards );
346
+ assertDocs ("mounted_full_copy_" + indexName , numDocs , expectedIds , sourceOnlyRepository , oldVersion , numberOfShards );
382
347
383
348
// mount as shared cache searchable snapshot
384
349
mountRequest = new Request ("POST" , "/_snapshot/" + repoName + "/" + snapshotName + "/_mount" );
@@ -391,15 +356,14 @@ private void restoreMountAndVerify(
391
356
assertEquals (numberOfShards , (int ) mountResponse .evaluate ("snapshot.shards.successful" ));
392
357
393
358
// run a search against the index
394
- assertDocs ("mounted_shared_cache_" + indexName , numDocs , expectedIds , client , sourceOnlyRepository , oldVersion , numberOfShards );
359
+ assertDocs ("mounted_shared_cache_" + indexName , numDocs , expectedIds , sourceOnlyRepository , oldVersion , numberOfShards );
395
360
}
396
361
397
362
@ SuppressWarnings ("removal" )
398
363
private void assertDocs (
399
364
String index ,
400
365
int numDocs ,
401
366
Set <String > expectedIds ,
402
- RestHighLevelClient client ,
403
367
boolean sourceOnlyRepository ,
404
368
Version oldVersion ,
405
369
int numberOfShards
@@ -410,8 +374,10 @@ private void assertDocs(
410
374
.build ();
411
375
RequestOptions randomRequestOptions = randomBoolean () ? RequestOptions .DEFAULT : v7RequestOptions ;
412
376
377
+ SearchResponse searchResponse ;
378
+
413
379
// run a search against the index
414
- SearchResponse searchResponse = client . search (new SearchRequest ( index ) , randomRequestOptions );
380
+ searchResponse = search (index , null , randomRequestOptions );
415
381
logger .info (searchResponse );
416
382
// check hit count
417
383
assertEquals (numDocs , searchResponse .getHits ().getTotalHits ().value );
@@ -429,12 +395,11 @@ private void assertDocs(
429
395
String id = randomFrom (expectedIds );
430
396
int num = getIdAsNumeric (id );
431
397
// run a search using runtime fields against the index
432
- searchResponse = client .search (
433
- new SearchRequest (index ).source (
434
- SearchSourceBuilder .searchSource ()
435
- .query (QueryBuilders .matchQuery ("val" , num ))
436
- .runtimeMappings (Map .of ("val" , Map .of ("type" , "long" )))
437
- ),
398
+ searchResponse = search (
399
+ index ,
400
+ SearchSourceBuilder .searchSource ()
401
+ .query (QueryBuilders .matchQuery ("val" , num ))
402
+ .runtimeMappings (Map .of ("val" , Map .of ("type" , "long" ))),
438
403
randomRequestOptions
439
404
);
440
405
logger .info (searchResponse );
@@ -444,24 +409,24 @@ private void assertDocs(
444
409
445
410
if (sourceOnlyRepository == false ) {
446
411
// search using reverse sort on val
447
- searchResponse = client .search (
448
- new SearchRequest (index ).source (
449
- SearchSourceBuilder .searchSource ()
450
- .query (QueryBuilders .matchAllQuery ())
451
- .sort (SortBuilders .fieldSort ("val" ).order (SortOrder .DESC ))
452
- ),
412
+ searchResponse = search (
413
+ index ,
414
+ SearchSourceBuilder .searchSource ()
415
+ .query (QueryBuilders .matchAllQuery ())
416
+ .sort (SortBuilders .fieldSort ("val" ).order (SortOrder .DESC )),
453
417
randomRequestOptions
454
418
);
455
419
logger .info (searchResponse );
456
420
// check sort order
457
421
assertEquals (
458
- expectedIds .stream ().sorted (Comparator .comparingInt (this ::getIdAsNumeric ).reversed ()).collect ( Collectors . toList () ),
459
- Arrays .stream (searchResponse .getHits ().getHits ()).map (SearchHit ::getId ).collect ( Collectors . toList () )
422
+ expectedIds .stream ().sorted (Comparator .comparingInt (this ::getIdAsNumeric ).reversed ()).toList (),
423
+ Arrays .stream (searchResponse .getHits ().getHits ()).map (SearchHit ::getId ).toList ()
460
424
);
461
425
462
426
// look up postings
463
- searchResponse = client .search (
464
- new SearchRequest (index ).source (SearchSourceBuilder .searchSource ().query (QueryBuilders .matchQuery ("test" , "test" + num ))),
427
+ searchResponse = search (
428
+ index ,
429
+ SearchSourceBuilder .searchSource ().query (QueryBuilders .matchQuery ("test" , "test" + num )),
465
430
randomRequestOptions
466
431
);
467
432
logger .info (searchResponse );
@@ -472,8 +437,9 @@ private void assertDocs(
472
437
// search on _type and check that results contain _type information
473
438
String randomType = getType (oldVersion , randomFrom (expectedIds ));
474
439
long typeCount = expectedIds .stream ().filter (idd -> getType (oldVersion , idd ).equals (randomType )).count ();
475
- searchResponse = client .search (
476
- new SearchRequest (index ).source (SearchSourceBuilder .searchSource ().query (QueryBuilders .termQuery ("_type" , randomType ))),
440
+ searchResponse = search (
441
+ index ,
442
+ SearchSourceBuilder .searchSource ().query (QueryBuilders .termQuery ("_type" , randomType )),
477
443
randomRequestOptions
478
444
);
479
445
logger .info (searchResponse );
@@ -493,10 +459,9 @@ private void assertDocs(
493
459
);
494
460
495
461
// check that shards are skipped based on non-matching date
496
- searchResponse = client .search (
497
- new SearchRequest (index ).source (
498
- SearchSourceBuilder .searchSource ().query (QueryBuilders .rangeQuery ("create_date" ).from ("2020-02-01" ))
499
- ),
462
+ searchResponse = search (
463
+ index ,
464
+ SearchSourceBuilder .searchSource ().query (QueryBuilders .rangeQuery ("create_date" ).from ("2020-02-01" )),
500
465
randomRequestOptions
501
466
);
502
467
logger .info (searchResponse );
@@ -507,13 +472,22 @@ private void assertDocs(
507
472
}
508
473
}
509
474
475
+ private static SearchResponse search (String index , @ Nullable SearchSourceBuilder builder , RequestOptions options ) throws IOException {
476
+ Request request = new Request ("POST" , "/" + index + "/_search" );
477
+ if (builder != null ) {
478
+ request .setJsonEntity (builder .toString ());
479
+ }
480
+ request .setOptions (options );
481
+ return SearchResponse .fromXContent (responseAsParser (client ().performRequest (request )));
482
+ }
483
+
510
484
private int getIdAsNumeric (String id ) {
511
485
return Integer .parseInt (id .substring ("testdoc" .length ()));
512
486
}
513
487
514
- static ShardsAcknowledgedResponse closeIndex (RestClient client , String index ) throws IOException {
488
+ private static void closeIndex (RestClient client , String index ) throws IOException {
515
489
Request request = new Request ("POST" , "/" + index + "/_close" );
516
- Response response = client .performRequest (request );
517
- return ShardsAcknowledgedResponse . fromXContent ( responseAsParser ( response ));
490
+ ObjectPath doc = ObjectPath . createFromResponse ( client .performRequest (request ) );
491
+ assertTrue ( doc . evaluate ( "shards_acknowledged" ));
518
492
}
519
493
}
0 commit comments