blob: f423ebc513277626b35e872434d86c21de81df67 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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.SetMultimap;
tom95329eb2014-10-06 08:40:06 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Brian O'Connor69d6ac72015-05-29 16:24:06 -070026import org.apache.felix.scr.annotations.ReferencePolicy;
tom95329eb2014-10-06 08:40:06 -070027import org.apache.felix.scr.annotations.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.event.Event;
Brian O'Connorb5dcc512015-03-24 17:28:00 -070029import org.onosproject.net.DeviceId;
30import org.onosproject.net.ElementId;
31import org.onosproject.net.HostId;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.Link;
33import org.onosproject.net.LinkKey;
34import org.onosproject.net.NetworkResource;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080035import org.onosproject.net.PortNumber;
Brian O'Connorb5dcc512015-03-24 17:28:00 -070036import org.onosproject.net.device.DeviceEvent;
37import org.onosproject.net.device.DeviceListener;
38import org.onosproject.net.device.DeviceService;
39import org.onosproject.net.host.HostEvent;
40import org.onosproject.net.host.HostListener;
41import org.onosproject.net.host.HostService;
Brian O'Connor69d6ac72015-05-29 16:24:06 -070042import org.onosproject.net.intent.Intent;
Thomas Vachuskac46af202015-06-03 16:43:27 -070043import org.onosproject.net.intent.IntentData;
Brian O'Connorabafb502014-12-02 22:26:20 -080044import org.onosproject.net.intent.IntentService;
Ray Milkeyf9af43c2015-02-09 16:45:48 -080045import org.onosproject.net.intent.Key;
Yi Tseng24d9be72017-05-12 11:28:13 -070046import org.onosproject.net.intent.ObjectiveTrackerService;
47import org.onosproject.net.intent.TopologyChangeDelegate;
Madan Jampani3b8101a2016-09-15 13:22:01 -070048import org.onosproject.net.intent.WorkPartitionEvent;
49import org.onosproject.net.intent.WorkPartitionEventListener;
50import org.onosproject.net.intent.WorkPartitionService;
Brian O'Connorabafb502014-12-02 22:26:20 -080051import org.onosproject.net.link.LinkEvent;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080052import org.onosproject.net.resource.ResourceEvent;
53import org.onosproject.net.resource.ResourceListener;
54import org.onosproject.net.resource.ResourceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080055import org.onosproject.net.topology.TopologyEvent;
56import org.onosproject.net.topology.TopologyListener;
57import org.onosproject.net.topology.TopologyService;
tom95329eb2014-10-06 08:40:06 -070058import org.slf4j.Logger;
59
Brian O'Connor86f6f7f2014-12-01 17:02:45 -080060import java.util.Collection;
Brian O'Connorb5dcc512015-03-24 17:28:00 -070061import java.util.Collections;
Brian O'Connor86f6f7f2014-12-01 17:02:45 -080062import java.util.HashSet;
Thomas Vachuskac46af202015-06-03 16:43:27 -070063import java.util.List;
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.ScheduledExecutorService;
67import java.util.concurrent.TimeUnit;
68import java.util.concurrent.atomic.AtomicBoolean;
tom95329eb2014-10-06 08:40:06 -070069
70import static com.google.common.base.Preconditions.checkArgument;
71import static com.google.common.base.Preconditions.checkNotNull;
72import static com.google.common.collect.Multimaps.synchronizedSetMultimap;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -070073import static java.util.concurrent.Executors.newScheduledThreadPool;
tom95329eb2014-10-06 08:40:06 -070074import 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,
jaegonkimdc8a5562018-04-05 23:14:57 +0900114 bind = "bindComponentConfigService",
115 unbind = "unbindComponentConfigService",
116 policy = ReferencePolicy.DYNAMIC)
Brian O'Connor86f6f7f2014-12-01 17:02:45 -0800117 protected IntentService intentService;
118
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700119 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Madan Jampani3b8101a2016-09-15 13:22:01 -0700120 protected WorkPartitionService partitionService;
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700121
tom95329eb2014-10-06 08:40:06 -0700122 private ExecutorService executorService =
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700123 newSingleThreadExecutor(groupedThreads("onos/intent", "objectivetracker", log));
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700124 private ScheduledExecutorService executor =
125 newScheduledThreadPool(1, groupedThreads("onos/intent", "scheduledIntentUpdate", log));
tom95329eb2014-10-06 08:40:06 -0700126
127 private TopologyListener listener = new InternalTopologyListener();
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -0800128 private ResourceListener resourceListener = new InternalResourceListener();
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700129 private DeviceListener deviceListener = new InternalDeviceListener();
130 private HostListener hostListener = new InternalHostListener();
Madan Jampani3b8101a2016-09-15 13:22:01 -0700131 private WorkPartitionEventListener partitionListener = new InternalPartitionListener();
tom95329eb2014-10-06 08:40:06 -0700132 private TopologyChangeDelegate delegate;
133
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700134 protected final AtomicBoolean updateScheduled = new AtomicBoolean(false);
135
jaegonkimdc8a5562018-04-05 23:14:57 +0900136 /**
137 * Hook for wiring up optional reference to a service.
138 *
139 * @param service service being announced
140 */
141 protected void bindComponentConfigService(IntentService service) {
142 if (intentService == null) {
143 intentService = service;
144 scheduleIntentUpdate(1);
145 }
146 }
147
148 /**
149 * Hook for unwiring optional reference to a service.
150 *
151 * @param service service being withdrawn
152 */
153 protected void unbindComponentConfigService(IntentService service) {
154 if (intentService == service) {
155 intentService = null;
156 }
157 }
158
tom95329eb2014-10-06 08:40:06 -0700159 @Activate
160 public void activate() {
161 topologyService.addListener(listener);
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -0800162 resourceService.addListener(resourceListener);
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700163 deviceService.addListener(deviceListener);
164 hostService.addListener(hostListener);
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700165 partitionService.addListener(partitionListener);
jaegonkimdc8a5562018-04-05 23:14:57 +0900166 scheduleIntentUpdate(1);
tom95329eb2014-10-06 08:40:06 -0700167 log.info("Started");
168 }
169
170 @Deactivate
171 public void deactivate() {
172 topologyService.removeListener(listener);
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -0800173 resourceService.removeListener(resourceListener);
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700174 deviceService.removeListener(deviceListener);
175 hostService.removeListener(hostListener);
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700176 partitionService.removeListener(partitionListener);
tom95329eb2014-10-06 08:40:06 -0700177 log.info("Stopped");
178 }
179
Brian O'Connor5d55ed42014-12-01 18:27:47 -0800180 protected void bindIntentService(IntentService service) {
181 if (intentService == null) {
182 intentService = service;
183 }
184 }
185
186 protected void unbindIntentService(IntentService service) {
187 if (intentService == service) {
188 intentService = null;
189 }
190 }
191
tom95329eb2014-10-06 08:40:06 -0700192 @Override
193 public void setDelegate(TopologyChangeDelegate delegate) {
194 checkNotNull(delegate, "Delegate cannot be null");
195 checkArgument(this.delegate == null || this.delegate == delegate,
196 "Another delegate already set");
197 this.delegate = delegate;
198 }
199
200 @Override
201 public void unsetDelegate(TopologyChangeDelegate delegate) {
202 checkArgument(this.delegate == delegate, "Not the current delegate");
203 this.delegate = null;
204 }
205
206 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800207 public void addTrackedResources(Key intentKey,
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700208 Collection<NetworkResource> resources) {
209 for (NetworkResource resource : resources) {
210 if (resource instanceof Link) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800211 intentsByLink.put(linkKey((Link) resource), intentKey);
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700212 } else if (resource instanceof ElementId) {
213 intentsByDevice.put((ElementId) resource, intentKey);
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700214 }
tom95329eb2014-10-06 08:40:06 -0700215 }
216 }
217
218 @Override
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800219 public void removeTrackedResources(Key intentKey,
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700220 Collection<NetworkResource> resources) {
221 for (NetworkResource resource : resources) {
222 if (resource instanceof Link) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800223 intentsByLink.remove(linkKey((Link) resource), intentKey);
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700224 } else if (resource instanceof ElementId) {
Sho SHIMIZUd82a4e62015-09-09 14:53:46 -0700225 intentsByDevice.remove(resource, intentKey);
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700226 }
tom95329eb2014-10-06 08:40:06 -0700227 }
228 }
229
Thomas Vachuskac46af202015-06-03 16:43:27 -0700230 @Override
231 public void trackIntent(IntentData intentData) {
232
233 //NOTE: This will be called for intents that are being added to the store
234 // locally (i.e. every intent update)
235
236 Key key = intentData.key();
237 Intent intent = intentData.intent();
238 boolean isLocal = intentService.isLocal(key);
Brian O'Connor690fd1c2015-06-04 19:50:33 -0700239 boolean isInstalled = intentData.state() == INSTALLING ||
240 intentData.state() == INSTALLED;
Thomas Vachuskac46af202015-06-03 16:43:27 -0700241 List<Intent> installables = intentData.installables();
242
243 if (log.isTraceEnabled()) {
244 log.trace("intent {}, old: {}, new: {}, installableCount: {}, resourceCount: {}",
245 key,
246 intentsByDevice.values().contains(key),
Brian O'Connor690fd1c2015-06-04 19:50:33 -0700247 isLocal && isInstalled,
Thomas Vachuskac46af202015-06-03 16:43:27 -0700248 installables.size(),
249 intent.resources().size() +
250 installables.stream()
251 .mapToLong(i -> i.resources().size()).sum());
252 }
253
254 if (isNullOrEmpty(installables) && intentData.state() == INSTALLED) {
255 log.warn("Intent {} is INSTALLED with no installables", key);
256 }
257
Brian O'Connor690fd1c2015-06-04 19:50:33 -0700258 // FIXME Intents will be added 3 times (once directly using addTracked,
259 // then when installing and when installed)
260 if (isLocal && isInstalled) {
Thomas Vachuskac46af202015-06-03 16:43:27 -0700261 addTrackedResources(key, intent.resources());
262 for (Intent installable : installables) {
263 addTrackedResources(key, installable.resources());
264 }
265 // FIXME check all resources against current topo service(s); recompile if necessary
266 } else {
267 removeTrackedResources(key, intent.resources());
268 for (Intent installable : installables) {
269 removeTrackedResources(key, installable.resources());
270 }
271 }
272 }
273
tom95329eb2014-10-06 08:40:06 -0700274 // Internal re-actor to topology change events.
275 private class InternalTopologyListener implements TopologyListener {
276 @Override
277 public void event(TopologyEvent event) {
278 executorService.execute(new TopologyChangeHandler(event));
279 }
280 }
281
282 // Re-dispatcher of topology change events.
283 private class TopologyChangeHandler implements Runnable {
284
285 private final TopologyEvent event;
286
287 TopologyChangeHandler(TopologyEvent event) {
288 this.event = event;
289 }
290
291 @Override
292 public void run() {
Thomas Vachuska7b652ad2014-10-30 14:10:51 -0700293 // If there is no delegate, why bother? Just bail.
294 if (delegate == null) {
295 return;
296 }
297
Ray Milkey7c44c052014-12-05 10:34:54 -0800298 if (event.reasons() == null || event.reasons().isEmpty()) {
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700299 delegate.triggerCompile(Collections.emptySet(), true);
tom85258ee2014-10-07 00:10:02 -0700300
tom95329eb2014-10-06 08:40:06 -0700301 } else {
Jonathan Hart96c5a4a2015-07-31 14:23:33 -0700302 Set<Key> intentsToRecompile = new HashSet<>();
303 boolean dontRecompileAllFailedIntents = true;
tom85258ee2014-10-07 00:10:02 -0700304
305 // Scan through the list of reasons and keep accruing all
306 // intents that need to be recompiled.
tom95329eb2014-10-06 08:40:06 -0700307 for (Event reason : event.reasons()) {
308 if (reason instanceof LinkEvent) {
309 LinkEvent linkEvent = (LinkEvent) reason;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -0700310 final LinkKey linkKey = linkKey(linkEvent.subject());
311 synchronized (intentsByLink) {
312 Set<Key> intentKeys = intentsByLink.get(linkKey);
313 log.debug("recompile triggered by LinkEvent {} ({}) for {}",
314 linkKey, linkEvent.type(), intentKeys);
315 intentsToRecompile.addAll(intentKeys);
tom85258ee2014-10-07 00:10:02 -0700316 }
Jonathan Hart96c5a4a2015-07-31 14:23:33 -0700317 dontRecompileAllFailedIntents = dontRecompileAllFailedIntents &&
Praseed Balakrishnan00dd1f92014-11-19 17:12:36 -0800318 (linkEvent.type() == LINK_REMOVED ||
319 (linkEvent.type() == LINK_UPDATED &&
Ray Milkey8521f812017-05-24 09:49:26 -0700320 linkEvent.subject().isExpected()));
tom95329eb2014-10-06 08:40:06 -0700321 }
322 }
Jonathan Hart96c5a4a2015-07-31 14:23:33 -0700323 delegate.triggerCompile(intentsToRecompile, !dontRecompileAllFailedIntents);
tom95329eb2014-10-06 08:40:06 -0700324 }
325 }
326 }
327
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -0800328 private class InternalResourceListener implements ResourceListener {
Ray Milkeye97ede92014-11-20 10:43:12 -0800329 @Override
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -0800330 public void event(ResourceEvent event) {
Sho SHIMIZUb08d5862016-02-11 12:37:28 -0800331 if (event.subject().isSubTypeOf(PortNumber.class)) {
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -0800332 executorService.execute(() -> {
333 if (delegate == null) {
334 return;
335 }
Ray Milkeye97ede92014-11-20 10:43:12 -0800336
Sho SHIMIZU0b9c4682015-11-02 18:30:34 -0800337 delegate.triggerCompile(Collections.emptySet(), true);
338 });
Ray Milkeye97ede92014-11-20 10:43:12 -0800339 }
Ray Milkeye97ede92014-11-20 10:43:12 -0800340 }
341 }
342
Brian O'Connor72a034c2014-11-26 18:24:23 -0800343 //TODO consider adding flow rule event tracking
Ray Milkeye97ede92014-11-20 10:43:12 -0800344
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700345 /*
346 * Re-dispatcher of device and host events.
347 */
348 private class DeviceAvailabilityHandler implements Runnable {
349
350 private final ElementId id;
351 private final boolean available;
352
353 DeviceAvailabilityHandler(ElementId id, boolean available) {
354 this.id = checkNotNull(id);
355 this.available = available;
356 }
357
358 @Override
359 public void run() {
360 // If there is no delegate, why bother? Just bail.
361 if (delegate == null) {
362 return;
363 }
364
365 // TODO should we recompile on available==true?
HIGUCHI Yuta99b7b342015-09-29 16:54:21 -0700366
367 final ImmutableSet<Key> snapshot;
368 synchronized (intentsByDevice) {
369 snapshot = ImmutableSet.copyOf(intentsByDevice.get(id));
370 }
371 delegate.triggerCompile(snapshot, available);
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700372 }
373 }
374
375
376 private class InternalDeviceListener implements DeviceListener {
377 @Override
378 public void event(DeviceEvent event) {
379 DeviceEvent.Type type = event.type();
Jonathan Hart94470fe2015-07-31 11:41:10 -0700380 switch (type) {
381 case DEVICE_ADDED:
382 case DEVICE_AVAILABILITY_CHANGED:
383 case DEVICE_REMOVED:
384 case DEVICE_SUSPENDED:
385 case DEVICE_UPDATED:
386 DeviceId id = event.subject().id();
387 // TODO we need to check whether AVAILABILITY_CHANGED means up or down
388 boolean available = (type == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
389 type == DeviceEvent.Type.DEVICE_ADDED ||
390 type == DeviceEvent.Type.DEVICE_UPDATED);
391 executorService.execute(new DeviceAvailabilityHandler(id, available));
392 break;
393 case PORT_ADDED:
394 case PORT_REMOVED:
395 case PORT_UPDATED:
396 case PORT_STATS_UPDATED:
397 default:
398 // Don't handle port events for now
399 break;
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700400 }
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700401 }
402 }
403
404 private class InternalHostListener implements HostListener {
405 @Override
406 public void event(HostEvent event) {
407 HostId id = event.subject().id();
Brian O'Connora9c18b92015-09-28 16:03:43 -0700408 switch (event.type()) {
409 case HOST_ADDED:
410 case HOST_MOVED:
411 case HOST_REMOVED:
412 executorService.execute(new DeviceAvailabilityHandler(id, false));
413 break;
414 case HOST_UPDATED:
415 default:
416 // DO NOTHING
417 break;
418 }
Brian O'Connorb5dcc512015-03-24 17:28:00 -0700419 }
420 }
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700421
Sho SHIMIZU97a93dd2016-03-30 18:07:51 -0700422 private void doIntentUpdate() {
jaegonkimdc8a5562018-04-05 23:14:57 +0900423 synchronized (this) {
424 updateScheduled.set(false);
425 if (intentService == null) {
426 log.warn("Intent service is not bound yet");
427 return;
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700428 }
jaegonkimdc8a5562018-04-05 23:14:57 +0900429 try {
430 //FIXME very inefficient
431 for (IntentData intentData : intentService.getIntentData()) {
432 try {
433 trackIntent(intentData);
434 } catch (NullPointerException npe) {
435 log.warn("intent error {}", intentData.key(), npe);
436 }
437 }
438 } catch (Exception e) {
439 log.warn("Exception caught during update task", e);
440 }
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700441 }
442 }
443
444 private void scheduleIntentUpdate(int afterDelaySec) {
445 if (updateScheduled.compareAndSet(false, true)) {
446 executor.schedule(this::doIntentUpdate, afterDelaySec, TimeUnit.SECONDS);
447 }
448 }
449
Madan Jampani3b8101a2016-09-15 13:22:01 -0700450 private final class InternalPartitionListener implements WorkPartitionEventListener {
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700451 @Override
Madan Jampani3b8101a2016-09-15 13:22:01 -0700452 public void event(WorkPartitionEvent event) {
Jon Hall274cecb2017-08-09 12:15:48 -0700453 log.debug("got message {}:{}", event.type(), event.subject());
Brian O'Connor69d6ac72015-05-29 16:24:06 -0700454 scheduleIntentUpdate(1);
455 }
456 }
tom95329eb2014-10-06 08:40:06 -0700457}