Use Empty instead of Void in cohorts
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / VotingFuture.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.databroker.actors.dds;
9
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;
13
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;
21
22 /**
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.
27  *
28  * <p>
29  * Implementation is geared toward positive votes. Negative votes have to synchronize and therefore are more likely
30  * to see contention.
31  *
32  * @author Robert Varga
33  *
34  * @param <T> Type of value returned on success
35  */
36 class VotingFuture<T> extends AbstractFuture<T> {
37     @SuppressWarnings("rawtypes")
38     private static final AtomicIntegerFieldUpdater<VotingFuture> VOTES_UPDATER =
39             AtomicIntegerFieldUpdater.newUpdater(VotingFuture.class, "neededVotes");
40
41     private final T result;
42
43     @GuardedBy("failures")
44     private final Collection<Throwable> failures = new ArrayList<>(0);
45     @SuppressWarnings("unused")
46     private volatile int neededVotes;
47
48     VotingFuture(final T result, final int requiredVotes) {
49         this.result = requireNonNull(result);
50         checkArgument(requiredVotes > 0);
51         this.neededVotes = requiredVotes;
52
53     }
54
55     void voteYes() {
56         if (castVote()) {
57             synchronized (failures) {
58                 resolveResult();
59             }
60         }
61     }
62
63     void voteNo(final Throwable cause) {
64         synchronized (failures) {
65             failures.add(cause);
66             if (castVote()) {
67                 resolveResult();
68             }
69         }
70     }
71
72     private boolean castVote() {
73         final int votes = VOTES_UPDATER.decrementAndGet(this);
74         verify(votes >= 0);
75         return votes == 0;
76     }
77
78     @Holding("failures")
79     private void resolveResult() {
80         final Iterator<Throwable> it = failures.iterator();
81         if (!it.hasNext()) {
82             set(result);
83             return;
84         }
85
86         final Throwable t = it.next();
87         while (it.hasNext()) {
88             t.addSuppressed(it.next());
89         }
90
91         setException(t);
92     }
93 }