Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [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 | */ |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 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.cache.CacheBuilder; |
| 22 | import com.google.common.cache.CacheLoader; |
| 23 | import com.google.common.cache.LoadingCache; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 24 | import com.google.common.collect.ImmutableSet; |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 25 | |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 31 | import org.apache.felix.scr.annotations.Service; |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 32 | import org.onlab.metrics.MetricsService; |
| 33 | import org.onlab.onos.core.MetricsHelper; |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.intent.Intent; |
| 35 | import org.onlab.onos.net.intent.IntentEvent; |
| 36 | import org.onlab.onos.net.intent.IntentId; |
| 37 | import org.onlab.onos.net.intent.IntentState; |
| 38 | import org.onlab.onos.net.intent.IntentStore; |
| 39 | import org.onlab.onos.net.intent.IntentStoreDelegate; |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 40 | import org.onlab.onos.net.intent.IntentStore.BatchWrite.Operation; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 41 | import org.onlab.onos.store.AbstractStore; |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 42 | import org.onlab.onos.store.serializers.KryoNamespaces; |
| 43 | import org.onlab.onos.store.serializers.KryoSerializer; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 44 | import org.onlab.onos.store.serializers.StoreSerializer; |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 45 | import org.onlab.onos.store.service.BatchWriteRequest; |
| 46 | import org.onlab.onos.store.service.BatchWriteRequest.Builder; |
| 47 | import org.onlab.onos.store.service.BatchWriteResult; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 48 | import org.onlab.onos.store.service.DatabaseAdminService; |
| 49 | import org.onlab.onos.store.service.DatabaseService; |
| 50 | import org.onlab.onos.store.service.impl.CMap; |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 51 | import org.onlab.util.KryoNamespace; |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 52 | import org.slf4j.Logger; |
| 53 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 54 | import java.util.ArrayList; |
Yuta HIGUCHI | 9a2e18a | 2014-11-18 16:41:58 -0800 | [diff] [blame] | 55 | import java.util.EnumSet; |
Yuta HIGUCHI | f568245 | 2014-12-01 10:17:15 -0800 | [diff] [blame] | 56 | import java.util.HashSet; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 57 | import java.util.List; |
| 58 | import java.util.Map; |
Yuta HIGUCHI | 9a2e18a | 2014-11-18 16:41:58 -0800 | [diff] [blame] | 59 | import java.util.Set; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 60 | import java.util.concurrent.ConcurrentHashMap; |
| 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; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [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 | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 67 | |
Yuta HIGUCHI | 9b108b3 | 2014-12-01 11:10:26 -0800 | [diff] [blame^] | 68 | @Component(immediate = true, enabled = false) |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 69 | @Service |
| 70 | public class DistributedIntentStore |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 71 | extends AbstractStore<IntentEvent, IntentStoreDelegate> |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 72 | implements IntentStore, MetricsHelper { |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 73 | |
Yuta HIGUCHI | 9a2e18a | 2014-11-18 16:41:58 -0800 | [diff] [blame] | 74 | /** Valid parking state, which can transition to INSTALLED. */ |
Yuta HIGUCHI | 89a7f47 | 2014-11-21 14:50:24 -0800 | [diff] [blame] | 75 | private static final Set<IntentState> PRE_INSTALLED = EnumSet.of(SUBMITTED, INSTALLED, FAILED); |
Yuta HIGUCHI | 9a2e18a | 2014-11-18 16:41:58 -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 | |
Yuta HIGUCHI | f568245 | 2014-12-01 10:17:15 -0800 | [diff] [blame] | 80 | private static final Set<IntentState> PARKING = EnumSet.of(SUBMITTED, INSTALLED, WITHDRAWN, FAILED); |
| 81 | |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 82 | private final Logger log = getLogger(getClass()); |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 83 | |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 84 | // Assumption: IntentId will not have synonyms |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 85 | private static final String INTENTS_TABLE = "intents"; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 86 | private CMap<IntentId, Intent> intents; |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 87 | |
| 88 | private static final String STATES_TABLE = "intent-states"; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 89 | private CMap<IntentId, IntentState> states; |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 90 | |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 91 | // TODO left behind transient state issue: ONOS-103 |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 92 | // Map to store instance local intermediate state transition |
| 93 | private transient Map<IntentId, IntentState> transientStates = new ConcurrentHashMap<>(); |
| 94 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 95 | private static final String INSTALLABLE_TABLE = "installable-intents"; |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 96 | private CMap<IntentId, List<Intent>> installable; |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 97 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 98 | private LoadingCache<IntentId, String> keyCache; |
| 99 | |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 100 | private StoreSerializer serializer; |
| 101 | |
| 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 103 | protected DatabaseAdminService dbAdminService; |
| 104 | |
| 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 106 | protected DatabaseService dbService; |
| 107 | |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 109 | protected MetricsService metricsService; |
| 110 | |
Yuta HIGUCHI | c8f3026 | 2014-11-20 19:14:42 -0800 | [diff] [blame] | 111 | // TODO make this configurable |
| 112 | private boolean onlyLogTransitionError = true; |
| 113 | |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 114 | private Timer createIntentTimer; |
| 115 | private Timer removeIntentTimer; |
| 116 | private Timer setInstallableIntentsTimer; |
| 117 | private Timer getInstallableIntentsTimer; |
| 118 | private Timer removeInstalledIntentsTimer; |
| 119 | private Timer setStateTimer; |
| 120 | private Timer getIntentCountTimer; |
| 121 | private Timer getIntentsTimer; |
| 122 | private Timer getIntentTimer; |
| 123 | private Timer getIntentStateTimer; |
| 124 | |
| 125 | |
| 126 | private Timer createResponseTimer(String methodName) { |
| 127 | return createTimer("IntentStore", methodName, "responseTime"); |
| 128 | } |
| 129 | |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 130 | @Activate |
| 131 | public void activate() { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 132 | createIntentTimer = createResponseTimer("createIntent"); |
| 133 | removeIntentTimer = createResponseTimer("removeIntent"); |
| 134 | setInstallableIntentsTimer = createResponseTimer("setInstallableIntents"); |
| 135 | getInstallableIntentsTimer = createResponseTimer("getInstallableIntents"); |
| 136 | removeInstalledIntentsTimer = createResponseTimer("removeInstalledIntents"); |
| 137 | setStateTimer = createResponseTimer("setState"); |
| 138 | getIntentCountTimer = createResponseTimer("getIntentCount"); |
| 139 | getIntentsTimer = createResponseTimer("getIntents"); |
| 140 | getIntentTimer = createResponseTimer("getIntent"); |
| 141 | getIntentStateTimer = createResponseTimer("getIntentState"); |
| 142 | |
Yuta HIGUCHI | 71fa493 | 2014-10-28 22:27:49 -0700 | [diff] [blame] | 143 | // FIXME: We need a way to add serializer for intents which has been plugged-in. |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 144 | // As a short term workaround, relax Kryo config to |
| 145 | // registrationRequired=false |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 146 | serializer = new KryoSerializer() { |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 147 | |
| 148 | @Override |
| 149 | protected void setupKryoPool() { |
| 150 | serializerPool = KryoNamespace.newBuilder() |
| 151 | .setRegistrationRequired(false) |
| 152 | .register(KryoNamespaces.API) |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 153 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 154 | .build(); |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 155 | } |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 156 | }; |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 157 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 158 | keyCache = CacheBuilder.newBuilder() |
| 159 | .softValues() |
| 160 | .build(new CacheLoader<IntentId, String>() { |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 161 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 162 | @Override |
| 163 | public String load(IntentId key) { |
| 164 | return key.toString(); |
| 165 | } |
| 166 | }); |
| 167 | |
| 168 | intents = new IntentIdMap<>(dbAdminService, dbService, INTENTS_TABLE, serializer); |
| 169 | |
| 170 | states = new IntentIdMap<>(dbAdminService, dbService, STATES_TABLE, serializer); |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 171 | |
| 172 | transientStates.clear(); |
| 173 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 174 | installable = new IntentIdMap<>(dbAdminService, dbService, INSTALLABLE_TABLE, serializer); |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 175 | |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 176 | log.info("Started"); |
| 177 | } |
| 178 | |
| 179 | @Deactivate |
| 180 | public void deactivate() { |
| 181 | log.info("Stopped"); |
| 182 | } |
| 183 | |
| 184 | @Override |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 185 | public MetricsService metricsService() { |
| 186 | return metricsService; |
| 187 | } |
| 188 | |
| 189 | @Override |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 190 | public IntentEvent createIntent(Intent intent) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 191 | Context timer = startTimer(createIntentTimer); |
| 192 | try { |
| 193 | boolean absent = intents.putIfAbsent(intent.id(), intent); |
| 194 | if (!absent) { |
| 195 | // duplicate, ignore |
| 196 | return null; |
| 197 | } else { |
| 198 | return this.setState(intent, IntentState.SUBMITTED); |
| 199 | } |
| 200 | } finally { |
| 201 | stopTimer(timer); |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 202 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | @Override |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 206 | public void removeIntent(IntentId intentId) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 207 | Context timer = startTimer(removeIntentTimer); |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 208 | checkState(getIntentState(intentId) == WITHDRAWN, |
| 209 | "Intent state for {} is not WITHDRAWN.", intentId); |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 210 | try { |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 211 | intents.remove(intentId); |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 212 | states.remove(intentId); |
| 213 | transientStates.remove(intentId); |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 214 | installable.remove(intentId); |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 215 | } finally { |
| 216 | stopTimer(timer); |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 217 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public long getIntentCount() { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 222 | Context timer = startTimer(getIntentCountTimer); |
| 223 | try { |
| 224 | return intents.size(); |
| 225 | } finally { |
| 226 | stopTimer(timer); |
| 227 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public Iterable<Intent> getIntents() { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 232 | Context timer = startTimer(getIntentsTimer); |
| 233 | try { |
| 234 | return ImmutableSet.copyOf(intents.values()); |
| 235 | } finally { |
| 236 | stopTimer(timer); |
| 237 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public Intent getIntent(IntentId intentId) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 242 | Context timer = startTimer(getIntentTimer); |
| 243 | try { |
| 244 | return intents.get(intentId); |
| 245 | } finally { |
| 246 | stopTimer(timer); |
| 247 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public IntentState getIntentState(IntentId id) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 252 | Context timer = startTimer(getIntentStateTimer); |
| 253 | try { |
| 254 | final IntentState localState = transientStates.get(id); |
| 255 | if (localState != null) { |
| 256 | return localState; |
| 257 | } |
| 258 | return states.get(id); |
| 259 | } finally { |
| 260 | stopTimer(timer); |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 261 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 264 | // FIXME temporary workaround until we fix our state machine |
Yuta HIGUCHI | c8f3026 | 2014-11-20 19:14:42 -0800 | [diff] [blame] | 265 | private void verify(boolean expression, String errorMessageTemplate, Object... errorMessageArgs) { |
| 266 | if (onlyLogTransitionError) { |
| 267 | if (!expression) { |
| 268 | log.error(errorMessageTemplate.replace("%s", "{}"), errorMessageArgs); |
| 269 | } |
| 270 | } else { |
| 271 | Verify.verify(expression, errorMessageTemplate, errorMessageArgs); |
| 272 | } |
| 273 | } |
Yuta HIGUCHI | 9a2e18a | 2014-11-18 16:41:58 -0800 | [diff] [blame] | 274 | |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 275 | @Override |
| 276 | public IntentEvent setState(Intent intent, IntentState state) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 277 | Context timer = startTimer(setStateTimer); |
| 278 | try { |
| 279 | final IntentId id = intent.id(); |
| 280 | IntentEvent.Type evtType = null; |
| 281 | final IntentState prevParking; |
| 282 | boolean transitionedToParking = true; |
| 283 | boolean updated; |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 284 | |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 285 | // parking state transition |
| 286 | switch (state) { |
| 287 | case SUBMITTED: |
| 288 | prevParking = states.get(id); |
| 289 | if (prevParking == null) { |
| 290 | updated = states.putIfAbsent(id, SUBMITTED); |
| 291 | verify(updated, "Conditional replace %s => %s failed", prevParking, SUBMITTED); |
| 292 | } else { |
| 293 | verify(prevParking == WITHDRAWN, |
| 294 | "Illegal state transition attempted from %s to SUBMITTED", |
| 295 | prevParking); |
| 296 | updated = states.replace(id, prevParking, SUBMITTED); |
| 297 | verify(updated, "Conditional replace %s => %s failed", prevParking, SUBMITTED); |
| 298 | } |
| 299 | evtType = IntentEvent.Type.SUBMITTED; |
| 300 | break; |
| 301 | |
| 302 | case INSTALLED: |
| 303 | prevParking = states.get(id); |
| 304 | verify(PRE_INSTALLED.contains(prevParking), |
| 305 | "Illegal state transition attempted from %s to INSTALLED", |
| 306 | prevParking); |
| 307 | updated = states.replace(id, prevParking, INSTALLED); |
| 308 | verify(updated, "Conditional replace %s => %s failed", prevParking, INSTALLED); |
| 309 | evtType = IntentEvent.Type.INSTALLED; |
| 310 | break; |
| 311 | |
| 312 | case FAILED: |
| 313 | prevParking = states.get(id); |
| 314 | updated = states.replace(id, prevParking, FAILED); |
| 315 | verify(updated, "Conditional replace %s => %s failed", prevParking, FAILED); |
| 316 | evtType = IntentEvent.Type.FAILED; |
| 317 | break; |
| 318 | |
| 319 | case WITHDRAWN: |
| 320 | prevParking = states.get(id); |
| 321 | verify(PRE_WITHDRAWN.contains(prevParking), |
| 322 | "Illegal state transition attempted from %s to WITHDRAWN", |
| 323 | prevParking); |
| 324 | updated = states.replace(id, prevParking, WITHDRAWN); |
| 325 | verify(updated, "Conditional replace %s => %s failed", prevParking, WITHDRAWN); |
| 326 | evtType = IntentEvent.Type.WITHDRAWN; |
| 327 | break; |
| 328 | |
| 329 | default: |
| 330 | transitionedToParking = false; |
| 331 | prevParking = null; |
| 332 | break; |
Yuta HIGUCHI | 89a7f47 | 2014-11-21 14:50:24 -0800 | [diff] [blame] | 333 | } |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 334 | if (transitionedToParking) { |
| 335 | log.debug("Parking State change: {} {}=>{}", id, prevParking, state); |
| 336 | // remove instance local state |
| 337 | transientStates.remove(id); |
| 338 | } else { |
| 339 | // Update instance local state, which includes non-parking state transition |
| 340 | final IntentState prevTransient = transientStates.put(id, state); |
| 341 | log.debug("Transient State change: {} {}=>{}", id, prevTransient, state); |
| 342 | } |
Yuta HIGUCHI | 4490a73 | 2014-11-18 20:20:30 -0800 | [diff] [blame] | 343 | |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 344 | if (evtType == null) { |
| 345 | return null; |
| 346 | } |
| 347 | return new IntentEvent(evtType, intent); |
| 348 | } finally { |
| 349 | stopTimer(timer); |
Pavlin Radoslavov | c8ccbd9 | 2014-10-22 09:59:37 -0700 | [diff] [blame] | 350 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | @Override |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 354 | public void setInstallableIntents(IntentId intentId, List<Intent> result) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 355 | Context timer = startTimer(setInstallableIntentsTimer); |
| 356 | try { |
| 357 | installable.put(intentId, result); |
| 358 | } finally { |
| 359 | stopTimer(timer); |
| 360 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | @Override |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 364 | public List<Intent> getInstallableIntents(IntentId intentId) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 365 | Context timer = startTimer(getInstallableIntentsTimer); |
| 366 | try { |
| 367 | return installable.get(intentId); |
| 368 | } finally { |
| 369 | stopTimer(timer); |
| 370 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | @Override |
| 374 | public void removeInstalledIntents(IntentId intentId) { |
Yuta HIGUCHI | e367fb9 | 2014-11-24 22:26:58 -0800 | [diff] [blame] | 375 | Context timer = startTimer(removeInstalledIntentsTimer); |
| 376 | try { |
| 377 | installable.remove(intentId); |
| 378 | } finally { |
| 379 | stopTimer(timer); |
| 380 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 381 | } |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 382 | |
| 383 | protected String strIntentId(IntentId key) { |
| 384 | return keyCache.getUnchecked(key); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Distributed Map from IntentId to some value. |
| 389 | * |
| 390 | * @param <V> Map value type |
| 391 | */ |
| 392 | final class IntentIdMap<V> extends CMap<IntentId, V> { |
| 393 | |
| 394 | /** |
| 395 | * Creates a IntentIdMap instance. |
| 396 | * |
| 397 | * @param dbAdminService DatabaseAdminService to use for this instance |
| 398 | * @param dbService DatabaseService to use for this instance |
| 399 | * @param tableName table which this Map corresponds to |
| 400 | * @param serializer Value serializer |
| 401 | */ |
| 402 | public IntentIdMap(DatabaseAdminService dbAdminService, |
| 403 | DatabaseService dbService, |
| 404 | String tableName, |
| 405 | StoreSerializer serializer) { |
| 406 | super(dbAdminService, dbService, tableName, serializer); |
| 407 | } |
| 408 | |
| 409 | @Override |
| 410 | protected String sK(IntentId key) { |
| 411 | return strIntentId(key); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | @Override |
| 416 | public List<Operation> batchWrite(BatchWrite batch) { |
| 417 | |
| 418 | List<Operation> failed = new ArrayList<>(); |
| 419 | final Builder builder = BatchWriteRequest.newBuilder(); |
| 420 | |
Yuta HIGUCHI | f568245 | 2014-12-01 10:17:15 -0800 | [diff] [blame] | 421 | final Set<IntentId> transitionedToParking = new HashSet<>(); |
| 422 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 423 | for (Operation op : batch.operations()) { |
| 424 | switch (op.type()) { |
| 425 | case CREATE_INTENT: |
| 426 | checkArgument(op.args().size() == 1, |
| 427 | "CREATE_INTENT takes 1 argument. %s", op); |
| 428 | Intent intent = op.arg(0); |
| 429 | builder.putIfAbsent(INTENTS_TABLE, strIntentId(intent.id()), serializer.encode(intent)); |
| 430 | builder.putIfAbsent(STATES_TABLE, strIntentId(intent.id()), serializer.encode(SUBMITTED)); |
| 431 | break; |
| 432 | |
| 433 | case REMOVE_INTENT: |
| 434 | checkArgument(op.args().size() == 1, |
| 435 | "REMOVE_INTENT takes 1 argument. %s", op); |
| 436 | IntentId intentId = (IntentId) op.arg(0); |
| 437 | builder.remove(INTENTS_TABLE, strIntentId(intentId)); |
| 438 | builder.remove(STATES_TABLE, strIntentId(intentId)); |
| 439 | builder.remove(INSTALLABLE_TABLE, strIntentId(intentId)); |
| 440 | break; |
| 441 | |
| 442 | case SET_STATE: |
| 443 | checkArgument(op.args().size() == 2, |
| 444 | "SET_STATE takes 2 arguments. %s", op); |
| 445 | intent = op.arg(0); |
| 446 | IntentState newState = op.arg(1); |
| 447 | builder.put(STATES_TABLE, strIntentId(intent.id()), serializer.encode(newState)); |
Yuta HIGUCHI | f568245 | 2014-12-01 10:17:15 -0800 | [diff] [blame] | 448 | if (PARKING.contains(newState)) { |
| 449 | transitionedToParking.add(intent.id()); |
| 450 | } else { |
| 451 | transitionedToParking.remove(intent.id()); |
| 452 | } |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 453 | break; |
| 454 | |
| 455 | case SET_INSTALLABLE: |
| 456 | checkArgument(op.args().size() == 2, |
| 457 | "SET_INSTALLABLE takes 2 arguments. %s", op); |
| 458 | intentId = op.arg(0); |
| 459 | List<Intent> installableIntents = op.arg(1); |
| 460 | builder.put(INSTALLABLE_TABLE, strIntentId(intentId), serializer.encode(installableIntents)); |
| 461 | break; |
| 462 | |
| 463 | case REMOVE_INSTALLED: |
| 464 | checkArgument(op.args().size() == 1, |
| 465 | "REMOVE_INSTALLED takes 1 argument. %s", op); |
| 466 | intentId = op.arg(0); |
| 467 | builder.remove(INSTALLABLE_TABLE, strIntentId(intentId)); |
| 468 | break; |
| 469 | |
| 470 | default: |
| 471 | log.warn("Unknown Operation encountered: {}", op); |
| 472 | failed.add(op); |
| 473 | break; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | BatchWriteResult batchWriteResult = dbService.batchWrite(builder.build()); |
| 478 | if (batchWriteResult.isSuccessful()) { |
| 479 | // no-failure (except for invalid input) |
Yuta HIGUCHI | f568245 | 2014-12-01 10:17:15 -0800 | [diff] [blame] | 480 | transitionedToParking.forEach((intentId) -> transientStates.remove(intentId)); |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame] | 481 | return failed; |
| 482 | } else { |
| 483 | // everything failed |
| 484 | return batch.operations(); |
| 485 | } |
| 486 | } |
Yuta HIGUCHI | 406a565 | 2014-10-20 22:18:16 -0700 | [diff] [blame] | 487 | } |