* Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF 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
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
package org.apache.rya.periodic.notification.pruner;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.fluo.api.client.FluoClient;
import org.apache.rya.indexing.pcj.storage.PeriodicQueryResultStorage;
import org.apache.rya.periodic.notification.api.LifeCycle;
import org.apache.rya.periodic.notification.api.NodeBin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Preconditions;

 * Executor service that runs {@link PeriodicQueryPruner}s with added functionality
 * for starting, stopping, and determining if the query pruners are running.
public class PeriodicQueryPrunerExecutor implements LifeCycle {

    private static final Logger log = LoggerFactory.getLogger(PeriodicQueryPrunerExecutor.class);
    private final FluoClient client;
    private final int numThreads;
    private final ExecutorService executor;
    private final BlockingQueue<NodeBin> bins;
    private final PeriodicQueryResultStorage periodicStorage;
    private final List<PeriodicQueryPruner> pruners;
    private boolean running = false;

    public PeriodicQueryPrunerExecutor(final PeriodicQueryResultStorage periodicStorage, final FluoClient client, final int numThreads,
            final BlockingQueue<NodeBin> bins) {
        Preconditions.checkArgument(numThreads > 0);
        this.periodicStorage = periodicStorage;
        this.numThreads = numThreads;
        executor = Executors.newFixedThreadPool(numThreads);
        this.bins = bins;
        this.client = client;
        this.pruners = new ArrayList<>();

    public void start() {
        if (!running) {
            final AccumuloBinPruner accPruner = new AccumuloBinPruner(periodicStorage);
            final FluoBinPruner fluoPruner = new FluoBinPruner(client);

            for (int threadNumber = 0; threadNumber < numThreads; threadNumber++) {
                final PeriodicQueryPruner pruner = new PeriodicQueryPruner(fluoPruner, accPruner, client, bins, threadNumber);
            running = true;

    public void stop() {
        if (pruners != null && pruners.size() > 0) {
            pruners.forEach(x -> x.shutdown());
        if(client != null) {
        if (executor != null) {
            running = false;
        try {
            if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
                log.info("Timed out waiting for consumer threads to shut down, exiting uncleanly");
        } catch (final InterruptedException e) {
            log.info("Interrupted during shutdown, exiting uncleanly");

    public boolean currentlyRunning() {
        return running;