Search in sources :

Example 1 with AliasAction

use of org.elasticsearch.cluster.metadata.AliasAction 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 2 with AliasAction

use of org.elasticsearch.cluster.metadata.AliasAction in project elasticsearch by elastic.

the class TransportRolloverAction method prepareRolloverAliasesUpdateRequest.

static IndicesAliasesClusterStateUpdateRequest prepareRolloverAliasesUpdateRequest(String oldIndex, String newIndex, RolloverRequest request) {
    List<AliasAction> actions = unmodifiableList(Arrays.asList(new AliasAction.Add(newIndex, request.getAlias(), null, null, null), new AliasAction.Remove(oldIndex, request.getAlias())));
    final IndicesAliasesClusterStateUpdateRequest updateRequest = new IndicesAliasesClusterStateUpdateRequest(actions).ackTimeout(request.ackTimeout()).masterNodeTimeout(request.masterNodeTimeout());
    return updateRequest;
}
Also used : AliasAction(org.elasticsearch.cluster.metadata.AliasAction) IndicesAliasesClusterStateUpdateRequest(org.elasticsearch.action.admin.indices.alias.IndicesAliasesClusterStateUpdateRequest)

Example 3 with AliasAction

use of org.elasticsearch.cluster.metadata.AliasAction in project elasticsearch by elastic.

the class TransportRolloverActionTests method testCreateUpdateAliasRequest.

public void testCreateUpdateAliasRequest() throws Exception {
    String sourceAlias = randomAsciiOfLength(10);
    String sourceIndex = randomAsciiOfLength(10);
    String targetIndex = randomAsciiOfLength(10);
    final RolloverRequest rolloverRequest = new RolloverRequest(sourceAlias, targetIndex);
    final IndicesAliasesClusterStateUpdateRequest updateRequest = TransportRolloverAction.prepareRolloverAliasesUpdateRequest(sourceIndex, targetIndex, rolloverRequest);
    List<AliasAction> actions = updateRequest.actions();
    assertThat(actions, hasSize(2));
    boolean foundAdd = false;
    boolean foundRemove = false;
    for (AliasAction action : actions) {
        if (action.getIndex().equals(targetIndex)) {
            assertEquals(sourceAlias, ((AliasAction.Add) action).getAlias());
            foundAdd = true;
        } else if (action.getIndex().equals(sourceIndex)) {
            assertEquals(sourceAlias, ((AliasAction.Remove) action).getAlias());
            foundRemove = true;
        } else {
            throw new AssertionError("Unknow index [" + action.getIndex() + "]");
        }
    }
    assertTrue(foundAdd);
    assertTrue(foundRemove);
}
Also used : AliasAction(org.elasticsearch.cluster.metadata.AliasAction) IndicesAliasesClusterStateUpdateRequest(org.elasticsearch.action.admin.indices.alias.IndicesAliasesClusterStateUpdateRequest)

Aggregations

AliasAction (org.elasticsearch.cluster.metadata.AliasAction)3 IndicesAliasesClusterStateUpdateRequest (org.elasticsearch.action.admin.indices.alias.IndicesAliasesClusterStateUpdateRequest)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 AliasActions (org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions)1 ClusterStateUpdateResponse (org.elasticsearch.cluster.ack.ClusterStateUpdateResponse)1 ClusterBlockException (org.elasticsearch.cluster.block.ClusterBlockException)1 AliasesNotFoundException (org.elasticsearch.rest.action.admin.indices.AliasesNotFoundException)1