Committed by
Yuta Higuchi
Attempt to fix nested exception
ONOS-375 Change-Id: I623f58f2ea35b01602439a296cf92654ad2a240c
Showing
1 changed file
with
34 additions
and
5 deletions
| ... | @@ -182,21 +182,31 @@ public class HazelcastLinkResourceStore | ... | @@ -182,21 +182,31 @@ public class HazelcastLinkResourceStore |
| 182 | 182 | ||
| 183 | @Override | 183 | @Override |
| 184 | public Set<ResourceAllocation> getFreeResources(Link link) { | 184 | public Set<ResourceAllocation> getFreeResources(Link link) { |
| 185 | - Map<ResourceType, Set<? extends ResourceAllocation>> freeResources = getFreeResourcesEx(link); | 185 | + TransactionOptions opt = new TransactionOptions(); |
| 186 | + // read-only and will never be commited, thus does not need durability | ||
| 187 | + opt.setTransactionType(TransactionType.LOCAL); | ||
| 188 | + TransactionContext tx = theInstance.newTransactionContext(opt); | ||
| 189 | + tx.beginTransaction(); | ||
| 190 | + try { | ||
| 191 | + Map<ResourceType, Set<? extends ResourceAllocation>> freeResources = getFreeResourcesEx(tx, link); | ||
| 186 | Set<ResourceAllocation> allFree = new HashSet<>(); | 192 | Set<ResourceAllocation> allFree = new HashSet<>(); |
| 187 | - for (Set<? extends ResourceAllocation> r:freeResources.values()) { | 193 | + for (Set<? extends ResourceAllocation> r : freeResources.values()) { |
| 188 | allFree.addAll(r); | 194 | allFree.addAll(r); |
| 189 | } | 195 | } |
| 190 | return allFree; | 196 | return allFree; |
| 197 | + } finally { | ||
| 198 | + tx.rollbackTransaction(); | ||
| 199 | + } | ||
| 200 | + | ||
| 191 | } | 201 | } |
| 192 | 202 | ||
| 193 | - private Map<ResourceType, Set<? extends ResourceAllocation>> getFreeResourcesEx(Link link) { | 203 | + private Map<ResourceType, Set<? extends ResourceAllocation>> getFreeResourcesEx(TransactionContext tx, Link link) { |
| 194 | // returns capacity - allocated | 204 | // returns capacity - allocated |
| 195 | 205 | ||
| 196 | checkNotNull(link); | 206 | checkNotNull(link); |
| 197 | Map<ResourceType, Set<? extends ResourceAllocation>> free = new HashMap<>(); | 207 | Map<ResourceType, Set<? extends ResourceAllocation>> free = new HashMap<>(); |
| 198 | final Map<ResourceType, Set<? extends ResourceAllocation>> caps = getResourceCapacity(link); | 208 | final Map<ResourceType, Set<? extends ResourceAllocation>> caps = getResourceCapacity(link); |
| 199 | - final Iterable<LinkResourceAllocations> allocations = getAllocations(link); | 209 | + final Iterable<LinkResourceAllocations> allocations = getAllocations(tx, link); |
| 200 | 210 | ||
| 201 | for (ResourceType type : ResourceType.values()) { | 211 | for (ResourceType type : ResourceType.values()) { |
| 202 | // there should be class/category of resources | 212 | // there should be class/category of resources |
| ... | @@ -299,7 +309,7 @@ public class HazelcastLinkResourceStore | ... | @@ -299,7 +309,7 @@ public class HazelcastLinkResourceStore |
| 299 | // requested resources | 309 | // requested resources |
| 300 | Set<ResourceAllocation> reqs = allocations.getResourceAllocation(link); | 310 | Set<ResourceAllocation> reqs = allocations.getResourceAllocation(link); |
| 301 | 311 | ||
| 302 | - Map<ResourceType, Set<? extends ResourceAllocation>> available = getFreeResourcesEx(link); | 312 | + Map<ResourceType, Set<? extends ResourceAllocation>> available = getFreeResourcesEx(tx, link); |
| 303 | for (ResourceAllocation req : reqs) { | 313 | for (ResourceAllocation req : reqs) { |
| 304 | Set<? extends ResourceAllocation> avail = available.get(req.type()); | 314 | Set<? extends ResourceAllocation> avail = available.get(req.type()); |
| 305 | if (req instanceof BandwidthResourceAllocation) { | 315 | if (req instanceof BandwidthResourceAllocation) { |
| ... | @@ -446,7 +456,26 @@ public class HazelcastLinkResourceStore | ... | @@ -446,7 +456,26 @@ public class HazelcastLinkResourceStore |
| 446 | } | 456 | } |
| 447 | } | 457 | } |
| 448 | return res; | 458 | return res; |
| 459 | + } | ||
| 449 | 460 | ||
| 461 | + private Iterable<LinkResourceAllocations> getAllocations(TransactionContext tx, | ||
| 462 | + Link link) { | ||
| 463 | + checkNotNull(tx); | ||
| 464 | + checkNotNull(link); | ||
| 465 | + final LinkKey key = LinkKey.linkKey(link); | ||
| 466 | + | ||
| 467 | + STxMap<LinkKey, List<LinkResourceAllocations>> linkAllocs = getLinkAllocs(tx); | ||
| 468 | + List<LinkResourceAllocations> res = null; | ||
| 469 | + res = linkAllocs.get(key); | ||
| 470 | + if (res == null) { | ||
| 471 | + res = linkAllocs.putIfAbsent(key, new ArrayList<LinkResourceAllocations>()); | ||
| 472 | + if (res == null) { | ||
| 473 | + return Collections.emptyList(); | ||
| 474 | + } else { | ||
| 475 | + return res; | ||
| 476 | + } | ||
| 477 | + } | ||
| 478 | + return null; | ||
| 450 | } | 479 | } |
| 451 | 480 | ||
| 452 | @Override | 481 | @Override | ... | ... |
-
Please register or login to post a comment