28
28
import org .elasticsearch .action .support .master .AcknowledgedResponse ;
29
29
import org .elasticsearch .cluster .ClusterState ;
30
30
import org .elasticsearch .cluster .ClusterStateObserver ;
31
+ import org .elasticsearch .cluster .ClusterStateUpdateTask ;
31
32
import org .elasticsearch .cluster .SnapshotDeletionsInProgress ;
32
33
import org .elasticsearch .cluster .SnapshotsInProgress ;
33
34
import org .elasticsearch .cluster .metadata .IndexMetadata ;
82
83
83
84
import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertAcked ;
84
85
import static org .hamcrest .Matchers .empty ;
85
- import static org .hamcrest .Matchers .equalTo ;
86
+ import static org .hamcrest .Matchers .hasSize ;
86
87
import static org .hamcrest .Matchers .is ;
87
88
88
89
public abstract class AbstractSnapshotIntegTestCase extends ESIntegTestCase {
@@ -122,11 +123,11 @@ public void verifyNoLeakedListeners() throws Exception {
122
123
@ After
123
124
public void assertRepoConsistency () {
124
125
if (skipRepoConsistencyCheckReason == null ) {
125
- client (). admin (). cluster ().prepareGetRepositories ().get ().repositories ().forEach (repositoryMetadata -> {
126
+ clusterAdmin ().prepareGetRepositories ().get ().repositories ().forEach (repositoryMetadata -> {
126
127
final String name = repositoryMetadata .name ();
127
128
if (repositoryMetadata .settings ().getAsBoolean ("readonly" , false ) == false ) {
128
- client (). admin (). cluster ().prepareDeleteSnapshot (name , OLD_VERSION_SNAPSHOT_PREFIX + "*" ).get ();
129
- client (). admin (). cluster ().prepareCleanupRepository (name ).get ();
129
+ clusterAdmin ().prepareDeleteSnapshot (name , OLD_VERSION_SNAPSHOT_PREFIX + "*" ).get ();
130
+ clusterAdmin ().prepareCleanupRepository (name ).get ();
130
131
}
131
132
BlobStoreTestUtil .assertRepoConsistency (internalCluster (), name );
132
133
});
@@ -202,12 +203,10 @@ public static void waitForBlock(String node, String repository, TimeValue timeou
202
203
public SnapshotInfo waitForCompletion (String repository , String snapshotName , TimeValue timeout ) throws InterruptedException {
203
204
long start = System .currentTimeMillis ();
204
205
while (System .currentTimeMillis () - start < timeout .millis ()) {
205
- List <SnapshotInfo > snapshotInfos = client ().admin ().cluster ().prepareGetSnapshots (repository ).setSnapshots (snapshotName )
206
- .get ().getSnapshots (repository );
207
- assertThat (snapshotInfos .size (), equalTo (1 ));
208
- if (snapshotInfos .get (0 ).state ().completed ()) {
206
+ final SnapshotInfo snapshotInfo = getSnapshot (repository , snapshotName );
207
+ if (snapshotInfo .state ().completed ()) {
209
208
// Make sure that snapshot clean up operations are finished
210
- ClusterStateResponse stateResponse = client (). admin (). cluster ().prepareState ().get ();
209
+ ClusterStateResponse stateResponse = clusterAdmin ().prepareState ().get ();
211
210
boolean found = false ;
212
211
for (SnapshotsInProgress .Entry entry :
213
212
stateResponse .getState ().custom (SnapshotsInProgress .TYPE , SnapshotsInProgress .EMPTY ).entries ()) {
@@ -218,7 +217,7 @@ public SnapshotInfo waitForCompletion(String repository, String snapshotName, Ti
218
217
}
219
218
}
220
219
if (found == false ) {
221
- return snapshotInfos . get ( 0 ) ;
220
+ return snapshotInfo ;
222
221
}
223
222
}
224
223
Thread .sleep (100 );
@@ -307,7 +306,7 @@ public void unblockNode(final String repository, final String node) {
307
306
308
307
protected void createRepository (String repoName , String type , Settings .Builder settings ) {
309
308
logger .info ("--> creating repository [{}] [{}]" , repoName , type );
310
- assertAcked (client (). admin (). cluster ().preparePutRepository (repoName )
309
+ assertAcked (clusterAdmin ().preparePutRepository (repoName )
311
310
.setType (type ).setSettings (settings ));
312
311
}
313
312
@@ -349,7 +348,7 @@ protected void maybeInitWithOldSnapshotVersion(String repoName, Path repoPath) t
349
348
protected String initWithSnapshotVersion (String repoName , Path repoPath , Version version ) throws IOException {
350
349
assertThat ("This hack only works on an empty repository" , getRepositoryData (repoName ).getSnapshotIds (), empty ());
351
350
final String oldVersionSnapshot = OLD_VERSION_SNAPSHOT_PREFIX + version .id ;
352
- final CreateSnapshotResponse createSnapshotResponse = client (). admin (). cluster ()
351
+ final CreateSnapshotResponse createSnapshotResponse = clusterAdmin ()
353
352
.prepareCreateSnapshot (repoName , oldVersionSnapshot ).setIndices ("does-not-exist-for-sure-*" )
354
353
.setWaitForCompletion (true ).get ();
355
354
assertThat (createSnapshotResponse .getSnapshotInfo ().totalShards (), is (0 ));
@@ -372,7 +371,7 @@ protected String initWithSnapshotVersion(String repoName, Path repoPath, Version
372
371
373
372
protected SnapshotInfo createFullSnapshot (String repoName , String snapshotName ) {
374
373
logger .info ("--> creating full snapshot [{}] in [{}]" , snapshotName , repoName );
375
- CreateSnapshotResponse createSnapshotResponse = client (). admin (). cluster ().prepareCreateSnapshot (repoName , snapshotName )
374
+ CreateSnapshotResponse createSnapshotResponse = clusterAdmin ().prepareCreateSnapshot (repoName , snapshotName )
376
375
.setIncludeGlobalState (true )
377
376
.setWaitForCompletion (true )
378
377
.get ();
@@ -416,7 +415,7 @@ protected void assertDocCount(String index, long count) {
416
415
* @param metadata snapshot metadata to write (as returned by {@link SnapshotInfo#userMetadata()})
417
416
*/
418
417
protected void addBwCFailedSnapshot (String repoName , String snapshotName , Map <String , Object > metadata ) throws Exception {
419
- final ClusterState state = client (). admin (). cluster ().prepareState ().get ().getState ();
418
+ final ClusterState state = clusterAdmin ().prepareState ().get ().getState ();
420
419
final RepositoriesMetadata repositoriesMetadata = state .metadata ().custom (RepositoriesMetadata .TYPE );
421
420
assertNotNull (repositoriesMetadata );
422
421
final RepositoryMetadata initialRepoMetadata = repositoriesMetadata .repository (repoName );
@@ -486,7 +485,7 @@ protected ActionFuture<CreateSnapshotResponse> startFullSnapshot(String repoName
486
485
487
486
protected ActionFuture <CreateSnapshotResponse > startFullSnapshot (String repoName , String snapshotName , boolean partial ) {
488
487
logger .info ("--> creating full snapshot [{}] to repo [{}]" , snapshotName , repoName );
489
- return client (). admin (). cluster ().prepareCreateSnapshot (repoName , snapshotName ).setWaitForCompletion (true )
488
+ return clusterAdmin ().prepareCreateSnapshot (repoName , snapshotName ).setWaitForCompletion (true )
490
489
.setPartial (partial ).execute ();
491
490
}
492
491
@@ -517,6 +516,35 @@ protected void createIndexWithContent(String indexName, Settings indexSettings)
517
516
518
517
protected ActionFuture <AcknowledgedResponse > startDeleteSnapshot (String repoName , String snapshotName ) {
519
518
logger .info ("--> deleting snapshot [{}] from repo [{}]" , snapshotName , repoName );
520
- return client ().admin ().cluster ().prepareDeleteSnapshot (repoName , snapshotName ).execute ();
519
+ return clusterAdmin ().prepareDeleteSnapshot (repoName , snapshotName ).execute ();
520
+ }
521
+
522
+ protected void updateClusterState (final Function <ClusterState , ClusterState > updater ) throws Exception {
523
+ final PlainActionFuture <Void > future = PlainActionFuture .newFuture ();
524
+ final ClusterService clusterService = internalCluster ().getCurrentMasterNodeInstance (ClusterService .class );
525
+ clusterService .submitStateUpdateTask ("test" , new ClusterStateUpdateTask () {
526
+ @ Override
527
+ public ClusterState execute (ClusterState currentState ) {
528
+ return updater .apply (currentState );
529
+ }
530
+
531
+ @ Override
532
+ public void onFailure (String source , Exception e ) {
533
+ future .onFailure (e );
534
+ }
535
+
536
+ @ Override
537
+ public void clusterStateProcessed (String source , ClusterState oldState , ClusterState newState ) {
538
+ future .onResponse (null );
539
+ }
540
+ });
541
+ future .get ();
542
+ }
543
+
544
+ protected SnapshotInfo getSnapshot (String repository , String snapshot ) {
545
+ final List <SnapshotInfo > snapshotInfos = clusterAdmin ().prepareGetSnapshots (repository ).setSnapshots (snapshot )
546
+ .get ().getSnapshots (repository );
547
+ assertThat (snapshotInfos , hasSize (1 ));
548
+ return snapshotInfos .get (0 );
521
549
}
522
550
}
0 commit comments