Search in sources :

Example 16 with ClusterStateUpdateResponse

use of org.elasticsearch.cluster.ack.ClusterStateUpdateResponse in project crate by crate.

the class TransportDeleteIndexAction method masterOperation.

@Override
protected void masterOperation(final DeleteIndexRequest request, final ClusterState state, final ActionListener<AcknowledgedResponse> listener) {
    final Set<Index> concreteIndices = new HashSet<>(Arrays.asList(indexNameExpressionResolver.concreteIndices(state, request)));
    if (concreteIndices.isEmpty()) {
        listener.onResponse(new AcknowledgedResponse(true));
        return;
    }
    DeleteIndexClusterStateUpdateRequest deleteRequest = new DeleteIndexClusterStateUpdateRequest().ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout()).indices(concreteIndices.toArray(new Index[concreteIndices.size()]));
    deleteIndexService.deleteIndices(deleteRequest, new ActionListener<ClusterStateUpdateResponse>() {

        @Override
        public void onResponse(ClusterStateUpdateResponse response) {
            listener.onResponse(new AcknowledgedResponse(response.isAcknowledged()));
        }

        @Override
        public void onFailure(Exception t) {
            logger.debug(() -> new ParameterizedMessage("failed to delete indices [{}]", concreteIndices), t);
            listener.onFailure(t);
        }
    });
}
Also used : AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) Index(org.elasticsearch.index.Index) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ClusterStateUpdateResponse(org.elasticsearch.cluster.ack.ClusterStateUpdateResponse) IOException(java.io.IOException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) HashSet(java.util.HashSet)

Example 17 with ClusterStateUpdateResponse

use of org.elasticsearch.cluster.ack.ClusterStateUpdateResponse in project crate by crate.

the class MetadataUpdateSettingsService method updateSettings.

