Search in sources :

Example 36 with MoveAllocationCommand

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

the class IndicesStoreIntegrationIT method testShardCleanupIfShardDeletionAfterRelocationFailedAndIndexDeleted.

/* Test that shard is deleted in case ShardActiveRequest after relocation and next incoming cluster state is an index delete. */
public void testShardCleanupIfShardDeletionAfterRelocationFailedAndIndexDeleted() throws Exception {
    final String node_1 = internalCluster().startNode();
    logger.info("--> creating index [test] with one shard and on replica");
    assertAcked(prepareCreate("test").setSettings(Settings.builder().put(indexSettings()).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)));
    ensureGreen("test");
    ClusterState state = client().admin().cluster().prepareState().get().getState();
    Index index = state.metaData().index("test").getIndex();
    assertThat(Files.exists(shardDirectory(node_1, index, 0)), equalTo(true));
    assertThat(Files.exists(indexDirectory(node_1, index)), equalTo(true));
    final String node_2 = internalCluster().startDataOnlyNode(Settings.builder().build());
    assertFalse(client().admin().cluster().prepareHealth().setWaitForNodes("2").get().isTimedOut());
    assertThat(Files.exists(shardDirectory(node_1, index, 0)), equalTo(true));
    assertThat(Files.exists(indexDirectory(node_1, index)), equalTo(true));
    assertThat(Files.exists(shardDirectory(node_2, index, 0)), equalTo(false));
    assertThat(Files.exists(indexDirectory(node_2, index)), equalTo(false));
    // add a transport delegate that will prevent the shard active request to succeed the first time after relocation has finished.
    // node_1 will then wait for the next cluster state change before it tries a next attempt to delete the shard.
    MockTransportService transportServiceNode_1 = (MockTransportService) internalCluster().getInstance(TransportService.class, node_1);
    TransportService transportServiceNode_2 = internalCluster().getInstance(TransportService.class, node_2);
    final CountDownLatch shardActiveRequestSent = new CountDownLatch(1);
    transportServiceNode_1.addDelegate(transportServiceNode_2, new MockTransportService.DelegateTransport(transportServiceNode_1.original()) {

        @Override
        protected void sendRequest(Connection connection, long requestId, String action, TransportRequest request, TransportRequestOptions options) throws IOException {
            if (action.equals("internal:index/shard/exists") && shardActiveRequestSent.getCount() > 0) {
                shardActiveRequestSent.countDown();
                logger.info("prevent shard active request from being sent");
                throw new ConnectTransportException(connection.getNode(), "DISCONNECT: simulated");
            }
            super.sendRequest(connection, requestId, action, request, options);
        }
    });
    logger.info("--> move shard from {} to {}, and wait for relocation to finish", node_1, node_2);
    internalCluster().client().admin().cluster().prepareReroute().add(new MoveAllocationCommand("test", 0, node_1, node_2)).get();
    shardActiveRequestSent.await();
    ClusterHealthResponse clusterHealth = client().admin().cluster().prepareHealth().setWaitForNoRelocatingShards(true).get();
    assertThat(clusterHealth.isTimedOut(), equalTo(false));
    logClusterState();
    // delete the index. node_1 that still waits for the next cluster state update will then get the delete index next.
    // it must still delete the shard, even if it cannot find it anymore in indicesservice
    client().admin().indices().prepareDelete("test").get();
    assertThat(waitForShardDeletion(node_1, index, 0), equalTo(false));
    assertThat(waitForIndexDeletion(node_1, index), equalTo(false));
    assertThat(Files.exists(shardDirectory(node_1, index, 0)), equalTo(false));
    assertThat(Files.exists(indexDirectory(node_1, index)), equalTo(false));
    assertThat(waitForShardDeletion(node_2, index, 0), equalTo(false));
    assertThat(waitForIndexDeletion(node_2, index), equalTo(false));
    assertThat(Files.exists(shardDirectory(node_2, index, 0)), equalTo(false));
    assertThat(Files.exists(indexDirectory(node_2, index)), equalTo(false));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) TransportRequest(org.elasticsearch.transport.TransportRequest) MockTransportService(org.elasticsearch.test.transport.MockTransportService) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand) Index(org.elasticsearch.index.Index) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) MockTransportService(org.elasticsearch.test.transport.MockTransportService) TransportService(org.elasticsearch.transport.TransportService) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions)

Example 37 with MoveAllocationCommand

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

the class ClusterRerouteIT method testRerouteExplain.

