Search in sources :

Example 1 with PutMappingResponse

use of org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse in project elasticsearch by elastic.

the class RareClusterStateIT method testDelayedMappingPropagationOnPrimary.

public void testDelayedMappingPropagationOnPrimary() throws Exception {
    // Here we want to test that things go well if there is a first request
    // that adds mappings but before mappings are propagated to all nodes
    // another index request introduces the same mapping. The master node
    // will reply immediately since it did not change the cluster state
    // but the change might not be on the node that performed the indexing
    // operation yet
    Settings settings = Settings.builder().put(DiscoverySettings.COMMIT_TIMEOUT_SETTING.getKey(), // explicitly set so it won't default to publish timeout
    "30s").put(DiscoverySettings.PUBLISH_TIMEOUT_SETTING.getKey(), // don't wait post commit as we are blocking things by design
    "0s").build();
    final List<String> nodeNames = internalCluster().startNodes(2, settings);
    assertFalse(client().admin().cluster().prepareHealth().setWaitForNodes("2").get().isTimedOut());
    final String master = internalCluster().getMasterName();
    assertThat(nodeNames, hasItem(master));
    String otherNode = null;
    for (String node : nodeNames) {
        if (node.equals(master) == false) {
            otherNode = node;
            break;
        }
    }
    assertNotNull(otherNode);
    // Don't allocate the shard on the master node
    assertAcked(prepareCreate("index").setSettings(Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).put("index.routing.allocation.exclude._name", master)).get());
    ensureGreen();
    // Check routing tables
    ClusterState state = client().admin().cluster().prepareState().get().getState();
    assertEquals(master, state.nodes().getMasterNode().getName());
    List<ShardRouting> shards = state.routingTable().allShards("index");
    assertThat(shards, hasSize(1));
    for (ShardRouting shard : shards) {
        if (shard.primary()) {
            // primary must not be on the master node
            assertFalse(state.nodes().getMasterNodeId().equals(shard.currentNodeId()));
        } else {
            // only primaries
            fail();
        }
    }
    // Block cluster state processing where our shard is
    BlockClusterStateProcessing disruption = new BlockClusterStateProcessing(otherNode, random());
    internalCluster().setDisruptionScheme(disruption);
    disruption.startDisrupting();
    // Add a new mapping...
    final AtomicReference<Object> putMappingResponse = new AtomicReference<>();
    client().admin().indices().preparePutMapping("index").setType("type").setSource("field", "type=long").execute(new ActionListener<PutMappingResponse>() {

        @Override
        public void onResponse(PutMappingResponse response) {
            putMappingResponse.set(response);
        }

        @Override
        public void onFailure(Exception e) {
            putMappingResponse.set(e);
        }
    });
    // ...and wait for mappings to be available on master
    assertBusy(new Runnable() {

        @Override
        public void run() {
            ImmutableOpenMap<String, MappingMetaData> indexMappings = client().admin().indices().prepareGetMappings("index").get().getMappings().get("index");
            assertNotNull(indexMappings);
            MappingMetaData typeMappings = indexMappings.get("type");
            assertNotNull(typeMappings);
            Object properties;
            try {
                properties = typeMappings.getSourceAsMap().get("properties");
            } catch (IOException e) {
                throw new AssertionError(e);
            }
            assertNotNull(properties);
            Object fieldMapping = ((Map<String, Object>) properties).get("field");
            assertNotNull(fieldMapping);
        }
    });
    final AtomicReference<Object> docIndexResponse = new AtomicReference<>();
    client().prepareIndex("index", "type", "1").setSource("field", 42).execute(new ActionListener<IndexResponse>() {

        @Override
        public void onResponse(IndexResponse response) {
            docIndexResponse.set(response);
        }

        @Override
        public void onFailure(Exception e) {
            docIndexResponse.set(e);
        }
    });
    // Wait a bit to make sure that the reason why we did not get a response
    // is that cluster state processing is blocked and not just that it takes
    // time to process the indexing request
    Thread.sleep(100);
    assertThat(putMappingResponse.get(), equalTo(null));
    assertThat(docIndexResponse.get(), equalTo(null));
    // Now make sure the indexing request finishes successfully
    disruption.stopDisrupting();
    assertBusy(new Runnable() {

        @Override
        public void run() {
            assertThat(putMappingResponse.get(), instanceOf(PutMappingResponse.class));
            PutMappingResponse resp = (PutMappingResponse) putMappingResponse.get();
            assertTrue(resp.isAcknowledged());
            assertThat(docIndexResponse.get(), instanceOf(IndexResponse.class));
            IndexResponse docResp = (IndexResponse) docIndexResponse.get();
            assertEquals(Arrays.toString(docResp.getShardInfo().getFailures()), 1, docResp.getShardInfo().getTotal());
        }
    });
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) BlockClusterStateProcessing(org.elasticsearch.test.disruption.BlockClusterStateProcessing) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) IOException(java.io.IOException) IndexResponse(org.elasticsearch.action.index.IndexResponse) PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Settings(org.elasticsearch.common.settings.Settings) DiscoverySettings(org.elasticsearch.discovery.DiscoverySettings)

