10
10
import org .elasticsearch .action .ActionRequest ;
11
11
import org .elasticsearch .action .ActionResponse ;
12
12
import org .elasticsearch .action .ActionType ;
13
- import org .elasticsearch .action .admin .cluster .snapshots .restore .RestoreSnapshotAction ;
14
- import org .elasticsearch .action .admin .cluster .snapshots .restore .RestoreSnapshotRequest ;
13
+ import org .elasticsearch .action .admin .cluster .snapshots .restore .RestoreSnapshotResponse ;
15
14
import org .elasticsearch .cluster .ClusterState ;
16
15
import org .elasticsearch .cluster .metadata .IndexMetadata ;
17
16
import org .elasticsearch .cluster .metadata .Metadata ;
17
+ import org .elasticsearch .snapshots .RestoreInfo ;
18
18
import org .elasticsearch .test .client .NoOpClient ;
19
19
import org .elasticsearch .xpack .core .ilm .Step .StepKey ;
20
+ import org .elasticsearch .xpack .core .searchablesnapshots .MountSearchableSnapshotAction ;
21
+ import org .elasticsearch .xpack .core .searchablesnapshots .MountSearchableSnapshotRequest ;
20
22
21
23
import java .util .HashMap ;
24
+ import java .util .List ;
22
25
import java .util .Map ;
23
26
24
27
import static org .elasticsearch .xpack .core .ilm .AbstractStepMasterTimeoutTestCase .emptyClusterState ;
28
31
29
32
public class MountSnapshotStepTests extends AbstractStepTestCase <MountSnapshotStep > {
30
33
34
+ private static final String RESTORED_INDEX_PREFIX = "restored-" ;
35
+
31
36
@ Override
32
37
public MountSnapshotStep createRandomInstance () {
33
38
StepKey stepKey = randomStepKey ();
@@ -127,6 +132,8 @@ public void testPerformAction() {
127
132
Map <String , String > ilmCustom = new HashMap <>();
128
133
String snapshotName = indexName + "-" + policyName ;
129
134
ilmCustom .put ("snapshot_name" , snapshotName );
135
+ String repository = "repository" ;
136
+ ilmCustom .put ("snapshot_repository" , repository );
130
137
131
138
IndexMetadata .Builder indexMetadataBuilder =
132
139
IndexMetadata .builder (indexName ).settings (settings (Version .CURRENT ).put (LifecycleSettings .LIFECYCLE_NAME , policyName ))
@@ -137,42 +144,109 @@ public void testPerformAction() {
137
144
ClusterState clusterState =
138
145
ClusterState .builder (emptyClusterState ()).metadata (Metadata .builder ().put (indexMetaData , true ).build ()).build ();
139
146
140
- String repository = "repository" ;
141
- String restoredIndexPrefix = "restored-" ;
142
- try (NoOpClient client = getRestoreSnapshotRequestAssertingClient (repository , snapshotName , indexName , restoredIndexPrefix )) {
143
- MountSnapshotStep step = new MountSnapshotStep (randomStepKey (), randomStepKey (), client , restoredIndexPrefix );
147
+ try (NoOpClient client = getRestoreSnapshotRequestAssertingClient (repository , snapshotName , indexName , RESTORED_INDEX_PREFIX )) {
148
+ MountSnapshotStep step = new MountSnapshotStep (randomStepKey (), randomStepKey (), client , RESTORED_INDEX_PREFIX );
144
149
step .performAction (indexMetaData , clusterState , null , new AsyncActionStep .Listener () {
145
150
@ Override
146
151
public void onResponse (boolean complete ) {
152
+ assertThat (complete , is (true ));
147
153
}
148
154
149
155
@ Override
150
156
public void onFailure (Exception e ) {
157
+ fail ("expecting successful response but got: [" + e .getMessage () + "]" );
151
158
}
152
159
});
153
160
}
154
161
}
155
162
163
+ public void testResponseStatusHandling () {
164
+ String indexName = randomAlphaOfLength (10 );
165
+ String policyName = "test-ilm-policy" ;
166
+ Map <String , String > ilmCustom = new HashMap <>();
167
+ String snapshotName = indexName + "-" + policyName ;
168
+ ilmCustom .put ("snapshot_name" , snapshotName );
169
+ String repository = "repository" ;
170
+ ilmCustom .put ("snapshot_repository" , repository );
171
+
172
+ IndexMetadata .Builder indexMetadataBuilder =
173
+ IndexMetadata .builder (indexName ).settings (settings (Version .CURRENT ).put (LifecycleSettings .LIFECYCLE_NAME , policyName ))
174
+ .putCustom (LifecycleExecutionState .ILM_CUSTOM_METADATA_KEY , ilmCustom )
175
+ .numberOfShards (randomIntBetween (1 , 5 )).numberOfReplicas (randomIntBetween (0 , 5 ));
176
+ IndexMetadata indexMetaData = indexMetadataBuilder .build ();
177
+
178
+ ClusterState clusterState =
179
+ ClusterState .builder (emptyClusterState ()).metadata (Metadata .builder ().put (indexMetaData , true ).build ()).build ();
180
+
181
+ {
182
+ RestoreSnapshotResponse responseWithOKStatus = new RestoreSnapshotResponse (new RestoreInfo ("test" , List .of (), 1 , 1 ));
183
+ try (NoOpClient clientPropagatingOKResponse = getClientTriggeringResponse (responseWithOKStatus )) {
184
+ MountSnapshotStep step = new MountSnapshotStep (randomStepKey (), randomStepKey (), clientPropagatingOKResponse ,
185
+ RESTORED_INDEX_PREFIX );
186
+ step .performAction (indexMetaData , clusterState , null , new AsyncActionStep .Listener () {
187
+ @ Override
188
+ public void onResponse (boolean complete ) {
189
+ assertThat (complete , is (true ));
190
+ }
191
+
192
+ @ Override
193
+ public void onFailure (Exception e ) {
194
+ fail ("expecting successful response but got: [" + e .getMessage () + "]" );
195
+ }
196
+ });
197
+ }
198
+ }
199
+
200
+ {
201
+ RestoreSnapshotResponse responseWithACCEPTEDStatus = new RestoreSnapshotResponse ((RestoreInfo ) null );
202
+ try (NoOpClient clientPropagatingACCEPTEDResponse = getClientTriggeringResponse (responseWithACCEPTEDStatus )) {
203
+ MountSnapshotStep step = new MountSnapshotStep (randomStepKey (), randomStepKey (), clientPropagatingACCEPTEDResponse ,
204
+ RESTORED_INDEX_PREFIX );
205
+ step .performAction (indexMetaData , clusterState , null , new AsyncActionStep .Listener () {
206
+ @ Override
207
+ public void onResponse (boolean complete ) {
208
+ assertThat (complete , is (true ));
209
+ }
210
+
211
+ @ Override
212
+ public void onFailure (Exception e ) {
213
+ fail ("expecting successful response but got: [" + e .getMessage () + "]" );
214
+ }
215
+ });
216
+ }
217
+ }
218
+ }
219
+
220
+ @ SuppressWarnings ("unchecked" )
221
+ private NoOpClient getClientTriggeringResponse (RestoreSnapshotResponse response ) {
222
+ return new NoOpClient (getTestName ()) {
223
+ @ Override
224
+ protected <Request extends ActionRequest , Response extends ActionResponse > void doExecute (ActionType <Response > action ,
225
+ Request request ,
226
+ ActionListener <Response > listener ) {
227
+ listener .onResponse ((Response ) response );
228
+ }
229
+ };
230
+ }
231
+
232
+ @ SuppressWarnings ("unchecked" )
156
233
private NoOpClient getRestoreSnapshotRequestAssertingClient (String expectedRepoName , String expectedSnapshotName , String indexName ,
157
234
String restoredIndexPrefix ) {
158
235
return new NoOpClient (getTestName ()) {
159
236
@ Override
160
237
protected <Request extends ActionRequest , Response extends ActionResponse > void doExecute (ActionType <Response > action ,
161
238
Request request ,
162
239
ActionListener <Response > listener ) {
163
- assertThat (action .name (), is (RestoreSnapshotAction .NAME ));
164
- assertTrue (request instanceof RestoreSnapshotRequest );
165
- RestoreSnapshotRequest restoreSnapshotRequest = (RestoreSnapshotRequest ) request ;
166
- assertThat (restoreSnapshotRequest . repository (), is (expectedRepoName ));
167
- assertThat (restoreSnapshotRequest . snapshot (), is (expectedSnapshotName ));
240
+ assertThat (action .name (), is (MountSearchableSnapshotAction .NAME ));
241
+ assertTrue (request instanceof MountSearchableSnapshotRequest );
242
+ MountSearchableSnapshotRequest mountSearchableSnapshotRequest = (MountSearchableSnapshotRequest ) request ;
243
+ assertThat (mountSearchableSnapshotRequest . repositoryName (), is (expectedRepoName ));
244
+ assertThat (mountSearchableSnapshotRequest . snapshotName (), is (expectedSnapshotName ));
168
245
assertThat ("another ILM step will wait for the restore to complete. the " + MountSnapshotStep .NAME + " step should not" ,
169
- restoreSnapshotRequest .waitForCompletion (), is (false ));
170
- assertThat ("another ILM step will transfer the aliases to the restored index" , restoreSnapshotRequest .includeAliases (),
171
- is (false ));
172
- assertThat (restoreSnapshotRequest .ignoreIndexSettings (), is (notNullValue ()));
173
- assertThat (restoreSnapshotRequest .ignoreIndexSettings ()[0 ], is (LifecycleSettings .LIFECYCLE_NAME ));
174
- assertThat (restoreSnapshotRequest .renameReplacement (), is (restoredIndexPrefix + indexName ));
175
- assertThat (restoreSnapshotRequest .renamePattern (), is (indexName ));
246
+ mountSearchableSnapshotRequest .waitForCompletion (), is (false ));
247
+ assertThat (mountSearchableSnapshotRequest .ignoreIndexSettings (), is (notNullValue ()));
248
+ assertThat (mountSearchableSnapshotRequest .ignoreIndexSettings ()[0 ], is (LifecycleSettings .LIFECYCLE_NAME ));
249
+ assertThat (mountSearchableSnapshotRequest .mountedIndexName (), is (restoredIndexPrefix + indexName ));
176
250
}
177
251
};
178
252
}
0 commit comments