Search in sources :

Example 1 with NodeVersionAllocationDecider

use of org.opensearch.cluster.routing.allocation.decider.NodeVersionAllocationDecider in project OpenSearch by opensearch-project.

the class ClusterModule method createAllocationDeciders.

// TODO: this is public so allocation benchmark can access the default deciders...can we do that in another way?
/**
 * Return a new {@link AllocationDecider} instance with builtin deciders as well as those from plugins.
 */
public static Collection<AllocationDecider> createAllocationDeciders(Settings settings, ClusterSettings clusterSettings, List<ClusterPlugin> clusterPlugins) {
    // collect deciders by class so that we can detect duplicates
    Map<Class, AllocationDecider> deciders = new LinkedHashMap<>();
    addAllocationDecider(deciders, new MaxRetryAllocationDecider());
    addAllocationDecider(deciders, new ResizeAllocationDecider());
    addAllocationDecider(deciders, new ReplicaAfterPrimaryActiveAllocationDecider());
    addAllocationDecider(deciders, new RebalanceOnlyWhenActiveAllocationDecider());
    addAllocationDecider(deciders, new ClusterRebalanceAllocationDecider(settings, clusterSettings));
    addAllocationDecider(deciders, new ConcurrentRebalanceAllocationDecider(settings, clusterSettings));
    addAllocationDecider(deciders, new ConcurrentRecoveriesAllocationDecider(settings, clusterSettings));
    addAllocationDecider(deciders, new EnableAllocationDecider(settings, clusterSettings));
    addAllocationDecider(deciders, new NodeVersionAllocationDecider());
    addAllocationDecider(deciders, new SnapshotInProgressAllocationDecider());
    addAllocationDecider(deciders, new RestoreInProgressAllocationDecider());
    addAllocationDecider(deciders, new FilterAllocationDecider(settings, clusterSettings));
    addAllocationDecider(deciders, new SameShardAllocationDecider(settings, clusterSettings));
    addAllocationDecider(deciders, new DiskThresholdDecider(settings, clusterSettings));
    addAllocationDecider(deciders, new ThrottlingAllocationDecider(settings, clusterSettings));
    addAllocationDecider(deciders, new ShardsLimitAllocationDecider(settings, clusterSettings));
    addAllocationDecider(deciders, new AwarenessAllocationDecider(settings, clusterSettings));
    addAllocationDecider(deciders, new NodeLoadAwareAllocationDecider(settings, clusterSettings));
    clusterPlugins.stream().flatMap(p -> p.createAllocationDeciders(settings, clusterSettings).stream()).forEach(d -> addAllocationDecider(deciders, d));
    return deciders.values();
}
Also used : IndexGraveyard(org.opensearch.cluster.metadata.IndexGraveyard) NamedWriteable(org.opensearch.common.io.stream.NamedWriteable) Metadata(org.opensearch.cluster.metadata.Metadata) EnableAllocationDecider(org.opensearch.cluster.routing.allocation.decider.EnableAllocationDecider) MetadataDeleteIndexService(org.opensearch.cluster.metadata.MetadataDeleteIndexService) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService) ClusterRebalanceAllocationDecider(org.opensearch.cluster.routing.allocation.decider.ClusterRebalanceAllocationDecider) SnapshotInProgressAllocationDecider(org.opensearch.cluster.routing.allocation.decider.SnapshotInProgressAllocationDecider) ThrottlingAllocationDecider(org.opensearch.cluster.routing.allocation.decider.ThrottlingAllocationDecider) Entry(org.opensearch.common.io.stream.NamedWriteableRegistry.Entry) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) ComponentTemplateMetadata(org.opensearch.cluster.metadata.ComponentTemplateMetadata) DelayedAllocationService(org.opensearch.cluster.routing.DelayedAllocationService) AllocationDecider(org.opensearch.cluster.routing.allocation.decider.AllocationDecider) ComposableIndexTemplateMetadata(org.opensearch.cluster.metadata.ComposableIndexTemplateMetadata) Property(org.opensearch.common.settings.Setting.Property) Map(java.util.Map) BalancedShardsAllocator(org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) ShardStateAction(org.opensearch.cluster.action.shard.ShardStateAction) ShardsAllocator(org.opensearch.cluster.routing.allocation.allocator.ShardsAllocator) TaskResultsService(org.opensearch.tasks.TaskResultsService) AllocationDeciders(org.opensearch.cluster.routing.allocation.decider.AllocationDeciders) DiskThresholdDecider(org.opensearch.cluster.routing.allocation.decider.DiskThresholdDecider) ResizeAllocationDecider(org.opensearch.cluster.routing.allocation.decider.ResizeAllocationDecider) SameShardAllocationDecider(org.opensearch.cluster.routing.allocation.decider.SameShardAllocationDecider) ClusterPlugin(org.opensearch.plugins.ClusterPlugin) AwarenessAllocationDecider(org.opensearch.cluster.routing.allocation.decider.AwarenessAllocationDecider) Collection(java.util.Collection) PersistentTasksCustomMetadata(org.opensearch.persistent.PersistentTasksCustomMetadata) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) Task(org.opensearch.tasks.Task) PersistentTasksNodeService(org.opensearch.persistent.PersistentTasksNodeService) Objects(java.util.Objects) List(java.util.List) DataStreamMetadata(org.opensearch.cluster.metadata.DataStreamMetadata) ExistingShardsAllocator(org.opensearch.cluster.routing.allocation.ExistingShardsAllocator) GatewayAllocator(org.opensearch.gateway.GatewayAllocator) Reader(org.opensearch.common.io.stream.Writeable.Reader) ScriptMetadata(org.opensearch.script.ScriptMetadata) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) SnapshotsInfoService(org.opensearch.snapshots.SnapshotsInfoService) RestoreInProgressAllocationDecider(org.opensearch.cluster.routing.allocation.decider.RestoreInProgressAllocationDecider) ConcurrentRecoveriesAllocationDecider(org.opensearch.cluster.routing.allocation.decider.ConcurrentRecoveriesAllocationDecider) MetadataIndexAliasesService(org.opensearch.cluster.metadata.MetadataIndexAliasesService) MetadataUpdateSettingsService(org.opensearch.cluster.metadata.MetadataUpdateSettingsService) HashMap(java.util.HashMap) ParseField(org.opensearch.common.ParseField) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) MappingUpdatedAction(org.opensearch.cluster.action.index.MappingUpdatedAction) NodeVersionAllocationDecider(org.opensearch.cluster.routing.allocation.decider.NodeVersionAllocationDecider) LinkedHashMap(java.util.LinkedHashMap) ShardsLimitAllocationDecider(org.opensearch.cluster.routing.allocation.decider.ShardsLimitAllocationDecider) MetadataMappingService(org.opensearch.cluster.metadata.MetadataMappingService) AbstractModule(org.opensearch.common.inject.AbstractModule) NodeMappingRefreshAction(org.opensearch.cluster.action.index.NodeMappingRefreshAction) ClusterSettings(org.opensearch.common.settings.ClusterSettings) ReplicaAfterPrimaryActiveAllocationDecider(org.opensearch.cluster.routing.allocation.decider.ReplicaAfterPrimaryActiveAllocationDecider) MetadataIndexStateService(org.opensearch.cluster.metadata.MetadataIndexStateService) RebalanceOnlyWhenActiveAllocationDecider(org.opensearch.cluster.routing.allocation.decider.RebalanceOnlyWhenActiveAllocationDecider) IngestMetadata(org.opensearch.ingest.IngestMetadata) FilterAllocationDecider(org.opensearch.cluster.routing.allocation.decider.FilterAllocationDecider) MetadataIndexTemplateService(org.opensearch.cluster.metadata.MetadataIndexTemplateService) Setting(org.opensearch.common.settings.Setting) RepositoriesMetadata(org.opensearch.cluster.metadata.RepositoriesMetadata) MaxRetryAllocationDecider(org.opensearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider) Sets(org.opensearch.common.util.set.Sets) ConcurrentRebalanceAllocationDecider(org.opensearch.cluster.routing.allocation.decider.ConcurrentRebalanceAllocationDecider) NodeLoadAwareAllocationDecider(org.opensearch.cluster.routing.allocation.decider.NodeLoadAwareAllocationDecider) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ClusterService(org.opensearch.cluster.service.ClusterService) Collections(java.util.Collections) ResizeAllocationDecider(org.opensearch.cluster.routing.allocation.decider.ResizeAllocationDecider) SnapshotInProgressAllocationDecider(org.opensearch.cluster.routing.allocation.decider.SnapshotInProgressAllocationDecider) EnableAllocationDecider(org.opensearch.cluster.routing.allocation.decider.EnableAllocationDecider) NodeLoadAwareAllocationDecider(org.opensearch.cluster.routing.allocation.decider.NodeLoadAwareAllocationDecider) RebalanceOnlyWhenActiveAllocationDecider(org.opensearch.cluster.routing.allocation.decider.RebalanceOnlyWhenActiveAllocationDecider) ThrottlingAllocationDecider(org.opensearch.cluster.routing.allocation.decider.ThrottlingAllocationDecider) MaxRetryAllocationDecider(org.opensearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider) ReplicaAfterPrimaryActiveAllocationDecider(org.opensearch.cluster.routing.allocation.decider.ReplicaAfterPrimaryActiveAllocationDecider) ConcurrentRecoveriesAllocationDecider(org.opensearch.cluster.routing.allocation.decider.ConcurrentRecoveriesAllocationDecider) LinkedHashMap(java.util.LinkedHashMap) ClusterRebalanceAllocationDecider(org.opensearch.cluster.routing.allocation.decider.ClusterRebalanceAllocationDecider) SameShardAllocationDecider(org.opensearch.cluster.routing.allocation.decider.SameShardAllocationDecider) RestoreInProgressAllocationDecider(org.opensearch.cluster.routing.allocation.decider.RestoreInProgressAllocationDecider) FilterAllocationDecider(org.opensearch.cluster.routing.allocation.decider.FilterAllocationDecider) DiskThresholdDecider(org.opensearch.cluster.routing.allocation.decider.DiskThresholdDecider) ShardsLimitAllocationDecider(org.opensearch.cluster.routing.allocation.decider.ShardsLimitAllocationDecider) AwarenessAllocationDecider(org.opensearch.cluster.routing.allocation.decider.AwarenessAllocationDecider) NodeVersionAllocationDecider(org.opensearch.cluster.routing.allocation.decider.NodeVersionAllocationDecider) EnableAllocationDecider(org.opensearch.cluster.routing.allocation.decider.EnableAllocationDecider) ClusterRebalanceAllocationDecider(org.opensearch.cluster.routing.allocation.decider.ClusterRebalanceAllocationDecider) SnapshotInProgressAllocationDecider(org.opensearch.cluster.routing.allocation.decider.SnapshotInProgressAllocationDecider) ThrottlingAllocationDecider(org.opensearch.cluster.routing.allocation.decider.ThrottlingAllocationDecider) AllocationDecider(org.opensearch.cluster.routing.allocation.decider.AllocationDecider) ResizeAllocationDecider(org.opensearch.cluster.routing.allocation.decider.ResizeAllocationDecider) SameShardAllocationDecider(org.opensearch.cluster.routing.allocation.decider.SameShardAllocationDecider) AwarenessAllocationDecider(org.opensearch.cluster.routing.allocation.decider.AwarenessAllocationDecider) RestoreInProgressAllocationDecider(org.opensearch.cluster.routing.allocation.decider.RestoreInProgressAllocationDecider) ConcurrentRecoveriesAllocationDecider(org.opensearch.cluster.routing.allocation.decider.ConcurrentRecoveriesAllocationDecider) NodeVersionAllocationDecider(org.opensearch.cluster.routing.allocation.decider.NodeVersionAllocationDecider) ShardsLimitAllocationDecider(org.opensearch.cluster.routing.allocation.decider.ShardsLimitAllocationDecider) ReplicaAfterPrimaryActiveAllocationDecider(org.opensearch.cluster.routing.allocation.decider.ReplicaAfterPrimaryActiveAllocationDecider) RebalanceOnlyWhenActiveAllocationDecider(org.opensearch.cluster.routing.allocation.decider.RebalanceOnlyWhenActiveAllocationDecider) FilterAllocationDecider(org.opensearch.cluster.routing.allocation.decider.FilterAllocationDecider) MaxRetryAllocationDecider(org.opensearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider) ConcurrentRebalanceAllocationDecider(org.opensearch.cluster.routing.allocation.decider.ConcurrentRebalanceAllocationDecider) NodeLoadAwareAllocationDecider(org.opensearch.cluster.routing.allocation.decider.NodeLoadAwareAllocationDecider) ConcurrentRebalanceAllocationDecider(org.opensearch.cluster.routing.allocation.decider.ConcurrentRebalanceAllocationDecider)