Example 2 with PutMappingResponse

use of org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse in project elasticsearch by elastic.

the class UpdateMappingIntegrationIT method testUpdateMappingWithoutType.

public void testUpdateMappingWithoutType() throws Exception {
    client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0)).addMapping("doc", "{\"doc\":{\"properties\":{\"body\":{\"type\":\"text\"}}}}", XContentType.JSON).execute().actionGet();
    client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
    PutMappingResponse putMappingResponse = client().admin().indices().preparePutMapping("test").setType("doc").setSource("{\"properties\":{\"date\":{\"type\":\"integer\"}}}", XContentType.JSON).execute().actionGet();
    assertThat(putMappingResponse.isAcknowledged(), equalTo(true));
    GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("test").execute().actionGet();
    assertThat(getMappingsResponse.mappings().get("test").get("doc").source().toString(), equalTo("{\"doc\":{\"properties\":{\"body\":{\"type\":\"text\"},\"date\":{\"type\":\"integer\"}}}}"));
}
Also used : PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 3 with PutMappingResponse

use of org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse in project elasticsearch by elastic.

the class UpdateMappingIntegrationIT method testUpdateDefaultMappingSettings.

@SuppressWarnings("unchecked")
public void testUpdateDefaultMappingSettings() throws Exception {
    logger.info("Creating index with _default_ mappings");
    client().admin().indices().prepareCreate("test").addMapping(MapperService.DEFAULT_MAPPING, JsonXContent.contentBuilder().startObject().startObject(MapperService.DEFAULT_MAPPING).field("date_detection", false).endObject().endObject()).get();
    GetMappingsResponse getResponse = client().admin().indices().prepareGetMappings("test").addTypes(MapperService.DEFAULT_MAPPING).get();
    Map<String, Object> defaultMapping = getResponse.getMappings().get("test").get(MapperService.DEFAULT_MAPPING).sourceAsMap();
    assertThat(defaultMapping, hasKey("date_detection"));
    logger.info("Emptying _default_ mappings");
    // now remove it
    PutMappingResponse putResponse = client().admin().indices().preparePutMapping("test").setType(MapperService.DEFAULT_MAPPING).setSource(JsonXContent.contentBuilder().startObject().startObject(MapperService.DEFAULT_MAPPING).endObject().endObject()).get();
    assertThat(putResponse.isAcknowledged(), equalTo(true));
    logger.info("Done Emptying _default_ mappings");
    getResponse = client().admin().indices().prepareGetMappings("test").addTypes(MapperService.DEFAULT_MAPPING).get();
    defaultMapping = getResponse.getMappings().get("test").get(MapperService.DEFAULT_MAPPING).sourceAsMap();
    assertThat(defaultMapping, not(hasKey("date_detection")));
    // now test you can change stuff that are normally unchangeable
    logger.info("Creating _default_ mappings with an analyzed field");
    putResponse = client().admin().indices().preparePutMapping("test").setType(MapperService.DEFAULT_MAPPING).setSource(JsonXContent.contentBuilder().startObject().startObject(MapperService.DEFAULT_MAPPING).startObject("properties").startObject("f").field("type", "text").field("index", true).endObject().endObject().endObject().endObject()).get();
    assertThat(putResponse.isAcknowledged(), equalTo(true));
    logger.info("Changing _default_ mappings field from analyzed to non-analyzed");
    putResponse = client().admin().indices().preparePutMapping("test").setType(MapperService.DEFAULT_MAPPING).setSource(JsonXContent.contentBuilder().startObject().startObject(MapperService.DEFAULT_MAPPING).startObject("properties").startObject("f").field("type", "keyword").endObject().endObject().endObject().endObject()).get();
    assertThat(putResponse.isAcknowledged(), equalTo(true));
    logger.info("Done changing _default_ mappings field from analyzed to non-analyzed");
    getResponse = client().admin().indices().prepareGetMappings("test").addTypes(MapperService.DEFAULT_MAPPING).get();
    defaultMapping = getResponse.getMappings().get("test").get(MapperService.DEFAULT_MAPPING).sourceAsMap();
    Map<String, Object> fieldSettings = (Map<String, Object>) ((Map) defaultMapping.get("properties")).get("f");
    assertThat(fieldSettings, hasEntry("type", (Object) "keyword"));
    // but we still validate the _default_ type
    logger.info("Confirming _default_ mappings validation");
    assertThrows(client().admin().indices().preparePutMapping("test").setType(MapperService.DEFAULT_MAPPING).setSource(JsonXContent.contentBuilder().startObject().startObject(MapperService.DEFAULT_MAPPING).startObject("properties").startObject("f").field("type", "DOESNT_EXIST").endObject().endObject().endObject().endObject()), MapperParsingException.class);
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) Map(java.util.Map) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 4 with PutMappingResponse

