新手,水平较低,很多地方还不是很理解,错漏在所难免,后续会慢慢完善的。
我总结了一下,想要通过读懂源代码来学习一个新技术,大概可以分为三个步骤走:
1.理解相关的类库的功能及使用。
2.理清代码中对象的继承及调用关系。
3.阅读代码了解细节
相关类库
PythonWeb服务器网关接口(Python Web Server Gateway Interface,缩写为WSGI)
paste.deploy/webob/routes
oslo
1.wsgi
wsgi简要介绍:http://blog.csdn.net/u012977347/article/details/50490597
2.paste.deploy
官方文档:http://pythonpaste.org/deploy/
不想看官档可以看这个简要介绍:http://blog.csdn.net/u012977347/article/details/50465918
3.webob
官方文档:http://webob.org/
不想看官档可以看这个简要介绍:http://blog.csdn.net/u012977347/article/details/50487623
4.routers
官方文档:http://routes.readthedocs.org/en/latest/
不想看官档可以看这个简要介绍:http://blog.csdn.net/u012977347/article/details/50490392
源码流程分析
1.openstack api服务启动脚本api_os_computer.py
def main():
config.parse_args(sys.argv) #加载命令行参数
logging.setup(CONF, "nova") #加载日志模块
utils.monkey_patch() #加载辅助工具
objects.register_all() #不是很确定,应该是nova的对象的关系数据库映射对象类
gmr.TextGuruMeditation.setup_autorun(version)
#启动wsgi server
should_use_ssl = 'osapi_compute' in CONF.enabled_ssl_apis
server = service.WSGIService('osapi_compute', use_ssl=should_use_ssl)
service.serve(server, workers=server.workers)
service.wait()
可以看到nova-api服务都是通过nova.service.WSGIService来管理的。
2.nova.service.WSGIService
class WSGIService(service.Service):
"""Provides ability to launch API from a 'paste' configuration."""
def __init__(self, name, loader=None, use_ssl=False, max_url_len=None):
"""Initialize, but do not start the WSGI server.
:param name: The name of the WSGI server given to the loader.
:param loader: Loads the WSGI application using the given name.
:returns: None
"""
self.name = name #wsgi server的名字,在这里是osapi_compute
self.manager = self._get_manager() #推测和后台有关,迟点回来看
self.loader = loader or wsgi.Loader() #创建一个nova.wsgi.Loader对象,用于加载处理HTTP请求的wsgi app,实现restful接口,后面会继续讨论这个点。
self.app = self.loader.load_app(name) #加载app
# inherit all compute_api worker counts from osapi_compute
if name.startswith('openstack_compute_api'):
wname = 'osapi_compute'
else:
wname = name
self.host = getattr(CONF, '%s_listen' % name, "0.0.0.0") #监听的主机地址
self.port = getattr(CONF, '%s_listen_port' % name, 0) #监听的端口
#推测这里是设置worker的数量,也就是协程的数量,不太确定。
self.workers = (getattr(CONF, '%s_workers' % wname, None) or
processutils.get_worker_count())
if self.workers and self.workers < 1:
worker_name = '%s_workers' % name
msg = (_("%(worker_name)s value of %(workers)s is invalid, "
"must be greater than 0") %
{'worker_name': worker_name,
'workers': str(self.workers)})
raise exception.InvalidInput(msg)
self.use_ssl = use_ssl #是否使用ssl
#调用nova.wsgi.Server创建wsgi server
self.server = wsgi.Server(name,
self.app,
host=self.host,
port=self.port,
use_ssl=self.use_ssl,
max_url_len=max_url_len)
# Pull back actual port used
self.port = self.server.port
self.backdoor_port = None
nova.service.WSGIService的基类是oslo.service,官网对oslo.service的描述是provides functionality for running OpenStack services.
oslo.service的git地址: http://git.openstack.org/cgit/openstack/oslo.service
下载oslo.service的源码来看,可以知道oslo.service.Service是通过eventlet.greenpool来管理wsgiServer的,也就是协程。
3.nova.wsgi.Loader
class Loader(object):
"""Used to load WSGI applications from paste configurations."""
def __init__(self, config_path=None):
"""Initialize the loader, and attempt to find the config.
:param config_path: Full or relative path to the paste config.
:returns: None
"""
self.config_path = None
config_path = config_path or CONF.api_paste_config
if not os.path.isabs(config_path):
self.config_path = CONF.find_file(config_path)
elif os.path.exists(config_path):
self.config_path = config_path
if not self.config_path:
raise exception.ConfigNotFound(path=config_path)
def load_app(self, name):
"""Return the paste URLMap wrapped WSGI application.
:param name: Name of the application to load.
:returns: Paste URLMap object wrapping the requested application.
:raises: `nova.exception.PasteAppNotFound`
"""
try:
LOG.debug("Loading app %(name)s from %(path)s",
{'name': name, 'path': self.config_path})
return deploy.loadapp("config:%s" % self.config_path, name=name)
except LookupError:
LOG.exception(_LE("Couldn't lookup app: %s"), name)
raise exception.PasteAppNotFound(name=name, path=self.config_path)