Fix remaining CS warnings in sal-distributed-datastore
[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  * <p>
26  * Implementation is geared toward positive votes. Negative votes have to synchronize and therefore are more likely
27  * to see contention.
28  *
29  * @author Robert Varga
30  *
31  * @param <T> Type of value returned on success
32  */
33 class VotingFuture<T> extends AbstractFuture<T> {
34     @SuppressWarnings("rawtypes")
35     private static final AtomicIntegerFieldUpdater<VotingFuture> VOTES_UPDATER =
36             AtomicIntegerFieldUpdater.newUpdater(VotingFuture.class, "neededVotes");
37
38     private final T result;
39
40     @GuardedBy("failures")
41     private final Collection<Throwable> failures = new ArrayList<>(0);
42     @SuppressWarnings("unused")
43     private volatile int neededVotes;
44
45     VotingFuture(final T result, final int requiredVotes) {
46         Preconditions.checkArgument(requiredVotes > 0);
47         this.neededVotes = requiredVotes;
48
49         // null is okay to allow Void type
50         this.result = result;
51     }
52
53     void voteYes() {
54         if (castVote()) {
55             synchronized (failures) {
56                 resolveResult();
57             }
58         }
59     }
60
61     void voteNo(final Throwable cause) {
62         synchronized (failures) {
63             failures.add(cause);
64             if (castVote()) {
65                 resolveResult();
66             }
67         }
68     }
69
70     private boolean castVote() {
71         final int votes = VOTES_UPDATER.decrementAndGet(this);
72         Verify.verify(votes >= 0);
73         return votes == 0;
74     }
75
76     @GuardedBy("failures")
77     private void resolveResult() {
78         final Iterator<Throwable> it = failures.iterator();
79         if (!it.hasNext()) {
80             set(result);
81             return;
82         }
83
84         final Throwable t = it.next();
85         while (it.hasNext()) {
86             t.addSuppressed(it.next());
87         }
88
89         setException(t);
90     }
91 }