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