Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 16 | package org.onosproject.store.resource.impl; |
| 17 | |
| 18 | import java.util.ArrayList; |
| 19 | import java.util.Collection; |
| 20 | import java.util.Collections; |
| 21 | import java.util.HashMap; |
| 22 | import java.util.HashSet; |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | import java.util.Set; |
| 26 | import java.util.stream.Collectors; |
| 27 | |
| 28 | import org.apache.felix.scr.annotations.Component; |
| 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 31 | import org.apache.felix.scr.annotations.Service; |
| 32 | import org.apache.felix.scr.annotations.Activate; |
| 33 | import org.apache.felix.scr.annotations.Deactivate; |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 34 | import org.onlab.util.Bandwidth; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 35 | import org.onosproject.net.OmsPort; |
| 36 | import org.onosproject.net.device.DeviceService; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 37 | import org.slf4j.Logger; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 38 | import org.onlab.util.PositionalParameterStringFormatter; |
| 39 | import org.onosproject.net.Link; |
| 40 | import org.onosproject.net.LinkKey; |
Ayaka Koshibe | 8117f36 | 2015-06-02 13:44:01 -0700 | [diff] [blame] | 41 | import org.onosproject.net.Port; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 42 | import org.onosproject.net.intent.IntentId; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 43 | import org.onosproject.net.resource.link.BandwidthResource; |
| 44 | import org.onosproject.net.resource.link.BandwidthResourceAllocation; |
| 45 | import org.onosproject.net.resource.link.LambdaResource; |
| 46 | import org.onosproject.net.resource.link.LambdaResourceAllocation; |
| 47 | import org.onosproject.net.resource.link.LinkResourceAllocations; |
| 48 | import org.onosproject.net.resource.link.LinkResourceEvent; |
| 49 | import org.onosproject.net.resource.link.LinkResourceStore; |
| 50 | import org.onosproject.net.resource.link.LinkResourceStoreDelegate; |
| 51 | import org.onosproject.net.resource.link.MplsLabel; |
| 52 | import org.onosproject.net.resource.link.MplsLabelResourceAllocation; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 53 | import org.onosproject.net.resource.ResourceAllocation; |
| 54 | import org.onosproject.net.resource.ResourceAllocationException; |
| 55 | import org.onosproject.net.resource.ResourceType; |
| 56 | import org.onosproject.store.AbstractStore; |
| 57 | import org.onosproject.store.serializers.KryoNamespaces; |
| 58 | import org.onosproject.store.service.ConsistentMap; |
| 59 | import org.onosproject.store.service.Serializer; |
| 60 | import org.onosproject.store.service.StorageService; |
| 61 | import org.onosproject.store.service.TransactionContext; |
| 62 | import org.onosproject.store.service.TransactionException; |
| 63 | import org.onosproject.store.service.TransactionalMap; |
| 64 | import org.onosproject.store.service.Versioned; |
| 65 | |
| 66 | import com.google.common.collect.ImmutableList; |
| 67 | import com.google.common.collect.ImmutableSet; |
| 68 | import com.google.common.collect.Sets; |
| 69 | |
| 70 | import static com.google.common.base.Preconditions.checkNotNull; |
| 71 | import static com.google.common.base.Preconditions.checkState; |
| 72 | import static org.slf4j.LoggerFactory.getLogger; |
| 73 | import static org.onosproject.net.AnnotationKeys.BANDWIDTH; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * Store that manages link resources using Copycat-backed TransactionalMaps. |
| 77 | */ |
Ayaka Koshibe | 474ef5f | 2015-05-01 15:24:51 -0700 | [diff] [blame] | 78 | @Component(immediate = true, enabled = true) |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 79 | @Service |
| 80 | public class ConsistentLinkResourceStore extends |
| 81 | AbstractStore<LinkResourceEvent, LinkResourceStoreDelegate> implements |
| 82 | LinkResourceStore { |
| 83 | |
| 84 | private final Logger log = getLogger(getClass()); |
| 85 | |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 86 | private static final BandwidthResource DEFAULT_BANDWIDTH = new BandwidthResource(Bandwidth.mbps(1_000)); |
| 87 | private static final BandwidthResource EMPTY_BW = new BandwidthResource(Bandwidth.bps(0)); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 88 | |
| 89 | // Smallest non-reserved MPLS label |
| 90 | private static final int MIN_UNRESERVED_LABEL = 0x10; |
| 91 | // Max non-reserved MPLS label = 239 |
| 92 | private static final int MAX_UNRESERVED_LABEL = 0xEF; |
| 93 | |
| 94 | // table to store current allocations |
| 95 | /** LinkKey -> List<LinkResourceAllocations>. */ |
Ayaka Koshibe | bcb0237 | 2015-06-01 10:56:42 -0700 | [diff] [blame] | 96 | private static final String LINK_RESOURCE_ALLOCATIONS = "LinkAllocations"; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 97 | |
| 98 | /** IntentId -> LinkResourceAllocations. */ |
Ayaka Koshibe | bcb0237 | 2015-06-01 10:56:42 -0700 | [diff] [blame] | 99 | private static final String INTENT_ALLOCATIONS = "LinkIntentAllocations"; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 100 | |
Sho SHIMIZU | 200a739 | 2015-07-23 16:28:35 -0700 | [diff] [blame] | 101 | private static final Serializer SERIALIZER = Serializer.using(KryoNamespaces.API); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 102 | |
| 103 | // for reading committed values. |
| 104 | private ConsistentMap<IntentId, LinkResourceAllocations> intentAllocMap; |
| 105 | |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 107 | protected StorageService storageService; |
| 108 | |
| 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 110 | protected DeviceService deviceService; |
| 111 | |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 112 | @Activate |
| 113 | public void activate() { |
| 114 | intentAllocMap = storageService.<IntentId, LinkResourceAllocations>consistentMapBuilder() |
| 115 | .withName(INTENT_ALLOCATIONS) |
| 116 | .withSerializer(SERIALIZER) |
| 117 | .build(); |
| 118 | log.info("Started"); |
| 119 | } |
| 120 | |
| 121 | @Deactivate |
| 122 | public void deactivate() { |
| 123 | log.info("Stopped"); |
| 124 | } |
| 125 | |
| 126 | private TransactionalMap<IntentId, LinkResourceAllocations> getIntentAllocs(TransactionContext tx) { |
| 127 | return tx.getTransactionalMap(INTENT_ALLOCATIONS, SERIALIZER); |
| 128 | } |
| 129 | |
| 130 | private TransactionalMap<LinkKey, List<LinkResourceAllocations>> getLinkAllocs(TransactionContext tx) { |
| 131 | return tx.getTransactionalMap(LINK_RESOURCE_ALLOCATIONS, SERIALIZER); |
| 132 | } |
| 133 | |
| 134 | private TransactionContext getTxContext() { |
| 135 | return storageService.transactionContextBuilder().build(); |
| 136 | } |
| 137 | |
| 138 | private Set<? extends ResourceAllocation> getResourceCapacity(ResourceType type, Link link) { |
| 139 | if (type == ResourceType.BANDWIDTH) { |
| 140 | return ImmutableSet.of(getBandwidthResourceCapacity(link)); |
| 141 | } |
| 142 | if (type == ResourceType.LAMBDA) { |
| 143 | return getLambdaResourceCapacity(link); |
| 144 | } |
| 145 | if (type == ResourceType.MPLS_LABEL) { |
| 146 | return getMplsResourceCapacity(); |
| 147 | } |
| 148 | return ImmutableSet.of(); |
| 149 | } |
| 150 | |
| 151 | private Set<LambdaResourceAllocation> getLambdaResourceCapacity(Link link) { |
| 152 | Set<LambdaResourceAllocation> allocations = new HashSet<>(); |
Ayaka Koshibe | 8117f36 | 2015-06-02 13:44:01 -0700 | [diff] [blame] | 153 | Port port = deviceService.getPort(link.src().deviceId(), link.src().port()); |
| 154 | if (port instanceof OmsPort) { |
| 155 | OmsPort omsPort = (OmsPort) port; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 156 | |
Ayaka Koshibe | 8117f36 | 2015-06-02 13:44:01 -0700 | [diff] [blame] | 157 | // Assume fixed grid for now |
| 158 | for (int i = 0; i < omsPort.totalChannels(); i++) { |
| 159 | allocations.add(new LambdaResourceAllocation(LambdaResource.valueOf(i))); |
| 160 | } |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 161 | } |
| 162 | return allocations; |
| 163 | } |
| 164 | |
| 165 | private BandwidthResourceAllocation getBandwidthResourceCapacity(Link link) { |
| 166 | |
| 167 | // if Link annotation exist, use them |
| 168 | // if all fails, use DEFAULT_BANDWIDTH |
Sho SHIMIZU | 63feca7 | 2015-05-07 10:44:25 -0700 | [diff] [blame] | 169 | BandwidthResource bandwidth = null; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 170 | String strBw = link.annotations().value(BANDWIDTH); |
| 171 | if (strBw != null) { |
| 172 | try { |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 173 | bandwidth = new BandwidthResource(Bandwidth.mbps(Double.parseDouble(strBw))); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 174 | } catch (NumberFormatException e) { |
| 175 | // do nothings |
| 176 | bandwidth = null; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if (bandwidth == null) { |
| 181 | // fall back, use fixed default |
| 182 | bandwidth = DEFAULT_BANDWIDTH; |
| 183 | } |
| 184 | return new BandwidthResourceAllocation(bandwidth); |
| 185 | } |
| 186 | |
| 187 | private Set<MplsLabelResourceAllocation> getMplsResourceCapacity() { |
| 188 | Set<MplsLabelResourceAllocation> allocations = new HashSet<>(); |
| 189 | //Ignoring reserved labels of 0 through 15 |
| 190 | for (int i = MIN_UNRESERVED_LABEL; i <= MAX_UNRESERVED_LABEL; i++) { |
| 191 | allocations.add(new MplsLabelResourceAllocation(MplsLabel |
| 192 | .valueOf(i))); |
| 193 | |
| 194 | } |
| 195 | return allocations; |
| 196 | } |
| 197 | |
| 198 | private Map<ResourceType, Set<? extends ResourceAllocation>> getResourceCapacity(Link link) { |
| 199 | Map<ResourceType, Set<? extends ResourceAllocation>> caps = new HashMap<>(); |
| 200 | for (ResourceType type : ResourceType.values()) { |
| 201 | Set<? extends ResourceAllocation> cap = getResourceCapacity(type, link); |
Sho SHIMIZU | f618cb0 | 2015-10-02 14:43:50 -0700 | [diff] [blame^] | 202 | caps.put(type, cap); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 203 | } |
| 204 | return caps; |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public Set<ResourceAllocation> getFreeResources(Link link) { |
| 209 | TransactionContext tx = getTxContext(); |
| 210 | |
| 211 | tx.begin(); |
| 212 | try { |
| 213 | Map<ResourceType, Set<? extends ResourceAllocation>> freeResources = getFreeResourcesEx(tx, link); |
| 214 | Set<ResourceAllocation> allFree = new HashSet<>(); |
| 215 | freeResources.values().forEach(allFree::addAll); |
| 216 | return allFree; |
| 217 | } finally { |
| 218 | tx.abort(); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | private Map<ResourceType, Set<? extends ResourceAllocation>> getFreeResourcesEx(TransactionContext tx, Link link) { |
| 223 | checkNotNull(tx); |
| 224 | checkNotNull(link); |
| 225 | |
| 226 | Map<ResourceType, Set<? extends ResourceAllocation>> free = new HashMap<>(); |
| 227 | final Map<ResourceType, Set<? extends ResourceAllocation>> caps = getResourceCapacity(link); |
| 228 | final Iterable<LinkResourceAllocations> allocations = getAllocations(tx, link); |
| 229 | |
| 230 | for (ResourceType type : ResourceType.values()) { |
| 231 | // there should be class/category of resources |
| 232 | |
| 233 | switch (type) { |
| 234 | case BANDWIDTH: |
| 235 | Set<? extends ResourceAllocation> bw = caps.get(type); |
| 236 | if (bw == null || bw.isEmpty()) { |
| 237 | bw = Sets.newHashSet(new BandwidthResourceAllocation(EMPTY_BW)); |
| 238 | } |
| 239 | |
| 240 | BandwidthResourceAllocation cap = (BandwidthResourceAllocation) bw.iterator().next(); |
| 241 | double freeBw = cap.bandwidth().toDouble(); |
| 242 | |
| 243 | // enumerate current allocations, subtracting resources |
Sho SHIMIZU | 88d36e2 | 2015-10-02 14:23:46 -0700 | [diff] [blame] | 244 | double allocatedBw = ImmutableList.copyOf(allocations).stream() |
| 245 | .flatMap(x -> x.getResourceAllocation(link).stream()) |
| 246 | .filter(x -> x instanceof BandwidthResourceAllocation) |
| 247 | .map(x -> (BandwidthResourceAllocation) x) |
| 248 | .mapToDouble(x -> x.bandwidth().toDouble()) |
| 249 | .sum(); |
| 250 | freeBw -= allocatedBw; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 251 | |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 252 | free.put(type, Sets.newHashSet( |
| 253 | new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(freeBw))))); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 254 | break; |
| 255 | case LAMBDA: |
| 256 | Set<? extends ResourceAllocation> lmd = caps.get(type); |
| 257 | if (lmd == null || lmd.isEmpty()) { |
| 258 | // nothing left |
| 259 | break; |
| 260 | } |
Sho SHIMIZU | 88d36e2 | 2015-10-02 14:23:46 -0700 | [diff] [blame] | 261 | Set<LambdaResourceAllocation> freeL = lmd.stream() |
| 262 | .filter(x -> x instanceof LambdaResourceAllocation) |
| 263 | .map(x -> (LambdaResourceAllocation) x) |
| 264 | .collect(Collectors.toSet()); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 265 | |
| 266 | // enumerate current allocations, removing resources |
Sho SHIMIZU | 88d36e2 | 2015-10-02 14:23:46 -0700 | [diff] [blame] | 267 | List<LambdaResourceAllocation> allocatedLambda = ImmutableList.copyOf(allocations).stream() |
| 268 | .flatMap(x -> x.getResourceAllocation(link).stream()) |
| 269 | .filter(x -> x instanceof LambdaResourceAllocation) |
| 270 | .map(x -> (LambdaResourceAllocation) x) |
| 271 | .collect(Collectors.toList()); |
| 272 | freeL.removeAll(allocatedLambda); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 273 | |
| 274 | free.put(type, freeL); |
| 275 | break; |
| 276 | case MPLS_LABEL: |
| 277 | Set<? extends ResourceAllocation> mpls = caps.get(type); |
| 278 | if (mpls == null || mpls.isEmpty()) { |
| 279 | // nothing left |
| 280 | break; |
| 281 | } |
Sho SHIMIZU | 88d36e2 | 2015-10-02 14:23:46 -0700 | [diff] [blame] | 282 | Set<MplsLabelResourceAllocation> freeLabel = mpls.stream() |
| 283 | .filter(x -> x instanceof MplsLabelResourceAllocation) |
| 284 | .map(x -> (MplsLabelResourceAllocation) x) |
| 285 | .collect(Collectors.toSet()); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 286 | |
| 287 | // enumerate current allocations, removing resources |
Sho SHIMIZU | 88d36e2 | 2015-10-02 14:23:46 -0700 | [diff] [blame] | 288 | List<MplsLabelResourceAllocation> allocatedLabel = ImmutableList.copyOf(allocations).stream() |
| 289 | .flatMap(x -> x.getResourceAllocation(link).stream()) |
| 290 | .filter(x -> x instanceof MplsLabelResourceAllocation) |
| 291 | .map(x -> (MplsLabelResourceAllocation) x) |
| 292 | .collect(Collectors.toList()); |
| 293 | freeLabel.removeAll(allocatedLabel); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 294 | |
| 295 | free.put(type, freeLabel); |
| 296 | break; |
| 297 | default: |
| 298 | log.debug("unsupported ResourceType {}", type); |
| 299 | break; |
| 300 | } |
| 301 | } |
| 302 | return free; |
| 303 | } |
| 304 | |
| 305 | @Override |
| 306 | public void allocateResources(LinkResourceAllocations allocations) { |
| 307 | checkNotNull(allocations); |
| 308 | TransactionContext tx = getTxContext(); |
| 309 | |
| 310 | tx.begin(); |
| 311 | try { |
| 312 | TransactionalMap<IntentId, LinkResourceAllocations> intentAllocs = getIntentAllocs(tx); |
| 313 | intentAllocs.put(allocations.intentId(), allocations); |
| 314 | allocations.links().forEach(link -> allocateLinkResource(tx, link, allocations)); |
| 315 | tx.commit(); |
| 316 | } catch (Exception e) { |
| 317 | log.error("Exception thrown, rolling back", e); |
| 318 | tx.abort(); |
| 319 | throw e; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | private void allocateLinkResource(TransactionContext tx, Link link, |
| 324 | LinkResourceAllocations allocations) { |
| 325 | // requested resources |
| 326 | Set<ResourceAllocation> reqs = allocations.getResourceAllocation(link); |
| 327 | Map<ResourceType, Set<? extends ResourceAllocation>> available = getFreeResourcesEx(tx, link); |
| 328 | for (ResourceAllocation req : reqs) { |
| 329 | Set<? extends ResourceAllocation> avail = available.get(req.type()); |
| 330 | if (req instanceof BandwidthResourceAllocation) { |
| 331 | // check if allocation should be accepted |
| 332 | if (avail.isEmpty()) { |
| 333 | checkState(!avail.isEmpty(), |
| 334 | "There's no Bandwidth resource on %s?", |
| 335 | link); |
| 336 | } |
| 337 | BandwidthResourceAllocation bw = (BandwidthResourceAllocation) avail.iterator().next(); |
| 338 | double bwLeft = bw.bandwidth().toDouble(); |
| 339 | BandwidthResourceAllocation bwReq = ((BandwidthResourceAllocation) req); |
| 340 | bwLeft -= bwReq.bandwidth().toDouble(); |
| 341 | if (bwLeft < 0) { |
| 342 | throw new ResourceAllocationException( |
| 343 | PositionalParameterStringFormatter.format( |
| 344 | "Unable to allocate bandwidth for link {} " |
| 345 | + " requested amount is {} current allocation is {}", |
| 346 | link, |
| 347 | bwReq.bandwidth().toDouble(), |
| 348 | bw)); |
| 349 | } |
| 350 | } else if (req instanceof LambdaResourceAllocation) { |
| 351 | LambdaResourceAllocation lambdaAllocation = (LambdaResourceAllocation) req; |
| 352 | // check if allocation should be accepted |
| 353 | if (!avail.contains(req)) { |
| 354 | // requested lambda was not available |
| 355 | throw new ResourceAllocationException( |
| 356 | PositionalParameterStringFormatter.format( |
| 357 | "Unable to allocate lambda for link {} lambda is {}", |
| 358 | link, |
| 359 | lambdaAllocation.lambda().toInt())); |
| 360 | } |
| 361 | } else if (req instanceof MplsLabelResourceAllocation) { |
| 362 | MplsLabelResourceAllocation mplsAllocation = (MplsLabelResourceAllocation) req; |
| 363 | if (!avail.contains(req)) { |
| 364 | throw new ResourceAllocationException( |
| 365 | PositionalParameterStringFormatter |
| 366 | .format("Unable to allocate MPLS label for link " |
| 367 | + "{} MPLS label is {}", |
| 368 | link, |
| 369 | mplsAllocation |
| 370 | .mplsLabel() |
| 371 | .toString())); |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | // all requests allocatable => add allocation |
| 376 | final LinkKey linkKey = LinkKey.linkKey(link); |
| 377 | TransactionalMap<LinkKey, List<LinkResourceAllocations>> linkAllocs = getLinkAllocs(tx); |
| 378 | List<LinkResourceAllocations> before = linkAllocs.get(linkKey); |
| 379 | if (before == null) { |
| 380 | List<LinkResourceAllocations> after = new ArrayList<>(); |
| 381 | after.add(allocations); |
| 382 | before = linkAllocs.putIfAbsent(linkKey, after); |
| 383 | if (before != null) { |
| 384 | // concurrent allocation detected, retry transaction : is this needed? |
| 385 | log.warn("Concurrent Allocation, retrying"); |
| 386 | throw new TransactionException(); |
| 387 | } |
| 388 | } else { |
| 389 | List<LinkResourceAllocations> after = new ArrayList<>(before.size() + 1); |
| 390 | after.addAll(before); |
| 391 | after.add(allocations); |
| 392 | linkAllocs.replace(linkKey, before, after); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | @Override |
| 397 | public LinkResourceEvent releaseResources(LinkResourceAllocations allocations) { |
| 398 | checkNotNull(allocations); |
| 399 | |
| 400 | final IntentId intentId = allocations.intentId(); |
| 401 | final Collection<Link> links = allocations.links(); |
| 402 | boolean success = false; |
| 403 | do { |
| 404 | TransactionContext tx = getTxContext(); |
| 405 | tx.begin(); |
| 406 | try { |
| 407 | TransactionalMap<IntentId, LinkResourceAllocations> intentAllocs = getIntentAllocs(tx); |
| 408 | intentAllocs.remove(intentId); |
| 409 | |
| 410 | TransactionalMap<LinkKey, List<LinkResourceAllocations>> linkAllocs = getLinkAllocs(tx); |
| 411 | links.forEach(link -> { |
| 412 | final LinkKey linkId = LinkKey.linkKey(link); |
| 413 | |
| 414 | List<LinkResourceAllocations> before = linkAllocs.get(linkId); |
| 415 | if (before == null || before.isEmpty()) { |
| 416 | // something is wrong, but it is already freed |
| 417 | log.warn("There was no resource left to release on {}", linkId); |
| 418 | return; |
| 419 | } |
| 420 | List<LinkResourceAllocations> after = new ArrayList<>(before); |
| 421 | after.remove(allocations); |
| 422 | linkAllocs.replace(linkId, before, after); |
| 423 | }); |
| 424 | tx.commit(); |
| 425 | success = true; |
| 426 | } catch (TransactionException e) { |
| 427 | log.debug("Transaction failed, retrying", e); |
| 428 | tx.abort(); |
| 429 | } catch (Exception e) { |
| 430 | log.error("Exception thrown during releaseResource {}", allocations, e); |
| 431 | tx.abort(); |
| 432 | throw e; |
| 433 | } |
| 434 | } while (!success); |
| 435 | |
| 436 | // Issue events to force recompilation of intents. |
| 437 | final List<LinkResourceAllocations> releasedResources = ImmutableList.of(allocations); |
| 438 | return new LinkResourceEvent( |
| 439 | LinkResourceEvent.Type.ADDITIONAL_RESOURCES_AVAILABLE, |
| 440 | releasedResources); |
| 441 | |
| 442 | } |
| 443 | |
| 444 | @Override |
| 445 | public LinkResourceAllocations getAllocations(IntentId intentId) { |
| 446 | checkNotNull(intentId); |
| 447 | Versioned<LinkResourceAllocations> alloc = null; |
| 448 | try { |
| 449 | alloc = intentAllocMap.get(intentId); |
| 450 | } catch (Exception e) { |
| 451 | log.warn("Could not read resource allocation information", e); |
| 452 | } |
| 453 | return alloc == null ? null : alloc.value(); |
| 454 | } |
| 455 | |
| 456 | @Override |
| 457 | public Iterable<LinkResourceAllocations> getAllocations(Link link) { |
| 458 | checkNotNull(link); |
| 459 | TransactionContext tx = getTxContext(); |
| 460 | Iterable<LinkResourceAllocations> res = null; |
| 461 | tx.begin(); |
| 462 | try { |
| 463 | res = getAllocations(tx, link); |
| 464 | } finally { |
| 465 | tx.abort(); |
| 466 | } |
| 467 | return res == null ? Collections.emptyList() : res; |
| 468 | } |
| 469 | |
| 470 | @Override |
| 471 | public Iterable<LinkResourceAllocations> getAllocations() { |
| 472 | try { |
| 473 | Set<LinkResourceAllocations> allocs = |
| 474 | intentAllocMap.values().stream().map(Versioned::value).collect(Collectors.toSet()); |
| 475 | return ImmutableSet.copyOf(allocs); |
| 476 | } catch (Exception e) { |
| 477 | log.warn("Could not read resource allocation information", e); |
| 478 | } |
| 479 | return ImmutableSet.of(); |
| 480 | } |
| 481 | |
| 482 | private Iterable<LinkResourceAllocations> getAllocations(TransactionContext tx, Link link) { |
| 483 | checkNotNull(tx); |
| 484 | checkNotNull(link); |
| 485 | final LinkKey key = LinkKey.linkKey(link); |
| 486 | TransactionalMap<LinkKey, List<LinkResourceAllocations>> linkAllocs = getLinkAllocs(tx); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 487 | |
Sho SHIMIZU | 88d36e2 | 2015-10-02 14:23:46 -0700 | [diff] [blame] | 488 | List<LinkResourceAllocations> res = linkAllocs.get(key); |
| 489 | if (res != null) { |
| 490 | return res; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 491 | } |
Sho SHIMIZU | 88d36e2 | 2015-10-02 14:23:46 -0700 | [diff] [blame] | 492 | |
| 493 | res = linkAllocs.putIfAbsent(key, new ArrayList<>()); |
| 494 | if (res == null) { |
| 495 | return Collections.emptyList(); |
| 496 | } else { |
| 497 | return res; |
| 498 | } |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | } |