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));
}
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));
}
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"));
}
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"));
}
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();
}
Aggregations