Madan Jampani

Publish a list of changes when leadership changes occur

Change-Id: I99a4e239ac5aa9999b3a735cdf004941a5957a93
...@@ -291,17 +291,14 @@ public final class AtomixLeaderElectorCommands { ...@@ -291,17 +291,14 @@ public final class AtomixLeaderElectorCommands {
291 } 291 }
292 292
293 /** 293 /**
294 - * Command for administratively anointing a node as leader. 294 + * Command for administratively changing the leadership state for a node.
295 */ 295 */
296 @SuppressWarnings("serial") 296 @SuppressWarnings("serial")
297 - public static class Anoint extends ElectionCommand<Boolean> { 297 + public abstract static class ElectionChangeCommand<V> extends ElectionCommand<V> {
298 private String topic; 298 private String topic;
299 private NodeId nodeId; 299 private NodeId nodeId;
300 300
301 - public Anoint() { 301 + public ElectionChangeCommand(String topic, NodeId nodeId) {
302 - }
303 -
304 - public Anoint(String topic, NodeId nodeId) {
305 this.topic = topic; 302 this.topic = topic;
306 this.nodeId = nodeId; 303 this.nodeId = nodeId;
307 } 304 }
...@@ -346,57 +343,22 @@ public final class AtomixLeaderElectorCommands { ...@@ -346,57 +343,22 @@ public final class AtomixLeaderElectorCommands {
346 } 343 }
347 344
348 /** 345 /**
349 - * Command for administratively promote a node as top candidate. 346 + * Command for administratively anoint a node as leader.
350 */ 347 */
351 @SuppressWarnings("serial") 348 @SuppressWarnings("serial")
352 - public static class Promote extends ElectionCommand<Boolean> { 349 + public static class Anoint extends ElectionChangeCommand<Boolean> {
353 - private String topic; 350 + public Anoint(String topic, NodeId nodeId) {
354 - private NodeId nodeId; 351 + super(topic, nodeId);
355 -
356 - public Promote() {
357 - }
358 -
359 - public Promote(String topic, NodeId nodeId) {
360 - this.topic = topic;
361 - this.nodeId = nodeId;
362 } 352 }
363 -
364 - /**
365 - * Returns the topic.
366 - *
367 - * @return The topic
368 - */
369 - public String topic() {
370 - return topic;
371 } 353 }
372 354
373 /** 355 /**
374 - * Returns the nodeId to make top candidate. 356 + * Command for administratively promote a node as top candidate.
375 - *
376 - * @return The nodeId
377 */ 357 */
378 - public NodeId nodeId() { 358 + @SuppressWarnings("serial")
379 - return nodeId; 359 + public static class Promote extends ElectionChangeCommand<Boolean> {
380 - } 360 + public Promote(String topic, NodeId nodeId) {
381 - 361 + super(topic, nodeId);
382 - @Override
383 - public String toString() {
384 - return MoreObjects.toStringHelper(getClass())
385 - .add("topic", topic)
386 - .add("nodeId", nodeId)
387 - .toString();
388 - }
389 -
390 - @Override
391 - public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
392 - buffer.writeString(topic);
393 - buffer.writeString(nodeId.toString());
394 - }
395 -
396 - @Override
397 - public void readObject(BufferInput<?> buffer, Serializer serializer) {
398 - topic = buffer.readString();
399 - nodeId = new NodeId(buffer.readString());
400 } 362 }
401 } 363 }
402 364
......
...@@ -97,7 +97,7 @@ public class AtomixLeaderElectorState extends ResourceStateMachine ...@@ -97,7 +97,7 @@ public class AtomixLeaderElectorState extends ResourceStateMachine
97 } 97 }
98 98
99 private void notifyLeadershipChange(Leadership previousLeadership, Leadership newLeadership) { 99 private void notifyLeadershipChange(Leadership previousLeadership, Leadership newLeadership) {
100 - notifyLeadershipChanges(Arrays.asList(new Change<>(previousLeadership, newLeadership))); 100 + notifyLeadershipChanges(Lists.newArrayList(new Change<>(previousLeadership, newLeadership)));
101 } 101 }
102 102
103 private void notifyLeadershipChanges(List<Change<Leadership>> changes) { 103 private void notifyLeadershipChanges(List<Change<Leadership>> changes) {
...@@ -247,7 +247,7 @@ public class AtomixLeaderElectorState extends ResourceStateMachine ...@@ -247,7 +247,7 @@ public class AtomixLeaderElectorState extends ResourceStateMachine
247 */ 247 */
248 public void evict(Commit<? extends Evict> commit) { 248 public void evict(Commit<? extends Evict> commit) {
249 try { 249 try {
250 - List<Change<Leadership>> changes = Lists.newLinkedList(); 250 + List<Change<Leadership>> changes = Lists.newArrayList();
251 NodeId nodeId = commit.operation().nodeId(); 251 NodeId nodeId = commit.operation().nodeId();
252 Set<String> topics = Maps.filterValues(elections, e -> e.candidates().contains(nodeId)).keySet(); 252 Set<String> topics = Maps.filterValues(elections, e -> e.candidates().contains(nodeId)).keySet();
253 topics.forEach(topic -> { 253 topics.forEach(topic -> {
...@@ -330,14 +330,16 @@ public class AtomixLeaderElectorState extends ResourceStateMachine ...@@ -330,14 +330,16 @@ public class AtomixLeaderElectorState extends ResourceStateMachine
330 listener.close(); 330 listener.close();
331 } 331 }
332 Set<String> topics = elections.keySet(); 332 Set<String> topics = elections.keySet();
333 + List<Change<Leadership>> changes = Lists.newArrayList();
333 topics.forEach(topic -> { 334 topics.forEach(topic -> {
334 Leadership oldLeadership = leadership(topic); 335 Leadership oldLeadership = leadership(topic);
335 elections.compute(topic, (k, v) -> v.cleanup(session, termCounter(topic)::incrementAndGet)); 336 elections.compute(topic, (k, v) -> v.cleanup(session, termCounter(topic)::incrementAndGet));
336 Leadership newLeadership = leadership(topic); 337 Leadership newLeadership = leadership(topic);
337 if (!Objects.equal(oldLeadership, newLeadership)) { 338 if (!Objects.equal(oldLeadership, newLeadership)) {
338 - notifyLeadershipChange(oldLeadership, newLeadership); 339 + changes.add(new Change<>(oldLeadership, newLeadership));
339 } 340 }
340 }); 341 });
342 + notifyLeadershipChanges(changes);
341 } 343 }
342 344
343 private static class Registration { 345 private static class Registration {
......