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