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 | |
| 64 | @Property(name = "period", intValue = DEFAULT_PERIOD, |
| 65 | label = "Frequency in ms between cleanup runs") |
| 66 | protected int period = DEFAULT_PERIOD; |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 67 | private long periodMs; |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 68 | |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 69 | @Property(name = "retryThreshold", intValue = DEFAULT_THRESHOLD, |
| 70 | label = "Number of times to retry CORRUPT intent without delay") |
Brian O'Connor | cdec493 | 2015-04-30 16:16:47 -0700 | [diff] [blame] | 71 | protected int retryThreshold = DEFAULT_THRESHOLD; |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 72 | |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 74 | protected IntentService service; |
| 75 | |
| 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 77 | protected IntentStore store; |
| 78 | |
| 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 80 | protected ComponentConfigService cfgService; |
| 81 | |
| 82 | private ExecutorService executor; |
| 83 | private Timer timer; |
| 84 | private TimerTask timerTask; |
| 85 | |
| 86 | @Activate |
| 87 | public void activate() { |
| 88 | cfgService.registerProperties(getClass()); |
| 89 | executor = newSingleThreadExecutor(groupedThreads("onos/intent", "cleanup")); |
| 90 | timer = new Timer("onos-intent-cleanup-timer"); |
| 91 | service.addListener(this); |
| 92 | adjustRate(); |
| 93 | log.info("Started"); |
| 94 | } |
| 95 | |
| 96 | @Deactivate |
| 97 | public void deactivate() { |
| 98 | cfgService.unregisterProperties(getClass(), false); |
| 99 | service.removeListener(this); |
| 100 | timer.cancel(); |
| 101 | timerTask = null; |
| 102 | executor.shutdown(); |
| 103 | log.info("Stopped"); |
| 104 | } |
| 105 | |
| 106 | @Modified |
| 107 | public void modified(ComponentContext context) { |
| 108 | Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties(); |
| 109 | |
| 110 | int newPeriod; |
| 111 | try { |
| 112 | String s = get(properties, "period"); |
| 113 | newPeriod = isNullOrEmpty(s) ? period : Integer.parseInt(s.trim()); |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 114 | |
| 115 | s = get(properties, "retryThreshold"); |
| 116 | retryThreshold = isNullOrEmpty(s) ? period : Integer.parseInt(s.trim()); |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 117 | } catch (NumberFormatException e) { |
| 118 | log.warn(e.getMessage()); |
| 119 | newPeriod = period; |
| 120 | } |
| 121 | |
| 122 | // Any change in the following parameters implies hard restart |
| 123 | if (newPeriod != period) { |
| 124 | period = newPeriod; |
| 125 | adjustRate(); |
| 126 | } |
| 127 | |
| 128 | log.info("Settings: period={}", period); |
| 129 | } |
| 130 | |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 131 | protected void adjustRate() { |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 132 | if (timerTask != null) { |
| 133 | timerTask.cancel(); |
| 134 | } |
| 135 | |
| 136 | timerTask = new TimerTask() { |
| 137 | @Override |
| 138 | public void run() { |
| 139 | executor.submit(IntentCleanup.this); |
| 140 | } |
| 141 | }; |
| 142 | |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 143 | periodMs = period * 1_000; //convert to ms |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 144 | timer.scheduleAtFixedRate(timerTask, periodMs, periodMs); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | @Override |
| 149 | public void run() { |
| 150 | try { |
| 151 | cleanup(); |
| 152 | } catch (Exception e) { |
| 153 | log.warn("Caught exception during Intent cleanup", e); |
| 154 | } |
| 155 | } |
| 156 | |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 157 | private void resubmitCorrupt(IntentData intentData, boolean checkThreshold) { |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 158 | if (checkThreshold && intentData.errorCount() >= retryThreshold) { |
| 159 | return; // threshold met or exceeded |
| 160 | } |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 161 | |
| 162 | switch (intentData.request()) { |
| 163 | case INSTALL_REQ: |
| 164 | service.submit(intentData.intent()); |
| 165 | break; |
| 166 | case WITHDRAW_REQ: |
| 167 | service.withdraw(intentData.intent()); |
| 168 | break; |
| 169 | default: |
| 170 | //TODO this is an error, might want to log it |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | private void resubmitPendingRequest(IntentData intentData) { |
| 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: |
| 184 | //TODO this is an error (or could be purge), might want to log it |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 189 | /** |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 190 | * Iterate through CORRUPT intents and re-submit/withdraw appropriately. |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 191 | * |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 192 | */ |
| 193 | private void cleanup() { |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 194 | int corruptCount = 0, stuckCount = 0, pendingCount = 0; |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 195 | store.getIntentData(true, periodMs); |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 196 | for (IntentData intentData : store.getIntentData(true, periodMs)) { |
| 197 | switch (intentData.state()) { |
| 198 | case CORRUPT: |
| 199 | resubmitCorrupt(intentData, false); |
| 200 | corruptCount++; |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 201 | break; |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 202 | case INSTALLING: //FALLTHROUGH |
| 203 | case WITHDRAWING: |
| 204 | resubmitPendingRequest(intentData); |
| 205 | stuckCount++; |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 206 | break; |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 207 | default: |
| 208 | //NOOP |
| 209 | break; |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 212 | |
| 213 | for (IntentData intentData : store.getPendingData(true, periodMs)) { |
| 214 | //TODO should we do age check here, or in the store? |
| 215 | resubmitPendingRequest(intentData); |
| 216 | stuckCount++; |
| 217 | } |
| 218 | |
| 219 | log.debug("Intent cleanup ran and resubmitted {} corrupt, {} stuck, and {} pending intents", |
| 220 | corruptCount, stuckCount, pendingCount); |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public void event(IntentEvent event) { |
Brian O'Connor | 6d8e317 | 2015-04-30 15:43:57 -0700 | [diff] [blame] | 225 | // 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] | 226 | //TODO we might consider using the timer to back off for subsequent retries |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 227 | if (event.type() == IntentEvent.Type.CORRUPT) { |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 228 | Key key = event.subject().key(); |
| 229 | if (store.isMaster(key)) { |
| 230 | IntentData data = store.getIntentData(event.subject().key()); |
| 231 | resubmitCorrupt(data, true); |
| 232 | } |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | } |