Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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.onlab.onos.store.intent.impl; |
| 17 | |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 18 | import com.codahale.metrics.Timer; |
| 19 | import com.codahale.metrics.Timer.Context; |
Yuta HIGUCHI | c8f3026 | 2014-11-20 19:14:42 -0800 | [diff] [blame] | 20 | import com.google.common.base.Verify; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import com.hazelcast.core.EntryAdapter; |
| 23 | import com.hazelcast.core.EntryEvent; |
| 24 | import com.hazelcast.core.EntryListener; |
| 25 | import com.hazelcast.core.IMap; |
| 26 | import com.hazelcast.core.Member; |
| 27 | |
| 28 | import org.apache.felix.scr.annotations.Activate; |
| 29 | import org.apache.felix.scr.annotations.Component; |
| 30 | import org.apache.felix.scr.annotations.Deactivate; |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 31 | import org.apache.felix.scr.annotations.Reference; |
| 32 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 33 | import org.apache.felix.scr.annotations.Service; |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 34 | import org.onlab.metrics.MetricsService; |
| 35 | import org.onlab.onos.core.MetricsHelper; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 36 | import org.onlab.onos.net.intent.Intent; |
| 37 | import org.onlab.onos.net.intent.IntentEvent; |
| 38 | import org.onlab.onos.net.intent.IntentId; |
| 39 | import org.onlab.onos.net.intent.IntentState; |
| 40 | import org.onlab.onos.net.intent.IntentStore; |
| 41 | import org.onlab.onos.net.intent.IntentStoreDelegate; |
| 42 | import org.onlab.onos.store.hz.AbstractHazelcastStore; |
| 43 | import org.onlab.onos.store.hz.SMap; |
| 44 | import org.onlab.onos.store.serializers.KryoNamespaces; |
| 45 | import org.onlab.onos.store.serializers.KryoSerializer; |
| 46 | import org.onlab.util.KryoNamespace; |
| 47 | import org.slf4j.Logger; |
| 48 | |
| 49 | import java.util.EnumSet; |
| 50 | import java.util.List; |
| 51 | import java.util.Map; |
| 52 | import java.util.Set; |
| 53 | import java.util.concurrent.ConcurrentHashMap; |
| 54 | |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 55 | import static com.google.common.base.Preconditions.checkState; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 56 | import static org.onlab.onos.net.intent.IntentState.*; |
| 57 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 58 | import static org.onlab.metrics.MetricsUtil.*; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 59 | |
Yuta HIGUCHI | c8f3026 | 2014-11-20 19:14:42 -0800 | [diff] [blame] | 60 | @Component(immediate = true, enabled = false) |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 61 | @Service |
| 62 | public class HazelcastIntentStore |
| 63 | extends AbstractHazelcastStore<IntentEvent, IntentStoreDelegate> |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 64 | implements IntentStore, MetricsHelper { |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 65 | |
| 66 | /** Valid parking state, which can transition to INSTALLED. */ |
Yuta HIGUCHI | 89a7f47 | 2014-11-21 14:50:24 -0800 | [diff] [blame] | 67 | private static final Set<IntentState> PRE_INSTALLED = EnumSet.of(SUBMITTED, INSTALLED, FAILED); |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 68 | |
| 69 | /** Valid parking state, which can transition to WITHDRAWN. */ |
| 70 | private static final Set<IntentState> PRE_WITHDRAWN = EnumSet.of(INSTALLED, FAILED); |
| 71 | |
| 72 | private final Logger log = getLogger(getClass()); |
| 73 | |
| 74 | // Assumption: IntentId will not have synonyms |
| 75 | private SMap<IntentId, Intent> intents; |
| 76 | private SMap<IntentId, IntentState> states; |
| 77 | |
| 78 | // Map to store instance local intermediate state transition |
| 79 | private transient Map<IntentId, IntentState> transientStates = new ConcurrentHashMap<>(); |
| 80 | |
| 81 | private SMap<IntentId, List<Intent>> installable; |
| 82 | |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 84 | protected MetricsService metricsService; |
| 85 | |
Yuta HIGUCHI | c8f3026 | 2014-11-20 19:14:42 -0800 | [diff] [blame] | 86 | // TODO make this configurable |
| 87 | private boolean onlyLogTransitionError = true; |
| 88 | |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 89 | private Timer createIntentTimer; |
| 90 | private Timer removeIntentTimer; |
| 91 | private Timer setInstallableIntentsTimer; |
| 92 | private Timer getInstallableIntentsTimer; |
| 93 | private Timer removeInstalledIntentsTimer; |
| 94 | private Timer setStateTimer; |
| 95 | private Timer getIntentCountTimer; |
| 96 | private Timer getIntentsTimer; |
| 97 | private Timer getIntentTimer; |
| 98 | private Timer getIntentStateTimer; |
| 99 | |
| 100 | private Timer createResponseTimer(String methodName) { |
| 101 | return createTimer("IntentStore", methodName, "responseTime"); |
| 102 | } |
| 103 | |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 104 | @Override |
| 105 | @Activate |
| 106 | public void activate() { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 107 | createIntentTimer = createResponseTimer("createIntent"); |
| 108 | removeIntentTimer = createResponseTimer("removeIntent"); |
| 109 | setInstallableIntentsTimer = createResponseTimer("setInstallableIntents"); |
| 110 | getInstallableIntentsTimer = createResponseTimer("getInstallableIntents"); |
| 111 | removeInstalledIntentsTimer = createResponseTimer("removeInstalledIntents"); |
| 112 | setStateTimer = createResponseTimer("setState"); |
| 113 | getIntentCountTimer = createResponseTimer("getIntentCount"); |
| 114 | getIntentsTimer = createResponseTimer("getIntents"); |
| 115 | getIntentTimer = createResponseTimer("getIntent"); |
| 116 | getIntentStateTimer = createResponseTimer("getIntentState"); |
| 117 | |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 118 | // FIXME: We need a way to add serializer for intents which has been plugged-in. |
| 119 | // As a short term workaround, relax Kryo config to |
| 120 | // registrationRequired=false |
| 121 | super.activate(); |
| 122 | super.serializer = new KryoSerializer() { |
| 123 | |
| 124 | @Override |
| 125 | protected void setupKryoPool() { |
| 126 | serializerPool = KryoNamespace.newBuilder() |
| 127 | .setRegistrationRequired(false) |
| 128 | .register(KryoNamespaces.API) |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 129 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 130 | .build(); |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | }; |
| 134 | |
| 135 | // TODO: enable near cache, allow read from backup for this IMap |
| 136 | IMap<byte[], byte[]> rawIntents = super.theInstance.getMap("intents"); |
| 137 | intents = new SMap<>(rawIntents , super.serializer); |
| 138 | |
| 139 | // TODO: disable near cache, disable read from backup for this IMap |
| 140 | IMap<byte[], byte[]> rawStates = super.theInstance.getMap("intent-states"); |
| 141 | states = new SMap<>(rawStates , super.serializer); |
| 142 | EntryListener<IntentId, IntentState> listener = new RemoteIntentStateListener(); |
| 143 | states.addEntryListener(listener , false); |
| 144 | |
| 145 | transientStates.clear(); |
| 146 | |
| 147 | // TODO: disable near cache, disable read from backup for this IMap |
| 148 | IMap<byte[], byte[]> rawInstallables = super.theInstance.getMap("installable-intents"); |
| 149 | installable = new SMap<>(rawInstallables , super.serializer); |
| 150 | |
| 151 | log.info("Started"); |
| 152 | } |
| 153 | |
| 154 | @Deactivate |
| 155 | public void deactivate() { |
| 156 | log.info("Stopped"); |
| 157 | } |
| 158 | |
| 159 | @Override |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 160 | public MetricsService metricsService() { |
| 161 | return metricsService; |
| 162 | } |
| 163 | |
| 164 | @Override |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 165 | public IntentEvent createIntent(Intent intent) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 166 | Context timer = startTimer(createIntentTimer); |
| 167 | try { |
| 168 | Intent existing = intents.putIfAbsent(intent.id(), intent); |
| 169 | if (existing != null) { |
| 170 | // duplicate, ignore |
| 171 | return null; |
| 172 | } else { |
| 173 | return this.setState(intent, IntentState.SUBMITTED); |
| 174 | } |
| 175 | } finally { |
| 176 | stopTimer(timer); |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | @Override |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 181 | public void removeIntent(IntentId intentId) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 182 | Context timer = startTimer(removeIntentTimer); |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 183 | checkState(getIntentState(intentId) == WITHDRAWN, |
| 184 | "Intent state for {} is not WITHDRAWN.", intentId); |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 185 | try { |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 186 | intents.remove(intentId); |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 187 | installable.remove(intentId); |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 188 | states.remove(intentId); |
| 189 | transientStates.remove(intentId); |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 190 | } finally { |
| 191 | stopTimer(timer); |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 192 | } |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public long getIntentCount() { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 197 | Context timer = startTimer(getIntentCountTimer); |
| 198 | try { |
| 199 | return intents.size(); |
| 200 | } finally { |
| 201 | stopTimer(timer); |
| 202 | } |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public Iterable<Intent> getIntents() { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 207 | Context timer = startTimer(getIntentsTimer); |
| 208 | try { |
| 209 | return ImmutableSet.copyOf(intents.values()); |
| 210 | } finally { |
| 211 | stopTimer(timer); |
| 212 | } |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public Intent getIntent(IntentId intentId) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 217 | Context timer = startTimer(getIntentTimer); |
| 218 | try { |
| 219 | return intents.get(intentId); |
| 220 | } finally { |
| 221 | stopTimer(timer); |
| 222 | } |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public IntentState getIntentState(IntentId id) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 227 | Context timer = startTimer(getIntentStateTimer); |
| 228 | try { |
| 229 | final IntentState localState = transientStates.get(id); |
| 230 | if (localState != null) { |
| 231 | return localState; |
| 232 | } |
| 233 | return states.get(id); |
| 234 | } finally { |
| 235 | stopTimer(timer); |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 236 | } |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Yuta HIGUCHI | c8f3026 | 2014-11-20 19:14:42 -0800 | [diff] [blame] | 239 | private void verify(boolean expression, String errorMessageTemplate, Object... errorMessageArgs) { |
| 240 | if (onlyLogTransitionError) { |
| 241 | if (!expression) { |
| 242 | log.error(errorMessageTemplate.replace("%s", "{}"), errorMessageArgs); |
| 243 | } |
| 244 | } else { |
| 245 | Verify.verify(expression, errorMessageTemplate, errorMessageArgs); |
| 246 | } |
| 247 | } |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 248 | |
| 249 | @Override |
| 250 | public IntentEvent setState(Intent intent, IntentState state) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 251 | Context timer = startTimer(setStateTimer); |
| 252 | try { |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 253 | |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 254 | final IntentId id = intent.id(); |
| 255 | IntentEvent.Type type = null; |
| 256 | final IntentState prevParking; |
| 257 | boolean transientStateChangeOnly = false; |
| 258 | |
| 259 | // parking state transition |
| 260 | switch (state) { |
| 261 | case SUBMITTED: |
| 262 | prevParking = states.get(id); |
| 263 | if (prevParking == null) { |
| 264 | IntentState existing = states.putIfAbsent(id, SUBMITTED); |
| 265 | verify(existing == null, "Conditional replace %s => %s failed", prevParking, SUBMITTED); |
| 266 | } else { |
| 267 | verify(prevParking == WITHDRAWN, |
| 268 | "Illegal state transition attempted from %s to SUBMITTED", |
| 269 | prevParking); |
| 270 | boolean updated = states.replace(id, prevParking, SUBMITTED); |
| 271 | verify(updated, "Conditional replace %s => %s failed", prevParking, SUBMITTED); |
| 272 | } |
| 273 | type = IntentEvent.Type.SUBMITTED; |
| 274 | break; |
| 275 | case INSTALLED: |
| 276 | prevParking = states.replace(id, INSTALLED); |
| 277 | verify(PRE_INSTALLED.contains(prevParking), |
| 278 | "Illegal state transition attempted from %s to INSTALLED", |
| 279 | prevParking); |
| 280 | type = IntentEvent.Type.INSTALLED; |
| 281 | break; |
| 282 | case FAILED: |
| 283 | prevParking = states.replace(id, FAILED); |
| 284 | type = IntentEvent.Type.FAILED; |
| 285 | break; |
| 286 | case WITHDRAWN: |
| 287 | prevParking = states.replace(id, WITHDRAWN); |
| 288 | verify(PRE_WITHDRAWN.contains(prevParking), |
| 289 | "Illegal state transition attempted from %s to WITHDRAWN", |
| 290 | prevParking); |
| 291 | type = IntentEvent.Type.WITHDRAWN; |
| 292 | break; |
| 293 | default: |
| 294 | transientStateChangeOnly = true; |
| 295 | prevParking = null; |
| 296 | break; |
Yuta HIGUCHI | 89a7f47 | 2014-11-21 14:50:24 -0800 | [diff] [blame] | 297 | } |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 298 | if (!transientStateChangeOnly) { |
| 299 | log.debug("Parking State change: {} {}=>{}", id, prevParking, state); |
| 300 | } |
| 301 | // Update instance local state, which includes non-parking state transition |
| 302 | final IntentState prevTransient = transientStates.put(id, state); |
| 303 | log.debug("Transient State change: {} {}=>{}", id, prevTransient, state); |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 304 | |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 305 | if (type == null) { |
| 306 | return null; |
| 307 | } |
| 308 | return new IntentEvent(type, intent); |
| 309 | } finally { |
| 310 | stopTimer(timer); |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 311 | } |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | @Override |
| 315 | public void setInstallableIntents(IntentId intentId, List<Intent> result) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 316 | Context timer = startTimer(setInstallableIntentsTimer); |
| 317 | try { |
| 318 | installable.put(intentId, result); |
| 319 | } finally { |
| 320 | stopTimer(timer); |
| 321 | } |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | @Override |
| 325 | public List<Intent> getInstallableIntents(IntentId intentId) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 326 | Context timer = startTimer(getInstallableIntentsTimer); |
| 327 | try { |
| 328 | return installable.get(intentId); |
| 329 | } finally { |
| 330 | stopTimer(timer); |
| 331 | } |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | @Override |
| 335 | public void removeInstalledIntents(IntentId intentId) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 336 | Context timer = startTimer(removeInstalledIntentsTimer); |
| 337 | try { |
| 338 | installable.remove(intentId); |
| 339 | } finally { |
| 340 | stopTimer(timer); |
| 341 | } |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | public final class RemoteIntentStateListener extends EntryAdapter<IntentId, IntentState> { |
| 345 | |
| 346 | @Override |
| 347 | public void onEntryEvent(EntryEvent<IntentId, IntentState> event) { |
| 348 | final Member myself = theInstance.getCluster().getLocalMember(); |
| 349 | if (!myself.equals(event.getMember())) { |
| 350 | // When Intent state was modified by remote node, |
| 351 | // clear local transient state. |
| 352 | final IntentId intentId = event.getKey(); |
| 353 | IntentState oldState = transientStates.remove(intentId); |
| 354 | if (oldState != null) { |
| 355 | log.debug("{} state updated remotely, removing transient state {}", |
| 356 | intentId, oldState); |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | } |