|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.remotestore; |
| 10 | + |
| 11 | +import org.junit.After; |
| 12 | +import org.junit.Before; |
| 13 | +import org.opensearch.action.admin.cluster.remotestore.restore.RestoreRemoteStoreRequest; |
| 14 | +import org.opensearch.action.index.IndexResponse; |
| 15 | +import org.opensearch.action.support.PlainActionFuture; |
| 16 | +import org.opensearch.cluster.metadata.IndexMetadata; |
| 17 | +import org.opensearch.common.UUIDs; |
| 18 | +import org.opensearch.common.settings.Settings; |
| 19 | +import org.opensearch.common.util.FeatureFlags; |
| 20 | +import org.opensearch.index.IndexModule; |
| 21 | +import org.opensearch.indices.replication.common.ReplicationType; |
| 22 | +import org.opensearch.plugins.Plugin; |
| 23 | +import org.opensearch.test.InternalTestCluster; |
| 24 | +import org.opensearch.test.OpenSearchIntegTestCase; |
| 25 | +import org.opensearch.test.transport.MockTransportService; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.nio.file.Path; |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.Collection; |
| 31 | +import java.util.HashMap; |
| 32 | +import java.util.Map; |
| 33 | + |
| 34 | +import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked; |
| 35 | +import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount; |
| 36 | + |
| 37 | +@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST, numDataNodes = 0) |
| 38 | +public class RemoteStoreIT extends OpenSearchIntegTestCase { |
| 39 | + |
| 40 | + private static final String REPOSITORY_NAME = "test-remore-store-repo"; |
| 41 | + private static final String INDEX_NAME = "remote-store-test-idx-1"; |
| 42 | + private static final String TOTAL_OPERATIONS = "total-operations"; |
| 43 | + private static final String REFRESHED_OR_FLUSHED_OPERATIONS = "refreshed-or-flushed-operations"; |
| 44 | + private static final String MAX_SEQ_NO_TOTAL = "max-seq-no-total"; |
| 45 | + private static final String MAX_SEQ_NO_REFRESHED_OR_FLUSHED = "max-seq-no-refreshed-or-flushed"; |
| 46 | + |
| 47 | + @Override |
| 48 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 49 | + return Arrays.asList(MockTransportService.TestPlugin.class); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public Settings indexSettings() { |
| 54 | + return remoteStoreIndexSettings(0); |
| 55 | + } |
| 56 | + |
| 57 | + private Settings remoteStoreIndexSettings(int numberOfReplicas) { |
| 58 | + return Settings.builder() |
| 59 | + .put(super.indexSettings()) |
| 60 | + .put("index.refresh_interval", "300s") |
| 61 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) |
| 62 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, numberOfReplicas) |
| 63 | + .put(IndexModule.INDEX_QUERY_CACHE_ENABLED_SETTING.getKey(), false) |
| 64 | + .put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.SEGMENT) |
| 65 | + .put(IndexMetadata.SETTING_REMOTE_STORE_ENABLED, true) |
| 66 | + .put(IndexMetadata.SETTING_REMOTE_STORE_REPOSITORY, REPOSITORY_NAME) |
| 67 | + .build(); |
| 68 | + } |
| 69 | + |
| 70 | + private Settings remoteTranslogIndexSettings(int numberOfReplicas) { |
| 71 | + return Settings.builder() |
| 72 | + .put(remoteStoreIndexSettings(numberOfReplicas)) |
| 73 | + .put(IndexMetadata.SETTING_REMOTE_TRANSLOG_STORE_ENABLED, true) |
| 74 | + .put(IndexMetadata.SETTING_REMOTE_TRANSLOG_STORE_REPOSITORY, REPOSITORY_NAME) |
| 75 | + .build(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + protected boolean addMockInternalEngine() { |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected Settings featureFlagSettings() { |
| 85 | + return Settings.builder() |
| 86 | + .put(super.featureFlagSettings()) |
| 87 | + .put(FeatureFlags.REPLICATION_TYPE, "true") |
| 88 | + .put(FeatureFlags.REMOTE_STORE, "true") |
| 89 | + .build(); |
| 90 | + } |
| 91 | + |
| 92 | + @Before |
| 93 | + public void setup() { |
| 94 | + Path absolutePath = randomRepoPath().toAbsolutePath(); |
| 95 | + assertAcked( |
| 96 | + clusterAdmin().preparePutRepository(REPOSITORY_NAME).setType("fs").setSettings(Settings.builder().put("location", absolutePath)) |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + @After |
| 101 | + public void teardown() { |
| 102 | + assertAcked(clusterAdmin().prepareDeleteRepository(REPOSITORY_NAME)); |
| 103 | + } |
| 104 | + |
| 105 | + private IndexResponse indexSingleDoc() { |
| 106 | + return client().prepareIndex(INDEX_NAME) |
| 107 | + .setId(UUIDs.randomBase64UUID()) |
| 108 | + .setSource(randomAlphaOfLength(5), randomAlphaOfLength(5)) |
| 109 | + .get(); |
| 110 | + } |
| 111 | + |
| 112 | + private Map<String, Long> indexData() { |
| 113 | + long totalOperations = 0; |
| 114 | + long refreshedOrFlushedOperations = 0; |
| 115 | + long maxSeqNo = 0; |
| 116 | + long maxSeqNoRefreshedOrFlushed = 0; |
| 117 | + for (int i = 0; i < randomIntBetween(1, 10); i++) { |
| 118 | + if (randomBoolean()) { |
| 119 | + flush(INDEX_NAME); |
| 120 | + } else { |
| 121 | + refresh(INDEX_NAME); |
| 122 | + } |
| 123 | + maxSeqNoRefreshedOrFlushed = maxSeqNo; |
| 124 | + refreshedOrFlushedOperations = totalOperations; |
| 125 | + int numberOfOperations = randomIntBetween(20, 50); |
| 126 | + for (int j = 0; j < numberOfOperations; j++) { |
| 127 | + IndexResponse response = indexSingleDoc(); |
| 128 | + maxSeqNo = response.getSeqNo(); |
| 129 | + } |
| 130 | + totalOperations += numberOfOperations; |
| 131 | + } |
| 132 | + Map<String, Long> indexingStats = new HashMap<>(); |
| 133 | + indexingStats.put(TOTAL_OPERATIONS, totalOperations); |
| 134 | + indexingStats.put(REFRESHED_OR_FLUSHED_OPERATIONS, refreshedOrFlushedOperations); |
| 135 | + indexingStats.put(MAX_SEQ_NO_TOTAL, maxSeqNo); |
| 136 | + indexingStats.put(MAX_SEQ_NO_REFRESHED_OR_FLUSHED, maxSeqNoRefreshedOrFlushed); |
| 137 | + return indexingStats; |
| 138 | + } |
| 139 | + |
| 140 | + private void verifyRestoredData(Map<String, Long> indexStats, boolean checkTotal) { |
| 141 | + String statsGranularity = checkTotal ? TOTAL_OPERATIONS : REFRESHED_OR_FLUSHED_OPERATIONS; |
| 142 | + String maxSeqNoGranularity = checkTotal ? MAX_SEQ_NO_TOTAL : MAX_SEQ_NO_REFRESHED_OR_FLUSHED; |
| 143 | + ensureYellowAndNoInitializingShards(INDEX_NAME); |
| 144 | + ensureGreen(INDEX_NAME); |
| 145 | + assertHitCount(client().prepareSearch(INDEX_NAME).setSize(0).get(), indexStats.get(statsGranularity)); |
| 146 | + IndexResponse response = indexSingleDoc(); |
| 147 | + assertEquals(indexStats.get(maxSeqNoGranularity) + 1, response.getSeqNo()); |
| 148 | + refresh(INDEX_NAME); |
| 149 | + assertHitCount(client().prepareSearch(INDEX_NAME).setSize(0).get(), indexStats.get(statsGranularity) + 1); |
| 150 | + } |
| 151 | + |
| 152 | + public void testRemoteStoreRestoreFromRemoteSegmentStore() throws IOException { |
| 153 | + internalCluster().startNodes(3); |
| 154 | + createIndex(INDEX_NAME, remoteStoreIndexSettings(0)); |
| 155 | + ensureYellowAndNoInitializingShards(INDEX_NAME); |
| 156 | + ensureGreen(INDEX_NAME); |
| 157 | + |
| 158 | + Map<String, Long> indexStats = indexData(); |
| 159 | + |
| 160 | + internalCluster().stopRandomNode(InternalTestCluster.nameFilter(primaryNodeName(INDEX_NAME))); |
| 161 | + assertAcked(client().admin().indices().prepareClose(INDEX_NAME)); |
| 162 | + |
| 163 | + client().admin().cluster().restoreRemoteStore(new RestoreRemoteStoreRequest().indices(INDEX_NAME), PlainActionFuture.newFuture()); |
| 164 | + |
| 165 | + verifyRestoredData(indexStats, false); |
| 166 | + } |
| 167 | + |
| 168 | + public void testRemoteTranslogRestore() throws IOException { |
| 169 | + internalCluster().startNodes(3); |
| 170 | + createIndex(INDEX_NAME, remoteTranslogIndexSettings(0)); |
| 171 | + ensureYellowAndNoInitializingShards(INDEX_NAME); |
| 172 | + ensureGreen(INDEX_NAME); |
| 173 | + |
| 174 | + Map<String, Long> indexStats = indexData(); |
| 175 | + |
| 176 | + internalCluster().stopRandomNode(InternalTestCluster.nameFilter(primaryNodeName(INDEX_NAME))); |
| 177 | + assertAcked(client().admin().indices().prepareClose(INDEX_NAME)); |
| 178 | + |
| 179 | + client().admin().cluster().restoreRemoteStore(new RestoreRemoteStoreRequest().indices(INDEX_NAME), PlainActionFuture.newFuture()); |
| 180 | + |
| 181 | + verifyRestoredData(indexStats, true); |
| 182 | + } |
| 183 | +} |
0 commit comments