use of org.elasticsearch.action.admin.indices.create.CreateIndexResponse in project elasticsearch by elastic.
the class WaitActiveShardCountIT method testReplicationWaitsForActiveShardCount.
public void testReplicationWaitsForActiveShardCount() throws Exception {
CreateIndexResponse createIndexResponse = prepareCreate("test", 1, Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 2)).get();
assertAcked(createIndexResponse);
// indexing, by default, will work (waiting for one shard copy only)
client().prepareIndex("test", "type1", "1").setSource(source("1", "test"), XContentType.JSON).execute().actionGet();
try {
client().prepareIndex("test", "type1", "1").setSource(source("1", "test"), XContentType.JSON).setWaitForActiveShards(// wait for 2 active shard copies
2).setTimeout(timeValueMillis(100)).execute().actionGet();
fail("can't index, does not enough active shard copies");
} catch (UnavailableShardsException e) {
assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));
assertThat(e.getMessage(), startsWith("[test][0] Not enough active copies to meet shard count of [2] (have 1, needed 2). Timeout: [100ms], request:"));
// but really, all is well
}
allowNodes("test", 2);
ClusterHealthResponse clusterHealth = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForActiveShards(2).setWaitForYellowStatus().execute().actionGet();
logger.info("Done Cluster Health, status {}", clusterHealth.getStatus());
assertThat(clusterHealth.isTimedOut(), equalTo(false));
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.YELLOW));
// this should work, since we now have two
client().prepareIndex("test", "type1", "1").setSource(source("1", "test"), XContentType.JSON).setWaitForActiveShards(2).setTimeout(timeValueSeconds(1)).execute().actionGet();
try {
client().prepareIndex("test", "type1", "1").setSource(source("1", "test"), XContentType.JSON).setWaitForActiveShards(ActiveShardCount.ALL).setTimeout(timeValueMillis(100)).execute().actionGet();
fail("can't index, not enough active shard copies");
} catch (UnavailableShardsException e) {
assertThat(e.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE));
assertThat(e.getMessage(), startsWith("[test][0] Not enough active copies to meet shard count of [" + ActiveShardCount.ALL + "] (have 2, needed 3). Timeout: [100ms], request:"));
// but really, all is well
}
allowNodes("test", 3);
clusterHealth = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForActiveShards(3).setWaitForGreenStatus().execute().actionGet();
logger.info("Done Cluster Health, status {}", clusterHealth.getStatus());
assertThat(clusterHealth.isTimedOut(), equalTo(false));
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));
// this should work, since we now have all shards started
client().prepareIndex("test", "type1", "1").setSource(source("1", "test"), XContentType.JSON).setWaitForActiveShards(ActiveShardCount.ALL).setTimeout(timeValueSeconds(1)).execute().actionGet();
}
use of org.elasticsearch.action.admin.indices.create.CreateIndexResponse in project elasticsearch by elastic.
the class TypesExistsIT method testSimple.
public void testSimple() throws Exception {
Client client = client();
CreateIndexResponse response1 = client.admin().indices().prepareCreate("test1").addMapping("type1", jsonBuilder().startObject().startObject("type1").endObject().endObject()).addMapping("type2", jsonBuilder().startObject().startObject("type2").endObject().endObject()).execute().actionGet();
CreateIndexResponse response2 = client.admin().indices().prepareCreate("test2").addMapping("type1", jsonBuilder().startObject().startObject("type1").endObject().endObject()).execute().actionGet();
client.admin().indices().prepareAliases().addAlias("test1", "alias1").execute().actionGet();
assertAcked(response1);
assertAcked(response2);
TypesExistsResponse response = client.admin().indices().prepareTypesExists("test1").setTypes("type1").execute().actionGet();
assertThat(response.isExists(), equalTo(true));
response = client.admin().indices().prepareTypesExists("test1").setTypes("type2").execute().actionGet();
assertThat(response.isExists(), equalTo(true));
response = client.admin().indices().prepareTypesExists("test1").setTypes("type3").execute().actionGet();
assertThat(response.isExists(), equalTo(false));
try {
client.admin().indices().prepareTypesExists("notExist").setTypes("type1").execute().actionGet();
fail("Exception should have been thrown");
} catch (IndexNotFoundException e) {
}
try {
client.admin().indices().prepareTypesExists("notExist").setTypes("type0").execute().actionGet();
fail("Exception should have been thrown");
} catch (IndexNotFoundException e) {
}
response = client.admin().indices().prepareTypesExists("alias1").setTypes("type1").execute().actionGet();
assertThat(response.isExists(), equalTo(true));
response = client.admin().indices().prepareTypesExists("*").setTypes("type1").execute().actionGet();
assertThat(response.isExists(), equalTo(true));
response = client.admin().indices().prepareTypesExists("test1", "test2").setTypes("type1").execute().actionGet();
assertThat(response.isExists(), equalTo(true));
response = client.admin().indices().prepareTypesExists("test1", "test2").setTypes("type2").execute().actionGet();
assertThat(response.isExists(), equalTo(false));
}
use of org.elasticsearch.action.admin.indices.create.CreateIndexResponse in project elasticsearch by elastic.
the class SimpleIndexStateIT method testConsistencyAfterIndexCreationFailure.
public void testConsistencyAfterIndexCreationFailure() {
logger.info("--> deleting test index....");
try {
client().admin().indices().prepareDelete("test").get();
} catch (IndexNotFoundException ex) {
// Ignore
}
logger.info("--> creating test index with invalid settings ");
try {
client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put("number_of_shards", "bad")).get();
fail();
} catch (IllegalArgumentException ex) {
assertEquals("Failed to parse value [bad] for setting [index.number_of_shards]", ex.getMessage());
// Expected
}
logger.info("--> creating test index with valid settings ");
CreateIndexResponse response = client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put("number_of_shards", 1)).get();
assertThat(response.isAcknowledged(), equalTo(true));
}
use of org.elasticsearch.action.admin.indices.create.CreateIndexResponse in project elasticsearch by elastic.
the class RestCreateIndexAction method prepareRequest.
@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
CreateIndexRequest createIndexRequest = new CreateIndexRequest(request.param("index"));
if (request.hasContent()) {
createIndexRequest.source(request.content(), request.getXContentType());
}
createIndexRequest.updateAllTypes(request.paramAsBoolean("update_all_types", false));
createIndexRequest.timeout(request.paramAsTime("timeout", createIndexRequest.timeout()));
createIndexRequest.masterNodeTimeout(request.paramAsTime("master_timeout", createIndexRequest.masterNodeTimeout()));
createIndexRequest.waitForActiveShards(ActiveShardCount.parseString(request.param("wait_for_active_shards")));
return channel -> client.admin().indices().create(createIndexRequest, new AcknowledgedRestListener<CreateIndexResponse>(channel) {
@Override
public void addCustomFields(XContentBuilder builder, CreateIndexResponse response) throws IOException {
response.addCustomFields(builder);
}
});
}
use of org.elasticsearch.action.admin.indices.create.CreateIndexResponse in project elasticsearch by elastic.
the class TaskResultsService method storeResult.
public void storeResult(TaskResult taskResult, ActionListener<Void> listener) {
ClusterState state = clusterService.state();
if (state.routingTable().hasIndex(TASK_INDEX) == false) {
CreateIndexRequest createIndexRequest = new CreateIndexRequest();
createIndexRequest.settings(taskResultIndexSettings());
createIndexRequest.index(TASK_INDEX);
createIndexRequest.mapping(TASK_TYPE, taskResultIndexMapping(), XContentType.JSON);
createIndexRequest.cause("auto(task api)");
createIndexAction.execute(null, createIndexRequest, new ActionListener<CreateIndexResponse>() {
@Override
public void onResponse(CreateIndexResponse result) {
doStoreResult(taskResult, listener);
}
@Override
public void onFailure(Exception e) {
if (ExceptionsHelper.unwrapCause(e) instanceof ResourceAlreadyExistsException) {
// we have the index, do it
try {
doStoreResult(taskResult, listener);
} catch (Exception inner) {
inner.addSuppressed(e);
listener.onFailure(inner);
}
} else {
listener.onFailure(e);
}
}
});
} else {
IndexMetaData metaData = state.getMetaData().index(TASK_INDEX);
if (metaData.getMappings().containsKey(TASK_TYPE) == false) {
// The index already exists but doesn't have our mapping
client.admin().indices().preparePutMapping(TASK_INDEX).setType(TASK_TYPE).setSource(taskResultIndexMapping(), XContentType.JSON).execute(new ActionListener<PutMappingResponse>() {
@Override
public void onResponse(PutMappingResponse putMappingResponse) {
doStoreResult(taskResult, listener);
}
@Override
public void onFailure(Exception e) {
listener.onFailure(e);
}
});
} else {
doStoreResult(taskResult, listener);
}
}
}
Aggregations