Exemple #1
0
    def make_reservation(self, context, project_id, resources, deltas, plugin):
        resources_over_limit = []
        with db_api.CONTEXT_WRITER.using(context):
            # Filter out unlimited resources.
            limits = self.get_tenant_quotas(context, resources, project_id)
            unlimited_resources = set([
                resource for (resource, limit) in limits.items() if limit < 0
            ])
            requested_resources = (set(deltas.keys()) - unlimited_resources)

            # Delete expired reservations before counting valid ones. This
            # operation is fast and by calling it before making any
            # reservation, we ensure the freshness of the reservations.
            quota_api.remove_expired_reservations(context,
                                                  tenant_id=project_id)

            # Count the number of (1) used and (2) reserved resources for this
            # project_id. If any resource limit is exceeded, raise exception.
            for resource_name in requested_resources:
                tracked_resource = resources.get(resource_name)
                if not tracked_resource:
                    continue

                used_and_reserved = tracked_resource.count(
                    context, None, project_id, count_db_registers=True)
                resource_num = deltas[resource_name]
                if limits[resource_name] < (used_and_reserved + resource_num):
                    resources_over_limit.append(resource_name)

            if resources_over_limit:
                raise exceptions.OverQuota(overs=sorted(resources_over_limit))

            return quota_api.create_reservation(context, project_id, deltas)
Exemple #2
0
    def _remove_expired_reservations():
        """Remove expired reservations from all projects

        Any DB exception will be catch and dismissed. This operation can have
        been successfully executed by another concurrent request. There is no
        need to fail or retry it.
        """
        context = n_context.get_admin_context()
        quota_api.remove_expired_reservations(
            context, timeout=quota_api.RESERVATION_EXPIRATION_TIMEOUT)
Exemple #3
0
 def _handle_expired_reservations(self, context, tenant_id, resource, expired_amount):
     LOG.debug(
         ("Adjusting usage for resource %(resource)s: " "removing %(expired)d reserved items"),
         {"resource": resource, "expired": expired_amount},
     )
     # TODO(salv-orlando): It should be possible to do this
     # operation for all resources with a single query.
     # Update reservation usage
     quota_api.set_quota_usage(context, resource, tenant_id, reserved=-expired_amount, delta=True)
     # Delete expired reservations (we don't want them to accrue
     # in the database)
     quota_api.remove_expired_reservations(context, tenant_id=tenant_id)
Exemple #4
0
 def _handle_expired_reservations(self, context, tenant_id,
                                  resource, expired_amount):
     LOG.debug(("Adjusting usage for resource %(resource)s: "
                "removing %(expired)d reserved items"),
               {'resource': resource,
                'expired': expired_amount})
     # TODO(salv-orlando): It should be possible to do this
     # operation for all resources with a single query.
     # Update reservation usage
     quota_api.set_quota_usage(
         context,
         resource,
         tenant_id,
         reserved=-expired_amount,
         delta=True)
     # Delete expired reservations (we don't want them to accrue
     # in the database)
     quota_api.remove_expired_reservations(
         context, tenant_id=tenant_id)
Exemple #5
0
 def test_remove_expired_reservations(self):
     with mock.patch('neutron.db.quota.api.utcnow') as mock_utcnow:
         mock_utcnow.return_value = datetime.datetime(
             2015, 5, 20, 0, 0)
         resources = {'goals': 2, 'assists': 1}
         exp_date_1 = datetime.datetime(2016, 3, 31, 14, 30)
         resv_1 = self._create_reservation(resources, expiration=exp_date_1)
         exp_date_2 = datetime.datetime(2015, 3, 31, 14, 30)
         resv_2 = self._create_reservation(resources, expiration=exp_date_2)
         self.assertEqual(1, quota_api.remove_expired_reservations(
             self.context, self.tenant_id))
         self.assertIsNone(quota_api.get_reservation(
             self.context, resv_2.reservation_id))
         self.assertIsNotNone(quota_api.get_reservation(
             self.context, resv_1.reservation_id))
Exemple #6
0
 def test_remove_expired_reservations(self):
     with mock.patch('neutron.db.quota.api.utcnow') as mock_utcnow:
         mock_utcnow.return_value = datetime.datetime(
             2015, 5, 20, 0, 0)
         resources = {'goals': 2, 'assists': 1}
         exp_date_1 = datetime.datetime(2016, 3, 31, 14, 30)
         resv_1 = self._create_reservation(resources, expiration=exp_date_1)
         exp_date_2 = datetime.datetime(2015, 3, 31, 14, 30)
         resv_2 = self._create_reservation(resources, expiration=exp_date_2)
         self.assertEqual(1, quota_api.remove_expired_reservations(
             self.context, self.tenant_id))
         self.assertIsNone(quota_api.get_reservation(
             self.context, resv_2.reservation_id))
         self.assertIsNotNone(quota_api.get_reservation(
             self.context, resv_1.reservation_id))
Exemple #7
0
 def test_remove_expired_reservations_no_project(self):
     with mock.patch('neutron.db.quota.api.utcnow') as mock_utcnow:
         mock_utcnow.return_value = datetime.datetime(2015, 5, 20, 0, 0)
         resources = {'goals': 2, 'assists': 1}
         exp_date_1 = datetime.datetime(2014, 3, 31, 14, 30)
         resv_1 = self._create_reservation(resources, expiration=exp_date_1)
         exp_date_2 = datetime.datetime(2015, 3, 31, 14, 30)
         resv_2 = self._create_reservation(resources,
                                           expiration=exp_date_2,
                                           project_id='Callejon')
         self.assertEqual(
             2,
             quota_api.remove_expired_reservations(
                 context.get_admin_context()))
         self.assertIsNone(
             quota_api.get_reservation(self.context, resv_2.reservation_id))
         self.assertIsNone(
             quota_api.get_reservation(self.context, resv_1.reservation_id))
Exemple #8
0
 def _handle_expired_reservations(self, context, tenant_id):
     LOG.debug("Deleting expired reservations for tenant:%s" % tenant_id)
     # Delete expired reservations (we don't want them to accrue
     # in the database)
     quota_api.remove_expired_reservations(
         context, tenant_id=tenant_id)
Exemple #9
0
 def _handle_expired_reservations(self, context, tenant_id):
     LOG.debug("Deleting expired reservations for tenant:%s", tenant_id)
     # Delete expired reservations (we don't want them to accrue
     # in the database)
     quota_api.remove_expired_reservations(context, tenant_id=tenant_id)