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 | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableList; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableSet; |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 23 | import com.google.common.collect.Lists; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 24 | import com.hazelcast.core.EntryAdapter; |
| 25 | import com.hazelcast.core.EntryEvent; |
| 26 | import com.hazelcast.core.EntryListener; |
| 27 | import com.hazelcast.core.IMap; |
| 28 | import com.hazelcast.core.Member; |
| 29 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 30 | import org.apache.commons.lang3.tuple.Pair; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 31 | import org.apache.felix.scr.annotations.Activate; |
| 32 | import org.apache.felix.scr.annotations.Component; |
| 33 | import org.apache.felix.scr.annotations.Deactivate; |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 34 | import org.apache.felix.scr.annotations.Reference; |
| 35 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 36 | import org.apache.felix.scr.annotations.Service; |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 37 | import org.onlab.metrics.MetricsService; |
| 38 | import org.onlab.onos.core.MetricsHelper; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 39 | import org.onlab.onos.net.intent.Intent; |
| 40 | import org.onlab.onos.net.intent.IntentEvent; |
| 41 | import org.onlab.onos.net.intent.IntentId; |
| 42 | import org.onlab.onos.net.intent.IntentState; |
| 43 | import org.onlab.onos.net.intent.IntentStore; |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 44 | import org.onlab.onos.net.intent.IntentStore.BatchWrite.Operation; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 45 | import org.onlab.onos.net.intent.IntentStoreDelegate; |
| 46 | import org.onlab.onos.store.hz.AbstractHazelcastStore; |
| 47 | import org.onlab.onos.store.hz.SMap; |
| 48 | import org.onlab.onos.store.serializers.KryoNamespaces; |
| 49 | import org.onlab.onos.store.serializers.KryoSerializer; |
| 50 | import org.onlab.util.KryoNamespace; |
| 51 | import org.slf4j.Logger; |
| 52 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 53 | import java.util.ArrayList; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 54 | import java.util.EnumSet; |
| 55 | import java.util.List; |
| 56 | import java.util.Map; |
| 57 | import java.util.Set; |
| 58 | import java.util.concurrent.ConcurrentHashMap; |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 59 | import java.util.concurrent.ExecutionException; |
| 60 | import java.util.concurrent.Future; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 61 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 62 | import static com.google.common.base.Preconditions.checkArgument; |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 63 | import static com.google.common.base.Preconditions.checkState; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 64 | import static org.onlab.onos.net.intent.IntentState.*; |
| 65 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 66 | import static org.onlab.metrics.MetricsUtil.*; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 67 | |
Yuta HIGUCHI | 9b108b3 | 2014-12-01 11:10:26 -0800 | [diff] [blame] | 68 | @Component(immediate = true, enabled = true) |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 69 | @Service |
| 70 | public class HazelcastIntentStore |
| 71 | extends AbstractHazelcastStore<IntentEvent, IntentStoreDelegate> |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 72 | implements IntentStore, MetricsHelper { |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 73 | |
| 74 | /** Valid parking state, which can transition to INSTALLED. */ |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 75 | private static final Set<IntentState> PRE_INSTALLED = EnumSet.of(INSTALL_REQ, INSTALLED, FAILED); |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 76 | |
| 77 | /** Valid parking state, which can transition to WITHDRAWN. */ |
| 78 | private static final Set<IntentState> PRE_WITHDRAWN = EnumSet.of(INSTALLED, FAILED); |
| 79 | |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 80 | private static final Set<IntentState> PARKING = EnumSet.of(INSTALL_REQ, INSTALLED, WITHDRAWN, FAILED); |
Yuta HIGUCHI | f568245 | 2014-12-01 10:17:15 -0800 | [diff] [blame] | 81 | |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 82 | private final Logger log = getLogger(getClass()); |
| 83 | |
| 84 | // Assumption: IntentId will not have synonyms |
| 85 | private SMap<IntentId, Intent> intents; |
| 86 | private SMap<IntentId, IntentState> states; |
| 87 | |
| 88 | // Map to store instance local intermediate state transition |
| 89 | private transient Map<IntentId, IntentState> transientStates = new ConcurrentHashMap<>(); |
| 90 | |
| 91 | private SMap<IntentId, List<Intent>> installable; |
| 92 | |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 94 | protected MetricsService metricsService; |
| 95 | |
Yuta HIGUCHI | c8f3026 | 2014-11-20 19:14:42 -0800 | [diff] [blame] | 96 | // TODO make this configurable |
| 97 | private boolean onlyLogTransitionError = true; |
| 98 | |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 99 | private Timer createIntentTimer; |
| 100 | private Timer removeIntentTimer; |
| 101 | private Timer setInstallableIntentsTimer; |
| 102 | private Timer getInstallableIntentsTimer; |
| 103 | private Timer removeInstalledIntentsTimer; |
| 104 | private Timer setStateTimer; |
| 105 | private Timer getIntentCountTimer; |
| 106 | private Timer getIntentsTimer; |
| 107 | private Timer getIntentTimer; |
| 108 | private Timer getIntentStateTimer; |
| 109 | |
| 110 | private Timer createResponseTimer(String methodName) { |
| 111 | return createTimer("IntentStore", methodName, "responseTime"); |
| 112 | } |
| 113 | |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 114 | @Override |
| 115 | @Activate |
| 116 | public void activate() { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 117 | createIntentTimer = createResponseTimer("createIntent"); |
| 118 | removeIntentTimer = createResponseTimer("removeIntent"); |
| 119 | setInstallableIntentsTimer = createResponseTimer("setInstallableIntents"); |
| 120 | getInstallableIntentsTimer = createResponseTimer("getInstallableIntents"); |
| 121 | removeInstalledIntentsTimer = createResponseTimer("removeInstalledIntents"); |
| 122 | setStateTimer = createResponseTimer("setState"); |
| 123 | getIntentCountTimer = createResponseTimer("getIntentCount"); |
| 124 | getIntentsTimer = createResponseTimer("getIntents"); |
| 125 | getIntentTimer = createResponseTimer("getIntent"); |
| 126 | getIntentStateTimer = createResponseTimer("getIntentState"); |
| 127 | |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 128 | // FIXME: We need a way to add serializer for intents which has been plugged-in. |
| 129 | // As a short term workaround, relax Kryo config to |
| 130 | // registrationRequired=false |
| 131 | super.activate(); |
| 132 | super.serializer = new KryoSerializer() { |
| 133 | |
| 134 | @Override |
| 135 | protected void setupKryoPool() { |
| 136 | serializerPool = KryoNamespace.newBuilder() |
| 137 | .setRegistrationRequired(false) |
| 138 | .register(KryoNamespaces.API) |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 139 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 140 | .build(); |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | }; |
| 144 | |
| 145 | // TODO: enable near cache, allow read from backup for this IMap |
| 146 | IMap<byte[], byte[]> rawIntents = super.theInstance.getMap("intents"); |
| 147 | intents = new SMap<>(rawIntents , super.serializer); |
| 148 | |
| 149 | // TODO: disable near cache, disable read from backup for this IMap |
| 150 | IMap<byte[], byte[]> rawStates = super.theInstance.getMap("intent-states"); |
| 151 | states = new SMap<>(rawStates , super.serializer); |
| 152 | EntryListener<IntentId, IntentState> listener = new RemoteIntentStateListener(); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 153 | states.addEntryListener(listener , true); |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 154 | |
| 155 | transientStates.clear(); |
| 156 | |
| 157 | // TODO: disable near cache, disable read from backup for this IMap |
| 158 | IMap<byte[], byte[]> rawInstallables = super.theInstance.getMap("installable-intents"); |
| 159 | installable = new SMap<>(rawInstallables , super.serializer); |
| 160 | |
| 161 | log.info("Started"); |
| 162 | } |
| 163 | |
| 164 | @Deactivate |
| 165 | public void deactivate() { |
| 166 | log.info("Stopped"); |
| 167 | } |
| 168 | |
| 169 | @Override |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 170 | public MetricsService metricsService() { |
| 171 | return metricsService; |
| 172 | } |
| 173 | |
| 174 | @Override |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 175 | public void createIntent(Intent intent) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 176 | Context timer = startTimer(createIntentTimer); |
| 177 | try { |
| 178 | Intent existing = intents.putIfAbsent(intent.id(), intent); |
| 179 | if (existing != null) { |
| 180 | // duplicate, ignore |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 181 | return; |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 182 | } else { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 183 | this.setState(intent, IntentState.INSTALL_REQ); |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 184 | return; |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 185 | } |
| 186 | } finally { |
| 187 | stopTimer(timer); |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | @Override |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 192 | public void removeIntent(IntentId intentId) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 193 | Context timer = startTimer(removeIntentTimer); |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 194 | checkState(getIntentState(intentId) == WITHDRAWN, |
| 195 | "Intent state for {} is not WITHDRAWN.", intentId); |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 196 | try { |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 197 | intents.remove(intentId); |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 198 | installable.remove(intentId); |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 199 | states.remove(intentId); |
| 200 | transientStates.remove(intentId); |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 201 | } finally { |
| 202 | stopTimer(timer); |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 203 | } |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public long getIntentCount() { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 208 | Context timer = startTimer(getIntentCountTimer); |
| 209 | try { |
| 210 | return intents.size(); |
| 211 | } finally { |
| 212 | stopTimer(timer); |
| 213 | } |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public Iterable<Intent> getIntents() { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 218 | Context timer = startTimer(getIntentsTimer); |
| 219 | try { |
| 220 | return ImmutableSet.copyOf(intents.values()); |
| 221 | } finally { |
| 222 | stopTimer(timer); |
| 223 | } |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public Intent getIntent(IntentId intentId) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 228 | Context timer = startTimer(getIntentTimer); |
| 229 | try { |
| 230 | return intents.get(intentId); |
| 231 | } finally { |
| 232 | stopTimer(timer); |
| 233 | } |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public IntentState getIntentState(IntentId id) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 238 | Context timer = startTimer(getIntentStateTimer); |
| 239 | try { |
| 240 | final IntentState localState = transientStates.get(id); |
| 241 | if (localState != null) { |
| 242 | return localState; |
| 243 | } |
| 244 | return states.get(id); |
| 245 | } finally { |
| 246 | stopTimer(timer); |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 247 | } |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Yuta HIGUCHI | c8f3026 | 2014-11-20 19:14:42 -0800 | [diff] [blame] | 250 | private void verify(boolean expression, String errorMessageTemplate, Object... errorMessageArgs) { |
| 251 | if (onlyLogTransitionError) { |
| 252 | if (!expression) { |
| 253 | log.error(errorMessageTemplate.replace("%s", "{}"), errorMessageArgs); |
| 254 | } |
| 255 | } else { |
| 256 | Verify.verify(expression, errorMessageTemplate, errorMessageArgs); |
| 257 | } |
| 258 | } |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 259 | |
| 260 | @Override |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 261 | public void setState(Intent intent, IntentState state) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 262 | Context timer = startTimer(setStateTimer); |
| 263 | try { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 264 | final IntentId id = intent.id(); |
| 265 | IntentEvent.Type type = null; |
| 266 | final IntentState prevParking; |
| 267 | boolean transientStateChangeOnly = false; |
| 268 | |
| 269 | // parking state transition |
| 270 | switch (state) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 271 | case INSTALL_REQ: |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 272 | prevParking = states.get(id); |
| 273 | if (prevParking == null) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 274 | IntentState existing = states.putIfAbsent(id, INSTALL_REQ); |
| 275 | verify(existing == null, "Conditional replace %s => %s failed", prevParking, INSTALL_REQ); |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 276 | } else { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 277 | verify(PRE_INSTALLED.contains(prevParking), |
| 278 | "Illegal state transition attempted from %s to INSTALL_REQ", |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 279 | prevParking); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 280 | boolean updated = states.replace(id, prevParking, INSTALL_REQ); |
| 281 | verify(updated, "Conditional replace %s => %s failed", prevParking, INSTALL_REQ); |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 282 | } |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 283 | type = IntentEvent.Type.INSTALL_REQ; |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 284 | break; |
| 285 | case INSTALLED: |
| 286 | prevParking = states.replace(id, INSTALLED); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 287 | verify(prevParking == INSTALL_REQ, |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 288 | "Illegal state transition attempted from %s to INSTALLED", |
| 289 | prevParking); |
| 290 | type = IntentEvent.Type.INSTALLED; |
| 291 | break; |
| 292 | case FAILED: |
| 293 | prevParking = states.replace(id, FAILED); |
| 294 | type = IntentEvent.Type.FAILED; |
| 295 | break; |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 296 | case WITHDRAW_REQ: |
| 297 | prevParking = states.replace(id, WITHDRAW_REQ); |
| 298 | verify(PRE_WITHDRAWN.contains(prevParking), |
| 299 | "Illegal state transition attempted from %s to WITHDRAW_REQ", |
| 300 | prevParking); |
| 301 | type = IntentEvent.Type.WITHDRAW_REQ; |
| 302 | break; |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 303 | case WITHDRAWN: |
| 304 | prevParking = states.replace(id, WITHDRAWN); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 305 | verify(prevParking == WITHDRAW_REQ, |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 306 | "Illegal state transition attempted from %s to WITHDRAWN", |
| 307 | prevParking); |
| 308 | type = IntentEvent.Type.WITHDRAWN; |
| 309 | break; |
| 310 | default: |
| 311 | transientStateChangeOnly = true; |
| 312 | prevParking = null; |
| 313 | break; |
Yuta HIGUCHI | 89a7f47 | 2014-11-21 14:50:24 -0800 | [diff] [blame] | 314 | } |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 315 | if (!transientStateChangeOnly) { |
| 316 | log.debug("Parking State change: {} {}=>{}", id, prevParking, state); |
| 317 | } |
| 318 | // Update instance local state, which includes non-parking state transition |
| 319 | final IntentState prevTransient = transientStates.put(id, state); |
| 320 | log.debug("Transient State change: {} {}=>{}", id, prevTransient, state); |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 321 | |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 322 | if (type != null) { |
| 323 | notifyDelegate(new IntentEvent(type, intent)); |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 324 | } |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 325 | } finally { |
| 326 | stopTimer(timer); |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 327 | } |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | @Override |
| 331 | public void setInstallableIntents(IntentId intentId, List<Intent> result) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 332 | Context timer = startTimer(setInstallableIntentsTimer); |
| 333 | try { |
| 334 | installable.put(intentId, result); |
| 335 | } finally { |
| 336 | stopTimer(timer); |
| 337 | } |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | @Override |
| 341 | public List<Intent> getInstallableIntents(IntentId intentId) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 342 | Context timer = startTimer(getInstallableIntentsTimer); |
| 343 | try { |
| 344 | return installable.get(intentId); |
| 345 | } finally { |
| 346 | stopTimer(timer); |
| 347 | } |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | @Override |
| 351 | public void removeInstalledIntents(IntentId intentId) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 352 | Context timer = startTimer(removeInstalledIntentsTimer); |
| 353 | try { |
| 354 | installable.remove(intentId); |
| 355 | } finally { |
| 356 | stopTimer(timer); |
| 357 | } |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Yuta HIGUCHI | f568245 | 2014-12-01 10:17:15 -0800 | [diff] [blame] | 360 | // TODO slice out methods after merging Ali's patch |
| 361 | // CHECKSTYLE IGNORE MethodLength FOR NEXT 1 LINES |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 362 | @Override |
| 363 | public List<Operation> batchWrite(BatchWrite batch) { |
| 364 | // Hazelcast version will never fail for conditional failure now. |
| 365 | List<Operation> failed = new ArrayList<>(); |
| 366 | |
| 367 | List<Pair<Operation, List<Future<?>>>> futures = new ArrayList<>(batch.operations().size()); |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 368 | List<IntentEvent> events = Lists.newArrayList(); |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 369 | |
| 370 | for (Operation op : batch.operations()) { |
| 371 | switch (op.type()) { |
| 372 | case CREATE_INTENT: |
| 373 | checkArgument(op.args().size() == 1, |
| 374 | "CREATE_INTENT takes 1 argument. %s", op); |
| 375 | Intent intent = op.arg(0); |
| 376 | futures.add(Pair.of(op, |
| 377 | ImmutableList.of(intents.putAsync(intent.id(), intent), |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 378 | states.putAsync(intent.id(), INSTALL_REQ)))); |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 379 | break; |
| 380 | |
| 381 | case REMOVE_INTENT: |
| 382 | checkArgument(op.args().size() == 1, |
| 383 | "REMOVE_INTENT takes 1 argument. %s", op); |
| 384 | IntentId intentId = (IntentId) op.arg(0); |
| 385 | futures.add(Pair.of(op, |
| 386 | ImmutableList.of(intents.removeAsync(intentId), |
| 387 | states.removeAsync(intentId), |
| 388 | installable.removeAsync(intentId)))); |
| 389 | break; |
| 390 | |
| 391 | case SET_STATE: |
| 392 | checkArgument(op.args().size() == 2, |
| 393 | "SET_STATE takes 2 arguments. %s", op); |
| 394 | intent = op.arg(0); |
| 395 | IntentState newState = op.arg(1); |
| 396 | futures.add(Pair.of(op, |
| 397 | ImmutableList.of(states.putAsync(intent.id(), newState)))); |
| 398 | break; |
| 399 | |
| 400 | case SET_INSTALLABLE: |
| 401 | checkArgument(op.args().size() == 2, |
| 402 | "SET_INSTALLABLE takes 2 arguments. %s", op); |
| 403 | intentId = op.arg(0); |
| 404 | List<Intent> installableIntents = op.arg(1); |
| 405 | futures.add(Pair.of(op, |
| 406 | ImmutableList.of(installable.putAsync(intentId, installableIntents)))); |
| 407 | break; |
| 408 | |
| 409 | case REMOVE_INSTALLED: |
| 410 | checkArgument(op.args().size() == 1, |
| 411 | "REMOVE_INSTALLED takes 1 argument. %s", op); |
| 412 | intentId = op.arg(0); |
| 413 | futures.add(Pair.of(op, |
| 414 | ImmutableList.of(installable.removeAsync(intentId)))); |
| 415 | break; |
| 416 | |
| 417 | default: |
| 418 | log.warn("Unknown Operation encountered: {}", op); |
| 419 | failed.add(op); |
| 420 | break; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | // verify result |
| 425 | for (Pair<Operation, List<Future<?>>> future : futures) { |
| 426 | final Operation op = future.getLeft(); |
| 427 | final List<Future<?>> subops = future.getRight(); |
| 428 | |
| 429 | switch (op.type()) { |
| 430 | |
| 431 | case CREATE_INTENT: |
| 432 | { |
| 433 | Intent intent = op.arg(0); |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 434 | IntentState newIntentState = INSTALL_REQ; |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 435 | |
| 436 | try { |
| 437 | Intent prevIntent = (Intent) subops.get(0).get(); |
| 438 | IntentState prevIntentState = (IntentState) subops.get(1).get(); |
| 439 | |
| 440 | if (prevIntent != null || prevIntentState != null) { |
| 441 | log.warn("Overwriting existing Intent: {}@{} with {}@{}", |
| 442 | prevIntent, prevIntentState, |
| 443 | intent, newIntentState); |
| 444 | } |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 445 | events.add(IntentEvent.getEvent(INSTALL_REQ, intent)); |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 446 | } catch (InterruptedException e) { |
| 447 | log.error("Batch write was interrupted while processing {}", op, e); |
| 448 | failed.add(op); |
| 449 | Thread.currentThread().interrupt(); |
| 450 | } catch (ExecutionException e) { |
| 451 | log.error("Batch write failed processing {}", op, e); |
| 452 | failed.add(op); |
| 453 | } |
| 454 | break; |
| 455 | } |
| 456 | |
| 457 | case REMOVE_INTENT: |
| 458 | { |
| 459 | IntentId intentId = op.arg(0); |
| 460 | |
| 461 | try { |
| 462 | Intent prevIntent = (Intent) subops.get(0).get(); |
| 463 | IntentState prevIntentState = (IntentState) subops.get(1).get(); |
| 464 | @SuppressWarnings("unchecked") |
| 465 | List<Intent> prevInstallable = (List<Intent>) subops.get(2).get(); |
| 466 | |
| 467 | if (prevIntent == null) { |
| 468 | log.warn("Intent {} was already removed.", intentId); |
| 469 | } |
| 470 | if (prevIntentState == null) { |
| 471 | log.warn("Intent {} state was already removed", intentId); |
| 472 | } |
| 473 | if (prevInstallable == null) { |
| 474 | log.info("Intent {} installable was already removed", intentId); |
| 475 | } |
| 476 | } catch (InterruptedException e) { |
| 477 | log.error("Batch write was interrupted while processing {}", op, e); |
| 478 | failed.add(op); |
| 479 | Thread.currentThread().interrupt(); |
| 480 | } catch (ExecutionException e) { |
| 481 | log.error("Batch write failed processing {}", op, e); |
| 482 | failed.add(op); |
| 483 | } |
| 484 | break; |
| 485 | } |
| 486 | |
| 487 | case SET_STATE: |
| 488 | { |
| 489 | Intent intent = op.arg(0); |
| 490 | IntentId intentId = intent.id(); |
| 491 | IntentState newState = op.arg(1); |
| 492 | |
| 493 | try { |
| 494 | IntentState prevIntentState = (IntentState) subops.get(0).get(); |
Yuta HIGUCHI | f568245 | 2014-12-01 10:17:15 -0800 | [diff] [blame] | 495 | |
| 496 | if (PARKING.contains(newState)) { |
| 497 | transientStates.remove(intentId); |
Yuta HIGUCHI | 5cd352d | 2014-12-01 20:16:02 -0800 | [diff] [blame] | 498 | events.add(IntentEvent.getEvent(newState, intent)); |
Yuta HIGUCHI | f568245 | 2014-12-01 10:17:15 -0800 | [diff] [blame] | 499 | } |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 500 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 501 | log.trace("{} - {} -> {}", intentId, prevIntentState, newState); |
| 502 | // TODO sanity check and log? |
| 503 | } catch (InterruptedException e) { |
| 504 | log.error("Batch write was interrupted while processing {}", op, e); |
| 505 | failed.add(op); |
| 506 | Thread.currentThread().interrupt(); |
| 507 | } catch (ExecutionException e) { |
| 508 | log.error("Batch write failed processing {}", op, e); |
| 509 | failed.add(op); |
| 510 | } |
| 511 | break; |
| 512 | } |
| 513 | |
| 514 | case SET_INSTALLABLE: |
| 515 | { |
| 516 | IntentId intentId = op.arg(0); |
| 517 | List<Intent> installableIntents = op.arg(1); |
| 518 | |
| 519 | try { |
| 520 | @SuppressWarnings("unchecked") |
| 521 | List<Intent> prevInstallable = (List<Intent>) subops.get(0).get(); |
| 522 | |
| 523 | if (prevInstallable != null) { |
| 524 | log.warn("Overwriting Intent {} installable {} -> {}", |
| 525 | intentId, prevInstallable, installableIntents); |
| 526 | } |
| 527 | } catch (InterruptedException e) { |
| 528 | log.error("Batch write was interrupted while processing {}", op, e); |
| 529 | failed.add(op); |
| 530 | Thread.currentThread().interrupt(); |
| 531 | } catch (ExecutionException e) { |
| 532 | log.error("Batch write failed processing {}", op, e); |
| 533 | failed.add(op); |
| 534 | } |
| 535 | break; |
| 536 | } |
| 537 | |
| 538 | case REMOVE_INSTALLED: |
| 539 | { |
| 540 | IntentId intentId = op.arg(0); |
| 541 | |
| 542 | try { |
| 543 | @SuppressWarnings("unchecked") |
| 544 | List<Intent> prevInstallable = (List<Intent>) subops.get(0).get(); |
| 545 | |
| 546 | if (prevInstallable == null) { |
| 547 | log.warn("Intent {} installable was already removed", intentId); |
| 548 | } |
| 549 | } catch (InterruptedException e) { |
| 550 | log.error("Batch write was interrupted while processing {}", op, e); |
| 551 | failed.add(op); |
| 552 | Thread.currentThread().interrupt(); |
| 553 | } catch (ExecutionException e) { |
| 554 | log.error("Batch write failed processing {}", op, e); |
| 555 | failed.add(op); |
| 556 | } |
| 557 | break; |
| 558 | } |
| 559 | |
| 560 | default: |
| 561 | log.warn("Unknown Operation encountered: {}", op); |
| 562 | if (!failed.contains(op)) { |
| 563 | failed.add(op); |
| 564 | } |
| 565 | break; |
| 566 | } |
| 567 | } |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 568 | |
| 569 | notifyDelegate(events); |
| 570 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 571 | return failed; |
| 572 | } |
| 573 | |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 574 | public final class RemoteIntentStateListener extends EntryAdapter<IntentId, IntentState> { |
| 575 | |
| 576 | @Override |
| 577 | public void onEntryEvent(EntryEvent<IntentId, IntentState> event) { |
| 578 | final Member myself = theInstance.getCluster().getLocalMember(); |
| 579 | if (!myself.equals(event.getMember())) { |
| 580 | // When Intent state was modified by remote node, |
| 581 | // clear local transient state. |
| 582 | final IntentId intentId = event.getKey(); |
| 583 | IntentState oldState = transientStates.remove(intentId); |
| 584 | if (oldState != null) { |
| 585 | log.debug("{} state updated remotely, removing transient state {}", |
| 586 | intentId, oldState); |
| 587 | } |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 588 | |
Yuta HIGUCHI | 8837578 | 2014-12-02 14:21:17 -0800 | [diff] [blame^] | 589 | if (event.getValue() != null) { |
| 590 | // notify if this is not entry removed event |
| 591 | notifyDelegate(IntentEvent.getEvent(event.getValue(), getIntent(intentId))); |
| 592 | } |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | } |
| 596 | } |