예제 #1
0
def update_alloc_to_next_hard_limit(context, resources, deltas, res,
                                    expire, project_id):
    from cinder import quota
    QUOTAS = quota.QUOTAS
    reservations = []
    projects = get_project_hierarchy(context, project_id,
                                     parents_as_ids=True).parents
    hard_limit_found = False
    # Update allocated values up the chain til we hit a hard limit or run out
    # of parents
    while projects and not hard_limit_found:
        cur_proj_id = list(projects)[0]
        projects = projects[cur_proj_id]
        cur_quota_lim = QUOTAS.get_by_project_or_default(
            context, cur_proj_id, res)
        hard_limit_found = (cur_quota_lim != -1)
        cur_quota = {res: cur_quota_lim}
        cur_delta = {res: deltas[res]}
        try:
            reservations += db.quota_reserve(
                context, resources, cur_quota, cur_delta, expire,
                CONF.until_refresh, CONF.max_age, cur_proj_id,
                is_allocated_reserve=True)
        except exception.OverQuota:
            db.reservation_rollback(context, reservations)
            raise
    return reservations
예제 #2
0
def update_alloc_to_next_hard_limit(context, resources, deltas, res, expire,
                                    project_id):
    from cinder import quota
    QUOTAS = quota.QUOTAS
    reservations = []
    projects = get_project_hierarchy(context, project_id,
                                     parents_as_ids=True).parents
    hard_limit_found = False
    # Update allocated values up the chain til we hit a hard limit or run out
    # of parents
    while projects and not hard_limit_found:
        cur_proj_id = list(projects)[0]
        projects = projects[cur_proj_id]
        cur_quota_lim = QUOTAS.get_by_project_or_default(
            context, cur_proj_id, res)
        hard_limit_found = (cur_quota_lim != -1)
        cur_quota = {res: cur_quota_lim}
        cur_delta = {res: deltas[res]}
        try:
            reservations += db.quota_reserve(context,
                                             resources,
                                             cur_quota,
                                             cur_delta,
                                             expire,
                                             CONF.until_refresh,
                                             CONF.max_age,
                                             cur_proj_id,
                                             is_allocated_reserve=True)
        except exception.OverQuota:
            db.reservation_rollback(context, reservations)
            raise
    return reservations
예제 #3
0
def _quota_reserve(context, project_id):
    """Create sample Quota, QuotaUsage and Reservation objects.

    There is no method db.quota_usage_create(), so we have to use
    db.quota_reserve() for creating QuotaUsage objects.

    Returns reservations uuids.

    """
    def get_sync(resource, usage):
        def sync(elevated, project_id, session):
            return {resource: usage}

        return sync

    quotas = {}
    resources = {}
    deltas = {}
    for i, resource in enumerate(('volumes', 'gigabytes')):
        quotas[resource] = db.quota_create(context, project_id, resource,
                                           i + 1)
        resources[resource] = ReservableResource(resource,
                                                 '_sync_%s' % resource)
        deltas[resource] = i + 1
    return db.quota_reserve(context, resources, quotas, deltas,
                            datetime.datetime.utcnow(),
                            datetime.datetime.utcnow(),
                            datetime.timedelta(days=1), project_id)
예제 #4
0
def _quota_reserve(context, project_id):
    """Create sample Quota, QuotaUsage and Reservation objects.

    There is no method db.quota_usage_create(), so we have to use
    db.quota_reserve() for creating QuotaUsage objects.

    Returns reservations uuids.

    """
    def get_sync(resource, usage):
        def sync(elevated, project_id, session):
            return {resource: usage}
        return sync
    quotas = {}
    resources = {}
    deltas = {}
    for i, resource in enumerate(('volumes', 'gigabytes')):
        quotas[resource] = db.quota_create(context, project_id,
                                           resource, i + 1)
        resources[resource] = ReservableResource(resource,
                                                 '_sync_%s' % resource)
        deltas[resource] = i + 1
    return db.quota_reserve(
        context, resources, quotas, deltas,
        datetime.datetime.utcnow(), datetime.datetime.utcnow(),
        datetime.timedelta(days=1), project_id
    )
예제 #5
0
def _quota_reserve(context, project_id):
    """Create sample Quota, QuotaUsage and Reservation objects.

    There is no method db.quota_usage_create(), so we have to use
    db.quota_reserve() for creating QuotaUsage objects.

    Returns reservations uuids.

    """
    def get_sync(resource, usage):
        def sync(elevated, project_id, session):
            return {resource: usage}

        return sync

    quotas = {}
    resources = {}
    deltas = {}
    for i in range(3):
        resource = 'res%d' % i
        quotas[resource] = db.quota_create(context, project_id, resource, i)
        resources[resource] = ReservableResource(resource,
                                                 get_sync(resource, i),
                                                 'quota_res_%d' % i)
        deltas[resource] = i
    return db.quota_reserve(context, resources, quotas, deltas,
                            datetime.utcnow(), datetime.utcnow(),
                            timedelta(days=1), project_id)
