diff --git a/cinder-powervc/powervc/volume/manager/manager.py b/cinder-powervc/powervc/volume/manager/manager.py index 550f459..f63f325 100644 --- a/cinder-powervc/powervc/volume/manager/manager.py +++ b/cinder-powervc/powervc/volume/manager/manager.py @@ -11,6 +11,7 @@ from cinder import db from cinder import context from cinder import service as taskservice from cinder.openstack.common import service +from cinder import quota from oslo_log import log from powervc.common import config from powervc.common.gettextutils import _ @@ -23,6 +24,7 @@ from powervc.common import messaging CONF = config.CONF LOG = log.getLogger(__name__) +QUOTAS = quota.QUOTAS volume_sync_opts = [ cfg.IntOpt('volume_sync_interval', @@ -782,7 +784,7 @@ class PowerVCCinderManager(service.Service): LOG.debug('Volume is non-existent locally, ignore delete handle') return - self._unregister_volumes(context, local_volume.get('id')) + self._unregister_volumes(context, local_volume) def _handle_powervc_volume_update(self, context=None, @@ -986,7 +988,7 @@ class PowerVCCinderManager(service.Service): return ret - def _unregister_volumes(self, context, volume_id): + def _unregister_volumes(self, context, local_volume): """ Unregister the volume from the local database. This does not use the Cinder API which would send an RPC to have the instance deleted. @@ -994,13 +996,23 @@ class PowerVCCinderManager(service.Service): own notifications locally and remove it from the database. """ ret = False - + volume_id = local_volume.get('id') + volume_name = local_volume.get('name') + volume_size = local_volume.get('size') if volume_id is None: LOG.debug('Volume id is none and ignore it') return ret try: db.volume_destroy(context, volume_id) + # update the quotas + reserve_opts = {'volumes': -1, + 'gigabytes': -volume_size} + reservations = QUOTAS.reserve(context, + **reserve_opts) + LOG.info(_("Start to deduct quota of volume: %s, size: %s") % + (volume_name, volume_size)) + QUOTAS.commit(context, reservations) ret = True except Exception as e: ret = False @@ -1067,7 +1079,18 @@ class PowerVCCinderManager(service.Service): else: try: local_volume = db.volume_create(context, values) + # update the instances that attach this volume self._update_volume_attachments(context, volume, local_volume) + # update the quotas + volume_name = local_volume.get('name') + volume_size = local_volume.get('size') + reserve_opts = {'volumes': 1, + 'gigabytes': volume_size} + LOG.info(_("Start to reserve quota of volume: %s, size: %s") % + (volume_name, volume_size)) + reservations = QUOTAS.reserve(context, + **reserve_opts) + QUOTAS.commit(context, reservations) except Exception as e: LOG.debug(_("Failed to create volume %s. Exception: %s") % (str(values), str(e))) @@ -1296,7 +1319,7 @@ class PowerVCCinderManager(service.Service): local_volume, pvc_volumes): # If it is not valid in pvc, also delete form the local. - self._unregister_volumes(context, local_volume.get('id')) + self._unregister_volumes(context, local_volume) count_deleted_volumes += 1 # Try delete unused volume-types diff --git a/nova-powervc/powervc/nova/driver/compute/computes.py b/nova-powervc/powervc/nova/driver/compute/computes.py index 24bb3a6..28e657b 100644 --- a/nova-powervc/powervc/nova/driver/compute/computes.py +++ b/nova-powervc/powervc/nova/driver/compute/computes.py @@ -192,15 +192,11 @@ class ComputeServiceManager(object): if local_service is None: LOG.debug("local service not found for %s" % remote_service.host) return - if (remote_service.state == "down" or - remote_service.hypervisor_state != "operating") \ - and local_service.started: + if remote_service.state == "down" and local_service.started: LOG.debug("Stopping remote service %s" % local_service.host) local_service.stop() return - if (remote_service.state == "up" and - remote_service.hypervisor_state == "operating") \ - and not local_service.started: + if remote_service.state == "up" and not local_service.started: LOG.debug("Starting remote service %s" % local_service.host) local_service.start() diff --git a/nova-powervc/powervc/nova/driver/compute/manager.py b/nova-powervc/powervc/nova/driver/compute/manager.py index 1d532e7..3f77c59 100644 --- a/nova-powervc/powervc/nova/driver/compute/manager.py +++ b/nova-powervc/powervc/nova/driver/compute/manager.py @@ -666,6 +666,21 @@ class PowerVCCloudManager(manager.Manager): LOG.warning(_("Removing PowerVC instance %s in nova failed."), local_instance.get('name')) + # Update quota + try: + dis_name = local_instance.get("display_name") + vcpus = local_instance.get("vcpus") + memory_mb = local_instance.get("memory_mb") + LOG.info(_("Start to deduct quota of vm: %s, cores: %s, ram: %s") % + (dis_name, vcpus, memory_mb)) + quotas = objects.Quotas(ctx) + quotas.reserve(instances=-1, + cores=-vcpus, + ram=-memory_mb) + quotas.commit() + except Exception as e: + LOG.warning(_("Decrease quota failed: %s") % str(e)) + # delete network resource # transfer db object to nova instance obj to meet latest community # change