blob: ebf681a29e71cab3e4dbb69e67c5ee0867f1be65 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent.impl;
tom95329eb2014-10-06 08:40:06 -070017
Brian O'Connor86f6f7f2014-12-01 17:02:45 -080018import com.google.common.collect.HashMultimap;
Brian O'Connora9c18b92015-09-28 16:03:43 -070019import com.google.common.collect.ImmutableSet;
Brian O'Connor86f6f7f2014-12-01 17:02:45 -080020import com.google.common.collect.Lists;
21import com.google.common.collect.SetMultimap;
tom95329eb2014-10-06 08:40:06 -070022import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
Brian O'Connor69d6ac72015-05-29 16:24:06 -070027import org.apache.felix.scr.annotations.ReferencePolicy;
tom95329eb2014-10-06 08:40:06 -070028import org.apache.felix.scr.annotations.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.core.ApplicationId;
30import org.onosproject.event.Event;
Brian O'Connorb5dcc512015-03-24 17:28:00 -070031import org.onosproject.net.DeviceId;
32import org.onosproject.net.ElementId;
33import org.onosproject.net.HostId;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.Link;
35import org.onosproject.net.LinkKey;
36import org.onosproject.net.NetworkResource;
Brian O'Connorb5dcc512015-03-24 17:28:00 -070037import org.onosproject.net.device.DeviceEvent;
38import org.onosproject.net.device.DeviceListener;
39import org.onosproject.net.device.DeviceService;
40import org.onosproject.net.host.HostEvent;
41import org.onosproject.net.host.HostListener;
42import org.onosproject.net.host.HostService;
Brian O'Connor69d6ac72015-05-29 16:24:06 -070043import org.onosproject.net.intent.Intent;
Thomas Vachuskac46af202015-06-03 16:43:27 -070044import org.onosproject.net.intent.IntentData;
Brian O'Connorabafb502014-12-02 22:26:20 -080045import org.onosproject.net.intent.IntentService;
Ray Milkeyf9af43c2015-02-09 16:45:48 -080046import org.onosproject.net.intent.Key;
Brian O'Connor69d6ac72015-05-29 16:24:06 -070047import org.onosproject.net.intent.PartitionEvent;
48import org.onosproject.net.intent.PartitionEventListener;
49import org.onosproject.net.intent.PartitionService;
Brian O'Connorabafb502014-12-02 22:26:20 -080050import org.onosproject.net.link.LinkEvent;
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -080051import org.onosproject.net.newresource.ResourceEvent;
52import org.onosproject.net.newresource.ResourceListener;
53import org.onosproject.net.newresource.ResourceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080054import org.onosproject.net.topology.TopologyEvent;
55import org.onosproject.net.topology.TopologyListener;
56import org.onosproject.net.topology.TopologyService;
tom95329eb2014-10-06 08:40:06 -070057import org.slf4j.Logger;
58
Brian O'Connor86f6f7f2014-12-01 17:02:45 -080059import java.util.Collection;
Brian O'Connorb5dcc512015-03-24 17:28:00 -070060import java.util.Collections;
Brian O'Connor86f6f7f2014-12-01 17:02:45 -080061import java.util.HashSet;
Thomas Vachuskac46af202015-06-03 16:43:27 -070062import java.util.List;
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -080063import java.util.Optional;
Brian O'Connor86f6f7f2014-12-01 17:02:45 -080064import java.util.Set;
65import java.util.concurrent.ExecutorService;
Brian O'Connor69d6ac72015-05-29 16:24:06 -070066import java.util.concurrent.Executors;
67import java.util.concurrent.ScheduledExecutorService;
68import java.util.concurrent.TimeUnit;
69import java.util.concurrent.atomic.AtomicBoolean;
tom95329eb2014-10-06 08:40:06 -070070
71import static com.google.common.base.Preconditions.checkArgument;
72import static com.google.common.base.Preconditions.checkNotNull;
73import static com.google.common.collect.Multimaps.synchronizedSetMultimap;
74import static java.util.concurrent.Executors.newSingleThreadExecutor;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080075import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuskac46af202015-06-03 16:43:27 -070076import static org.onlab.util.Tools.isNullOrEmpty;
Brian O'Connorabafb502014-12-02 22:26:20 -080077import static org.onosproject.net.LinkKey.linkKey;
Thomas Vachuskac46af202015-06-03 16:43:27 -070078import static org.onosproject.net.intent.IntentState.INSTALLED;
Brian O'Connor690fd1c2015-06-04 19:50:33 -070079import static org.onosproject.net.intent.IntentState.INSTALLING;
Brian O'Connorabafb502014-12-02 22:26:20 -080080import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED;
81import static org.onosproject.net.link.LinkEvent.Type.LINK_UPDATED;
tom95329eb2014-10-06 08:40:06 -070082import static org.slf4j.LoggerFactory.getLogger;
83
84/**
85 * Entity responsible for tracking installed flows and for monitoring topology
86 * events to determine what flows are affected by topology changes.
87 */
Ray Milkeye97ede92014-11-20 10:43:12 -080088@Component(immediate = true)
tom95329eb2014-10-06 08:40:06 -070089@Service
tom85258ee2014-10-07 00:10:02 -070090public class ObjectiveTracker implements ObjectiveTrackerService {
tom95329eb2014-10-06 08:40:06 -070091
92 private final Logger log = getLogger(getClass());
93
Ray Milkeyf9af43c2015-02-09 16:45:48 -080094 private final SetMultimap<LinkKey, Key> intentsByLink =
Brian O'Connor64a0369d2015-02-20 22:02:59 -080095 //TODO this could be slow as a point of synchronization
Ray Milkeyf9af43c2015-02-09 16:45:48 -080096 synchronizedSetMultimap(HashMultimap.<LinkKey, Key>create());
tom95329eb2014-10-06 08:40:06 -070097
Brian O'Connorb5dcc512015-03-24 17:28:00 -070098 private final SetMultimap<ElementId, Key> intentsByDevice =
99 synchronizedSetMultimap(HashMultimap.<ElementId, Key>create());
100
tom95329eb2014-10-06 08:40:06 -0700101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
102 protected TopologyService topologyService;
103
Ray Milkeye97ede92014-11-20 10:43:12 -0800104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -0800105 protected ResourceService resourceService;
Ray Milkeye97ede92014-11-20 10:43:12 -0800106
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700107 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
108 protected DeviceService deviceService;
109
110 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
111 protected HostService hostService;
112
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700113 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY,
114 policy = ReferencePolicy.DYNAMIC)
Brian O'Connor86f6f7f2014-12-01 17:02:45 -0800115 protected IntentService intentService;
116
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
118 protected PartitionService partitionService;
119
tom95329eb2014-10-06 08:40:06 -0700120 private ExecutorService executorService =
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700121 newSingleThreadExecutor(groupedThreads("onos/intent", "objectivetracker"));
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700122 private ScheduledExecutorService executor = Executors
123 .newScheduledThreadPool(1);
tom95329eb2014-10-06 08:40:06 -0700124
125 private TopologyListener listener = new InternalTopologyListener();
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -0800126 private ResourceListener resourceListener = new InternalResourceListener();
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700127 private DeviceListener deviceListener = new InternalDeviceListener();
128 private HostListener hostListener = new InternalHostListener();
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700129 private PartitionEventListener partitionListener = new InternalPartitionListener();
tom95329eb2014-10-06 08:40:06 -0700130 private TopologyChangeDelegate delegate;
131
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700132 protected final AtomicBoolean updateScheduled = new AtomicBoolean(false);
133
tom95329eb2014-10-06 08:40:06 -0700134 @Activate
135 public void activate() {
136 topologyService.addListener(listener);
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -0800137 resourceService.addListener(resourceListener);
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700138 deviceService.addListener(deviceListener);
139 hostService.addListener(hostListener);
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700140 partitionService.addListener(partitionListener);
tom95329eb2014-10-06 08:40:06 -0700141 log.info("Started");
142 }
143
144 @Deactivate
145 public void deactivate() {
146 topologyService.removeListener(listener);
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -0800147 resourceService.removeListener(resourceListener);
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700148 deviceService.removeListener(deviceListener);
149 hostService.removeListener(hostListener);
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700150 partitionService.removeListener(partitionListener);
tom95329eb2014-10-06 08:40:06 -0700151 log.info("Stopped");
152 }
153
Brian O'Connor5d55ed42014-12-01 18:27:47 -0800154 protected void bindIntentService(IntentService service) {
155 if (intentService == null) {
156 intentService = service;
157 }
158 }
159
160 protected void unbindIntentService(IntentService service) {
161 if (intentService == service) {
162 intentService = null;
163 }
164 }
165
tom95329eb2014-10-06 08:40:06 -0700166 @Override
167 public void setDelegate(TopologyChangeDelegate delegate) {
168 checkNotNull(delegate, "Delegate cannot be null");
169 checkArgument(this.delegate == null || this.delegate == delegate,
170 "Another delegate already set");
171 this.delegate = delegate;
172 }
173
174 @Override
175 public void unsetDelegate(TopologyChangeDelegate delegate) {
176 checkArgument(this.delegate == delegate, "Not the current delegate");
177 this.delegate = null;
178 }
179
180 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800181 public void addTrackedResources(Key intentKey,
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700182 Collection<NetworkResource> resources) {
183 for (NetworkResource resource : resources) {
184 if (resource instanceof Link) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800185 intentsByLink.put(linkKey((Link) resource), intentKey);
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700186 } else if (resource instanceof ElementId) {
187 intentsByDevice.put((ElementId) resource, intentKey);
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700188 }
tom95329eb2014-10-06 08:40:06 -0700189 }
190 }
191
192 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800193 public void removeTrackedResources(Key intentKey,
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700194 Collection<NetworkResource> resources) {
195 for (NetworkResource resource : resources) {
196 if (resource instanceof Link) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800197 intentsByLink.remove(linkKey((Link) resource), intentKey);
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700198 } else if (resource instanceof ElementId) {
Sho SHIMIZUd82a4e62015-09-09 14:53:46 -0700199 intentsByDevice.remove(resource, intentKey);
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700200 }
tom95329eb2014-10-06 08:40:06 -0700201 }
202 }
203
Thomas Vachuskac46af202015-06-03 16:43:27 -0700204 @Override
205 public void trackIntent(IntentData intentData) {
206
207 //NOTE: This will be called for intents that are being added to the store
208 // locally (i.e. every intent update)
209
210 Key key = intentData.key();
211 Intent intent = intentData.intent();
212 boolean isLocal = intentService.isLocal(key);
Brian O'Connor690fd1c2015-06-04 19:50:33 -0700213 boolean isInstalled = intentData.state() == INSTALLING ||
214 intentData.state() == INSTALLED;
Thomas Vachuskac46af202015-06-03 16:43:27 -0700215 List<Intent> installables = intentData.installables();
216
217 if (log.isTraceEnabled()) {
218 log.trace("intent {}, old: {}, new: {}, installableCount: {}, resourceCount: {}",
219 key,
220 intentsByDevice.values().contains(key),
Brian O'Connor690fd1c2015-06-04 19:50:33 -0700221 isLocal && isInstalled,
Thomas Vachuskac46af202015-06-03 16:43:27 -0700222 installables.size(),
223 intent.resources().size() +
224 installables.stream()
225 .mapToLong(i -> i.resources().size()).sum());
226 }
227
228 if (isNullOrEmpty(installables) && intentData.state() == INSTALLED) {
229 log.warn("Intent {} is INSTALLED with no installables", key);
230 }
231
Brian O'Connor690fd1c2015-06-04 19:50:33 -0700232 // FIXME Intents will be added 3 times (once directly using addTracked,
233 // then when installing and when installed)
234 if (isLocal && isInstalled) {
Thomas Vachuskac46af202015-06-03 16:43:27 -0700235 addTrackedResources(key, intent.resources());
236 for (Intent installable : installables) {
237 addTrackedResources(key, installable.resources());
238 }
239 // FIXME check all resources against current topo service(s); recompile if necessary
240 } else {
241 removeTrackedResources(key, intent.resources());
242 for (Intent installable : installables) {
243 removeTrackedResources(key, installable.resources());
244 }
245 }
246 }
247
tom95329eb2014-10-06 08:40:06 -0700248 // Internal re-actor to topology change events.
249 private class InternalTopologyListener implements TopologyListener {
250 @Override
251 public void event(TopologyEvent event) {
252 executorService.execute(new TopologyChangeHandler(event));
253 }
254 }
255
256 // Re-dispatcher of topology change events.
257 private class TopologyChangeHandler implements Runnable {
258
259 private final TopologyEvent event;
260
261 TopologyChangeHandler(TopologyEvent event) {
262 this.event = event;
263 }
264
265 @Override
266 public void run() {
Thomas Vachuska7b652ad2014-10-30 14:10:51 -0700267 // If there is no delegate, why bother? Just bail.
268 if (delegate == null) {
269 return;
270 }
271
Ray Milkey7c44c052014-12-05 10:34:54 -0800272 if (event.reasons() == null || event.reasons().isEmpty()) {
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700273 delegate.triggerCompile(Collections.emptySet(), true);
tom85258ee2014-10-07 00:10:02 -0700274
tom95329eb2014-10-06 08:40:06 -0700275 } else {
Jonathan Hart96c5a4a2015-07-31 14:23:33 -0700276 Set<Key> intentsToRecompile = new HashSet<>();
277 boolean dontRecompileAllFailedIntents = true;
tom85258ee2014-10-07 00:10:02 -0700278
279 // Scan through the list of reasons and keep accruing all
280 // intents that need to be recompiled.
tom95329eb2014-10-06 08:40:06 -0700281 for (Event reason : event.reasons()) {
282 if (reason instanceof LinkEvent) {
283 LinkEvent linkEvent = (LinkEvent) reason;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -0700284 final LinkKey linkKey = linkKey(linkEvent.subject());
285 synchronized (intentsByLink) {
286 Set<Key> intentKeys = intentsByLink.get(linkKey);
287 log.debug("recompile triggered by LinkEvent {} ({}) for {}",
288 linkKey, linkEvent.type(), intentKeys);
289 intentsToRecompile.addAll(intentKeys);
tom85258ee2014-10-07 00:10:02 -0700290 }
Jonathan Hart96c5a4a2015-07-31 14:23:33 -0700291 dontRecompileAllFailedIntents = dontRecompileAllFailedIntents &&
Praseed Balakrishnan00dd1f92014-11-19 17:12:36 -0800292 (linkEvent.type() == LINK_REMOVED ||
293 (linkEvent.type() == LINK_UPDATED &&
294 linkEvent.subject().isDurable()));
tom95329eb2014-10-06 08:40:06 -0700295 }
296 }
Jonathan Hart96c5a4a2015-07-31 14:23:33 -0700297 delegate.triggerCompile(intentsToRecompile, !dontRecompileAllFailedIntents);
tom95329eb2014-10-06 08:40:06 -0700298 }
299 }
300 }
301
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -0800302 private class InternalResourceListener implements ResourceListener {
Ray Milkeye97ede92014-11-20 10:43:12 -0800303 @Override
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -0800304 public void event(ResourceEvent event) {
305 Optional<Class<?>> linkEvent = event.subject().components().stream()
306 .map(Object::getClass)
307 .filter(x -> x == LinkKey.class)
308 .findFirst();
309 if (linkEvent.isPresent()) {
310 executorService.execute(() -> {
311 if (delegate == null) {
312 return;
313 }
Ray Milkeye97ede92014-11-20 10:43:12 -0800314
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -0800315 delegate.triggerCompile(Collections.emptySet(), true);
316 });
Ray Milkeye97ede92014-11-20 10:43:12 -0800317 }
Ray Milkeye97ede92014-11-20 10:43:12 -0800318 }
319 }
320
Brian O'Connor72a034c2014-11-26 18:24:23 -0800321 //TODO consider adding flow rule event tracking
Ray Milkeye97ede92014-11-20 10:43:12 -0800322
Brian O'Connor86f6f7f2014-12-01 17:02:45 -0800323 private void updateTrackedResources(ApplicationId appId, boolean track) {
Brian O'Connor5d55ed42014-12-01 18:27:47 -0800324 if (intentService == null) {
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700325 log.warn("Intent service is not bound yet");
Brian O'Connor5d55ed42014-12-01 18:27:47 -0800326 return;
327 }
Brian O'Connor86f6f7f2014-12-01 17:02:45 -0800328 intentService.getIntents().forEach(intent -> {
329 if (intent.appId().equals(appId)) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800330 Key key = intent.key();
Brian O'Connor86f6f7f2014-12-01 17:02:45 -0800331 Collection<NetworkResource> resources = Lists.newArrayList();
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800332 intentService.getInstallableIntents(key).stream()
Brian O'Connor86f6f7f2014-12-01 17:02:45 -0800333 .map(installable -> installable.resources())
334 .forEach(resources::addAll);
335 if (track) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800336 addTrackedResources(key, resources);
Brian O'Connor86f6f7f2014-12-01 17:02:45 -0800337 } else {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800338 removeTrackedResources(key, resources);
Brian O'Connor86f6f7f2014-12-01 17:02:45 -0800339 }
340 }
341 });
342 }
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700343
344 /*
345 * Re-dispatcher of device and host events.
346 */
347 private class DeviceAvailabilityHandler implements Runnable {
348
349 private final ElementId id;
350 private final boolean available;
351
352 DeviceAvailabilityHandler(ElementId id, boolean available) {
353 this.id = checkNotNull(id);
354 this.available = available;
355 }
356
357 @Override
358 public void run() {
359 // If there is no delegate, why bother? Just bail.
360 if (delegate == null) {
361 return;
362 }
363
364 // TODO should we recompile on available==true?
HIGUCHI Yuta99b7b342015-09-29 16:54:21 -0700365
366 final ImmutableSet<Key> snapshot;
367 synchronized (intentsByDevice) {
368 snapshot = ImmutableSet.copyOf(intentsByDevice.get(id));
369 }
370 delegate.triggerCompile(snapshot, available);
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700371 }
372 }
373
374
375 private class InternalDeviceListener implements DeviceListener {
376 @Override
377 public void event(DeviceEvent event) {
378 DeviceEvent.Type type = event.type();
Jonathan Hart94470fe2015-07-31 11:41:10 -0700379 switch (type) {
380 case DEVICE_ADDED:
381 case DEVICE_AVAILABILITY_CHANGED:
382 case DEVICE_REMOVED:
383 case DEVICE_SUSPENDED:
384 case DEVICE_UPDATED:
385 DeviceId id = event.subject().id();
386 // TODO we need to check whether AVAILABILITY_CHANGED means up or down
387 boolean available = (type == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
388 type == DeviceEvent.Type.DEVICE_ADDED ||
389 type == DeviceEvent.Type.DEVICE_UPDATED);
390 executorService.execute(new DeviceAvailabilityHandler(id, available));
391 break;
392 case PORT_ADDED:
393 case PORT_REMOVED:
394 case PORT_UPDATED:
395 case PORT_STATS_UPDATED:
396 default:
397 // Don't handle port events for now
398 break;
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700399 }
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700400 }
401 }
402
403 private class InternalHostListener implements HostListener {
404 @Override
405 public void event(HostEvent event) {
406 HostId id = event.subject().id();
Brian O'Connora9c18b92015-09-28 16:03:43 -0700407 switch (event.type()) {
408 case HOST_ADDED:
409 case HOST_MOVED:
410 case HOST_REMOVED:
411 executorService.execute(new DeviceAvailabilityHandler(id, false));
412 break;
413 case HOST_UPDATED:
414 default:
415 // DO NOTHING
416 break;
417 }
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700418 }
419 }
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700420
421 protected void doIntentUpdate() {
422 updateScheduled.set(false);
423 if (intentService == null) {
424 log.warn("Intent service is not bound yet");
425 return;
426 }
427 try {
428 //FIXME very inefficient
Thomas Vachuskac46af202015-06-03 16:43:27 -0700429 for (IntentData intentData : intentService.getIntentData()) {
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700430 try {
Thomas Vachuskac46af202015-06-03 16:43:27 -0700431 trackIntent(intentData);
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700432 } catch (NullPointerException npe) {
Thomas Vachuskac46af202015-06-03 16:43:27 -0700433 log.warn("intent error {}", intentData.key(), npe);
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700434 }
435 }
436 } catch (Exception e) {
437 log.warn("Exception caught during update task", e);
438 }
439 }
440
441 private void scheduleIntentUpdate(int afterDelaySec) {
442 if (updateScheduled.compareAndSet(false, true)) {
443 executor.schedule(this::doIntentUpdate, afterDelaySec, TimeUnit.SECONDS);
444 }
445 }
446
447 private final class InternalPartitionListener implements PartitionEventListener {
448 @Override
449 public void event(PartitionEvent event) {
Brian O'Connor3057f212015-05-29 18:22:18 -0700450 log.debug("got message {}", event.subject());
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700451 scheduleIntentUpdate(1);
452 }
453 }
tom95329eb2014-10-06 08:40:06 -0700454}