Sho SHIMIZU
Committed by Gerrit Code Review

Refactor: Move allocation check into transactional store implementation

Change-Id: Id2381d6789f12c8a0c0730b17e3395d144265f14
...@@ -173,23 +173,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour ...@@ -173,23 +173,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
173 Map<DiscreteResourceId, List<Resource>> resourceMap = resources.stream() 173 Map<DiscreteResourceId, List<Resource>> resourceMap = resources.stream()
174 .collect(Collectors.groupingBy(x -> x.parent().get().id(), LinkedHashMap::new, Collectors.toList())); 174 .collect(Collectors.groupingBy(x -> x.parent().get().id(), LinkedHashMap::new, Collectors.toList()));
175 175
176 - // even if one of the resources is allocated to a consumer,
177 - // all unregistrations are regarded as failure
178 for (Map.Entry<DiscreteResourceId, List<Resource>> entry : resourceMap.entrySet()) { 176 for (Map.Entry<DiscreteResourceId, List<Resource>> entry : resourceMap.entrySet()) {
179 - boolean allocated = entry.getValue().stream().anyMatch(x -> {
180 - if (x instanceof DiscreteResource) {
181 - return discreteTxStore.isAllocated(((DiscreteResource) x).id());
182 - } else if (x instanceof ContinuousResource) {
183 - return continuousTxStore.isAllocated(((ContinuousResource) x).id());
184 - } else {
185 - return false;
186 - }
187 - });
188 - if (allocated) {
189 - log.warn("Failed to unregister {}: allocation exists", entry.getKey());
190 - return abortTransaction(tx);
191 - }
192 -
193 if (!unregister(discreteTxStore, continuousTxStore, entry.getKey(), entry.getValue())) { 177 if (!unregister(discreteTxStore, continuousTxStore, entry.getKey(), entry.getValue())) {
194 log.warn("Failed to unregister {}: Failed to remove {} values.", 178 log.warn("Failed to unregister {}: Failed to remove {} values.",
195 entry.getKey(), entry.getValue().size()); 179 entry.getKey(), entry.getValue().size());
......
...@@ -102,6 +102,14 @@ class TransactionalContinuousResourceStore { ...@@ -102,6 +102,14 @@ class TransactionalContinuousResourceStore {
102 return true; 102 return true;
103 } 103 }
104 104
105 + // even if one of the resources is allocated to a consumer,
106 + // all unregistrations are regarded as failure
107 + boolean allocated = values.stream().anyMatch(x -> isAllocated(x.id()));
108 + if (allocated) {
109 + log.warn("Failed to unregister {}: allocation exists", key);
110 + return false;
111 + }
112 +
105 Set<ContinuousResource> oldValues = childMap.putIfAbsent(key, new LinkedHashSet<>()); 113 Set<ContinuousResource> oldValues = childMap.putIfAbsent(key, new LinkedHashSet<>());
106 if (oldValues == null) { 114 if (oldValues == null) {
107 log.trace("No-Op removing values. key {} did not exist", key); 115 log.trace("No-Op removing values. key {} did not exist", key);
......
...@@ -92,6 +92,14 @@ class TransactionalDiscreteResourceStore { ...@@ -92,6 +92,14 @@ class TransactionalDiscreteResourceStore {
92 return true; 92 return true;
93 } 93 }
94 94
95 + // even if one of the resources is allocated to a consumer,
96 + // all unregistrations are regarded as failure
97 + boolean allocated = values.stream().anyMatch(x -> isAllocated(x.id()));
98 + if (allocated) {
99 + log.warn("Failed to unregister {}: allocation exists", key);
100 + return false;
101 + }
102 +
95 Set<DiscreteResource> oldValues = childMap.putIfAbsent(key, new LinkedHashSet<>()); 103 Set<DiscreteResource> oldValues = childMap.putIfAbsent(key, new LinkedHashSet<>());
96 if (oldValues == null) { 104 if (oldValues == null) {
97 log.trace("No-Op removing values. key {} did not exist", key); 105 log.trace("No-Op removing values. key {} did not exist", key);
......