Search in sources :

Example 1 with AllocationService

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

the class BulkRetryCoordinatorPoolTest method prepare.

@Before
public void prepare() {
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder(TEST_INDEX).settings(settings(Version.CURRENT)).numberOfShards(3).numberOfReplicas(0)).build();
    RoutingTable routingTable = RoutingTable.builder().addAsNew(metaData.index(TEST_INDEX)).build();
    ClusterState state = ClusterState.builder(org.elasticsearch.cluster.ClusterName.DEFAULT).metaData(metaData).routingTable(routingTable).build();
    state = ClusterState.builder(state).nodes(DiscoveryNodes.builder().put(newNode(NODE_IDS[0])).localNodeId(NODE_IDS[0])).build();
    AllocationService allocationService = createAllocationService();
    routingTable = allocationService.reroute(state, "test").routingTable();
    state = ClusterState.builder(state).routingTable(routingTable).build();
    ClusterService clusterService = new NoopClusterService(state);
    this.state = state;
    pool = new BulkRetryCoordinatorPool(Settings.EMPTY, clusterService, mock(ThreadPool.class));
    pool.start();
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) NoopClusterService(org.elasticsearch.test.cluster.NoopClusterService) ClusterService(org.elasticsearch.cluster.ClusterService) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) NoopClusterService(org.elasticsearch.test.cluster.NoopClusterService) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService) ESAllocationTestCase.createAllocationService(org.elasticsearch.test.ESAllocationTestCase.createAllocationService) Before(org.junit.Before)

Example 2 with AllocationService

use of org.elasticsearch.cluster.routing.allocation.AllocationService in project elasticsearch by elastic.

the class RoutingIteratorTests method testNodeSelectorRouting.

public void testNodeSelectorRouting() {
    AllocationService strategy = createAllocationService(Settings.builder().put("cluster.routing.allocation.node_concurrent_recoveries", 10).put(ClusterRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ALLOW_REBALANCE_SETTING.getKey(), "always").build());
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)).build();
    RoutingTable routingTable = RoutingTable.builder().addAsNew(metaData.index("test")).build();
    ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build();
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("fred", "node1", singletonMap("disk", "ebs"))).add(newNode("barney", "node2", singletonMap("disk", "ephemeral"))).localNodeId("node1")).build();
    clusterState = strategy.reroute(clusterState, "reroute");
    clusterState = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    ShardsIterator shardsIterator = clusterState.routingTable().index("test").shard(0).onlyNodeSelectorActiveInitializingShardsIt("disk:ebs", clusterState.nodes());
    assertThat(shardsIterator.size(), equalTo(1));
    assertThat(shardsIterator.nextOrNull().currentNodeId(), equalTo("node1"));
    shardsIterator = clusterState.routingTable().index("test").shard(0).onlyNodeSelectorActiveInitializingShardsIt("dis*:eph*", clusterState.nodes());
    assertThat(shardsIterator.size(), equalTo(1));
    assertThat(shardsIterator.nextOrNull().currentNodeId(), equalTo("node2"));
    shardsIterator = clusterState.routingTable().index("test").shard(0).onlyNodeSelectorActiveInitializingShardsIt("fred", clusterState.nodes());
    assertThat(shardsIterator.size(), equalTo(1));
    assertThat(shardsIterator.nextOrNull().currentNodeId(), equalTo("node1"));
    shardsIterator = clusterState.routingTable().index("test").shard(0).onlyNodeSelectorActiveInitializingShardsIt("bar*", clusterState.nodes());
    assertThat(shardsIterator.size(), equalTo(1));
    assertThat(shardsIterator.nextOrNull().currentNodeId(), equalTo("node2"));
    shardsIterator = clusterState.routingTable().index("test").shard(0).onlyNodeSelectorActiveInitializingShardsIt(new String[] { "disk:eph*", "disk:ebs" }, clusterState.nodes());
    assertThat(shardsIterator.size(), equalTo(2));
    assertThat(shardsIterator.nextOrNull().currentNodeId(), equalTo("node2"));
    assertThat(shardsIterator.nextOrNull().currentNodeId(), equalTo("node1"));
    shardsIterator = clusterState.routingTable().index("test").shard(0).onlyNodeSelectorActiveInitializingShardsIt(new String[] { "disk:*", "invalid_name" }, clusterState.nodes());
    assertThat(shardsIterator.size(), equalTo(2));
    assertThat(shardsIterator.nextOrNull().currentNodeId(), equalTo("node2"));
    assertThat(shardsIterator.nextOrNull().currentNodeId(), equalTo("node1"));
    shardsIterator = clusterState.routingTable().index("test").shard(0).onlyNodeSelectorActiveInitializingShardsIt(new String[] { "disk:*", "disk:*" }, clusterState.nodes());
    assertThat(shardsIterator.size(), equalTo(2));
    assertThat(shardsIterator.nextOrNull().currentNodeId(), equalTo("node2"));
    assertThat(shardsIterator.nextOrNull().currentNodeId(), equalTo("node1"));
    try {
        shardsIterator = clusterState.routingTable().index("test").shard(0).onlyNodeSelectorActiveInitializingShardsIt("welma", clusterState.nodes());
        fail("should have raised illegalArgumentException");
    } catch (IllegalArgumentException illegal) {
    //expected exception
    }
    shardsIterator = clusterState.routingTable().index("test").shard(0).onlyNodeSelectorActiveInitializingShardsIt("fred", clusterState.nodes());
    assertThat(shardsIterator.size(), equalTo(1));
    assertThat(shardsIterator.nextOrNull().currentNodeId(), equalTo("node1"));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) GroupShardsIterator(org.elasticsearch.cluster.routing.GroupShardsIterator) ShardsIterator(org.elasticsearch.cluster.routing.ShardsIterator) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService)

