分享

Swift源码分析----swift-account-auditor

tntzbzc 发表于 2014-11-20 15:35:02 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 0 13960
本帖最后由 pig2 于 2014-11-21 15:21 编辑
问题导读

1、审计的主要职责是什么?
2、账户是如何获取全局数据的?
3、如果调用的是AccountAuditor类中的run_forever方法,做什么处理?




概述部分:
账户审计守护进程;  
审计的主要的职责就是检查账户数据是否被删除(通过解析账户目录下的.db文件);
这里定义的once=True,说明系统默认调用守护进程类Daemon中的run_once方法,有人说run_once主要应用于测试,这个没有验证;
从而最终实现调用AccountAuditor类中的run_once方法;
如果调用的是AccountAuditor类中的run_forever方法,则会实现循环实现检查指定账户数据是否被删除;

源码解析部分:
  1. from swift.account.auditor import AccountAuditor
  2. from swift.common.utils import parse_options
  3. from swift.common.daemon import run_daemon
  4. if __name__ == '__main__':
  5.     conf_file, options = parse_options(once=True)
  6.     run_daemon(AccountAuditor, conf_file, **options)
  7. def run_daemon(klass, conf_file, section_name='', once=False, **kwargs):
  8.     """
  9.     从配置文件加载设置,然后实例化守护进程“klass”并运行这个守护进程通过指定的参数kwarg;
  10.     """
  11.     ......
  12.    try:
  13.        klass(conf).run(once=once, **kwargs)
  14.    except KeyboardInterrupt:
  15.        logger.info('User quit')
  16.    logger.info('Exited')
  17. def run(self, once=False, **kwargs):
  18.     """
  19.    Run the daemon
  20.     运行守护进程程序;
  21.     即运行方法run_once,或者方法run_forever;
  22.     """
  23.     # 配置参数相关;
  24.    utils.validate_configuration()
  25.    utils.drop_privileges(self.conf.get('user', 'swift'))
  26.     # 日志相关处理;
  27.    utils.capture_stdio(self.logger, **kwargs)
  28.    def kill_children(*args):
  29.        signal.signal(signal.SIGTERM, signal.SIG_IGN)
  30.        os.killpg(0, signal.SIGTERM)
  31.        sys.exit()
  32.    signal.signal(signal.SIGTERM, kill_children)
  33.    if once:
  34.        self.run_once(**kwargs)
  35.    else:
  36.        self.run_forever(**kwargs)
  37. def run_once(self, *args, **kwargs):
  38.     """
  39.    Override this to run the script once
  40.     子类中的方法需要被重写;
  41.     """
  42.    raise NotImplementedError('run_once not implemented')
  43. def run_once(self, *args, **kwargs):
  44.     """
  45.    Run the account audit once.
  46.     运行一次账户审计;
  47.     调用方法_one_audit_pass,首先计算获得path,device,partition;
  48.     然后通过方法account_audit对这些账户内容进行审计,看是否被删除;
  49.     """
  50.    self.logger.info(_('Begin account audit "once" mode'))
  51.    begin = reported = time.time()
  52.    self._one_audit_pass(reported)
  53.    elapsed = time.time() - begin
  54.    self.logger.info(
  55.      _('Account audit "once" mode completed: %.02fs'), elapsed)
  56.      dump_recon_cache({'account_auditor_pass_completed': elapsed},
  57.                       self.rcache, self.logger)
  58. def _one_audit_pass(self, reported):
  59.     """
  60.     首先通过方法audit_location_generator方法计算获得所有以.db为后缀的文件生成path,device,partition;
  61.     然后通过方法account_audit对这些账户内容进行审计,看是否被删除;
  62.     """
  63.         
  64.     # 基于给定的设备路径和文件后缀,为在这个数据目录中的所有以.db为后缀的文件生成元组(path, device, partition);
  65.     # 通过方法audit_location_generator方法计算获得path,device,partition;
  66.    all_locs = audit_location_generator(self.devices, DATADIR, '.db',
  67.                                        mount_check=self.mount_check,
  68.                                        logger=self.logger)
  69.    for path, device, partition in all_locs:
  70.         # 审计给定的账户,看看是否已经被删除,如果没有删除,则为账户获取全局数据;
  71.         # 获取包括account, created_at, put_timestamp, delete_timestamp, container_count, object_count, bytes_used, hash, id等值的字典;
  72.        self.account_audit(path)
  73.        if time.time() - reported >= 3600:  # once an hour
  74.            self.logger.info(_('Since %(time)s: Account audits: '
  75.                               '%(passed)s passed audit,'
  76.                               '%(failed)s failed audit'),
  77.                               {'time': time.ctime(reported),
  78.                                'passed': self.account_passes,
  79.                                'failed': self.account_failures})
  80.            dump_recon_cache({'account_audits_since': reported,
  81.                              'account_audits_passed': self.account_passes,
  82.                              'account_audits_failed':
  83.                              self.account_failures},
  84.                              self.rcache, self.logger)
  85.            reported = time.time()
  86.            self.account_passes = 0
  87.            self.account_failures = 0
  88.        self.accounts_running_time = ratelimit_sleep(self.accounts_running_time, self.max_accounts_per_second)
复制代码

   return reported1.调用方法audit_location_generator,实现基于给定的设备路径(self.devices,DATADIR=accounts)和文件后缀(.db),为在这个数据目录中的所有以.db为后缀的文件生成元组(path, device, partition);
生成路径示例如下:
  1. path = /devices/device/accounts/partition/asuffix/hsh/****.db
  2. device:self.devices下具体的一个设备名称;
  3. partition:/devices/device/accounts/下具体的一个分区名称;
复制代码


2.遍历元组(path, device, partition)中的每一个匹配对,调用方法account_audit实现审计给定的账户,看看是否已经被删除,如果没有删除,则为账户获取全局数据;
转到2,来看方法account_audit:
  1. def account_audit(self, path):
  2.         """
  3.         审计给定的账户,看看是否已经被删除,如果没有删除,则为账户获取全局数据;
  4.         获取包括account, created_at, put_timestamp, delete_timestamp, container_count, object_count, bytes_used, hash, id等值的字典;
  5.         """
  6.       start_time = time.time()
  7.       try:
  8.           broker = AccountBroker(path)
  9.           # is_deleted:检测帐号的数据库是否被删除;
  10.           if not broker.is_deleted():
  11.                   # 为账户获取全局数据;
  12.                   # 返回包括account, created_at, put_timestamp, delete_timestamp, container_count, object_count, bytes_used, hash, id等值的字典;
  13.               broker.get_info()
  14.               self.logger.increment('passes')
  15.               self.account_passes += 1
  16.               self.logger.debug(_('Audit passed for %s') % broker)
  17.       except (Exception, Timeout):
  18.           self.logger.increment('failures')
  19.           self.account_failures += 1
  20.           self.logger.exception(_('ERROR Could not get account info %s'),
  21.                                   path)
  22.       self.logger.timing_since('timing', start_time)这个方法的实现比较简单,这里就不再进一步进行解析;
复制代码




没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条