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

com.yahoo.maha.parrequest2.future.CombinedFuture4 Maven / Gradle / Ivy

There is a newer version: 6.158
Show newest version
// Copyright 2017, Yahoo Holdings Inc.
// Licensed under the terms of the Apache License 2.0. Please see LICENSE file in project root for terms.
package com.yahoo.maha.parrequest2.future;

import com.google.common.util.concurrent.AbstractFuture;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.Uninterruptibles;
import com.yahoo.maha.parrequest2.EitherUtils;
import scala.util.Either;
import scala.util.Right;
import com.yahoo.maha.parrequest2.GeneralError;
import scala.Tuple4;

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

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

/**
 * This class takes four futures and on completion of both, sets a Tuple4 as a result of this future.
 */
class CombinedFuture4 extends AbstractFuture>> {

    private final ListenableFuture> firstFuture;
    private final ListenableFuture> secondFuture;
    private final ListenableFuture> thirdFuture;
    private final ListenableFuture> fourthFuture;
    private final AtomicInteger remaining = new AtomicInteger(4);
    private final AtomicBoolean shortCircuit = new AtomicBoolean(false);
    private volatile T firstValue = null;
    private volatile U secondValue = null;
    private volatile V thirdValue = null;
    private volatile W fourthValue = null;

    public CombinedFuture4(ParallelServiceExecutor executor,
                           final ListenableFuture> firstFuture,
                           final ListenableFuture> secondFuture,
                           final ListenableFuture> thirdFuture,
                           final ListenableFuture> fourthFuture
    ) {
        checkNotNull(executor, "executor is null");
        checkNotNull(firstFuture, "First future is null");
        checkNotNull(secondFuture, "Second future is null");
        checkNotNull(thirdFuture, "Third future is null");
        checkNotNull(fourthFuture, "Fourth future is null");
        this.firstFuture = firstFuture;
        this.secondFuture = secondFuture;
        this.thirdFuture = thirdFuture;
        this.fourthFuture = fourthFuture;

        // First, schedule cleanup to execute when the Future is done.
        executor.addListener(this, new Runnable() {
            @Override
            public void run() {
                // Cancel all the component futures.
                if (isCancelled()) {
                    if (firstFuture != null && !firstFuture.isDone()) {
                        firstFuture.cancel(wasInterrupted());
                    }
                    if (secondFuture != null && !secondFuture.isDone()) {
                        secondFuture.cancel(wasInterrupted());
                    }
                    if (thirdFuture != null && !thirdFuture.isDone()) {
                        thirdFuture.cancel(wasInterrupted());
                    }
                    if (fourthFuture != null && !fourthFuture.isDone()) {
                        fourthFuture.cancel(wasInterrupted());
                    }
                }
            }
        });

        executor.addListener(firstFuture, new Runnable() {
            @Override
            public void run() {
                setFirstValue();
            }
        });
        executor.addListener(secondFuture, new Runnable() {
            @Override
            public void run() {
                setSecondValue();
            }
        });
        executor.addListener(thirdFuture, new Runnable() {
            @Override
            public void run() {
                setThirdValue();
            }
        });
        executor.addListener(fourthFuture, new Runnable() {
            @Override
            public void run() {
                setFourthValue();
            }
        });
    }

    private void setFirstValue() {
        try {
            checkState(firstFuture.isDone(),
                    "Tried to set value from future which is not done");
            Either returnValue = Uninterruptibles.getUninterruptibly(firstFuture);
            if (returnValue.isLeft()) {
                if (shortCircuit.compareAndSet(false, true)) {
                    set(EitherUtils.>castLeft(returnValue));
                }
            } else {
                firstValue = returnValue.right().get();
            }
        } catch (CancellationException e) {
            cancel(false);
        } catch (Throwable t) {
            super.setException(t);
        } finally {
            checkAndSet();
        }
    }

    private void setSecondValue() {
        try {
            checkState(secondFuture.isDone(),
                    "Tried to set value from future which is not done");
            Either returnValue = Uninterruptibles.getUninterruptibly(secondFuture);
            if (returnValue.isLeft()) {
                if (shortCircuit.compareAndSet(false, true)) {
                    set(EitherUtils.>castLeft(returnValue));
                }
            } else {
                secondValue = returnValue.right().get();
            }
        } catch (CancellationException e) {
            cancel(false);
        } catch (Throwable t) {
            super.setException(t);
        } finally {
            checkAndSet();
        }
    }

    private void setThirdValue() {
        try {
            checkState(thirdFuture.isDone(),
                    "Tried to set value from future which is not done");
            Either returnValue = Uninterruptibles.getUninterruptibly(thirdFuture);
            if (returnValue.isLeft()) {
                if (shortCircuit.compareAndSet(false, true)) {
                    set(EitherUtils.>castLeft(returnValue));
                }
            } else {
                thirdValue = returnValue.right().get();
            }
        } catch (CancellationException e) {
            cancel(false);
        } catch (Throwable t) {
            super.setException(t);
        } finally {
            checkAndSet();
        }
    }

    private void setFourthValue() {
        try {
            checkState(fourthFuture.isDone(),
                    "Tried to set value from future which is not done");
            Either returnValue = Uninterruptibles.getUninterruptibly(fourthFuture);
            if (returnValue.isLeft()) {
                if (shortCircuit.compareAndSet(false, true)) {
                    set(EitherUtils.>castLeft(returnValue));
                }
            } else {
                fourthValue = returnValue.right().get();
            }
        } catch (CancellationException e) {
            cancel(false);
        } catch (Throwable t) {
            super.setException(t);
        } finally {
            checkAndSet();
        }
    }

    private void checkAndSet() {
        int newRemaining = remaining.decrementAndGet();
        checkState(newRemaining >= 0, "Less than 0 remaining futures");
        if (newRemaining == 0) {
            if (!shortCircuit.get() && !isDone()) {
                checkNotNull(firstValue, "First value is null on completion");
                checkNotNull(secondValue, "Second value is null on completion");
                checkNotNull(thirdValue, "Third value is null on completion");
                checkNotNull(fourthFuture, "Fourth value is null on completion");
                set(new Right>(new Tuple4<>(firstValue, secondValue, thirdValue, fourthValue)));
            }
        }
    }

    public static  CombinedFuture4 from(ParallelServiceExecutor executor,
                                                                ListenableFuture> firstFuture,
                                                                ListenableFuture> secondFuture,
                                                                ListenableFuture> thirdFuture,
                                                                ListenableFuture> fourthFuture
    ) {
        return new CombinedFuture4<>(executor, firstFuture, secondFuture, thirdFuture, fourthFuture);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy