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); |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame^] | 146 | if (log.isTraceEnabled()) { |
| 147 | resources.forEach(r -> log.trace("registering {}", r)); |
| 148 | } |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 149 | |
| 150 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 151 | tx.begin(); |
| 152 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 153 | TransactionalMap<Resource.Discrete, Set<Resource>> childTxMap = |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 154 | tx.getTransactionalMap(CHILD_MAP, SERIALIZER); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 155 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 156 | Map<Resource.Discrete, List<Resource>> resourceMap = resources.stream() |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 157 | .filter(x -> x.parent().isPresent()) |
| 158 | .collect(Collectors.groupingBy(x -> x.parent().get())); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 159 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 160 | for (Map.Entry<Resource.Discrete, List<Resource>> entry: resourceMap.entrySet()) { |
| 161 | Optional<Resource.Discrete> child = lookup(childTxMap, entry.getKey()); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 162 | if (!child.isPresent()) { |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 163 | return abortTransaction(tx); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 166 | if (!appendValues(childTxMap, entry.getKey(), entry.getValue())) { |
| 167 | return abortTransaction(tx); |
| 168 | } |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 169 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 170 | |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 171 | boolean success = tx.commit(); |
| 172 | if (success) { |
| 173 | List<ResourceEvent> events = resources.stream() |
| 174 | .filter(x -> x.parent().isPresent()) |
| 175 | .map(x -> new ResourceEvent(RESOURCE_ADDED, x)) |
| 176 | .collect(Collectors.toList()); |
| 177 | notifyDelegate(events); |
| 178 | } |
| 179 | return success; |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 183 | public boolean unregister(List<Resource> resources) { |
Sho SHIMIZU | 83e17a0 | 2015-08-20 14:07:05 -0700 | [diff] [blame] | 184 | checkNotNull(resources); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 185 | |
| 186 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 187 | tx.begin(); |
| 188 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 189 | TransactionalMap<Resource.Discrete, Set<Resource>> childTxMap = |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 190 | tx.getTransactionalMap(CHILD_MAP, SERIALIZER); |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 191 | TransactionalMap<Resource.Discrete, ResourceConsumer> discreteConsumerTxMap = |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 192 | tx.getTransactionalMap(DISCRETE_CONSUMER_MAP, SERIALIZER); |
| 193 | TransactionalMap<ResourceId, ContinuousResourceAllocation> continuousConsumerTxMap = |
| 194 | tx.getTransactionalMap(CONTINUOUS_CONSUMER_MAP, SERIALIZER); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 195 | |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 196 | // Extract Discrete instances from resources |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 197 | Map<Resource.Discrete, List<Resource>> resourceMap = resources.stream() |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 198 | .filter(x -> x.parent().isPresent()) |
| 199 | .collect(Collectors.groupingBy(x -> x.parent().get())); |
Sho SHIMIZU | 83e17a0 | 2015-08-20 14:07:05 -0700 | [diff] [blame] | 200 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 201 | // even if one of the resources is allocated to a consumer, |
| 202 | // all unregistrations are regarded as failure |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 203 | for (Map.Entry<Resource.Discrete, List<Resource>> entry: resourceMap.entrySet()) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 204 | boolean allocated = entry.getValue().stream().anyMatch(x -> { |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 205 | if (x instanceof Resource.Discrete) { |
| 206 | return discreteConsumerTxMap.get((Resource.Discrete) x) != null; |
| 207 | } else if (x instanceof Resource.Continuous) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 208 | ContinuousResourceAllocation allocations = continuousConsumerTxMap.get(x.id()); |
| 209 | return allocations != null && !allocations.allocations().isEmpty(); |
| 210 | } else { |
| 211 | return false; |
| 212 | } |
| 213 | }); |
| 214 | if (allocated) { |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 215 | return abortTransaction(tx); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 218 | if (!removeValues(childTxMap, entry.getKey(), entry.getValue())) { |
| 219 | return abortTransaction(tx); |
| 220 | } |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 221 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 222 | |
Sho SHIMIZU | fa62b47 | 2015-11-02 17:35:46 -0800 | [diff] [blame] | 223 | boolean success = tx.commit(); |
| 224 | if (success) { |
| 225 | List<ResourceEvent> events = resources.stream() |
| 226 | .filter(x -> x.parent().isPresent()) |
| 227 | .map(x -> new ResourceEvent(RESOURCE_REMOVED, x)) |
| 228 | .collect(Collectors.toList()); |
| 229 | notifyDelegate(events); |
| 230 | } |
| 231 | return success; |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 235 | public boolean allocate(List<Resource> resources, ResourceConsumer consumer) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 236 | checkNotNull(resources); |
| 237 | checkNotNull(consumer); |
| 238 | |
| 239 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 240 | tx.begin(); |
| 241 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 242 | TransactionalMap<Resource.Discrete, Set<Resource>> childTxMap = |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 243 | tx.getTransactionalMap(CHILD_MAP, SERIALIZER); |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 244 | TransactionalMap<Resource.Discrete, ResourceConsumer> discreteConsumerTxMap = |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 245 | tx.getTransactionalMap(DISCRETE_CONSUMER_MAP, SERIALIZER); |
| 246 | TransactionalMap<ResourceId, ContinuousResourceAllocation> continuousConsumerTxMap = |
| 247 | tx.getTransactionalMap(CONTINUOUS_CONSUMER_MAP, SERIALIZER); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 248 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 249 | for (Resource resource: resources) { |
| 250 | if (resource instanceof Resource.Discrete) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 251 | if (!lookup(childTxMap, resource).isPresent()) { |
| 252 | return abortTransaction(tx); |
| 253 | } |
Sho SHIMIZU | d29847f | 2015-08-13 09:10:59 -0700 | [diff] [blame] | 254 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 255 | ResourceConsumer oldValue = discreteConsumerTxMap.put((Resource.Discrete) resource, consumer); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 256 | if (oldValue != null) { |
| 257 | return abortTransaction(tx); |
| 258 | } |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 259 | } else if (resource instanceof Resource.Continuous) { |
| 260 | Optional<Resource.Continuous> continuous = lookup(childTxMap, (Resource.Continuous) resource); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 261 | if (!continuous.isPresent()) { |
| 262 | return abortTransaction(tx); |
| 263 | } |
| 264 | |
| 265 | ContinuousResourceAllocation allocations = continuousConsumerTxMap.get(continuous.get().id()); |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 266 | if (!hasEnoughResource(continuous.get(), (Resource.Continuous) resource, allocations)) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 267 | return abortTransaction(tx); |
| 268 | } |
| 269 | |
| 270 | boolean success = appendValue(continuousConsumerTxMap, |
| 271 | continuous.get(), new ResourceAllocation(continuous.get(), consumer)); |
| 272 | if (!success) { |
| 273 | return abortTransaction(tx); |
| 274 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 275 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 276 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 277 | |
| 278 | return tx.commit(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 282 | public boolean release(List<Resource> resources, List<ResourceConsumer> consumers) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 283 | checkNotNull(resources); |
| 284 | checkNotNull(consumers); |
| 285 | checkArgument(resources.size() == consumers.size()); |
| 286 | |
| 287 | TransactionContext tx = service.transactionContextBuilder().build(); |
| 288 | tx.begin(); |
| 289 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 290 | TransactionalMap<Resource.Discrete, ResourceConsumer> discreteConsumerTxMap = |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 291 | tx.getTransactionalMap(DISCRETE_CONSUMER_MAP, SERIALIZER); |
| 292 | TransactionalMap<ResourceId, ContinuousResourceAllocation> continuousConsumerTxMap = |
| 293 | tx.getTransactionalMap(CONTINUOUS_CONSUMER_MAP, SERIALIZER); |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 294 | Iterator<Resource> resourceIte = resources.iterator(); |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 295 | Iterator<ResourceConsumer> consumerIte = consumers.iterator(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 296 | |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 297 | while (resourceIte.hasNext() && consumerIte.hasNext()) { |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 298 | Resource resource = resourceIte.next(); |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 299 | ResourceConsumer consumer = consumerIte.next(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 300 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 301 | if (resource instanceof Resource.Discrete) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 302 | // if this single release fails (because the resource is allocated to another consumer, |
| 303 | // the whole release fails |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 304 | if (!discreteConsumerTxMap.remove((Resource.Discrete) resource, consumer)) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 305 | return abortTransaction(tx); |
| 306 | } |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 307 | } else if (resource instanceof Resource.Continuous) { |
| 308 | Resource.Continuous continuous = (Resource.Continuous) resource; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 309 | ContinuousResourceAllocation allocation = continuousConsumerTxMap.get(continuous.id()); |
| 310 | ImmutableList<ResourceAllocation> newAllocations = allocation.allocations().stream() |
| 311 | .filter(x -> !(x.consumer().equals(consumer) && |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 312 | ((Resource.Continuous) x.resource()).value() == continuous.value())) |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 313 | .collect(GuavaCollectors.toImmutableList()); |
| 314 | |
| 315 | if (!continuousConsumerTxMap.replace(continuous.id(), allocation, |
| 316 | new ContinuousResourceAllocation(allocation.original(), newAllocations))) { |
| 317 | return abortTransaction(tx); |
| 318 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 319 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 320 | } |
Sho SHIMIZU | 1e0a34c | 2015-11-02 16:52:29 -0800 | [diff] [blame] | 321 | |
| 322 | return tx.commit(); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 326 | public boolean isAvailable(Resource resource) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 327 | checkNotNull(resource); |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 328 | checkArgument(resource instanceof Resource.Discrete || resource instanceof Resource.Continuous); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 329 | |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame^] | 330 | // check if it's registered or not. |
| 331 | Versioned<Set<Resource>> v = childMap.get(resource.parent().get()); |
| 332 | if (v == null || !v.value().contains(resource)) { |
| 333 | return false; |
| 334 | } |
| 335 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 336 | if (resource instanceof Resource.Discrete) { |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame^] | 337 | // check if already consumed |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 338 | return getConsumer((Resource.Discrete) resource).isEmpty(); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 339 | } else { |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame^] | 340 | Resource.Continuous requested = (Resource.Continuous) resource; |
| 341 | Resource.Continuous registered = v.value().stream() |
| 342 | .filter(c -> c.equals(resource)) |
| 343 | .findFirst() |
| 344 | .map(c -> (Resource.Continuous) c) |
| 345 | .get(); |
| 346 | if (registered.value() < requested.value()) { |
| 347 | // Capacity < requested, can never satisfy |
| 348 | return false; |
| 349 | } |
| 350 | // check if there's enough left |
| 351 | return isAvailable(requested); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 355 | private boolean isAvailable(Resource.Continuous resource) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 356 | Versioned<ContinuousResourceAllocation> allocation = continuousConsumers.get(resource.id()); |
| 357 | if (allocation == null) { |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame^] | 358 | // no allocation (=no consumer) full registered resources available |
| 359 | return true; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | return hasEnoughResource(allocation.value().original(), resource, allocation.value()); |
| 363 | } |
| 364 | |
| 365 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 366 | public Collection<Resource> getResources(ResourceConsumer consumer) { |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 367 | checkNotNull(consumer); |
| 368 | |
| 369 | // NOTE: getting all entries may become performance bottleneck |
| 370 | // TODO: revisit for better backend data structure |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 371 | Stream<Resource.Discrete> discreteStream = discreteConsumers.entrySet().stream() |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 372 | .filter(x -> x.getValue().value().equals(consumer)) |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 373 | .map(Map.Entry::getKey); |
| 374 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 375 | Stream<Resource.Continuous> continuousStream = continuousConsumers.values().stream() |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 376 | .flatMap(x -> x.value().allocations().stream() |
| 377 | .map(y -> Maps.immutableEntry(x.value().original(), y))) |
| 378 | .filter(x -> x.getValue().consumer().equals(consumer)) |
| 379 | .map(x -> x.getKey()); |
| 380 | |
| 381 | return Stream.concat(discreteStream, continuousStream).collect(Collectors.toList()); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 384 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 385 | public Collection<Resource> getChildResources(Resource parent) { |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 386 | checkNotNull(parent); |
HIGUCHI Yuta | 6f828c3 | 2016-01-20 18:11:05 -0800 | [diff] [blame^] | 387 | if (!(parent instanceof Resource.Discrete)) { |
| 388 | // only Discrete resource can have child resource |
| 389 | return ImmutableList.of(); |
| 390 | } |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 391 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 392 | Versioned<Set<Resource>> children = childMap.get((Resource.Discrete) parent); |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 393 | if (children == null) { |
Sho SHIMIZU | 2c0ae12 | 2016-01-20 13:14:38 -0800 | [diff] [blame] | 394 | return ImmutableList.of(); |
Sho SHIMIZU | e7f4f3f | 2015-10-13 16:27:25 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | return children.value(); |
| 398 | } |
| 399 | |
| 400 | @Override |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 401 | public <T> Collection<Resource> getAllocatedResources(Resource parent, Class<T> cls) { |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 402 | checkNotNull(parent); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 403 | checkNotNull(cls); |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 404 | checkArgument(parent instanceof Resource.Discrete); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 405 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 406 | Versioned<Set<Resource>> children = childMap.get((Resource.Discrete) parent); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 407 | if (children == null) { |
Sho SHIMIZU | 2c0ae12 | 2016-01-20 13:14:38 -0800 | [diff] [blame] | 408 | return ImmutableList.of(); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 411 | Stream<Resource.Discrete> discrete = children.value().stream() |
Sho SHIMIZU | c9546a3 | 2015-11-10 11:22:28 -0800 | [diff] [blame] | 412 | .filter(x -> x.last().getClass().equals(cls)) |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 413 | .filter(x -> x instanceof Resource.Discrete) |
| 414 | .map(x -> (Resource.Discrete) x) |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 415 | .filter(discreteConsumers::containsKey); |
| 416 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 417 | Stream<Resource.Continuous> continuous = children.value().stream() |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 418 | .filter(x -> x.last().getClass().equals(cls)) |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 419 | .filter(x -> x instanceof Resource.Continuous) |
| 420 | .map(x -> (Resource.Continuous) x) |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 421 | .filter(x -> continuousConsumers.containsKey(x.id())) |
| 422 | .filter(x -> continuousConsumers.get(x.id()) != null) |
| 423 | .filter(x -> !continuousConsumers.get(x.id()).value().allocations().isEmpty()); |
| 424 | |
| 425 | return Stream.concat(discrete, continuous).collect(Collectors.toList()); |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 426 | } |
Sho SHIMIZU | d29847f | 2015-08-13 09:10:59 -0700 | [diff] [blame] | 427 | |
| 428 | /** |
| 429 | * Abort the transaction. |
| 430 | * |
| 431 | * @param tx transaction context |
| 432 | * @return always false |
| 433 | */ |
| 434 | private boolean abortTransaction(TransactionContext tx) { |
| 435 | tx.abort(); |
| 436 | return false; |
| 437 | } |
| 438 | |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 439 | // Appends the specified ResourceAllocation to the existing values stored in the map |
| 440 | private boolean appendValue(TransactionalMap<ResourceId, ContinuousResourceAllocation> map, |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 441 | Resource.Continuous original, ResourceAllocation value) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 442 | ContinuousResourceAllocation oldValue = map.putIfAbsent(original.id(), |
| 443 | new ContinuousResourceAllocation(original, ImmutableList.of(value))); |
| 444 | if (oldValue == null) { |
| 445 | return true; |
| 446 | } |
| 447 | |
| 448 | if (oldValue.allocations().contains(value)) { |
| 449 | // don't write to map because all values are already stored |
| 450 | return true; |
| 451 | } |
| 452 | |
| 453 | ContinuousResourceAllocation newValue = new ContinuousResourceAllocation(original, |
| 454 | ImmutableList.<ResourceAllocation>builder() |
| 455 | .addAll(oldValue.allocations()) |
| 456 | .add(value) |
| 457 | .build()); |
| 458 | return map.replace(original.id(), oldValue, newValue); |
| 459 | } |
Sho SHIMIZU | d29847f | 2015-08-13 09:10:59 -0700 | [diff] [blame] | 460 | /** |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 461 | * Appends the values to the existing values associated with the specified key. |
Sho SHIMIZU | 4568c41 | 2015-08-21 16:39:07 -0700 | [diff] [blame] | 462 | * 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] | 463 | * |
| 464 | * @param map map holding multiple values for a key |
| 465 | * @param key key specifying values |
| 466 | * @param values values to be appended |
| 467 | * @param <K> type of the key |
| 468 | * @param <V> type of the element of the list |
| 469 | * @return true if the operation succeeds, false otherwise. |
| 470 | */ |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 471 | private <K, V> boolean appendValues(TransactionalMap<K, Set<V>> map, K key, List<V> values) { |
| 472 | Set<V> oldValues = map.putIfAbsent(key, new LinkedHashSet<>(values)); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 473 | if (oldValues == null) { |
Sho SHIMIZU | 93a74b3 | 2015-11-09 11:48:23 -0800 | [diff] [blame] | 474 | return true; |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 477 | if (oldValues.containsAll(values)) { |
Sho SHIMIZU | 4568c41 | 2015-08-21 16:39:07 -0700 | [diff] [blame] | 478 | // don't write to map because all values are already stored |
| 479 | return true; |
| 480 | } |
| 481 | |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 482 | LinkedHashSet<V> newValues = new LinkedHashSet<>(oldValues); |
| 483 | newValues.addAll(values); |
| 484 | return map.replace(key, oldValues, newValues); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /** |
Sho SHIMIZU | ba1f83b | 2015-10-14 08:11:20 -0700 | [diff] [blame] | 488 | * Removes the values from the existing values associated with the specified key. |
Sho SHIMIZU | 5618ee5 | 2015-08-21 17:19:44 -0700 | [diff] [blame] | 489 | * 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] | 490 | * |
| 491 | * @param map map holding multiple values for a key |
| 492 | * @param key key specifying values |
| 493 | * @param values values to be removed |
| 494 | * @param <K> type of the key |
| 495 | * @param <V> type of the element of the list |
| 496 | * @return true if the operation succeeds, false otherwise |
| 497 | */ |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 498 | private <K, V> boolean removeValues(TransactionalMap<K, Set<V>> map, K key, List<? extends V> values) { |
| 499 | Set<V> oldValues = map.putIfAbsent(key, new LinkedHashSet<>()); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 500 | if (oldValues == null) { |
Sho SHIMIZU | 93a74b3 | 2015-11-09 11:48:23 -0800 | [diff] [blame] | 501 | return true; |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 504 | if (values.stream().allMatch(x -> !oldValues.contains(x))) { |
Sho SHIMIZU | 5618ee5 | 2015-08-21 17:19:44 -0700 | [diff] [blame] | 505 | // don't write map because none of the values are stored |
| 506 | return true; |
| 507 | } |
| 508 | |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 509 | LinkedHashSet<V> newValues = new LinkedHashSet<>(oldValues); |
| 510 | newValues.removeAll(values); |
| 511 | return map.replace(key, oldValues, newValues); |
Sho SHIMIZU | 2d8a13a | 2015-08-18 22:37:41 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | /** |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 515 | * Returns the resource which has the same key as the key of the specified resource |
| 516 | * in the list as a value of the map. |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 517 | * |
| 518 | * @param map map storing parent - child relationship of resources |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 519 | * @param resource resource to be checked for its key |
| 520 | * @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] | 521 | */ |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 522 | // Naive implementation, which traverses all elements in the list |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 523 | private <T extends Resource> Optional<T> lookup( |
| 524 | TransactionalMap<Resource.Discrete, Set<Resource>> map, T resource) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 525 | // if it is root, always returns itself |
Sho SHIMIZU | c9546a3 | 2015-11-10 11:22:28 -0800 | [diff] [blame] | 526 | if (!resource.parent().isPresent()) { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 527 | return Optional.of(resource); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 530 | Set<Resource> values = map.get(resource.parent().get()); |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 531 | if (values == null) { |
| 532 | return Optional.empty(); |
| 533 | } |
| 534 | |
| 535 | @SuppressWarnings("unchecked") |
| 536 | Optional<T> result = values.stream() |
| 537 | .filter(x -> x.id().equals(resource.id())) |
| 538 | .map(x -> (T) x) |
| 539 | .findFirst(); |
| 540 | return result; |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Checks if there is enough resource volume to allocated the requested resource |
| 545 | * against the specified resource. |
| 546 | * |
| 547 | * @param original original resource |
| 548 | * @param request requested resource |
| 549 | * @param allocation current allocation of the resource |
| 550 | * @return true if there is enough resource volume. Otherwise, false. |
| 551 | */ |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 552 | private boolean hasEnoughResource(Resource.Continuous original, |
| 553 | Resource.Continuous request, |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 554 | ContinuousResourceAllocation allocation) { |
| 555 | if (allocation == null) { |
| 556 | return request.value() <= original.value(); |
| 557 | } |
| 558 | |
| 559 | double allocated = allocation.allocations().stream() |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 560 | .filter(x -> x.resource() instanceof Resource.Continuous) |
| 561 | .map(x -> (Resource.Continuous) x.resource()) |
| 562 | .mapToDouble(Resource.Continuous::value) |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 563 | .sum(); |
| 564 | double left = original.value() - allocated; |
| 565 | return request.value() <= left; |
| 566 | } |
| 567 | |
| 568 | // internal use only |
| 569 | private static final class ContinuousResourceAllocation { |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 570 | private final Resource.Continuous original; |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 571 | private final ImmutableList<ResourceAllocation> allocations; |
| 572 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 573 | private ContinuousResourceAllocation(Resource.Continuous original, |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 574 | ImmutableList<ResourceAllocation> allocations) { |
| 575 | this.original = original; |
| 576 | this.allocations = allocations; |
| 577 | } |
| 578 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 579 | private Resource.Continuous original() { |
Sho SHIMIZU | 6c9e33a | 2016-01-07 18:45:27 -0800 | [diff] [blame] | 580 | return original; |
| 581 | } |
| 582 | |
| 583 | private ImmutableList<ResourceAllocation> allocations() { |
| 584 | return allocations; |
| 585 | } |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 586 | } |
Sho SHIMIZU | 78ee25c | 2015-07-16 15:54:14 -0700 | [diff] [blame] | 587 | } |