分享

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

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

1、如何运行守护进程程序?
2、一次container审计操作的流程是怎样的?
3、怎样对以.db为后缀的文件生成元组?









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

源码解析部分:
  1. from swift.container.auditor import ContainerAuditor
  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(ContainerAuditor, 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.     执行一次container审计操作;
  46.     """
  47.    self.logger.info(_('Begin container audit "once" mode'))
  48.    begin = reported = time.time()   
  49.     # 进行一次审计操作;
  50.    self._one_audit_pass(reported)
  51.    elapsed = time.time() - begin
  52.    self.logger.info(
  53.         ('Container audit "once" mode completed: %.02fs'), elapsed)
  54.         dump_recon_cache({'container_auditor_pass_completed': elapsed},
  55.                     self.rcache, self.logger)
  56. def _one_audit_pass(self, reported):
  57.     """
  58.     对container进行审计操作;
  59.     """
  60.     # 基于给定的设备路径和文件后缀,为在这个数据目录中的所有以.db为后缀的文件生成元组(path, device, partition);
  61.     # 通过方法audit_location_generator方法计算获得path,device,partition;
  62.    all_locs = audit_location_generator(self.devices, DATADIR, '.db',
  63.                                        mount_check=self.mount_check,
  64.                                        logger=self.logger)
  65.    for path, device, partition in all_locs:
  66.        # container_audit:对给定的container路径的db文件进行检查;
  67.           # 审计给定的容器,看看是否已经被删除,如果没有删除,则为容器获取全局数据;
  68.        self.container_audit(path)
  69.        if time.time() - reported >= 3600:  # once an hour
  70.            self.logger.info(_('Since %(time)s: Container audits: %(pass)s passed '
  71.                               'audit, %(fail)s failed audit'),
  72.                             {'time': time.ctime(reported),
  73.                              'pass': self.container_passes,
  74.                              'fail': self.container_failures})
  75.            dump_recon_cache(
  76.                             {'container_audits_since': reported,
  77.                              'container_audits_passed': self.container_passes,
  78.                              'container_audits_failed': self.container_failures},
  79.                              self.rcache, self.logger)
  80.            reported = time.time()
  81.            self.container_passes = 0
  82.            self.container_failures = 0
  83.        self.containers_running_time = ratelimit_sleep(self.containers_running_time, self.max_containers_per_second)
复制代码


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


2.遍历元组(path, device, partition)中的每一个匹配对,调用方法container_audit实现审计给定的容器,看看是否已经被删除,如果没有删除,则为账户获取全局数据;

转到2,来看方法container_audit:
  1. def container_audit(self, path):
  2.     """
  3.     对给定的container路径的db文件进行检查;
  4.     审计给定的容器,看看是否被视为删除,如果没有删除,则为容器获取全局数据;
  5.     """
  6.    start_time = time.time()
  7.    try:
  8.        # ContainerBroker:container数据库的封装工作;
  9.        broker = ContainerBroker(path)
  10.        # is_deleted:检测指定容器的数据库是否被视为删除;
  11.        if not broker.is_deleted():
  12.            # get_info:获取container的全局数据;
  13.            broker.get_info()
  14.            self.logger.increment('passes')
  15.            self.container_passes += 1
  16.            self.logger.debug(_('Audit passed for %s'), broker)
  17.        except (Exception, Timeout):
  18.            self.logger.increment('failures')
  19.            self.container_failures += 1
  20.            self.logger.exception(_('ERROR Could not get container info %s'), path)
  21.        self.logger.timing_since('timing', start_time)这个方法的实现比较简单,这里就不再进一步进行解析。
复制代码




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

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

本版积分规则

关闭

推荐上一条 /2 下一条