Example 2 with NodeVersionAllocationDecider

use of org.opensearch.cluster.routing.allocation.decider.NodeVersionAllocationDecider in project OpenSearch by opensearch-project.

the class NodeVersionAllocationDeciderTests method testRebalanceDoesNotAllocatePrimaryAndReplicasOnDifferentVersionNodes.

public void testRebalanceDoesNotAllocatePrimaryAndReplicasOnDifferentVersionNodes() {
    ShardId shard1 = new ShardId("test1", "_na_", 0);
    ShardId shard2 = new ShardId("test2", "_na_", 0);
    final DiscoveryNode newNode = new DiscoveryNode("newNode", buildNewFakeTransportAddress(), emptyMap(), MASTER_DATA_ROLES, Version.CURRENT);
    final DiscoveryNode oldNode1 = new DiscoveryNode("oldNode1", buildNewFakeTransportAddress(), emptyMap(), MASTER_DATA_ROLES, VersionUtils.getPreviousVersion());
    final DiscoveryNode oldNode2 = new DiscoveryNode("oldNode2", buildNewFakeTransportAddress(), emptyMap(), MASTER_DATA_ROLES, VersionUtils.getPreviousVersion());
    AllocationId allocationId1P = AllocationId.newInitializing();
    AllocationId allocationId1R = AllocationId.newInitializing();
    AllocationId allocationId2P = AllocationId.newInitializing();
    AllocationId allocationId2R = AllocationId.newInitializing();
    Metadata metadata = Metadata.builder().put(IndexMetadata.builder(shard1.getIndexName()).settings(settings(Version.CURRENT).put(Settings.EMPTY)).numberOfShards(1).numberOfReplicas(1).putInSyncAllocationIds(0, Sets.newHashSet(allocationId1P.getId(), allocationId1R.getId()))).put(IndexMetadata.builder(shard2.getIndexName()).settings(settings(Version.CURRENT).put(Settings.EMPTY)).numberOfShards(1).numberOfReplicas(1).putInSyncAllocationIds(0, Sets.newHashSet(allocationId2P.getId(), allocationId2R.getId()))).build();
    RoutingTable routingTable = RoutingTable.builder().add(IndexRoutingTable.builder(shard1.getIndex()).addIndexShard(new IndexShardRoutingTable.Builder(shard1).addShard(TestShardRouting.newShardRouting(shard1.getIndexName(), shard1.getId(), newNode.getId(), null, true, ShardRoutingState.STARTED, allocationId1P)).addShard(TestShardRouting.newShardRouting(shard1.getIndexName(), shard1.getId(), oldNode1.getId(), null, false, ShardRoutingState.STARTED, allocationId1R)).build())).add(IndexRoutingTable.builder(shard2.getIndex()).addIndexShard(new IndexShardRoutingTable.Builder(shard2).addShard(TestShardRouting.newShardRouting(shard2.getIndexName(), shard2.getId(), newNode.getId(), null, true, ShardRoutingState.STARTED, allocationId2P)).addShard(TestShardRouting.newShardRouting(shard2.getIndexName(), shard2.getId(), oldNode1.getId(), null, false, ShardRoutingState.STARTED, allocationId2R)).build())).build();
    ClusterState state = ClusterState.builder(org.opensearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metadata(metadata).routingTable(routingTable).nodes(DiscoveryNodes.builder().add(newNode).add(oldNode1).add(oldNode2)).build();
    AllocationDeciders allocationDeciders = new AllocationDeciders(Collections.singleton(new NodeVersionAllocationDecider()));
    AllocationService strategy = new MockAllocationService(allocationDeciders, new TestGatewayAllocator(), new BalancedShardsAllocator(Settings.EMPTY), EmptyClusterInfoService.INSTANCE, EmptySnapshotsInfoService.INSTANCE);
    state = strategy.reroute(state, new AllocationCommands(), true, false).getClusterState();
    // the two indices must stay as is, the replicas cannot move to oldNode2 because versions don't match
    assertThat(state.routingTable().index(shard2.getIndex()).shardsWithState(ShardRoutingState.RELOCATING).size(), equalTo(0));
    assertThat(state.routingTable().index(shard1.getIndex()).shardsWithState(ShardRoutingState.RELOCATING).size(), equalTo(0));
}
Also used : TestGatewayAllocator(org.opensearch.test.gateway.TestGatewayAllocator) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) BalancedShardsAllocator(org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) AllocationId(org.opensearch.cluster.routing.AllocationId) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AllocationDeciders(org.opensearch.cluster.routing.allocation.decider.AllocationDeciders) AllocationCommands(org.opensearch.cluster.routing.allocation.command.AllocationCommands) ShardId(org.opensearch.index.shard.ShardId) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) RoutingTable(org.opensearch.cluster.routing.RoutingTable) NodeVersionAllocationDecider(org.opensearch.cluster.routing.allocation.decider.NodeVersionAllocationDecider)

