Search in sources :

Example 36 with Decision

use of org.elasticsearch.cluster.routing.allocation.decider.Decision in project crate by crate.

the class AutoExpandReplicas method getDesiredNumberOfReplicas.

private OptionalInt getDesiredNumberOfReplicas(IndexMetadata indexMetaData, RoutingAllocation allocation) {
    if (enabled) {
        int numMatchingDataNodes = 0;
        // Only start using new logic once all nodes are migrated to 4.4.0, avoiding disruption during an upgrade
        if (allocation.nodes().getMinNodeVersion().onOrAfter(Version.V_4_4_0)) {
            for (ObjectCursor<DiscoveryNode> cursor : allocation.nodes().getDataNodes().values()) {
                Decision decision = allocation.deciders().shouldAutoExpandToNode(indexMetaData, cursor.value, allocation);
                if (decision.type() != Decision.Type.NO) {
                    numMatchingDataNodes++;
                }
            }
        } else {
            numMatchingDataNodes = allocation.nodes().getDataNodes().size();
        }
        final int min = getMinReplicas();
        final int max = getMaxReplicas(numMatchingDataNodes);
        int numberOfReplicas = numMatchingDataNodes - 1;
        if (numberOfReplicas < min) {
            numberOfReplicas = min;
        } else if (numberOfReplicas > max) {
            numberOfReplicas = max;
        }
        if (numberOfReplicas >= min && numberOfReplicas <= max) {
            return OptionalInt.of(numberOfReplicas);
        }
    }
    return OptionalInt.empty();
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Decision(org.elasticsearch.cluster.routing.allocation.decider.Decision)

Example 37 with Decision

use of org.elasticsearch.cluster.routing.allocation.decider.Decision in project crate by crate.

the class DecommissionAllocationDeciderTest method testShouldNotBeAbleToAllocatePrimaryOntoDecommissionedNode.

@Test
public void testShouldNotBeAbleToAllocatePrimaryOntoDecommissionedNode() throws Exception {
    Settings settings = Settings.builder().put(DecommissioningService.DECOMMISSION_PREFIX + "n1", true).build();
    DecommissionAllocationDecider allocationDecider = new DecommissionAllocationDecider(settings, clusterService.getClusterSettings());
    Decision decision = allocationDecider.canAllocate(primaryShard, n1, routingAllocation);
    assertThat(decision.type(), is(Decision.Type.NO));
    decision = allocationDecider.canAllocate(primaryShard, n2, routingAllocation);
    assertThat(decision.type(), is(Decision.Type.YES));
}
Also used : Settings(org.elasticsearch.common.settings.Settings) Decision(org.elasticsearch.cluster.routing.allocation.decider.Decision) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 38 with Decision

use of org.elasticsearch.cluster.routing.allocation.decider.Decision in project crate by crate.

the class DecommissionAllocationDeciderTest method testReplicasCanRemainButCannotAllocateOnDecommissionedNodeWithPrimariesDataAvailability.

@Test
public void testReplicasCanRemainButCannotAllocateOnDecommissionedNodeWithPrimariesDataAvailability() throws Exception {
    Settings settings = Settings.builder().put(DecommissioningService.GRACEFUL_STOP_MIN_AVAILABILITY_SETTING.getKey(), "primaries").put(DecommissioningService.DECOMMISSION_PREFIX + "n1", true).build();
    DecommissionAllocationDecider allocationDecider = new DecommissionAllocationDecider(settings, clusterService.getClusterSettings());
    Decision decision = allocationDecider.canAllocate(replicaShard, n1, routingAllocation);
    assertThat(decision.type(), is(Decision.Type.NO));
    decision = allocationDecider.canRemain(replicaShard, n1, routingAllocation);
    assertThat(decision.type(), is(Decision.Type.YES));
}
Also used : Settings(org.elasticsearch.common.settings.Settings) Decision(org.elasticsearch.cluster.routing.allocation.decider.Decision) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 39 with Decision

use of org.elasticsearch.cluster.routing.allocation.decider.Decision in project crate by crate.

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 ClusterName clusterName = 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, 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 SnapshotRecoverySource newVersionSnapshot = new SnapshotRecoverySource(UUIDs.randomBase64UUID(), new Snapshot("rep1", new SnapshotId("snp1", UUIDs.randomBase64UUID())), newNode.node().getVersion(), "test");
    final SnapshotRecoverySource oldVersionSnapshot = new SnapshotRecoverySource(UUIDs.randomBase64UUID(), new Snapshot("rep1", new SnapshotId("snp1", UUIDs.randomBase64UUID())), oldNode.node().getVersion(), "test");
    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, 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, 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.elasticsearch.cluster.ClusterState) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) RoutingChangesObserver(org.elasticsearch.cluster.routing.RoutingChangesObserver) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) Decision(org.elasticsearch.cluster.routing.allocation.decider.Decision) ShardId(org.elasticsearch.index.shard.ShardId) Snapshot(org.elasticsearch.snapshots.Snapshot) SnapshotId(org.elasticsearch.snapshots.SnapshotId) SnapshotRecoverySource(org.elasticsearch.cluster.routing.RecoverySource.SnapshotRecoverySource) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) ClusterName(org.elasticsearch.cluster.ClusterName) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) NodeVersionAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.NodeVersionAllocationDecider)