public void testRerouteExplain() {
    Settings commonSettings = Settings.builder().build();
    logger.info("--> starting a node");
    String node_1 = internalCluster().startNode(commonSettings);
    assertThat(cluster().size(), equalTo(1));
    ClusterHealthResponse healthResponse = client().admin().cluster().prepareHealth().setWaitForNodes("1").execute().actionGet();
    assertThat(healthResponse.isTimedOut(), equalTo(false));
    logger.info("--> create an index with 1 shard");
    client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0)).execute().actionGet();
    ensureGreen("test");
    logger.info("--> disable allocation");
    Settings newSettings = Settings.builder().put(CLUSTER_ROUTING_ALLOCATION_ENABLE_SETTING.getKey(), Allocation.NONE.name()).build();
    client().admin().cluster().prepareUpdateSettings().setTransientSettings(newSettings).execute().actionGet();
    logger.info("--> starting a second node");
    String node_2 = internalCluster().startNode(commonSettings);
    assertThat(cluster().size(), equalTo(2));
    healthResponse = client().admin().cluster().prepareHealth().setWaitForNodes("2").execute().actionGet();
    assertThat(healthResponse.isTimedOut(), equalTo(false));
    logger.info("--> try to move the shard from node1 to node2");
    MoveAllocationCommand cmd = new MoveAllocationCommand("test", 0, node_1, node_2);
    ClusterRerouteResponse resp = client().admin().cluster().prepareReroute().add(cmd).setExplain(true).execute().actionGet();
    RoutingExplanations e = resp.getExplanations();
    assertThat(e.explanations().size(), equalTo(1));
    RerouteExplanation explanation = e.explanations().get(0);
    assertThat(explanation.command().name(), equalTo(cmd.name()));
    assertThat(((MoveAllocationCommand) explanation.command()).shardId(), equalTo(cmd.shardId()));
    assertThat(((MoveAllocationCommand) explanation.command()).fromNode(), equalTo(cmd.fromNode()));
    assertThat(((MoveAllocationCommand) explanation.command()).toNode(), equalTo(cmd.toNode()));
    assertThat(explanation.decisions().type(), equalTo(Decision.Type.YES));
}
Also used : RoutingExplanations(org.elasticsearch.cluster.routing.allocation.RoutingExplanations) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand) RerouteExplanation(org.elasticsearch.cluster.routing.allocation.RerouteExplanation) Settings(org.elasticsearch.common.settings.Settings) ClusterRerouteResponse(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse)

Example 38 with MoveAllocationCommand

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

the class AlterTableRerouteAnalyzerTest method testRerouteOnBlobTable.

@Test
public void testRerouteOnBlobTable() {
    MoveAllocationCommand command = analyze("ALTER TABLE blob.blobs REROUTE MOVE SHARD 0 FROM 'n1' TO 'n2'");
    assertThat(command.index(), is(".blob_blobs"));
}
Also used : MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 39 with MoveAllocationCommand

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

the class AlterTableRerouteAnalyzerTest method testRerouteMoveShardWithParameters.

@Test
public void testRerouteMoveShardWithParameters() {
    MoveAllocationCommand command = analyze("ALTER TABLE users REROUTE MOVE SHARD 0 FROM ? TO ?", "n2", "n1");
    assertThat(command.index(), is("users"));
    assertThat(command.shardId(), is(0));
    assertThat(command.fromNode(), is("n2"));
    assertThat(command.toNode(), is("n1"));
}
Also used : MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 40 with MoveAllocationCommand

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

the class PartitionedTableConcurrentIntegrationTest method testSelectWhileShardsAreRelocating.

/**
 * Test depends on 2 data nodes
 */
