@Override
  protected void masterOperation(
      final IndicesAliasesRequest request,
      final ClusterState state,
      final ActionListener<IndicesAliasesResponse> listener)
      throws ElasticsearchException {

    // Expand the indices names
    List<AliasActions> actions = request.aliasActions();
    List<AliasAction> finalActions = new ArrayList<>();
    boolean hasOnlyDeletesButNoneCanBeDone = true;
    Set<String> aliases = new HashSet<>();
    for (AliasActions action : actions) {
      // expand indices
      String[] concreteIndices =
          state.metaData().concreteIndices(request.indicesOptions(), action.indices());
      // collect the aliases
      Collections.addAll(aliases, action.aliases());
      for (String index : concreteIndices) {
        for (String alias : action.concreteAliases(state.metaData(), index)) {
          AliasAction finalAction = new AliasAction(action.aliasAction());
          finalAction.index(index);
          finalAction.alias(alias);
          finalActions.add(finalAction);
          // if there is only delete requests, none will be added if the types do not map to any
          // existing type
          hasOnlyDeletesButNoneCanBeDone = false;
        }
      }
    }
    if (hasOnlyDeletesButNoneCanBeDone && actions.size() != 0) {
      throw new AliasesMissingException(aliases.toArray(new String[aliases.size()]));
    }
    request.aliasActions().clear();
    IndicesAliasesClusterStateUpdateRequest updateRequest =
        new IndicesAliasesClusterStateUpdateRequest()
            .ackTimeout(request.timeout())
            .masterNodeTimeout(request.masterNodeTimeout())
            .actions(finalActions.toArray(new AliasAction[finalActions.size()]));

    indexAliasesService.indicesAliases(
        updateRequest,
        new ActionListener<ClusterStateUpdateResponse>() {
          @Override
          public void onResponse(ClusterStateUpdateResponse response) {
            listener.onResponse(new IndicesAliasesResponse(response.isAcknowledged()));
          }

          @Override
          public void onFailure(Throwable t) {
            logger.debug("failed to perform aliases", t);
            listener.onFailure(t);
          }
        });
  }
 @Override
 protected ClusterBlockException checkBlock(IndicesAliasesRequest request, ClusterState state) {
   Set<String> indices = Sets.newHashSet();
   for (AliasActions aliasAction : request.aliasActions()) {
     for (String index : aliasAction.indices()) {
       indices.add(index);
     }
   }
   return state
       .blocks()
       .indicesBlockedException(
           ClusterBlockLevel.METADATA_WRITE, indices.toArray(new String[indices.size()]));
 }