Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 1 | /* |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 2 | * Copyright 2015-2016 Open Networking Laboratory |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [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.newresource.impl; |
| 17 | |
| 18 | import com.google.common.annotations.Beta; |
Sho SHIMIZU | e7db614 | 2015-11-04 11:24:22 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 26 | import org.onlab.util.GuavaCollectors; |
Thomas Vachuska | 762a2d8 | 2016-01-04 10:25:20 -0800 | [diff] [blame] | 27 | import org.onlab.util.Tools; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 28 | import org.onosproject.net.newresource.ResourceAllocation; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 29 | import org.onosproject.net.newresource.ResourceConsumer; |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 30 | import org.onosproject.net.newresource.ResourceEvent; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 31 | import org.onosproject.net.newresource.ResourceId; |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 32 | import org.onosproject.net.newresource.Resource; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 33 | import org.onosproject.net.newresource.ResourceStore; |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 34 | import org.onosproject.net.newresource.ResourceStoreDelegate; |
| 35 | import org.onosproject.store.AbstractStore; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 36 | import org.onosproject.store.serializers.KryoNamespaces; |
| 37 | import org.onosproject.store.service.ConsistentMap; |
Thomas Vachuska | 762a2d8 | 2016-01-04 10:25:20 -0800 | [diff] [blame] | 38 | import org.onosproject.store.service.ConsistentMapException; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 39 | import org.onosproject.store.service.Serializer; |
| 40 | import org.onosproject.store.service.StorageService; |
| 41 | import org.onosproject.store.service.TransactionContext; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 42 | import org.onosproject.store.service.TransactionalMap; |
| 43 | import org.onosproject.store.service.Versioned; |
| 44 | import org.slf4j.Logger; |
| 45 | import org.slf4j.LoggerFactory; |
| 46 | |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 47 | import java.util.Arrays; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 48 | import java.util.Collection; |
| 49 | import java.util.Iterator; |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 50 | import java.util.LinkedHashSet; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 51 | import java.util.List; |
| 52 | import java.util.Map; |
| 53 | import java.util.Optional; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 54 | import java.util.Set; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 55 | import java.util.stream.Collectors; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 56 | import java.util.stream.Stream; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 57 | |
| 58 | import static com.google.common.base.Preconditions.checkArgument; |
| 59 | import static com.google.common.base.Preconditions.checkNotNull; |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 60 | import static org.onosproject.net.newresource.ResourceEvent.Type.*; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Implementation of ResourceStore using TransactionalMap. |
| 64 | */ |
Sho SHIMIZU | 9a2b829 | 2015-10-28 13:00:16 -0700 | [diff] [blame] | 65 | @Component(immediate = true) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 66 | @Service |
| 67 | @Beta |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 68 | public class ConsistentResourceStore extends AbstractStore<ResourceEvent, ResourceStoreDelegate> |
| 69 | implements ResourceStore { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 70 | private static final Logger log = LoggerFactory.getLogger(ConsistentResourceStore.class); |
| 71 | |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 72 | private static final String DISCRETE_CONSUMER_MAP = "onos-discrete-consumers"; |
| 73 | private static final String CONTINUOUS_CONSUMER_MAP = "onos-continuous-consumers"; |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 74 | private static final String CHILD_MAP = "onos-resource-children"; |
| 75 | private static final Serializer SERIALIZER = Serializer.using( |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 76 | Arrays.asList(KryoNamespaces.BASIC, KryoNamespaces.API), |
| 77 | ContinuousResourceAllocation.class); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 78 | |
Thomas Vachuska | 762a2d8 | 2016-01-04 10:25:20 -0800 | [diff] [blame] | 79 | // TODO: We should provide centralized values for this |
| 80 | private static final int MAX_RETRIES = 5; |
| 81 | private static final int RETRY_DELAY = 1_000; // millis |
| 82 | |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 84 | protected StorageService service; |
| 85 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 86 | private ConsistentMap<Resource.Discrete, ResourceConsumer> discreteConsumers; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 87 | private ConsistentMap<ResourceId, ContinuousResourceAllocation> continuousConsumers; |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 88 | private ConsistentMap<Resource.Discrete, Set<Resource>> childMap; |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 89 | |
| 90 | @Activate |
| 91 | public void activate() { |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 92 | discreteConsumers = service.<Resource.Discrete, ResourceConsumer>consistentMapBuilder() |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 93 | .withName(DISCRETE_CONSUMER_MAP) |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 94 | .withSerializer(SERIALIZER) |
| 95 | .build(); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 96 | continuousConsumers = service.<ResourceId, ContinuousResourceAllocation>consistentMapBuilder() |
| 97 | .withName(CONTINUOUS_CONSUMER_MAP) |
| 98 | .withSerializer(SERIALIZER) |
| 99 | .build(); |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 100 | childMap = service.<Resource.Discrete, Set<Resource>>consistentMapBuilder() |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 101 | .withName(CHILD_MAP) |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 102 | .withSerializer(SERIALIZER) |
| 103 | .build(); |
Sho SHIMIZU | e7db614 | 2015-11-04 11:24:22 -0800 | [diff] [blame] | 104 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 105 | Tools.retryable(() -> childMap.put(Resource.ROOT, new LinkedHashSet<>()), |
Thomas Vachuska | 762a2d8 | 2016-01-04 10:25:20 -0800 | [diff] [blame] | 106 | ConsistentMapException.class, MAX_RETRIES, RETRY_DELAY); |
Madan Jampani | c7f49f9 | 2015-12-10 11:35:06 -0800 | [diff] [blame] | 107 | log.info("Started"); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 111 | public List<ResourceConsumer> getConsumers(Resource resource) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 112 | checkNotNull(resource); |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 113 | checkArgument(resource instanceof Resource.Discrete || resource instanceof Resource.Continuous); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 114 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 115 | if (resource instanceof Resource.Discrete) { |
| 116 | return getConsumer((Resource.Discrete) resource); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 117 | } else { |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 118 | return getConsumer((Resource.Continuous) resource); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 122 | private List<ResourceConsumer> getConsumer(Resource.Discrete resource) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 123 | Versioned<ResourceConsumer> consumer = discreteConsumers.get(resource); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 124 | if (consumer == null) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 125 | return ImmutableList.of(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 128 | return ImmutableList.of(consumer.value()); |
| 129 | } |
| 130 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 131 | private List<ResourceConsumer> getConsumer(Resource.Continuous resource) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 132 | Versioned<ContinuousResourceAllocation> allocations = continuousConsumers.get(resource.id()); |
| 133 | if (allocations == null) { |
| 134 | return ImmutableList.of(); |
| 135 | } |
| 136 | |
| 137 | return allocations.value().allocations().stream() |
| 138 | .filter(x -> x.resource().equals(resource)) |
| 139 | .map(ResourceAllocation::consumer) |
| 140 | .collect(GuavaCollectors.toImmutableList()); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 144 | public boolean register(List<Resource> resources) { |
Sho SHIMIZU | 83e17a0 | 2015-08-20 14:07:05 -0700 | [diff] [blame] | 145 | checkNotNull(resources); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 146 | |
| 147 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 148 | tx.begin(); |
| 149 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 150 | TransactionalMap<Resource.Discrete, Set<Resource>> childTxMap = |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 151 | tx.getTransactionalMap(CHILD_MAP, SERIALIZER); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 152 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 153 | Map<Resource.Discrete, List<Resource>> resourceMap = resources.stream() |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 154 | .filter(x -> x.parent().isPresent()) |
| 155 | .collect(Collectors.groupingBy(x -> x.parent().get())); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 156 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 157 | for (Map.Entry<Resource.Discrete, List<Resource>> entry: resourceMap.entrySet()) { |
| 158 | Optional<Resource.Discrete> child = lookup(childTxMap, entry.getKey()); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 159 | if (!child.isPresent()) { |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 160 | return abortTransaction(tx); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 163 | if (!appendValues(childTxMap, entry.getKey(), entry.getValue())) { |
| 164 | return abortTransaction(tx); |
| 165 | } |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 166 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 167 | |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 168 | boolean success = tx.commit(); |
| 169 | if (success) { |
| 170 | List<ResourceEvent> events = resources.stream() |
| 171 | .filter(x -> x.parent().isPresent()) |
| 172 | .map(x -> new ResourceEvent(RESOURCE_ADDED, x)) |
| 173 | .collect(Collectors.toList()); |
| 174 | notifyDelegate(events); |
| 175 | } |
| 176 | return success; |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 180 | public boolean unregister(List<Resource> resources) { |
Sho SHIMIZU | 83e17a0 | 2015-08-20 14:07:05 -0700 | [diff] [blame] | 181 | checkNotNull(resources); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 182 | |
| 183 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 184 | tx.begin(); |
| 185 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 186 | TransactionalMap<Resource.Discrete, Set<Resource>> childTxMap = |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 187 | tx.getTransactionalMap(CHILD_MAP, SERIALIZER); |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 188 | TransactionalMap<Resource.Discrete, ResourceConsumer> discreteConsumerTxMap = |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 189 | tx.getTransactionalMap(DISCRETE_CONSUMER_MAP, SERIALIZER); |
| 190 | TransactionalMap<ResourceId, ContinuousResourceAllocation> continuousConsumerTxMap = |
| 191 | tx.getTransactionalMap(CONTINUOUS_CONSUMER_MAP, SERIALIZER); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 192 | |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 193 | // Extract Discrete instances from resources |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 194 | Map<Resource.Discrete, List<Resource>> resourceMap = resources.stream() |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 195 | .filter(x -> x.parent().isPresent()) |
| 196 | .collect(Collectors.groupingBy(x -> x.parent().get())); |
Sho SHIMIZU | 83e17a0 | 2015-08-20 14:07:05 -0700 | [diff] [blame] | 197 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 198 | // even if one of the resources is allocated to a consumer, |
| 199 | // all unregistrations are regarded as failure |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 200 | for (Map.Entry<Resource.Discrete, List<Resource>> entry: resourceMap.entrySet()) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 201 | boolean allocated = entry.getValue().stream().anyMatch(x -> { |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 202 | if (x instanceof Resource.Discrete) { |
| 203 | return discreteConsumerTxMap.get((Resource.Discrete) x) != null; |
| 204 | } else if (x instanceof Resource.Continuous) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 205 | ContinuousResourceAllocation allocations = continuousConsumerTxMap.get(x.id()); |
| 206 | return allocations != null && !allocations.allocations().isEmpty(); |
| 207 | } else { |
| 208 | return false; |
| 209 | } |
| 210 | }); |
| 211 | if (allocated) { |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 212 | return abortTransaction(tx); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 215 | if (!removeValues(childTxMap, entry.getKey(), entry.getValue())) { |
| 216 | return abortTransaction(tx); |
| 217 | } |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 218 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 219 | |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 220 | boolean success = tx.commit(); |
| 221 | if (success) { |
| 222 | List<ResourceEvent> events = resources.stream() |
| 223 | .filter(x -> x.parent().isPresent()) |
| 224 | .map(x -> new ResourceEvent(RESOURCE_REMOVED, x)) |
| 225 | .collect(Collectors.toList()); |
| 226 | notifyDelegate(events); |
| 227 | } |
| 228 | return success; |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 232 | public boolean allocate(List<Resource> resources, ResourceConsumer consumer) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 233 | checkNotNull(resources); |
| 234 | checkNotNull(consumer); |
| 235 | |
| 236 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 237 | tx.begin(); |
| 238 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 239 | TransactionalMap<Resource.Discrete, Set<Resource>> childTxMap = |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 240 | tx.getTransactionalMap(CHILD_MAP, SERIALIZER); |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 241 | TransactionalMap<Resource.Discrete, ResourceConsumer> discreteConsumerTxMap = |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 242 | tx.getTransactionalMap(DISCRETE_CONSUMER_MAP, SERIALIZER); |
| 243 | TransactionalMap<ResourceId, ContinuousResourceAllocation> continuousConsumerTxMap = |
| 244 | tx.getTransactionalMap(CONTINUOUS_CONSUMER_MAP, SERIALIZER); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 245 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 246 | for (Resource resource: resources) { |
| 247 | if (resource instanceof Resource.Discrete) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 248 | if (!lookup(childTxMap, resource).isPresent()) { |
| 249 | return abortTransaction(tx); |
| 250 | } |
Sho SHIMIZU | d29847f | 2015-08-13 09:10:59 -0700 | [diff] [blame] | 251 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 252 | ResourceConsumer oldValue = discreteConsumerTxMap.put((Resource.Discrete) resource, consumer); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 253 | if (oldValue != null) { |
| 254 | return abortTransaction(tx); |
| 255 | } |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 256 | } else if (resource instanceof Resource.Continuous) { |
| 257 | Optional<Resource.Continuous> continuous = lookup(childTxMap, (Resource.Continuous) resource); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 258 | if (!continuous.isPresent()) { |
| 259 | return abortTransaction(tx); |
| 260 | } |
| 261 | |
| 262 | ContinuousResourceAllocation allocations = continuousConsumerTxMap.get(continuous.get().id()); |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 263 | if (!hasEnoughResource(continuous.get(), (Resource.Continuous) resource, allocations)) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 264 | return abortTransaction(tx); |
| 265 | } |
| 266 | |
| 267 | boolean success = appendValue(continuousConsumerTxMap, |
| 268 | continuous.get(), new ResourceAllocation(continuous.get(), consumer)); |
| 269 | if (!success) { |
| 270 | return abortTransaction(tx); |
| 271 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 272 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 273 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 274 | |
| 275 | return tx.commit(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 279 | public boolean release(List<Resource> resources, List<ResourceConsumer> consumers) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 280 | checkNotNull(resources); |
| 281 | checkNotNull(consumers); |
| 282 | checkArgument(resources.size() == consumers.size()); |
| 283 | |
| 284 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 285 | tx.begin(); |
| 286 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 287 | TransactionalMap<Resource.Discrete, ResourceConsumer> discreteConsumerTxMap = |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 288 | tx.getTransactionalMap(DISCRETE_CONSUMER_MAP, SERIALIZER); |
| 289 | TransactionalMap<ResourceId, ContinuousResourceAllocation> continuousConsumerTxMap = |
| 290 | tx.getTransactionalMap(CONTINUOUS_CONSUMER_MAP, SERIALIZER); |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 291 | Iterator<Resource> resourceIte = resources.iterator(); |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 292 | Iterator<ResourceConsumer> consumerIte = consumers.iterator(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 293 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 294 | while (resourceIte.hasNext() && consumerIte.hasNext()) { |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 295 | Resource resource = resourceIte.next(); |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 296 | ResourceConsumer consumer = consumerIte.next(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 297 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 298 | if (resource instanceof Resource.Discrete) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 299 | // if this single release fails (because the resource is allocated to another consumer, |
| 300 | // the whole release fails |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 301 | if (!discreteConsumerTxMap.remove((Resource.Discrete) resource, consumer)) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 302 | return abortTransaction(tx); |
| 303 | } |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 304 | } else if (resource instanceof Resource.Continuous) { |
| 305 | Resource.Continuous continuous = (Resource.Continuous) resource; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 306 | ContinuousResourceAllocation allocation = continuousConsumerTxMap.get(continuous.id()); |
| 307 | ImmutableList<ResourceAllocation> newAllocations = allocation.allocations().stream() |
| 308 | .filter(x -> !(x.consumer().equals(consumer) && |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 309 | ((Resource.Continuous) x.resource()).value() == continuous.value())) |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 310 | .collect(GuavaCollectors.toImmutableList()); |
| 311 | |
| 312 | if (!continuousConsumerTxMap.replace(continuous.id(), allocation, |
| 313 | new ContinuousResourceAllocation(allocation.original(), newAllocations))) { |
| 314 | return abortTransaction(tx); |
| 315 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 316 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 317 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 318 | |
| 319 | return tx.commit(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 323 | public boolean isAvailable(Resource resource) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 324 | checkNotNull(resource); |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 325 | checkArgument(resource instanceof Resource.Discrete || resource instanceof Resource.Continuous); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 326 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 327 | if (resource instanceof Resource.Discrete) { |
| 328 | return getConsumer((Resource.Discrete) resource).isEmpty(); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 329 | } else { |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 330 | return isAvailable((Resource.Continuous) resource); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 334 | private boolean isAvailable(Resource.Continuous resource) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 335 | Versioned<ContinuousResourceAllocation> allocation = continuousConsumers.get(resource.id()); |
| 336 | if (allocation == null) { |
| 337 | return false; |
| 338 | } |
| 339 | |
| 340 | return hasEnoughResource(allocation.value().original(), resource, allocation.value()); |
| 341 | } |
| 342 | |
| 343 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 344 | public Collection<Resource> getResources(ResourceConsumer consumer) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 345 | checkNotNull(consumer); |
| 346 | |
| 347 | // NOTE: getting all entries may become performance bottleneck |
| 348 | // TODO: revisit for better backend data structure |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 349 | Stream<Resource.Discrete> discreteStream = discreteConsumers.entrySet().stream() |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 350 | .filter(x -> x.getValue().value().equals(consumer)) |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 351 | .map(Map.Entry::getKey); |
| 352 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 353 | Stream<Resource.Continuous> continuousStream = continuousConsumers.values().stream() |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 354 | .flatMap(x -> x.value().allocations().stream() |
| 355 | .map(y -> Maps.immutableEntry(x.value().original(), y))) |
| 356 | .filter(x -> x.getValue().consumer().equals(consumer)) |
| 357 | .map(x -> x.getKey()); |
| 358 | |
| 359 | return Stream.concat(discreteStream, continuousStream).collect(Collectors.toList()); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 362 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 363 | public Collection<Resource> getChildResources(Resource parent) { |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 364 | checkNotNull(parent); |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 365 | checkArgument(parent instanceof Resource.Discrete); |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 366 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 367 | Versioned<Set<Resource>> children = childMap.get((Resource.Discrete) parent); |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 368 | if (children == null) { |
Sho SHIMIZU | 2c0ae12 | 2016-01-20 13:14:38 -0800 | [diff] [blame] | 369 | return ImmutableList.of(); |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | return children.value(); |
| 373 | } |
| 374 | |
| 375 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 376 | public <T> Collection<Resource> getAllocatedResources(Resource parent, Class<T> cls) { |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 377 | checkNotNull(parent); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 378 | checkNotNull(cls); |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 379 | checkArgument(parent instanceof Resource.Discrete); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 380 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 381 | Versioned<Set<Resource>> children = childMap.get((Resource.Discrete) parent); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 382 | if (children == null) { |
Sho SHIMIZU | 2c0ae12 | 2016-01-20 13:14:38 -0800 | [diff] [blame] | 383 | return ImmutableList.of(); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 386 | Stream<Resource.Discrete> discrete = children.value().stream() |
Sho SHIMIZU | c9546a3 | 2015-11-10 11:22:28 -0800 | [diff] [blame] | 387 | .filter(x -> x.last().getClass().equals(cls)) |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 388 | .filter(x -> x instanceof Resource.Discrete) |
| 389 | .map(x -> (Resource.Discrete) x) |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 390 | .filter(discreteConsumers::containsKey); |
| 391 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 392 | Stream<Resource.Continuous> continuous = children.value().stream() |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 393 | .filter(x -> x.last().getClass().equals(cls)) |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 394 | .filter(x -> x instanceof Resource.Continuous) |
| 395 | .map(x -> (Resource.Continuous) x) |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 396 | .filter(x -> continuousConsumers.containsKey(x.id())) |
| 397 | .filter(x -> continuousConsumers.get(x.id()) != null) |
| 398 | .filter(x -> !continuousConsumers.get(x.id()).value().allocations().isEmpty()); |
| 399 | |
| 400 | return Stream.concat(discrete, continuous).collect(Collectors.toList()); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 401 | } |
Sho SHIMIZU | d29847f | 2015-08-13 09:10:59 -0700 | [diff] [blame] | 402 | |
| 403 | /** |
| 404 | * Abort the transaction. |
| 405 | * |
| 406 | * @param tx transaction context |
| 407 | * @return always false |
| 408 | */ |
| 409 | private boolean abortTransaction(TransactionContext tx) { |
| 410 | tx.abort(); |
| 411 | return false; |
| 412 | } |
| 413 | |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 414 | // Appends the specified ResourceAllocation to the existing values stored in the map |
| 415 | private boolean appendValue(TransactionalMap<ResourceId, ContinuousResourceAllocation> map, |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 416 | Resource.Continuous original, ResourceAllocation value) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 417 | ContinuousResourceAllocation oldValue = map.putIfAbsent(original.id(), |
| 418 | new ContinuousResourceAllocation(original, ImmutableList.of(value))); |
| 419 | if (oldValue == null) { |
| 420 | return true; |
| 421 | } |
| 422 | |
| 423 | if (oldValue.allocations().contains(value)) { |
| 424 | // don't write to map because all values are already stored |
| 425 | return true; |
| 426 | } |
| 427 | |
| 428 | ContinuousResourceAllocation newValue = new ContinuousResourceAllocation(original, |
| 429 | ImmutableList.<ResourceAllocation>builder() |
| 430 | .addAll(oldValue.allocations()) |
| 431 | .add(value) |
| 432 | .build()); |
| 433 | return map.replace(original.id(), oldValue, newValue); |
| 434 | } |
Sho SHIMIZU | d29847f | 2015-08-13 09:10:59 -0700 | [diff] [blame] | 435 | /** |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 436 | * Appends the values to the existing values associated with the specified key. |
Sho SHIMIZU | 4568c41 | 2015-08-21 16:39:07 -0700 | [diff] [blame] | 437 | * If the map already has all the given values, appending will not happen. |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 438 | * |
| 439 | * @param map map holding multiple values for a key |
| 440 | * @param key key specifying values |
| 441 | * @param values values to be appended |
| 442 | * @param <K> type of the key |
| 443 | * @param <V> type of the element of the list |
| 444 | * @return true if the operation succeeds, false otherwise. |
| 445 | */ |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 446 | private <K, V> boolean appendValues(TransactionalMap<K, Set<V>> map, K key, List<V> values) { |
| 447 | Set<V> oldValues = map.putIfAbsent(key, new LinkedHashSet<>(values)); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 448 | if (oldValues == null) { |
Sho SHIMIZU | 93a74b3 | 2015-11-09 11:48:23 -0800 | [diff] [blame] | 449 | return true; |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 452 | if (oldValues.containsAll(values)) { |
Sho SHIMIZU | 4568c41 | 2015-08-21 16:39:07 -0700 | [diff] [blame] | 453 | // don't write to map because all values are already stored |
| 454 | return true; |
| 455 | } |
| 456 | |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 457 | LinkedHashSet<V> newValues = new LinkedHashSet<>(oldValues); |
| 458 | newValues.addAll(values); |
| 459 | return map.replace(key, oldValues, newValues); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /** |
Sho SHIMIZU | ba1f83b | 2015-10-14 08:11:20 -0700 | [diff] [blame] | 463 | * Removes the values from the existing values associated with the specified key. |
Sho SHIMIZU | 5618ee5 | 2015-08-21 17:19:44 -0700 | [diff] [blame] | 464 | * If the map doesn't contain the given values, removal will not happen. |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 465 | * |
| 466 | * @param map map holding multiple values for a key |
| 467 | * @param key key specifying values |
| 468 | * @param values values to be removed |
| 469 | * @param <K> type of the key |
| 470 | * @param <V> type of the element of the list |
| 471 | * @return true if the operation succeeds, false otherwise |
| 472 | */ |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 473 | private <K, V> boolean removeValues(TransactionalMap<K, Set<V>> map, K key, List<? extends V> values) { |
| 474 | Set<V> oldValues = map.putIfAbsent(key, new LinkedHashSet<>()); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 475 | if (oldValues == null) { |
Sho SHIMIZU | 93a74b3 | 2015-11-09 11:48:23 -0800 | [diff] [blame] | 476 | return true; |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 479 | if (values.stream().allMatch(x -> !oldValues.contains(x))) { |
Sho SHIMIZU | 5618ee5 | 2015-08-21 17:19:44 -0700 | [diff] [blame] | 480 | // don't write map because none of the values are stored |
| 481 | return true; |
| 482 | } |
| 483 | |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 484 | LinkedHashSet<V> newValues = new LinkedHashSet<>(oldValues); |
| 485 | newValues.removeAll(values); |
| 486 | return map.replace(key, oldValues, newValues); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | /** |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 490 | * Returns the resource which has the same key as the key of the specified resource |
| 491 | * in the list as a value of the map. |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 492 | * |
| 493 | * @param map map storing parent - child relationship of resources |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 494 | * @param resource resource to be checked for its key |
| 495 | * @return the resource which is regarded as the same as the specified resource |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 496 | */ |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 497 | // Naive implementation, which traverses all elements in the list |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 498 | private <T extends Resource> Optional<T> lookup( |
| 499 | TransactionalMap<Resource.Discrete, Set<Resource>> map, T resource) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 500 | // if it is root, always returns itself |
Sho SHIMIZU | c9546a3 | 2015-11-10 11:22:28 -0800 | [diff] [blame] | 501 | if (!resource.parent().isPresent()) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 502 | return Optional.of(resource); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 505 | Set<Resource> values = map.get(resource.parent().get()); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 506 | if (values == null) { |
| 507 | return Optional.empty(); |
| 508 | } |
| 509 | |
| 510 | @SuppressWarnings("unchecked") |
| 511 | Optional<T> result = values.stream() |
| 512 | .filter(x -> x.id().equals(resource.id())) |
| 513 | .map(x -> (T) x) |
| 514 | .findFirst(); |
| 515 | return result; |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * Checks if there is enough resource volume to allocated the requested resource |
| 520 | * against the specified resource. |
| 521 | * |
| 522 | * @param original original resource |
| 523 | * @param request requested resource |
| 524 | * @param allocation current allocation of the resource |
| 525 | * @return true if there is enough resource volume. Otherwise, false. |
| 526 | */ |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 527 | private boolean hasEnoughResource(Resource.Continuous original, |
| 528 | Resource.Continuous request, |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 529 | ContinuousResourceAllocation allocation) { |
| 530 | if (allocation == null) { |
| 531 | return request.value() <= original.value(); |
| 532 | } |
| 533 | |
| 534 | double allocated = allocation.allocations().stream() |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 535 | .filter(x -> x.resource() instanceof Resource.Continuous) |
| 536 | .map(x -> (Resource.Continuous) x.resource()) |
| 537 | .mapToDouble(Resource.Continuous::value) |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 538 | .sum(); |
| 539 | double left = original.value() - allocated; |
| 540 | return request.value() <= left; |
| 541 | } |
| 542 | |
| 543 | // internal use only |
| 544 | private static final class ContinuousResourceAllocation { |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 545 | private final Resource.Continuous original; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 546 | private final ImmutableList<ResourceAllocation> allocations; |
| 547 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 548 | private ContinuousResourceAllocation(Resource.Continuous original, |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 549 | ImmutableList<ResourceAllocation> allocations) { |
| 550 | this.original = original; |
| 551 | this.allocations = allocations; |
| 552 | } |
| 553 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 554 | private Resource.Continuous original() { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 555 | return original; |
| 556 | } |
| 557 | |
| 558 | private ImmutableList<ResourceAllocation> allocations() { |
| 559 | return allocations; |
| 560 | } |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 561 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 562 | } |