Example 3 with NodeVersionAllocationDecider

use of org.opensearch.cluster.routing.allocation.decider.NodeVersionAllocationDecider in project OpenSearch by opensearch-project.

the class NodeVersionAllocationDeciderTests method testMessages.

public void testMessages() {
    Metadata metadata = Metadata.builder().put(IndexMetadata.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)).build();
    RoutingTable initialRoutingTable = RoutingTable.builder().addAsNew(metadata.index("test")).build();
    RoutingNode newNode = new RoutingNode("newNode", newNode("newNode", Version.CURRENT));
    RoutingNode oldNode = new RoutingNode("oldNode", newNode("oldNode", VersionUtils.getPreviousVersion()));
    final org.opensearch.cluster.ClusterName clusterName = org.opensearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY);
    ClusterState clusterState = ClusterState.builder(clusterName).metadata(metadata).routingTable(initialRoutingTable).nodes(DiscoveryNodes.builder().add(newNode.node()).add(oldNode.node())).build();
    final ShardId shardId = clusterState.routingTable().index("test").shard(0).getShardId();
    final ShardRouting primaryShard = clusterState.routingTable().shardRoutingTable(shardId).primaryShard();
    final ShardRouting replicaShard = clusterState.routingTable().shardRoutingTable(shardId).replicaShards().get(0);
    RoutingAllocation routingAllocation = new RoutingAllocation(null, clusterState.getRoutingNodes(), clusterState, null, null, 0);
    routingAllocation.debugDecision(true);
    final NodeVersionAllocationDecider allocationDecider = new NodeVersionAllocationDecider();
    Decision decision = allocationDecider.canAllocate(primaryShard, newNode, routingAllocation);
    assertThat(decision.type(), is(Decision.Type.YES));
    assertThat(decision.getExplanation(), is("the primary shard is new or already existed on the node"));
    decision = allocationDecider.canAllocate(ShardRoutingHelper.initialize(primaryShard, "oldNode"), newNode, routingAllocation);
    assertThat(decision.type(), is(Decision.Type.YES));
    assertThat(decision.getExplanation(), is("can relocate primary shard from a node with version [" + oldNode.node().getVersion() + "] to a node with equal-or-newer version [" + newNode.node().getVersion() + "]"));
    decision = allocationDecider.canAllocate(ShardRoutingHelper.initialize(primaryShard, "newNode"), oldNode, routingAllocation);
    assertThat(decision.type(), is(Decision.Type.NO));
    assertThat(decision.getExplanation(), is("cannot relocate primary shard from a node with version [" + newNode.node().getVersion() + "] to a node with older version [" + oldNode.node().getVersion() + "]"));
    final IndexId indexId = new IndexId("test", UUIDs.randomBase64UUID(random()));
    final SnapshotRecoverySource newVersionSnapshot = new SnapshotRecoverySource(UUIDs.randomBase64UUID(), new Snapshot("rep1", new SnapshotId("snp1", UUIDs.randomBase64UUID())), newNode.node().getVersion(), indexId);
    final SnapshotRecoverySource oldVersionSnapshot = new SnapshotRecoverySource(UUIDs.randomBase64UUID(), new Snapshot("rep1", new SnapshotId("snp1", UUIDs.randomBase64UUID())), oldNode.node().getVersion(), indexId);
    decision = allocationDecider.canAllocate(ShardRoutingHelper.newWithRestoreSource(primaryShard, newVersionSnapshot), oldNode, routingAllocation);
    assertThat(decision.type(), is(Decision.Type.NO));
    assertThat(decision.getExplanation(), is("node version [" + oldNode.node().getVersion() + "] is older than the snapshot version [" + newNode.node().getVersion() + "]"));
    decision = allocationDecider.canAllocate(ShardRoutingHelper.newWithRestoreSource(primaryShard, oldVersionSnapshot), newNode, routingAllocation);
    assertThat(decision.type(), is(Decision.Type.YES));
    assertThat(decision.getExplanation(), is("node version [" + newNode.node().getVersion() + "] is the same or newer than snapshot version [" + oldNode.node().getVersion() + "]"));
    final RoutingChangesObserver routingChangesObserver = new RoutingChangesObserver.AbstractRoutingChangesObserver();
    final RoutingNodes routingNodes = new RoutingNodes(clusterState, false);
    final ShardRouting startedPrimary = routingNodes.startShard(logger, routingNodes.initializeShard(primaryShard, "newNode", null, 0, routingChangesObserver), routingChangesObserver);
    routingAllocation = new RoutingAllocation(null, routingNodes, clusterState, null, null, 0);
    routingAllocation.debugDecision(true);
    decision = allocationDecider.canAllocate(replicaShard, oldNode, routingAllocation);
    assertThat(decision.type(), is(Decision.Type.NO));
    assertThat(decision.getExplanation(), is("cannot allocate replica shard to a node with version [" + oldNode.node().getVersion() + "] since this is older than the primary version [" + newNode.node().getVersion() + "]"));
    routingNodes.startShard(logger, routingNodes.relocateShard(startedPrimary, "oldNode", 0, routingChangesObserver).v2(), routingChangesObserver);
    routingAllocation = new RoutingAllocation(null, routingNodes, clusterState, null, null, 0);
    routingAllocation.debugDecision(true);
    decision = allocationDecider.canAllocate(replicaShard, newNode, routingAllocation);
    assertThat(decision.type(), is(Decision.Type.YES));
    assertThat(decision.getExplanation(), is("can allocate replica shard to a node with version [" + newNode.node().getVersion() + "] since this is equal-or-newer than the primary version [" + oldNode.node().getVersion() + "]"));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) IndexId(org.opensearch.repositories.IndexId) RoutingNodes(org.opensearch.cluster.routing.RoutingNodes) RoutingChangesObserver(org.opensearch.cluster.routing.RoutingChangesObserver) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Decision(org.opensearch.cluster.routing.allocation.decider.Decision) ShardId(org.opensearch.index.shard.ShardId) Snapshot(org.opensearch.snapshots.Snapshot) SnapshotId(org.opensearch.snapshots.SnapshotId) SnapshotRecoverySource(org.opensearch.cluster.routing.RecoverySource.SnapshotRecoverySource) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) RoutingTable(org.opensearch.cluster.routing.RoutingTable) RoutingNode(org.opensearch.cluster.routing.RoutingNode) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting) NodeVersionAllocationDecider(org.opensearch.cluster.routing.allocation.decider.NodeVersionAllocationDecider)

