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