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