OpenStack_Swift源代碼分析——ObjectReplicator源代碼分析(1)
阿新 • • 發佈:2017-06-18
ini log tar spa uri () oca bug period
最後一行: 因ObjectReplicator繼承了Daemon類,代碼片段
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‘)
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方法的詳細實現
OpenStack_Swift源代碼分析——ObjectReplicator源代碼分析(1)