simplifying options and code

This commit is contained in:
David Goetz
2011-02-21 16:37:12 -08:00
parent 7144693bf5
commit a86a569cae
5 changed files with 35 additions and 38 deletions

View File

@@ -21,9 +21,7 @@ from optparse import OptionParser
if __name__ == '__main__': if __name__ == '__main__':
parser = OptionParser("%prog CONFIG [options]") parser = OptionParser("%prog CONFIG [options]")
parser.add_option('-z', '--zero_byte_only', default=False, parser.add_option('-z', '--zero_byte_fps',
action='store_true', help='Audit only zero byte files') help='Audit only zero byte files at specified files/sec')
parser.add_option('-f', '--zero_byte_fps',
help='Override zero byte files per second in config.')
conf_file, options = parse_options(parser=parser, once=True) conf_file, options = parse_options(parser=parser, once=True)
run_daemon(ObjectAuditor, conf_file, **options) run_daemon(ObjectAuditor, conf_file, **options)

View File

@@ -297,7 +297,6 @@ trying to write and produce zero byte files. The object-auditor will catch
these problems but in the case of a system crash it would be advisable to run these problems but in the case of a system crash it would be advisable to run
an extra, less rate limited sweep to check for these specific files. You can an extra, less rate limited sweep to check for these specific files. You can
run this command as follows: run this command as follows:
`swift-object-auditor /path/to/object-server/config/file.conf once -z -f 1000` `swift-object-auditor /path/to/object-server/config/file.conf once -z 1000`
"-z" will check for only zero-byte files and "-f" overrides the "-z" means to only check for zero-byte files at 1000 files per second.
zero_byte_files_per_second to a 1000 from the config file, which by default is
only 50.

View File

@@ -72,3 +72,5 @@ use = egg:swift#object
# files_per_second = 20 # files_per_second = 20
# bytes_per_second = 10000000 # bytes_per_second = 10000000
# log_time = 3600 # log_time = 3600
# zero_byte_files_per_second = 50

View File

@@ -29,7 +29,7 @@ from swift.common.daemon import Daemon
class AuditorWorker(object): class AuditorWorker(object):
"""Walk through file system to audit object""" """Walk through file system to audit object"""
def __init__(self, conf, zero_byte_file_worker=False, zero_byte_fps=None): def __init__(self, conf, zero_byte_only_at_fps=0):
self.conf = conf self.conf = conf
self.logger = get_logger(conf, log_route='object-auditor') self.logger = get_logger(conf, log_route='object-auditor')
self.devices = conf.get('devices', '/srv/node') self.devices = conf.get('devices', '/srv/node')
@@ -39,16 +39,9 @@ class AuditorWorker(object):
self.max_bytes_per_second = float(conf.get('bytes_per_second', self.max_bytes_per_second = float(conf.get('bytes_per_second',
10000000)) 10000000))
self.auditor_type = 'ALL' self.auditor_type = 'ALL'
self.fasttrack_zero_byte_files = conf.get( self.zero_byte_only_at_fps = zero_byte_only_at_fps
'fasttrack_zero_byte_files', 'False').lower() in TRUE_VALUES if self.zero_byte_only_at_fps:
self.zero_byte_file_worker = zero_byte_file_worker self.max_files_per_second = float(self.zero_byte_only_at_fps)
if self.zero_byte_file_worker:
self.fasttrack_zero_byte_files = True
if zero_byte_fps:
self.max_files_per_second = float(zero_byte_fps)
else:
self.max_files_per_second = float(
conf.get('zero_byte_files_per_second', 50))
self.auditor_type = 'ZBF' self.auditor_type = 'ZBF'
self.log_time = int(conf.get('log_time', 3600)) self.log_time = int(conf.get('log_time', 3600))
self.files_running_time = 0 self.files_running_time = 0
@@ -135,8 +128,7 @@ class AuditorWorker(object):
raise AuditException('Content-Length of %s does not match ' raise AuditException('Content-Length of %s does not match '
'file size of %s' % (int(df.metadata['Content-Length']), 'file size of %s' % (int(df.metadata['Content-Length']),
os.path.getsize(df.data_file))) os.path.getsize(df.data_file)))
if self.fasttrack_zero_byte_files and \ if self.zero_byte_only_at_fps and obj_size:
bool(self.zero_byte_file_worker) == bool(obj_size):
return return
etag = md5() etag = md5()
for chunk in df: for chunk in df:
@@ -172,31 +164,30 @@ class ObjectAuditor(Daemon):
def __init__(self, conf, **options): def __init__(self, conf, **options):
self.conf = conf self.conf = conf
self.logger = get_logger(conf, 'object-auditor') self.logger = get_logger(conf, 'object-auditor')
self.fasttrack_zero_byte_files = conf.get( self.conf_zero_byte_fps = int(conf.get(
'fasttrack_zero_byte_files', 'False').lower() in TRUE_VALUES 'zero_byte_files_per_second', 50))
def run_forever(self, *args, **kwargs): def run_forever(self, *args, **kwargs):
"""Run the object audit until stopped.""" """Run the object audit until stopped."""
zero_byte_only = kwargs.get('zero_byte_only', False) zero_byte_only_at_fps = kwargs.get('zero_byte_fps', 0) or \
zero_byte_fps = kwargs.get('zero_byte_fps', None) self.conf_zero_byte_fps
zero_byte_pid = 1 zero_byte_pid = 1
if zero_byte_only or self.fasttrack_zero_byte_files: if zero_byte_only_at_fps:
zero_byte_pid = os.fork() zero_byte_pid = os.fork()
if zero_byte_pid == 0: if zero_byte_pid == 0:
while True: while True:
self.run_once(mode='forever', zero_byte_only=True, self.run_once(mode='forever',
zero_byte_fps=zero_byte_fps) zero_byte_fps=zero_byte_only_at_fps)
time.sleep(30) time.sleep(30)
else: else:
while not zero_byte_only: while not zero_byte_only_at_fps:
self.run_once(mode='forever') self.run_once(mode='forever')
time.sleep(30) time.sleep(30)
def run_once(self, *args, **kwargs): def run_once(self, *args, **kwargs):
"""Run the object audit once.""" """Run the object audit once."""
mode = kwargs.get('mode', 'once') mode = kwargs.get('mode', 'once')
zero_byte_only = kwargs.get('zero_byte_only', False) zero_byte_only_at_fps = kwargs.get('zero_byte_fps', 0)
zero_byte_fps = kwargs.get('zero_byte_fps', None) worker = AuditorWorker(self.conf,
worker = AuditorWorker(self.conf, zero_byte_file_worker=zero_byte_only, zero_byte_only_at_fps=zero_byte_only_at_fps)
zero_byte_fps=zero_byte_fps)
worker.audit_all_objects(mode=mode) worker.audit_all_objects(mode=mode)

