Madan Jampani
Committed by Gerrit Code Review

Updates to ConsistentMap and LeaderElector state machines

Change-Id: I7734b253a56fef7300a8a094a3cfc8c1b45c2453
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
15 */ 15 */
16 package org.onosproject.store.primitives.impl; 16 package org.onosproject.store.primitives.impl;
17 17
18 +import static org.slf4j.LoggerFactory.getLogger;
18 import io.atomix.catalyst.serializer.Serializer; 19 import io.atomix.catalyst.serializer.Serializer;
19 import io.atomix.catalyst.transport.Address; 20 import io.atomix.catalyst.transport.Address;
20 import io.atomix.catalyst.transport.Transport; 21 import io.atomix.catalyst.transport.Transport;
...@@ -35,6 +36,7 @@ import java.util.function.Supplier; ...@@ -35,6 +36,7 @@ import java.util.function.Supplier;
35 36
36 import org.onosproject.cluster.NodeId; 37 import org.onosproject.cluster.NodeId;
37 import org.onosproject.store.service.PartitionInfo; 38 import org.onosproject.store.service.PartitionInfo;
39 +import org.slf4j.Logger;
38 40
39 import com.google.common.collect.ImmutableSet; 41 import com.google.common.collect.ImmutableSet;
40 import com.google.common.collect.Sets; 42 import com.google.common.collect.Sets;
...@@ -44,6 +46,8 @@ import com.google.common.collect.Sets; ...@@ -44,6 +46,8 @@ import com.google.common.collect.Sets;
44 */ 46 */
45 public class StoragePartitionServer implements Managed<StoragePartitionServer> { 47 public class StoragePartitionServer implements Managed<StoragePartitionServer> {
46 48
49 + private final Logger log = getLogger(getClass());
50 +
47 private static final int MAX_ENTRIES_PER_LOG_SEGMENT = 32768; 51 private static final int MAX_ENTRIES_PER_LOG_SEGMENT = 32768;
48 private final StoragePartition partition; 52 private final StoragePartition partition;
49 private final Address localAddress; 53 private final Address localAddress;
...@@ -81,7 +85,13 @@ public class StoragePartitionServer implements Managed<StoragePartitionServer> { ...@@ -81,7 +85,13 @@ public class StoragePartitionServer implements Managed<StoragePartitionServer> {
81 } else { 85 } else {
82 serverOpenFuture = CompletableFuture.completedFuture(null); 86 serverOpenFuture = CompletableFuture.completedFuture(null);
83 } 87 }
84 - return serverOpenFuture.thenApply(v -> null); 88 + return serverOpenFuture.whenComplete((r, e) -> {
89 + if (e == null) {
90 + log.info("Successfully started server for partition {}", partition.getId());
91 + } else {
92 + log.info("Failed to start server for partition {}", partition.getId(), e);
93 + }
94 + }).thenApply(v -> null);
85 } 95 }
86 96
87 @Override 97 @Override
...@@ -105,7 +115,6 @@ public class StoragePartitionServer implements Managed<StoragePartitionServer> { ...@@ -105,7 +115,6 @@ public class StoragePartitionServer implements Managed<StoragePartitionServer> {
105 .withStorage(Storage.builder() 115 .withStorage(Storage.builder()
106 // FIXME: StorageLevel should be DISK 116 // FIXME: StorageLevel should be DISK
107 .withStorageLevel(StorageLevel.MEMORY) 117 .withStorageLevel(StorageLevel.MEMORY)
108 - .withSerializer(serializer.clone())
109 .withDirectory(dataFolder) 118 .withDirectory(dataFolder)
110 .withMaxEntriesPerSegment(MAX_ENTRIES_PER_LOG_SEGMENT) 119 .withMaxEntriesPerSegment(MAX_ENTRIES_PER_LOG_SEGMENT)
111 .build()) 120 .build())
......
...@@ -17,12 +17,12 @@ package org.onosproject.store.primitives.resources.impl; ...@@ -17,12 +17,12 @@ package org.onosproject.store.primitives.resources.impl;
17 17
18 import io.atomix.catalyst.util.Listener; 18 import io.atomix.catalyst.util.Listener;
19 import io.atomix.copycat.client.CopycatClient; 19 import io.atomix.copycat.client.CopycatClient;
20 -import io.atomix.resource.Consistency;
21 import io.atomix.resource.Resource; 20 import io.atomix.resource.Resource;
22 import io.atomix.resource.ResourceTypeInfo; 21 import io.atomix.resource.ResourceTypeInfo;
23 22
24 import java.util.Collection; 23 import java.util.Collection;
25 import java.util.ConcurrentModificationException; 24 import java.util.ConcurrentModificationException;
25 +import java.util.List;
26 import java.util.Map.Entry; 26 import java.util.Map.Entry;
27 import java.util.Set; 27 import java.util.Set;
28 import java.util.concurrent.CompletableFuture; 28 import java.util.concurrent.CompletableFuture;
...@@ -49,7 +49,7 @@ public class AtomixConsistentMap extends Resource<AtomixConsistentMap, Resource. ...@@ -49,7 +49,7 @@ public class AtomixConsistentMap extends Resource<AtomixConsistentMap, Resource.
49 49
50 private final Set<MapEventListener<String, byte[]>> mapEventListeners = Sets.newCopyOnWriteArraySet(); 50 private final Set<MapEventListener<String, byte[]>> mapEventListeners = Sets.newCopyOnWriteArraySet();
51 51
52 - private static final String CHANGE_SUBJECT = "change"; 52 + public static final String CHANGE_SUBJECT = "changeEvents";
53 53
54 public AtomixConsistentMap(CopycatClient client, Resource.Options options) { 54 public AtomixConsistentMap(CopycatClient client, Resource.Options options) {
55 super(client, options); 55 super(client, options);
...@@ -68,14 +68,8 @@ public class AtomixConsistentMap extends Resource<AtomixConsistentMap, Resource. ...@@ -68,14 +68,8 @@ public class AtomixConsistentMap extends Resource<AtomixConsistentMap, Resource.
68 }); 68 });
69 } 69 }
70 70
71 - private void handleEvent(MapEvent<String, byte[]> event) { 71 + private void handleEvent(List<MapEvent<String, byte[]>> events) {
72 - mapEventListeners.forEach(listener -> listener.event(event)); 72 + events.forEach(event -> mapEventListeners.forEach(listener -> listener.event(event)));
73 - }
74 -
75 - @Override
76 - public AtomixConsistentMap with(Consistency consistency) {
77 - super.with(consistency);
78 - return this;
79 } 73 }
80 74
81 @Override 75 @Override
......
...@@ -18,7 +18,9 @@ package org.onosproject.store.primitives.resources.impl; ...@@ -18,7 +18,9 @@ package org.onosproject.store.primitives.resources.impl;
18 import io.atomix.catalyst.buffer.BufferInput; 18 import io.atomix.catalyst.buffer.BufferInput;
19 import io.atomix.catalyst.buffer.BufferOutput; 19 import io.atomix.catalyst.buffer.BufferOutput;
20 import io.atomix.catalyst.serializer.CatalystSerializable; 20 import io.atomix.catalyst.serializer.CatalystSerializable;
21 +import io.atomix.catalyst.serializer.SerializableTypeResolver;
21 import io.atomix.catalyst.serializer.Serializer; 22 import io.atomix.catalyst.serializer.Serializer;
23 +import io.atomix.catalyst.serializer.SerializerRegistry;
22 import io.atomix.catalyst.util.Assert; 24 import io.atomix.catalyst.util.Assert;
23 import io.atomix.copycat.client.Command; 25 import io.atomix.copycat.client.Command;
24 import io.atomix.copycat.client.Query; 26 import io.atomix.copycat.client.Query;
...@@ -514,4 +516,28 @@ public final class AtomixConsistentMapCommands { ...@@ -514,4 +516,28 @@ public final class AtomixConsistentMapCommands {
514 .toString(); 516 .toString();
515 } 517 }
516 } 518 }
519 +
520 + /**
521 + * Map command type resolver.
522 + */
523 + public static class TypeResolver implements SerializableTypeResolver {
524 + @Override
525 + public void resolve(SerializerRegistry registry) {
526 + registry.register(ContainsKey.class, -761);
527 + registry.register(ContainsValue.class, -762);
528 + registry.register(Get.class, -763);
529 + registry.register(EntrySet.class, -764);
530 + registry.register(Values.class, -765);
531 + registry.register(KeySet.class, -766);
532 + registry.register(Clear.class, -767);
533 + registry.register(IsEmpty.class, -768);
534 + registry.register(Size.class, -769);
535 + registry.register(Listen.class, -770);
536 + registry.register(Unlisten.class, -771);
537 + registry.register(TransactionPrepare.class, -772);
538 + registry.register(TransactionCommit.class, -773);
539 + registry.register(TransactionRollback.class, -774);
540 + registry.register(UpdateAndGet.class, -775);
541 + }
542 + }
517 } 543 }
......
...@@ -30,6 +30,7 @@ import io.atomix.resource.ResourceStateMachine; ...@@ -30,6 +30,7 @@ import io.atomix.resource.ResourceStateMachine;
30 import java.util.Collection; 30 import java.util.Collection;
31 import java.util.HashMap; 31 import java.util.HashMap;
32 import java.util.Iterator; 32 import java.util.Iterator;
33 +import java.util.List;
33 import java.util.Map; 34 import java.util.Map;
34 import java.util.Set; 35 import java.util.Set;
35 import java.util.concurrent.atomic.AtomicLong; 36 import java.util.concurrent.atomic.AtomicLong;
...@@ -39,11 +40,26 @@ import org.onlab.util.CountDownCompleter; ...@@ -39,11 +40,26 @@ import org.onlab.util.CountDownCompleter;
39 import org.onlab.util.Match; 40 import org.onlab.util.Match;
40 import org.onosproject.store.primitives.MapUpdate; 41 import org.onosproject.store.primitives.MapUpdate;
41 import org.onosproject.store.primitives.TransactionId; 42 import org.onosproject.store.primitives.TransactionId;
43 +import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.Clear;
44 +import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.ContainsKey;
45 +import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.ContainsValue;
46 +import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.EntrySet;
47 +import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.Get;
48 +import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.IsEmpty;
49 +import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.KeySet;
50 +import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.Listen;
51 +import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.Size;
52 +import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.TransactionCommit;
42 import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.TransactionPrepare; 53 import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.TransactionPrepare;
54 +import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.TransactionRollback;
55 +import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.Unlisten;
56 +import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.UpdateAndGet;
57 +import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapCommands.Values;
43 import org.onosproject.store.service.MapEvent; 58 import org.onosproject.store.service.MapEvent;
44 import org.onosproject.store.service.MapTransaction; 59 import org.onosproject.store.service.MapTransaction;
45 import org.onosproject.store.service.Versioned; 60 import org.onosproject.store.service.Versioned;
46 61
62 +import com.google.common.collect.Lists;
47 import com.google.common.collect.Maps; 63 import com.google.common.collect.Maps;
48 import com.google.common.collect.Sets; 64 import com.google.common.collect.Sets;
49 65
...@@ -52,13 +68,11 @@ import static com.google.common.base.Preconditions.checkState; ...@@ -52,13 +68,11 @@ import static com.google.common.base.Preconditions.checkState;
52 /** 68 /**
53 * State Machine for {@link AtomixConsistentMap} resource. 69 * State Machine for {@link AtomixConsistentMap} resource.
54 */ 70 */
55 -public class AtomixConsistentMapState extends ResourceStateMachine implements 71 +public class AtomixConsistentMapState extends ResourceStateMachine implements SessionListener, Snapshottable {
56 - SessionListener, Snapshottable {
57 private final Map<Long, Commit<? extends AtomixConsistentMapCommands.Listen>> listeners = new HashMap<>(); 72 private final Map<Long, Commit<? extends AtomixConsistentMapCommands.Listen>> listeners = new HashMap<>();
58 private final Map<String, MapEntryValue> mapEntries = new HashMap<>(); 73 private final Map<String, MapEntryValue> mapEntries = new HashMap<>();
59 private final Set<String> preparedKeys = Sets.newHashSet(); 74 private final Set<String> preparedKeys = Sets.newHashSet();
60 - private final Map<TransactionId, Commit<? extends TransactionPrepare>> pendingTransactions = Maps 75 + private final Map<TransactionId, Commit<? extends TransactionPrepare>> pendingTransactions = Maps.newHashMap();
61 - .newHashMap();
62 private AtomicLong versionCounter = new AtomicLong(0); 76 private AtomicLong versionCounter = new AtomicLong(0);
63 77
64 @Override 78 @Override
...@@ -74,36 +88,23 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -74,36 +88,23 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
74 @Override 88 @Override
75 protected void configure(StateMachineExecutor executor) { 89 protected void configure(StateMachineExecutor executor) {
76 // Listeners 90 // Listeners
77 - executor.register(AtomixConsistentMapCommands.Listen.class, 91 + executor.register(Listen.class, this::listen);
78 - this::listen); 92 + executor.register(Unlisten.class, this::unlisten);
79 - executor.register(AtomixConsistentMapCommands.Unlisten.class,
80 - this::unlisten);
81 // Queries 93 // Queries
82 - executor.register(AtomixConsistentMapCommands.ContainsKey.class, 94 + executor.register(ContainsKey.class, this::containsKey);
83 - this::containsKey); 95 + executor.register(ContainsValue.class, this::containsValue);
84 - executor.register(AtomixConsistentMapCommands.ContainsValue.class, 96 + executor.register(EntrySet.class, this::entrySet);
85 - this::containsValue); 97 + executor.register(Get.class, this::get);
86 - executor.register(AtomixConsistentMapCommands.EntrySet.class, 98 + executor.register(IsEmpty.class, this::isEmpty);
87 - this::entrySet); 99 + executor.register(KeySet.class, this::keySet);
88 - executor.register(AtomixConsistentMapCommands.Get.class, this::get); 100 + executor.register(Size.class, this::size);
89 - executor.register(AtomixConsistentMapCommands.IsEmpty.class, 101 + executor.register(Values.class, this::values);
90 - this::isEmpty);
91 - executor.register(AtomixConsistentMapCommands.KeySet.class,
92 - this::keySet);
93 - executor.register(AtomixConsistentMapCommands.Size.class, this::size);
94 - executor.register(AtomixConsistentMapCommands.Values.class,
95 - this::values);
96 // Commands 102 // Commands
97 - executor.register(AtomixConsistentMapCommands.UpdateAndGet.class, 103 + executor.register(UpdateAndGet.class, this::updateAndGet);
98 - this::updateAndGet);
99 executor.register(AtomixConsistentMapCommands.Clear.class, this::clear); 104 executor.register(AtomixConsistentMapCommands.Clear.class, this::clear);
100 - executor.register(AtomixConsistentMapCommands.TransactionPrepare.class, 105 + executor.register(TransactionPrepare.class, this::prepare);
101 - this::prepare); 106 + executor.register(TransactionCommit.class, this::commit);
102 - executor.register(AtomixConsistentMapCommands.TransactionCommit.class, 107 + executor.register(TransactionRollback.class, this::rollback);
103 - this::commit);
104 - executor.register(
105 - AtomixConsistentMapCommands.TransactionRollback.class,
106 - this::rollback);
107 } 108 }
108 109
109 @Override 110 @Override
...@@ -120,12 +121,10 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -120,12 +121,10 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
120 /** 121 /**
121 * Handles a contains key commit. 122 * Handles a contains key commit.
122 * 123 *
123 - * @param commit 124 + * @param commit containsKey commit
124 - * containsKey commit
125 * @return {@code true} if map contains key 125 * @return {@code true} if map contains key
126 */ 126 */
127 - protected boolean containsKey( 127 + protected boolean containsKey(Commit<? extends ContainsKey> commit) {
128 - Commit<? extends AtomixConsistentMapCommands.ContainsKey> commit) {
129 try { 128 try {
130 return toVersioned(mapEntries.get(commit.operation().key())) != null; 129 return toVersioned(mapEntries.get(commit.operation().key())) != null;
131 } finally { 130 } finally {
...@@ -136,12 +135,10 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -136,12 +135,10 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
136 /** 135 /**
137 * Handles a contains value commit. 136 * Handles a contains value commit.
138 * 137 *
139 - * @param commit 138 + * @param commit containsValue commit
140 - * containsValue commit
141 * @return {@code true} if map contains value 139 * @return {@code true} if map contains value
142 */ 140 */
143 - protected boolean containsValue( 141 + protected boolean containsValue(Commit<? extends ContainsValue> commit) {
144 - Commit<? extends AtomixConsistentMapCommands.ContainsValue> commit) {
145 try { 142 try {
146 Match<byte[]> valueMatch = Match 143 Match<byte[]> valueMatch = Match
147 .ifValue(commit.operation().value()); 144 .ifValue(commit.operation().value());
...@@ -159,8 +156,7 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -159,8 +156,7 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
159 * get commit 156 * get commit
160 * @return value mapped to key 157 * @return value mapped to key
161 */ 158 */
162 - protected Versioned<byte[]> get( 159 + protected Versioned<byte[]> get(Commit<? extends Get> commit) {
163 - Commit<? extends AtomixConsistentMapCommands.Get> commit) {
164 try { 160 try {
165 return toVersioned(mapEntries.get(commit.operation().key())); 161 return toVersioned(mapEntries.get(commit.operation().key()));
166 } finally { 162 } finally {
...@@ -171,11 +167,10 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -171,11 +167,10 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
171 /** 167 /**
172 * Handles a count commit. 168 * Handles a count commit.
173 * 169 *
174 - * @param commit 170 + * @param commit size commit
175 - * size commit
176 * @return number of entries in map 171 * @return number of entries in map
177 */ 172 */
178 - protected int size(Commit<? extends AtomixConsistentMapCommands.Size> commit) { 173 + protected int size(Commit<? extends Size> commit) {
179 try { 174 try {
180 return mapEntries.size(); 175 return mapEntries.size();
181 } finally { 176 } finally {
...@@ -186,12 +181,10 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -186,12 +181,10 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
186 /** 181 /**
187 * Handles an is empty commit. 182 * Handles an is empty commit.
188 * 183 *
189 - * @param commit 184 + * @param commit isEmpty commit
190 - * isEmpty commit
191 * @return {@code true} if map is empty 185 * @return {@code true} if map is empty
192 */ 186 */
193 - protected boolean isEmpty( 187 + protected boolean isEmpty(Commit<? extends IsEmpty> commit) {
194 - Commit<? extends AtomixConsistentMapCommands.IsEmpty> commit) {
195 try { 188 try {
196 return mapEntries.isEmpty(); 189 return mapEntries.isEmpty();
197 } finally { 190 } finally {
...@@ -202,14 +195,12 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -202,14 +195,12 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
202 /** 195 /**
203 * Handles a keySet commit. 196 * Handles a keySet commit.
204 * 197 *
205 - * @param commit 198 + * @param commit keySet commit
206 - * keySet commit
207 * @return set of keys in map 199 * @return set of keys in map
208 */ 200 */
209 - protected Set<String> keySet( 201 + protected Set<String> keySet(Commit<? extends KeySet> commit) {
210 - Commit<? extends AtomixConsistentMapCommands.KeySet> commit) {
211 try { 202 try {
212 - return mapEntries.keySet(); 203 + return mapEntries.keySet().stream().collect(Collectors.toSet());
213 } finally { 204 } finally {
214 commit.close(); 205 commit.close();
215 } 206 }
...@@ -218,15 +209,12 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -218,15 +209,12 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
218 /** 209 /**
219 * Handles a values commit. 210 * Handles a values commit.
220 * 211 *
221 - * @param commit 212 + * @param commit values commit
222 - * values commit
223 * @return collection of values in map 213 * @return collection of values in map
224 */ 214 */
225 - protected Collection<Versioned<byte[]>> values( 215 + protected Collection<Versioned<byte[]>> values(Commit<? extends Values> commit) {
226 - Commit<? extends AtomixConsistentMapCommands.Values> commit) {
227 try { 216 try {
228 - return mapEntries.values().stream().map(this::toVersioned) 217 + return mapEntries.values().stream().map(this::toVersioned).collect(Collectors.toList());
229 - .collect(Collectors.toList());
230 } finally { 218 } finally {
231 commit.close(); 219 commit.close();
232 } 220 }
...@@ -239,8 +227,7 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -239,8 +227,7 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
239 * entrySet commit 227 * entrySet commit
240 * @return set of map entries 228 * @return set of map entries
241 */ 229 */
242 - protected Set<Map.Entry<String, Versioned<byte[]>>> entrySet( 230 + protected Set<Map.Entry<String, Versioned<byte[]>>> entrySet(Commit<? extends EntrySet> commit) {
243 - Commit<? extends AtomixConsistentMapCommands.EntrySet> commit) {
244 try { 231 try {
245 return mapEntries 232 return mapEntries
246 .entrySet() 233 .entrySet()
...@@ -256,12 +243,10 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -256,12 +243,10 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
256 /** 243 /**
257 * Handles a update and get commit. 244 * Handles a update and get commit.
258 * 245 *
259 - * @param commit 246 + * @param commit updateAndGet commit
260 - * updateAndGet commit
261 * @return update result 247 * @return update result
262 */ 248 */
263 - protected MapEntryUpdateResult<String, byte[]> updateAndGet( 249 + protected MapEntryUpdateResult<String, byte[]> updateAndGet(Commit<? extends UpdateAndGet> commit) {
264 - Commit<? extends AtomixConsistentMapCommands.UpdateAndGet> commit) {
265 MapEntryUpdateResult.Status updateStatus = validate(commit.operation()); 250 MapEntryUpdateResult.Status updateStatus = validate(commit.operation());
266 String key = commit.operation().key(); 251 String key = commit.operation().key();
267 MapEntryValue oldCommitValue = mapEntries.get(commit.operation().key()); 252 MapEntryValue oldCommitValue = mapEntries.get(commit.operation().key());
...@@ -286,8 +271,10 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -286,8 +271,10 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
286 } 271 }
287 if (updateType == INSERT || updateType == UPDATE) { 272 if (updateType == INSERT || updateType == UPDATE) {
288 mapEntries.put(key, new NonTransactionalCommit(newVersion, commit)); 273 mapEntries.put(key, new NonTransactionalCommit(newVersion, commit));
274 + } else {
275 + commit.close();
289 } 276 }
290 - notify(new MapEvent<>("", key, newMapValue, oldMapValue)); 277 + publish(Lists.newArrayList(new MapEvent<>("", key, newMapValue, oldMapValue)));
291 return new MapEntryUpdateResult<>(updateStatus, "", key, oldMapValue, 278 return new MapEntryUpdateResult<>(updateStatus, "", key, oldMapValue,
292 newMapValue); 279 newMapValue);
293 } 280 }
...@@ -295,12 +282,10 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -295,12 +282,10 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
295 /** 282 /**
296 * Handles a clear commit. 283 * Handles a clear commit.
297 * 284 *
298 - * @param commit 285 + * @param commit clear commit
299 - * clear commit
300 * @return clear result 286 * @return clear result
301 */ 287 */
302 - protected MapEntryUpdateResult.Status clear( 288 + protected MapEntryUpdateResult.Status clear(Commit<? extends Clear> commit) {
303 - Commit<? extends AtomixConsistentMapCommands.Clear> commit) {
304 try { 289 try {
305 Iterator<Map.Entry<String, MapEntryValue>> iterator = mapEntries 290 Iterator<Map.Entry<String, MapEntryValue>> iterator = mapEntries
306 .entrySet().iterator(); 291 .entrySet().iterator();
...@@ -310,7 +295,7 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -310,7 +295,7 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
310 MapEntryValue value = entry.getValue(); 295 MapEntryValue value = entry.getValue();
311 Versioned<byte[]> removedValue = new Versioned<>(value.value(), 296 Versioned<byte[]> removedValue = new Versioned<>(value.value(),
312 value.version()); 297 value.version());
313 - notify(new MapEvent<>("", key, null, removedValue)); 298 + publish(Lists.newArrayList(new MapEvent<>("", key, null, removedValue)));
314 value.discard(); 299 value.discard();
315 iterator.remove(); 300 iterator.remove();
316 } 301 }
...@@ -323,11 +308,9 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -323,11 +308,9 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
323 /** 308 /**
324 * Handles a listen commit. 309 * Handles a listen commit.
325 * 310 *
326 - * @param commit 311 + * @param commit listen commit
327 - * listen commit
328 */ 312 */
329 - protected void listen( 313 + protected void listen(Commit<? extends Listen> commit) {
330 - Commit<? extends AtomixConsistentMapCommands.Listen> commit) {
331 Long sessionId = commit.session().id(); 314 Long sessionId = commit.session().id();
332 listeners.put(sessionId, commit); 315 listeners.put(sessionId, commit);
333 commit.session() 316 commit.session()
...@@ -335,8 +318,7 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -335,8 +318,7 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
335 state -> { 318 state -> {
336 if (state == Session.State.CLOSED 319 if (state == Session.State.CLOSED
337 || state == Session.State.EXPIRED) { 320 || state == Session.State.EXPIRED) {
338 - Commit<? extends AtomixConsistentMapCommands.Listen> listener = listeners 321 + Commit<? extends Listen> listener = listeners.remove(sessionId);
339 - .remove(sessionId);
340 if (listener != null) { 322 if (listener != null) {
341 listener.close(); 323 listener.close();
342 } 324 }
...@@ -347,14 +329,12 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -347,14 +329,12 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
347 /** 329 /**
348 * Handles an unlisten commit. 330 * Handles an unlisten commit.
349 * 331 *
350 - * @param commit 332 + * @param commit unlisten commit
351 - * unlisten commit
352 */ 333 */
353 protected void unlisten( 334 protected void unlisten(
354 - Commit<? extends AtomixConsistentMapCommands.Unlisten> commit) { 335 + Commit<? extends Unlisten> commit) {
355 try { 336 try {
356 - Commit<? extends AtomixConsistentMapCommands.Listen> listener = listeners 337 + Commit<? extends Listen> listener = listeners.remove(commit.session());
357 - .remove(commit.session());
358 if (listener != null) { 338 if (listener != null) {
359 listener.close(); 339 listener.close();
360 } 340 }
...@@ -364,25 +344,12 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -364,25 +344,12 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
364 } 344 }
365 345
366 /** 346 /**
367 - * Triggers a change event.
368 - *
369 - * @param value
370 - * map event
371 - */
372 - private void notify(MapEvent<String, byte[]> value) {
373 - listeners.values().forEach(
374 - commit -> commit.session().publish("change", value));
375 - }
376 -
377 - /**
378 * Handles an prepare commit. 347 * Handles an prepare commit.
379 * 348 *
380 - * @param commit 349 + * @param commit transaction prepare commit
381 - * transaction prepare commit
382 * @return prepare result 350 * @return prepare result
383 */ 351 */
384 - protected PrepareResult prepare( 352 + protected PrepareResult prepare(Commit<? extends TransactionPrepare> commit) {
385 - Commit<? extends AtomixConsistentMapCommands.TransactionPrepare> commit) {
386 boolean ok = false; 353 boolean ok = false;
387 try { 354 try {
388 MapTransaction<String, byte[]> transaction = commit.operation().transaction(); 355 MapTransaction<String, byte[]> transaction = commit.operation().transaction();
...@@ -403,8 +370,7 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -403,8 +370,7 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
403 } 370 }
404 } 371 }
405 // No violations detected. Add to pendingTranctions and mark 372 // No violations detected. Add to pendingTranctions and mark
406 - // modified keys as 373 + // modified keys as locked for updates.
407 - // currently locked to updates.
408 pendingTransactions.put(transaction.transactionId(), commit); 374 pendingTransactions.put(transaction.transactionId(), commit);
409 transaction.updates().forEach(u -> preparedKeys.add(u.key())); 375 transaction.updates().forEach(u -> preparedKeys.add(u.key()));
410 ok = true; 376 ok = true;
...@@ -422,11 +388,10 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -422,11 +388,10 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
422 * @param commit transaction commit commit 388 * @param commit transaction commit commit
423 * @return commit result 389 * @return commit result
424 */ 390 */
425 - protected CommitResult commit( 391 + protected CommitResult commit(Commit<? extends TransactionCommit> commit) {
426 - Commit<? extends AtomixConsistentMapCommands.TransactionCommit> commit) {
427 TransactionId transactionId = commit.operation().transactionId(); 392 TransactionId transactionId = commit.operation().transactionId();
428 try { 393 try {
429 - Commit<? extends AtomixConsistentMapCommands.TransactionPrepare> prepareCommit = pendingTransactions 394 + Commit<? extends TransactionPrepare> prepareCommit = pendingTransactions
430 .remove(transactionId); 395 .remove(transactionId);
431 if (prepareCommit == null) { 396 if (prepareCommit == null) {
432 return CommitResult.UNKNOWN_TRANSACTION_ID; 397 return CommitResult.UNKNOWN_TRANSACTION_ID;
...@@ -437,8 +402,9 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -437,8 +402,9 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
437 .stream() 402 .stream()
438 .filter(update -> update.type() != MapUpdate.Type.REMOVE_IF_VERSION_MATCH) 403 .filter(update -> update.type() != MapUpdate.Type.REMOVE_IF_VERSION_MATCH)
439 .count(); 404 .count();
440 - CountDownCompleter<Commit<? extends AtomixConsistentMapCommands.TransactionPrepare>> completer = 405 + CountDownCompleter<Commit<? extends TransactionPrepare>> completer =
441 new CountDownCompleter<>(prepareCommit, totalReferencesToCommit, Commit::close); 406 new CountDownCompleter<>(prepareCommit, totalReferencesToCommit, Commit::close);
407 + List<MapEvent<String, byte[]>> eventsToPublish = Lists.newArrayList();
442 for (MapUpdate<String, byte[]> update : transaction.updates()) { 408 for (MapUpdate<String, byte[]> update : transaction.updates()) {
443 String key = update.key(); 409 String key = update.key();
444 MapEntryValue previousValue = mapEntries.remove(key); 410 MapEntryValue previousValue = mapEntries.remove(key);
...@@ -448,11 +414,15 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -448,11 +414,15 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
448 newValue = new TransactionalCommit(key, 414 newValue = new TransactionalCommit(key,
449 versionCounter.incrementAndGet(), completer); 415 versionCounter.incrementAndGet(), completer);
450 } 416 }
451 - mapEntries.put(key, newValue); 417 + eventsToPublish.add(new MapEvent<>("", key, toVersioned(newValue), toVersioned(previousValue)));
452 - // Notify map listeners 418 + if (newValue != null) {
453 - notify(new MapEvent<>("", key, toVersioned(newValue), 419 + mapEntries.put(key, newValue);
454 - toVersioned(previousValue))); 420 + }
421 + if (previousValue != null) {
422 + previousValue.discard();
423 + }
455 } 424 }
425 + publish(eventsToPublish);
456 return CommitResult.OK; 426 return CommitResult.OK;
457 } finally { 427 } finally {
458 commit.close(); 428 commit.close();
...@@ -465,12 +435,10 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -465,12 +435,10 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
465 * @param commit transaction rollback commit 435 * @param commit transaction rollback commit
466 * @return rollback result 436 * @return rollback result
467 */ 437 */
468 - protected RollbackResult rollback( 438 + protected RollbackResult rollback(Commit<? extends TransactionRollback> commit) {
469 - Commit<? extends AtomixConsistentMapCommands.TransactionRollback> commit) {
470 TransactionId transactionId = commit.operation().transactionId(); 439 TransactionId transactionId = commit.operation().transactionId();
471 try { 440 try {
472 - Commit<? extends AtomixConsistentMapCommands.TransactionPrepare> prepareCommit = pendingTransactions 441 + Commit<? extends TransactionPrepare> prepareCommit = pendingTransactions.remove(transactionId);
473 - .remove(transactionId);
474 if (prepareCommit == null) { 442 if (prepareCommit == null) {
475 return RollbackResult.UNKNOWN_TRANSACTION_ID; 443 return RollbackResult.UNKNOWN_TRANSACTION_ID;
476 } else { 444 } else {
...@@ -486,8 +454,14 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -486,8 +454,14 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
486 } 454 }
487 } 455 }
488 456
489 - private MapEntryUpdateResult.Status validate( 457 + /**
490 - AtomixConsistentMapCommands.UpdateAndGet update) { 458 + * Computes the update status that would result if the specified update were to applied to
459 + * the state machine.
460 + *
461 + * @param update update
462 + * @return status
463 + */
464 + private MapEntryUpdateResult.Status validate(UpdateAndGet update) {
491 MapEntryValue existingValue = mapEntries.get(update.key()); 465 MapEntryValue existingValue = mapEntries.get(update.key());
492 if (existingValue == null && update.value() == null) { 466 if (existingValue == null && update.value() == null) {
493 return MapEntryUpdateResult.Status.NOOP; 467 return MapEntryUpdateResult.Status.NOOP;
...@@ -504,9 +478,22 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -504,9 +478,22 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
504 : MapEntryUpdateResult.Status.PRECONDITION_FAILED; 478 : MapEntryUpdateResult.Status.PRECONDITION_FAILED;
505 } 479 }
506 480
481 + /**
482 + * Utility for turning a {@code MapEntryValue} to {@code Versioned}.
483 + * @param value map entry value
484 + * @return versioned instance
485 + */
507 private Versioned<byte[]> toVersioned(MapEntryValue value) { 486 private Versioned<byte[]> toVersioned(MapEntryValue value) {
508 - return value == null ? null : new Versioned<>(value.value(), 487 + return value == null ? null : new Versioned<>(value.value(), value.version());
509 - value.version()); 488 + }
489 +
490 + /**
491 + * Publishes events to listeners.
492 + *
493 + * @param events list of map event to publish
494 + */
495 + private void publish(List<MapEvent<String, byte[]>> events) {
496 + listeners.values().forEach(commit -> commit.session().publish(AtomixConsistentMap.CHANGE_SUBJECT, events));
510 } 497 }
511 498
512 @Override 499 @Override
...@@ -529,8 +516,7 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -529,8 +516,7 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
529 } 516 }
530 517
531 private void closeListener(Long sessionId) { 518 private void closeListener(Long sessionId) {
532 - Commit<? extends AtomixConsistentMapCommands.Listen> commit = listeners 519 + Commit<? extends Listen> commit = listeners.remove(sessionId);
533 - .remove(sessionId);
534 if (commit != null) { 520 if (commit != null) {
535 commit.close(); 521 commit.close();
536 } 522 }
...@@ -566,11 +552,9 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -566,11 +552,9 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
566 */ 552 */
567 private class NonTransactionalCommit implements MapEntryValue { 553 private class NonTransactionalCommit implements MapEntryValue {
568 private final long version; 554 private final long version;
569 - private final Commit<? extends AtomixConsistentMapCommands.UpdateAndGet> commit; 555 + private final Commit<? extends UpdateAndGet> commit;
570 556
571 - public NonTransactionalCommit( 557 + public NonTransactionalCommit(long version, Commit<? extends UpdateAndGet> commit) {
572 - long version,
573 - Commit<? extends AtomixConsistentMapCommands.UpdateAndGet> commit) {
574 this.version = version; 558 this.version = version;
575 this.commit = commit; 559 this.commit = commit;
576 } 560 }
...@@ -598,12 +582,12 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements ...@@ -598,12 +582,12 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements
598 private class TransactionalCommit implements MapEntryValue { 582 private class TransactionalCommit implements MapEntryValue {
599 private final String key; 583 private final String key;
600 private final long version; 584 private final long version;
601 - private final CountDownCompleter<Commit<? extends AtomixConsistentMapCommands.TransactionPrepare>> completer; 585 + private final CountDownCompleter<Commit<? extends TransactionPrepare>> completer;
602 586
603 public TransactionalCommit( 587 public TransactionalCommit(
604 String key, 588 String key,
605 long version, 589 long version,
606 - CountDownCompleter<Commit<? extends AtomixConsistentMapCommands.TransactionPrepare>> commit) { 590 + CountDownCompleter<Commit<? extends TransactionPrepare>> commit) {
607 this.key = key; 591 this.key = key;
608 this.version = version; 592 this.version = version;
609 this.completer = commit; 593 this.completer = commit;
......
...@@ -17,7 +17,6 @@ package org.onosproject.store.primitives.resources.impl; ...@@ -17,7 +17,6 @@ package org.onosproject.store.primitives.resources.impl;
17 17
18 import io.atomix.catalyst.util.Listener; 18 import io.atomix.catalyst.util.Listener;
19 import io.atomix.copycat.client.CopycatClient; 19 import io.atomix.copycat.client.CopycatClient;
20 -import io.atomix.resource.Consistency;
21 import io.atomix.resource.Resource; 20 import io.atomix.resource.Resource;
22 import io.atomix.resource.ResourceTypeInfo; 21 import io.atomix.resource.ResourceTypeInfo;
23 22
...@@ -26,6 +25,7 @@ import java.util.Set; ...@@ -26,6 +25,7 @@ import java.util.Set;
26 import java.util.concurrent.CompletableFuture; 25 import java.util.concurrent.CompletableFuture;
27 import java.util.function.Consumer; 26 import java.util.function.Consumer;
28 27
28 +import org.onlab.util.SharedExecutors;
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;
...@@ -37,8 +37,8 @@ import com.google.common.collect.Sets; ...@@ -37,8 +37,8 @@ import com.google.common.collect.Sets;
37 * Distributed resource providing the {@link AsyncLeaderElector} primitive. 37 * Distributed resource providing the {@link AsyncLeaderElector} primitive.
38 */ 38 */
39 @ResourceTypeInfo(id = -152, stateMachine = AtomixLeaderElectorState.class) 39 @ResourceTypeInfo(id = -152, stateMachine = AtomixLeaderElectorState.class)
40 -public class AtomixLeaderElector 40 +public class AtomixLeaderElector extends Resource<AtomixLeaderElector, Resource.Options>
41 - extends Resource<AtomixLeaderElector, Resource.Options> implements AsyncLeaderElector { 41 + implements AsyncLeaderElector {
42 private final Set<Consumer<Change<Leadership>>> leadershipChangeListeners = 42 private final Set<Consumer<Change<Leadership>>> leadershipChangeListeners =
43 Sets.newConcurrentHashSet(); 43 Sets.newConcurrentHashSet();
44 44
...@@ -62,13 +62,8 @@ public class AtomixLeaderElector ...@@ -62,13 +62,8 @@ public class AtomixLeaderElector
62 } 62 }
63 63
64 private void handleEvent(Change<Leadership> change) { 64 private void handleEvent(Change<Leadership> change) {
65 - leadershipChangeListeners.forEach(l -> l.accept(change)); 65 + SharedExecutors.getSingleThreadExecutor().execute(() ->
66 - } 66 + leadershipChangeListeners.forEach(l -> l.accept(change)));
67 -
68 - @Override
69 - public AtomixLeaderElector with(Consistency consistency) {
70 - super.with(consistency);
71 - return this;
72 } 67 }
73 68
74 @Override 69 @Override
......
...@@ -21,13 +21,16 @@ import java.util.Set; ...@@ -21,13 +21,16 @@ import java.util.Set;
21 import org.onosproject.cluster.Leadership; 21 import org.onosproject.cluster.Leadership;
22 import org.onosproject.cluster.NodeId; 22 import org.onosproject.cluster.NodeId;
23 23
24 +
24 import com.google.common.base.MoreObjects; 25 import com.google.common.base.MoreObjects;
25 import com.google.common.base.Strings; 26 import com.google.common.base.Strings;
26 27
27 import io.atomix.catalyst.buffer.BufferInput; 28 import io.atomix.catalyst.buffer.BufferInput;
28 import io.atomix.catalyst.buffer.BufferOutput; 29 import io.atomix.catalyst.buffer.BufferOutput;
29 import io.atomix.catalyst.serializer.CatalystSerializable; 30 import io.atomix.catalyst.serializer.CatalystSerializable;
31 +import io.atomix.catalyst.serializer.SerializableTypeResolver;
30 import io.atomix.catalyst.serializer.Serializer; 32 import io.atomix.catalyst.serializer.Serializer;
33 +import io.atomix.catalyst.serializer.SerializerRegistry;
31 import io.atomix.catalyst.util.Assert; 34 import io.atomix.catalyst.util.Assert;
32 import io.atomix.copycat.client.Command; 35 import io.atomix.copycat.client.Command;
33 import io.atomix.copycat.client.Query; 36 import io.atomix.copycat.client.Query;
...@@ -232,6 +235,18 @@ public final class AtomixLeaderElectorCommands { ...@@ -232,6 +235,18 @@ public final class AtomixLeaderElectorCommands {
232 .add("nodeId", nodeId) 235 .add("nodeId", nodeId)
233 .toString(); 236 .toString();
234 } 237 }
238 +
239 + @Override
240 + public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
241 + buffer.writeString(topic);
242 + buffer.writeString(nodeId.toString());
243 + }
244 +
245 + @Override
246 + public void readObject(BufferInput<?> buffer, Serializer serializer) {
247 + topic = buffer.readString();
248 + nodeId = new NodeId(buffer.readString());
249 + }
235 } 250 }
236 251
237 /** 252 /**
...@@ -263,6 +278,16 @@ public final class AtomixLeaderElectorCommands { ...@@ -263,6 +278,16 @@ public final class AtomixLeaderElectorCommands {
263 .add("topic", topic) 278 .add("topic", topic)
264 .toString(); 279 .toString();
265 } 280 }
281 +
282 + @Override
283 + public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
284 + buffer.writeString(topic);
285 + }
286 +
287 + @Override
288 + public void readObject(BufferInput<?> buffer, Serializer serializer) {
289 + topic = buffer.readString();
290 + }
266 } 291 }
267 292
268 /** 293 /**
...@@ -306,5 +331,34 @@ public final class AtomixLeaderElectorCommands { ...@@ -306,5 +331,34 @@ public final class AtomixLeaderElectorCommands {
306 .add("nodeId", nodeId) 331 .add("nodeId", nodeId)
307 .toString(); 332 .toString();
308 } 333 }
334 +
335 + @Override
336 + public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
337 + buffer.writeString(topic);
338 + buffer.writeString(nodeId.toString());
339 + }
340 +
341 + @Override
342 + public void readObject(BufferInput<?> buffer, Serializer serializer) {
343 + topic = buffer.readString();
344 + nodeId = new NodeId(buffer.readString());
345 + }
346 + }
347 +
348 + /**
349 + * Map command type resolver.
350 + */
351 + public static class TypeResolver implements SerializableTypeResolver {
352 + @Override
353 + public void resolve(SerializerRegistry registry) {
354 + registry.register(Run.class, -861);
355 + registry.register(Withdraw.class, -862);
356 + registry.register(Anoint.class, -863);
357 + registry.register(GetAllLeaderships.class, -864);
358 + registry.register(GetElectedTopics.class, -865);
359 + registry.register(GetLeadership.class, -866);
360 + registry.register(Listen.class, -867);
361 + registry.register(Unlisten.class, -868);
362 + }
309 } 363 }
310 } 364 }
......
...@@ -41,6 +41,14 @@ import org.onosproject.cluster.Leader; ...@@ -41,6 +41,14 @@ import org.onosproject.cluster.Leader;
41 import org.onosproject.cluster.Leadership; 41 import org.onosproject.cluster.Leadership;
42 import org.onosproject.cluster.NodeId; 42 import org.onosproject.cluster.NodeId;
43 import org.onosproject.event.Change; 43 import org.onosproject.event.Change;
44 +import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorCommands.Anoint;
45 +import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorCommands.GetAllLeaderships;
46 +import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorCommands.GetElectedTopics;
47 +import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorCommands.GetLeadership;
48 +import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorCommands.Listen;
49 +import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorCommands.Run;
50 +import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorCommands.Unlisten;
51 +import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorCommands.Withdraw;
44 import org.onosproject.store.serializers.KryoNamespaces; 52 import org.onosproject.store.serializers.KryoNamespaces;
45 import org.onosproject.store.service.Serializer; 53 import org.onosproject.store.service.Serializer;
46 import org.slf4j.Logger; 54 import org.slf4j.Logger;
...@@ -59,7 +67,7 @@ public class AtomixLeaderElectorState extends ResourceStateMachine ...@@ -59,7 +67,7 @@ public class AtomixLeaderElectorState extends ResourceStateMachine
59 private final Logger log = getLogger(getClass()); 67 private final Logger log = getLogger(getClass());
60 private Map<String, AtomicLong> termCounters = new HashMap<>(); 68 private Map<String, AtomicLong> termCounters = new HashMap<>();
61 private Map<String, ElectionState> elections = new HashMap<>(); 69 private Map<String, ElectionState> elections = new HashMap<>();
62 - private final Map<Long, Commit<? extends AtomixLeaderElectorCommands.Listen>> listeners = new LinkedHashMap<>(); 70 + private final Map<Long, Commit<? extends Listen>> listeners = new LinkedHashMap<>();
63 private final Serializer serializer = Serializer.using(Arrays.asList(KryoNamespaces.API), 71 private final Serializer serializer = Serializer.using(Arrays.asList(KryoNamespaces.API),
64 ElectionState.class, 72 ElectionState.class,
65 Registration.class); 73 Registration.class);
...@@ -67,16 +75,16 @@ public class AtomixLeaderElectorState extends ResourceStateMachine ...@@ -67,16 +75,16 @@ public class AtomixLeaderElectorState extends ResourceStateMachine
67 @Override 75 @Override
68 protected void configure(StateMachineExecutor executor) { 76 protected void configure(StateMachineExecutor executor) {
69 // Notification 77 // Notification
70 - executor.register(AtomixLeaderElectorCommands.Listen.class, this::listen); 78 + executor.register(Listen.class, this::listen);
71 - executor.register(AtomixLeaderElectorCommands.Unlisten.class, this::unlisten); 79 + executor.register(Unlisten.class, this::unlisten);
72 // Commands 80 // Commands
73 - executor.register(AtomixLeaderElectorCommands.Run.class, this::run); 81 + executor.register(Run.class, this::run);
74 - executor.register(AtomixLeaderElectorCommands.Withdraw.class, this::withdraw); 82 + executor.register(Withdraw.class, this::withdraw);
75 - executor.register(AtomixLeaderElectorCommands.Anoint.class, this::anoint); 83 + executor.register(Anoint.class, this::anoint);
76 // Queries 84 // Queries
77 - executor.register(AtomixLeaderElectorCommands.GetLeadership.class, this::leadership); 85 + executor.register(GetLeadership.class, this::leadership);
78 - executor.register(AtomixLeaderElectorCommands.GetAllLeaderships.class, this::allLeaderships); 86 + executor.register(GetAllLeaderships.class, this::allLeaderships);
79 - executor.register(AtomixLeaderElectorCommands.GetElectedTopics.class, this::electedTopics); 87 + executor.register(GetElectedTopics.class, this::electedTopics);
80 } 88 }
81 89
82 private void notifyLeadershipChange(Leadership previousLeadership, Leadership newLeadership) { 90 private void notifyLeadershipChange(Leadership previousLeadership, Leadership newLeadership) {
...@@ -96,7 +104,7 @@ public class AtomixLeaderElectorState extends ResourceStateMachine ...@@ -96,7 +104,7 @@ public class AtomixLeaderElectorState extends ResourceStateMachine
96 * 104 *
97 * @param commit listen commit 105 * @param commit listen commit
98 */ 106 */
99 - public void listen(Commit<? extends AtomixLeaderElectorCommands.Listen> commit) { 107 + public void listen(Commit<? extends Listen> commit) {
100 if (listeners.putIfAbsent(commit.session().id(), commit) != null) { 108 if (listeners.putIfAbsent(commit.session().id(), commit) != null) {
101 commit.close(); 109 commit.close();
102 } 110 }
...@@ -107,9 +115,9 @@ public class AtomixLeaderElectorState extends ResourceStateMachine ...@@ -107,9 +115,9 @@ public class AtomixLeaderElectorState extends ResourceStateMachine
107 * 115 *
108 * @param commit unlisten commit 116 * @param commit unlisten commit
109 */ 117 */
110 - public void unlisten(Commit<? extends AtomixLeaderElectorCommands.Unlisten> commit) { 118 + public void unlisten(Commit<? extends Unlisten> commit) {
111 try { 119 try {
112 - Commit<? extends AtomixLeaderElectorCommands.Listen> listener = listeners.remove(commit.session().id()); 120 + Commit<? extends Listen> listener = listeners.remove(commit.session().id());
113 if (listener != null) { 121 if (listener != null) {
114 listener.close(); 122 listener.close();
115 } 123 }
...@@ -123,7 +131,7 @@ public class AtomixLeaderElectorState extends ResourceStateMachine ...@@ -123,7 +131,7 @@ public class AtomixLeaderElectorState extends ResourceStateMachine
123 * @param commit commit entry 131 * @param commit commit entry
124 * @return topic leader. If no previous leader existed this is the node that just entered the race. 132 * @return topic leader. If no previous leader existed this is the node that just entered the race.
125 */ 133 */
126 - public Leadership run(Commit<? extends AtomixLeaderElectorCommands.Run> commit) { 134 + public Leadership run(Commit<? extends Run> commit) {
127 try { 135 try {
128 String topic = commit.operation().topic(); 136 String topic = commit.operation().topic();
129 Leadership oldLeadership = leadership(topic); 137 Leadership oldLeadership = leadership(topic);
...@@ -154,7 +162,7 @@ public class AtomixLeaderElectorState extends ResourceStateMachine ...@@ -154,7 +162,7 @@ public class AtomixLeaderElectorState extends ResourceStateMachine
154 * Applies an {@link AtomixLeaderElectorCommands.Withdraw} commit. 162 * Applies an {@link AtomixLeaderElectorCommands.Withdraw} commit.
155 * @param commit withdraw commit 163 * @param commit withdraw commit
156 */ 164 */
157 - public void withdraw(Commit<? extends AtomixLeaderElectorCommands.Withdraw> commit) { 165 + public void withdraw(Commit<? extends Withdraw> commit) {
158 try { 166 try {
159 String topic = commit.operation().topic(); 167 String topic = commit.operation().topic();
160 Leadership oldLeadership = leadership(topic); 168 Leadership oldLeadership = leadership(topic);
...@@ -174,7 +182,7 @@ public class AtomixLeaderElectorState extends ResourceStateMachine ...@@ -174,7 +182,7 @@ public class AtomixLeaderElectorState extends ResourceStateMachine
174 * @param commit anoint commit 182 * @param commit anoint commit
175 * @return {@code true} if changes were made and the transfer occurred; {@code false} if it did not. 183 * @return {@code true} if changes were made and the transfer occurred; {@code false} if it did not.
176 */ 184 */
177 - public boolean anoint(Commit<? extends AtomixLeaderElectorCommands.Anoint> commit) { 185 + public boolean anoint(Commit<? extends Anoint> commit) {
178 try { 186 try {
179 String topic = commit.operation().topic(); 187 String topic = commit.operation().topic();
180 Leadership oldLeadership = leadership(topic); 188 Leadership oldLeadership = leadership(topic);
...@@ -197,7 +205,7 @@ public class AtomixLeaderElectorState extends ResourceStateMachine ...@@ -197,7 +205,7 @@ public class AtomixLeaderElectorState extends ResourceStateMachine
197 * @param commit GetLeadership commit 205 * @param commit GetLeadership commit
198 * @return leader 206 * @return leader
199 */ 207 */
200 - public Leadership leadership(Commit<? extends AtomixLeaderElectorCommands.GetLeadership> commit) { 208 + public Leadership leadership(Commit<? extends GetLeadership> commit) {
201 String topic = commit.operation().topic(); 209 String topic = commit.operation().topic();
202 try { 210 try {
203 return leadership(topic); 211 return leadership(topic);
...@@ -211,7 +219,7 @@ public class AtomixLeaderElectorState extends ResourceStateMachine ...@@ -211,7 +219,7 @@ public class AtomixLeaderElectorState extends ResourceStateMachine
211 * @param commit commit entry 219 * @param commit commit entry
212 * @return set of topics for which the node is the leader 220 * @return set of topics for which the node is the leader
213 */ 221 */
214 - public Set<String> electedTopics(Commit<? extends AtomixLeaderElectorCommands.GetElectedTopics> commit) { 222 + public Set<String> electedTopics(Commit<? extends GetElectedTopics> commit) {
215 try { 223 try {
216 NodeId nodeId = commit.operation().nodeId(); 224 NodeId nodeId = commit.operation().nodeId();
217 return Maps.filterEntries(elections, e -> { 225 return Maps.filterEntries(elections, e -> {
...@@ -228,8 +236,7 @@ public class AtomixLeaderElectorState extends ResourceStateMachine ...@@ -228,8 +236,7 @@ public class AtomixLeaderElectorState extends ResourceStateMachine
228 * @param commit GetAllLeaderships commit 236 * @param commit GetAllLeaderships commit
229 * @return topic to leader mapping 237 * @return topic to leader mapping
230 */ 238 */
231 - public Map<String, Leadership> allLeaderships( 239 + public Map<String, Leadership> allLeaderships(Commit<? extends GetAllLeaderships> commit) {
232 - Commit<? extends AtomixLeaderElectorCommands.GetAllLeaderships> commit) {
233 try { 240 try {
234 return Maps.transformEntries(elections, (k, v) -> leadership(k)); 241 return Maps.transformEntries(elections, (k, v) -> leadership(k));
235 } finally { 242 } finally {
......