cyborg/db/sqlalchemy/api.py
def quota_reserve(self, context, resources, deltas, expire,
                      until_refresh, max_age, project_id=None,
                      is_allocated_reserve=False):
        """ Create reservation record in DB according to params"""
        with _session_for_write() as session:
            if project_id is None:
                project_id = context.project_id
            usages = self._get_quota_usages(context, project_id,
                                            resources=deltas.keys())
            work = set(deltas.keys())
            while work:
                resource = work.pop()

                # Do we need to refresh the usage?
                refresh = False
                # create quota usage in DB if there is no record of this type
                # of resource
                if resource not in usages:
                    usages[resource] = self._quota_usage_create(
                           project_id, resource, until_refresh or None,
                           in_use=0, reserved=0, session=session)
                    refresh = True
                elif usages[resource].in_use < 0:
                    # Negative in_use count indicates a desync, so try to
                    # heal from that...
                    refresh = True
                elif usages[resource].until_refresh is not None:
                    usages[resource].until_refresh -= 1
                    if usages[resource].until_refresh <= 0:
                        refresh = True
                elif max_age and usages[resource].updated_at is not None and (
                    (timeutils.utcnow() -
                        usages[resource].updated_at).total_seconds() >=
                        max_age):
                    refresh = True

                # refresh the usage
                if refresh:
                    # Grab the sync routine
                    updates= self._sync_acc_res(context, resource, project_id)
                    for res, in_use in updates.items():
                        # Make sure we have a destination for the usage!
                        if res not in usages:
                            usages[res] = self._quota_usage_create(
                                project_id,
                                res,
                                until_refresh or None,
                                in_use=0,
                                reserved=0,
                                session=session
                            )

                        # Update the usage
                        usages[res].in_use = in_use
                        usages[res].until_refresh = until_refresh or None

                        # Because more than one resource may be refreshed
                        # by the call to the sync routine, and we don't
                        # want to double-sync, we make sure all refreshed
                        # resources are dropped from the work set.
                        work.discard(res)

                        # NOTE(Vek): We make the assumption that the sync
                        #            routine actually refreshes the
                        #            resources that it is the sync routine
                        #            for.  We don't check, because this is
                        #            a best-effort mechanism.

            unders = [r for r, delta in deltas.items()
                      if delta < 0 and delta + usages[r].in_use < 0]
            reservations = []
            for resource, delta in deltas.items():
                usage = usages[resource]
                reservation = self._reservation_create(
                    str(uuid.uuid4()), usage, project_id, resource,
                    delta, expire, session=session)
                reservations.append(reservation.uuid)
                usages[resource].reserved += delta
            session.flush()
        if unders:
            LOG.warning("Change will make usage less than 0 for the "
                        "following resources: %s", unders)
        return reservations
 Create reservation record in DB according to params
Create reservation record in DB according to params
Create reservation record in DB according to params
def quota_reserve(self, context, resources, deltas, expire,
                      until_refresh, max_age, project_id=None,
                      is_allocated_reserve=False):
        with _session_for_write() as session:
            if project_id is None:
                project_id = context.project_id
            usages = self._get_quota_usages(context, project_id,
                                            resources=deltas.keys())
            work = set(deltas.keys())
            while work:
                resource = work.pop()
                refresh = False
                if resource not in usages:
                    usages[resource] = self._quota_usage_create(
                           project_id, resource, until_refresh or None,
                           in_use=0, reserved=0, session=session)
                    refresh = True
                elif usages[resource].in_use < 0:
                    refresh = True
                elif usages[resource].until_refresh is not None:
                    usages[resource].until_refresh -= 1
                    if usages[resource].until_refresh <= 0:
                        refresh = True
                elif max_age and usages[resource].updated_at is not None and (
                    (timeutils.utcnow() -
                        usages[resource].updated_at).total_seconds() >=
                        max_age):
                    refresh = True
                if refresh:
                    updates= self._sync_acc_res(context, resource, project_id)
                    for res, in_use in updates.items():
                        if res not in usages:
                            usages[res] = self._quota_usage_create(
                                project_id,
                                res,
                                until_refresh or None,
                                in_use=0,
                                reserved=0,
                                session=session
                            )
                        usages[res].in_use = in_use
                        usages[res].until_refresh = until_refresh or None
                        work.discard(res)
            unders = [r for r, delta in deltas.items()
                      if delta < 0 and delta + usages[r].in_use < 0]
            reservations = []
            for resource, delta in deltas.items():
                usage = usages[resource]
                reservation = self._reservation_create(
                    str(uuid.uuid4()), usage, project_id, resource,
                    delta, expire, session=session)
                reservations.append(reservation.uuid)
                usages[resource].reserved += delta
            session.flush()
        if unders:
            LOG.warning("Change will make usage less than 0 for the "
                        "following resources: %s", unders)
        return reservations
def quota_reserve ( self , context , resources , deltas , expire , until_refresh , max_age , project_id = None , is_allocated_reserve = False ) : with _session_for_write ( ) as session : if project_id is None : project_id = context . project_id usages = self . _get_quota_usages ( context , project_id , resources = deltas . keys ( ) ) work = set ( deltas . keys ( ) ) while work : resource = work . pop ( ) refresh = False if resource not in usages : usages [ resource ] = self . _quota_usage_create ( project_id , resource , until_refresh or None , in_use = 0 , reserved = 0 , session = session ) refresh = True elif usages [ resource ] . in_use < 0 : refresh = True elif usages [ resource ] . until_refresh is not None : usages [ resource ] . until_refresh -= 1 if usages [ resource ] . until_refresh <= 0 : refresh = True elif max_age and usages [ resource ] . updated_at is not None and ( ( timeutils . utcnow ( ) - usages [ resource ] . updated_at ) . total_seconds ( ) >= max_age ) : refresh = True if refresh : updates = self . _sync_acc_res ( context , resource , project_id ) for res , in_use in updates . items ( ) : if res not in usages : usages [ res ] = self . _quota_usage_create ( project_id , res , until_refresh or None , in_use = 0 , reserved = 0 , session = session ) usages [ res ] . in_use = in_use usages [ res ] . until_refresh = until_refresh or None work . discard ( res ) unders = [ r for r , delta in deltas . items ( ) if delta < 0 and delta + usages [ r ] . in_use < 0 ] reservations = [ ] for resource , delta in deltas . items ( ) : usage = usages [ resource ] reservation = self . _reservation_create ( str ( uuid . uuid4 ( ) ) , usage , project_id , resource , delta , expire , session = session ) reservations . append ( reservation . uuid ) usages [ resource ] . reserved += delta session . flush ( ) if unders : LOG . warning ( "Change will make usage less than 0 for the " "following resources: %s" , unders ) return reservations
Create reservation record in DB according to params
Create reservation record in DB according to params
""" Create reservation record in DB according to params""" # Do we need to refresh the usage? # create quota usage in DB if there is no record of this type # of resource # Negative in_use count indicates a desync, so try to # heal from that... # refresh the usage # Grab the sync routine # Make sure we have a destination for the usage! # Update the usage # Because more than one resource may be refreshed # by the call to the sync routine, and we don't # want to double-sync, we make sure all refreshed # resources are dropped from the work set. # NOTE(Vek): We make the assumption that the sync #            routine actually refreshes the #            resources that it is the sync routine #            for.  We don't check, because this is #            a best-effort mechanism.
self
context
resources
deltas
expire
until_refresh
max_age
project_id
is_allocated_reserve