blob: d41439e4762d2358a85f1211d6c8110f3d9f6775 [file] [log] [blame]
Brian O'Connor3c58e962015-04-28 23:21:51 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Brian O'Connor3c58e962015-04-28 23:21:51 -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 */
16package org.onosproject.net.intent.impl;
17
Brian O'Connor3c58e962015-04-28 23:21:51 -070018import org.onosproject.cfg.ComponentConfigService;
19import org.onosproject.net.intent.IntentData;
20import org.onosproject.net.intent.IntentEvent;
21import org.onosproject.net.intent.IntentListener;
22import org.onosproject.net.intent.IntentService;
23import org.onosproject.net.intent.IntentStore;
Brian O'Connora6c9b5c2015-04-29 22:38:29 -070024import org.onosproject.net.intent.Key;
Pier Luigie6caf682017-01-26 15:25:09 -080025import org.onosproject.store.service.WallClockTimestamp;
Brian O'Connor3c58e962015-04-28 23:21:51 -070026import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070027import org.osgi.service.component.annotations.Activate;
28import org.osgi.service.component.annotations.Component;
29import org.osgi.service.component.annotations.Deactivate;
30import org.osgi.service.component.annotations.Modified;
31import org.osgi.service.component.annotations.Reference;
32import org.osgi.service.component.annotations.ReferenceCardinality;
Brian O'Connor3c58e962015-04-28 23:21:51 -070033import org.slf4j.Logger;
34
35import java.util.Dictionary;
36import java.util.Properties;
37import java.util.Timer;
38import java.util.TimerTask;
39import java.util.concurrent.ExecutorService;
40
41import static com.google.common.base.Strings.isNullOrEmpty;
42import static java.util.concurrent.Executors.newSingleThreadExecutor;
43import static org.onlab.util.Tools.get;
44import static org.onlab.util.Tools.groupedThreads;
Ray Milkeyd04e2272018-10-16 18:20:18 -070045import static org.onosproject.net.OsgiPropertyConstants.ICU_ENABLED;
46import static org.onosproject.net.OsgiPropertyConstants.ICU_ENABLED_DEFAULT;
47import static org.onosproject.net.OsgiPropertyConstants.ICU_PERIOD;
48import static org.onosproject.net.OsgiPropertyConstants.ICU_PERIOD_DEFAULT;
49import static org.onosproject.net.OsgiPropertyConstants.ICU_RETRY_THRESHOLD;
50import static org.onosproject.net.OsgiPropertyConstants.ICU_RETRY_THRESHOLD_DEFAULT;
Brian O'Connor3c58e962015-04-28 23:21:51 -070051import static org.slf4j.LoggerFactory.getLogger;
52
53/**
Brian O'Connora6c9b5c2015-04-29 22:38:29 -070054 * This component cleans up intents that have encountered errors or otherwise
55 * stalled during installation or withdrawal.
56 * <p>
57 * It periodically polls (based on configured period) for pending and CORRUPT
58 * intents from the store and retries. It also listens for CORRUPT event
59 * notifications, which signify errors in processing, and retries.
60 * </p>
Brian O'Connor3c58e962015-04-28 23:21:51 -070061 */
Ray Milkeyd04e2272018-10-16 18:20:18 -070062@Component(
63 immediate = true,
64 property = {
Ray Milkey2d7bca12018-10-17 14:51:52 -070065 ICU_ENABLED + ":Boolean=" + ICU_ENABLED_DEFAULT,
66 ICU_PERIOD + ":Integer=" + ICU_PERIOD_DEFAULT,
67 ICU_RETRY_THRESHOLD + ":Integer=" + ICU_RETRY_THRESHOLD_DEFAULT
Ray Milkeyd04e2272018-10-16 18:20:18 -070068 }
69)
Brian O'Connor3c58e962015-04-28 23:21:51 -070070public class IntentCleanup implements Runnable, IntentListener {
71
Brian O'Connorcdec4932015-04-30 16:16:47 -070072 private static final Logger log = getLogger(IntentCleanup.class);
Brian O'Connor3c58e962015-04-28 23:21:51 -070073
Pier Luigie6caf682017-01-26 15:25:09 -080074 // Logical timeout for stuck Intents in INSTALLING or WITHDRAWING. The unit is seconds
75 private static final int INSTALLING_WITHDRAWING_PERIOD = 120;
76
Brian O'Connora6c9b5c2015-04-29 22:38:29 -070077 private long periodMs;
Pier Luigie6caf682017-01-26 15:25:09 -080078 private long periodMsForStuck;
Brian O'Connor3c58e962015-04-28 23:21:51 -070079
Thomas Vachuskaf566fa22018-10-30 14:03:36 -070080 /** Enables/disables the intent cleanup component. */
81 private boolean enabled = ICU_ENABLED_DEFAULT;
82
83 /** Frequency in ms between cleanup runs. */
84 protected int period = ICU_PERIOD_DEFAULT;
85
86 /** Number of times to retry CORRUPT intent without delay. */
Ray Milkeyd04e2272018-10-16 18:20:18 -070087 protected int retryThreshold = ICU_RETRY_THRESHOLD_DEFAULT;
Brian O'Connor6d8e3172015-04-30 15:43:57 -070088
Ray Milkeyd84f89b2018-08-17 14:54:17 -070089 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Brian O'Connor3c58e962015-04-28 23:21:51 -070090 protected IntentService service;
91
Ray Milkeyd84f89b2018-08-17 14:54:17 -070092 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Brian O'Connor3c58e962015-04-28 23:21:51 -070093 protected IntentStore store;
94
Ray Milkeyd84f89b2018-08-17 14:54:17 -070095 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Brian O'Connor3c58e962015-04-28 23:21:51 -070096 protected ComponentConfigService cfgService;
97
98 private ExecutorService executor;
99 private Timer timer;
100 private TimerTask timerTask;
101
102 @Activate
103 public void activate() {
104 cfgService.registerProperties(getClass());
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700105 executor = newSingleThreadExecutor(groupedThreads("onos/intent", "cleanup", log));
Brian O'Connor3c58e962015-04-28 23:21:51 -0700106 timer = new Timer("onos-intent-cleanup-timer");
107 service.addListener(this);
108 adjustRate();
109 log.info("Started");
110 }
111
112 @Deactivate
113 public void deactivate() {
114 cfgService.unregisterProperties(getClass(), false);
115 service.removeListener(this);
116 timer.cancel();
117 timerTask = null;
118 executor.shutdown();
119 log.info("Stopped");
120 }
121
122 @Modified
123 public void modified(ComponentContext context) {
124 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
125
126 int newPeriod;
Brian O'Connor5fcf6f52015-05-28 17:34:26 -0700127 boolean newEnabled;
Brian O'Connor3c58e962015-04-28 23:21:51 -0700128 try {
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700129 String s = get(properties, ICU_PERIOD);
Brian O'Connor3c58e962015-04-28 23:21:51 -0700130 newPeriod = isNullOrEmpty(s) ? period : Integer.parseInt(s.trim());
Brian O'Connor6d8e3172015-04-30 15:43:57 -0700131
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700132 s = get(properties, ICU_RETRY_THRESHOLD);
Brian O'Connor5fcf6f52015-05-28 17:34:26 -0700133 retryThreshold = isNullOrEmpty(s) ? retryThreshold : Integer.parseInt(s.trim());
134
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700135 s = get(properties, ICU_ENABLED);
Brian O'Connor5fcf6f52015-05-28 17:34:26 -0700136 newEnabled = isNullOrEmpty(s) ? enabled : Boolean.parseBoolean(s.trim());
Brian O'Connor3c58e962015-04-28 23:21:51 -0700137 } catch (NumberFormatException e) {
138 log.warn(e.getMessage());
139 newPeriod = period;
Brian O'Connor5fcf6f52015-05-28 17:34:26 -0700140 newEnabled = enabled;
Brian O'Connor3c58e962015-04-28 23:21:51 -0700141 }
142
143 // Any change in the following parameters implies hard restart
Pier Luigie6caf682017-01-26 15:25:09 -0800144 // We could further restrict only for values multiple of the period
145 // of the stuck intents
146 if (newPeriod != period || enabled != newEnabled || newPeriod <= INSTALLING_WITHDRAWING_PERIOD) {
Brian O'Connor3c58e962015-04-28 23:21:51 -0700147 period = newPeriod;
Brian O'Connor5fcf6f52015-05-28 17:34:26 -0700148 enabled = newEnabled;
Brian O'Connor3c58e962015-04-28 23:21:51 -0700149 adjustRate();
150 }
151
Brian O'Connor5fcf6f52015-05-28 17:34:26 -0700152 log.info("Settings: enabled={}, period={}, retryThreshold={}",
153 enabled, period, retryThreshold);
Brian O'Connor3c58e962015-04-28 23:21:51 -0700154 }
155
Brian O'Connoreba4e342015-04-30 22:50:13 -0700156 protected void adjustRate() {
Brian O'Connor3c58e962015-04-28 23:21:51 -0700157 if (timerTask != null) {
158 timerTask.cancel();
Brian O'Connor5fcf6f52015-05-28 17:34:26 -0700159 timerTask = null;
Brian O'Connor3c58e962015-04-28 23:21:51 -0700160 }
161
Brian O'Connor5fcf6f52015-05-28 17:34:26 -0700162 if (enabled) {
163 timerTask = new TimerTask() {
164 @Override
165 public void run() {
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700166 executor.execute(IntentCleanup.this);
Brian O'Connor5fcf6f52015-05-28 17:34:26 -0700167 }
168 };
Pier Luigie6caf682017-01-26 15:25:09 -0800169 // Convert to ms
Ray Milkey3717e602018-02-01 13:49:47 -0800170 periodMs = period * 1_000L;
171 periodMsForStuck = INSTALLING_WITHDRAWING_PERIOD * 1000L;
Pier Luigie6caf682017-01-26 15:25:09 -0800172 // Schedule the executions
Brian O'Connor5fcf6f52015-05-28 17:34:26 -0700173 timer.scheduleAtFixedRate(timerTask, periodMs, periodMs);
174 }
Brian O'Connor3c58e962015-04-28 23:21:51 -0700175 }
176
177
178 @Override
179 public void run() {
180 try {
181 cleanup();
182 } catch (Exception e) {
183 log.warn("Caught exception during Intent cleanup", e);
184 }
185 }
186
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700187 private void resubmitCorrupt(IntentData intentData, boolean checkThreshold) {
Brian O'Connor6d8e3172015-04-30 15:43:57 -0700188 if (checkThreshold && intentData.errorCount() >= retryThreshold) {
Brian O'Connor38224302016-08-02 22:03:01 -0700189 //FIXME trace or debug statement?
Brian O'Connor6d8e3172015-04-30 15:43:57 -0700190 return; // threshold met or exceeded
Brian O'Connor38224302016-08-02 22:03:01 -0700191 } // FIXME should we backoff here?
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700192
193 switch (intentData.request()) {
194 case INSTALL_REQ:
195 service.submit(intentData.intent());
196 break;
197 case WITHDRAW_REQ:
198 service.withdraw(intentData.intent());
199 break;
200 default:
Jonathan Hartaae93b22015-07-22 14:59:47 -0700201 log.warn("Trying to resubmit corrupt/failed intent {} in state {} with request {}",
Brian O'Connorb55d6e62015-06-01 15:25:53 -0700202 intentData.key(), intentData.state(), intentData.request());
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700203 break;
204 }
205 }
206
207 private void resubmitPendingRequest(IntentData intentData) {
Brian O'Connor38224302016-08-02 22:03:01 -0700208 // FIXME should we back off here?
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700209 switch (intentData.request()) {
210 case INSTALL_REQ:
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700211 case WITHDRAW_REQ:
Brian O'Connor105cf532016-04-19 13:07:38 -0700212 case PURGE_REQ:
jaegonkimcbe1c5e2018-05-20 15:11:18 +0900213 service.addPending(IntentData.copy(intentData, new WallClockTimestamp()));
Brian O'Connor105cf532016-04-19 13:07:38 -0700214 break;
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700215 default:
Brian O'Connorc90d1842015-10-01 15:48:00 -0700216 log.warn("Failed to resubmit pending intent {} in state {} with request {}",
Brian O'Connorb55d6e62015-06-01 15:25:53 -0700217 intentData.key(), intentData.state(), intentData.request());
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700218 break;
219 }
220 }
221
Brian O'Connor3c58e962015-04-28 23:21:51 -0700222 /**
Jonathan Hartaae93b22015-07-22 14:59:47 -0700223 * Iterates through corrupt, failed and pending intents and
224 * re-submit/withdraw appropriately.
Brian O'Connor3c58e962015-04-28 23:21:51 -0700225 */
226 private void cleanup() {
Pier Luigie6caf682017-01-26 15:25:09 -0800227 int corruptCount = 0, failedCount = 0, stuckCount = 0, pendingCount = 0, skipped = 0;
Jonathan Hartaae93b22015-07-22 14:59:47 -0700228
Brian O'Connorc590ebb2016-12-08 18:16:41 -0800229 // Check the pending map first, because the check of the current map
230 // will add items to the pending map.
231 for (IntentData intentData : store.getPendingData(true, periodMs)) {
Pier Luigi13b287f2017-01-10 15:07:52 -0800232 log.debug("Resubmit Pending Intent: key {}, state {}, request {}",
233 intentData.key(), intentData.state(), intentData.request());
Brian O'Connorc590ebb2016-12-08 18:16:41 -0800234 resubmitPendingRequest(intentData);
235 pendingCount++;
236 }
237
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700238 for (IntentData intentData : store.getIntentData(true, periodMs)) {
jaegonkimab7e59f2018-05-07 13:04:05 +0900239 IntentData pendingIntentData = store.getPendingData(intentData.key());
240 if (pendingIntentData != null) {
241 continue;
242 }
243
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700244 switch (intentData.state()) {
Jonathan Hartaae93b22015-07-22 14:59:47 -0700245 case FAILED:
Pier Luigi13b287f2017-01-10 15:07:52 -0800246 log.debug("Resubmit Failed Intent: key {}, state {}, request {}",
jaegonkimab7e59f2018-05-07 13:04:05 +0900247 intentData.key(), intentData.state(), intentData.request());
Jonathan Hartaae93b22015-07-22 14:59:47 -0700248 resubmitCorrupt(intentData, false);
249 failedCount++;
250 break;
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700251 case CORRUPT:
Pier Luigi13b287f2017-01-10 15:07:52 -0800252 log.debug("Resubmit Corrupt Intent: key {}, state {}, request {}",
jaegonkimab7e59f2018-05-07 13:04:05 +0900253 intentData.key(), intentData.state(), intentData.request());
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700254 resubmitCorrupt(intentData, false);
255 corruptCount++;
Brian O'Connoreba4e342015-04-30 22:50:13 -0700256 break;
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700257 case INSTALLING: //FALLTHROUGH
258 case WITHDRAWING:
Pier Luigie6caf682017-01-26 15:25:09 -0800259 // Instances can have different clocks and potentially we can have problems
260 // An Intent can be submitted again before the real period of the stuck intents
261 final WallClockTimestamp time = new WallClockTimestamp(
262 System.currentTimeMillis() - periodMsForStuck
263 );
264 if (intentData.version().isOlderThan(time)) {
265 resubmitPendingRequest(intentData);
266 stuckCount++;
267 } else {
268 skipped++;
269 }
Brian O'Connoreba4e342015-04-30 22:50:13 -0700270 break;
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700271 default:
272 //NOOP
273 break;
Brian O'Connor3c58e962015-04-28 23:21:51 -0700274 }
275 }
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700276
Jonathan Hart82efa692015-10-10 18:30:28 -0700277 if (corruptCount + failedCount + stuckCount + pendingCount > 0) {
278 log.debug("Intent cleanup ran and resubmitted {} corrupt, {} failed, {} stuck, and {} pending intents",
279 corruptCount, failedCount, stuckCount, pendingCount);
280 }
Pier Luigie6caf682017-01-26 15:25:09 -0800281 if (skipped > 0) {
282 log.debug("Intent cleanup skipped {} intents", skipped);
283 }
Brian O'Connor3c58e962015-04-28 23:21:51 -0700284 }
285
286 @Override
287 public void event(IntentEvent event) {
Brian O'Connor6d8e3172015-04-30 15:43:57 -0700288 // this is the fast path for CORRUPT intents, retry on event notification.
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700289 //TODO we might consider using the timer to back off for subsequent retries
Brian O'Connor5fcf6f52015-05-28 17:34:26 -0700290 if (enabled && event.type() == IntentEvent.Type.CORRUPT) {
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700291 Key key = event.subject().key();
292 if (store.isMaster(key)) {
293 IntentData data = store.getIntentData(event.subject().key());
294 resubmitCorrupt(data, true);
295 }
Brian O'Connor3c58e962015-04-28 23:21:51 -0700296 }
297 }
298}