2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.databroker.actors.dds;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verify;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.util.concurrent.AbstractFuture;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Iterator;
18 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
19 import org.checkerframework.checker.lock.qual.GuardedBy;
20 import org.checkerframework.checker.lock.qual.Holding;
23 * An {@link AbstractFuture} implementation which requires a certain number of votes before it completes. If all votes
24 * are 'yes', then it completes with a pre-determined value. If any of the votes are 'no', the future completes with
25 * an exception. This exception corresponds to the cause reported by the first 'no' vote, with all subsequent votes
26 * added as suppressed exceptions.
29 * Implementation is geared toward positive votes. Negative votes have to synchronize and therefore are more likely
32 * @author Robert Varga
34 * @param <T> Type of value returned on success
36 class VotingFuture<T> extends AbstractFuture<T> {
37 @SuppressWarnings("rawtypes")
38 private static final AtomicIntegerFieldUpdater<VotingFuture> VOTES_UPDATER =
39 AtomicIntegerFieldUpdater.newUpdater(VotingFuture.class, "neededVotes");
41 private final T result;
43 @GuardedBy("failures")
44 private final Collection<Throwable> failures = new ArrayList<>(0);
45 @SuppressWarnings("unused")
46 private volatile int neededVotes;
48 VotingFuture(final T result, final int requiredVotes) {
49 this.result = requireNonNull(result);
50 checkArgument(requiredVotes > 0);
51 this.neededVotes = requiredVotes;
57 synchronized (failures) {
63 void voteNo(final Throwable cause) {
64 synchronized (failures) {
72 private boolean castVote() {
73 final int votes = VOTES_UPDATER.decrementAndGet(this);
79 private void resolveResult() {
80 final Iterator<Throwable> it = failures.iterator();
86 final Throwable t = it.next();
87 while (it.hasNext()) {
88 t.addSuppressed(it.next());