Skip to content

Instantly share code, notes, and snippets.

@clayg
Last active September 6, 2023 10:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save clayg/7f66eab2a61c77869e1e84ac4ed6f1df to your computer and use it in GitHub Desktop.
Save clayg/7f66eab2a61c77869e1e84ac4ed6f1df to your computer and use it in GitHub Desktop.
check on the expirer queue
#!/usr/bin/env python
from collections import defaultdict
import sys
import time
from argparse import ArgumentParser
from swift.common.internal_client import InternalClient
from swift.common.utils import Timestamp
from swift.common.wsgi import ConfigString
from swift.container.sync import ic_conf_body
from swift.obj.expirer import parse_task_obj
parser = ArgumentParser()
parser.add_argument('-v', '--verbose', action='store_true',
help='more detailed info')
parser.add_argument('--objects-container-divisor', type=int, default=86400,
help='The window size for pending expirations')
parser.add_argument('-d', '--details', action='store_true',
help='more detailed counts')
def main():
args = parser.parse_args()
now = time.time()
t_now = Timestamp.now()
expiring_account = '.expiring_objects'
conf = ConfigString(ic_conf_body)
swift = InternalClient(conf, 'swift-expirer-status', 1)
stats = defaultdict(int)
for container_item in swift.iter_containers(expiring_account):
if args.verbose:
print ('%s/' % expiring_account +
'%(name)s: %(count)s' % container_item)
try:
container = int(container_item['name'])
except ValueError:
continue
stats['total'] += container_item['count']
if container < now:
if container < (now - args.objects_container_divisor):
stats['stale'] += container_item['count']
else:
if not args.details:
stats['pending'] += container_item['count']
continue
for object_item in swift.iter_objects(
expiring_account, container_item['name']):
if args.verbose:
print '%(name)s: %(content_type)s' % object_item
# this should probably be done in parse_task_obj
task_object = object_item['name'].encode('utf8')
delete_timestamp, _target_account, _target_container, \
_target_object = parse_task_obj(task_object)
if t_now > delete_timestamp:
stats['stale'] += 1
else:
stats['pending'] += 1
print ' Total entries: %(total)s' % stats
print 'Pending entries: %(pending)s' % stats
print ' Stale entries: %(stale)s' % stats
if __name__ == "__main__":
sys.exit(main())
@mcv21
Copy link

mcv21 commented Feb 1, 2023

Hi,

I used this to look at an issue we're investigating at work, made a couple of tweaks to make it run on a Debian bullseye system (and correspondingly more recent Swift code). In case I want to use it again, would you mind agreeing to licensing it please? [e.g. Apache-2].

Work = The Wikimedia Foundation, a non-profit org.

--- swift-expired-status        2023-02-01 11:55:23.588929202 +0000
+++ swift_expired_stats.py      2023-02-01 11:41:02.780967075 +0000
@@ -1,4 +1,5 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
+#originally from https://gist.github.com/clayg/7f66eab2a61c77869e1e84ac4ed6f1df
 from collections import defaultdict
 import sys
 import time
@@ -47,19 +48,17 @@
                 for object_item in swift.iter_objects(
                         expiring_account, container_item['name']):
                     if args.verbose:
-                        print '%(name)s: %(content_type)s' % object_item
-                    # this should probably be done in parse_task_obj
-                    task_object = object_item['name'].encode('utf8')
+                        print('%(name)s: %(content_type)s' % object_item)
                     delete_timestamp, _target_account, _target_container, \
-                        _target_object = parse_task_obj(task_object)
+                        _target_object = parse_task_obj(object_item['name'])
                     if t_now > delete_timestamp:
                         stats['stale'] += 1
                     else:
                         stats['pending'] += 1
 
-    print '  Total entries: %(total)s' % stats
-    print 'Pending entries: %(pending)s' % stats
-    print '  Stale entries: %(stale)s' % stats
+    print('  Total entries: %(total)s' % stats)
+    print('Pending entries: %(pending)s' % stats)
+    print('  Stale entries: %(stale)s' % stats)
 
 
 if __name__ == "__main__":

@clayg
Copy link
Author

clayg commented Feb 1, 2023

Let's just submit it to gerrit as a new cli tool to the main swift repo, you can add me as a co-author or "inspired by" if you want.

@mcv21
Copy link

mcv21 commented Feb 3, 2023

Sounds good to me; I've put this on my TODO (no promises as to timescale)

@mcv21
Copy link

mcv21 commented Sep 6, 2023

I've finally got round to doing this - you can see the CR here:
https://review.opendev.org/c/openstack/swift/+/893861

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment