<commit_before>/*
 * Licensed to Elastic Search and <NAME> under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Elastic Search licenses this
 * file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.cluster.metadata;

import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateUpdateTask;
import org.elasticsearch.cluster.action.index.NodeIndexDeletedAction;
import org.elasticsearch.cluster.block.ClusterBlocks;
import org.elasticsearch.cluster.routing.IndexRoutingTable;
import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
import org.elasticsearch.cluster.routing.RoutingTable;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.cluster.routing.allocation.RoutingAllocation;
import org.elasticsearch.cluster.routing.allocation.ShardsAllocation;
import org.elasticsearch.common.collect.Sets;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.timer.Timeout;
import org.elasticsearch.common.timer.TimerTask;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.Index;
import org.elasticsearch.indices.IndexMissingException;
import org.elasticsearch.timer.TimerService;

import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import static org.elasticsearch.cluster.ClusterState.*;
import static org.elasticsearch.cluster.metadata.MetaData.*;

/**
 * @author kimchy (shay.banon)
 */
public class MetaDataDeleteIndexService extends AbstractComponent {

    private final TimerService timerService;

    private final ClusterService clusterService;

    private final ShardsAllocation shardsAllocation;

    private final NodeIndexDeletedAction nodeIndexDeletedAction;

    @Inject public MetaDataDeleteIndexService(Settings settings, TimerService timerService, ClusterService clusterService, ShardsAllocation shardsAllocation,
                                              NodeIndexDeletedAction nodeIndexDeletedAction) {
        super(settings);
        this.timerService = timerService;
        this.clusterService = clusterService;
        this.shardsAllocation = shardsAllocation;
        this.nodeIndexDeletedAction = nodeIndexDeletedAction;
    }

    public void deleteIndex(final Request request, final Listener userListener) {
        clusterService.submitStateUpdateTask("delete-index [" + request.index + "]", new ClusterStateUpdateTask() {
            @Override public ClusterState execute(ClusterState currentState) {
                final DeleteIndexListener listener = new DeleteIndexListener(request, userListener);
                try {
                    RoutingTable routingTable = currentState.routingTable();
                    if (!routingTable.hasIndex(request.index)) {
                        listener.onFailure(new IndexMissingException(new Index(request.index)));
                        return currentState;
                    }

                    logger.info("[{}] deleting index", request.index);

                    RoutingTable.Builder routingTableBuilder = new RoutingTable.Builder();
                    for (IndexRoutingTable indexRoutingTable : currentState.routingTable().indicesRouting().values()) {
                        if (!indexRoutingTable.index().equals(request.index)) {
                            routingTableBuilder.add(indexRoutingTable);
                        }
                    }
                    MetaData newMetaData = newMetaDataBuilder()
                            .metaData(currentState.metaData())
                            .remove(request.index)
                            .build();

                    RoutingAllocation.Result routingResult = shardsAllocation.reroute(
                            newClusterStateBuilder().state(currentState).routingTable(routingTableBuilder).metaData(newMetaData).build());

                    ClusterBlocks blocks = ClusterBlocks.builder().blocks(currentState.blocks()).removeIndexBlocks(request.index).build();

                    // initialize the counter only for nodes the shards are allocated to
                    Set<String> allocatedNodes = Sets.newHashSet();
                    for (IndexShardRoutingTable indexShardRoutingTable : currentState.routingTable().index(request.index)) {
                        for (ShardRouting shardRouting : indexShardRoutingTable) {
                            if (shardRouting.currentNodeId() != null) {
                                allocatedNodes.add(shardRouting.currentNodeId());
                            }
                            if (shardRouting.relocatingNodeId() != null) {
                                allocatedNodes.add(shardRouting.relocatingNodeId());
                            }
                        }
                    }
                    final AtomicInteger counter = new AtomicInteger(allocatedNodes.size());

                    final NodeIndexDeletedAction.Listener nodeIndexDeleteListener = new NodeIndexDeletedAction.Listener() {
                        @Override public void onNodeIndexDeleted(String index, String nodeId) {
                            if (index.equals(request.index)) {
                                if (counter.decrementAndGet() == 0) {
                                    listener.onResponse(new Response(true));
                                    nodeIndexDeletedAction.remove(this);
                                }
                            }
                        }
                    };
                    nodeIndexDeletedAction.add(nodeIndexDeleteListener);

                    Timeout timeoutTask = timerService.newTimeout(new TimerTask() {
                        @Override public void run(Timeout timeout) throws Exception {
                            listener.onResponse(new Response(false));
                            nodeIndexDeletedAction.remove(nodeIndexDeleteListener);
                        }
                    }, request.timeout, TimerService.ExecutionType.THREADED);
                    listener.timeout = timeoutTask;


                    return newClusterStateBuilder().state(currentState).routingResult(routingResult).metaData(newMetaData).blocks(blocks).build();
                } catch (Exception e) {
                    listener.onFailure(e);
                    return currentState;
                }
            }
        });
    }

    class DeleteIndexListener implements Listener {

        private AtomicBoolean notified = new AtomicBoolean();

        private final Request request;

        private final Listener listener;

        volatile Timeout timeout;

        private DeleteIndexListener(Request request, Listener listener) {
            this.request = request;
            this.listener = listener;
        }