Example 3 with AllocationService

use of org.elasticsearch.cluster.routing.allocation.AllocationService in project elasticsearch by elastic.

the class NodeRemovalClusterStateTaskExecutorTests method testNotEnoughMasterNodesAfterRemove.

public void testNotEnoughMasterNodesAfterRemove() throws Exception {
    final ElectMasterService electMasterService = mock(ElectMasterService.class);
    when(electMasterService.hasEnoughMasterNodes(any(Iterable.class))).thenReturn(false);
    final AllocationService allocationService = mock(AllocationService.class);
    final AtomicBoolean rejoinCalled = new AtomicBoolean();
    final Consumer<String> submitRejoin = source -> rejoinCalled.set(true);
    final AtomicReference<ClusterState> remainingNodesClusterState = new AtomicReference<>();
    final ZenDiscovery.NodeRemovalClusterStateTaskExecutor executor = new ZenDiscovery.NodeRemovalClusterStateTaskExecutor(allocationService, electMasterService, submitRejoin, logger) {

        @Override
        ClusterState remainingNodesClusterState(ClusterState currentState, DiscoveryNodes.Builder remainingNodesBuilder) {
            remainingNodesClusterState.set(super.remainingNodesClusterState(currentState, remainingNodesBuilder));
            return remainingNodesClusterState.get();
        }
    };
    final DiscoveryNodes.Builder builder = DiscoveryNodes.builder();
    final int nodes = randomIntBetween(2, 16);
    final List<ZenDiscovery.NodeRemovalClusterStateTaskExecutor.Task> tasks = new ArrayList<>();
    // to ensure there is at least one removal
    boolean first = true;
    for (int i = 0; i < nodes; i++) {
        final DiscoveryNode node = node(i);
        builder.add(node);
        if (first || randomBoolean()) {
            tasks.add(new ZenDiscovery.NodeRemovalClusterStateTaskExecutor.Task(node, randomBoolean() ? "left" : "failed"));
        }
        first = false;
    }
    final ClusterState clusterState = ClusterState.builder(new ClusterName("test")).nodes(builder).build();
    final ClusterStateTaskExecutor.ClusterTasksResult<ZenDiscovery.NodeRemovalClusterStateTaskExecutor.Task> result = executor.execute(clusterState, tasks);
    verify(electMasterService).hasEnoughMasterNodes(eq(remainingNodesClusterState.get().nodes()));
    verifyNoMoreInteractions(electMasterService);
    // ensure that we did not reroute
    verifyNoMoreInteractions(allocationService);
    assertTrue(rejoinCalled.get());
    assertThat(result.resultingState, equalTo(clusterState));
    for (final ZenDiscovery.NodeRemovalClusterStateTaskExecutor.Task task : tasks) {
        assertNotNull(result.resultingState.nodes().get(task.node().getId()));
    }
}
Also used : DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterStateTaskExecutor(org.elasticsearch.cluster.ClusterStateTaskExecutor) Mockito.when(org.mockito.Mockito.when) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) Matchers.any(org.mockito.Matchers.any) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) List(java.util.List) Version(org.elasticsearch.Version) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Matchers.eq(org.mockito.Matchers.eq) StreamSupport(java.util.stream.StreamSupport) ClusterName(org.elasticsearch.cluster.ClusterName) ESTestCase(org.elasticsearch.test.ESTestCase) Mockito.mock(org.mockito.Mockito.mock) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ArrayList(java.util.ArrayList) ClusterName(org.elasticsearch.cluster.ClusterName) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) ClusterState(org.elasticsearch.cluster.ClusterState) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterStateTaskExecutor(org.elasticsearch.cluster.ClusterStateTaskExecutor)

