Search in sources :

Example 1 with CreateIndexResponse

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();
}
Also used : UnavailableShardsException(org.elasticsearch.action.UnavailableShardsException) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse)

Example 2 with CreateIndexResponse

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));
}
Also used : IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) TypesExistsResponse(org.elasticsearch.action.admin.indices.exists.types.TypesExistsResponse) Client(org.elasticsearch.client.Client) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse)

Example 3 with CreateIndexResponse

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));
}
Also used : IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse)

Example 4 with CreateIndexResponse

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);
        }
    });
}
Also used : ActiveShardCount(org.elasticsearch.action.support.ActiveShardCount) BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) Settings(org.elasticsearch.common.settings.Settings) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) AcknowledgedRestListener(org.elasticsearch.rest.action.AcknowledgedRestListener) IOException(java.io.IOException) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 5 with CreateIndexResponse

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);
        }
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ResourceAlreadyExistsException(org.elasticsearch.ResourceAlreadyExistsException) PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) ElasticsearchException(org.elasticsearch.ElasticsearchException) ResourceAlreadyExistsException(org.elasticsearch.ResourceAlreadyExistsException) IOException(java.io.IOException) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Aggregations

CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)32 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)11 Settings (org.elasticsearch.common.settings.Settings)9 ElasticsearchException (org.elasticsearch.ElasticsearchException)6 IOException (java.io.IOException)5 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)4 CreateIndexRequestBuilder (org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder)4 PutMappingResponse (org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse)4 ClusterState (org.elasticsearch.cluster.ClusterState)4 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)4 ResourceAlreadyExistsException (org.elasticsearch.ResourceAlreadyExistsException)3 IndicesExistsResponse (org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse)3 IndicesAdminClient (org.elasticsearch.client.IndicesAdminClient)3 RelationName (io.crate.metadata.RelationName)2 DeleteIndexResponse (org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse)2 ClusterBlockException (org.elasticsearch.cluster.block.ClusterBlockException)2 Before (org.junit.Before)2 IndexingServiceException (alien4cloud.exception.IndexingServiceException)1 TitanException (com.thinkaurelius.titan.core.TitanException)1 FutureActionListener (io.crate.action.FutureActionListener)1