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