use of org.openkilda.persistence.tx.TransactionRequired in project open-kilda by telstra.
the class MirrorGroupIdPool method allocate.
/**
* Allocates a group id for the flow path.
*/
@TransactionRequired
public MirrorGroup allocate(SwitchId switchId, String flowId, PathId pathId, MirrorGroupType type, MirrorDirection direction) {
GroupId nextGroupId = nextGroupIds.get(switchId);
if (nextGroupId != null && nextGroupId.getValue() > 0) {
if (nextGroupId.compareTo(maxGroupId) <= 0 && !mirrorGroupRepository.exists(switchId, nextGroupId)) {
MirrorGroup mirrorGroup = addMirrorGroup(flowId, pathId, switchId, nextGroupId, type, direction);
nextGroupIds.put(switchId, new GroupId(nextGroupId.getValue() + 1));
return mirrorGroup;
} else {
nextGroupIds.remove(switchId);
}
}
// The pool requires (re-)initialization.
if (!nextGroupIds.containsKey(switchId)) {
long numOfPools = (maxGroupId.getValue() - minGroupId.getValue()) / poolSize;
if (numOfPools > 1) {
long poolToTake = Math.abs(new Random().nextInt()) % numOfPools;
Optional<GroupId> availableGroupId = mirrorGroupRepository.findFirstUnassignedGroupId(switchId, new GroupId(minGroupId.getValue() + poolToTake * poolSize), new GroupId(minGroupId.getValue() + (poolToTake + 1) * poolSize - 1));
if (availableGroupId.isPresent()) {
nextGroupId = availableGroupId.get();
MirrorGroup mirrorGroup = addMirrorGroup(flowId, pathId, switchId, nextGroupId, type, direction);
nextGroupIds.put(switchId, new GroupId(nextGroupId.getValue() + 1));
return mirrorGroup;
}
}
// The pool requires full scan.
nextGroupId = new GroupId(-1);
nextGroupIds.put(switchId, nextGroupId);
}
if (nextGroupId != null && nextGroupId.getValue() == -1) {
Optional<GroupId> availableMeter = mirrorGroupRepository.findFirstUnassignedGroupId(switchId, minGroupId, maxGroupId);
if (availableMeter.isPresent()) {
nextGroupId = availableMeter.get();
MirrorGroup mirrorGroup = addMirrorGroup(flowId, pathId, switchId, nextGroupId, type, direction);
nextGroupIds.put(switchId, new GroupId(nextGroupId.getValue() + 1));
return mirrorGroup;
}
}
throw new ResourceNotAvailableException(format("No group id available for switch %s", switchId));
}
Aggregations