use of org.elasticsearch.action.admin.cluster.state.ClusterStateResponse in project vertexium by visallo.
the class ElasticsearchResource method getLocation.
private String getLocation() {
ClusterStateResponse responsee = runner.node().client().admin().cluster().prepareState().execute().actionGet();
InetSocketTransportAddress address = (InetSocketTransportAddress) responsee.getState().getNodes().getNodes().values().iterator().next().value.getAddress();
return "localhost:" + address.address().getPort();
}
use of org.elasticsearch.action.admin.cluster.state.ClusterStateResponse 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();
}
use of org.elasticsearch.action.admin.cluster.state.ClusterStateResponse in project crate by crate.
the class SnapshotRestoreIntegrationTest method waitForCompletion.
private SnapshotInfo waitForCompletion(String repository, String snapshotName, TimeValue timeout) throws InterruptedException {
long start = System.currentTimeMillis();
Snapshot snapshot = new Snapshot(repository, new SnapshotId(repository, snapshotName));
while (System.currentTimeMillis() - start < timeout.millis()) {
List<SnapshotInfo> snapshotInfos = client().admin().cluster().prepareGetSnapshots(repository).setSnapshots(snapshotName).get().getSnapshots();
assertThat(snapshotInfos.size(), equalTo(1));
if (snapshotInfos.get(0).state().completed()) {
// Make sure that snapshot clean up operations are finished
ClusterStateResponse stateResponse = client().admin().cluster().prepareState().get();
SnapshotsInProgress snapshotsInProgress = stateResponse.getState().custom(SnapshotsInProgress.TYPE);
if (snapshotsInProgress == null || snapshotsInProgress.snapshot(snapshot) == null) {
return snapshotInfos.get(0);
}
}
Thread.sleep(100);
}
fail("Timeout waiting for snapshot completion!");
return null;
}
use of org.elasticsearch.action.admin.cluster.state.ClusterStateResponse in project crate by crate.
the class GatewayIndexStateIT method testTwoNodesSingleDoc.
@Test
public void testTwoNodesSingleDoc() throws Exception {
logger.info("--> cleaning nodes");
logger.info("--> starting 2 nodes");
internalCluster().startNodes(2);
logger.info("--> indexing a simple document");
var tableName = getFqn("test");
execute("create table test (id int) with (number_of_replicas = 0)");
execute("insert into test (id) values (1)");
execute("refresh table test");
logger.info("--> waiting for green status");
ClusterHealthResponse health = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().setWaitForNodes("2").execute().actionGet(REQUEST_TIMEOUT);
assertThat(health.isTimedOut(), equalTo(false));
logger.info("--> verify 1 doc in the index");
for (int i = 0; i < 10; i++) {
execute("select id from test");
assertThat(response.rowCount(), is(1L));
}
logger.info("--> closing test index...");
execute("alter table test close");
ClusterStateResponse stateResponse = client().admin().cluster().prepareState().execute().actionGet(REQUEST_TIMEOUT);
assertThat(stateResponse.getState().metadata().index(tableName).getState(), equalTo(IndexMetadata.State.CLOSE));
assertThat(stateResponse.getState().routingTable().index(tableName), notNullValue());
logger.info("--> opening the index...");
execute("alter table test open");
logger.info("--> waiting for green status");
health = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().setWaitForNodes("2").execute().actionGet(REQUEST_TIMEOUT);
assertThat(health.isTimedOut(), equalTo(false));
logger.info("--> verify 1 doc in the index");
execute("select id from test");
assertThat(response.rowCount(), is(1L));
for (int i = 0; i < 10; i++) {
execute("select id from test");
assertThat(response.rowCount(), is(1L));
}
}
use of org.elasticsearch.action.admin.cluster.state.ClusterStateResponse in project crate by crate.
the class GatewayIndexStateIT method testSimpleOpenClose.
public void testSimpleOpenClose() throws Exception {
logger.info("--> starting 2 nodes");
internalCluster().startNodes(2);
logger.info("--> creating test index");
int numPrimaries = 2;
int numReplicas = 1;
int totalNumShards = numPrimaries + (numPrimaries * numReplicas);
var tableName = getFqn("test");
execute("create table test (id int primary key) clustered into ? shards with (number_of_replicas = ?)", new Object[] { numPrimaries, numReplicas });
logger.info("--> waiting for green status");
ensureGreen();
ClusterStateResponse stateResponse = client().admin().cluster().prepareState().execute().actionGet();
assertThat(stateResponse.getState().metadata().index(tableName).getState(), equalTo(IndexMetadata.State.OPEN));
assertThat(stateResponse.getState().routingTable().index(tableName).shards().size(), equalTo(numPrimaries));
assertThat(stateResponse.getState().routingTable().index(tableName).shardsWithState(ShardRoutingState.STARTED).size(), equalTo(totalNumShards));
logger.info("--> insert a simple document");
execute("insert into test (id) values (1)");
logger.info("--> closing test index...");
execute("alter table test close");
stateResponse = client().admin().cluster().prepareState().execute().actionGet();
assertThat(stateResponse.getState().metadata().index(tableName).getState(), equalTo(IndexMetadata.State.CLOSE));
assertThat(stateResponse.getState().routingTable().index(tableName), notNullValue());
logger.info("--> verifying that the state is green");
ensureGreen();
logger.info("--> trying to index into a closed index ...");
try {
execute("insert into test (id) values (2)");
fail();
} catch (Exception e) {
// all is well
}
logger.info("--> creating another index (test2) and indexing into it");
execute("create table test2 (id int primary key) with (number_of_replicas = 0)");
execute("insert into test2 (id) values (1)");
logger.info("--> verifying that the state is green");
ensureGreen();
logger.info("--> opening the first index again...");
execute("alter table test open");
logger.info("--> verifying that the state is green");
ensureGreen();
stateResponse = client().admin().cluster().prepareState().execute().actionGet(REQUEST_TIMEOUT);
assertThat(stateResponse.getState().metadata().index(tableName).getState(), equalTo(IndexMetadata.State.OPEN));
assertThat(stateResponse.getState().routingTable().index(tableName).shards().size(), equalTo(numPrimaries));
assertThat(stateResponse.getState().routingTable().index(tableName).shardsWithState(ShardRoutingState.STARTED).size(), equalTo(totalNumShards));
logger.info("--> trying to get the indexed document on the first index");
execute("select id from test where id = 1");
assertThat(response.rowCount(), is(1L));
logger.info("--> closing test index...");
execute("alter table test close");
stateResponse = client().admin().cluster().prepareState().execute().actionGet(REQUEST_TIMEOUT);
assertThat(stateResponse.getState().metadata().index(tableName).getState(), equalTo(IndexMetadata.State.CLOSE));
assertThat(stateResponse.getState().routingTable().index(tableName), notNullValue());
logger.info("--> restarting nodes...");
internalCluster().fullRestart();
logger.info("--> waiting for two nodes and green status");
ensureGreen();
stateResponse = client().admin().cluster().prepareState().execute().actionGet(REQUEST_TIMEOUT);
assertThat(stateResponse.getState().metadata().index(tableName).getState(), equalTo(IndexMetadata.State.CLOSE));
assertThat(stateResponse.getState().routingTable().index(tableName), notNullValue());
logger.info("--> trying to index into a closed index ...");
try {
execute("insert into test (id) values (2)");
fail();
} catch (Exception e) {
// all is well
}
logger.info("--> opening index...");
execute("alter table test open");
logger.info("--> waiting for green status");
ensureGreen();
stateResponse = client().admin().cluster().prepareState().execute().actionGet();
assertThat(stateResponse.getState().metadata().index(tableName).getState(), equalTo(IndexMetadata.State.OPEN));
assertThat(stateResponse.getState().routingTable().index(tableName).shards().size(), equalTo(numPrimaries));
assertThat(stateResponse.getState().routingTable().index(tableName).shardsWithState(ShardRoutingState.STARTED).size(), equalTo(totalNumShards));
logger.info("--> trying to get the indexed document on the first round (before close and shutdown)");
execute("select id from test where id = 1");
assertThat(response.rowCount(), is(1L));
logger.info("--> indexing a simple document");
execute("insert into test (id) values (2)");
}
Aggregations