Optimize VM list in API

With many VMs this slows down linearly. Rework it a bit so there are
fewer calls to getInformationFromXML and so the processing could happen
in parallel at some point.
This commit is contained in:
Joshua Boniface 2021-06-23 19:14:26 -04:00
parent bbb132414c
commit cd860bae6b
1 changed files with 32 additions and 25 deletions

View File

@ -821,7 +821,6 @@ def get_list(zkhandler, node, state, limit, is_fuzzy=True):
return False, 'VM state "{}" is not valid.'.format(state) return False, 'VM state "{}" is not valid.'.format(state)
full_vm_list = zkhandler.children('base.domain') full_vm_list = zkhandler.children('base.domain')
vm_list = []
# Set our limit to a sensible regex # Set our limit to a sensible regex
if limit and is_fuzzy: if limit and is_fuzzy:
@ -834,38 +833,46 @@ def get_list(zkhandler, node, state, limit, is_fuzzy=True):
except Exception as e: except Exception as e:
return False, 'Regex Error: {}'.format(e) return False, 'Regex Error: {}'.format(e)
# If we're limited, remove other nodes' VMs get_vm_info = dict()
vm_node = {}
vm_state = {}
for vm in full_vm_list: for vm in full_vm_list:
# Check we don't match the limit
name = zkhandler.read(('domain', vm)) name = zkhandler.read(('domain', vm))
vm_node[vm] = zkhandler.read(('domain.node', vm)) is_limit_match = False
vm_state[vm] = zkhandler.read(('domain.state', vm)) is_node_match = False
# Handle limiting is_state_match = False
# Check on limit
if limit: if limit:
try: try:
if re.match(limit, vm): if re.match(limit, vm):
if not node and not state: is_limit_match = True
vm_list.append(common.getInformationFromXML(zkhandler, vm))
else:
if vm_node[vm] == node or vm_state[vm] == state:
vm_list.append(common.getInformationFromXML(zkhandler, vm))
if re.match(limit, name): if re.match(limit, name):
if not node and not state: is_limit_match = True
vm_list.append(common.getInformationFromXML(zkhandler, vm))
else:
if vm_node[vm] == node or vm_state[vm] == state:
vm_list.append(common.getInformationFromXML(zkhandler, vm))
except Exception as e: except Exception as e:
return False, 'Regex Error: {}'.format(e) return False, 'Regex Error: {}'.format(e)
else: else:
# Check node to avoid unneeded ZK calls is_limit_match = True
if not node and not state:
vm_list.append(common.getInformationFromXML(zkhandler, vm)) # Check on node
if node:
vm_node = zkhandler.read(('domain.node', vm))
if vm_node == node:
is_node_match = True
else: else:
if vm_node[vm] == node or vm_state[vm] == state: is_node_match = True
vm_list.append(common.getInformationFromXML(zkhandler, vm))
# Check on state
if state:
vm_state = zkhandler.read(('domain.state', vm))
if vm_state == state:
is_state_match = True
else:
is_state_match = True
if is_limit_match and is_node_match and is_state_match:
get_vm_info[vm] = True
else:
get_vm_info[vm] = False
vm_list = [common.getInformationFromXML(zkhandler, vm) for vm in full_vm_list if get_vm_info[vm]]
return True, vm_list return True, vm_list