分享

Glance源码架构探秘(二)

坎蒂丝_Swan 发表于 2014-12-6 18:52:22 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 0 12230
本帖最后由 坎蒂丝_Swan 于 2014-12-6 19:36 编辑

问题导读


问题1:WSGI的作用流程是怎样的?

问题2:OpenStack的服务为什么没有使用市面上的web server?而又使用了什么库来实现自己的server功能?







Glance源码架构探秘(一)
Glance源码架构探秘(二)
Glance源码架构探秘(三)

Glance源码架构探秘(四)
上一章的最后,为大家介绍了Glance服务的对外启动接口/bin/glance-api,其中最用要的部分就是通过server = eventlet.wsgi.Server()生成了一个http server,并通过server.start()启动了一个WSGI程序。
首先为大家说说这个WSGI程序的作用。WSGI(web server gateway interface)web服务器网关接口。简单来说,WSGI的作用就是将client发给web server的请求转发给要实际处理这个请求的程序。

WSGI在Python中有官方的参考实现wsgiref,http://docs.python.org/2/library/wsgiref.html#module-wsgiref

  1. from wsgiref.simple_server import make_server
  2. # Every WSGI application must have an application object - a callable
  3. # object that accepts two arguments. For that purpose, we're going to
  4. # use a function (note that you're not limited to a function, you can
  5. # use a class for example). The first argument passed to the function
  6. # is a dictionary containing CGI-style envrironment variables and the
  7. # second variable is the callable object (see PEP 333).
  8. def hello_world_app(environ, start_response):
  9.     status = '200 OK' # HTTP Status
  10.     headers = [('Content-type', 'text/plain')] # HTTP Headers
  11.     start_response(status, headers)
  12.     # The returned object is going to be printed
  13.     return ["Hello World"]
  14. httpd = make_server('', 8000, hello_world_app)
  15. print "Serving on port 8000..."
  16. # Serve until process is killed
  17. httpd.serve_forever()
复制代码

参考流程大概就是server中定义一个start_response()函数,返回值为一个write()函数,用来返回给client的响应。application函数要实现两个接口参数,environ和start_response(),前者就是服务器server传递过来的request请求,application控制后者将程序的返回值发回给web server。


这并不是本文所要讨论的重点,如有兴趣可自行做引申阅读。下面我们分析openstack中wsgi接口的实现,/glance/common/wsgi.py

  1. class Server(object):
  2.     """Server class to manage multiple WSGI sockets and applications."""
  3.     def __init__(self, threads=1000):
  4.         self.threads = threads
  5.         self.children = []
  6.         self.running = True
  7.     def start(self, application, default_port):
  8.         """
  9.         Run a WSGI server with the given application.
  10.         :param application: The application to be run in the WSGI server
  11.         :param default_port: Port to bind to if none is specified in conf
  12.         """
  13.         def kill_children(*args):
  14.             """Kills the entire process group."""
  15.             self.logger.info(_('SIGTERM or SIGINT received'))
  16.             signal.signal(signal.SIGTERM, signal.SIG_IGN)
  17.             signal.signal(signal.SIGINT, signal.SIG_IGN)
  18.             self.running = False
  19.             os.killpg(0, signal.SIGTERM)
  20.         def hup(*args):
  21.             """
  22.             Shuts down the server, but allows running requests to complete
  23.             """
  24.             self.logger.info(_('SIGHUP received'))
  25.             signal.signal(signal.SIGHUP, signal.SIG_IGN)
  26.             self.running = False
  27.         self.application = application
  28.         self.sock = get_socket(default_port)
  29.         os.umask(027)  # ensure files are created with the correct privileges
  30.         self.logger = os_logging.getLogger('eventlet.wsgi.server')
  31.         if CONF.workers == 0:
  32.             # Useful for profiling, test, debug etc.
  33.             self.pool = self.create_pool()
  34.             self.pool.spawn_n(self._single_run, self.application, self.sock)
  35.             return
  36.         else:
  37.             self.logger.info(_("Starting %d workers") % CONF.workers)
  38.             signal.signal(signal.SIGTERM, kill_children)
  39.             signal.signal(signal.SIGINT, kill_children)
  40.             signal.signal(signal.SIGHUP, hup)
  41.             while len(self.children) < CONF.workers:
  42.                 self.run_child()
  43.     def create_pool(self):
  44.         eventlet.patcher.monkey_patch(all=False, socket=True)
  45.         return eventlet.GreenPool(size=self.threads)
  46.     def wait_on_children(self):
  47.         while self.running:
  48.             try:
  49.                 pid, status = os.wait()
  50.                 if os.WIFEXITED(status) or os.WIFSIGNALED(status):
  51.                     self.logger.info(_('Removing dead child %s') % pid)
  52.                     self.children.remove(pid)
  53.                     if os.WIFEXITED(status) and os.WEXITSTATUS(status) != 0:
  54.                         self.logger.error(_('Not respawning child %d, cannot '
  55.                                             'recover from termination') % pid)
  56.                         if not self.children:
  57.                             self.logger.info(
  58.                                 _('All workers have terminated. Exiting'))
  59.                             self.running = False
  60.                     else:
  61.                         self.run_child()
  62.             except OSError, err:
  63.                 if err.errno not in (errno.EINTR, errno.ECHILD):
  64.                     raise
  65.             except KeyboardInterrupt:
  66.                 self.logger.info(_('Caught keyboard interrupt. Exiting.'))
  67.                 break
  68.         eventlet.greenio.shutdown_safe(self.sock)
  69.         self.sock.close()
  70.         self.logger.debug(_('Exited'))
  71.     def wait(self):
  72.         """Wait until all servers have completed running."""
  73.         try:
  74.             if self.children:
  75.                 self.wait_on_children()
  76.             else:
  77.                 self.pool.waitall()
  78.         except KeyboardInterrupt:
  79.             pass
  80.     def run_child(self):
  81.         pid = os.fork()
  82.         if pid == 0:
  83.             signal.signal(signal.SIGHUP, signal.SIG_DFL)
  84.             signal.signal(signal.SIGTERM, signal.SIG_DFL)
  85.             # ignore the interrupt signal to avoid a race whereby
  86.             # a child worker receives the signal before the parent
  87.             # and is respawned unneccessarily as a result
  88.             signal.signal(signal.SIGINT, signal.SIG_IGN)
  89.             self.run_server()
  90.             self.logger.info(_('Child %d exiting normally') % os.getpid())
  91.             # self.pool.waitall() has been called by run_server, so
  92.             # its safe to exit here
  93.             sys.exit(0)
  94.         else:
  95.             self.logger.info(_('Started child %s') % pid)
  96.             self.children.append(pid)
  97.     def run_server(self):
  98.         """Run a WSGI server."""
  99.         if cfg.CONF.pydev_worker_debug_host:
  100.             utils.setup_remote_pydev_debug(cfg.CONF.pydev_worker_debug_host,
  101.                                            cfg.CONF.pydev_worker_debug_port)
  102.         eventlet.wsgi.HttpProtocol.default_request_version = "HTTP/1.0"
  103.         try:
  104.             eventlet.hubs.use_hub('poll')
  105.         except Exception:
  106.             msg = _("eventlet 'poll' hub is not available on this platform")
  107.             raise exception.WorkerCreationFailure(reason=msg)
  108.         self.pool = self.create_pool()
  109.         try:
  110.             eventlet.wsgi.server(self.sock,
  111.                                  self.application,
  112.                                  log=WritableLogger(self.logger),
  113.                                  custom_pool=self.pool)
  114.         except socket.error, err:
  115.             if err[0] != errno.EINVAL:
  116.                 raise
  117.         self.pool.waitall()
