Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.store.intent.impl; |
| 17 | |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Brian O'Connor | 4964d3d | 2015-03-12 20:38:10 -0700 | [diff] [blame] | 19 | import org.apache.commons.lang.math.RandomUtils; |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 20 | import org.onlab.util.Backtrace; |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 21 | import org.onlab.util.KryoNamespace; |
Aaron Kruglikov | 3721041 | 2016-12-06 12:55:57 -0800 | [diff] [blame] | 22 | import org.onosproject.cfg.ComponentConfigService; |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 23 | import org.onosproject.cluster.ClusterService; |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 24 | import org.onosproject.cluster.ControllerNode; |
| 25 | import org.onosproject.cluster.NodeId; |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 26 | import org.onosproject.net.intent.Intent; |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 27 | import org.onosproject.net.intent.IntentData; |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 28 | import org.onosproject.net.intent.IntentEvent; |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 29 | import org.onosproject.net.intent.IntentState; |
| 30 | import org.onosproject.net.intent.IntentStore; |
| 31 | import org.onosproject.net.intent.IntentStoreDelegate; |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 32 | import org.onosproject.net.intent.Key; |
Pier Ventre | 6582808 | 2017-01-04 10:15:00 -0800 | [diff] [blame] | 33 | import org.onosproject.net.intent.WorkPartitionService; |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 34 | import org.onosproject.store.AbstractStore; |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 35 | import org.onosproject.store.Timestamp; |
Jonathan Hart | 539a646 | 2015-01-27 17:05:43 -0800 | [diff] [blame] | 36 | import org.onosproject.store.serializers.KryoNamespaces; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 37 | import org.onosproject.store.service.EventuallyConsistentMap; |
Aaron Kruglikov | 3721041 | 2016-12-06 12:55:57 -0800 | [diff] [blame] | 38 | import org.onosproject.store.service.EventuallyConsistentMapBuilder; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 39 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 40 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
Brian Stanke | 2617dac | 2016-05-03 16:15:44 -0400 | [diff] [blame] | 41 | import org.onosproject.store.service.MultiValuedTimestamp; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 42 | import org.onosproject.store.service.StorageService; |
Brian Stanke | 2617dac | 2016-05-03 16:15:44 -0400 | [diff] [blame] | 43 | import org.onosproject.store.service.WallClockTimestamp; |
Aaron Kruglikov | 3721041 | 2016-12-06 12:55:57 -0800 | [diff] [blame] | 44 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 45 | import org.osgi.service.component.annotations.Activate; |
| 46 | import org.osgi.service.component.annotations.Component; |
| 47 | import org.osgi.service.component.annotations.Deactivate; |
| 48 | import org.osgi.service.component.annotations.Modified; |
| 49 | import org.osgi.service.component.annotations.Reference; |
| 50 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 51 | import org.slf4j.Logger; |
| 52 | |
Brian O'Connor | 9081c90 | 2015-03-05 16:26:25 -0800 | [diff] [blame] | 53 | import java.util.Collection; |
Aaron Kruglikov | 3721041 | 2016-12-06 12:55:57 -0800 | [diff] [blame] | 54 | import java.util.Dictionary; |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 55 | import java.util.List; |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 56 | import java.util.Objects; |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 57 | import java.util.Optional; |
Aaron Kruglikov | 3721041 | 2016-12-06 12:55:57 -0800 | [diff] [blame] | 58 | import java.util.Properties; |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 59 | import java.util.concurrent.atomic.AtomicLong; |
Jonathan Hart | 4fd4ebb | 2015-02-04 17:38:48 -0800 | [diff] [blame] | 60 | import java.util.stream.Collectors; |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 61 | |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 62 | import static com.google.common.base.Preconditions.checkNotNull; |
Aaron Kruglikov | 3721041 | 2016-12-06 12:55:57 -0800 | [diff] [blame] | 63 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 64 | import static org.onlab.util.Tools.get; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 65 | import static org.onosproject.net.intent.IntentState.PURGE_REQ; |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 66 | import static org.onosproject.store.OsgiPropertyConstants.GIS_PERSISTENCE_ENABLED; |
| 67 | import static org.onosproject.store.OsgiPropertyConstants.GIS_PERSISTENCE_ENABLED_DEFAULT; |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 68 | import static org.slf4j.LoggerFactory.getLogger; |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame^] | 69 | |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 70 | /** |
| 71 | * Manages inventory of Intents in a distributed data store that uses optimistic |
| 72 | * replication and gossip based techniques. |
| 73 | */ |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 74 | //FIXME we should listen for leadership changes. if the local instance has just |
| 75 | // ... become a leader, scan the pending map and process those |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 76 | @Component( |
| 77 | immediate = true, |
| 78 | service = IntentStore.class, |
| 79 | property = { |
Ray Milkey | 2d7bca1 | 2018-10-17 14:51:52 -0700 | [diff] [blame] | 80 | GIS_PERSISTENCE_ENABLED + ":Boolean=" + GIS_PERSISTENCE_ENABLED_DEFAULT |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 81 | } |
| 82 | ) |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 83 | public class GossipIntentStore |
| 84 | extends AbstractStore<IntentEvent, IntentStoreDelegate> |
| 85 | implements IntentStore { |
| 86 | |
| 87 | private final Logger log = getLogger(getClass()); |
| 88 | |
Aaron Kruglikov | 3721041 | 2016-12-06 12:55:57 -0800 | [diff] [blame] | 89 | private static final boolean PERSIST = false; |
| 90 | |
Jonathan Hart | 4fd4ebb | 2015-02-04 17:38:48 -0800 | [diff] [blame] | 91 | // Map of intent key => current intent state |
Brian O'Connor | 645bf20 | 2015-02-24 12:00:58 -0800 | [diff] [blame] | 92 | private EventuallyConsistentMap<Key, IntentData> currentMap; |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 93 | |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 94 | // Map of intent key => pending intent operation |
Brian O'Connor | 645bf20 | 2015-02-24 12:00:58 -0800 | [diff] [blame] | 95 | private EventuallyConsistentMap<Key, IntentData> pendingMap; |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 96 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Aaron Kruglikov | 3721041 | 2016-12-06 12:55:57 -0800 | [diff] [blame] | 98 | protected ComponentConfigService configService; |
| 99 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 101 | protected ClusterService clusterService; |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 102 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 104 | protected StorageService storageService; |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 105 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 107 | protected WorkPartitionService partitionService; |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 108 | |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 109 | private final AtomicLong sequenceNumber = new AtomicLong(0); |
| 110 | |
Frank Wang | e0eb5ce | 2016-07-01 18:21:25 +0800 | [diff] [blame] | 111 | private EventuallyConsistentMapListener<Key, IntentData> |
| 112 | mapCurrentListener = new InternalCurrentListener(); |
| 113 | |
| 114 | private EventuallyConsistentMapListener<Key, IntentData> |
| 115 | mapPendingListener = new InternalPendingListener(); |
| 116 | |
Aaron Kruglikov | 3721041 | 2016-12-06 12:55:57 -0800 | [diff] [blame] | 117 | //Denotes the initial persistence value for this structure |
| 118 | private boolean initiallyPersistent = false; |
| 119 | |
| 120 | //TODO this is currently an experimental feature used for performance |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 121 | // evaluation, enabling persistence with persist the intents but they will |
Aaron Kruglikov | 3721041 | 2016-12-06 12:55:57 -0800 | [diff] [blame] | 122 | // not be reinstalled and network state will not be consistent with the |
| 123 | // intents on cluster restart |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 124 | |
| 125 | /** EXPERIMENTAL: Enable intent persistence. */ |
Ray Milkey | b5646e6 | 2018-10-16 11:42:18 -0700 | [diff] [blame] | 126 | private boolean persistenceEnabled = GIS_PERSISTENCE_ENABLED_DEFAULT; |
Aaron Kruglikov | 3721041 | 2016-12-06 12:55:57 -0800 | [diff] [blame] | 127 | |
| 128 | |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 129 | /** |
| 130 | * TimestampProvieder for currentMap. |
| 131 | * |
| 132 | * @param key Intent key |
| 133 | * @param data Intent data |
| 134 | * @return generated time stamp |
| 135 | */ |
| 136 | private Timestamp currentTimestampProvider(Key key, IntentData data) { |
| 137 | // vector timestamp consisting from 3 components |
| 138 | // (request timestamp, internal state, sequence #) |
| 139 | |
| 140 | // 2nd component required to avoid compilation result overwriting installation state |
| 141 | // note: above is likely to be a sign of design issue in transition to installation phase |
| 142 | // 3rd component required for generating new timestamp for removal.. |
| 143 | return new MultiValuedTimestamp<>( |
| 144 | Optional.ofNullable(data.version()).orElseGet(WallClockTimestamp::new), |
| 145 | new MultiValuedTimestamp<>(data.internalStateVersion(), |
| 146 | sequenceNumber.incrementAndGet())); |
| 147 | } |
| 148 | |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 149 | @Activate |
Aaron Kruglikov | 3721041 | 2016-12-06 12:55:57 -0800 | [diff] [blame] | 150 | public void activate(ComponentContext context) { |
| 151 | configService.registerProperties(getClass()); |
| 152 | modified(context); |
| 153 | //TODO persistent intents must be reevaluated and the appropriate |
| 154 | //processing done here, current implementation is not functional |
| 155 | //and is for performance evaluation only |
| 156 | initiallyPersistent = persistenceEnabled; |
Jonathan Hart | 539a646 | 2015-01-27 17:05:43 -0800 | [diff] [blame] | 157 | KryoNamespace.Builder intentSerializer = KryoNamespace.newBuilder() |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 158 | .register(KryoNamespaces.API) |
| 159 | .register(IntentData.class) |
HIGUCHI Yuta | 03666a3 | 2016-05-18 11:49:09 -0700 | [diff] [blame] | 160 | .register(MultiValuedTimestamp.class); |
Jonathan Hart | 4fd4ebb | 2015-02-04 17:38:48 -0800 | [diff] [blame] | 161 | |
Aaron Kruglikov | 3721041 | 2016-12-06 12:55:57 -0800 | [diff] [blame] | 162 | EventuallyConsistentMapBuilder currentECMapBuilder = |
| 163 | storageService.<Key, IntentData>eventuallyConsistentMapBuilder() |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 164 | .withName("intent-current") |
| 165 | .withSerializer(intentSerializer) |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 166 | .withTimestampProvider(this::currentTimestampProvider) |
Aaron Kruglikov | 3721041 | 2016-12-06 12:55:57 -0800 | [diff] [blame] | 167 | .withPeerUpdateFunction((key, intentData) -> getPeerNodes(key, intentData)); |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 168 | |
Aaron Kruglikov | 3721041 | 2016-12-06 12:55:57 -0800 | [diff] [blame] | 169 | EventuallyConsistentMapBuilder pendingECMapBuilder = |
| 170 | storageService.<Key, IntentData>eventuallyConsistentMapBuilder() |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 171 | .withName("intent-pending") |
| 172 | .withSerializer(intentSerializer) |
Brian O'Connor | c590ebb | 2016-12-08 18:16:41 -0800 | [diff] [blame] | 173 | .withTimestampProvider((key, intentData) -> |
| 174 | /* |
| 175 | We always want to accept new values in the pending map, |
| 176 | so we should use a high performance logical clock. |
| 177 | */ |
| 178 | /* |
| 179 | TODO We use the wall clock for the time being, but |
| 180 | this could result in issues if there is clock skew |
| 181 | across instances. |
| 182 | */ |
| 183 | new MultiValuedTimestamp<>(new WallClockTimestamp(), System.nanoTime())) |
Aaron Kruglikov | 3721041 | 2016-12-06 12:55:57 -0800 | [diff] [blame] | 184 | .withPeerUpdateFunction((key, intentData) -> getPeerNodes(key, intentData)); |
| 185 | if (initiallyPersistent) { |
| 186 | currentECMapBuilder = currentECMapBuilder.withPersistence(); |
| 187 | pendingECMapBuilder = pendingECMapBuilder.withPersistence(); |
| 188 | } |
| 189 | currentMap = currentECMapBuilder.build(); |
| 190 | pendingMap = pendingECMapBuilder.build(); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 191 | |
Frank Wang | e0eb5ce | 2016-07-01 18:21:25 +0800 | [diff] [blame] | 192 | currentMap.addListener(mapCurrentListener); |
| 193 | pendingMap.addListener(mapPendingListener); |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 194 | |
| 195 | log.info("Started"); |
| 196 | } |
| 197 | |
| 198 | @Deactivate |
| 199 | public void deactivate() { |
Aaron Kruglikov | 3721041 | 2016-12-06 12:55:57 -0800 | [diff] [blame] | 200 | if (initiallyPersistent && !persistenceEnabled) { |
| 201 | pendingMap.clear(); |
| 202 | currentMap.clear(); |
| 203 | log.debug("Persistent state has been purged"); |
| 204 | } |
Frank Wang | e0eb5ce | 2016-07-01 18:21:25 +0800 | [diff] [blame] | 205 | currentMap.removeListener(mapCurrentListener); |
| 206 | pendingMap.removeListener(mapPendingListener); |
Brian O'Connor | 645bf20 | 2015-02-24 12:00:58 -0800 | [diff] [blame] | 207 | currentMap.destroy(); |
| 208 | pendingMap.destroy(); |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 209 | |
| 210 | log.info("Stopped"); |
| 211 | } |
| 212 | |
Aaron Kruglikov | 3721041 | 2016-12-06 12:55:57 -0800 | [diff] [blame] | 213 | @Modified |
| 214 | public void modified(ComponentContext context) { |
| 215 | Dictionary<?, ?> properties = context != null ? context.getProperties() |
| 216 | : new Properties(); |
| 217 | try { |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 218 | String s = get(properties, GIS_PERSISTENCE_ENABLED); |
Aaron Kruglikov | 3721041 | 2016-12-06 12:55:57 -0800 | [diff] [blame] | 219 | persistenceEnabled = isNullOrEmpty(s) ? PERSIST : |
| 220 | Boolean.parseBoolean(s.trim()); |
| 221 | } catch (Exception e) { |
| 222 | persistenceEnabled = initiallyPersistent; |
| 223 | log.error("Failed to retrieve the property value for persist," + |
| 224 | "defaulting to the initial setting of \"{}\"" + |
| 225 | "any persistent state will not be purged, if " + |
| 226 | "this occurred at startup changes made in this" + |
| 227 | "session will not be persisted to disk", |
| 228 | initiallyPersistent); |
| 229 | } |
| 230 | if (persistenceEnabled) { |
| 231 | //FIXME persistence is an experimental feature, warnings can be removed |
| 232 | //when the feature is completed |
| 233 | log.warn("Persistence is an experimental feature, it is not fully " + |
| 234 | "functional and is intended only for " + |
| 235 | "performance evaluation"); |
| 236 | } |
| 237 | if (!initiallyPersistent && !persistenceEnabled) { |
| 238 | log.info("Persistence is set to \"false\", this was the initial" + |
| 239 | " setting so no state will be purged or " + |
| 240 | "persisted"); |
| 241 | } else if (!initiallyPersistent && persistenceEnabled) { |
| 242 | log.info("Persistence is set to \"true\", entries will be begin " + |
| 243 | "to be persisted after restart"); |
| 244 | } else if (initiallyPersistent && !persistenceEnabled) { |
| 245 | log.info("Persistence is set to \"false\", all persistent state " + |
| 246 | "will be purged on next shutdown"); |
| 247 | } else { |
| 248 | log.info("Persistence is set to \"true\", entries from this and" + |
| 249 | " subsequent sessions will be persisted"); |
| 250 | } |
| 251 | |
| 252 | |
| 253 | } |
| 254 | |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 255 | @Override |
| 256 | public long getIntentCount() { |
Brian O'Connor | 645bf20 | 2015-02-24 12:00:58 -0800 | [diff] [blame] | 257 | return currentMap.size(); |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public Iterable<Intent> getIntents() { |
Brian O'Connor | 645bf20 | 2015-02-24 12:00:58 -0800 | [diff] [blame] | 262 | return currentMap.values().stream() |
Jonathan Hart | 4fd4ebb | 2015-02-04 17:38:48 -0800 | [diff] [blame] | 263 | .map(IntentData::intent) |
| 264 | .collect(Collectors.toList()); |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | @Override |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 268 | public Iterable<IntentData> getIntentData(boolean localOnly, long olderThan) { |
| 269 | if (localOnly || olderThan > 0) { |
| 270 | long now = System.currentTimeMillis(); |
| 271 | final WallClockTimestamp time = new WallClockTimestamp(now - olderThan); |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 272 | return currentMap.values().stream() |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 273 | .filter(data -> data.version().isOlderThan(time) && |
| 274 | (!localOnly || isMaster(data.key()))) |
Brian O'Connor | 3c58e96 | 2015-04-28 23:21:51 -0700 | [diff] [blame] | 275 | .collect(Collectors.toList()); |
| 276 | } |
| 277 | return currentMap.values(); |
| 278 | } |
| 279 | |
| 280 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 281 | public IntentState getIntentState(Key intentKey) { |
Brian O'Connor | 645bf20 | 2015-02-24 12:00:58 -0800 | [diff] [blame] | 282 | IntentData data = currentMap.get(intentKey); |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 283 | if (data != null) { |
| 284 | return data.state(); |
| 285 | } |
| 286 | return null; |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | @Override |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 290 | public List<Intent> getInstallableIntents(Key intentKey) { |
Brian O'Connor | 645bf20 | 2015-02-24 12:00:58 -0800 | [diff] [blame] | 291 | IntentData data = currentMap.get(intentKey); |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 292 | if (data != null) { |
| 293 | return data.installables(); |
| 294 | } |
Sho SHIMIZU | 9fa6b8b | 2016-01-15 16:45:57 -0800 | [diff] [blame] | 295 | return ImmutableList.of(); |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 298 | @Override |
| 299 | public void write(IntentData newData) { |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 300 | checkNotNull(newData); |
| 301 | |
Brian O'Connor | 645bf20 | 2015-02-24 12:00:58 -0800 | [diff] [blame] | 302 | IntentData currentData = currentMap.get(newData.key()); |
Jonathan Hart | 72175c2 | 2015-03-24 18:55:58 -0700 | [diff] [blame] | 303 | if (IntentData.isUpdateAcceptable(currentData, newData)) { |
Jonathan Hart | 07e58be | 2015-02-12 09:57:16 -0800 | [diff] [blame] | 304 | // Only the master is modifying the current state. Therefore assume |
| 305 | // this always succeeds |
Thomas Vachuska | 7d61ab2 | 2016-06-13 19:41:52 -0700 | [diff] [blame] | 306 | if (newData.state() == PURGE_REQ) { |
| 307 | if (currentData != null) { |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 308 | if (log.isTraceEnabled()) { |
| 309 | log.trace("Purging {} in currentMap. {}@{}", |
| 310 | newData.key(), newData.state(), newData.version(), |
| 311 | new Backtrace()); |
| 312 | } |
Thomas Vachuska | 7d61ab2 | 2016-06-13 19:41:52 -0700 | [diff] [blame] | 313 | currentMap.remove(newData.key(), currentData); |
| 314 | } else { |
| 315 | log.info("Gratuitous purge request for intent: {}", newData.key()); |
| 316 | } |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 317 | } else { |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 318 | if (log.isTraceEnabled()) { |
| 319 | log.trace("Putting {} in currentMap. {}@{}", |
| 320 | newData.key(), newData.state(), newData.version(), |
| 321 | new Backtrace()); |
| 322 | } |
| 323 | currentMap.put(newData.key(), IntentData.copy(newData)); |
Ray Milkey | 8c6d00e | 2015-03-13 14:14:34 -0700 | [diff] [blame] | 324 | } |
Yuta HIGUCHI | f76f6d5 | 2017-05-15 18:02:09 -0700 | [diff] [blame] | 325 | } else { |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 326 | log.debug("Update for {} not acceptable from:\n{}\nto:\n{}", |
| 327 | newData.key(), currentData, newData); |
Jonathan Hart | 07e58be | 2015-02-12 09:57:16 -0800 | [diff] [blame] | 328 | } |
Pier Luigi | 13b287f | 2017-01-10 15:07:52 -0800 | [diff] [blame] | 329 | // Remove the intent data from the pending map if the newData is more |
| 330 | // recent or equal to the existing entry. No matter if it is an acceptable |
| 331 | // update or not |
Yi Tseng | 5bd294e | 2017-05-04 11:00:03 -0700 | [diff] [blame] | 332 | Key key = newData.key(); |
| 333 | IntentData existingValue = pendingMap.get(key); |
| 334 | |
| 335 | if (existingValue == null) { |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | if (!existingValue.version().isNewerThan(newData.version())) { |
| 340 | pendingMap.remove(key, existingValue); |
Yuta HIGUCHI | f76f6d5 | 2017-05-15 18:02:09 -0700 | [diff] [blame] | 341 | } else { |
| 342 | log.debug("{} in pending map was newer, leaving it there", key); |
Yi Tseng | 5bd294e | 2017-05-04 11:00:03 -0700 | [diff] [blame] | 343 | } |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Brian O'Connor | 9081c90 | 2015-03-05 16:26:25 -0800 | [diff] [blame] | 346 | private Collection<NodeId> getPeerNodes(Key key, IntentData data) { |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 347 | NodeId master = partitionService.getLeader(key, Key::hash); |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 348 | NodeId origin = (data != null) ? data.origin() : null; |
Jonathan Hart | 4a29c59 | 2015-09-23 17:55:07 -0700 | [diff] [blame] | 349 | if (data != null && (master == null || origin == null)) { |
Brian O'Connor | 6325dad | 2015-07-07 15:36:29 -0700 | [diff] [blame] | 350 | log.debug("Intent {} missing master and/or origin; master = {}, origin = {}", |
| 351 | key, master, origin); |
Brian O'Connor | 4964d3d | 2015-03-12 20:38:10 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 354 | NodeId me = clusterService.getLocalNode().id(); |
| 355 | boolean isMaster = Objects.equals(master, me); |
| 356 | boolean isOrigin = Objects.equals(origin, me); |
| 357 | if (isMaster && isOrigin) { |
Brian O'Connor | 4964d3d | 2015-03-12 20:38:10 -0700 | [diff] [blame] | 358 | return getRandomNode(); |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 359 | } else if (isMaster) { |
Brian O'Connor | 4964d3d | 2015-03-12 20:38:10 -0700 | [diff] [blame] | 360 | return origin != null ? ImmutableList.of(origin) : getRandomNode(); |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 361 | } else if (isOrigin) { |
Brian O'Connor | 4964d3d | 2015-03-12 20:38:10 -0700 | [diff] [blame] | 362 | return master != null ? ImmutableList.of(master) : getRandomNode(); |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 363 | } else { |
Brian O'Connor | 6325dad | 2015-07-07 15:36:29 -0700 | [diff] [blame] | 364 | log.warn("No master or origin for intent {}", key); |
Brian O'Connor | 148ba2f | 2015-07-06 11:38:44 -0700 | [diff] [blame] | 365 | return master != null ? ImmutableList.of(master) : getRandomNode(); |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Brian O'Connor | 4964d3d | 2015-03-12 20:38:10 -0700 | [diff] [blame] | 369 | private List<NodeId> getRandomNode() { |
| 370 | NodeId me = clusterService.getLocalNode().id(); |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 371 | List<NodeId> nodes = clusterService.getNodes().stream() |
Brian O'Connor | 4964d3d | 2015-03-12 20:38:10 -0700 | [diff] [blame] | 372 | .map(ControllerNode::id) |
| 373 | .filter(node -> !Objects.equals(node, me)) |
| 374 | .collect(Collectors.toList()); |
Sho SHIMIZU | 27e1b92 | 2016-01-15 22:47:44 -0800 | [diff] [blame] | 375 | if (nodes.isEmpty()) { |
| 376 | return ImmutableList.of(); |
Brian O'Connor | 4964d3d | 2015-03-12 20:38:10 -0700 | [diff] [blame] | 377 | } |
| 378 | return ImmutableList.of(nodes.get(RandomUtils.nextInt(nodes.size()))); |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 381 | @Override |
| 382 | public void batchWrite(Iterable<IntentData> updates) { |
Jonathan Hart | 4fd4ebb | 2015-02-04 17:38:48 -0800 | [diff] [blame] | 383 | updates.forEach(this::write); |
| 384 | } |
| 385 | |
| 386 | @Override |
| 387 | public Intent getIntent(Key key) { |
Brian O'Connor | 645bf20 | 2015-02-24 12:00:58 -0800 | [diff] [blame] | 388 | IntentData data = currentMap.get(key); |
Jonathan Hart | 4fd4ebb | 2015-02-04 17:38:48 -0800 | [diff] [blame] | 389 | if (data != null) { |
| 390 | return data.intent(); |
| 391 | } |
| 392 | return null; |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | @Override |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 396 | public IntentData getIntentData(Key key) { |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 397 | IntentData current = currentMap.get(key); |
| 398 | if (current == null) { |
| 399 | return null; |
| 400 | } |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 401 | return IntentData.copy(current); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | @Override |
| 405 | public void addPending(IntentData data) { |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 406 | checkNotNull(data); |
Pier Ventre | 6582808 | 2017-01-04 10:15:00 -0800 | [diff] [blame] | 407 | if (data.version() == null) { |
Pier Luigi | e6caf68 | 2017-01-26 15:25:09 -0800 | [diff] [blame] | 408 | // Copy IntentData including request state in this way we can |
| 409 | // avoid the creation of Intents with state == request, which can |
| 410 | // be problematic if the Intent state is different from *REQ |
| 411 | // {INSTALL_, WITHDRAW_ and PURGE_}. |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 412 | pendingMap.put(data.key(), IntentData.assign(data, |
| 413 | new WallClockTimestamp(), |
| 414 | clusterService.getLocalNode().id())); |
Pier Ventre | 6582808 | 2017-01-04 10:15:00 -0800 | [diff] [blame] | 415 | } else { |
| 416 | pendingMap.compute(data.key(), (key, existingValue) -> { |
| 417 | if (existingValue == null || existingValue.version().isOlderThan(data.version())) { |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 418 | return IntentData.assign(data, data.version(), clusterService.getLocalNode().id()); |
Pier Ventre | 6582808 | 2017-01-04 10:15:00 -0800 | [diff] [blame] | 419 | } else { |
| 420 | return existingValue; |
| 421 | } |
| 422 | }); |
| 423 | } |
Jonathan Hart | 0d18df3 | 2015-03-21 08:42:59 -0700 | [diff] [blame] | 424 | |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | @Override |
Brian O'Connor | be28a87 | 2015-02-19 21:44:37 -0800 | [diff] [blame] | 428 | public boolean isMaster(Key intentKey) { |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame] | 429 | return partitionService.isMine(intentKey, Key::hash); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 430 | } |
| 431 | |
Jonathan Hart | 34f1e38 | 2015-02-24 16:52:23 -0800 | [diff] [blame] | 432 | @Override |
| 433 | public Iterable<Intent> getPending() { |
Brian O'Connor | 645bf20 | 2015-02-24 12:00:58 -0800 | [diff] [blame] | 434 | return pendingMap.values().stream() |
Jonathan Hart | 34f1e38 | 2015-02-24 16:52:23 -0800 | [diff] [blame] | 435 | .map(IntentData::intent) |
| 436 | .collect(Collectors.toList()); |
| 437 | } |
| 438 | |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 439 | @Override |
| 440 | public Iterable<IntentData> getPendingData() { |
| 441 | return pendingMap.values(); |
| 442 | } |
| 443 | |
| 444 | @Override |
Brian O'Connor | c590ebb | 2016-12-08 18:16:41 -0800 | [diff] [blame] | 445 | public IntentData getPendingData(Key intentKey) { |
| 446 | return pendingMap.get(intentKey); |
| 447 | } |
| 448 | |
| 449 | @Override |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 450 | public Iterable<IntentData> getPendingData(boolean localOnly, long olderThan) { |
| 451 | long now = System.currentTimeMillis(); |
| 452 | final WallClockTimestamp time = new WallClockTimestamp(now - olderThan); |
| 453 | return pendingMap.values().stream() |
| 454 | .filter(data -> data.version().isOlderThan(time) && |
Brian Stanke | 2617dac | 2016-05-03 16:15:44 -0400 | [diff] [blame] | 455 | (!localOnly || isMaster(data.key()))) |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 456 | .collect(Collectors.toList()); |
| 457 | } |
| 458 | |
Brian O'Connor | 5eb77c8 | 2015-03-02 18:09:39 -0800 | [diff] [blame] | 459 | private final class InternalCurrentListener implements |
Jonathan Hart | 4fd4ebb | 2015-02-04 17:38:48 -0800 | [diff] [blame] | 460 | EventuallyConsistentMapListener<Key, IntentData> { |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 461 | @Override |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 462 | public void event(EventuallyConsistentMapEvent<Key, IntentData> event) { |
| 463 | IntentData intentData = event.value(); |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 464 | if (event.type() == EventuallyConsistentMapEvent.Type.PUT) { |
| 465 | // The current intents map has been updated. If we are master for |
| 466 | // this intent's partition, notify the Manager that it should |
| 467 | // emit notifications about updated tracked resources. |
| 468 | if (delegate != null && isMaster(event.value().intent().key())) { |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 469 | delegate.onUpdate(IntentData.copy(intentData)); // copy for safety, likely unnecessary |
Thomas Vachuska | c46af20 | 2015-06-03 16:43:27 -0700 | [diff] [blame] | 470 | } |
Sho SHIMIZU | 96b39e2 | 2016-01-15 17:35:04 -0800 | [diff] [blame] | 471 | IntentEvent.getEvent(intentData).ifPresent(e -> notifyDelegate(e)); |
Jonathan Hart | 539a646 | 2015-01-27 17:05:43 -0800 | [diff] [blame] | 472 | } |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 476 | private final class InternalPendingListener implements |
Jonathan Hart | 4fd4ebb | 2015-02-04 17:38:48 -0800 | [diff] [blame] | 477 | EventuallyConsistentMapListener<Key, IntentData> { |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 478 | @Override |
| 479 | public void event( |
Jonathan Hart | 4fd4ebb | 2015-02-04 17:38:48 -0800 | [diff] [blame] | 480 | EventuallyConsistentMapEvent<Key, IntentData> event) { |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 481 | if (event.type() == EventuallyConsistentMapEvent.Type.PUT) { |
| 482 | // The pending intents map has been updated. If we are master for |
Jonathan Hart | 4fd4ebb | 2015-02-04 17:38:48 -0800 | [diff] [blame] | 483 | // this intent's partition, notify the Manager that it should do |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 484 | // some work. |
Brian O'Connor | be28a87 | 2015-02-19 21:44:37 -0800 | [diff] [blame] | 485 | if (isMaster(event.value().intent().key())) { |
Jonathan Hart | 4fd4ebb | 2015-02-04 17:38:48 -0800 | [diff] [blame] | 486 | if (delegate != null) { |
Yuta HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 487 | delegate.process(IntentData.copy(event.value())); |
Jonathan Hart | 4fd4ebb | 2015-02-04 17:38:48 -0800 | [diff] [blame] | 488 | } |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 489 | } |
Jonathan Hart | 5ec32ba | 2015-02-05 13:33:58 -0800 | [diff] [blame] | 490 | |
Sho SHIMIZU | 96b39e2 | 2016-01-15 17:35:04 -0800 | [diff] [blame] | 491 | IntentEvent.getEvent(event.value()).ifPresent(e -> notifyDelegate(e)); |
Jonathan Hart | 74c8313 | 2015-02-02 18:37:57 -0800 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
Jonathan Hart | 5573d32 | 2015-01-21 10:13:25 -0800 | [diff] [blame] | 496 | } |
| 497 | |