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

org.apache.cassandra.utils.concurrent.Future Maven / Gradle / Ivy

Go to download

The Apache Cassandra Project develops a highly scalable second-generation distributed database, bringing together Dynamo's fully distributed design and Bigtable's ColumnFamily-based data model.

There is a newer version: 5.0-rc1
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.utils.concurrent;

import java.util.concurrent.Executor;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

import com.google.common.util.concurrent.AsyncFunction;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.ListenableFuture; // checkstyle: permit this import

import io.netty.util.concurrent.GenericFutureListener;

import io.netty.util.internal.PlatformDependent;
import org.apache.cassandra.utils.Shared;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.apache.cassandra.utils.Shared.Recursive.INTERFACES;
import static org.apache.cassandra.utils.Shared.Scope.SIMULATION;

/**
 * A Future that integrates several different (but equivalent) APIs used within Cassandra into a single concept,
 * integrating also with our {@link Awaitable} abstraction, to overall improve coherency and clarity in the codebase.
 */
@Shared(scope = SIMULATION, ancestors = INTERFACES)
public interface Future extends io.netty.util.concurrent.Future, ListenableFuture, Awaitable
{
    /**
     * Wait indefinitely for this future to complete, throwing any interrupt
     * @throws InterruptedException if interrupted
     */
    @Override
    Future await() throws InterruptedException;

    /**
     * Wait indefinitely for this future to complete
     */
    @Override
    Future awaitUninterruptibly();

    /**
     * Wait indefinitely for this promise to complete, throwing any interrupt as an UncheckedInterruptedException
     * @throws UncheckedInterruptedException if interrupted
     */
    @Override
    Future awaitThrowUncheckedOnInterrupt();

    default void rethrowIfFailed()
    {
        Throwable cause = this.cause();
        if (cause != null)
        {
            PlatformDependent.throwException(cause);
        }
    }

    /**
     * waits for completion; in case of failure rethrows the original exception without a new wrapping exception
     * so may cause problems for reporting stack traces
     */
    @Override
    default Future sync() throws InterruptedException
    {
        await();
        rethrowIfFailed();
        return this;
    }

    /**
     * waits for completion; in case of failure rethrows the original exception without a new wrapping exception
     * so may cause problems for reporting stack traces
     */
    @Override
    default Future syncUninterruptibly()
    {
        awaitUninterruptibly();
        rethrowIfFailed();
        return this;
    }

    /**
     * waits for completion; in case of failure rethrows the original exception without a new wrapping exception
     * so may cause problems for reporting stack traces
     */
    default Future syncThrowUncheckedOnInterrupt()
    {
        awaitThrowUncheckedOnInterrupt();
        rethrowIfFailed();
        return this;
    }

    /** @deprecated See CASSANDRA-16924 */
    @Deprecated(since = "4.1")
    @Override
    default boolean await(long l) throws InterruptedException
    {
        return await(l, MILLISECONDS);
    }

    /** @deprecated See CASSANDRA-16924 */
    @Deprecated(since = "4.1")
    @Override
    default boolean awaitUninterruptibly(long l)
    {
        return awaitUninterruptibly(l, MILLISECONDS);
    }

    /**
     * Support {@link com.google.common.util.concurrent.Futures#addCallback} natively
     */
    Future addCallback(BiConsumer callback);

    /**
     * Support {@link com.google.common.util.concurrent.Futures#addCallback} natively
     */
    Future addCallback(BiConsumer callback, Executor executor);

    /**
     * Support {@link com.google.common.util.concurrent.Futures#addCallback} natively
     */
    Future addCallback(FutureCallback callback);

    /**
     * Support {@link com.google.common.util.concurrent.Futures#addCallback} natively
     */
    Future addCallback(FutureCallback callback, Executor executor);

    /**
     * Support {@link com.google.common.util.concurrent.Futures#addCallback} natively
     */
    Future addCallback(Consumer onSuccess, Consumer onFailure);

    /**
     * Support {@link com.google.common.util.concurrent.Futures#addCallback} natively
     */
    Future addCallback(Consumer onSuccess, Consumer onFailure, Executor executor);

    /**
     * Support {@link com.google.common.util.concurrent.Futures#transform(ListenableFuture, com.google.common.base.Function, Executor)} natively
     */
    default  Future map(Function mapper)
    {
        return map(mapper, null);
    }

    /**
     * Support {@link com.google.common.util.concurrent.Futures#transform(ListenableFuture, com.google.common.base.Function, Executor)} natively
     */
     Future map(Function mapper, Executor executor);

    /**
     * Support {@link com.google.common.util.concurrent.Futures#transformAsync(ListenableFuture, AsyncFunction, Executor)} natively
     */
    default  Future flatMap(Function> flatMapper)
    {
        return flatMap(flatMapper, null);
    }

    /**
     * Support {@link com.google.common.util.concurrent.Futures#transformAsync(ListenableFuture, AsyncFunction, Executor)} natively
     */
     Future flatMap(Function> flatMapper, Executor executor);

    /**
     * Invoke {@code runnable} on completion, using {@code executor}.
     *
     * Tasks are submitted to their executors in the order they were added to this Future.
     */
    @Override
    void addListener(Runnable runnable, Executor executor);

    /**
     * Invoke {@code runnable} on completion. Depending on the implementation and its configuration, this
     * may be executed immediately by the notifying/completing thread, or asynchronously by an executor.
     * Tasks are executed, or submitted to the executor, in the order they were added to this Future.
     */
    void addListener(Runnable runnable);

    Executor notifyExecutor();

    @Override Future addListener(GenericFutureListener> genericFutureListener);
    @Override Future addListeners(GenericFutureListener>... genericFutureListeners);
    @Override Future removeListener(GenericFutureListener> genericFutureListener);
    @Override Future removeListeners(GenericFutureListener>... genericFutureListeners);
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy