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