Skip to content

Instantly share code, notes, and snippets.

@prashanthpai
Created December 18, 2014 12:01
Show Gist options
  • Select an option

  • Save prashanthpai/427e0d1468dacec9022e to your computer and use it in GitHub Desktop.

Select an option

Save prashanthpai/427e0d1468dacec9022e to your computer and use it in GitHub Desktop.
Script to manually clean expired objects from GlusterFS volume.
#!/usr/bin/env python
import os
import argparse
import math
import time
import xattr
import cPickle
METADATA_KEY = 'user.swift.metadata'
def read_metadata(path):
metadata = ''
key = 0
try:
while True:
metadata += xattr.getxattr(path, '%s%s' %
(METADATA_KEY, (key or '')))
key += 1
except IOError:
pass
return cPickle.loads(metadata)
def delete_file(path):
try:
os.unlink(path)
except OSError as e:
if e.errno != errno.ENOENT:
print "delete_file(%s): %s" % (path, e.strerror)
USAGE = "%(prog)s [options] <mount>"
DESCRIPTION="Delete expired objects by crawling the filsystem. \
Expired objects are only printed, unless -d option is specified \
which actually deletes objects from filesystem."
parser = argparse.ArgumentParser(usage=USAGE, description=DESCRIPTION)
parser.add_argument("-d", "--delete", dest="delete_objects",
action="store_true", default=False,
help="Delete expired objects found in filesystem.")
parser.add_argument("-p", "--print", dest="print_objects",
action="store_true", default=True,
help="Print expired objects found in filesystem (Default).")
parser.add_argument("mount", help="Mount point path of a GlusterFS volume.")
args = parser.parse_args()
if not os.path.isdir(args.mount):
parser.error("Invalid mount point path %s" % args.mount)
count = 0
for root, dirs, files in os.walk(args.mount):
for f in files:
path = os.path.join(root, f)
md = read_metadata(path)
if md:
x_delete_at = md.get('X-Delete-At', None)
if x_delete_at:
if int(math.floor(time.time())) > int(x_delete_at):
count += 1
if args.print_objects:
print path
if args.delete_objects:
delete_file(path)
print "Total number of expired objects found: %d" % (count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment