BUG-5280: Create AbstractProxyHistory class
[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 com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import com.google.common.util.concurrent.AbstractFuture;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.Iterator;
16 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
17 import javax.annotation.concurrent.GuardedBy;
18
19 /**
20  * An {@link AbstractFuture} implementation which requires a certain number of votes before it completes. If all votes
21  * are 'yes', then it completes with a pre-determined value. If any of the votes are 'no', the future completes with
22  * an exception. This exception corresponds to the cause reported by the first 'no' vote, with all subsequent votes
23  * added as suppressed exceptions.
24  *
25  * Implementation is geared toward positive votes. Negative votes have to synchronize and therefore are more likely
26  * to see contention.
27  *
28  * @author Robert Varga
29  *
30  * @param <T> Type of value returned on success
31  */
32 class VotingFuture<T> extends AbstractFuture<T> {
33     @SuppressWarnings("rawtypes")
34     private static final AtomicIntegerFieldUpdater<VotingFuture> VOTES_UPDATER =
35             AtomicIntegerFieldUpdater.newUpdater(VotingFuture.class, "neededVotes");
36
37     private final T result;
38
39     @GuardedBy("failures")
40     private final Collection<Throwable> failures = new ArrayList<>(0);
41     @SuppressWarnings("unused")
42     private volatile int neededVotes;
43
44     VotingFuture(final T result, final int requiredVotes) {
45         Preconditions.checkArgument(requiredVotes > 0);
46         this.neededVotes = requiredVotes;
47
48         // null is okay to allow Void type
49         this.result = result;
50     }
51
52     void voteYes() {
53         if (castVote()) {
54             synchronized (failures) {
55                 resolveResult();
56              }
57         }
58     }
59
60     void voteNo(final Throwable cause) {
61         synchronized (failures) {
62             failures.add(cause);
63             if (castVote()) {
64                 resolveResult();
65             }
66         }
67     }
68
69     private boolean castVote() {
70         final int votes = VOTES_UPDATER.decrementAndGet(this);
71         Verify.verify(votes >= 0);
72         return votes == 0;
73     }
74
75     @GuardedBy("failures")
76     private void resolveResult() {
77         final Iterator<Throwable> it = failures.iterator();
78         if (!it.hasNext()) {
79             set(result);
80             return;
81         }
82
83         final Throwable t = it.next();
84         while (it.hasNext()) {
85             t.addSuppressed(it.next());
86         }
87
88         setException(t);
89     }
90 }