Exemple #1
0
    def _check_project_sync(self, context, project, do_fix, resources):
        print('Processing quota usage for project %s' % project)

        discrepancy = False
        action_msg = ' - fixed' if do_fix else ''

        # NOTE: It's important to always get the quota first and then the
        # reservations to prevent deadlocks with quota commit and rollback from
        # running Cinder services.

        # We only want to sync existing quota usage rows
        usages = self._get_usages(context, resources, project)

        # Check for duplicated entries (bug#1484343)
        usages, duplicates_found = self._check_duplicates(
            context,
            usages,
            do_fix,
        )
        if duplicates_found:
            discrepancy = True

        # Check quota and reservations
        for usage in usages:
            resource_name = usage.resource
            # Get the correct value for this quota usage resource
            updates = db_api._get_sync_updates(
                context,
                project,
                resources,
                resource_name,
            )
            in_use = updates[resource_name]
            if in_use != usage.in_use:
                print('\t%s: invalid usage saved=%s actual=%s%s' %
                      (resource_name, usage.in_use, in_use, action_msg))
                discrepancy = True
                if do_fix:
                    usage.in_use = in_use

            reservations = self._get_reservations(
                context,
                project,
                usage.id,
            )
            num_reservations = sum(r.delta for r in reservations
                                   if r.delta > 0)
            if num_reservations != usage.reserved:
                print('\t%s: invalid reserved saved=%s actual=%s%s' % (
                    resource_name,
                    usage.reserved,
                    num_reservations,
                    action_msg,
                ))
                discrepancy = True
                if do_fix:
                    usage.reserved = num_reservations

        return discrepancy
Exemple #2
0
    def _check_sync(self, project_id, do_fix):
        """Check the quotas and reservations optionally fixing them."""

        ctxt = context.get_admin_context()
        # Get the quota usage types and their sync methods
        resources = quota.QUOTAS.resources

        # Get all project ids that have quota usage. Method doesn't lock
        # projects, since newly added projects should not be out of sync and
        # projects removed will just turn nothing on the quota usage.
        projects = self._get_quota_projects(ctxt, project_id)

        session = db_api.get_session()

        action_msg = ' - fixed' if do_fix else ''

        discrepancy = False

        # NOTE: It's important to always get the quota first and then the
        # reservations to prevent deadlocks with quota commit and rollback from
        # running Cinder services.
        for project in projects:
            with session.begin():
                print('Processing quota usage for project %s' % project)
                # We only want to sync existing quota usage rows
                usages = self._get_usages(ctxt, session, resources, project)

                # Check for duplicated entries (bug#1484343)
                usages, duplicates_found = self._check_duplicates(
                    ctxt, session, usages, do_fix)
                if duplicates_found:
                    discrepancy = True

                # Check quota and reservations
                for usage in usages:
                    resource_name = usage.resource
                    # Get the correct value for this quota usage resource
                    updates = db_api._get_sync_updates(ctxt, project, session,
                                                       resources,
                                                       resource_name)
                    in_use = updates[resource_name]
                    if in_use != usage.in_use:
                        print(
                            '\t%s: invalid usage saved=%s actual=%s%s' %
                            (resource_name, usage.in_use, in_use, action_msg))
                        discrepancy = True
                        if do_fix:
                            usage.in_use = in_use

                    reservations = self._get_reservations(
                        ctxt, session, project, usage.id)
                    num_reservations = sum(r.delta for r in reservations
                                           if r.delta > 0)
                    if num_reservations != usage.reserved:
                        print('\t%s: invalid reserved saved=%s actual=%s%s' %
                              (resource_name, usage.reserved, num_reservations,
                               action_msg))
                        discrepancy = True
                        if do_fix:
                            usage.reserved = num_reservations
        print('Action successfully completed')
        return discrepancy