Search in sources :

Example 1 with Snapshot

use of org.opensearch.snapshots.Snapshot in project OpenSearch by opensearch-project.

the class IndexRecoveryIT method testSnapshotRecovery.

public void testSnapshotRecovery() throws Exception {
    logger.info("--> start node A");
    String nodeA = internalCluster().startNode();
    logger.info("--> create repository");
    assertAcked(client().admin().cluster().preparePutRepository(REPO_NAME).setType("fs").setSettings(Settings.builder().put("location", randomRepoPath()).put("compress", false)).get());
    ensureGreen();
    logger.info("--> create index on node: {}", nodeA);
    createAndPopulateIndex(INDEX_NAME, 1, SHARD_COUNT, REPLICA_COUNT);
    logger.info("--> snapshot");
    CreateSnapshotResponse createSnapshotResponse = client().admin().cluster().prepareCreateSnapshot(REPO_NAME, SNAP_NAME).setWaitForCompletion(true).setIndices(INDEX_NAME).get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    assertThat(client().admin().cluster().prepareGetSnapshots(REPO_NAME).setSnapshots(SNAP_NAME).get().getSnapshots().get(0).state(), equalTo(SnapshotState.SUCCESS));
    client().admin().indices().prepareClose(INDEX_NAME).execute().actionGet();
    logger.info("--> restore");
    RestoreSnapshotResponse restoreSnapshotResponse = client().admin().cluster().prepareRestoreSnapshot(REPO_NAME, SNAP_NAME).setWaitForCompletion(true).execute().actionGet();
    int totalShards = restoreSnapshotResponse.getRestoreInfo().totalShards();
    assertThat(totalShards, greaterThan(0));
    ensureGreen();
    logger.info("--> request recoveries");
    RecoveryResponse response = client().admin().indices().prepareRecoveries(INDEX_NAME).execute().actionGet();
    Repository repository = internalCluster().getMasterNodeInstance(RepositoriesService.class).repository(REPO_NAME);
    final RepositoryData repositoryData = PlainActionFuture.get(repository::getRepositoryData);
    for (Map.Entry<String, List<RecoveryState>> indexRecoveryStates : response.shardRecoveryStates().entrySet()) {
        assertThat(indexRecoveryStates.getKey(), equalTo(INDEX_NAME));
        List<RecoveryState> recoveryStates = indexRecoveryStates.getValue();
        assertThat(recoveryStates.size(), equalTo(totalShards));
        for (RecoveryState recoveryState : recoveryStates) {
            SnapshotRecoverySource recoverySource = new SnapshotRecoverySource(((SnapshotRecoverySource) recoveryState.getRecoverySource()).restoreUUID(), new Snapshot(REPO_NAME, createSnapshotResponse.getSnapshotInfo().snapshotId()), Version.CURRENT, repositoryData.resolveIndexId(INDEX_NAME));
            assertRecoveryState(recoveryState, 0, recoverySource, true, Stage.DONE, null, nodeA);
            validateIndexRecoveryState(recoveryState.getIndex());
        }
    }
}
Also used : RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse) RecoveryResponse(org.opensearch.action.admin.indices.recovery.RecoveryResponse) RepositoryData(org.opensearch.repositories.RepositoryData) Snapshot(org.opensearch.snapshots.Snapshot) Repository(org.opensearch.repositories.Repository) SnapshotRecoverySource(org.opensearch.cluster.routing.RecoverySource.SnapshotRecoverySource) CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) RepositoriesService(org.opensearch.repositories.RepositoriesService) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap)

Example 2 with Snapshot

use of org.opensearch.snapshots.Snapshot in project OpenSearch by opensearch-project.

the class ExceptionSerializationTests method testSnapshotException.

