connection: Clean up domain polling

Signed-off-by: Cole Robinson <crobinso@redhat.com>
This commit is contained in:
Cole Robinson 2011-04-09 23:51:43 -04:00
parent 5570851998
commit 39fe2ed1f7

View File

@ -1277,9 +1277,18 @@ class vmmConnection(vmmGObject):
""" """
returns lists of changed VM states returns lists of changed VM states
""" """
newActiveIDs = []
newInactiveNames = []
oldActiveIDs = {} oldActiveIDs = {}
oldInactiveNames = {} oldInactiveNames = {}
current = {}
maybenew = {}
gone = {}
start = []
new = []
activeUUIDs = []
for uuid in self.vms.keys(): for uuid in self.vms.keys():
# first pull out all the current inactive VMs we know about # first pull out all the current inactive VMs we know about
vm = self.vms[uuid] vm = self.vms[uuid]
@ -1292,94 +1301,80 @@ class vmmConnection(vmmGObject):
if vm.is_active(): if vm.is_active():
oldActiveIDs[vm.get_id()] = vm oldActiveIDs[vm.get_id()] = vm
newActiveIDs = []
try: try:
newActiveIDs = self.vmm.listDomainsID() newActiveIDs = self.vmm.listDomainsID()
except: except:
logging.exception("Unable to list active domains") logging.exception("Unable to list active domains")
newInactiveNames = []
try: try:
newInactiveNames = self.vmm.listDefinedDomains() newInactiveNames = self.vmm.listDefinedDomains()
except: except:
logging.exception("Unable to list inactive domains") logging.exception("Unable to list inactive domains")
curUUIDs = {} # new master list of vms
maybeNewUUIDs = {} # list of vms that changed state or are brand new
oldUUIDs = {} # no longer present vms
newUUIDs = [] # brand new vms
startedUUIDs = [] # previously present vms that are now running
activeUUIDs = [] # all running vms
# NB in these first 2 loops, we go to great pains to # NB in these first 2 loops, we go to great pains to
# avoid actually instantiating a new VM object so that # avoid actually instantiating a new VM object so that
# the common case of 'no new/old VMs' avoids hitting # the common case of 'no new/old VMs' avoids hitting
# XenD too much & thus slowing stuff down. # XenD too much & thus slowing stuff down.
# Filter out active domains which haven't changed # Filter out active domains which haven't changed
if newActiveIDs != None: for _id in newActiveIDs:
for _id in newActiveIDs: if _id in oldActiveIDs:
if _id in oldActiveIDs: # No change, copy across existing VM object
# No change, copy across existing VM object vm = oldActiveIDs[_id]
vm = oldActiveIDs[_id] current[vm.get_uuid()] = vm
curUUIDs[vm.get_uuid()] = vm activeUUIDs.append(vm.get_uuid())
activeUUIDs.append(vm.get_uuid()) else:
else: # May be a new VM, we have no choice but
# May be a new VM, we have no choice but # to create the wrapper so we can see
# to create the wrapper so we can see # if its a previously inactive domain.
# if its a previously inactive domain. try:
try: vm = self.vmm.lookupByID(_id)
vm = self.vmm.lookupByID(_id) uuid = util.uuidstr(vm.UUID())
uuid = util.uuidstr(vm.UUID()) maybenew[uuid] = vm
maybeNewUUIDs[uuid] = vm start.append(uuid)
startedUUIDs.append(uuid) activeUUIDs.append(uuid)
activeUUIDs.append(uuid) except:
except: logging.exception("Couldn't fetch domain id '%s'" % _id)
logging.exception("Couldn't fetch domain id '%s'" %
str(_id))
# Filter out inactive domains which haven't changed # Filter out inactive domains which haven't changed
if newInactiveNames != None: for name in newInactiveNames:
for name in newInactiveNames: if name in oldInactiveNames:
if name in oldInactiveNames: # No change, copy across existing VM object
# No change, copy across existing VM object vm = oldInactiveNames[name]
vm = oldInactiveNames[name] current[vm.get_uuid()] = vm
curUUIDs[vm.get_uuid()] = vm else:
else: # May be a new VM, we have no choice but
# May be a new VM, we have no choice but # to create the wrapper so we can see
# to create the wrapper so we can see # if its a previously inactive domain.
# if its a previously inactive domain. try:
try: vm = self.vmm.lookupByName(name)
vm = self.vmm.lookupByName(name) uuid = util.uuidstr(vm.UUID())
uuid = util.uuidstr(vm.UUID()) maybenew[uuid] = vm
maybeNewUUIDs[uuid] = vm except:
except: logging.exception("Couldn't fetch domain '%s'" % name)
logging.exception("Couldn't fetch domain id '%s'" %
str(id))
# At this point, maybeNewUUIDs has domains which are # At this point, maybeNewUUIDs has domains which are
# either completely new, or changed state. # either completely new, or changed state.
# Filter out VMs which merely changed state, leaving # Filter out VMs which merely changed state, leaving
# only new domains # only new domains
for uuid in maybeNewUUIDs.keys(): for uuid in maybenew.keys():
rawvm = maybeNewUUIDs[uuid] rawvm = maybenew[uuid]
if uuid not in self.vms: if uuid not in self.vms:
vm = vmmDomain(self, rawvm, uuid) vm = vmmDomain(self, rawvm, uuid)
newUUIDs.append(uuid) new.append(uuid)
curUUIDs[uuid] = vm
else: else:
vm = self.vms[uuid] vm = self.vms[uuid]
vm.set_handle(rawvm) vm.set_handle(rawvm)
curUUIDs[uuid] = vm current[uuid] = vm
# Finalize list of domains which went away altogether # Finalize list of domains which went away altogether
for uuid in self.vms.keys(): for uuid in self.vms.keys():
vm = self.vms[uuid] vm = self.vms[uuid]
if uuid not in curUUIDs: if uuid not in current:
oldUUIDs[uuid] = vm gone[uuid] = vm
return (startedUUIDs, newUUIDs, oldUUIDs, curUUIDs, activeUUIDs) return (start, new, gone, current, activeUUIDs)
def tick(self, noStatsUpdate=False): def tick(self, noStatsUpdate=False):
""" main update function: polls for new objects, updates stats, ...""" """ main update function: polls for new objects, updates stats, ..."""