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