Jonathan Hart
Committed by Gerrit Code Review

Simplify anti-entropy code

Change-Id: I6568b1cc7c67e12c5a81ec9f8680f6461813ddce
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
15 */ 15 */
16 package org.onosproject.store; 16 package org.onosproject.store;
17 17
18 +import static com.google.common.base.Preconditions.checkNotNull;
19 +
18 /** 20 /**
19 * Opaque version structure. 21 * Opaque version structure.
20 * <p> 22 * <p>
...@@ -28,4 +30,14 @@ public interface Timestamp extends Comparable<Timestamp> { ...@@ -28,4 +30,14 @@ public interface Timestamp extends Comparable<Timestamp> {
28 30
29 @Override 31 @Override
30 public abstract boolean equals(Object obj); 32 public abstract boolean equals(Object obj);
33 +
34 + /**
35 + * Tests if this timestamp is newer than the specified timestamp.
36 + *
37 + * @param other timestamp to compare against
38 + * @return true if this instance is newer
39 + */
40 + public default boolean isNewerThan(Timestamp other) {
41 + return this.compareTo(checkNotNull(other)) > 0;
42 + }
31 } 43 }
......
...@@ -1170,7 +1170,8 @@ public class GossipDeviceStore ...@@ -1170,7 +1170,8 @@ public class GossipDeviceStore
1170 Timestamped<DeviceDescription> lProvDevice = lDeviceDescs.getDeviceDesc(); 1170 Timestamped<DeviceDescription> lProvDevice = lDeviceDescs.getDeviceDesc();
1171 Timestamp advDevTimestamp = devAds.get(devFragId); 1171 Timestamp advDevTimestamp = devAds.get(devFragId);
1172 1172
1173 - if (advDevTimestamp == null || lProvDevice.isNewer(advDevTimestamp)) { 1173 + if (advDevTimestamp == null || lProvDevice.isNewerThan(
1174 + advDevTimestamp)) {
1174 // remote does not have it or outdated, suggest 1175 // remote does not have it or outdated, suggest
1175 notifyPeer(sender, new InternalDeviceEvent(provId, deviceId, lProvDevice)); 1176 notifyPeer(sender, new InternalDeviceEvent(provId, deviceId, lProvDevice));
1176 } else if (!lProvDevice.timestamp().equals(advDevTimestamp)) { 1177 } else if (!lProvDevice.timestamp().equals(advDevTimestamp)) {
...@@ -1188,7 +1189,8 @@ public class GossipDeviceStore ...@@ -1188,7 +1189,8 @@ public class GossipDeviceStore
1188 final PortFragmentId portFragId = new PortFragmentId(deviceId, provId, num); 1189 final PortFragmentId portFragId = new PortFragmentId(deviceId, provId, num);
1189 1190
1190 Timestamp advPortTimestamp = portAds.get(portFragId); 1191 Timestamp advPortTimestamp = portAds.get(portFragId);
1191 - if (advPortTimestamp == null || lPort.isNewer(advPortTimestamp)) { 1192 + if (advPortTimestamp == null || lPort.isNewerThan(
1193 + advPortTimestamp)) {
1192 // remote does not have it or outdated, suggest 1194 // remote does not have it or outdated, suggest
1193 notifyPeer(sender, new InternalPortStatusEvent(provId, deviceId, lPort)); 1195 notifyPeer(sender, new InternalPortStatusEvent(provId, deviceId, lPort));
1194 } else if (!lPort.timestamp().equals(advPortTimestamp)) { 1196 } else if (!lPort.timestamp().equals(advPortTimestamp)) {
......
...@@ -262,7 +262,7 @@ public class EventuallyConsistentMapImpl<K, V> ...@@ -262,7 +262,7 @@ public class EventuallyConsistentMapImpl<K, V>
262 262
263 private boolean putInternal(K key, V value, Timestamp timestamp) { 263 private boolean putInternal(K key, V value, Timestamp timestamp) {
264 Timestamp removed = removedItems.get(key); 264 Timestamp removed = removedItems.get(key);
265 - if (removed != null && removed.compareTo(timestamp) > 0) { 265 + if (removed != null && removed.isNewerThan(timestamp)) {
266 log.debug("ecmap - removed was newer {}", value); 266 log.debug("ecmap - removed was newer {}", value);
267 return false; 267 return false;
268 } 268 }
...@@ -270,7 +270,7 @@ public class EventuallyConsistentMapImpl<K, V> ...@@ -270,7 +270,7 @@ public class EventuallyConsistentMapImpl<K, V>
270 boolean success; 270 boolean success;
271 synchronized (this) { 271 synchronized (this) {
272 Timestamped<V> existing = items.get(key); 272 Timestamped<V> existing = items.get(key);
273 - if (existing != null && existing.isNewer(timestamp)) { 273 + if (existing != null && existing.isNewerThan(timestamp)) {
274 log.debug("ecmap - existing was newer {}", value); 274 log.debug("ecmap - existing was newer {}", value);
275 success = false; 275 success = false;
276 } else { 276 } else {
...@@ -305,7 +305,7 @@ public class EventuallyConsistentMapImpl<K, V> ...@@ -305,7 +305,7 @@ public class EventuallyConsistentMapImpl<K, V>
305 private boolean removeInternal(K key, Timestamp timestamp) { 305 private boolean removeInternal(K key, Timestamp timestamp) {
306 Timestamped<V> value = items.get(key); 306 Timestamped<V> value = items.get(key);
307 if (value != null) { 307 if (value != null) {
308 - if (value.isNewer(timestamp)) { 308 + if (value.isNewerThan(timestamp)) {
309 return false; 309 return false;
310 } else { 310 } else {
311 items.remove(key, value); 311 items.remove(key, value);
...@@ -315,7 +315,7 @@ public class EventuallyConsistentMapImpl<K, V> ...@@ -315,7 +315,7 @@ public class EventuallyConsistentMapImpl<K, V>
315 Timestamp removedTimestamp = removedItems.get(key); 315 Timestamp removedTimestamp = removedItems.get(key);
316 if (removedTimestamp == null) { 316 if (removedTimestamp == null) {
317 return removedItems.putIfAbsent(key, timestamp) == null; 317 return removedItems.putIfAbsent(key, timestamp) == null;
318 - } else if (timestamp.compareTo(removedTimestamp) > 0) { 318 + } else if (timestamp.isNewerThan(removedTimestamp)) {
319 return removedItems.replace(key, removedTimestamp, timestamp); 319 return removedItems.replace(key, removedTimestamp, timestamp);
320 } else { 320 } else {
321 return false; 321 return false;
...@@ -614,7 +614,7 @@ public class EventuallyConsistentMapImpl<K, V> ...@@ -614,7 +614,7 @@ public class EventuallyConsistentMapImpl<K, V>
614 remoteTimestamp = ad.tombstones().get(key); 614 remoteTimestamp = ad.tombstones().get(key);
615 } 615 }
616 if (remoteTimestamp == null || localValue 616 if (remoteTimestamp == null || localValue
617 - .isNewer(remoteTimestamp)) { 617 + .isNewerThan(remoteTimestamp)) {
618 // local value is more recent, push to sender 618 // local value is more recent, push to sender
619 updatesToSend 619 updatesToSend
620 .add(new PutEntry<>(key, localValue.value(), 620 .add(new PutEntry<>(key, localValue.value(),
...@@ -623,7 +623,7 @@ public class EventuallyConsistentMapImpl<K, V> ...@@ -623,7 +623,7 @@ public class EventuallyConsistentMapImpl<K, V>
623 623
624 Timestamp remoteDeadTimestamp = ad.tombstones().get(key); 624 Timestamp remoteDeadTimestamp = ad.tombstones().get(key);
625 if (remoteDeadTimestamp != null && 625 if (remoteDeadTimestamp != null &&
626 - remoteDeadTimestamp.compareTo(localValue.timestamp()) > 0) { 626 + remoteDeadTimestamp.isNewerThan(localValue.timestamp())) {
627 // sender has a more recent remove 627 // sender has a more recent remove
628 if (removeInternal(key, remoteDeadTimestamp)) { 628 if (removeInternal(key, remoteDeadTimestamp)) {
629 externalEvents.add(new EventuallyConsistentMapEvent<>( 629 externalEvents.add(new EventuallyConsistentMapEvent<>(
...@@ -664,7 +664,7 @@ public class EventuallyConsistentMapImpl<K, V> ...@@ -664,7 +664,7 @@ public class EventuallyConsistentMapImpl<K, V>
664 664
665 Timestamp remoteLiveTimestamp = ad.timestamps().get(key); 665 Timestamp remoteLiveTimestamp = ad.timestamps().get(key);
666 if (remoteLiveTimestamp != null 666 if (remoteLiveTimestamp != null
667 - && localDeadTimestamp.compareTo(remoteLiveTimestamp) > 0) { 667 + && localDeadTimestamp.isNewerThan(remoteLiveTimestamp)) {
668 // sender has zombie, push remove 668 // sender has zombie, push remove
669 removesToSend 669 removesToSend
670 .add(new RemoveEntry<>(key, localDeadTimestamp)); 670 .add(new RemoveEntry<>(key, localDeadTimestamp));
...@@ -702,18 +702,19 @@ public class EventuallyConsistentMapImpl<K, V> ...@@ -702,18 +702,19 @@ public class EventuallyConsistentMapImpl<K, V>
702 702
703 Timestamped<V> local = items.get(key); 703 Timestamped<V> local = items.get(key);
704 Timestamp localDead = removedItems.get(key); 704 Timestamp localDead = removedItems.get(key);
705 - if (local != null 705 + if (local != null && remoteDeadTimestamp.isNewerThan(
706 - && remoteDeadTimestamp.compareTo(local.timestamp()) > 0) { 706 + local.timestamp())) {
707 - // remove our version 707 + // If the remote has a more recent tombstone than either our local
708 + // value, then do a remove with their timestamp
708 if (removeInternal(key, remoteDeadTimestamp)) { 709 if (removeInternal(key, remoteDeadTimestamp)) {
709 externalEvents.add(new EventuallyConsistentMapEvent<>( 710 externalEvents.add(new EventuallyConsistentMapEvent<>(
710 EventuallyConsistentMapEvent.Type.REMOVE, key, null)); 711 EventuallyConsistentMapEvent.Type.REMOVE, key, null));
711 } 712 }
712 - } else if (localDead != null && 713 + } else if (localDead != null && remoteDeadTimestamp.isNewerThan(
713 - remoteDeadTimestamp.compareTo(localDead) > 0) { 714 + localDead)) {
714 - // If we both had the item as removed, but their timestamp is 715 + // If the remote has a more recent tombstone than us, update ours
715 - // newer, update ours to the newer value 716 + // to their timestamp
716 - removedItems.put(key, remoteDeadTimestamp); 717 + removeInternal(key, remoteDeadTimestamp);
717 } 718 }
718 } 719 }
719 720
......
...@@ -235,7 +235,7 @@ public class GossipHostStore ...@@ -235,7 +235,7 @@ public class GossipHostStore
235 private boolean isHostRemoved(HostId hostId, Timestamp timestamp) { 235 private boolean isHostRemoved(HostId hostId, Timestamp timestamp) {
236 Timestamped<Host> removedInfo = removedHosts.get(hostId); 236 Timestamped<Host> removedInfo = removedHosts.get(hostId);
237 if (removedInfo != null) { 237 if (removedInfo != null) {
238 - if (removedInfo.isNewer(timestamp)) { 238 + if (removedInfo.isNewerThan(timestamp)) {
239 return true; 239 return true;
240 } 240 }
241 removedHosts.remove(hostId, removedInfo); 241 removedHosts.remove(hostId, removedInfo);
......
...@@ -15,13 +15,12 @@ ...@@ -15,13 +15,12 @@
15 */ 15 */
16 package org.onosproject.store.impl; 16 package org.onosproject.store.impl;
17 17
18 -import static com.google.common.base.Preconditions.checkNotNull; 18 +import com.google.common.base.MoreObjects;
19 +import org.onosproject.store.Timestamp;
19 20
20 import java.util.Objects; 21 import java.util.Objects;
21 22
22 -import org.onosproject.store.Timestamp; 23 +import static com.google.common.base.Preconditions.checkNotNull;
23 -
24 -import com.google.common.base.MoreObjects;
25 24
26 /** 25 /**
27 * Wrapper class to store Timestamped value. 26 * Wrapper class to store Timestamped value.
...@@ -69,17 +68,17 @@ public final class Timestamped<T> { ...@@ -69,17 +68,17 @@ public final class Timestamped<T> {
69 * @return true if this instance is newer. 68 * @return true if this instance is newer.
70 */ 69 */
71 public boolean isNewer(Timestamped<T> other) { 70 public boolean isNewer(Timestamped<T> other) {
72 - return isNewer(checkNotNull(other).timestamp()); 71 + return isNewerThan(checkNotNull(other).timestamp());
73 } 72 }
74 73
75 /** 74 /**
76 * Tests if this timestamp is newer than the specified timestamp. 75 * Tests if this timestamp is newer than the specified timestamp.
76 + *
77 * @param other timestamp to compare against 77 * @param other timestamp to compare against
78 * @return true if this instance is newer 78 * @return true if this instance is newer
79 */ 79 */
80 - //TODO put this in Timestamp 80 + public boolean isNewerThan(Timestamp other) {
81 - public boolean isNewer(Timestamp other) { 81 + return timestamp.isNewerThan(other);
82 - return this.timestamp.compareTo(checkNotNull(other)) > 0;
83 } 82 }
84 83
85 @Override 84 @Override
......
...@@ -360,7 +360,7 @@ public class GossipLinkStore ...@@ -360,7 +360,7 @@ public class GossipLinkStore
360 // only if this request is more recent. 360 // only if this request is more recent.
361 Timestamp linkRemovedTimestamp = removedLinks.get(key); 361 Timestamp linkRemovedTimestamp = removedLinks.get(key);
362 if (linkRemovedTimestamp != null) { 362 if (linkRemovedTimestamp != null) {
363 - if (linkDescription.isNewer(linkRemovedTimestamp)) { 363 + if (linkDescription.isNewerThan(linkRemovedTimestamp)) {
364 removedLinks.remove(key); 364 removedLinks.remove(key);
365 } else { 365 } else {
366 log.trace("Link {} was already removed ignoring.", key); 366 log.trace("Link {} was already removed ignoring.", key);
...@@ -762,7 +762,7 @@ public class GossipLinkStore ...@@ -762,7 +762,7 @@ public class GossipLinkStore
762 remoteTimestamp = ad.linkTombstones().get(key); 762 remoteTimestamp = ad.linkTombstones().get(key);
763 } 763 }
764 if (remoteTimestamp == null || 764 if (remoteTimestamp == null ||
765 - pDesc.isNewer(remoteTimestamp)) { 765 + pDesc.isNewerThan(remoteTimestamp)) {
766 // I have more recent link description. update peer. 766 // I have more recent link description. update peer.
767 notifyPeer(sender, new InternalLinkEvent(providerId, pDesc)); 767 notifyPeer(sender, new InternalLinkEvent(providerId, pDesc));
768 } else { 768 } else {
...@@ -776,7 +776,7 @@ public class GossipLinkStore ...@@ -776,7 +776,7 @@ public class GossipLinkStore
776 776
777 // search local latest along the way 777 // search local latest along the way
778 if (localLatest == null || 778 if (localLatest == null ||
779 - pDesc.isNewer(localLatest)) { 779 + pDesc.isNewerThan(localLatest)) {
780 localLatest = pDesc.timestamp(); 780 localLatest = pDesc.timestamp();
781 } 781 }
782 } 782 }
......