复制代码

大家可能看了代码之后又觉得头痛,其实没关系,前面有许多行无非是设置log写入程序,打开conf读取ip,port等信息,最重要的内容就是代码最后几行中的

  1. self.pool = self.create_pool()
  2. eventlet.wsgi.server(self.sock,self.application,log=WritableLogger(self.logger),custom_pool=self.pool)
复制代码

刚才提到了server和application之间的通讯接口WSGI,现在我们要讲讲server。OpenStack并没有使用Python标准库中的BaseHTTPServer,而是使用了在网络并发等领域处理效率非常优异的eventlet库http://eventlet.net/



eventlet提供了一套API以实现“协程”(coroutines)。所谓的“协程”可以简单的看做是“假线程”,他可以实现线程的非阻塞异步IO调用的功能,但是协程没有独立的堆栈,这和线程有自己独立的堆栈是有区别的。eventlet会维护一个协程“池”,用来存放所有创建的协程。但是不同于线程,协程同时只能有一个实例在运行,其他的协程要运行,必须等待当前协程显式的被挂起。不同于线程的执行顺序随机,协程的执行时按调用顺序的。

OpenStack的服务,没用使用市面上常见的web server的原因大概就是其处理并发无非就是使用多线程或IO复用等。然而,当多客户端并发访问时,OpenStack内部的一些共享资源,并不能十分安全的利用互斥锁等方法进行线程共享资源的互斥。为了防止并发出现资源死锁,简化架构设计流程,采用“协程”是个非常不错的选择。并且,线程间的切换需要大量的时间和空间的开销,而协程可以有效的避免这个问题。


  1. import eventlet
  2. def handle(client):
  3.     while True:
  4.         c = client.recv(1)
  5.         if not c: break
  6.         client.sendall(c)
  7. server = eventlet.listen(('0.0.0.0', 6000))
  8. pool = eventlet.GreenPool(10000)
  9. while True:
  10.     new_sock, address = server.accept()
  11.     pool.spawn_n(handle, new_sock)
复制代码

上面是eventlet一个简单服务器端的示例,首先用eventlet.GreenPool(1000)生成一个最大容量为1000的“协程”缓冲池,server.accept()等待,服务器server端收到一个客户端的连接请求,就用pool.spawn_n()启动一个“协程”进行处理响应。

回到glance中,OpenStack将Python原版的CGIHTTPServer进行“绿化”,提供了eventlet.wsgi.server进行http的响应,其内部实现结构和作用和上面的代码相似,同样都是来一个http请求,就会启动一个协程server进行响应。参数custom_pool就是我们上面刚刚申请的GreenPool协程池。参数self.application为WSGI程序入口。这样我们就成功运行了一个WSGI服务程序。

本章结束,我们已经成功运行启动了Glance的WSGI服务,下一章将会开始具体介绍WSGI程序所使用的请求分发组件Routes和request与response的包装类webob的相关内容。

本章提到了些高并发访问处理方面的相关内容,实际上eventlet协程的设置上也使用了eventlet.hubs.use_hub('poll'),欢迎大家继续展开阅读与讨论Select I/O poll epoll等相关内容。



欢迎加入about云群90371779322273151432264021 ,云计算爱好者群,亦可关注about云腾讯认证空间||关注本站微信

没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条