Search in sources :

Example 21 with AcknowledgedResponse

use of org.elasticsearch.action.support.master.AcknowledgedResponse in project sonarqube by SonarSource.

the class IndexCreator method createIndex.

private void createIndex(BuiltIndex<?> builtIndex, boolean useMetadata) {
    Index index = builtIndex.getMainType().getIndex();
    LOGGER.info(String.format("Create index [%s]", index.getName()));
    Settings.Builder settings = Settings.builder();
    settings.put(builtIndex.getSettings());
    if (useMetadata) {
        metadataIndex.setHash(index, IndexDefinitionHash.of(builtIndex));
        metadataIndex.setInitialized(builtIndex.getMainType(), false);
        builtIndex.getRelationTypes().forEach(relationType -> metadataIndex.setInitialized(relationType, false));
    }
    CreateIndexResponse indexResponse = client.create(new CreateIndexRequest(index.getName()).settings((settings)));
    if (!indexResponse.isAcknowledged()) {
        throw new IllegalStateException("Failed to create index [" + index.getName() + "]");
    }
    client.waitForStatus(ClusterHealthStatus.YELLOW);
    // create types
    LOGGER.info("Create type {}", builtIndex.getMainType().format());
    AcknowledgedResponse mappingResponse = client.putMapping(new PutMappingRequest(builtIndex.getMainType().getIndex().getName()).type(builtIndex.getMainType().getType()).source(builtIndex.getAttributes()));
    if (!mappingResponse.isAcknowledged()) {
        throw new IllegalStateException("Failed to create type " + builtIndex.getMainType().getType());
    }
    client.waitForStatus(ClusterHealthStatus.YELLOW);
}
Also used : PutMappingRequest(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) BuiltIndex(org.sonar.server.es.newindex.BuiltIndex) MetadataIndex(org.sonar.server.es.metadata.MetadataIndex) NewIndex(org.sonar.server.es.newindex.NewIndex) CreateIndexResponse(org.elasticsearch.client.indices.CreateIndexResponse) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) Settings(org.elasticsearch.common.settings.Settings)

Example 22 with AcknowledgedResponse

use of org.elasticsearch.action.support.master.AcknowledgedResponse in project snow-owl by b2ihealthcare.

the class EsIndexAdmin method create.

@Override
public void create() {
    log.info("Preparing '{}' indexes...", name);
    // register any type that requires a refresh at the end of the index create/open
    Set<DocumentMapping> mappingsToRefresh = Sets.newHashSet();
    // create number of indexes based on number of types
    for (DocumentMapping mapping : mappings.getMappings()) {
        final String index = getTypeIndex(mapping);
        final Map<String, Object> typeMapping = ImmutableMap.<String, Object>builder().put("date_detection", false).put("numeric_detection", false).put("dynamic_templates", List.of(stringsAsKeywords())).putAll(toProperties(mapping)).build();
        if (exists(mapping)) {
            // update mapping if required
            final MappingMetadata currentIndexMapping;
            try {
                final GetMappingsRequest getMappingsRequest = new GetMappingsRequest().indices(index);
                currentIndexMapping = client.indices().getMapping(getMappingsRequest).mappings().get(index);
            } catch (Exception e) {
                throw new IndexException(String.format("Failed to get mapping of '%s' for type '%s'", name, mapping.typeAsString()), e);
            }
            try {
                final ObjectNode newTypeMapping = mapper.valueToTree(typeMapping);
                final ObjectNode currentTypeMapping = mapper.valueToTree(currentIndexMapping.getSourceAsMap());
                SortedSet<String> compatibleChanges = Sets.newTreeSet();
                SortedSet<String> incompatibleChanges = Sets.newTreeSet();
                JsonDiff.diff(currentTypeMapping, newTypeMapping).forEach(change -> {
                    if (change.isAdd()) {
                        compatibleChanges.add(change.getFieldPath());
                    } else if (change.isMove() || change.isReplace()) {
                        incompatibleChanges.add(change.getFieldPath());
                    }
                });
                if (!incompatibleChanges.isEmpty()) {
                    log.warn("Cannot migrate index '{}' to new mapping with breaking changes on properties '{}'. Run repository reindex to migrate to new mapping schema or drop that index manually using the Elasticsearch API.", index, incompatibleChanges);
                } else if (!compatibleChanges.isEmpty()) {
                    log.info("Applying mapping changes {} in index {}", compatibleChanges, index);
                    PutMappingRequest putMappingRequest = new PutMappingRequest(index).source(typeMapping);
                    AcknowledgedResponse response = client.indices().updateMapping(putMappingRequest);
                    checkState(response.isAcknowledged(), "Failed to update mapping '%s' for type '%s'", name, mapping.typeAsString());
                    // new fields do not require reindex, they will be added to new documents, existing documents don't have any data that needs reindex
                    if (hasFieldAliasChange(compatibleChanges)) {
                        if (bulkIndexByScroll(client, mapping, Expressions.matchAll(), "update", null, /*no script, in place update of docs to pick up mapping changes*/
                        "mapping migration")) {
                            mappingsToRefresh.add(mapping);
                        }
                        log.info("Migrated documents to new mapping in index '{}'", index);
                    }
                }
            } catch (IOException e) {
                throw new IndexException(String.format("Failed to update mapping '%s' for type '%s'", name, mapping.typeAsString()), e);
            }
        } else {
            // create index
            final Map<String, Object> indexSettings;
            try {
                indexSettings = createIndexSettings();
                log.info("Configuring '{}' index with settings: {}", index, indexSettings);
            } catch (IOException e) {
                throw new IndexException("Couldn't prepare settings for index " + index, e);
            }
            final CreateIndexRequest createIndexRequest = new CreateIndexRequest(index).mapping(typeMapping).settings(indexSettings);
            try {
                final CreateIndexResponse response = client.indices().create(createIndexRequest);
                checkState(response.isAcknowledged(), "Failed to create index '%s' for type '%s'", name, mapping.typeAsString());
            } catch (Exception e) {
                throw new IndexException(String.format("Failed to create index '%s' for type '%s'", name, mapping.typeAsString()), e);
            }
        }
    }
    // wait until the cluster processes each index create request
    waitForYellowHealth(indices());
    if (!mappingsToRefresh.isEmpty()) {
        refresh(mappingsToRefresh);
    }
    log.info("'{}' indexes are ready.", name);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) PutMappingRequest(org.elasticsearch.client.indices.PutMappingRequest) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) IOException(java.io.IOException) GetMappingsRequest(org.elasticsearch.client.indices.GetMappingsRequest) DocumentMapping(com.b2international.index.mapping.DocumentMapping) IOException(java.io.IOException) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) CreateIndexResponse(org.elasticsearch.client.indices.CreateIndexResponse) MappingMetadata(org.elasticsearch.cluster.metadata.MappingMetadata)

