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