Search in sources :

Example 1 with ClusterStateUpdateResponse

use of org.opensearch.cluster.ack.ClusterStateUpdateResponse in project OpenSearch by opensearch-project.

the class MetadataCreateDataStreamService method createDataStream.

public void createDataStream(CreateDataStreamClusterStateUpdateRequest request, ActionListener<AcknowledgedResponse> finalListener) {
    AtomicReference<String> firstBackingIndexRef = new AtomicReference<>();
    ActionListener<ClusterStateUpdateResponse> listener = ActionListener.wrap(response -> {
        if (response.isAcknowledged()) {
            String firstBackingIndexName = firstBackingIndexRef.get();
            assert firstBackingIndexName != null;
            activeShardsObserver.waitForActiveShards(new String[] { firstBackingIndexName }, ActiveShardCount.DEFAULT, request.masterNodeTimeout(), shardsAcked -> {
                finalListener.onResponse(new AcknowledgedResponse(true));
            }, finalListener::onFailure);
        } else {
            finalListener.onResponse(new AcknowledgedResponse(false));
        }
    }, finalListener::onFailure);
    clusterService.submitStateUpdateTask("create-data-stream [" + request.name + "]", new AckedClusterStateUpdateTask<ClusterStateUpdateResponse>(Priority.HIGH, request, listener) {

        @Override
        public ClusterState execute(ClusterState currentState) throws Exception {
            ClusterState clusterState = createDataStream(metadataCreateIndexService, currentState, request);
            firstBackingIndexRef.set(clusterState.metadata().dataStreams().get(request.name).getIndices().get(0).getName());
            return clusterState;
        }

        @Override
        protected ClusterStateUpdateResponse newResponse(boolean acknowledged) {
            return new ClusterStateUpdateResponse(acknowledged);
        }
    });
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) ClusterStateUpdateResponse(org.opensearch.cluster.ack.ClusterStateUpdateResponse) OpenSearchStatusException(org.opensearch.OpenSearchStatusException) IOException(java.io.IOException) ResourceAlreadyExistsException(org.opensearch.ResourceAlreadyExistsException)

Example 2 with ClusterStateUpdateResponse

use of org.opensearch.cluster.ack.ClusterStateUpdateResponse in project OpenSearch by opensearch-project.

the class MetadataIndexStateService method onlyOpenIndex.

private void onlyOpenIndex(final OpenIndexClusterStateUpdateRequest request, final ActionListener<ClusterStateUpdateResponse> listener) {
    if (request.indices() == null || request.indices().length == 0) {
        throw new IllegalArgumentException("Index name is required");
    }
    final String indicesAsString = Arrays.toString(request.indices());
    clusterService.submitStateUpdateTask("open-indices " + indicesAsString, new AckedClusterStateUpdateTask<ClusterStateUpdateResponse>(Priority.URGENT, request, listener) {

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

        @Override
        public ClusterState execute(final ClusterState currentState) {
            final ClusterState updatedState = openIndices(request.indices(), currentState);
            // no explicit wait for other nodes needed as we use AckedClusterStateUpdateTask
            return allocationService.reroute(updatedState, "indices opened [" + indicesAsString + "]");
        }
    });
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) ClusterStateUpdateResponse(org.opensearch.cluster.ack.ClusterStateUpdateResponse) OpenIndexClusterStateUpdateResponse(org.opensearch.cluster.ack.OpenIndexClusterStateUpdateResponse)

Example 3 with ClusterStateUpdateResponse

use of org.opensearch.cluster.ack.ClusterStateUpdateResponse in project OpenSearch by opensearch-project.

the class RepositoriesService method registerRepository.

/**
 * Registers new repository in the cluster
 * <p>
 * This method can be only called on the master node. It tries to create a new repository on the master
 * and if it was successful it adds new repository to cluster metadata.
 *
 * @param request  register repository request
 * @param listener register repository listener
 */