public void testSnapshotException() throws IOException {
    final Snapshot snapshot = new Snapshot("repo", new SnapshotId("snap", UUIDs.randomBase64UUID()));
    SnapshotException ex = serialize(new SnapshotException(snapshot, "no such snapshot", new NullPointerException()));
    assertEquals(ex.getRepositoryName(), snapshot.getRepository());
    assertEquals(ex.getSnapshotName(), snapshot.getSnapshotId().getName());
    assertEquals(ex.getMessage(), "[" + snapshot + "] no such snapshot");
    assertTrue(ex.getCause() instanceof NullPointerException);
    ex = serialize(new SnapshotException(null, "no such snapshot", new NullPointerException()));
    assertNull(ex.getRepositoryName());
    assertNull(ex.getSnapshotName());
    assertEquals(ex.getMessage(), "[_na] no such snapshot");
    assertTrue(ex.getCause() instanceof NullPointerException);
}
Also used : Snapshot(org.opensearch.snapshots.Snapshot) SnapshotId(org.opensearch.snapshots.SnapshotId) SnapshotException(org.opensearch.snapshots.SnapshotException)

Example 3 with Snapshot

use of org.opensearch.snapshots.Snapshot in project OpenSearch by opensearch-project.

the class SnapshotStatusTests method createTestInstance.

@Override
protected SnapshotStatus createTestInstance() {
    SnapshotsInProgress.State state = randomFrom(SnapshotsInProgress.State.values());
    String uuid = UUIDs.randomBase64UUID();
    SnapshotId id = new SnapshotId("test-snap", uuid);
    Snapshot snapshot = new Snapshot("test-repo", id);
    SnapshotIndexShardStatusTests builder = new SnapshotIndexShardStatusTests();
    builder.createTestInstance();
    List<SnapshotIndexShardStatus> snapshotIndexShardStatuses = new ArrayList<>();
    for (int idx = 0; idx < randomIntBetween(0, 10); idx++) {
        SnapshotIndexShardStatus snapshotIndexShardStatus = builder.createTestInstance();
        snapshotIndexShardStatuses.add(snapshotIndexShardStatus);
    }
    boolean includeGlobalState = randomBoolean();
    return new SnapshotStatus(snapshot, state, snapshotIndexShardStatuses, includeGlobalState, 0L, 0L);
}
Also used : SnapshotId(org.opensearch.snapshots.SnapshotId) Snapshot(org.opensearch.snapshots.Snapshot) ArrayList(java.util.ArrayList) SnapshotsInProgress(org.opensearch.cluster.SnapshotsInProgress)

Example 4 with Snapshot

use of org.opensearch.snapshots.Snapshot in project OpenSearch by opensearch-project.

the class MetadataIndexStateServiceTests method addRestoredIndex.

private static ClusterState addRestoredIndex(final String index, final int numShards, final int numReplicas, final ClusterState state) {
    ClusterState newState = addOpenedIndex(index, numShards, numReplicas, state);
    final ImmutableOpenMap.Builder<ShardId, RestoreInProgress.ShardRestoreStatus> shardsBuilder = ImmutableOpenMap.builder();
    for (ShardRouting shardRouting : newState.routingTable().index(index).randomAllActiveShardsIt()) {
        shardsBuilder.put(shardRouting.shardId(), new RestoreInProgress.ShardRestoreStatus(shardRouting.currentNodeId()));
    }
    final Snapshot snapshot = new Snapshot(randomAlphaOfLength(10), new SnapshotId(randomAlphaOfLength(5), randomAlphaOfLength(5)));
    final RestoreInProgress.Entry entry = new RestoreInProgress.Entry("_uuid", snapshot, RestoreInProgress.State.INIT, Collections.singletonList(index), shardsBuilder.build());
    return ClusterState.builder(newState).putCustom(RestoreInProgress.TYPE, new RestoreInProgress.Builder().add(entry).build()).build();
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) ShardId(org.opensearch.index.shard.ShardId) Snapshot(org.opensearch.snapshots.Snapshot) SnapshotId(org.opensearch.snapshots.SnapshotId) RestoreInProgress(org.opensearch.cluster.RestoreInProgress) TestShardRouting.newShardRouting(org.opensearch.cluster.routing.TestShardRouting.newShardRouting) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Example 5 with Snapshot

use of org.opensearch.snapshots.Snapshot in project OpenSearch by opensearch-project.

the class DiskThresholdDeciderTests method testDiskThresholdWithSnapshotShardSizes.

