1、ObjectorReplicator的启动

首先执行启动脚本

swift-init object-replicator start

此执行脚本的执行过程和ring执行脚本执行过程差点儿相同。找到swift 源代码bin下的swift-object-replicator其代码例如以下所看到的

if __name__ == '__main__':
parser = OptionParser("%prog CONFIG [options]")
parser.add_option('-d', '--devices',
help='Replicate only given devices. '
'Comma-separated list')
parser.add_option('-p', '--partitions',
help='Replicate only given partitions. '
'Comma-separated list')
conf_file, options = parse_options(parser=parser, once=True)
run_daemon(ObjectReplicator, conf_file, **options)

最后一行:

 run_daemon(ObjectReplicator, conf_file, **options)

也就是要运行run_daemon()函数。为其传入的是ObjectReplicator和配置文件參数已经选项參数,以下继续看run_daemon方法。他是swift/daemon.py下Daemon类中的方法。看详细代码实现:

def run_daemon(klass, conf_file, section_name='', once=False, **kwargs):
"""
Loads settings from conf, then instantiates daemon "klass" and runs the
daemon with the specified once kwarg. The section_name will be derived
from the daemon "klass" if not provided (e.g. ObjectReplicator =>
object-replicator). :param klass: Class to instantiate, subclass of common.daemon.Daemon
:param conf_file: Path to configuration file
:param section_name: Section name from conf file to load config from
:param once: Passed to daemon run method
"""
# very often the config section_name is based on the class name
# the None singleton will be passed through to readconf as is
if section_name is '':
#得到section_name = ojbect-replicator sub()为正則表達式
section_name = sub(r'([a-z])([A-Z])', r'\1-\2',
klass.__name__).lower() conf = utils.readconf(conf_file, section_name,
log_name=kwargs.get('log_name')) # once on command line (i.e. daemonize=false) will over-ride config
once = once or not utils.config_true_value(conf.get('daemonize', 'true')) # pre-configure logger
if 'logger' in kwargs:
logger = kwargs.pop('logger')
else:
logger = utils.get_logger(conf, conf.get('log_name', section_name),
log_to_console=kwargs.pop('verbose', False),
log_route=section_name) # disable fallocate if desired
if utils.config_true_value(conf.get('disable_fallocate', 'no')):
utils.disable_fallocate()
# set utils.FALLOCATE_RESERVE if desired
reserve = int(conf.get('fallocate_reserve', 0))
if reserve > 0:
utils.FALLOCATE_RESERVE = reserve # By default, disable eventlet printing stacktraces
eventlet_debug = utils.config_true_value(conf.get('eventlet_debug', 'no'))
eventlet.debug.hub_exceptions(eventlet_debug) # Ensure TZ environment variable exists to avoid stat('/etc/localtime') on
# some platforms. This locks in reported times to the timezone in which
# the server first starts running in locations that periodically change
# timezones.
os.environ['TZ'] = time.strftime("%z", time.gmtime()) try:
#開始执行
klass(conf).run(once=once, **kwargs)
except KeyboardInterrupt:
logger.info('User quit')
logger.info('Exited')

因ObjectReplicator继承了Daemon类,代码片段

 klass(conf).run(once=once, **kwargs)

ObjectReplicator运行run方法,主要此时传入的once为False一般once在測试时能够设为True。

继续看run方法,在Objector中没有实现run方法,其继承了父类的方法,

    def run(self, once=False, **kwargs):
"""Run the daemon"""
utils.validate_configuration()
utils.drop_privileges(self.conf.get('user', 'swift'))
utils.capture_stdio(self.logger, **kwargs) def kill_children(*args):
#SIGTERM = 15 SIG_IGN = 1L
signal.signal(signal.SIGTERM, signal.SIG_IGN)
os.killpg(0, signal.SIGTERM)
sys.exit() signal.signal(signal.SIGTERM, kill_children)
if once:
self.run_once(**kwargs)
else:
self.run_forever(**kwargs)

因once为False所以执行的是run_forever方法。从方法名字面上就能够看出这是一个永久执行的程序。也就是会成为守护进程。

  def run_forever(self, *args, **kwargs):
self.logger.info(_("Starting object replicator in daemon mode."))
# Run the replicator continually
while True:
start = time.time()
self.logger.info(_("Starting object replication pass."))
# Run the replicator
#运行replicator 程序
self.replicate()
total = (time.time() - start) / 60
self.logger.info(
_("Object replication complete. (%.02f minutes)"), total)
dump_recon_cache({'object_replication_time': total,
'object_replication_last': time.time()},
self.rcache, self.logger)
self.logger.debug(_('Replication sleeping for %s seconds.'),
self.run_pause)
#sleep 一段时间时间自己在部署时自己设定,也能够默觉得30秒
sleep(self.run_pause)

在run_forever方法中会运行replicate()方法。下一节介绍replicate方法的详细实现

最新文章

  1. UNDER THE HOOD OF THE NEW AZURE PORTAL
  2. Verilog学习笔记设计和验证篇(五)...............层次化事件队列
  3. How to change statusbar text color to dark on android 4.4
  4. Flex Error #2156问题
  5. c语言之sizeof总结
  6. getUrlParam,jQuery中的URL参数获取
  7. uva 12171 hdu 1771 Sculpture
  8. SEO-百度推出新算法如何应对
  9. python 数据分析工具之 numpy pandas matplotlib
  10. 记一次Dynamic Batching不生效的爬坑实例分析[Unity]
  11. Django--QuerySet--基础查询
  12. linux命令学习(4):cd命令
  13. python内置函数bin
  14. Python无参装饰器
  15. BZOJ4571: [Scoi2016]美味【主席树】【贪心】
  16. docker 日志管理
  17. Java Persistence with MyBatis 3(中文版)
  18. 【Python】python文件名和文件路径操作
  19. Tomcat在处理GET和POST请求时产生的乱码问题
  20. 关于死循环while(true){}或for(;;){}的总结

热门文章

  1. 紫书 例题 10-7 UVa 10820 (欧拉函数)
  2. HDU 4928 Series 2
  3. 【Henu ACM Round#19 F】Dispute
  4. hadoop MR 任务 报错 "Error: java.io.IOException: Premature EOF from inputStream at org.apache.hadoop.io"
  5. bzoj 2120 数颜色 题解
  6. Ubuntu14环境下minigui安装问题记录--object.lo错误
  7. js --- 中字符串与unicode编码
  8. Weka中数据挖掘与机器学习系列之为什么要写Weka这一系列学习笔记?(一)
  9. JS的解析与执行过程—函数预处理
  10. 记一次 Apache HUE 优化之因使用 Python 魔术方法而遇到的坑