Example 4 with NodeVersionAllocationDecider

use of org.opensearch.cluster.routing.allocation.decider.NodeVersionAllocationDecider in project OpenSearch by opensearch-project.

the class NodeVersionAllocationDeciderTests method testRestoreDoesNotAllocateSnapshotOnOlderNodes.

public void testRestoreDoesNotAllocateSnapshotOnOlderNodes() {
    final DiscoveryNode newNode = new DiscoveryNode("newNode", buildNewFakeTransportAddress(), emptyMap(), MASTER_DATA_ROLES, Version.CURRENT);
    final DiscoveryNode oldNode1 = new DiscoveryNode("oldNode1", buildNewFakeTransportAddress(), emptyMap(), MASTER_DATA_ROLES, VersionUtils.getPreviousVersion());
    final DiscoveryNode oldNode2 = new DiscoveryNode("oldNode2", buildNewFakeTransportAddress(), emptyMap(), MASTER_DATA_ROLES, VersionUtils.getPreviousVersion());
    final Snapshot snapshot = new Snapshot("rep1", new SnapshotId("snp1", UUIDs.randomBase64UUID()));
    final IndexId indexId = new IndexId("test", UUIDs.randomBase64UUID(random()));
    final int numberOfShards = randomIntBetween(1, 3);
    final IndexMetadata.Builder indexMetadata = IndexMetadata.builder("test").settings(settings(Version.CURRENT)).numberOfShards(numberOfShards).numberOfReplicas(randomIntBetween(0, 3));
    for (int i = 0; i < numberOfShards; i++) {
        indexMetadata.putInSyncAllocationIds(i, Collections.singleton("_test_"));
    }
    Metadata metadata = Metadata.builder().put(indexMetadata).build();
    final ImmutableOpenMap.Builder<InternalSnapshotsInfoService.SnapshotShard, Long> snapshotShardSizes = ImmutableOpenMap.builder(numberOfShards);
    final Index index = metadata.index("test").getIndex();
    for (int i = 0; i < numberOfShards; i++) {
        final ShardId shardId = new ShardId(index, i);
        snapshotShardSizes.put(new InternalSnapshotsInfoService.SnapshotShard(snapshot, indexId, shardId), randomNonNegativeLong());
    }
    ClusterState state = ClusterState.builder(org.opensearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metadata(metadata).routingTable(RoutingTable.builder().addAsRestore(metadata.index("test"), new SnapshotRecoverySource(UUIDs.randomBase64UUID(), snapshot, Version.CURRENT, indexId)).build()).nodes(DiscoveryNodes.builder().add(newNode).add(oldNode1).add(oldNode2)).build();
    AllocationDeciders allocationDeciders = new AllocationDeciders(Arrays.asList(new ReplicaAfterPrimaryActiveAllocationDecider(), new NodeVersionAllocationDecider()));
    AllocationService strategy = new MockAllocationService(allocationDeciders, new TestGatewayAllocator(), new BalancedShardsAllocator(Settings.EMPTY), EmptyClusterInfoService.INSTANCE, () -> new SnapshotShardSizeInfo(snapshotShardSizes.build()));
    state = strategy.reroute(state, new AllocationCommands(), true, false).getClusterState();
    // Make sure that primary shards are only allocated on the new node
    for (int i = 0; i < numberOfShards; i++) {
        assertEquals("newNode", state.routingTable().index("test").getShards().get(i).primaryShard().currentNodeId());
    }
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Index(org.opensearch.index.Index) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) SnapshotShardSizeInfo(org.opensearch.snapshots.SnapshotShardSizeInfo) ShardId(org.opensearch.index.shard.ShardId) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) NodeVersionAllocationDecider(org.opensearch.cluster.routing.allocation.decider.NodeVersionAllocationDecider) TestGatewayAllocator(org.opensearch.test.gateway.TestGatewayAllocator) IndexId(org.opensearch.repositories.IndexId) ClusterState(org.opensearch.cluster.ClusterState) BalancedShardsAllocator(org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) InternalSnapshotsInfoService(org.opensearch.snapshots.InternalSnapshotsInfoService) AllocationDeciders(org.opensearch.cluster.routing.allocation.decider.AllocationDeciders) ReplicaAfterPrimaryActiveAllocationDecider(org.opensearch.cluster.routing.allocation.decider.ReplicaAfterPrimaryActiveAllocationDecider) AllocationCommands(org.opensearch.cluster.routing.allocation.command.AllocationCommands) Snapshot(org.opensearch.snapshots.Snapshot) SnapshotId(org.opensearch.snapshots.SnapshotId) SnapshotRecoverySource(org.opensearch.cluster.routing.RecoverySource.SnapshotRecoverySource)

Aggregations

Metadata (org.opensearch.cluster.metadata.Metadata)4 NodeVersionAllocationDecider (org.opensearch.cluster.routing.allocation.decider.NodeVersionAllocationDecider)4 ClusterState (org.opensearch.cluster.ClusterState)3 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)3 BalancedShardsAllocator (org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator)3 AllocationDeciders (org.opensearch.cluster.routing.allocation.decider.AllocationDeciders)3 ShardId (org.opensearch.index.shard.ShardId)3 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)2 IndexRoutingTable (org.opensearch.cluster.routing.IndexRoutingTable)2 IndexShardRoutingTable (org.opensearch.cluster.routing.IndexShardRoutingTable)2 SnapshotRecoverySource (org.opensearch.cluster.routing.RecoverySource.SnapshotRecoverySource)2 RoutingTable (org.opensearch.cluster.routing.RoutingTable)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1