public void registerRepository(final PutRepositoryRequest request, final ActionListener<ClusterStateUpdateResponse> listener) {
    assert lifecycle.started() : "Trying to register new repository but service is in state [" + lifecycle.state() + "]";
    final RepositoryMetadata newRepositoryMetadata = new RepositoryMetadata(request.name(), request.type(), request.settings());
    validate(request.name());
    final ActionListener<ClusterStateUpdateResponse> registrationListener;
    if (request.verify()) {
        registrationListener = ActionListener.delegateFailure(listener, (delegatedListener, clusterStateUpdateResponse) -> {
            if (clusterStateUpdateResponse.isAcknowledged()) {
                // The response was acknowledged - all nodes should know about the new repository, let's verify them
                verifyRepository(request.name(), ActionListener.delegateFailure(delegatedListener, (innerDelegatedListener, discoveryNodes) -> innerDelegatedListener.onResponse(clusterStateUpdateResponse)));
            } else {
                delegatedListener.onResponse(clusterStateUpdateResponse);
            }
        });
    } else {
        registrationListener = listener;
    }
    // Trying to create the new repository on master to make sure it works
    try {
        closeRepository(createRepository(newRepositoryMetadata, typesRegistry));
    } catch (Exception e) {
        registrationListener.onFailure(e);
        return;
    }
    clusterService.submitStateUpdateTask("put_repository [" + request.name() + "]", new AckedClusterStateUpdateTask<ClusterStateUpdateResponse>(request, registrationListener) {

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

        @Override
        public ClusterState execute(ClusterState currentState) {
            ensureRepositoryNotInUse(currentState, request.name());
            Metadata metadata = currentState.metadata();
            Metadata.Builder mdBuilder = Metadata.builder(currentState.metadata());
            RepositoriesMetadata repositories = metadata.custom(RepositoriesMetadata.TYPE);
            if (repositories == null) {
                logger.info("put repository [{}]", request.name());
                repositories = new RepositoriesMetadata(Collections.singletonList(new RepositoryMetadata(request.name(), request.type(), request.settings())));
            } else {
                boolean found = false;
                List<RepositoryMetadata> repositoriesMetadata = new ArrayList<>(repositories.repositories().size() + 1);
                for (RepositoryMetadata repositoryMetadata : repositories.repositories()) {
                    if (repositoryMetadata.name().equals(newRepositoryMetadata.name())) {
                        if (newRepositoryMetadata.equalsIgnoreGenerations(repositoryMetadata)) {
                            // Previous version is the same as this one no update is needed.
                            return currentState;
                        }
                        found = true;
                        repositoriesMetadata.add(newRepositoryMetadata);
                    } else {
                        repositoriesMetadata.add(repositoryMetadata);
                    }
                }
                if (!found) {
                    logger.info("put repository [{}]", request.name());
                    repositoriesMetadata.add(new RepositoryMetadata(request.name(), request.type(), request.settings()));
                } else {
                    logger.info("update repository [{}]", request.name());
                }
                repositories = new RepositoriesMetadata(repositoriesMetadata);
            }
            mdBuilder.putCustom(RepositoriesMetadata.TYPE, repositories);
            return ClusterState.builder(currentState).metadata(mdBuilder).build();
        }

        @Override
        public void onFailure(String source, Exception e) {
            logger.warn(() -> new ParameterizedMessage("failed to create repository [{}]", request.name()), e);
            super.onFailure(source, e);
        }

        @Override
        public boolean mustAck(DiscoveryNode discoveryNode) {
            // repository is created on both master and data nodes
            return discoveryNode.isMasterNode() || discoveryNode.isDataNode();
        }
    });
}
Also used : Metadata(org.opensearch.cluster.metadata.Metadata) ActionRunnable(org.opensearch.action.ActionRunnable) ThreadPool(org.opensearch.threadpool.ThreadPool) SnapshotsInProgress(org.opensearch.cluster.SnapshotsInProgress) HashMap(java.util.HashMap) SnapshotDeletionsInProgress(org.opensearch.cluster.SnapshotDeletionsInProgress) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ClusterStateApplier(org.opensearch.cluster.ClusterStateApplier) Regex(org.opensearch.common.regex.Regex) Strings(org.opensearch.common.Strings) ArrayList(java.util.ArrayList) ConcurrentCollections(org.opensearch.common.util.concurrent.ConcurrentCollections) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Map(java.util.Map) AckedClusterStateUpdateTask(org.opensearch.cluster.AckedClusterStateUpdateTask) ActionListener(org.opensearch.action.ActionListener) RepositoryCleanupInProgress(org.opensearch.cluster.RepositoryCleanupInProgress) Setting(org.opensearch.common.settings.Setting) TimeValue(org.opensearch.common.unit.TimeValue) DeleteRepositoryRequest(org.opensearch.action.admin.cluster.repositories.delete.DeleteRepositoryRequest) RepositoriesMetadata(org.opensearch.cluster.metadata.RepositoriesMetadata) Collection(java.util.Collection) RepositoryMetadata(org.opensearch.cluster.metadata.RepositoryMetadata) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) DiscoveryNodeRole(org.opensearch.cluster.node.DiscoveryNodeRole) TransportService(org.opensearch.transport.TransportService) Collectors(java.util.stream.Collectors) IOUtils(org.opensearch.core.internal.io.IOUtils) AbstractLifecycleComponent(org.opensearch.common.component.AbstractLifecycleComponent) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Stream(java.util.stream.Stream) MeteredBlobStoreRepository(org.opensearch.repositories.blobstore.MeteredBlobStoreRepository) PutRepositoryRequest(org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest) ClusterService(org.opensearch.cluster.service.ClusterService) RestoreInProgress(org.opensearch.cluster.RestoreInProgress) ClusterStateUpdateResponse(org.opensearch.cluster.ack.ClusterStateUpdateResponse) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Metadata(org.opensearch.cluster.metadata.Metadata) RepositoriesMetadata(org.opensearch.cluster.metadata.RepositoriesMetadata) RepositoryMetadata(org.opensearch.cluster.metadata.RepositoryMetadata) IOException(java.io.IOException) RepositoriesMetadata(org.opensearch.cluster.metadata.RepositoriesMetadata) RepositoryMetadata(org.opensearch.cluster.metadata.RepositoryMetadata) ArrayList(java.util.ArrayList) List(java.util.List) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ClusterStateUpdateResponse(org.opensearch.cluster.ack.ClusterStateUpdateResponse)

