这里都是通过pdb调试的,pdb教程见这里:http://iceyao.blog.51cto.com/9426658/1599320

# 先从nova-manage这个程序开始
[root@ceph01 ~]# which nova-manage
/usr/bin/nova-manage

[root@ceph01 ~]#
[root@ceph01 ~]# vim /usr/bin/nova-manage
#!/usr/bin/python
# PBR Generated from u'console_scripts'
import sys
from nova.cmd.manage import main
if __name__ == "__main__":
    sys.exit(main())
    
    
[root@ceph01 ~]# nova-manage service list
> /usr/lib/python2.7/site-packages/nova/cmd/manage.py(1402)main()
-> ret = fn(*fn_args, **fn_kwargs)
(Pdb) l
1397 	        CONF.print_help()
1398 	        print(e)
1399 	        return(1)
1400 	    try:
1401 	        import pdb;pdb.set_trace()       # 断点设在这里,前面是一些cfg、rpc的过程,这里不介绍
1402 ->	        ret = fn(*fn_args, **fn_kwargs)  
1403 	        rpc.cleanup()
1404 	        return(ret)
1405 	    except Exception:
1406 	        print(_("Command failed, please check log for more info"))
1407 	        raise
(Pdb) s     # 进入函数内部
--Call--
> /usr/lib/python2.7/site-packages/nova/cmd/manage.py(696)list()
-> @args('--host', metavar='', help='Host')
(Pdb) l
691
692
693  class ServiceCommands(object):       # ServiceCommands类
694      """Enable and disable running services."""
695
696  ->    @args('--host', metavar='', help='Host')
697      @args('--service', metavar='', help='Nova service')
698      def list(self, host=None, service=None):
699          """Show a list of all running services. Filter by host & service
700          name
701          """
(Pdb)
702          servicegroup_api = servicegroup.API()      #  s进入函数内部,提供三种servicegroup driver(可配置),这里返回db
703          ctxt = context.get_admin_context()     
704          services = db.service_get_all(ctxt)        #  获得nova service表所有数据
705          services = availability_zones.set_availability_zones(ctxt, services)  # 获得的service,按availablity_zone划分
706          if host:
707              services = [s for s in services if s['host'] == host]
708          if service:
709              services = [s for s in services if s['binary'] == service]
710          print_format = "%-16s %-36s %-16s %-10s %-5s %-10s"
711          print(print_format % (
712                      _('Binary'),


713                      _('Host'),
(Pdb)
714                      _('Zone'),
715                      _('Status'),
716                      _('State'),
717                      _('Updated_At')))
718          for svc in services:
719              alive = servicegroup_api.service_is_up(svc)     # 这里有判断服务是否是up的
720              art = (alive and ":-)") or "XXX"
721              active = 'enabled'
722              if svc['disabled']:
723                  active = 'disabled'
724              print(print_format % (svc['binary'], svc['host'],
(Pdb) l
725                                    svc['availability_zone'], active, art,
726                                    svc['updated_at']))


-> alive = servicegroup_api.service_is_up(svc)
(Pdb) l
714                      _('Zone'),
715                      _('Status'),
716                      _('State'),
717                      _('Updated_At')))
718          for svc in services:
719  ->            alive = servicegroup_api.service_is_up(svc)   # s,进入service_ip_up函数内部
720              art = (alive and ":-)") or "XXX"
721              active = 'enabled'
722              if svc['disabled']:
723                  active = 'disabled'
724              print(print_format % (svc['binary'], svc['host'],
(Pdb) s
--Call--
> /usr/lib/python2.7/site-packages/nova/servicegroup/api.py(112)service_is_up()
-> def service_is_up(self, member):
(Pdb) l
107                  '%(group_id)s group, service = %(service)s')
108          LOG.debug(msg, {'member_id': member_id, 'group_id': group_id,
109                          'service': service})
110          return self._driver.join(member_id, group_id, service)
111
112  ->    def service_is_up(self, member):
113          """Check if the given member is up."""
114          # NOTE(johngarbutt) no logging in this method,
115          # so this doesn't slow down the scheduler
116          return self._driver.is_up(member)      # s, 进入is_up函数
117


-> return self._driver.is_up(member)
(Pdb) l
111
112      def service_is_up(self, member):
113          """Check if the given member is up."""
114          # NOTE(johngarbutt) no logging in this method,
115          # so this doesn't slow down the scheduler
116  ->        return self._driver.is_up(member)
117
118      def leave(self, member_id, group_id):
119          """Explicitly remove the given member from the ServiceGroup
120          monitoring.
121          """
(Pdb) s
--Call--
> /usr/lib/python2.7/site-packages/nova/servicegroup/drivers/db.py(55)is_up()
-> def is_up(self, service_ref):
(Pdb) l
 50          report_interval = service.report_interval
 51          if report_interval:
 52              service.tg.add_timer(report_interval, self._report_state,
 53                                   api.INITIAL_REPORTING_DELAY, service)
 54
 55  ->    def is_up(self, service_ref):
 56          """Moved from nova.utils
 57          Check whether a service is up based on last heartbeat.
 58          """
 59          last_heartbeat = service_ref['updated_at'] or service_ref['created_at']  # 获得服务最后一次更新时间或是第一次创建的时间
 60          if isinstance(last_heartbeat, six.string_types):
 61  	            # NOTE(russellb) If this service_ref came in over rpc via
 62  	            # conductor, then the timestamp will be a string and needs to be
 63  	            # converted back to a datetime.
 64  	            last_heartbeat = timeutils.parse_strtime(last_heartbeat)
(Pdb) l
 65  	        else:
 66  	            # Objects have proper UTC timezones, but the timeutils comparison
 67  	            # below does not (and will fail)
 68  	            last_heartbeat = last_heartbeat.replace(tzinfo=None)
 69  	        # Timestamps in DB are UTC.
 70  	        elapsed = timeutils.delta_seconds(last_heartbeat, timeutils.utcnow())  # 与当前时间的差值
 71  	        is_up = abs(elapsed) <= self.service_down_time       # 如果这个差值小于service_down_time,就认为这个服务是up的,service_down_time是可配置的
 72  	        if not is_up:
 73  	            msg = _('Seems service is down. Last heartbeat was %(lhb)s. '
 74  	                    'Elapsed time is %(el)s')
 75  	            LOG.debug(msg, {'lhb': str(last_heartbeat), 'el': str(elapsed)})
(Pdb)
 76  	        return is_up