Committed by
Gerrit Code Review
Code clean up in ConsistentMap and LeaderElector resources
Change-Id: I1834188393f19e37394c32047538e6027522a13d
Showing
2 changed files
with
26 additions
and
54 deletions
This diff is collapsed. Click to expand it.
... | @@ -29,6 +29,15 @@ import java.util.function.Consumer; | ... | @@ -29,6 +29,15 @@ import java.util.function.Consumer; |
29 | import org.onosproject.cluster.Leadership; | 29 | import org.onosproject.cluster.Leadership; |
30 | import org.onosproject.cluster.NodeId; | 30 | import org.onosproject.cluster.NodeId; |
31 | import org.onosproject.event.Change; | 31 | import org.onosproject.event.Change; |
32 | +import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorCommands.Anoint; | ||
33 | +import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorCommands.GetAllLeaderships; | ||
34 | +import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorCommands.GetElectedTopics; | ||
35 | +import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorCommands.GetLeadership; | ||
36 | +import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorCommands.Listen; | ||
37 | +import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorCommands.Promote; | ||
38 | +import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorCommands.Run; | ||
39 | +import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorCommands.Unlisten; | ||
40 | +import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorCommands.Withdraw; | ||
32 | import org.onosproject.store.service.AsyncLeaderElector; | 41 | import org.onosproject.store.service.AsyncLeaderElector; |
33 | 42 | ||
34 | import com.google.common.collect.Sets; | 43 | import com.google.common.collect.Sets; |
... | @@ -70,22 +79,22 @@ public class AtomixLeaderElector extends Resource<AtomixLeaderElector> | ... | @@ -70,22 +79,22 @@ public class AtomixLeaderElector extends Resource<AtomixLeaderElector> |
70 | 79 | ||
71 | @Override | 80 | @Override |
72 | public CompletableFuture<Leadership> run(String topic, NodeId nodeId) { | 81 | public CompletableFuture<Leadership> run(String topic, NodeId nodeId) { |
73 | - return submit(new AtomixLeaderElectorCommands.Run(topic, nodeId)); | 82 | + return submit(new Run(topic, nodeId)); |
74 | } | 83 | } |
75 | 84 | ||
76 | @Override | 85 | @Override |
77 | public CompletableFuture<Void> withdraw(String topic) { | 86 | public CompletableFuture<Void> withdraw(String topic) { |
78 | - return submit(new AtomixLeaderElectorCommands.Withdraw(topic)); | 87 | + return submit(new Withdraw(topic)); |
79 | } | 88 | } |
80 | 89 | ||
81 | @Override | 90 | @Override |
82 | public CompletableFuture<Boolean> anoint(String topic, NodeId nodeId) { | 91 | public CompletableFuture<Boolean> anoint(String topic, NodeId nodeId) { |
83 | - return submit(new AtomixLeaderElectorCommands.Anoint(topic, nodeId)); | 92 | + return submit(new Anoint(topic, nodeId)); |
84 | } | 93 | } |
85 | 94 | ||
86 | @Override | 95 | @Override |
87 | public CompletableFuture<Boolean> promote(String topic, NodeId nodeId) { | 96 | public CompletableFuture<Boolean> promote(String topic, NodeId nodeId) { |
88 | - return submit(new AtomixLeaderElectorCommands.Promote(topic, nodeId)); | 97 | + return submit(new Promote(topic, nodeId)); |
89 | } | 98 | } |
90 | 99 | ||
91 | @Override | 100 | @Override |
... | @@ -95,69 +104,32 @@ public class AtomixLeaderElector extends Resource<AtomixLeaderElector> | ... | @@ -95,69 +104,32 @@ public class AtomixLeaderElector extends Resource<AtomixLeaderElector> |
95 | 104 | ||
96 | @Override | 105 | @Override |
97 | public CompletableFuture<Leadership> getLeadership(String topic) { | 106 | public CompletableFuture<Leadership> getLeadership(String topic) { |
98 | - return submit(new AtomixLeaderElectorCommands.GetLeadership(topic)); | 107 | + return submit(new GetLeadership(topic)); |
99 | } | 108 | } |
100 | 109 | ||
101 | @Override | 110 | @Override |
102 | public CompletableFuture<Map<String, Leadership>> getLeaderships() { | 111 | public CompletableFuture<Map<String, Leadership>> getLeaderships() { |
103 | - return submit(new AtomixLeaderElectorCommands.GetAllLeaderships()); | 112 | + return submit(new GetAllLeaderships()); |
104 | } | 113 | } |
105 | 114 | ||
106 | public CompletableFuture<Set<String>> getElectedTopics(NodeId nodeId) { | 115 | public CompletableFuture<Set<String>> getElectedTopics(NodeId nodeId) { |
107 | - return submit(new AtomixLeaderElectorCommands.GetElectedTopics(nodeId)); | 116 | + return submit(new GetElectedTopics(nodeId)); |
108 | - } | ||
109 | - | ||
110 | - /** | ||
111 | - * Leadership change listener context. | ||
112 | - */ | ||
113 | - private final class LeadershipChangeListener implements Listener<Change<Leadership>> { | ||
114 | - private final Consumer<Change<Leadership>> listener; | ||
115 | - | ||
116 | - private LeadershipChangeListener(Consumer<Change<Leadership>> listener) { | ||
117 | - this.listener = listener; | ||
118 | - } | ||
119 | - | ||
120 | - @Override | ||
121 | - public void accept(Change<Leadership> change) { | ||
122 | - listener.accept(change); | ||
123 | - } | ||
124 | - | ||
125 | - @Override | ||
126 | - public void close() { | ||
127 | - synchronized (AtomixLeaderElector.this) { | ||
128 | - submit(new AtomixLeaderElectorCommands.Unlisten()); | ||
129 | - } | ||
130 | - } | ||
131 | } | 117 | } |
132 | 118 | ||
133 | @Override | 119 | @Override |
134 | - public CompletableFuture<Void> addChangeListener(Consumer<Change<Leadership>> consumer) { | 120 | + public synchronized CompletableFuture<Void> addChangeListener(Consumer<Change<Leadership>> consumer) { |
135 | - leadershipChangeListeners.add(consumer); | 121 | + if (leadershipChangeListeners.isEmpty()) { |
136 | - return setupListener(); | 122 | + return submit(new Listen()).thenRun(() -> leadershipChangeListeners.add(consumer)); |
137 | - } | 123 | + } else { |
138 | - | 124 | + leadershipChangeListeners.add(consumer); |
139 | - @Override | 125 | + return CompletableFuture.completedFuture(null); |
140 | - public CompletableFuture<Void> removeChangeListener(Consumer<Change<Leadership>> consumer) { | ||
141 | - leadershipChangeListeners.remove(consumer); | ||
142 | - return teardownListener(); | ||
143 | - } | ||
144 | - | ||
145 | - private CompletableFuture<Void> setupListener() { | ||
146 | - if (listener == null && !leadershipChangeListeners.isEmpty()) { | ||
147 | - Consumer<Change<Leadership>> changeConsumer = change -> { | ||
148 | - leadershipChangeListeners.forEach(consumer -> consumer.accept(change)); | ||
149 | - }; | ||
150 | - return submit(new AtomixLeaderElectorCommands.Listen()) | ||
151 | - .thenAccept(v -> listener = new LeadershipChangeListener(changeConsumer)); | ||
152 | } | 126 | } |
153 | - return CompletableFuture.completedFuture(null); | ||
154 | } | 127 | } |
155 | 128 | ||
156 | - private CompletableFuture<Void> teardownListener() { | 129 | + @Override |
157 | - if (listener != null && leadershipChangeListeners.isEmpty()) { | 130 | + public synchronized CompletableFuture<Void> removeChangeListener(Consumer<Change<Leadership>> consumer) { |
158 | - listener.close(); | 131 | + if (leadershipChangeListeners.remove(listener) && leadershipChangeListeners.isEmpty()) { |
159 | - listener = null; | 132 | + return submit(new Unlisten()).thenApply(v -> null); |
160 | - return submit(new AtomixLeaderElectorCommands.Unlisten()); | ||
161 | } | 133 | } |
162 | return CompletableFuture.completedFuture(null); | 134 | return CompletableFuture.completedFuture(null); |
163 | } | 135 | } | ... | ... |
-
Please register or login to post a comment