From tashi-commits-return-217-apmail-incubator-tashi-commits-archive=incubator.apache.org@incubator.apache.org Sat Mar 19 04:56:20 2011 Return-Path: Delivered-To: apmail-incubator-tashi-commits-archive@minotaur.apache.org Received: (qmail 90078 invoked from network); 19 Mar 2011 04:56:20 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 19 Mar 2011 04:56:20 -0000 Received: (qmail 18790 invoked by uid 500); 19 Mar 2011 04:56:20 -0000 Delivered-To: apmail-incubator-tashi-commits-archive@incubator.apache.org Received: (qmail 18773 invoked by uid 500); 19 Mar 2011 04:56:20 -0000 Mailing-List: contact tashi-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: tashi-dev@incubator.apache.org Delivered-To: mailing list tashi-commits@incubator.apache.org Received: (qmail 18765 invoked by uid 99); 19 Mar 2011 04:56:18 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 19 Mar 2011 04:56:18 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED,T_FRT_STOCK2 X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 19 Mar 2011 04:56:15 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id CC6B023889D7; Sat, 19 Mar 2011 04:55:51 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1083141 - /incubator/tashi/trunk/src/tashi/agents/primitive.py Date: Sat, 19 Mar 2011 04:55:51 -0000 To: tashi-commits@incubator.apache.org From: stroucki@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110319045551.CC6B023889D7@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: stroucki Date: Sat Mar 19 04:55:50 2011 New Revision: 1083141 URL: http://svn.apache.org/viewvc?rev=1083141&view=rev Log: reduce indent level on code blocks using continue and break deduplicate code text correct indentation of block where target host has been found. Yay python. Modified: incubator/tashi/trunk/src/tashi/agents/primitive.py Modified: incubator/tashi/trunk/src/tashi/agents/primitive.py URL: http://svn.apache.org/viewvc/incubator/tashi/trunk/src/tashi/agents/primitive.py?rev=1083141&r1=1083140&r2=1083141&view=diff ============================================================================== --- incubator/tashi/trunk/src/tashi/agents/primitive.py (original) +++ incubator/tashi/trunk/src/tashi/agents/primitive.py Sat Mar 19 04:55:50 2011 @@ -66,22 +66,27 @@ class Primitive(object): for i in _instances: instances[i.id] = i for i in instances.itervalues(): + # XXXstroucki: do we need to look at Held machines here? if (i.hostId or i.state == InstanceState.Pending): + # Nonrunning VMs will have hostId of None load[i.hostId] = load[i.hostId] + [i.id] # Check for VMs that have exited and call # postDestroy hook for i in oldInstances: + # XXXstroucki: do we need to look at Held machines here? if (i not in instances and oldInstances[i].state != InstanceState.Pending): for hook in self.hooks: hook.postDestroy(oldInstances[i]) - # Schedule new VMs oldInstances = instances if (len(load.get(None, [])) > 0): + # Schedule VMs if they are waiting + + # sort by id number (FIFO?) load[None].sort() for i in load[None]: inst = instances[i] @@ -93,24 +98,27 @@ class Primitive(object): allowElsewhere = boolean(inst.hints.get("allowElsewhere", "False")) except Exception, e: allowElsewhere = False + # has a host preference been expressed? if (targetHost != None): for h in hosts.values(): - if ((str(h.id) == targetHost or h.name == targetHost)): - # make sure that host is up, in a normal state and is not reserved - if (h.up == True and h.state == HostState.Normal and len(h.reserved) == 0): - memUsage = reduce(lambda x, y: x + instances[y].memory, load[h.id], inst.memory) - coreUsage = reduce(lambda x, y: x + instances[y].cores, load[h.id], inst.cores) - if (memUsage <= h.memory and coreUsage <= h.cores): - minMax = len(load[h.id]) - minMaxHost = h - - # If a host machine is reserved, only allow if userid is in reserved list - if ((len(h.reserved) > 0) and inst.userId in h.reserved): - memUsage = reduce(lambda x, y: x + instances[y].memory, load[h.id], inst.memory) - coreUsage = reduce(lambda x, y: x + instances[y].cores, load[h.id], inst.cores) - if (memUsage <= h.memory and coreUsage <= h.cores): - minMax = len(load[h.id]) - minMaxHost = h + # if this is not the host we are looking for, continue + if ((str(h.id) != targetHost and h.name != targetHost)): + continue + # we found the targetHost + # If a host machine is reserved, only allow if userid is in reserved list + if ((len(h.reserved) > 0) and inst.userId not in h.reserved): + # Machine is reserved and not available for userId. + # XXXstroucki: Should we log something here for analysis? + break + # make sure that host is up, and in a normal state + if (h.up == True and h.state == HostState.Normal): + memUsage = reduce(lambda x, y: x + instances[y].memory, load[h.id], inst.memory) + coreUsage = reduce(lambda x, y: x + instances[y].cores, load[h.id], inst.cores) + if (memUsage <= h.memory and coreUsage <= h.cores): + minMax = len(load[h.id]) + minMaxHost = h + + # end targethost != none # If we don't have a host yet, find one here @@ -123,33 +131,43 @@ class Primitive(object): if ((targetHost == None or allowElsewhere) and minMaxHost == None): for h in hosts.values(): # if the machine is suitable to host a vm, lets look at it + # XXXstroucki: should we let users use their reserved machines here? if (h.up == True and h.state == HostState.Normal and len(h.reserved) == 0): pass else: # otherwise find another machine continue + # implement dense packing policy: + # consider this host if + # minMax has not been modified or + # the number of vms here is greater than minmax if we're dense packing or + # the number of vms here is less than minmax if we're not dense packing if (minMax is None or (self.densePack and len(load[h.id]) > minMax) or (not self.densePack and len(load[h.id]) < minMax)): memUsage = reduce(lambda x, y: x + instances[y].memory, load[h.id], inst.memory) coreUsage = reduce(lambda x, y: x + instances[y].cores, load[h.id], inst.cores) if (memUsage <= h.memory and coreUsage <= h.cores): minMax = len(load[h.id]) minMaxHost = h - if (minMaxHost): - # found a host - if (not inst.hints.get("__resume_source", None)): - for hook in self.hooks: - hook.preCreate(inst) - self.log.info("Scheduling instance %s (%d mem, %d cores, %d uid) on host %s" % (inst.name, inst.memory, inst.cores, inst.userId, minMaxHost.name)) - self.client.activateVm(i, minMaxHost) - load[minMaxHost.id] = load[minMaxHost.id] + [i] - # get rid of its possible entry in muffle if VM is scheduled to a host - if (inst.name in muffle): - muffle.pop(inst.name) - else: - # did not find a host - if (inst.name not in muffle): - self.log.info("Failed to find a suitable place to schedule %s" % (inst.name)) - muffle[inst.name] = True + + if (minMaxHost): + # found a host + if (not inst.hints.get("__resume_source", None)): + # only run preCreate hooks if newly starting + for hook in self.hooks: + hook.preCreate(inst) + self.log.info("Scheduling instance %s (%d mem, %d cores, %d uid) on host %s" % (inst.name, inst.memory, inst.cores, inst.userId, minMaxHost.name)) + self.client.activateVm(i, minMaxHost) + load[minMaxHost.id] = load[minMaxHost.id] + [i] + # get rid of its possible entry in muffle if VM is scheduled to a host + if (inst.name in muffle): + muffle.pop(inst.name) + else: + # did not find a host + if (inst.name not in muffle): + self.log.info("Failed to find a suitable place to schedule %s" % (inst.name)) + muffle[inst.name] = True + + except Exception, e: # XXXstroucki: how can we get here? if (inst.name not in muffle): @@ -158,15 +176,15 @@ class Primitive(object): - time.sleep(self.scheduleDelay) - - except TashiException, e: self.log.exception("Tashi exception") - time.sleep(self.scheduleDelay) + except Exception, e: self.log.exception("General exception") - time.sleep(self.scheduleDelay) + + + # wait to do the next iteration + time.sleep(self.scheduleDelay) def main(): (config, configFiles) = getConfig(["Agent"])