Example 4 with AllocationService

use of org.elasticsearch.cluster.routing.allocation.AllocationService in project elasticsearch by elastic.

the class NodeRemovalClusterStateTaskExecutorTests method testRerouteAfterRemovingNodes.

public void testRerouteAfterRemovingNodes() throws Exception {
    final ElectMasterService electMasterService = mock(ElectMasterService.class);
    when(electMasterService.hasEnoughMasterNodes(any(Iterable.class))).thenReturn(true);
    final AllocationService allocationService = mock(AllocationService.class);
    when(allocationService.deassociateDeadNodes(any(ClusterState.class), eq(true), any(String.class))).thenAnswer(im -> im.getArguments()[0]);
    final Consumer<String> submitRejoin = source -> fail("rejoin should not be invoked");
    final AtomicReference<ClusterState> remainingNodesClusterState = new AtomicReference<>();
    final ZenDiscovery.NodeRemovalClusterStateTaskExecutor executor = new ZenDiscovery.NodeRemovalClusterStateTaskExecutor(allocationService, electMasterService, submitRejoin, logger) {

        @Override
        ClusterState remainingNodesClusterState(ClusterState currentState, DiscoveryNodes.Builder remainingNodesBuilder) {
            remainingNodesClusterState.set(super.remainingNodesClusterState(currentState, remainingNodesBuilder));
            return remainingNodesClusterState.get();
        }
    };
    final DiscoveryNodes.Builder builder = DiscoveryNodes.builder();
    final int nodes = randomIntBetween(2, 16);
    final List<ZenDiscovery.NodeRemovalClusterStateTaskExecutor.Task> tasks = new ArrayList<>();
    // to ensure that there is at least one removal
    boolean first = true;
    for (int i = 0; i < nodes; i++) {
        final DiscoveryNode node = node(i);
        builder.add(node);
        if (first || randomBoolean()) {
            tasks.add(new ZenDiscovery.NodeRemovalClusterStateTaskExecutor.Task(node, randomBoolean() ? "left" : "failed"));
        }
        first = false;
    }
    final ClusterState clusterState = ClusterState.builder(new ClusterName("test")).nodes(builder).build();
    final ClusterStateTaskExecutor.ClusterTasksResult<ZenDiscovery.NodeRemovalClusterStateTaskExecutor.Task> result = executor.execute(clusterState, tasks);
    verify(electMasterService).hasEnoughMasterNodes(eq(remainingNodesClusterState.get().nodes()));
    verifyNoMoreInteractions(electMasterService);
    verify(allocationService).deassociateDeadNodes(eq(remainingNodesClusterState.get()), eq(true), any(String.class));
    for (final ZenDiscovery.NodeRemovalClusterStateTaskExecutor.Task task : tasks) {
        assertNull(result.resultingState.nodes().get(task.node().getId()));
    }
}
Also used : DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterStateTaskExecutor(org.elasticsearch.cluster.ClusterStateTaskExecutor) Mockito.when(org.mockito.Mockito.when) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) Matchers.any(org.mockito.Matchers.any) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) List(java.util.List) Version(org.elasticsearch.Version) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Matchers.eq(org.mockito.Matchers.eq) StreamSupport(java.util.stream.StreamSupport) ClusterName(org.elasticsearch.cluster.ClusterName) ESTestCase(org.elasticsearch.test.ESTestCase) Mockito.mock(org.mockito.Mockito.mock) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ArrayList(java.util.ArrayList) ClusterName(org.elasticsearch.cluster.ClusterName) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) ClusterState(org.elasticsearch.cluster.ClusterState) AtomicReference(java.util.concurrent.atomic.AtomicReference) ClusterStateTaskExecutor(org.elasticsearch.cluster.ClusterStateTaskExecutor)

Example 5 with AllocationService

use of org.elasticsearch.cluster.routing.allocation.AllocationService in project elasticsearch by elastic.

the class ClusterRerouteTests method testClusterStateUpdateTask.