        @Override public void onResponse(final Response response) {
            if (notified.compareAndSet(false, true)) {
                if (timeout != null) {<commit_msg>if no shards are allocated for a deleted index, then no need to wait for a response<commit_after>/*
 * Licensed to Elastic Search and <NAME> under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Elastic Search licenses this
 * file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.cluster.metadata;

import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateUpdateTask;
import org.elasticsearch.cluster.action.index.NodeIndexDeletedAction;
import org.elasticsearch.cluster.block.ClusterBlocks;
import org.elasticsearch.cluster.routing.IndexRoutingTable;
import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
import org.elasticsearch.cluster.routing.RoutingTable;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.cluster.routing.allocation.RoutingAllocation;
import org.elasticsearch.cluster.routing.allocation.ShardsAllocation;
import org.elasticsearch.common.collect.Sets;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.timer.Timeout;
import org.elasticsearch.common.timer.TimerTask;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.Index;
import org.elasticsearch.indices.IndexMissingException;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.timer.TimerService;

import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import static org.elasticsearch.cluster.ClusterState.*;
import static org.elasticsearch.cluster.metadata.MetaData.*;

/**
 * @author kimchy (shay.banon)
 */
public class MetaDataDeleteIndexService extends AbstractComponent {

    private final TimerService timerService;

    private final ThreadPool threadPool;

    private final ClusterService clusterService;

    private final ShardsAllocation shardsAllocation;

    private final NodeIndexDeletedAction nodeIndexDeletedAction;

    @Inject public MetaDataDeleteIndexService(Settings settings, ThreadPool threadPool, TimerService timerService, ClusterService clusterService, ShardsAllocation shardsAllocation,
                                              NodeIndexDeletedAction nodeIndexDeletedAction) {
        super(settings);
        this.threadPool = threadPool;
        this.timerService = timerService;
        this.clusterService = clusterService;
        this.shardsAllocation = shardsAllocation;
        this.nodeIndexDeletedAction = nodeIndexDeletedAction;
    }

    public void deleteIndex(final Request request, final Listener userListener) {
        clusterService.submitStateUpdateTask("delete-index [" + request.index + "]", new ClusterStateUpdateTask() {
            @Override public ClusterState execute(ClusterState currentState) {
                final DeleteIndexListener listener = new DeleteIndexListener(request, userListener);
                try {
                    RoutingTable routingTable = currentState.routingTable();
                    if (!routingTable.hasIndex(request.index)) {
                        threadPool.cached().execute(new Runnable() {
                            @Override public void run() {
                                listener.onFailure(new IndexMissingException(new Index(request.index)));
                            }
                        });
                        return currentState;
                    }

                    logger.info("[{}] deleting index", request.index);

                    RoutingTable.Builder routingTableBuilder = new RoutingTable.Builder();
                    for (IndexRoutingTable indexRoutingTable : currentState.routingTable().indicesRouting().values()) {
                        if (!indexRoutingTable.index().equals(request.index)) {
                            routingTableBuilder.add(indexRoutingTable);
                        }
                    }
                    MetaData newMetaData = newMetaDataBuilder()
                            .metaData(currentState.metaData())
                            .remove(request.index)
                            .build();

                    RoutingAllocation.Result routingResult = shardsAllocation.reroute(
                            newClusterStateBuilder().state(currentState).routingTable(routingTableBuilder).metaData(newMetaData).build());

                    ClusterBlocks blocks = ClusterBlocks.builder().blocks(currentState.blocks()).removeIndexBlocks(request.index).build();

                    // initialize the counter only for nodes the shards are allocated to
                    Set<String> allocatedNodes = Sets.newHashSet();
                    for (IndexShardRoutingTable indexShardRoutingTable : currentState.routingTable().index(request.index)) {
                        for (ShardRouting shardRouting : indexShardRoutingTable) {
                            if (shardRouting.currentNodeId() != null) {
                                allocatedNodes.add(shardRouting.currentNodeId());
                            }
                            if (shardRouting.relocatingNodeId() != null) {
                                allocatedNodes.add(shardRouting.relocatingNodeId());
                            }
                        }
                    }

                    if (allocatedNodes.isEmpty()) {
                        // no nodes allocated, don't wait for a response
                        threadPool.cached().execute(new Runnable() {
                            @Override public void run() {
                                listener.onResponse(new Response(true));
                            }
                        });
                    } else {
                        final AtomicInteger counter = new AtomicInteger(allocatedNodes.size());

                        final NodeIndexDeletedAction.Listener nodeIndexDeleteListener = new NodeIndexDeletedAction.Listener() {
                            @Override public void onNodeIndexDeleted(String index, String nodeId) {
                                if (index.equals(request.index)) {
                                    if (counter.decrementAndGet() == 0) {
                                        listener.onResponse(new Response(true));
                                        nodeIndexDeletedAction.remove(this);
                                    }
                                }
                            }
                        };
                        nodeIndexDeletedAction.add(nodeIndexDeleteListener);

                        Timeout timeoutTask = timerService.newTimeout(new TimerTask() {
                            @Override public void run(Timeout timeout) throws Exception {
                                listener.onResponse(new Response(false));
                                nodeIndexDeletedAction.remove(nodeIndexDeleteListener);
                            }
                        }, request.timeout, TimerService.ExecutionType.THREADED);
                        listener.timeout = timeoutTask;
                    }


                    return newClusterStateBuilder().state(currentState).routingResult(routingResult).metaData(newMetaData).blocks(blocks).build();
                } catch (Exception e) {
                    listener.onFailure(e);
                    return currentState;
                }
            }
        });
    }

    class DeleteIndexListener implements Listener {

        private AtomicBoolean notified = new AtomicBoolean();

        private final Request request;

        private final Listener listener;

        volatile Timeout timeout;

        private DeleteIndexListener(Request request, Listener listener) {
            this.request = request;
            this.listener = listener;
        }

        @Override public void onResponse(final Response response) {
            if (notified.compareAndSet(false, true)) {
                if (timeout != null) {
{'fraction_non_alphanumeric': 0.07525053143030672, 'fraction_numerical': 0.0006073489219556636, 'mean_word_length': 2.1531980084258904, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3115694', 'n_tokens_mistral': 3639, 'n_tokens_neox': 3601, 'n_words': 847}