Example 4 with ClusterStateUpdateResponse

use of org.opensearch.cluster.ack.ClusterStateUpdateResponse in project OpenSearch by opensearch-project.

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.opensearch.action.support.master.AcknowledgedResponse) Index(org.opensearch.index.Index) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ClusterStateUpdateResponse(org.opensearch.cluster.ack.ClusterStateUpdateResponse) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 5 with ClusterStateUpdateResponse

use of org.opensearch.cluster.ack.ClusterStateUpdateResponse in project OpenSearch by opensearch-project.

the class TransportIndicesAliasesAction method masterOperation.

@Override
protected void masterOperation(final IndicesAliasesRequest request, final ClusterState state, final ActionListener<AcknowledgedResponse> listener) {
    // Expand the indices names
    List<IndicesAliasesRequest.AliasActions> actions = request.aliasActions();
    List<AliasAction> finalActions = new ArrayList<>();
    // Resolve all the AliasActions into AliasAction instances and gather all the aliases
    Set<String> aliases = new HashSet<>();
    for (IndicesAliasesRequest.AliasActions action : actions) {
        final Index[] concreteIndices = indexNameExpressionResolver.concreteIndices(state, request.indicesOptions(), false, action.indices());
        for (Index concreteIndex : concreteIndices) {
            IndexAbstraction indexAbstraction = state.metadata().getIndicesLookup().get(concreteIndex.getName());
            assert indexAbstraction != null : "invalid cluster metadata. index [" + concreteIndex.getName() + "] was not found";
            if (indexAbstraction.getParentDataStream() != null) {
                throw new IllegalArgumentException("The provided expressions [" + String.join(",", action.indices()) + "] match a backing index belonging to data stream [" + indexAbstraction.getParentDataStream().getName() + "]. Data streams and their backing indices don't support aliases.");
            }
        }
        final Optional<Exception> maybeException = requestValidators.validateRequest(request, state, concreteIndices);
        if (maybeException.isPresent()) {
            listener.onFailure(maybeException.get());
            return;
        }
        Collections.addAll(aliases, action.getOriginalAliases());
        for (final Index index : concreteIndices) {
            switch(action.actionType()) {
                case ADD:
                    for (String alias : concreteAliases(action, state.metadata(), index.getName())) {
                        finalActions.add(new AliasAction.Add(index.getName(), alias, action.filter(), action.indexRouting(), action.searchRouting(), action.writeIndex(), action.isHidden()));
                    }
                    break;
                case REMOVE:
                    for (String alias : concreteAliases(action, state.metadata(), index.getName())) {
                        finalActions.add(new AliasAction.Remove(index.getName(), alias, action.mustExist()));
                    }
                    break;
                case REMOVE_INDEX:
                    finalActions.add(new AliasAction.RemoveIndex(index.getName()));
                    break;
                default:
                    throw new IllegalArgumentException("Unsupported action [" + action.actionType() + "]");
            }
        }
    }
    if (finalActions.isEmpty() && false == actions.isEmpty()) {
        throw new AliasesNotFoundException(aliases.toArray(new String[aliases.size()]));
    }
    request.aliasActions().clear();
    IndicesAliasesClusterStateUpdateRequest updateRequest = new IndicesAliasesClusterStateUpdateRequest(unmodifiableList(finalActions)).ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout());
    indexAliasesService.indicesAliases(updateRequest, new ActionListener<ClusterStateUpdateResponse>() {

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

        @Override
        public void onFailure(Exception t) {
            logger.debug("failed to perform aliases", t);
            listener.onFailure(t);
        }
    });
}
Also used : ArrayList(java.util.ArrayList) Index(org.opensearch.index.Index) ClusterStateUpdateResponse(org.opensearch.cluster.ack.ClusterStateUpdateResponse) HashSet(java.util.HashSet) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) AliasesNotFoundException(org.opensearch.rest.action.admin.indices.AliasesNotFoundException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) IOException(java.io.IOException) IndexAbstraction(org.opensearch.cluster.metadata.IndexAbstraction) AliasAction(org.opensearch.cluster.metadata.AliasAction) AliasesNotFoundException(org.opensearch.rest.action.admin.indices.AliasesNotFoundException)

Aggregations

ClusterStateUpdateResponse (org.opensearch.cluster.ack.ClusterStateUpdateResponse)7 IOException (java.io.IOException)6 ClusterState (org.opensearch.cluster.ClusterState)5 HashSet (java.util.HashSet)3 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)3 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)3 Index (org.opensearch.index.Index)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Set (java.util.Set)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 ResourceAlreadyExistsException (org.opensearch.ResourceAlreadyExistsException)2 ActionListener (org.opensearch.action.ActionListener)2 AckedClusterStateUpdateTask (org.opensearch.cluster.AckedClusterStateUpdateTask)2 ClusterBlockException (org.opensearch.cluster.block.ClusterBlockException)2 ClusterService (org.opensearch.cluster.service.ClusterService)2 ValidationException (org.opensearch.common.ValidationException)2 Regex (org.opensearch.common.regex.Regex)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1