From 653ff11692fa5cd5ec5f9ea75cddc03df1b3dcd5 Mon Sep 17 00:00:00 2001 From: Salvatore Orlando Date: Tue, 8 Feb 2011 16:39:46 +0000 Subject: avoiding HOST_UNAVAILABLE exception: if there is not enough free memory does not spawn the VM at all. instance state is set to "SHUTDOWN" --- nova/virt/xenapi/vm_utils.py | 11 +++++++++++ nova/virt/xenapi/vmops.py | 9 ++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) (limited to 'nova/virt') diff --git a/nova/virt/xenapi/vm_utils.py b/nova/virt/xenapi/vm_utils.py index 4bbd522c1..3a0f0a149 100644 --- a/nova/virt/xenapi/vm_utils.py +++ b/nova/virt/xenapi/vm_utils.py @@ -138,6 +138,17 @@ class VMHelper(HelperBase): LOG.debug(_('Created VM %(instance_name)s as %(vm_ref)s.') % locals()) return vm_ref + @classmethod + def ensure_free_mem(cls, session, instance): + instance_type = instance_types.INSTANCE_TYPES[instance.instance_type] + mem = str(long(instance_type['memory_mb']) * 1024 * 1024) + #get free memory from host + host = session.get_xenapi_host() + host_free_mem = session.get_xenapi().host.compute_free_memory(host) + if (host_free_mem < mem ): + return False + return True + @classmethod def create_vbd(cls, session, vm_ref, vdi_ref, userdevice, bootable): """Create a VBD record. Returns a Deferred that gives the new diff --git a/nova/virt/xenapi/vmops.py b/nova/virt/xenapi/vmops.py index e84ce20c4..2d4e53083 100644 --- a/nova/virt/xenapi/vmops.py +++ b/nova/virt/xenapi/vmops.py @@ -66,7 +66,14 @@ class VMOps(object): if vm is not None: raise exception.Duplicate(_('Attempted to create' ' non-unique name %s') % instance.name) - + #ensure enough free memory, otherwise don't bother + if not VMHelper.ensure_free_mem(self._session,instance): + LOG.exception(_('instance %s: not enough free memory'), + instance['name']) + db.instance_set_state(context.get_admin_context(), + instance['id'], + power_state.SHUTDOWN) + return bridge = db.network_get_by_instance(context.get_admin_context(), instance['id'])['bridge'] network_ref = \ -- cgit