@Test
public void testSelectWhileShardsAreRelocating() throws Throwable {
    // Automatic rebalancing would disturb our manual allocation and could lead to test failures as reallocation
    // may be issued/run concurrently (by the test and by the cluster itself).
    execute("SET GLOBAL cluster.routing.rebalance.enable = 'none'");
    execute("create table t (name string, p string) " + "clustered into 2 shards " + "partitioned by (p) with (number_of_replicas = 0)");
    ensureYellow();
    execute("insert into t (name, p) values (?, ?)", new Object[][] { new Object[] { "Marvin", "a" }, new Object[] { "Trillian", "a" } });
    execute("refresh table t");
    final AtomicReference<Throwable> lastThrowable = new AtomicReference<>();
    final CountDownLatch selects = new CountDownLatch(100);
    Thread t = new Thread(() -> {
        while (selects.getCount() > 0) {
            try {
                execute("select * from t");
            } catch (Throwable t1) {
                // The failed job should have three started operations
                SQLResponse res = execute("select id from sys.jobs_log where error is not null order by started desc limit 1");
                if (res.rowCount() > 0) {
                    String id = (String) res.rows()[0][0];
                    res = execute("select count(*) from sys.operations_log where name=? or name = ? and job_id = ?", new Object[] { "collect", "fetchContext", id });
                    if ((long) res.rows()[0][0] < 3) {
                        // set the error if there where less than three attempts
                        lastThrowable.set(t1);
                    }
                }
            } finally {
                selects.countDown();
            }
        }
    });
    t.start();
    PartitionName partitionName = new PartitionName(new RelationName(sqlExecutor.getCurrentSchema(), "t"), Collections.singletonList("a"));
    final String indexName = partitionName.asIndexName();
    ClusterService clusterService = internalCluster().getInstance(ClusterService.class);
    DiscoveryNodes nodes = clusterService.state().nodes();
    List<String> nodeIds = new ArrayList<>(2);
    for (DiscoveryNode node : nodes) {
        if (node.isDataNode()) {
            nodeIds.add(node.getId());
        }
    }
    final Map<String, String> nodeSwap = new HashMap<>(2);
    nodeSwap.put(nodeIds.get(0), nodeIds.get(1));
    nodeSwap.put(nodeIds.get(1), nodeIds.get(0));
    final CountDownLatch relocations = new CountDownLatch(20);
    Thread relocatingThread = new Thread(() -> {
        while (relocations.getCount() > 0) {
            ClusterStateResponse clusterStateResponse = admin().cluster().prepareState().setIndices(indexName).execute().actionGet();
            List<ShardRouting> shardRoutings = clusterStateResponse.getState().routingTable().allShards(indexName);
            ClusterRerouteRequestBuilder clusterRerouteRequestBuilder = admin().cluster().prepareReroute();
            int numMoves = 0;
            for (ShardRouting shardRouting : shardRoutings) {
                if (shardRouting.currentNodeId() == null) {
                    continue;
                }
                if (shardRouting.state() != ShardRoutingState.STARTED) {
                    continue;
                }
                String toNode = nodeSwap.get(shardRouting.currentNodeId());
                clusterRerouteRequestBuilder.add(new MoveAllocationCommand(shardRouting.getIndexName(), shardRouting.shardId().id(), shardRouting.currentNodeId(), toNode));
                numMoves++;
            }
            if (numMoves > 0) {
                clusterRerouteRequestBuilder.execute().actionGet();
                client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForNoRelocatingShards(false).setTimeout(ACCEPTABLE_RELOCATION_TIME).execute().actionGet();
                relocations.countDown();
            }
        }
    });
    relocatingThread.start();
    relocations.await(SQLTransportExecutor.REQUEST_TIMEOUT.getSeconds() + 1, TimeUnit.SECONDS);
    selects.await(SQLTransportExecutor.REQUEST_TIMEOUT.getSeconds() + 1, TimeUnit.SECONDS);
    Throwable throwable = lastThrowable.get();
    if (throwable != null) {
        throw throwable;
    }
    t.join();
    relocatingThread.join();
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) HashMap(java.util.HashMap) ClusterRerouteRequestBuilder(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteRequestBuilder) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) ArrayList(java.util.ArrayList) MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand) AtomicReference(java.util.concurrent.atomic.AtomicReference) SQLResponse(io.crate.testing.SQLResponse) CountDownLatch(java.util.concurrent.CountDownLatch) PartitionName(io.crate.metadata.PartitionName) ClusterService(org.elasticsearch.cluster.service.ClusterService) RelationName(io.crate.metadata.RelationName) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) Test(org.junit.Test)

Aggregations

MoveAllocationCommand (org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand)42 ClusterState (org.elasticsearch.cluster.ClusterState)24 AllocationCommands (org.elasticsearch.cluster.routing.allocation.command.AllocationCommands)15 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)14 Test (org.junit.Test)12 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)10 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)10 MetaData (org.elasticsearch.cluster.metadata.MetaData)9 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)9 Index (org.elasticsearch.index.Index)9 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)5 Metadata (org.elasticsearch.cluster.metadata.Metadata)5 AllocateEmptyPrimaryAllocationCommand (org.elasticsearch.cluster.routing.allocation.command.AllocateEmptyPrimaryAllocationCommand)5 CancelAllocationCommand (org.elasticsearch.cluster.routing.allocation.command.CancelAllocationCommand)5 Settings (org.elasticsearch.common.settings.Settings)5 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)4 AllocateReplicaAllocationCommand (org.elasticsearch.cluster.routing.allocation.command.AllocateReplicaAllocationCommand)4 ClusterService (org.elasticsearch.cluster.service.ClusterService)4