blob: bb08cd85d5d05507fa93cece90daea844e9a6399 [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;
Brian O'Connor3c58e962015-04-28 23:21:51 -070045import static org.slf4j.LoggerFactory.getLogger;
46
47/**
Brian O'Connora6c9b5c2015-04-29 22:38:29 -070048 * This component cleans up intents that have encountered errors or otherwise
49 * stalled during installation or withdrawal.
50 * <p>
51 * It periodically polls (based on configured period) for pending and CORRUPT
52 * intents from the store and retries. It also listens for CORRUPT event
53 * notifications, which signify errors in processing, and retries.
54 * </p>
Brian O'Connor3c58e962015-04-28 23:21:51 -070055 */
56@Component(immediate = true)
57public class IntentCleanup implements Runnable, IntentListener {
58
Brian O'Connorcdec4932015-04-30 16:16:47 -070059 private static final Logger log = getLogger(IntentCleanup.class);
Brian O'Connor3c58e962015-04-28 23:21:51 -070060
Pier Luigie6caf682017-01-26 15:25:09 -080061 // Logical timeout for stuck Intents in INSTALLING or WITHDRAWING. The unit is seconds
62 private static final int INSTALLING_WITHDRAWING_PERIOD = 120;
63
Brian O'Connor3c58e962015-04-28 23:21:51 -070064 private static final int DEFAULT_PERIOD = 5; //seconds
Brian O'Connor6d8e3172015-04-30 15:43:57 -070065 private static final int DEFAULT_THRESHOLD = 5; //tries
Brian O'Connor3c58e962015-04-28 23:21:51 -070066
Ray Milkeyd84f89b2018-08-17 14:54:17 -070067 //@Property(name = "enabled", boolValue = true,
68 // label = "Enables/disables the intent cleanup component")
Brian O'Connor5fcf6f52015-05-28 17:34:26 -070069 private boolean enabled = true;
70
Ray Milkeyd84f89b2018-08-17 14:54:17 -070071 //@Property(name = "period", intValue = DEFAULT_PERIOD,
72 // label = "Frequency in ms between cleanup runs")
Brian O'Connor3c58e962015-04-28 23:21:51 -070073 protected int period = DEFAULT_PERIOD;
Brian O'Connora6c9b5c2015-04-29 22:38:29 -070074 private long periodMs;
Pier Luigie6caf682017-01-26 15:25:09 -080075 private long periodMsForStuck;
Brian O'Connor3c58e962015-04-28 23:21:51 -070076
Ray Milkeyd84f89b2018-08-17 14:54:17 -070077 //@Property(name = "retryThreshold", intValue = DEFAULT_THRESHOLD,
78 // label = "Number of times to retry CORRUPT intent without delay")
Brian O'Connorcdec4932015-04-30 16:16:47 -070079 protected int retryThreshold = DEFAULT_THRESHOLD;
Brian O'Connor6d8e3172015-04-30 15:43:57 -070080
Ray Milkeyd84f89b2018-08-17 14:54:17 -070081 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Brian O'Connor3c58e962015-04-28 23:21:51 -070082 protected IntentService service;
83
Ray Milkeyd84f89b2018-08-17 14:54:17 -070084 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Brian O'Connor3c58e962015-04-28 23:21:51 -070085 protected IntentStore store;
86
Ray Milkeyd84f89b2018-08-17 14:54:17 -070087 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Brian O'Connor3c58e962015-04-28 23:21:51 -070088 protected ComponentConfigService cfgService;
89
90 private ExecutorService executor;
91 private Timer timer;
92 private TimerTask timerTask;
93
94 @Activate
95 public void activate() {
96 cfgService.registerProperties(getClass());
HIGUCHI Yutad9e01052016-04-14 09:31:42 -070097 executor = newSingleThreadExecutor(groupedThreads("onos/intent", "cleanup", log));
Brian O'Connor3c58e962015-04-28 23:21:51 -070098 timer = new Timer("onos-intent-cleanup-timer");
99 service.addListener(this);
100 adjustRate();
101 log.info("Started");
102 }
103
104 @Deactivate
105 public void deactivate() {
106 cfgService.unregisterProperties(getClass(), false);
107 service.removeListener(this);
108 timer.cancel();
109 timerTask = null;
110 executor.shutdown();
111 log.info("Stopped");
112 }
113
114 @Modified
115 public void modified(ComponentContext context) {
116 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
117
118 int newPeriod;
Brian O'Connor5fcf6f52015-05-28 17:34:26 -0700119 boolean newEnabled;
Brian O'Connor3c58e962015-04-28 23:21:51 -0700120 try {
121 String s = get(properties, "period");
122 newPeriod = isNullOrEmpty(s) ? period : Integer.parseInt(s.trim());
Brian O'Connor6d8e3172015-04-30 15:43:57 -0700123
124 s = get(properties, "retryThreshold");
Brian O'Connor5fcf6f52015-05-28 17:34:26 -0700125 retryThreshold = isNullOrEmpty(s) ? retryThreshold : Integer.parseInt(s.trim());
126
127 s = get(properties, "enabled");
128 newEnabled = isNullOrEmpty(s) ? enabled : Boolean.parseBoolean(s.trim());
Brian O'Connor3c58e962015-04-28 23:21:51 -0700129 } catch (NumberFormatException e) {
130 log.warn(e.getMessage());
131 newPeriod = period;
Brian O'Connor5fcf6f52015-05-28 17:34:26 -0700132 newEnabled = enabled;
Brian O'Connor3c58e962015-04-28 23:21:51 -0700133 }
134
135 // Any change in the following parameters implies hard restart
Pier Luigie6caf682017-01-26 15:25:09 -0800136 // We could further restrict only for values multiple of the period
137 // of the stuck intents
138 if (newPeriod != period || enabled != newEnabled || newPeriod <= INSTALLING_WITHDRAWING_PERIOD) {
Brian O'Connor3c58e962015-04-28 23:21:51 -0700139 period = newPeriod;
Brian O'Connor5fcf6f52015-05-28 17:34:26 -0700140 enabled = newEnabled;
Brian O'Connor3c58e962015-04-28 23:21:51 -0700141 adjustRate();
142 }
143
Brian O'Connor5fcf6f52015-05-28 17:34:26 -0700144 log.info("Settings: enabled={}, period={}, retryThreshold={}",
145 enabled, period, retryThreshold);
Brian O'Connor3c58e962015-04-28 23:21:51 -0700146 }
147
Brian O'Connoreba4e342015-04-30 22:50:13 -0700148 protected void adjustRate() {
Brian O'Connor3c58e962015-04-28 23:21:51 -0700149 if (timerTask != null) {
150 timerTask.cancel();
Brian O'Connor5fcf6f52015-05-28 17:34:26 -0700151 timerTask = null;
Brian O'Connor3c58e962015-04-28 23:21:51 -0700152 }
153
Brian O'Connor5fcf6f52015-05-28 17:34:26 -0700154 if (enabled) {
155 timerTask = new TimerTask() {
156 @Override
157 public void run() {
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700158 executor.execute(IntentCleanup.this);
Brian O'Connor5fcf6f52015-05-28 17:34:26 -0700159 }
160 };
Pier Luigie6caf682017-01-26 15:25:09 -0800161 // Convert to ms
Ray Milkey3717e602018-02-01 13:49:47 -0800162 periodMs = period * 1_000L;
163 periodMsForStuck = INSTALLING_WITHDRAWING_PERIOD * 1000L;
Pier Luigie6caf682017-01-26 15:25:09 -0800164 // Schedule the executions
Brian O'Connor5fcf6f52015-05-28 17:34:26 -0700165 timer.scheduleAtFixedRate(timerTask, periodMs, periodMs);
166 }
Brian O'Connor3c58e962015-04-28 23:21:51 -0700167 }
168
169
170 @Override
171 public void run() {
172 try {
173 cleanup();
174 } catch (Exception e) {
175 log.warn("Caught exception during Intent cleanup", e);
176 }
177 }
178
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700179 private void resubmitCorrupt(IntentData intentData, boolean checkThreshold) {
Brian O'Connor6d8e3172015-04-30 15:43:57 -0700180 if (checkThreshold && intentData.errorCount() >= retryThreshold) {
Brian O'Connor38224302016-08-02 22:03:01 -0700181 //FIXME trace or debug statement?
Brian O'Connor6d8e3172015-04-30 15:43:57 -0700182 return; // threshold met or exceeded
Brian O'Connor38224302016-08-02 22:03:01 -0700183 } // FIXME should we backoff here?
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700184
185 switch (intentData.request()) {
186 case INSTALL_REQ:
187 service.submit(intentData.intent());
188 break;
189 case WITHDRAW_REQ:
190 service.withdraw(intentData.intent());
191 break;
192 default:
Jonathan Hartaae93b22015-07-22 14:59:47 -0700193 log.warn("Trying to resubmit corrupt/failed intent {} in state {} with request {}",
Brian O'Connorb55d6e62015-06-01 15:25:53 -0700194 intentData.key(), intentData.state(), intentData.request());
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700195 break;
196 }
197 }
198
199 private void resubmitPendingRequest(IntentData intentData) {
Brian O'Connor38224302016-08-02 22:03:01 -0700200 // FIXME should we back off here?
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700201 switch (intentData.request()) {
202 case INSTALL_REQ:
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700203 case WITHDRAW_REQ:
Brian O'Connor105cf532016-04-19 13:07:38 -0700204 case PURGE_REQ:
jaegonkimcbe1c5e2018-05-20 15:11:18 +0900205 service.addPending(IntentData.copy(intentData, new WallClockTimestamp()));
Brian O'Connor105cf532016-04-19 13:07:38 -0700206 break;
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700207 default:
Brian O'Connorc90d1842015-10-01 15:48:00 -0700208 log.warn("Failed to resubmit pending intent {} in state {} with request {}",
Brian O'Connorb55d6e62015-06-01 15:25:53 -0700209 intentData.key(), intentData.state(), intentData.request());
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700210 break;
211 }
212 }
213
Brian O'Connor3c58e962015-04-28 23:21:51 -0700214 /**
Jonathan Hartaae93b22015-07-22 14:59:47 -0700215 * Iterates through corrupt, failed and pending intents and
216 * re-submit/withdraw appropriately.
Brian O'Connor3c58e962015-04-28 23:21:51 -0700217 */
218 private void cleanup() {
Pier Luigie6caf682017-01-26 15:25:09 -0800219 int corruptCount = 0, failedCount = 0, stuckCount = 0, pendingCount = 0, skipped = 0;
Jonathan Hartaae93b22015-07-22 14:59:47 -0700220
Brian O'Connorc590ebb2016-12-08 18:16:41 -0800221 // Check the pending map first, because the check of the current map
222 // will add items to the pending map.
223 for (IntentData intentData : store.getPendingData(true, periodMs)) {
Pier Luigi13b287f2017-01-10 15:07:52 -0800224 log.debug("Resubmit Pending Intent: key {}, state {}, request {}",
225 intentData.key(), intentData.state(), intentData.request());
Brian O'Connorc590ebb2016-12-08 18:16:41 -0800226 resubmitPendingRequest(intentData);
227 pendingCount++;
228 }
229
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700230 for (IntentData intentData : store.getIntentData(true, periodMs)) {
jaegonkimab7e59f2018-05-07 13:04:05 +0900231 IntentData pendingIntentData = store.getPendingData(intentData.key());
232 if (pendingIntentData != null) {
233 continue;
234 }
235
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700236 switch (intentData.state()) {
Jonathan Hartaae93b22015-07-22 14:59:47 -0700237 case FAILED:
Pier Luigi13b287f2017-01-10 15:07:52 -0800238 log.debug("Resubmit Failed Intent: key {}, state {}, request {}",
jaegonkimab7e59f2018-05-07 13:04:05 +0900239 intentData.key(), intentData.state(), intentData.request());
Jonathan Hartaae93b22015-07-22 14:59:47 -0700240 resubmitCorrupt(intentData, false);
241 failedCount++;
242 break;
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700243 case CORRUPT:
Pier Luigi13b287f2017-01-10 15:07:52 -0800244 log.debug("Resubmit Corrupt Intent: key {}, state {}, request {}",
jaegonkimab7e59f2018-05-07 13:04:05 +0900245 intentData.key(), intentData.state(), intentData.request());
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700246 resubmitCorrupt(intentData, false);
247 corruptCount++;
Brian O'Connoreba4e342015-04-30 22:50:13 -0700248 break;
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700249 case INSTALLING: //FALLTHROUGH
250 case WITHDRAWING:
Pier Luigie6caf682017-01-26 15:25:09 -0800251 // Instances can have different clocks and potentially we can have problems
252 // An Intent can be submitted again before the real period of the stuck intents
253 final WallClockTimestamp time = new WallClockTimestamp(
254 System.currentTimeMillis() - periodMsForStuck
255 );
256 if (intentData.version().isOlderThan(time)) {
257 resubmitPendingRequest(intentData);
258 stuckCount++;
259 } else {
260 skipped++;
261 }
Brian O'Connoreba4e342015-04-30 22:50:13 -0700262 break;
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700263 default:
264 //NOOP
265 break;
Brian O'Connor3c58e962015-04-28 23:21:51 -0700266 }
267 }
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700268
Jonathan Hart82efa692015-10-10 18:30:28 -0700269 if (corruptCount + failedCount + stuckCount + pendingCount > 0) {
270 log.debug("Intent cleanup ran and resubmitted {} corrupt, {} failed, {} stuck, and {} pending intents",
271 corruptCount, failedCount, stuckCount, pendingCount);
272 }
Pier Luigie6caf682017-01-26 15:25:09 -0800273 if (skipped > 0) {
274 log.debug("Intent cleanup skipped {} intents", skipped);
275 }
Brian O'Connor3c58e962015-04-28 23:21:51 -0700276 }
277
278 @Override
279 public void event(IntentEvent event) {
Brian O'Connor6d8e3172015-04-30 15:43:57 -0700280 // this is the fast path for CORRUPT intents, retry on event notification.
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700281 //TODO we might consider using the timer to back off for subsequent retries
Brian O'Connor5fcf6f52015-05-28 17:34:26 -0700282 if (enabled && event.type() == IntentEvent.Type.CORRUPT) {
Brian O'Connora6c9b5c2015-04-29 22:38:29 -0700283 Key key = event.subject().key();
284 if (store.isMaster(key)) {
285 IntentData data = store.getIntentData(event.subject().key());
286 resubmitCorrupt(data, true);
287 }
Brian O'Connor3c58e962015-04-28 23:21:51 -0700288 }
289 }
290}