Example 23 with AcknowledgedResponse

use of org.elasticsearch.action.support.master.AcknowledgedResponse in project crate by crate.

the class TransportSchemaUpdateAction method updateTemplate.

private CompletableFuture<AcknowledgedResponse> updateTemplate(ImmutableOpenMap<String, IndexTemplateMetadata> templates, String indexName, String mappingSource, TimeValue timeout) {
    CompletableFuture<AcknowledgedResponse> future = new CompletableFuture<>();
    String templateName = PartitionName.templateName(indexName);
    Map<String, Object> newMapping;
    try {
        XContentParser parser = JsonXContent.JSON_XCONTENT.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, mappingSource);
        newMapping = parser.map();
        if (newMappingAlreadyApplied(templates.get(templateName), newMapping)) {
            return CompletableFuture.completedFuture(new AcknowledgedResponse(true));
        }
    } catch (Exception e) {
        return CompletableFuture.failedFuture(e);
    }
    clusterService.submitStateUpdateTask("update-template-mapping", new ClusterStateUpdateTask(Priority.HIGH) {

        @Override
        public ClusterState execute(ClusterState currentState) throws Exception {
            return updateTemplate(xContentRegistry, currentState, templateName, newMapping);
        }

        @Override
        public TimeValue timeout() {
            return timeout;
        }

        @Override
        public void onFailure(String source, Exception e) {
            future.completeExceptionally(e);
        }

        @Override
        public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
            future.complete(new AcknowledgedResponse(true));
        }
    });
    return future;
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) CompletableFuture(java.util.concurrent.CompletableFuture) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) XContentParser(org.elasticsearch.common.xcontent.XContentParser) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException) IOException(java.io.IOException) TimeValue(io.crate.common.unit.TimeValue)

Example 24 with AcknowledgedResponse

use of org.elasticsearch.action.support.master.AcknowledgedResponse in project crate by crate.

the class AlterTableOperation method deleteIndex.

private CompletableFuture<Long> deleteIndex(String... indexNames) {
    DeleteIndexRequest request = new DeleteIndexRequest(indexNames);
    FutureActionListener<AcknowledgedResponse, Long> listener = new FutureActionListener<>(r -> 0L);
    transportDeleteIndexAction.execute(request, listener);
    return listener;
}
Also used : AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) FutureActionListener(io.crate.action.FutureActionListener)

Example 25 with AcknowledgedResponse

use of org.elasticsearch.action.support.master.AcknowledgedResponse in project crate by crate.

the class AlterTableOperation method swapAndDropIndex.

private CompletableFuture<Long> swapAndDropIndex(String source, String target) {
    SwapAndDropIndexRequest request = new SwapAndDropIndexRequest(source, target);
    FutureActionListener<AcknowledgedResponse, Long> listener = new FutureActionListener<>(response -> {
        if (!response.isAcknowledged()) {
            throw new RuntimeException("Publishing new cluster state during Shrink operation (rename phase) " + "has timed out");
        }
        return 0L;
    });
    transportSwapAndDropIndexNameAction.execute(request, listener);
    return listener;
}
Also used : SwapAndDropIndexRequest(io.crate.execution.ddl.index.SwapAndDropIndexRequest) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) FutureActionListener(io.crate.action.FutureActionListener)

Aggregations

AcknowledgedResponse (org.elasticsearch.action.support.master.AcknowledgedResponse)37 IOException (java.io.IOException)11 FutureActionListener (io.crate.action.FutureActionListener)9 ClusterState (org.elasticsearch.cluster.ClusterState)7 Row1 (io.crate.data.Row1)6 Map (java.util.Map)6 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)6 ClusterBlockException (org.elasticsearch.cluster.block.ClusterBlockException)6 Metadata (org.elasticsearch.cluster.metadata.Metadata)6 Row (io.crate.data.Row)5 RelationName (io.crate.metadata.RelationName)5 Arrays (java.util.Arrays)5 HashMap (java.util.HashMap)5 List (java.util.List)5 Reference (io.crate.metadata.Reference)4 DataType (io.crate.types.DataType)4 ArrayList (java.util.ArrayList)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 StreamSupport (java.util.stream.StreamSupport)4 ClusterService (org.elasticsearch.cluster.service.ClusterService)4