public void updateSettings(final UpdateSettingsClusterStateUpdateRequest request, final ActionListener<ClusterStateUpdateResponse> listener) {
    final Settings normalizedSettings = Settings.builder().put(request.settings()).normalizePrefix(IndexMetadata.INDEX_SETTING_PREFIX).build();
    Settings.Builder settingsForClosedIndices = Settings.builder();
    Settings.Builder settingsForOpenIndices = Settings.builder();
    final Set<String> skippedSettings = new HashSet<>();
    indexScopedSettings.validate(// don't validate wildcards
    normalizedSettings.filter(s -> Regex.isSimpleMatchPattern(s) == false), // don't validate dependencies here we check it below never allow to change the number of shards
    false, // validate internal or private index settings
    true);
    for (String key : normalizedSettings.keySet()) {
        Setting setting = indexScopedSettings.get(key);
        boolean isWildcard = setting == null && Regex.isSimpleMatchPattern(key);
        assert // we already validated the normalized settings
        setting != null || (isWildcard && normalizedSettings.hasValue(key) == false) : "unknown setting: " + key + " isWildcard: " + isWildcard + " hasValue: " + normalizedSettings.hasValue(key);
        settingsForClosedIndices.copy(key, normalizedSettings);
        if (isWildcard || setting.isDynamic()) {
            settingsForOpenIndices.copy(key, normalizedSettings);
        } else {
            skippedSettings.add(key);
        }
    }
    final Settings closedSettings = settingsForClosedIndices.build();
    final Settings openSettings = settingsForOpenIndices.build();
    final boolean preserveExisting = request.isPreserveExisting();
    clusterService.submitStateUpdateTask("update-settings", new AckedClusterStateUpdateTask<ClusterStateUpdateResponse>(Priority.URGENT, request, listener) {

        @Override
        protected ClusterStateUpdateResponse newResponse(boolean acknowledged) {
            return new ClusterStateUpdateResponse(acknowledged);
        }

        @Override
        public ClusterState execute(ClusterState currentState) {
            RoutingTable.Builder routingTableBuilder = RoutingTable.builder(currentState.routingTable());
            Metadata.Builder metadataBuilder = Metadata.builder(currentState.metadata());
            // allow to change any settings to a close index, and only allow dynamic settings to be changed
            // on an open index
            Set<Index> openIndices = new HashSet<>();
            Set<Index> closeIndices = new HashSet<>();
            final String[] actualIndices = new String[request.indices().length];
            for (int i = 0; i < request.indices().length; i++) {
                Index index = request.indices()[i];
                actualIndices[i] = index.getName();
                final IndexMetadata metadata = currentState.metadata().getIndexSafe(index);
                if (metadata.getState() == IndexMetadata.State.OPEN) {
                    openIndices.add(index);
                } else {
                    closeIndices.add(index);
                }
            }
            if (!skippedSettings.isEmpty() && !openIndices.isEmpty()) {
                throw new IllegalArgumentException(String.format(Locale.ROOT, "Can't update non dynamic settings [%s] for open indices %s", skippedSettings, openIndices));
            }
            if (IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.exists(openSettings)) {
                final int updatedNumberOfReplicas = IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.get(openSettings);
                if (preserveExisting == false) {
                    // Verify that this won't take us over the cluster shard limit.
                    int totalNewShards = Arrays.stream(request.indices()).mapToInt(i -> getTotalNewShards(i, currentState, updatedNumberOfReplicas)).sum();
                    Optional<String> error = shardLimitValidator.checkShardLimit(totalNewShards, currentState);
                    if (error.isPresent()) {
                        ValidationException ex = new ValidationException();
                        ex.addValidationError(error.get());
                        throw ex;
                    }
                    /*
                                 * We do not update the in-sync allocation IDs as they will be removed upon the first index operation which makes
                                 * these copies stale.
                                 *
                                 * TODO: should we update the in-sync allocation IDs once the data is deleted by the node?
                                 */
                    routingTableBuilder.updateNumberOfReplicas(updatedNumberOfReplicas, actualIndices);
                    metadataBuilder.updateNumberOfReplicas(updatedNumberOfReplicas, actualIndices);
                    LOGGER.info("updating number_of_replicas to [{}] for indices {}", updatedNumberOfReplicas, actualIndices);
                }
            }
            ClusterBlocks.Builder blocks = ClusterBlocks.builder().blocks(currentState.blocks());
            maybeUpdateClusterBlock(actualIndices, blocks, IndexMetadata.INDEX_READ_ONLY_BLOCK, IndexMetadata.INDEX_READ_ONLY_SETTING, openSettings);
            maybeUpdateClusterBlock(actualIndices, blocks, IndexMetadata.INDEX_READ_ONLY_ALLOW_DELETE_BLOCK, IndexMetadata.INDEX_BLOCKS_READ_ONLY_ALLOW_DELETE_SETTING, openSettings);
            maybeUpdateClusterBlock(actualIndices, blocks, IndexMetadata.INDEX_METADATA_BLOCK, IndexMetadata.INDEX_BLOCKS_METADATA_SETTING, openSettings);
            maybeUpdateClusterBlock(actualIndices, blocks, IndexMetadata.INDEX_WRITE_BLOCK, IndexMetadata.INDEX_BLOCKS_WRITE_SETTING, openSettings);
            maybeUpdateClusterBlock(actualIndices, blocks, IndexMetadata.INDEX_READ_BLOCK, IndexMetadata.INDEX_BLOCKS_READ_SETTING, openSettings);
            if (!openIndices.isEmpty()) {
                for (Index index : openIndices) {
                    IndexMetadata indexMetadata = metadataBuilder.getSafe(index);
                    Settings.Builder updates = Settings.builder();
                    Settings.Builder indexSettings = Settings.builder().put(indexMetadata.getSettings());
                    if (indexScopedSettings.updateDynamicSettings(openSettings, indexSettings, updates, index.getName())) {
                        if (preserveExisting) {
                            indexSettings.put(indexMetadata.getSettings());
                        }
                        Settings finalSettings = indexSettings.build();
                        indexScopedSettings.validate(finalSettings.filter(k -> indexScopedSettings.isPrivateSetting(k) == false), true);
                        metadataBuilder.put(IndexMetadata.builder(indexMetadata).settings(finalSettings));
                    }
                }
            }
            if (!closeIndices.isEmpty()) {
                for (Index index : closeIndices) {
                    IndexMetadata indexMetadata = metadataBuilder.getSafe(index);
                    Settings.Builder updates = Settings.builder();
                    Settings.Builder indexSettings = Settings.builder().put(indexMetadata.getSettings());
                    if (indexScopedSettings.updateSettings(closedSettings, indexSettings, updates, index.getName())) {
                        if (preserveExisting) {
                            indexSettings.put(indexMetadata.getSettings());
                        }
                        Settings finalSettings = indexSettings.build();
                        indexScopedSettings.validate(finalSettings.filter(k -> indexScopedSettings.isPrivateSetting(k) == false), true);
                        metadataBuilder.put(IndexMetadata.builder(indexMetadata).settings(finalSettings));
                    }
                }
            }
            // increment settings versions
            for (final String index : actualIndices) {
                if (same(currentState.metadata().index(index).getSettings(), metadataBuilder.get(index).getSettings()) == false) {
                    final IndexMetadata.Builder builder = IndexMetadata.builder(metadataBuilder.get(index));
                    builder.settingsVersion(1 + builder.settingsVersion());
                    metadataBuilder.put(builder);
                }
            }
            ClusterState updatedState = ClusterState.builder(currentState).metadata(metadataBuilder).routingTable(routingTableBuilder.build()).blocks(blocks).build();
            // now, reroute in case things change that require it (like number of replicas)
            updatedState = allocationService.reroute(updatedState, "settings update");
            try {
                for (Index index : openIndices) {
                    final IndexMetadata currentMetadata = currentState.getMetadata().getIndexSafe(index);
                    final IndexMetadata updatedMetadata = updatedState.metadata().getIndexSafe(index);
                    indicesService.verifyIndexMetadata(currentMetadata, updatedMetadata);
                }
                for (Index index : closeIndices) {
                    final IndexMetadata currentMetadata = currentState.getMetadata().getIndexSafe(index);
                    final IndexMetadata updatedMetadata = updatedState.metadata().getIndexSafe(index);
                    // Verifies that the current index settings can be updated with the updated dynamic settings.
                    indicesService.verifyIndexMetadata(currentMetadata, updatedMetadata);
                    // Now check that we can create the index with the updated settings (dynamic and non-dynamic).
                    // This step is mandatory since we allow to update non-dynamic settings on closed indices.
                    indicesService.verifyIndexMetadata(updatedMetadata, updatedMetadata);
                }
            } catch (IOException ex) {
                throw new UncheckedIOException(ex);
            }
            return updatedState;
        }
    });
}
Also used : Arrays(java.util.Arrays) AckedClusterStateUpdateTask(org.elasticsearch.cluster.AckedClusterStateUpdateTask) Tuple(io.crate.common.collections.Tuple) ShardLimitValidator(org.elasticsearch.indices.ShardLimitValidator) ClusterService(org.elasticsearch.cluster.service.ClusterService) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService) UpgradeSettingsClusterStateUpdateRequest(org.elasticsearch.action.admin.indices.upgrade.post.UpgradeSettingsClusterStateUpdateRequest) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) Index(org.elasticsearch.index.Index) Inject(org.elasticsearch.common.inject.Inject) HashSet(java.util.HashSet) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) ClusterBlock(org.elasticsearch.cluster.block.ClusterBlock) Locale(java.util.Locale) Map(java.util.Map) Regex(org.elasticsearch.common.regex.Regex) ValidationException(org.elasticsearch.common.ValidationException) IndicesService(org.elasticsearch.indices.IndicesService) Priority(org.elasticsearch.common.Priority) Setting(org.elasticsearch.common.settings.Setting) IndexSettings.same(org.elasticsearch.index.IndexSettings.same) Set(java.util.Set) IOException(java.io.IOException) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) UncheckedIOException(java.io.UncheckedIOException) Logger(org.apache.logging.log4j.Logger) Version(org.elasticsearch.Version) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) Optional(java.util.Optional) ClusterStateUpdateResponse(org.elasticsearch.cluster.ack.ClusterStateUpdateResponse) UpdateSettingsClusterStateUpdateRequest(org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsClusterStateUpdateRequest) LogManager(org.apache.logging.log4j.LogManager) ActionListener(org.elasticsearch.action.ActionListener) ClusterState(org.elasticsearch.cluster.ClusterState) HashSet(java.util.HashSet) Set(java.util.Set) ValidationException(org.elasticsearch.common.ValidationException) Optional(java.util.Optional) Setting(org.elasticsearch.common.settings.Setting) Index(org.elasticsearch.index.Index) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ClusterStateUpdateResponse(org.elasticsearch.cluster.ack.ClusterStateUpdateResponse) Settings(org.elasticsearch.common.settings.Settings) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) HashSet(java.util.HashSet)

Aggregations

ClusterStateUpdateResponse (org.elasticsearch.cluster.ack.ClusterStateUpdateResponse)17 Index (org.elasticsearch.index.Index)12 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)10 Supplier (org.apache.logging.log4j.util.Supplier)8 ClusterState (org.elasticsearch.cluster.ClusterState)8 ClusterBlockException (org.elasticsearch.cluster.block.ClusterBlockException)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 List (java.util.List)5 Settings (org.elasticsearch.common.settings.Settings)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Set (java.util.Set)4 IndexScopedSettings (org.elasticsearch.common.settings.IndexScopedSettings)4 Version (org.elasticsearch.Version)3 ActionListener (org.elasticsearch.action.ActionListener)3 AckedClusterStateUpdateTask (org.elasticsearch.cluster.AckedClusterStateUpdateTask)3 ClusterService (org.elasticsearch.cluster.service.ClusterService)3 Regex (org.elasticsearch.common.regex.Regex)3