use of org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse in project elasticsearch by elastic.

the class UpdateMappingIntegrationIT method testUpdateMappingConcurrently.

public void testUpdateMappingConcurrently() throws Throwable {
    createIndex("test1", "test2");
    final AtomicReference<Exception> threadException = new AtomicReference<>();
    final AtomicBoolean stop = new AtomicBoolean(false);
    Thread[] threads = new Thread[3];
    final CyclicBarrier barrier = new CyclicBarrier(threads.length);
    final ArrayList<Client> clientArray = new ArrayList<>();
    for (Client c : clients()) {
        clientArray.add(c);
    }
    for (int j = 0; j < threads.length; j++) {
        threads[j] = new Thread(new Runnable() {

            @SuppressWarnings("unchecked")
            @Override
            public void run() {
                try {
                    barrier.await();
                    for (int i = 0; i < 100; i++) {
                        if (stop.get()) {
                            return;
                        }
                        Client client1 = clientArray.get(i % clientArray.size());
                        Client client2 = clientArray.get((i + 1) % clientArray.size());
                        String indexName = i % 2 == 0 ? "test2" : "test1";
                        String typeName = "type" + (i % 10);
                        String fieldName = Thread.currentThread().getName() + "_" + i;
                        PutMappingResponse response = client1.admin().indices().preparePutMapping(indexName).setType(typeName).setSource(JsonXContent.contentBuilder().startObject().startObject(typeName).startObject("properties").startObject(fieldName).field("type", "text").endObject().endObject().endObject().endObject()).get();
                        assertThat(response.isAcknowledged(), equalTo(true));
                        GetMappingsResponse getMappingResponse = client2.admin().indices().prepareGetMappings(indexName).get();
                        ImmutableOpenMap<String, MappingMetaData> mappings = getMappingResponse.getMappings().get(indexName);
                        assertThat(mappings.containsKey(typeName), equalTo(true));
                        assertThat(((Map<String, Object>) mappings.get(typeName).getSourceAsMap().get("properties")).keySet(), Matchers.hasItem(fieldName));
                    }
                } catch (Exception e) {
                    threadException.set(e);
                    stop.set(true);
                }
            }
        });
        threads[j].setName("t_" + j);
        threads[j].start();
    }
    for (Thread t : threads) t.join();
    if (threadException.get() != null) {
        throw threadException.get();
    }
}
Also used : ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) IOException(java.io.IOException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) Client(org.elasticsearch.client.Client) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 5 with PutMappingResponse

use of org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse 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

PutMappingResponse (org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse)37 IOException (java.io.IOException)19 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)11 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)9 DeleteIndexResponse (org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse)9 IndicesExistsRequest (org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest)9 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)8 GetMappingsResponse (org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)7 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)4 ClusterState (org.elasticsearch.cluster.ClusterState)4 MappingMetaData (org.elasticsearch.cluster.metadata.MappingMetaData)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 ElasticsearchException (org.elasticsearch.ElasticsearchException)3 PutMappingRequest (org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest)3 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)3 TitanException (com.thinkaurelius.titan.core.TitanException)2 DtDefinition (io.vertigo.dynamo.domain.metamodel.DtDefinition)2 DtField (io.vertigo.dynamo.domain.metamodel.DtField)2 Map (java.util.Map)2