All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor Maven / Gradle / Ivy

There is a newer version: 8.14.1
Show newest version
/*
 * Licensed to ElasticSearch and Shay Banon under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. ElasticSearch 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.common.util.concurrent;

import org.elasticsearch.common.Priority;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;

/**
 * A prioritizing executor which uses a priority queue as a work queue. The jobs that will be submitted will be treated
 * as {@link PrioritizedRunnable} and/or {@link PrioritizedCallable}, those tasks that are not instances of these two will
 * be wrapped and assign a default {@link Priority#NORMAL} priority.
 *
 * Note, if two tasks have the same priority, the first to arrive will be executed first (FIFO style).
 */
public class PrioritizedEsThreadPoolExecutor extends EsThreadPoolExecutor {

    private AtomicLong tieBreaker = new AtomicLong(Long.MIN_VALUE);

    public PrioritizedEsThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, ThreadFactory threadFactory) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, new PriorityBlockingQueue(), threadFactory);
    }

    public PrioritizedEsThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, new PriorityBlockingQueue(), threadFactory, handler);
    }

    public PrioritizedEsThreadPoolExecutor(int corePoolSize, int initialWorkQueuSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, new PriorityBlockingQueue(initialWorkQueuSize), threadFactory, handler);
    }

    @Override
    public void execute(Runnable command) {
        if (command instanceof PrioritizedRunnable) {
            super.execute(new TieBreakingPrioritizedRunnable((PrioritizedRunnable) command, tieBreaker.incrementAndGet()));
            return;
        }
        if (!(command instanceof Comparable)) {
            command = new TieBreakingPrioritizedRunnable(command, Priority.NORMAL, tieBreaker.incrementAndGet());
        }
        super.execute(command);
    }

    @Override
    protected  RunnableFuture newTaskFor(Runnable runnable, T value) {
        if (!(runnable instanceof PrioritizedRunnable)) {
            runnable = PrioritizedRunnable.wrap(runnable, Priority.NORMAL);
        }
        return new PrioritizedFutureTask((PrioritizedRunnable) runnable, value, tieBreaker.incrementAndGet());
    }

    @Override
    protected  RunnableFuture newTaskFor(Callable callable) {
        if (!(callable instanceof PrioritizedCallable)) {
            callable = PrioritizedCallable.wrap(callable, Priority.NORMAL);
        }
        return new PrioritizedFutureTask((PrioritizedCallable) callable, tieBreaker.incrementAndGet());
    }

    static class TieBreakingPrioritizedRunnable extends PrioritizedRunnable {

        private final Runnable runnable;
        private final long tieBreaker;

        TieBreakingPrioritizedRunnable(PrioritizedRunnable runnable, long tieBreaker) {
            this(runnable, runnable.priority(), tieBreaker);
        }

        TieBreakingPrioritizedRunnable(Runnable runnable, Priority priority, long tieBreaker) {
            super(priority);
            this.runnable = runnable;
            this.tieBreaker = tieBreaker;
        }

        @Override
        public void run() {
            runnable.run();
        }

        @Override
        public int compareTo(PrioritizedRunnable pr) {
            int res = super.compareTo(pr);
            if (res != 0 || !(pr instanceof TieBreakingPrioritizedRunnable)) {
                return res;
            }
            return tieBreaker < ((TieBreakingPrioritizedRunnable)pr).tieBreaker ? -1 : 1;
        }
    }

    /**
     *
     */
    static class PrioritizedFutureTask extends FutureTask implements Comparable {

        private final Priority priority;
        private final long tieBreaker;

        public PrioritizedFutureTask(PrioritizedRunnable runnable, T value, long tieBreaker) {
            super(runnable, value);
            this.priority = runnable.priority();
            this.tieBreaker = tieBreaker;
        }

        public PrioritizedFutureTask(PrioritizedCallable callable, long tieBreaker) {
            super(callable);
            this.priority = callable.priority();
            this.tieBreaker = tieBreaker;
        }

        @Override
        public int compareTo(PrioritizedFutureTask pft) {
            int res = priority.compareTo(pft.priority);
            if (res != 0) {
                return res;
            }
            return tieBreaker < pft.tieBreaker ? -1 : 1;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy