12
12
import org .apache .http .entity .StringEntity ;
13
13
import org .elasticsearch .Version ;
14
14
import org .elasticsearch .action .admin .cluster .repositories .put .PutRepositoryRequest ;
15
- import org .elasticsearch .action .admin .cluster .snapshots .get .GetSnapshotsRequest ;
16
15
import org .elasticsearch .action .admin .cluster .snapshots .restore .RestoreSnapshotRequest ;
17
- import org .elasticsearch .action .admin .cluster .snapshots .restore .RestoreSnapshotResponse ;
18
- import org .elasticsearch .action .admin .cluster .snapshots .status .SnapshotStatus ;
19
- import org .elasticsearch .action .admin .cluster .snapshots .status .SnapshotsStatusRequest ;
20
- import org .elasticsearch .action .admin .cluster .snapshots .status .SnapshotsStatusResponse ;
21
16
import org .elasticsearch .action .search .SearchRequest ;
22
17
import org .elasticsearch .action .search .SearchResponse ;
23
18
import org .elasticsearch .action .support .master .AcknowledgedResponse ;
27
22
import org .elasticsearch .client .RestClient ;
28
23
import org .elasticsearch .client .RestHighLevelClient ;
29
24
import org .elasticsearch .client .core .ShardsAcknowledgedResponse ;
30
- import org .elasticsearch .cluster .SnapshotsInProgress ;
31
25
import org .elasticsearch .cluster .routing .Murmur3HashFunction ;
32
26
import org .elasticsearch .common .Strings ;
33
27
import org .elasticsearch .common .document .DocumentField ;
34
28
import org .elasticsearch .common .settings .SecureString ;
35
29
import org .elasticsearch .common .settings .Settings ;
36
30
import org .elasticsearch .common .util .concurrent .ThreadContext ;
37
- import org .elasticsearch .common .util .set .Sets ;
38
31
import org .elasticsearch .core .Booleans ;
39
32
import org .elasticsearch .core .PathUtils ;
40
33
import org .elasticsearch .index .query .QueryBuilders ;
41
34
import org .elasticsearch .search .SearchHit ;
42
35
import org .elasticsearch .search .builder .SearchSourceBuilder ;
43
36
import org .elasticsearch .search .sort .SortBuilders ;
44
37
import org .elasticsearch .search .sort .SortOrder ;
45
- import org .elasticsearch .snapshots .SnapshotInfo ;
46
38
import org .elasticsearch .snapshots .SnapshotState ;
47
- import org .elasticsearch .test .hamcrest .ElasticsearchAssertions ;
48
39
import org .elasticsearch .test .rest .ESRestTestCase ;
49
40
import org .elasticsearch .test .rest .ObjectPath ;
50
41
import org .elasticsearch .xcontent .XContentBuilder ;
61
52
import java .util .Set ;
62
53
import java .util .stream .Collectors ;
63
54
55
+ import static org .hamcrest .Matchers .contains ;
64
56
import static org .hamcrest .Matchers .empty ;
57
+ import static org .hamcrest .Matchers .equalTo ;
65
58
import static org .hamcrest .Matchers .greaterThan ;
66
59
import static org .hamcrest .Matchers .hasKey ;
67
- import static org .hamcrest .Matchers .hasSize ;
68
60
import static org .hamcrest .Matchers .instanceOf ;
69
61
import static org .hamcrest .Matchers .not ;
70
62
import static org .hamcrest .Matchers .startsWith ;
@@ -204,58 +196,54 @@ private void beforeRestart(
204
196
if (sourceOnlyRepository ) {
205
197
repoSettingsBuilder .put ("delegate_type" , "fs" );
206
198
}
207
- ElasticsearchAssertions .assertAcked (
208
- client .snapshot ()
209
- .createRepository (
210
- new PutRepositoryRequest (repoName ).type (sourceOnlyRepository ? "source" : "fs" ).settings (repoSettingsBuilder ),
211
- RequestOptions .DEFAULT
212
- )
199
+ Request createRepo = new Request ("PUT" , "/_snapshot/" + repoName );
200
+ createRepo .setJsonEntity (
201
+ Strings .toString (new PutRepositoryRequest ().type (sourceOnlyRepository ? "source" : "fs" ).settings (repoSettingsBuilder .build ()))
213
202
);
203
+ assertAcknowledged (client ().performRequest (createRepo ));
214
204
215
205
// list snapshots on new ES
216
- List <SnapshotInfo > snapshotInfos = client .snapshot ()
217
- .get (new GetSnapshotsRequest (repoName ).snapshots (new String [] { "_all" }), RequestOptions .DEFAULT )
218
- .getSnapshots ();
219
- assertThat (snapshotInfos , hasSize (1 ));
220
- SnapshotInfo snapshotInfo = snapshotInfos .get (0 );
221
- assertEquals (snapshotName , snapshotInfo .snapshotId ().getName ());
222
- assertEquals (repoName , snapshotInfo .repository ());
223
- assertEquals (Arrays .asList (indexName ), snapshotInfo .indices ());
224
- assertEquals (SnapshotState .SUCCESS , snapshotInfo .state ());
225
- assertEquals (numberOfShards , snapshotInfo .successfulShards ());
226
- assertEquals (numberOfShards , snapshotInfo .totalShards ());
227
- assertEquals (0 , snapshotInfo .failedShards ());
228
- assertEquals (oldVersion , snapshotInfo .version ());
206
+ Request getSnaps = new Request ("GET" , "/_snapshot/" + repoName + "/_all" );
207
+ Response getResponse = client ().performRequest (getSnaps );
208
+ ObjectPath getResp = ObjectPath .createFromResponse (getResponse );
209
+ assertThat (getResp .evaluate ("total" ), equalTo (1 ));
210
+ assertThat (getResp .evaluate ("snapshots.0.snapshot" ), equalTo (snapshotName ));
211
+ assertThat (getResp .evaluate ("snapshots.0.repository" ), equalTo (repoName ));
212
+ assertThat (getResp .evaluate ("snapshots.0.indices" ), contains (indexName ));
213
+ assertThat (getResp .evaluate ("snapshots.0.state" ), equalTo (SnapshotState .SUCCESS .toString ()));
214
+ assertEquals (numberOfShards , (int ) getResp .evaluate ("snapshots.0.shards.successful" ));
215
+ assertEquals (numberOfShards , (int ) getResp .evaluate ("snapshots.0.shards.total" ));
216
+ assertEquals (0 , (int ) getResp .evaluate ("snapshots.0.shards.failed" ));
217
+ assertEquals (oldVersion .toString (), getResp .evaluate ("snapshots.0.version" ));
229
218
230
219
// list specific snapshot on new ES
231
- snapshotInfos = client .snapshot ()
232
- .get (new GetSnapshotsRequest (repoName ).snapshots (new String [] { snapshotName }), RequestOptions .DEFAULT )
233
- .getSnapshots ();
234
- assertThat (snapshotInfos , hasSize (1 ));
235
- snapshotInfo = snapshotInfos .get (0 );
236
- assertEquals (snapshotName , snapshotInfo .snapshotId ().getName ());
237
- assertEquals (repoName , snapshotInfo .repository ());
238
- assertEquals (Arrays .asList (indexName ), snapshotInfo .indices ());
239
- assertEquals (SnapshotState .SUCCESS , snapshotInfo .state ());
240
- assertEquals (numberOfShards , snapshotInfo .successfulShards ());
241
- assertEquals (numberOfShards , snapshotInfo .totalShards ());
242
- assertEquals (0 , snapshotInfo .failedShards ());
243
- assertEquals (oldVersion , snapshotInfo .version ());
220
+ getSnaps = new Request ("GET" , "/_snapshot/" + repoName + "/" + snapshotName );
221
+ getResponse = client ().performRequest (getSnaps );
222
+ getResp = ObjectPath .createFromResponse (getResponse );
223
+ assertThat (getResp .evaluate ("total" ), equalTo (1 ));
224
+ assertThat (getResp .evaluate ("snapshots.0.snapshot" ), equalTo (snapshotName ));
225
+ assertThat (getResp .evaluate ("snapshots.0.repository" ), equalTo (repoName ));
226
+ assertThat (getResp .evaluate ("snapshots.0.indices" ), contains (indexName ));
227
+ assertThat (getResp .evaluate ("snapshots.0.state" ), equalTo (SnapshotState .SUCCESS .toString ()));
228
+ assertEquals (numberOfShards , (int ) getResp .evaluate ("snapshots.0.shards.successful" ));
229
+ assertEquals (numberOfShards , (int ) getResp .evaluate ("snapshots.0.shards.total" ));
230
+ assertEquals (0 , (int ) getResp .evaluate ("snapshots.0.shards.failed" ));
231
+ assertEquals (oldVersion .toString (), getResp .evaluate ("snapshots.0.version" ));
244
232
245
233
// list advanced snapshot info on new ES
246
- SnapshotsStatusResponse snapshotsStatusResponse = client . snapshot ()
247
- . status ( new SnapshotsStatusRequest ( repoName ). snapshots ( new String [] { snapshotName }), RequestOptions . DEFAULT );
248
- assertThat ( snapshotsStatusResponse . getSnapshots (), hasSize ( 1 ) );
249
- SnapshotStatus snapshotStatus = snapshotsStatusResponse . getSnapshots (). get ( 0 );
250
- assertEquals ( snapshotName , snapshotStatus . getSnapshot (). getSnapshotId (). getName ( ));
251
- assertEquals ( repoName , snapshotStatus . getSnapshot (). getRepository ( ));
252
- assertEquals ( Sets . newHashSet ( indexName ), snapshotStatus . getIndices () .keySet ());
253
- assertEquals ( SnapshotsInProgress . State . SUCCESS , snapshotStatus . getState ( ));
254
- assertEquals (numberOfShards , snapshotStatus . getShardsStats (). getDoneShards ( ));
255
- assertEquals (numberOfShards , snapshotStatus . getShardsStats (). getTotalShards ( ));
256
- assertEquals (0 , snapshotStatus . getShardsStats (). getFailedShards ( ));
257
- assertThat (snapshotStatus . getStats (). getTotalSize ( ), greaterThan (0L ));
258
- assertThat (snapshotStatus . getStats (). getTotalFileCount ( ), greaterThan (0 ));
234
+ getSnaps = new Request ( "GET" , "/_snapshot/" + repoName + "/" + snapshotName + "/_status" );
235
+ getResponse = client (). performRequest ( getSnaps );
236
+ getResp = ObjectPath . createFromResponse ( getResponse );
237
+ assertThat ((( List <?>) getResp . evaluate ( "snapshots" )). size (), equalTo ( 1 ) );
238
+ assertThat ( getResp . evaluate ( "snapshots.0.snapshot" ), equalTo ( snapshotName ));
239
+ assertThat ( getResp . evaluate ( "snapshots.0.repository" ), equalTo ( repoName ));
240
+ assertThat ((( Map <?, ?>) getResp . evaluate ( "snapshots.0.indices" )) .keySet (), contains ( indexName ));
241
+ assertThat ( getResp . evaluate ( "snapshots.0.state" ), equalTo ( SnapshotState . SUCCESS . toString () ));
242
+ assertEquals (numberOfShards , ( int ) getResp . evaluate ( "snapshots.0.shards_stats.done" ));
243
+ assertEquals (numberOfShards , ( int ) getResp . evaluate ( "snapshots.0.shards_stats.total" ));
244
+ assertEquals (0 , ( int ) getResp . evaluate ( "snapshots.0.shards_stats.failed" ));
245
+ assertThat (getResp . evaluate ( "snapshots.0.stats.total.size_in_bytes" ), greaterThan (0 ));
246
+ assertThat (getResp . evaluate ( "snapshots.0.stats.total.file_count" ), greaterThan (0 ));
259
247
260
248
// restore / mount and check whether searches work
261
249
restoreMountAndVerify (
@@ -271,10 +259,9 @@ private void beforeRestart(
271
259
);
272
260
273
261
// close indices
274
- RestClient llClient = client .getLowLevelClient ();
275
- assertTrue (closeIndex (llClient , "restored_" + indexName ).isShardsAcknowledged ());
276
- assertTrue (closeIndex (llClient , "mounted_full_copy_" + indexName ).isShardsAcknowledged ());
277
- assertTrue (closeIndex (llClient , "mounted_shared_cache_" + indexName ).isShardsAcknowledged ());
262
+ assertTrue (closeIndex (client (), "restored_" + indexName ).isShardsAcknowledged ());
263
+ assertTrue (closeIndex (client (), "mounted_full_copy_" + indexName ).isShardsAcknowledged ());
264
+ assertTrue (closeIndex (client (), "mounted_shared_cache_" + indexName ).isShardsAcknowledged ());
278
265
279
266
// restore / mount again
280
267
restoreMountAndVerify (
@@ -311,23 +298,20 @@ private void restoreMountAndVerify(
311
298
String snapshotName
312
299
) throws IOException {
313
300
// restore index
314
- RestoreSnapshotResponse restoreSnapshotResponse = client .snapshot ()
315
- .restore (
316
- new RestoreSnapshotRequest (repoName , snapshotName ).indices (indexName )
317
- .renamePattern ("(.+)" )
318
- .renameReplacement ("restored_$1" )
319
- .waitForCompletion (true ),
320
- RequestOptions .DEFAULT
321
- );
322
- assertNotNull (restoreSnapshotResponse .getRestoreInfo ());
323
- assertEquals (numberOfShards , restoreSnapshotResponse .getRestoreInfo ().totalShards ());
324
- assertEquals (numberOfShards , restoreSnapshotResponse .getRestoreInfo ().successfulShards ());
301
+ Request restoreRequest = new Request ("POST" , "/_snapshot/" + repoName + "/" + snapshotName + "/_restore" );
302
+ restoreRequest .setJsonEntity (
303
+ Strings .toString (new RestoreSnapshotRequest ().indices (indexName ).renamePattern ("(.+)" ).renameReplacement ("restored_$1" ))
304
+ );
305
+ restoreRequest .addParameter ("wait_for_completion" , "true" );
306
+ Response restoreResponse = client ().performRequest (restoreRequest );
307
+ ObjectPath restore = ObjectPath .createFromResponse (restoreResponse );
308
+ assertEquals (numberOfShards , (int ) restore .evaluate ("snapshot.shards.total" ));
309
+ assertEquals (numberOfShards , (int ) restore .evaluate ("snapshot.shards.successful" ));
325
310
326
311
ensureGreen ("restored_" + indexName );
327
312
328
313
String restoredIndex = "restored_" + indexName ;
329
- RestClient llClient = client .getLowLevelClient ();
330
- var response = responseAsMap (llClient .performRequest (new Request ("GET" , "/" + restoredIndex + "/_mapping" )));
314
+ var response = responseAsMap (client ().performRequest (new Request ("GET" , "/" + restoredIndex + "/_mapping" )));
331
315
Map <?, ?> mapping = ObjectPath .evaluate (response , restoredIndex + ".mappings" );
332
316
logger .info ("mapping for {}: {}" , restoredIndex , mapping );
333
317
assertThat (mapping , hasKey ("_meta" ));
0 commit comments