Brian O'Connor
Committed by Gerrit Code Review

Perform check before updating host in DistributedHostStore

By checking to see if the host needs to be updated, we can
avoid invalidating the local cache in many cases.

This also helps to alleviate slow pingall times with fwd
enabled.

Change-Id: I7d58a488cd91878e6028ddfaae407e849fc10211
...@@ -113,6 +113,42 @@ public class DistributedHostStore ...@@ -113,6 +113,42 @@ public class DistributedHostStore
113 log.info("Stopped"); 113 log.info("Stopped");
114 } 114 }
115 115
116 + private boolean shouldUpdate(DefaultHost existingHost,
117 + ProviderId providerId,
118 + HostId hostId,
119 + HostDescription hostDescription,
120 + boolean replaceIPs) {
121 + if (existingHost == null) {
122 + return true;
123 + }
124 +
125 + if (!Objects.equals(existingHost.providerId(), providerId) ||
126 + !Objects.equals(existingHost.mac(), hostDescription.hwAddress()) ||
127 + !Objects.equals(existingHost.vlan(), hostDescription.vlan()) ||
128 + !Objects.equals(existingHost.location(), hostDescription.location())) {
129 + return true;
130 + }
131 +
132 + if (replaceIPs) {
133 + if (!Objects.equals(hostDescription.ipAddress(),
134 + existingHost.ipAddresses())) {
135 + return true;
136 + }
137 + } else {
138 + if (!existingHost.ipAddresses().containsAll(hostDescription.ipAddress())) {
139 + return true;
140 + }
141 + }
142 +
143 + // check to see if any of the annotations provided by hostDescription
144 + // differ from those in the existing host
145 + return hostDescription.annotations().keys().stream()
146 + .anyMatch(k -> !Objects.equals(hostDescription.annotations().value(k),
147 + existingHost.annotations().value(k)));
148 +
149 +
150 + }
151 +
116 // TODO No longer need to return HostEvent 152 // TODO No longer need to return HostEvent
117 @Override 153 @Override
118 public HostEvent createOrUpdateHost(ProviderId providerId, 154 public HostEvent createOrUpdateHost(ProviderId providerId,
...@@ -120,9 +156,10 @@ public class DistributedHostStore ...@@ -120,9 +156,10 @@ public class DistributedHostStore
120 HostDescription hostDescription, 156 HostDescription hostDescription,
121 boolean replaceIPs) { 157 boolean replaceIPs) {
122 // TODO: We need a way to detect conflicting changes and abort update. 158 // TODO: We need a way to detect conflicting changes and abort update.
123 - // (BOC) Compute might do this for us. 159 + host.computeIf(hostId,
124 - 160 + existingHost -> shouldUpdate(existingHost, providerId, hostId,
125 - hosts.compute(hostId, (id, existingHost) -> { 161 + hostDescription, replaceIPs),
162 + (id, existingHost) -> {
126 HostLocation location = hostDescription.location(); 163 HostLocation location = hostDescription.location();
127 164
128 final Set<IpAddress> addresses; 165 final Set<IpAddress> addresses;
......