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

org.apache.cassandra.concurrent.AbstractLocalAwareExecutorService Maven / Gradle / Ivy

There is a newer version: 3.11.12.3
Show newest version
/*
 * 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 "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.apache.cassandra.concurrent;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.cassandra.utils.concurrent.SimpleCondition;
import org.apache.cassandra.utils.JVMStabilityInspector;

import static org.apache.cassandra.tracing.Tracing.isTracing;

public abstract class AbstractLocalAwareExecutorService implements LocalAwareExecutorService
{
    private static final Logger logger = LoggerFactory.getLogger(AbstractLocalAwareExecutorService.class);

    protected abstract void addTask(FutureTask futureTask);
    protected abstract void onCompletion();

    /** Task Submission / Creation / Objects **/

    public  FutureTask submit(Callable task)
    {
        return submit(newTaskFor(task));
    }

    public FutureTask submit(Runnable task)
    {
        return submit(newTaskFor(task, null));
    }

    public  FutureTask submit(Runnable task, T result)
    {
        return submit(newTaskFor(task, result));
    }

    public  List> invokeAll(Collection> tasks)
    {
        throw new UnsupportedOperationException();
    }

    public  List> invokeAll(Collection> tasks, long timeout, TimeUnit unit) throws InterruptedException
    {
        throw new UnsupportedOperationException();
    }

    public  T invokeAny(Collection> tasks) throws InterruptedException, ExecutionException
    {
        throw new UnsupportedOperationException();
    }

    public  T invokeAny(Collection> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
    {
        throw new UnsupportedOperationException();
    }

    protected  FutureTask newTaskFor(Runnable runnable, T result)
    {
        return newTaskFor(runnable, result, ExecutorLocals.create());
    }

    protected  FutureTask newTaskFor(Runnable runnable, T result, ExecutorLocals locals)
    {
        if (locals != null)
        {
            if (runnable instanceof LocalSessionFutureTask)
                return (LocalSessionFutureTask) runnable;
            return new LocalSessionFutureTask(runnable, result, locals);
        }
        if (runnable instanceof FutureTask)
            return (FutureTask) runnable;
        return new FutureTask<>(runnable, result);
    }

    protected  FutureTask newTaskFor(Callable callable)
    {
        if (isTracing())
        {
            if (callable instanceof LocalSessionFutureTask)
                return (LocalSessionFutureTask) callable;
            return new LocalSessionFutureTask(callable, ExecutorLocals.create());
        }
        if (callable instanceof FutureTask)
            return (FutureTask) callable;
        return new FutureTask<>(callable);
    }

    private class LocalSessionFutureTask extends FutureTask
    {
        private final ExecutorLocals locals;

        public LocalSessionFutureTask(Callable callable, ExecutorLocals locals)
        {
            super(callable);
            this.locals = locals;
        }

        public LocalSessionFutureTask(Runnable runnable, T result, ExecutorLocals locals)
        {
            super(runnable, result);
            this.locals = locals;
        }

        public void run()
        {
            ExecutorLocals old = ExecutorLocals.create();
            ExecutorLocals.set(locals);
            try
            {
                super.run();
            }
            finally
            {
                ExecutorLocals.set(old);
            }
        }
    }

    class FutureTask extends SimpleCondition implements Future, Runnable
    {
        private boolean failure;
        private Object result = this;
        private final Callable callable;

        public FutureTask(Callable callable)
        {
            this.callable = callable;
        }
        public FutureTask(Runnable runnable, T result)
        {
            this(Executors.callable(runnable, result));
        }

        public void run()
        {
            try
            {
                result = callable.call();
            }
            catch (Throwable t)
            {
                JVMStabilityInspector.inspectThrowable(t);
                logger.warn("Uncaught exception on thread {}: {}", Thread.currentThread(), t);
                result = t;
                failure = true;
            }
            finally
            {
                signalAll();
                onCompletion();
            }
        }

        public boolean cancel(boolean mayInterruptIfRunning)
        {
            return false;
        }

        public boolean isCancelled()
        {
            return false;
        }

        public boolean isDone()
        {
            return isSignaled();
        }

        public T get() throws InterruptedException, ExecutionException
        {
            await();
            Object result = this.result;
            if (failure)
                throw new ExecutionException((Throwable) result);
            return (T) result;
        }

        public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
        {
            if (!await(timeout, unit))
                throw new TimeoutException();
            Object result = this.result;
            if (failure)
                throw new ExecutionException((Throwable) result);
            return (T) result;
        }
    }

    private  FutureTask submit(FutureTask task)
    {
        addTask(task);
        return task;
    }

    public void execute(Runnable command)
    {
        addTask(newTaskFor(command, ExecutorLocals.create()));
    }

    public void execute(Runnable command, ExecutorLocals locals)
    {
        addTask(newTaskFor(command, null, locals));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy