|
| 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.remotemigration; |
| 10 | + |
| 11 | +import org.opensearch.action.admin.indices.stats.ShardStats; |
| 12 | +import org.opensearch.cluster.metadata.IndexMetadata; |
| 13 | +import org.opensearch.cluster.node.DiscoveryNode; |
| 14 | +import org.opensearch.cluster.node.DiscoveryNodes; |
| 15 | +import org.opensearch.cluster.routing.ShardRouting; |
| 16 | +import org.opensearch.common.blobstore.BlobPath; |
| 17 | +import org.opensearch.common.settings.Settings; |
| 18 | +import org.opensearch.common.unit.TimeValue; |
| 19 | +import org.opensearch.core.util.FileSystemUtils; |
| 20 | +import org.opensearch.index.remote.RemoteSegmentStats; |
| 21 | +import org.opensearch.index.translog.RemoteTranslogStats; |
| 22 | +import org.opensearch.test.InternalTestCluster; |
| 23 | +import org.opensearch.test.OpenSearchIntegTestCase; |
| 24 | + |
| 25 | +import java.nio.file.Path; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Set; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | +import java.util.stream.Collectors; |
| 32 | + |
| 33 | +import static org.opensearch.index.remote.RemoteStoreEnums.DataCategory.SEGMENTS; |
| 34 | +import static org.opensearch.index.remote.RemoteStoreEnums.DataType.DATA; |
| 35 | +import static org.opensearch.index.store.RemoteSegmentStoreDirectory.SEGMENT_NAME_UUID_SEPARATOR; |
| 36 | + |
| 37 | +@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST, numDataNodes = 0) |
| 38 | +public class RemotePrimaryLocalRecoveryIT extends MigrationBaseTestCase { |
| 39 | + String indexName = "idx1"; |
| 40 | + int numOfNodes = randomIntBetween(6, 9); |
| 41 | + |
| 42 | + /** |
| 43 | + * Tests local recovery sanity in the happy path flow |
| 44 | + */ |
| 45 | + public void testLocalRecoveryRollingRestart() throws Exception { |
| 46 | + triggerRollingRestartForRemoteMigration(0); |
| 47 | + internalCluster().stopAllNodes(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Tests local recovery sanity during remote migration with a node restart in between |
| 52 | + */ |
| 53 | + public void testLocalRecoveryRollingRestartAndNodeFailure() throws Exception { |
| 54 | + triggerRollingRestartForRemoteMigration(0); |
| 55 | + |
| 56 | + DiscoveryNodes discoveryNodes = internalCluster().client().admin().cluster().prepareState().get().getState().getNodes(); |
| 57 | + DiscoveryNode nodeToRestart = (DiscoveryNode) discoveryNodes.getDataNodes().values().toArray()[randomIntBetween(0, numOfNodes - 4)]; |
| 58 | + internalCluster().restartNode(nodeToRestart.getName()); |
| 59 | + |
| 60 | + Map<ShardRouting, ShardStats> shardStatsMap = internalCluster().client().admin().indices().prepareStats(indexName).get().asMap(); |
| 61 | + for (Map.Entry<ShardRouting, ShardStats> entry : shardStatsMap.entrySet()) { |
| 62 | + ShardRouting shardRouting = entry.getKey(); |
| 63 | + ShardStats shardStats = entry.getValue(); |
| 64 | + if (nodeToRestart.equals(shardRouting.currentNodeId())) { |
| 65 | + RemoteSegmentStats remoteSegmentStats = shardStats.getStats().getSegments().getRemoteSegmentStats(); |
| 66 | + assertTrue(remoteSegmentStats.getTotalUploadTime() > 0); |
| 67 | + assertTrue(remoteSegmentStats.getUploadBytesSucceeded() > 0); |
| 68 | + } |
| 69 | + |
| 70 | + assertBusy(() -> { |
| 71 | + String shardPath = getShardLevelBlobPath( |
| 72 | + client(), |
| 73 | + indexName, |
| 74 | + new BlobPath(), |
| 75 | + String.valueOf(shardRouting.getId()), |
| 76 | + SEGMENTS, |
| 77 | + DATA |
| 78 | + ).buildAsString(); |
| 79 | + Path segmentDataRepoPath = segmentRepoPath.resolve(shardPath); |
| 80 | + List<String> segmentsNFilesInRepo = Arrays.stream(FileSystemUtils.files(segmentDataRepoPath)) |
| 81 | + .filter(path -> path.getFileName().toString().contains("segments_")) |
| 82 | + .map(path -> path.getFileName().toString()) |
| 83 | + .collect(Collectors.toList()); |
| 84 | + Set<String> expectedUniqueSegmentsNFiles = segmentsNFilesInRepo.stream() |
| 85 | + .map(fileName -> fileName.split(SEGMENT_NAME_UUID_SEPARATOR)[0]) |
| 86 | + .collect(Collectors.toSet()); |
| 87 | + assertEquals( |
| 88 | + "Expected no duplicate segments_N files in remote but duplicates were found " + segmentsNFilesInRepo, |
| 89 | + expectedUniqueSegmentsNFiles.size(), |
| 90 | + segmentsNFilesInRepo.size() |
| 91 | + ); |
| 92 | + }, 90, TimeUnit.SECONDS); |
| 93 | + } |
| 94 | + |
| 95 | + internalCluster().stopAllNodes(); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Tests local recovery flow sanity in the happy path flow with replicas in place |
| 100 | + */ |
| 101 | + public void testLocalRecoveryFlowWithReplicas() throws Exception { |
| 102 | + triggerRollingRestartForRemoteMigration(randomIntBetween(1, 2)); |
| 103 | + internalCluster().stopAllNodes(); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Helper method to run a rolling restart for migration to remote backed cluster |
| 108 | + */ |
| 109 | + private void triggerRollingRestartForRemoteMigration(int replicaCount) throws Exception { |
| 110 | + internalCluster().startClusterManagerOnlyNodes(3); |
| 111 | + internalCluster().startNodes(numOfNodes - 3); |
| 112 | + |
| 113 | + // create index |
| 114 | + Settings indexSettings = Settings.builder() |
| 115 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, replicaCount) |
| 116 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) |
| 117 | + .build(); |
| 118 | + createIndex(indexName, indexSettings); |
| 119 | + ensureGreen(indexName); |
| 120 | + indexBulk(indexName, randomIntBetween(100, 10000)); |
| 121 | + refresh(indexName); |
| 122 | + indexBulk(indexName, randomIntBetween(100, 10000)); |
| 123 | + |
| 124 | + initDocRepToRemoteMigration(); |
| 125 | + |
| 126 | + // rolling restart |
| 127 | + final Settings remoteNodeAttributes = remoteStoreClusterSettings( |
| 128 | + REPOSITORY_NAME, |
| 129 | + segmentRepoPath, |
| 130 | + REPOSITORY_2_NAME, |
| 131 | + translogRepoPath |
| 132 | + ); |
| 133 | + internalCluster().rollingRestart(new InternalTestCluster.RestartCallback() { |
| 134 | + // Update remote attributes |
| 135 | + @Override |
| 136 | + public Settings onNodeStopped(String nodeName) { |
| 137 | + return remoteNodeAttributes; |
| 138 | + } |
| 139 | + }); |
| 140 | + ensureStableCluster(numOfNodes); |
| 141 | + ensureGreen(TimeValue.timeValueSeconds(90), indexName); |
| 142 | + assertEquals(internalCluster().size(), numOfNodes); |
| 143 | + |
| 144 | + // Assert on remote uploads |
| 145 | + Map<ShardRouting, ShardStats> shardStatsMap = internalCluster().client().admin().indices().prepareStats(indexName).get().asMap(); |
| 146 | + DiscoveryNodes discoveryNodes = internalCluster().client().admin().cluster().prepareState().get().getState().getNodes(); |
| 147 | + shardStatsMap.forEach((shardRouting, shardStats) -> { |
| 148 | + if (discoveryNodes.get(shardRouting.currentNodeId()).isRemoteStoreNode() && shardRouting.primary()) { |
| 149 | + RemoteSegmentStats remoteSegmentStats = shardStats.getStats().getSegments().getRemoteSegmentStats(); |
| 150 | + assertTrue(remoteSegmentStats.getTotalUploadTime() > 0); |
| 151 | + assertTrue(remoteSegmentStats.getUploadBytesSucceeded() > 0); |
| 152 | + } |
| 153 | + }); |
| 154 | + |
| 155 | + // Assert on new remote uploads after seeding |
| 156 | + indexBulk(indexName, randomIntBetween(100, 10000)); |
| 157 | + refresh(indexName); |
| 158 | + indexBulk(indexName, randomIntBetween(100, 10000)); |
| 159 | + Map<ShardRouting, ShardStats> newShardStatsMap = internalCluster().client().admin().indices().prepareStats(indexName).get().asMap(); |
| 160 | + newShardStatsMap.forEach((shardRouting, shardStats) -> { |
| 161 | + if (discoveryNodes.get(shardRouting.currentNodeId()).isRemoteStoreNode() && shardRouting.primary()) { |
| 162 | + RemoteSegmentStats prevRemoteSegmentStats = shardStatsMap.get(shardRouting) |
| 163 | + .getStats() |
| 164 | + .getSegments() |
| 165 | + .getRemoteSegmentStats(); |
| 166 | + RemoteSegmentStats newRemoteSegmentStats = shardStats.getStats().getSegments().getRemoteSegmentStats(); |
| 167 | + assertTrue(newRemoteSegmentStats.getTotalUploadTime() > prevRemoteSegmentStats.getTotalUploadTime()); |
| 168 | + assertTrue(newRemoteSegmentStats.getUploadBytesSucceeded() > prevRemoteSegmentStats.getUploadBytesSucceeded()); |
| 169 | + |
| 170 | + RemoteTranslogStats prevRemoteTranslogStats = shardStatsMap.get(shardRouting) |
| 171 | + .getStats() |
| 172 | + .getTranslog() |
| 173 | + .getRemoteTranslogStats(); |
| 174 | + RemoteTranslogStats newRemoteTranslogStats = shardStats.getStats().getTranslog().getRemoteTranslogStats(); |
| 175 | + assertTrue(newRemoteTranslogStats.getUploadBytesSucceeded() > prevRemoteTranslogStats.getUploadBytesSucceeded()); |
| 176 | + } |
| 177 | + }); |
| 178 | + } |
| 179 | +} |
0 commit comments