예제 #6
0
def _quota_reserve(context, project_id):
    """Create sample Quota, QuotaUsage and Reservation objects.

    There is no method db.quota_usage_create(), so we have to use
    db.quota_reserve() for creating QuotaUsage objects.

    Returns reservations uuids.

    """
    def get_sync(resource, usage):
        def sync(elevated, project_id, session):
            return {resource: usage}
        return sync
    quotas = {}
    resources = {}
    deltas = {}
    for i in range(3):
        resource = 'res%d' % i
        quotas[resource] = db.quota_create(context, project_id, resource, i)
        resources[resource] = ReservableResource(
            resource,
            get_sync(resource, i), 'quota_res_%d' % i)
        deltas[resource] = i
    return db.quota_reserve(
        context, resources, quotas, deltas,
        datetime.datetime.utcnow(), datetime.datetime.utcnow(),
        datetime.timedelta(days=1), project_id
    )
예제 #7
0
    def reserve(self, context, resources, deltas, expire=None):
        """Check quotas and reserve resources.

        For counting quotas--those quotas for which there is a usage
        synchronization function--this method checks quotas against
        current usage and the desired deltas.

        This method will raise a QuotaResourceUnknown exception if a
        given resource is unknown or if it does not have a usage
        synchronization function.

        If any of the proposed values is over the defined quota, an
        OverQuota exception will be raised with the sorted list of the
        resources which are too high.  Otherwise, the method returns a
        list of reservation UUIDs which were created.

        :param context: The request context, for access checks.
        :param resources: A dictionary of the registered resources.
        :param deltas: A dictionary of the proposed delta changes.
        :param expire: An optional parameter specifying an expiration
                       time for the reservations.  If it is a simple
                       number, it is interpreted as a number of
                       seconds and added to the current time; if it is
                       a datetime.timedelta object, it will also be
                       added to the current time.  A datetime.datetime
                       object will be interpreted as the absolute
                       expiration time.  If None is specified, the
                       default expiration time set by
                       --default-reservation-expire will be used (this
                       value will be treated as a number of seconds).
        """

        # Set up the reservation expiration
        if expire is None:
            expire = FLAGS.reservation_expire
        if isinstance(expire, (int, long)):
            expire = datetime.timedelta(seconds=expire)
        if isinstance(expire, datetime.timedelta):
            expire = timeutils.utcnow() + expire
        if not isinstance(expire, datetime.datetime):
            raise exception.InvalidReservationExpiration(expire=expire)

        # Get the applicable quotas.
        # NOTE(Vek): We're not worried about races at this point.
        #            Yes, the admin may be in the process of reducing
        #            quotas, but that's a pretty rare thing.
        quotas = self._get_quotas(context,
                                  resources,
                                  deltas.keys(),
                                  has_sync=True)

        # NOTE(Vek): Most of the work here has to be done in the DB
        #            API, because we have to do it in a transaction,
        #            which means access to the session.  Since the
        #            session isn't available outside the DBAPI, we
        #            have to do the work there.
        return db.quota_reserve(context, resources, quotas, deltas, expire,
                                FLAGS.until_refresh, FLAGS.max_age)
예제 #8
0
파일: quota.py 프로젝트: shaoyexin/cinder
    def reserve(self, context, resources, deltas, expire=None):
        """Check quotas and reserve resources.

        For counting quotas--those quotas for which there is a usage
        synchronization function--this method checks quotas against
        current usage and the desired deltas.

        This method will raise a QuotaResourceUnknown exception if a
        given resource is unknown or if it does not have a usage
        synchronization function.

        If any of the proposed values is over the defined quota, an
        OverQuota exception will be raised with the sorted list of the
        resources which are too high.  Otherwise, the method returns a
        list of reservation UUIDs which were created.

        :param context: The request context, for access checks.
        :param resources: A dictionary of the registered resources.
        :param deltas: A dictionary of the proposed delta changes.
        :param expire: An optional parameter specifying an expiration
                       time for the reservations.  If it is a simple
                       number, it is interpreted as a number of
                       seconds and added to the current time; if it is
                       a datetime.timedelta object, it will also be
                       added to the current time.  A datetime.datetime
                       object will be interpreted as the absolute
                       expiration time.  If None is specified, the
                       default expiration time set by
                       --default-reservation-expire will be used (this
                       value will be treated as a number of seconds).
        """

        # Set up the reservation expiration
        if expire is None:
            expire = FLAGS.reservation_expire
        if isinstance(expire, (int, long)):
            expire = datetime.timedelta(seconds=expire)
        if isinstance(expire, datetime.timedelta):
            expire = timeutils.utcnow() + expire
        if not isinstance(expire, datetime.datetime):
            raise exception.InvalidReservationExpiration(expire=expire)

        # Get the applicable quotas.
        # NOTE(Vek): We're not worried about races at this point.
        #            Yes, the admin may be in the process of reducing
        #            quotas, but that's a pretty rare thing.
        quotas = self._get_quotas(context, resources, deltas.keys(),
                                  has_sync=True)

        # NOTE(Vek): Most of the work here has to be done in the DB
        #            API, because we have to do it in a transaction,
        #            which means access to the session.  Since the
        #            session isn't available outside the DBAPI, we
        #            have to do the work there.
        return db.quota_reserve(context, resources, quotas, deltas, expire,
                                FLAGS.until_refresh, FLAGS.max_age)
예제 #9
0
 def _reserve(self, context, resources, quotas, deltas, expire, project_id):
     # NOTE(Vek): Most of the work here has to be done in the DB
     #            API, because we have to do it in a transaction,
     #            which means access to the session.  Since the
     #            session isn't available outside the DBAPI, we
     #            have to do the work there.
     return db.quota_reserve(context,
                             resources,
                             quotas,
                             deltas,
                             expire,
                             CONF.until_refresh,
                             CONF.max_age,
                             project_id=project_id)