1, the start of Objectorreplicator
First execute the startup script
Swift-init Object-replicator Start
The execution of this execution script is almost identical to the execution of the ring execution script. Locate the Swift source code bin under the Swift-object-replicator its code such as the following see
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)
Last line:
Run_daemon (Objectreplicator, Conf_file, **options)
That is to run the Run_daemon () function. for which the Objectreplicator and config file parameters are already selected, the following continue to see the Run_daemon method. He is the method of swift/daemon.py under the Daemon class. See the detailed code implementation:
Def Run_daemon (Klass, Conf_file, Section_name= ", Once=false, **kwargs):" "" Loads settings from conf, then Instanti Ates Daemon "Klass" and runs the daemon with the specified once kwarg. The section_name is derived from the daemon "Klass" if not provided (e.g. Objectreplicator = Object-replica TOR). :p Aram Klass:class to instantiate, subclass of Common.daemon.Daemon:p Aram conf_file:path to configuration file:p Aram Section_name:section name from conf the file to the load config from:p Aram once:passed to Daemon Run method "" # Very often the Config section_name is based on the class name # The None Singleton would be passed through to readconf As is if Section_name is ": #得到section_name = Ojbect-replicator Sub () is the normal form 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) would 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=kw Args.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_v Alue (Conf.get (' eventlet_debug ', ' no ')) Eventlet.debug.hub_exceptions (eventlet_debug) # ensure TZ Environment VARIABL e 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 ')
Because Objectreplicator inherits the Daemon class, the code fragment
Klass (CONF). Run (once=once, **kwargs)
Objectreplicator runs the Run method, the main time to pass in the once is false general once can be set to true when testing.
Continue to look at the Run method, the Run method is not implemented in objector, it inherits the method of the parent class,
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 = 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)
The Run_forever method is executed because the once is false. It can be seen from the method name surface that this is a permanently executed program. That is, it becomes a guardian process.
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 program self.replicate () total = (Time.time ()-start)/60 Self.logger.info (_ ("Object replication complete. (%.02f minutes) ") Dump_recon_cache ({' object_replication_time ': Total, ' obj Ect_replication_last ': Time.time ()}, Self.rcache, Self.logger) Self.logger.debug (_ ( ' Replication sleeping for%s seconds. '), Self.run_pause) #sleep a period of time to set itself at the time of deployment, and Can feel 30 seconds sleep (self.run_pause)
The replicate () method is run in the Run_forever method. The next section describes the detailed implementation of the Replicate method
Openstack_swift Source Code Analysis--objectreplicator Source Code Analysis (1)