|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.searchablesnapshots; |
| 8 | + |
| 9 | +import org.elasticsearch.action.ActionFuture; |
| 10 | +import org.elasticsearch.action.ActionListener; |
| 11 | +import org.elasticsearch.action.ActionRequest; |
| 12 | +import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse; |
| 13 | +import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotAction; |
| 14 | +import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse; |
| 15 | +import org.elasticsearch.action.support.ActionFilter; |
| 16 | +import org.elasticsearch.action.support.ActionFilters; |
| 17 | +import org.elasticsearch.action.support.PlainActionFuture; |
| 18 | +import org.elasticsearch.common.Strings; |
| 19 | +import org.elasticsearch.common.settings.Settings; |
| 20 | +import org.elasticsearch.plugins.ActionPlugin; |
| 21 | +import org.elasticsearch.plugins.Plugin; |
| 22 | +import org.elasticsearch.snapshots.SnapshotInfo; |
| 23 | +import org.elasticsearch.snapshots.SnapshotRestoreException; |
| 24 | +import org.elasticsearch.xpack.core.searchablesnapshots.MountSearchableSnapshotAction; |
| 25 | +import org.elasticsearch.xpack.core.searchablesnapshots.MountSearchableSnapshotRequest; |
| 26 | + |
| 27 | +import java.util.Collection; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Locale; |
| 30 | +import java.util.stream.Collectors; |
| 31 | +import java.util.stream.Stream; |
| 32 | + |
| 33 | +import static java.util.Collections.singletonList; |
| 34 | +import static org.elasticsearch.index.IndexSettings.INDEX_SOFT_DELETES_SETTING; |
| 35 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 36 | +import static org.hamcrest.Matchers.containsString; |
| 37 | +import static org.hamcrest.Matchers.equalTo; |
| 38 | +import static org.hamcrest.Matchers.greaterThan; |
| 39 | + |
| 40 | +public class SearchableSnapshotsUuidValidationIntegTests extends BaseSearchableSnapshotsIntegTestCase { |
| 41 | + |
| 42 | + public static class TestPlugin extends Plugin implements ActionPlugin { |
| 43 | + |
| 44 | + private final RestoreBlockingActionFilter restoreBlockingActionFilter; |
| 45 | + |
| 46 | + public TestPlugin() { |
| 47 | + restoreBlockingActionFilter = new RestoreBlockingActionFilter(); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public List<ActionFilter> getActionFilters() { |
| 52 | + return singletonList(restoreBlockingActionFilter); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public static class RestoreBlockingActionFilter extends org.elasticsearch.action.support.ActionFilter.Simple { |
| 57 | + private final PlainActionFuture<Void> executed = new PlainActionFuture<>(); |
| 58 | + private final PlainActionFuture<Void> unblocked = new PlainActionFuture<>(); |
| 59 | + |
| 60 | + @Override |
| 61 | + protected boolean apply(String action, ActionRequest request, ActionListener<?> listener) { |
| 62 | + if (RestoreSnapshotAction.NAME.equals(action)) { |
| 63 | + executed.onResponse(null); |
| 64 | + unblocked.actionGet(); |
| 65 | + } |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public int order() { |
| 71 | + return 0; |
| 72 | + } |
| 73 | + |
| 74 | + public void unblock() { |
| 75 | + unblocked.onResponse(null); |
| 76 | + } |
| 77 | + |
| 78 | + public void awaitExecution() { |
| 79 | + executed.actionGet(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 85 | + return Stream.concat(super.nodePlugins().stream(), Stream.of(TestPlugin.class)).collect(Collectors.toList()); |
| 86 | + } |
| 87 | + |
| 88 | + public void testMountFailsIfSnapshotChanged() throws Exception { |
| 89 | + final String fsRepoName = randomAlphaOfLength(10); |
| 90 | + final String indexName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT); |
| 91 | + final String restoredIndexName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT); |
| 92 | + final String snapshotName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT); |
| 93 | + |
| 94 | + createRepo(fsRepoName); |
| 95 | + |
| 96 | + final Settings.Builder originalIndexSettings = Settings.builder().put(INDEX_SOFT_DELETES_SETTING.getKey(), true); |
| 97 | + createAndPopulateIndex(indexName, originalIndexSettings); |
| 98 | + |
| 99 | + createSnapshot(fsRepoName, snapshotName); |
| 100 | + |
| 101 | + final MountSearchableSnapshotRequest req = new MountSearchableSnapshotRequest( |
| 102 | + restoredIndexName, |
| 103 | + fsRepoName, |
| 104 | + snapshotName, |
| 105 | + indexName, |
| 106 | + Settings.EMPTY, |
| 107 | + Strings.EMPTY_ARRAY, |
| 108 | + true |
| 109 | + ); |
| 110 | + |
| 111 | + final ActionFuture<RestoreSnapshotResponse> responseFuture = client().execute(MountSearchableSnapshotAction.INSTANCE, req); |
| 112 | + |
| 113 | + final RestoreBlockingActionFilter restoreBlockingActionFilter = getBlockingActionFilter(); |
| 114 | + restoreBlockingActionFilter.awaitExecution(); |
| 115 | + |
| 116 | + assertAcked(client().admin().cluster().prepareDeleteSnapshot(fsRepoName, snapshotName).get()); |
| 117 | + createSnapshot(fsRepoName, snapshotName); |
| 118 | + |
| 119 | + assertFalse(responseFuture.isDone()); |
| 120 | + restoreBlockingActionFilter.unblock(); |
| 121 | + |
| 122 | + assertThat( |
| 123 | + expectThrows(SnapshotRestoreException.class, responseFuture::actionGet).getMessage(), |
| 124 | + containsString("snapshot UUID mismatch") |
| 125 | + ); |
| 126 | + |
| 127 | + assertAcked(client().admin().indices().prepareDelete(indexName)); |
| 128 | + } |
| 129 | + |
| 130 | + private static void createSnapshot(String fsRepoName, String snapshotName) { |
| 131 | + final CreateSnapshotResponse createSnapshotResponse = client().admin() |
| 132 | + .cluster() |
| 133 | + .prepareCreateSnapshot(fsRepoName, snapshotName) |
| 134 | + .setWaitForCompletion(true) |
| 135 | + .get(); |
| 136 | + final SnapshotInfo snapshotInfo = createSnapshotResponse.getSnapshotInfo(); |
| 137 | + assertThat(snapshotInfo.successfulShards(), greaterThan(0)); |
| 138 | + assertThat(snapshotInfo.successfulShards(), equalTo(snapshotInfo.totalShards())); |
| 139 | + } |
| 140 | + |
| 141 | + private static RestoreBlockingActionFilter getBlockingActionFilter() { |
| 142 | + for (final ActionFilter filter : internalCluster().getCurrentMasterNodeInstance(ActionFilters.class).filters()) { |
| 143 | + if (filter instanceof RestoreBlockingActionFilter) { |
| 144 | + return (RestoreBlockingActionFilter) filter; |
| 145 | + } |
| 146 | + } |
| 147 | + throw new AssertionError("did not find BlockingActionFilter"); |
| 148 | + } |
| 149 | + |
| 150 | +} |
0 commit comments