public void testDiskThresholdWithSnapshotShardSizes() {
    final long shardSizeInBytes = randomBoolean() ? 10L : 50L;
    logger.info("--> using shard size [{}]", shardSizeInBytes);
    final Settings diskSettings = Settings.builder().put(DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_DISK_THRESHOLD_ENABLED_SETTING.getKey(), true).put(DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_LOW_DISK_WATERMARK_SETTING.getKey(), "90%").put(DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_HIGH_DISK_WATERMARK_SETTING.getKey(), "95%").build();
    final ImmutableOpenMap.Builder<String, DiskUsage> usagesBuilder = ImmutableOpenMap.builder();
    // 79% used
    usagesBuilder.put("node1", new DiskUsage("node1", "n1", "/dev/null", 100, 21));
    // 99% used
    usagesBuilder.put("node2", new DiskUsage("node2", "n2", "/dev/null", 100, 1));
    final ImmutableOpenMap<String, DiskUsage> usages = usagesBuilder.build();
    final ClusterInfoService clusterInfoService = () -> new DevNullClusterInfo(usages, usages, ImmutableOpenMap.of());
    final AllocationDeciders deciders = new AllocationDeciders(new HashSet<>(Arrays.asList(new RestoreInProgressAllocationDecider(), new SameShardAllocationDecider(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)), makeDecider(diskSettings))));
    final Snapshot snapshot = new Snapshot("_repository", new SnapshotId("_snapshot_name", UUIDs.randomBase64UUID(random())));
    final IndexId indexId = new IndexId("_indexid_name", UUIDs.randomBase64UUID(random()));
    final ShardId shardId = new ShardId(new Index("test", IndexMetadata.INDEX_UUID_NA_VALUE), 0);
    final Metadata metadata = Metadata.builder().put(IndexMetadata.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0).putInSyncAllocationIds(0, singleton(AllocationId.newInitializing().getId()))).build();
    final RoutingTable routingTable = RoutingTable.builder().addAsNewRestore(metadata.index("test"), new RecoverySource.SnapshotRecoverySource("_restore_uuid", snapshot, Version.CURRENT, indexId), new IntHashSet()).build();
    final ImmutableOpenMap.Builder<ShardId, RestoreInProgress.ShardRestoreStatus> shards = ImmutableOpenMap.builder();
    shards.put(shardId, new RestoreInProgress.ShardRestoreStatus("node1"));
    final RestoreInProgress.Builder restores = new RestoreInProgress.Builder().add(new RestoreInProgress.Entry("_restore_uuid", snapshot, RestoreInProgress.State.INIT, singletonList("test"), shards.build()));
    ClusterState clusterState = ClusterState.builder(new ClusterName(getTestName())).metadata(metadata).routingTable(routingTable).putCustom(RestoreInProgress.TYPE, restores.build()).nodes(// node2 is added because DiskThresholdDecider
    DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build();
    assertThat(clusterState.getRoutingNodes().shardsWithState(UNASSIGNED).stream().map(ShardRouting::unassignedInfo).allMatch(unassignedInfo -> Reason.NEW_INDEX_RESTORED.equals(unassignedInfo.getReason())), is(true));
    assertThat(clusterState.getRoutingNodes().shardsWithState(UNASSIGNED).stream().map(ShardRouting::unassignedInfo).allMatch(unassignedInfo -> AllocationStatus.NO_ATTEMPT.equals(unassignedInfo.getLastAllocationStatus())), is(true));
    assertThat(clusterState.getRoutingNodes().shardsWithState(UNASSIGNED).size(), equalTo(1));
    final AtomicReference<SnapshotShardSizeInfo> snapshotShardSizeInfoRef = new AtomicReference<>(SnapshotShardSizeInfo.EMPTY);
    final AllocationService strategy = new AllocationService(deciders, new TestGatewayAllocator(), new BalancedShardsAllocator(Settings.EMPTY), clusterInfoService, snapshotShardSizeInfoRef::get);
    // reroute triggers snapshot shard size fetching
    clusterState = strategy.reroute(clusterState, "reroute");
    logShardStates(clusterState);
    // shard cannot be assigned yet as the snapshot shard size is unknown
    assertThat(clusterState.getRoutingNodes().shardsWithState(UNASSIGNED).stream().map(ShardRouting::unassignedInfo).allMatch(unassignedInfo -> AllocationStatus.FETCHING_SHARD_DATA.equals(unassignedInfo.getLastAllocationStatus())), is(true));
    assertThat(clusterState.getRoutingNodes().shardsWithState(UNASSIGNED).size(), equalTo(1));
    final SnapshotShard snapshotShard = new SnapshotShard(snapshot, indexId, shardId);
    final ImmutableOpenMap.Builder<SnapshotShard, Long> snapshotShardSizes = ImmutableOpenMap.builder();
    final boolean shouldAllocate;
    if (randomBoolean()) {
        logger.info("--> simulating snapshot shards size retrieval success");
        snapshotShardSizes.put(snapshotShard, shardSizeInBytes);
        logger.info("--> shard allocation depends on its size");
        shouldAllocate = shardSizeInBytes < usages.get("node1").getFreeBytes();
    } else {
        logger.info("--> simulating snapshot shards size retrieval failure");
        snapshotShardSizes.put(snapshotShard, ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE);
        logger.info("--> shard is always allocated when its size could not be retrieved");
        shouldAllocate = true;
    }
    snapshotShardSizeInfoRef.set(new SnapshotShardSizeInfo(snapshotShardSizes.build()));
    // reroute uses the previous snapshot shard size
    clusterState = startInitializingShardsAndReroute(strategy, clusterState);
    logShardStates(clusterState);
    assertThat(clusterState.getRoutingNodes().shardsWithState(UNASSIGNED).size(), equalTo(shouldAllocate ? 0 : 1));
    assertThat(clusterState.getRoutingNodes().shardsWithState("test", INITIALIZING, STARTED).size(), equalTo(shouldAllocate ? 1 : 0));
}
Also used : ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) Arrays(java.util.Arrays) INITIALIZING(org.opensearch.cluster.routing.ShardRoutingState.INITIALIZING) Metadata(org.opensearch.cluster.metadata.Metadata) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService) Version(org.opensearch.Version) AllocationCommand(org.opensearch.cluster.routing.allocation.command.AllocationCommand) MoveAllocationCommand(org.opensearch.cluster.routing.allocation.command.MoveAllocationCommand) Collections.singletonList(java.util.Collections.singletonList) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) IndexId(org.opensearch.repositories.IndexId) Collections.singleton(java.util.Collections.singleton) OpenSearchAllocationTestCase(org.opensearch.cluster.OpenSearchAllocationTestCase) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) BalancedShardsAllocator(org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) DiskUsage(org.opensearch.cluster.DiskUsage) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) ClusterInfo(org.opensearch.cluster.ClusterInfo) Reason(org.opensearch.cluster.routing.UnassignedInfo.Reason) Index(org.opensearch.index.Index) SnapshotId(org.opensearch.snapshots.SnapshotId) Settings(org.opensearch.common.settings.Settings) DiscoveryNodeRole(org.opensearch.cluster.node.DiscoveryNodeRole) RELOCATING(org.opensearch.cluster.routing.ShardRoutingState.RELOCATING) List(java.util.List) UNASSIGNED(org.opensearch.cluster.routing.ShardRoutingState.UNASSIGNED) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.is(org.hamcrest.Matchers.is) RoutingNode(org.opensearch.cluster.routing.RoutingNode) Matchers.containsString(org.hamcrest.Matchers.containsString) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) SnapshotShard(org.opensearch.snapshots.InternalSnapshotsInfoService.SnapshotShard) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AllocationStatus(org.opensearch.cluster.routing.UnassignedInfo.AllocationStatus) ClusterInfoService(org.opensearch.cluster.ClusterInfoService) RoutingAllocation(org.opensearch.cluster.routing.allocation.RoutingAllocation) HashMap(java.util.HashMap) DiskThresholdSettings(org.opensearch.cluster.routing.allocation.DiskThresholdSettings) AtomicReference(java.util.concurrent.atomic.AtomicReference) RecoverySource(org.opensearch.cluster.routing.RecoverySource) HashSet(java.util.HashSet) CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING(org.opensearch.cluster.routing.allocation.decider.EnableAllocationDecider.CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) ClusterState(org.opensearch.cluster.ClusterState) STARTED(org.opensearch.cluster.routing.ShardRoutingState.STARTED) SnapshotShardSizeInfo(org.opensearch.snapshots.SnapshotShardSizeInfo) RoutingNodes(org.opensearch.cluster.routing.RoutingNodes) ShardRoutingState(org.opensearch.cluster.routing.ShardRoutingState) UUIDs(org.opensearch.common.UUIDs) ClusterSettings(org.opensearch.common.settings.ClusterSettings) TestGatewayAllocator(org.opensearch.test.gateway.TestGatewayAllocator) Collections.emptyMap(java.util.Collections.emptyMap) Matchers.oneOf(org.hamcrest.Matchers.oneOf) AllocationCommands(org.opensearch.cluster.routing.allocation.command.AllocationCommands) AllocationId(org.opensearch.cluster.routing.AllocationId) IntHashSet(com.carrotsearch.hppc.IntHashSet) ShardRouting(org.opensearch.cluster.routing.ShardRouting) ShardId(org.opensearch.index.shard.ShardId) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting) EmptySnapshotsInfoService(org.opensearch.snapshots.EmptySnapshotsInfoService) Snapshot(org.opensearch.snapshots.Snapshot) ClusterName(org.opensearch.cluster.ClusterName) RestoreInProgress(org.opensearch.cluster.RestoreInProgress) RoutingTable(org.opensearch.cluster.routing.RoutingTable) ClusterSettings(org.opensearch.common.settings.ClusterSettings) SnapshotShard(org.opensearch.snapshots.InternalSnapshotsInfoService.SnapshotShard) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) IntHashSet(com.carrotsearch.hppc.IntHashSet) Index(org.opensearch.index.Index) Matchers.containsString(org.hamcrest.Matchers.containsString) DiskUsage(org.opensearch.cluster.DiskUsage) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) SnapshotShardSizeInfo(org.opensearch.snapshots.SnapshotShardSizeInfo) ShardId(org.opensearch.index.shard.ShardId) ClusterName(org.opensearch.cluster.ClusterName) Settings(org.opensearch.common.settings.Settings) DiskThresholdSettings(org.opensearch.cluster.routing.allocation.DiskThresholdSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService) TestGatewayAllocator(org.opensearch.test.gateway.TestGatewayAllocator) IndexId(org.opensearch.repositories.IndexId) ClusterState(org.opensearch.cluster.ClusterState) ClusterInfoService(org.opensearch.cluster.ClusterInfoService) BalancedShardsAllocator(org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) AtomicReference(java.util.concurrent.atomic.AtomicReference) Snapshot(org.opensearch.snapshots.Snapshot) SnapshotId(org.opensearch.snapshots.SnapshotId) RestoreInProgress(org.opensearch.cluster.RestoreInProgress) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) RoutingTable(org.opensearch.cluster.routing.RoutingTable) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting)

Aggregations

Snapshot (org.opensearch.snapshots.Snapshot)26 SnapshotId (org.opensearch.snapshots.SnapshotId)23 IndexId (org.opensearch.repositories.IndexId)18 ShardId (org.opensearch.index.shard.ShardId)15 ClusterState (org.opensearch.cluster.ClusterState)13 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)12 ShardRouting (org.opensearch.cluster.routing.ShardRouting)12 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Map (java.util.Map)7 Metadata (org.opensearch.cluster.metadata.Metadata)7 SnapshotRecoverySource (org.opensearch.cluster.routing.RecoverySource.SnapshotRecoverySource)7 HashSet (java.util.HashSet)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)6 Arrays (java.util.Arrays)5 Collections (java.util.Collections)5 Collectors (java.util.stream.Collectors)5 SnapshotsInProgress (org.opensearch.cluster.SnapshotsInProgress)5