Example 40 with Decision

use of org.elasticsearch.cluster.routing.allocation.decider.Decision in project crate by crate.

the class AllocateUnassignedDecisionTests method testSerialization.

public void testSerialization() throws IOException {
    DiscoveryNode node1 = new DiscoveryNode("node1", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
    DiscoveryNode node2 = new DiscoveryNode("node2", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
    Decision.Type finalDecision = randomFrom(Decision.Type.values());
    DiscoveryNode assignedNode = finalDecision == Decision.Type.YES ? node1 : null;
    List<NodeAllocationResult> nodeDecisions = new ArrayList<>();
    nodeDecisions.add(new NodeAllocationResult(node1, Decision.NO, 2));
    nodeDecisions.add(new NodeAllocationResult(node2, finalDecision == Decision.Type.YES ? Decision.YES : randomFrom(Decision.NO, Decision.THROTTLE, Decision.YES), 1));
    AllocateUnassignedDecision decision;
    if (finalDecision == Decision.Type.YES) {
        decision = AllocateUnassignedDecision.yes(assignedNode, randomBoolean() ? randomAlphaOfLength(5) : null, nodeDecisions, randomBoolean());
    } else {
        decision = AllocateUnassignedDecision.no(randomFrom(AllocationStatus.DELAYED_ALLOCATION, AllocationStatus.NO_VALID_SHARD_COPY, AllocationStatus.FETCHING_SHARD_DATA), nodeDecisions, randomBoolean());
    }
    BytesStreamOutput output = new BytesStreamOutput();
    decision.writeTo(output);
    AllocateUnassignedDecision readDecision = new AllocateUnassignedDecision(output.bytes().streamInput());
    assertEquals(decision.getTargetNode(), readDecision.getTargetNode());
    assertEquals(decision.getAllocationStatus(), readDecision.getAllocationStatus());
    assertEquals(decision.getExplanation(), readDecision.getExplanation());
    assertEquals(decision.getNodeDecisions().size(), readDecision.getNodeDecisions().size());
    assertEquals(decision.getAllocationId(), readDecision.getAllocationId());
    assertEquals(decision.getAllocationDecision(), readDecision.getAllocationDecision());
    // node2 should have the highest sort order
    assertEquals("node2", readDecision.getNodeDecisions().iterator().next().getNode().getId());
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ArrayList(java.util.ArrayList) Decision(org.elasticsearch.cluster.routing.allocation.decider.Decision) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Aggregations

Decision (org.elasticsearch.cluster.routing.allocation.decider.Decision)41 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)24 RoutingNode (org.elasticsearch.cluster.routing.RoutingNode)18 AllocateUnassignedDecision (org.elasticsearch.cluster.routing.allocation.AllocateUnassignedDecision)18 NodeAllocationResult (org.elasticsearch.cluster.routing.allocation.NodeAllocationResult)16 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)12 ArrayList (java.util.ArrayList)10 UnassignedInfo (org.elasticsearch.cluster.routing.UnassignedInfo)10 AllocationDecision (org.elasticsearch.cluster.routing.allocation.AllocationDecision)10 ShardId (org.elasticsearch.index.shard.ShardId)9 ClusterInfo (org.elasticsearch.cluster.ClusterInfo)8 ShardRoutingState (org.elasticsearch.cluster.routing.ShardRoutingState)8 MoveDecision (org.elasticsearch.cluster.routing.allocation.MoveDecision)8 XContentParser (org.elasticsearch.common.xcontent.XContentParser)8 HashMap (java.util.HashMap)7 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)6 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)5 AllocationDeciders (org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders)5 Settings (org.elasticsearch.common.settings.Settings)5 Test (org.junit.Test)5