Search in sources :

Example 1 with ClusterStateUpdateResponse

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

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 RegisterRepositoryRequest request, final ActionListener<ClusterStateUpdateResponse> listener) {
    final RepositoryMetaData newRepositoryMetaData = new RepositoryMetaData(request.name, request.type, request.settings);
    final ActionListener<ClusterStateUpdateResponse> registrationListener;
    if (request.verify) {
        registrationListener = new VerifyingRegisterRepositoryListener(request.name, listener);
    } else {
        registrationListener = listener;
    }
    clusterService.submitStateUpdateTask(request.cause, new AckedClusterStateUpdateTask<ClusterStateUpdateResponse>(request, registrationListener) {

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

        @Override
        public ClusterState execute(ClusterState currentState) throws IOException {
            ensureRepositoryNotInUse(currentState, request.name);
            // Trying to create the new repository on master to make sure it works
            if (!registerRepository(newRepositoryMetaData)) {
                // The new repository has the same settings as the old one - ignore
                return currentState;
            }
            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(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())) {
                        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.toArray(new RepositoryMetaData[repositoriesMetaData.size()]));
            }
            mdBuilder.putCustom(RepositoriesMetaData.TYPE, repositories);
            return ClusterState.builder(currentState).metaData(mdBuilder).build();
        }

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

        @Override
        public boolean mustAck(DiscoveryNode discoveryNode) {
            return discoveryNode.isMasterNode();
        }
    });
}
Also used : RepositoriesMetaData(org.elasticsearch.cluster.metadata.RepositoriesMetaData) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) IOException(java.io.IOException) IOException(java.io.IOException) MetaData(org.elasticsearch.cluster.metadata.MetaData) RepositoriesMetaData(org.elasticsearch.cluster.metadata.RepositoriesMetaData) RepositoryMetaData(org.elasticsearch.cluster.metadata.RepositoryMetaData) RepositoryMetaData(org.elasticsearch.cluster.metadata.RepositoryMetaData) ArrayList(java.util.ArrayList) List(java.util.List) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ClusterStateUpdateResponse(org.elasticsearch.cluster.ack.ClusterStateUpdateResponse)

Example 2 with ClusterStateUpdateResponse

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

the class TransportCloseIndexAction method masterOperation.

@Override
protected void masterOperation(final CloseIndexRequest request, final ClusterState state, final ActionListener<CloseIndexResponse> listener) {
    final Index[] concreteIndices = indexNameExpressionResolver.concreteIndices(state, request);
    CloseIndexClusterStateUpdateRequest updateRequest = new CloseIndexClusterStateUpdateRequest().ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout()).indices(concreteIndices);
    indexStateService.closeIndex(updateRequest, new ActionListener<ClusterStateUpdateResponse>() {

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

        @Override
        public void onFailure(Exception t) {
            logger.debug((Supplier<?>) () -> new ParameterizedMessage("failed to close indices [{}]", (Object) concreteIndices), t);
            listener.onFailure(t);
        }
    });
}
Also used : Index(org.elasticsearch.index.Index) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ClusterStateUpdateResponse(org.elasticsearch.cluster.ack.ClusterStateUpdateResponse) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException)

Example 3 with ClusterStateUpdateResponse

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

the class TransportDeleteIndexAction method masterOperation.

@Override
protected void masterOperation(final DeleteIndexRequest request, final ClusterState state, final ActionListener<DeleteIndexResponse> listener) {
    final Set<Index> concreteIndices = new HashSet<>(Arrays.asList(indexNameExpressionResolver.concreteIndices(state, request)));
    if (concreteIndices.isEmpty()) {
        listener.onResponse(new DeleteIndexResponse(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 DeleteIndexResponse(response.isAcknowledged()));
        }

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

Example 4 with ClusterStateUpdateResponse

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

the class TransportIndicesAliasesAction method masterOperation.

@Override
protected void masterOperation(final IndicesAliasesRequest request, final ClusterState state, final ActionListener<IndicesAliasesResponse> listener) {
    //Expand the indices names
    List<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 (AliasActions action : actions) {
        String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(state, request.indicesOptions(), action.indices());
        Collections.addAll(aliases, action.aliases());
        for (String index : concreteIndices) {
            switch(action.actionType()) {
                case ADD:
                    for (String alias : action.concreteAliases(state.metaData(), index)) {
                        finalActions.add(new AliasAction.Add(index, alias, action.filter(), action.indexRouting(), action.searchRouting()));
                    }
                    break;
                case REMOVE:
                    for (String alias : action.concreteAliases(state.metaData(), index)) {
                        finalActions.add(new AliasAction.Remove(index, alias));
                    }
                    break;
                case REMOVE_INDEX:
                    finalActions.add(new AliasAction.RemoveIndex(index));
                    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 IndicesAliasesResponse(response.isAcknowledged()));
        }

        @Override
        public void onFailure(Exception t) {
            logger.debug("failed to perform aliases", t);
            listener.onFailure(t);
        }
    });
}
Also used : ArrayList(java.util.ArrayList) AliasActions(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions) AliasesNotFoundException(org.elasticsearch.rest.action.admin.indices.AliasesNotFoundException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) AliasAction(org.elasticsearch.cluster.metadata.AliasAction) AliasesNotFoundException(org.elasticsearch.rest.action.admin.indices.AliasesNotFoundException) ClusterStateUpdateResponse(org.elasticsearch.cluster.ack.ClusterStateUpdateResponse) HashSet(java.util.HashSet)

Example 5 with ClusterStateUpdateResponse

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

the class TransportOpenIndexAction method masterOperation.

@Override
protected void masterOperation(final OpenIndexRequest request, final ClusterState state, final ActionListener<OpenIndexResponse> listener) {
    final Index[] concreteIndices = indexNameExpressionResolver.concreteIndices(state, request);
    OpenIndexClusterStateUpdateRequest updateRequest = new OpenIndexClusterStateUpdateRequest().ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout()).indices(concreteIndices);
    indexStateService.openIndex(updateRequest, new ActionListener<ClusterStateUpdateResponse>() {

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

        @Override
        public void onFailure(Exception t) {
            logger.debug((Supplier<?>) () -> new ParameterizedMessage("failed to open indices [{}]", (Object) concreteIndices), t);
            listener.onFailure(t);
        }
    });
}
Also used : Index(org.elasticsearch.index.Index) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ClusterStateUpdateResponse(org.elasticsearch.cluster.ack.ClusterStateUpdateResponse) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException)

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