View File

@@ -234,7 +234,6 @@ class TestAuditor(unittest.TestCase):
self.assertEquals(self.auditor.quarantines, pre_quarantines + 1) self.assertEquals(self.auditor.quarantines, pre_quarantines + 1)
def test_object_run_fast_track_non_zero(self): def test_object_run_fast_track_non_zero(self):
self.conf['fasttrack_zero_byte_files'] = 'yes'
self.auditor = auditor.ObjectAuditor(self.conf) self.auditor = auditor.ObjectAuditor(self.conf)
self.auditor.log_time = 0 self.auditor.log_time = 0
cur_part = '0' cur_part = '0'
@@ -259,13 +258,12 @@ class TestAuditor(unittest.TestCase):
quarantine_path = os.path.join(self.devices, quarantine_path = os.path.join(self.devices,
'sda', 'quarantined', 'objects') 'sda', 'quarantined', 'objects')
self.auditor.run_once(zero_byte_only=True) self.auditor.run_once(zero_byte_fps=50)
self.assertFalse(os.path.isdir(quarantine_path)) self.assertFalse(os.path.isdir(quarantine_path))
self.auditor.run_once() self.auditor.run_once()
self.assertTrue(os.path.isdir(quarantine_path)) self.assertTrue(os.path.isdir(quarantine_path))
def test_object_run_fast_track_zero(self): def setup_bad_zero_byte(self):
self.conf['fasttrack_zero_byte_files'] = 'yes'
self.auditor = auditor.ObjectAuditor(self.conf) self.auditor = auditor.ObjectAuditor(self.conf)
self.auditor.log_time = 0 self.auditor.log_time = 0
cur_part = '0' cur_part = '0'
@@ -283,11 +281,20 @@ class TestAuditor(unittest.TestCase):
etag = etag.hexdigest() etag = etag.hexdigest()
metadata['ETag'] = etag metadata['ETag'] = etag
write_metadata(fd, metadata) write_metadata(fd, metadata)
def test_object_run_fast_track_all(self):
self.setup_bad_zero_byte()
self.auditor.run_once()
quarantine_path = os.path.join(self.devices, quarantine_path = os.path.join(self.devices,
'sda', 'quarantined', 'objects') 'sda', 'quarantined', 'objects')
self.auditor.run_once()
self.assertTrue(os.path.isdir(quarantine_path)) self.assertTrue(os.path.isdir(quarantine_path))
def test_object_run_fast_track_zero(self):
self.setup_bad_zero_byte()
self.auditor.run_once(zero_byte_fps=50)
quarantine_path = os.path.join(self.devices,
'sda', 'quarantined', 'objects')
self.assertTrue(os.path.isdir(quarantine_path))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()