public void testClusterStateUpdateTask() {
    AllocationService allocationService = new AllocationService(Settings.builder().build(), new AllocationDeciders(Settings.EMPTY, Collections.singleton(new MaxRetryAllocationDecider(Settings.EMPTY))), new TestGatewayAllocator(), new BalancedShardsAllocator(Settings.EMPTY), EmptyClusterInfoService.INSTANCE);
    ClusterState clusterState = createInitialClusterState(allocationService);
    ClusterRerouteRequest req = new ClusterRerouteRequest();
    req.dryRun(true);
    AtomicReference<ClusterRerouteResponse> responseRef = new AtomicReference<>();
    ActionListener<ClusterRerouteResponse> responseActionListener = new ActionListener<ClusterRerouteResponse>() {

        @Override
        public void onResponse(ClusterRerouteResponse clusterRerouteResponse) {
            responseRef.set(clusterRerouteResponse);
        }

        @Override
        public void onFailure(Exception e) {
        }
    };
    TransportClusterRerouteAction.ClusterRerouteResponseAckedClusterStateUpdateTask task = new TransportClusterRerouteAction.ClusterRerouteResponseAckedClusterStateUpdateTask(logger, allocationService, req, responseActionListener);
    ClusterState execute = task.execute(clusterState);
    // dry-run
    assertSame(execute, clusterState);
    task.onAllNodesAcked(null);
    assertNotSame(responseRef.get().getState(), execute);
    // now we allocate
    req.dryRun(false);
    final int retries = MaxRetryAllocationDecider.SETTING_ALLOCATION_MAX_RETRY.get(Settings.EMPTY);
    // now fail it N-1 times
    for (int i = 0; i < retries; i++) {
        ClusterState newState = task.execute(clusterState);
        // dry-run=false
        assertNotSame(newState, clusterState);
        clusterState = newState;
        RoutingTable routingTable = clusterState.routingTable();
        assertEquals(routingTable.index("idx").shards().size(), 1);
        assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), INITIALIZING);
        assertEquals(routingTable.index("idx").shard(0).shards().get(0).unassignedInfo().getNumFailedAllocations(), i);
        List<FailedShard> failedShards = Collections.singletonList(new FailedShard(routingTable.index("idx").shard(0).shards().get(0), "boom" + i, new UnsupportedOperationException()));
        newState = allocationService.applyFailedShards(clusterState, failedShards);
        assertThat(newState, not(equalTo(clusterState)));
        clusterState = newState;
        routingTable = clusterState.routingTable();
        assertEquals(routingTable.index("idx").shards().size(), 1);
        if (i == retries - 1) {
            assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), UNASSIGNED);
        } else {
            assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), INITIALIZING);
        }
        assertEquals(routingTable.index("idx").shard(0).shards().get(0).unassignedInfo().getNumFailedAllocations(), i + 1);
    }
    // without retry_failed we won't allocate that shard
    ClusterState newState = task.execute(clusterState);
    // dry-run=false
    assertNotSame(newState, clusterState);
    task.onAllNodesAcked(null);
    assertSame(responseRef.get().getState(), newState);
    RoutingTable routingTable = clusterState.routingTable();
    assertEquals(routingTable.index("idx").shards().size(), 1);
    assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), UNASSIGNED);
    assertEquals(routingTable.index("idx").shard(0).shards().get(0).unassignedInfo().getNumFailedAllocations(), retries);
    // now we manually retry and get the shard back into initializing
    req.setRetryFailed(true);
    newState = task.execute(clusterState);
    // dry-run=false
    assertNotSame(newState, clusterState);
    clusterState = newState;
    routingTable = clusterState.routingTable();
    assertEquals(routingTable.index("idx").shards().size(), 1);
    assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), INITIALIZING);
    assertEquals(routingTable.index("idx").shard(0).shards().get(0).unassignedInfo().getNumFailedAllocations(), retries);
}
Also used : TestGatewayAllocator(org.elasticsearch.test.gateway.TestGatewayAllocator) ClusterState(org.elasticsearch.cluster.ClusterState) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) FailedShard(org.elasticsearch.cluster.routing.allocation.FailedShard) AtomicReference(java.util.concurrent.atomic.AtomicReference) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) MaxRetryAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider) IOException(java.io.IOException) ActionListener(org.elasticsearch.action.ActionListener) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService)

Aggregations

ClusterState (org.elasticsearch.cluster.ClusterState)37 AllocationService (org.elasticsearch.cluster.routing.allocation.AllocationService)37 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)30 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)29 MetaData (org.elasticsearch.cluster.metadata.MetaData)28 BalancedShardsAllocator (org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator)12 Settings (org.elasticsearch.common.settings.Settings)12 TestGatewayAllocator (org.elasticsearch.test.gateway.TestGatewayAllocator)12 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)11 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)9 Matchers.containsString (org.hamcrest.Matchers.containsString)9 ClusterInfo (org.elasticsearch.cluster.ClusterInfo)8 DevNullClusterInfo (org.elasticsearch.cluster.MockInternalClusterInfoService.DevNullClusterInfo)8 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)8 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)8 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)8 ClusterInfoService (org.elasticsearch.cluster.ClusterInfoService)7 DiskUsage (org.elasticsearch.cluster.DiskUsage)7 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)7 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)7