From f243b8c739e589a17bfcfe2fca7ed5744e71900e Mon Sep 17 00:00:00 2001 From: Nick Jones Date: Wed, 11 Nov 2015 15:21:12 +0000 Subject: [PATCH] Address performance issues with listorphans.py script listorphans.py lists orphaned Neutron objects. 'Orphans' in this context are objects which OpenStack knows about and manages but which do not have a valid project (tenant) ID. The previous version was very inefficient in that for every object being checked, it would do a discrete Keystone API call to see if the associated tenant ID was valid or not. For an installation of any reasonable size, i.e one with 100s of Neutron routers, this method was particularly slow. The script has been updated to first build a list of all tenant IDs, and then for every Neutron object check project ownership validity against this list instead. Output has also changed slightly to print out a list of discovered orphans, simplifying workflow e.g when piping to another command which cleans up these objects. Closes-Bug: #1515300 Change-Id: I72ca84fe48beb623d43ee446a32ea1bb30730bcc --- neutron/listorphans.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/neutron/listorphans.py b/neutron/listorphans.py index fbbbf62..efed02c 100755 --- a/neutron/listorphans.py +++ b/neutron/listorphans.py @@ -21,18 +21,19 @@ credentials = get_credentials() neutron = nclient.Client(**credentials) keystone = ksclient.Client(**credentials) +def get_tenantids(): + return [tenant.id for tenant in keystone.tenants.list()] + def get_orphaned_neutron_objects(object): objects = getattr(neutron, 'list_' + object)() + tenantids = get_tenantids() orphans = [] for object in objects.get(object): - try: - keystone.tenants.get(object['tenant_id']) - # If the tenant ID doesn't exist, then this object is orphaned - except ksclient.exceptions.NotFound: + if object['tenant_id'] not in tenantids: orphans.append(object['id']) return orphans -def main(): +if __name__ == '__main__': if len(sys.argv) > 1: if sys.argv[1] == 'all': objects = [ 'networks', 'routers', 'subnets', 'floatingips' ] @@ -40,11 +41,8 @@ def main(): objects = sys.argv[1:] for object in objects: orphans = get_orphaned_neutron_objects(object) - print len(orphans), 'orphan(s) found of type', object, '[%s]' % ', '.join(map(str, orphans)) - + print len(orphans), 'orphan(s) found of type', object + print '\n'.join(map(str, orphans)) else: usage() sys.exit(1) - -if __name__ == '__main__': - main()