Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 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 | */ |
| 16 | package org.onosproject.net.intent.impl; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Modified; |
| 22 | import org.apache.felix.scr.annotations.Property; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.onosproject.cfg.ComponentConfigService; |
| 26 | import org.onosproject.net.intent.IntentData; |
| 27 | import org.onosproject.net.intent.IntentEvent; |
| 28 | import org.onosproject.net.intent.IntentListener; |
| 29 | import org.onosproject.net.intent.IntentService; |
| 30 | import org.onosproject.net.intent.IntentStore; |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 31 | import org.onosproject.net.intent.Key; |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 32 | import org.osgi.service.component.ComponentContext; |
| 33 | import org.slf4j.Logger; |
| 34 | |
| 35 | import java.util.Dictionary; |
| 36 | import java.util.Properties; |
| 37 | import java.util.Timer; |
| 38 | import java.util.TimerTask; |
| 39 | import java.util.concurrent.ExecutorService; |
| 40 | |
| 41 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 42 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 43 | import static org.onlab.util.Tools.get; |
| 44 | import static org.onlab.util.Tools.groupedThreads; |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 45 | import static org.slf4j.LoggerFactory.getLogger; |
| 46 | |
| 47 | /** |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 48 | * 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'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 55 | */ |
| 56 | @Component(immediate = true) |
| 57 | public class IntentCleanup implements Runnable, IntentListener { |
| 58 | |
Brian O'Connor | cdec493 | 2015-04-30 16:16:47 -0700 | [diff] [blame] | 59 | private static final Logger log = getLogger(IntentCleanup.class); |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 60 | |
| 61 | private static final int DEFAULT_PERIOD = 5; //seconds |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 62 | private static final int DEFAULT_THRESHOLD = 5; //tries |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 63 | |
Brian O'Connor | 5fcf6f5 | 2015-05-28 17:34:26 -0700 | [diff] [blame] | 64 | @Property(name = "enabled", boolValue = true, |
| 65 | label = "Enables/disables the intent cleanup component") |
| 66 | private boolean enabled = true; |
| 67 | |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 68 | @Property(name = "period", intValue = DEFAULT_PERIOD, |
| 69 | label = "Frequency in ms between cleanup runs") |
| 70 | protected int period = DEFAULT_PERIOD; |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 71 | private long periodMs; |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 72 | |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 73 | @Property(name = "retryThreshold", intValue = DEFAULT_THRESHOLD, |
| 74 | label = "Number of times to retry CORRUPT intent without delay") |
Brian O'Connor | cdec493 | 2015-04-30 16:16:47 -0700 | [diff] [blame] | 75 | protected int retryThreshold = DEFAULT_THRESHOLD; |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 76 | |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected IntentService service; |
| 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected IntentStore store; |
| 82 | |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 84 | protected ComponentConfigService cfgService; |
| 85 | |
| 86 | private ExecutorService executor; |
| 87 | private Timer timer; |
| 88 | private TimerTask timerTask; |
| 89 | |
| 90 | @Activate |
| 91 | public void activate() { |
| 92 | cfgService.registerProperties(getClass()); |
| 93 | executor = newSingleThreadExecutor(groupedThreads("onos/intent", "cleanup")); |
| 94 | timer = new Timer("onos-intent-cleanup-timer"); |
| 95 | service.addListener(this); |
| 96 | adjustRate(); |
| 97 | log.info("Started"); |
| 98 | } |
| 99 | |
| 100 | @Deactivate |
| 101 | public void deactivate() { |
| 102 | cfgService.unregisterProperties(getClass(), false); |
| 103 | service.removeListener(this); |
| 104 | timer.cancel(); |
| 105 | timerTask = null; |
| 106 | executor.shutdown(); |
| 107 | log.info("Stopped"); |
| 108 | } |
| 109 | |
| 110 | @Modified |
| 111 | public void modified(ComponentContext context) { |
| 112 | Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties(); |
| 113 | |
| 114 | int newPeriod; |
Brian O'Connor | 5fcf6f5 | 2015-05-28 17:34:26 -0700 | [diff] [blame] | 115 | boolean newEnabled; |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 116 | try { |
| 117 | String s = get(properties, "period"); |
| 118 | newPeriod = isNullOrEmpty(s) ? period : Integer.parseInt(s.trim()); |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 119 | |
| 120 | s = get(properties, "retryThreshold"); |
Brian O'Connor | 5fcf6f5 | 2015-05-28 17:34:26 -0700 | [diff] [blame] | 121 | retryThreshold = isNullOrEmpty(s) ? retryThreshold : Integer.parseInt(s.trim()); |
| 122 | |
| 123 | s = get(properties, "enabled"); |
| 124 | newEnabled = isNullOrEmpty(s) ? enabled : Boolean.parseBoolean(s.trim()); |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 125 | } catch (NumberFormatException e) { |
| 126 | log.warn(e.getMessage()); |
| 127 | newPeriod = period; |
Brian O'Connor | 5fcf6f5 | 2015-05-28 17:34:26 -0700 | [diff] [blame] | 128 | newEnabled = enabled; |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // Any change in the following parameters implies hard restart |
Brian O'Connor | 5fcf6f5 | 2015-05-28 17:34:26 -0700 | [diff] [blame] | 132 | if (newPeriod != period || enabled != newEnabled) { |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 133 | period = newPeriod; |
Brian O'Connor | 5fcf6f5 | 2015-05-28 17:34:26 -0700 | [diff] [blame] | 134 | enabled = newEnabled; |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 135 | adjustRate(); |
| 136 | } |
| 137 | |
Brian O'Connor | 5fcf6f5 | 2015-05-28 17:34:26 -0700 | [diff] [blame] | 138 | log.info("Settings: enabled={}, period={}, retryThreshold={}", |
| 139 | enabled, period, retryThreshold); |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 142 | protected void adjustRate() { |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 143 | if (timerTask != null) { |
| 144 | timerTask.cancel(); |
Brian O'Connor | 5fcf6f5 | 2015-05-28 17:34:26 -0700 | [diff] [blame] | 145 | timerTask = null; |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Brian O'Connor | 5fcf6f5 | 2015-05-28 17:34:26 -0700 | [diff] [blame] | 148 | if (enabled) { |
| 149 | timerTask = new TimerTask() { |
| 150 | @Override |
| 151 | public void run() { |
| 152 | executor.submit(IntentCleanup.this); |
| 153 | } |
| 154 | }; |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 155 | |
Brian O'Connor | 5fcf6f5 | 2015-05-28 17:34:26 -0700 | [diff] [blame] | 156 | periodMs = period * 1_000; //convert to ms |
| 157 | timer.scheduleAtFixedRate(timerTask, periodMs, periodMs); |
| 158 | } |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | |
| 162 | @Override |
| 163 | public void run() { |
| 164 | try { |
| 165 | cleanup(); |
| 166 | } catch (Exception e) { |
| 167 | log.warn("Caught exception during Intent cleanup", e); |
| 168 | } |
| 169 | } |
| 170 | |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 171 | private void resubmitCorrupt(IntentData intentData, boolean checkThreshold) { |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 172 | if (checkThreshold && intentData.errorCount() >= retryThreshold) { |
| 173 | return; // threshold met or exceeded |
| 174 | } |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 175 | |
| 176 | switch (intentData.request()) { |
| 177 | case INSTALL_REQ: |
| 178 | service.submit(intentData.intent()); |
| 179 | break; |
| 180 | case WITHDRAW_REQ: |
| 181 | service.withdraw(intentData.intent()); |
| 182 | break; |
| 183 | default: |
Jonathan Hart | aae93b2 | 2015-07-22 14:59:47 -0700 | [diff] [blame] | 184 | log.warn("Trying to resubmit corrupt/failed intent {} in state {} with request {}", |
Brian O'Connor | b55d6e6 | 2015-06-01 15:25:53 -0700 | [diff] [blame] | 185 | intentData.key(), intentData.state(), intentData.request()); |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 186 | break; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | private void resubmitPendingRequest(IntentData intentData) { |
| 191 | switch (intentData.request()) { |
| 192 | case INSTALL_REQ: |
| 193 | service.submit(intentData.intent()); |
| 194 | break; |
| 195 | case WITHDRAW_REQ: |
| 196 | service.withdraw(intentData.intent()); |
| 197 | break; |
| 198 | default: |
Brian O'Connor | c90d184 | 2015-10-01 15:48:00 -0700 | [diff] [blame] | 199 | log.warn("Failed to resubmit pending intent {} in state {} with request {}", |
Brian O'Connor | b55d6e6 | 2015-06-01 15:25:53 -0700 | [diff] [blame] | 200 | intentData.key(), intentData.state(), intentData.request()); |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 201 | break; |
| 202 | } |
| 203 | } |
| 204 | |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 205 | /** |
Jonathan Hart | aae93b2 | 2015-07-22 14:59:47 -0700 | [diff] [blame] | 206 | * Iterates through corrupt, failed and pending intents and |
| 207 | * re-submit/withdraw appropriately. |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 208 | */ |
| 209 | private void cleanup() { |
Jonathan Hart | aae93b2 | 2015-07-22 14:59:47 -0700 | [diff] [blame] | 210 | int corruptCount = 0, failedCount = 0, stuckCount = 0, pendingCount = 0; |
| 211 | |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 212 | for (IntentData intentData : store.getIntentData(true, periodMs)) { |
| 213 | switch (intentData.state()) { |
Jonathan Hart | aae93b2 | 2015-07-22 14:59:47 -0700 | [diff] [blame] | 214 | case FAILED: |
| 215 | resubmitCorrupt(intentData, false); |
| 216 | failedCount++; |
| 217 | break; |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 218 | case CORRUPT: |
| 219 | resubmitCorrupt(intentData, false); |
| 220 | corruptCount++; |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 221 | break; |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 222 | case INSTALLING: //FALLTHROUGH |
| 223 | case WITHDRAWING: |
| 224 | resubmitPendingRequest(intentData); |
| 225 | stuckCount++; |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 226 | break; |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 227 | default: |
| 228 | //NOOP |
| 229 | break; |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 232 | |
| 233 | for (IntentData intentData : store.getPendingData(true, periodMs)) { |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 234 | resubmitPendingRequest(intentData); |
| 235 | stuckCount++; |
| 236 | } |
| 237 | |
Jonathan Hart | 82efa69 | 2015-10-10 18:30:28 -0700 | [diff] [blame] | 238 | if (corruptCount + failedCount + stuckCount + pendingCount > 0) { |
| 239 | log.debug("Intent cleanup ran and resubmitted {} corrupt, {} failed, {} stuck, and {} pending intents", |
| 240 | corruptCount, failedCount, stuckCount, pendingCount); |
| 241 | } |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public void event(IntentEvent event) { |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 246 | // this is the fast path for CORRUPT intents, retry on event notification. |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 247 | //TODO we might consider using the timer to back off for subsequent retries |
Brian O'Connor | 5fcf6f5 | 2015-05-28 17:34:26 -0700 | [diff] [blame] | 248 | if (enabled && event.type() == IntentEvent.Type.CORRUPT) { |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 249 | Key key = event.subject().key(); |
| 250 | if (store.isMaster(key)) { |
| 251 | IntentData data = store.getIntentData(event.subject().key()); |
| 252 | resubmitCorrupt(data, true); |
| 253 | } |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | } |