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