Nova API Source Analysis (a)

Source: Internet
Author: User

Description

Source version: H version

Reference Document: Http://www.choudan.net/2013/12/09/OpenStack-WSGI-APP%E5%AD%A6%E4%B9%A0.html

First, the Prelude

The Nova API itself acts as a WSGI server, providing HTTP request services externally and responding to the corresponding HTTP requests from other modules that call Nova. It is divided into two parts, one is the app that is loaded when the server is created, this is used to process the request, and the server itself starts and runs.

The directory structure is as follows:

First, the Nova API is as a WSGI service, be sure to review its startup process, and view the startup script/etc/init.d/openstack-nova-api(using the service command is actually called/etc/ Script file under the Init.d folder). in the/etc/init.d/openstack-nova-api file, the start option represents the call script/usr/bin/nova-api, as follows:

/usr/bin/nova-api

Import SYS  from Import Main if __name__ " __main__ " : Sys.exit (main())

Then

/nova/cmd/api.py (the file location is/usr/lib/python2.6/site-packages, omitted here and below)

def main(): Config.parse_args (SYS.ARGV) Logging.setup ("Nova") Utils.monkey_patch () Launcher=Service.process_launcher () forApiinchCONF.enabled_apis:should_use_ssl= APIinchConf.enabled_ssl_apisifAPI = ='EC2': "" in order to be compatible with EC2  "" "Server= Service. Wsgiservice (API, use_ssl=Should_use_ssl, Max_url_len=16384)         Else: Server= Service.Wsgiservice(API, USE_SSL=SHOULD_USE_SSL)"1 " launcher. Launch_service (Server, Workers=server.workersor1)"2""""The current process is in a waiting state"""launcher. wait ()

Second, the analysis of "1" place: mainly the creation of Wsgiservice objects

/nova/service.py

Wsgiservice class:def __init__(Self, name, Loader=none, Use_ssl=false, max_url_len=None): Self.name=name Self.manager=Self._get_manager () self.loader = Loader or WSGI. Loader () Self.app = Self.loader.load_app (name) Self.host= GetAttr (CONF,'%s_listen'% Name,"0.0.0.0") Self.port= GetAttr (CONF,'%s_listen_port'%name, 0) self.workers= GetAttr (CONF,'%s_workers'%name, None) Self.use_ssl=Use_ssl"""encapsulates the server in the wsgi.py"""self.server = Wsgi.                              Server (name, Self.app, Host=self.host, Port=self.port, Use_ssl=self.use_ssl, Max_url_len=max_url_le N)#Pull back actual port usedSelf.port =Self.server.port Self.backdoor_port= None

/nova/wsgi.py

Loader class:def __init__(Self, config_path=None): "" "trying to find the Api-paste.ini configuration file"""Self.config_path=None Config_path= Config_pathorConf.api_paste_configif  notOs.path.isabs (Config_path): self. Config_path =conf.find_file (Config_path)elifos.path.exists (Config_path): self. Config_path =Config_pathif  notSelf.config_path:Raiseexception. Confignotfound (path=Config_path)def Load_app(self, name):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)exceptLookuperror as Err:LOG.error (err)Raiseexception. Pasteappnotfound (Name=name, Path=self.config_path)

See the "Nova API Source Analysis (ii)" For the specific loading process of the app here.

Third, the analysis of "2" place: Mainly the start of Wsgiservice

/nova/openstack/common/service.py

Processlauncher class:def Launch_service(Self, service, Workers=1): Wrap=Servicewrapper (service, workers) Log.info (_ ('Starting%d workers'), wrap.workers) "" "Cycle start workers number of child processes""" whileSelf.running andLen (Wrap.children) <wrap.workers:self. _start_child (Wrap)def _start_child(self, wrap): ... "" "Create Child process"""PID=os.fork ()ifPID = =0:status=0Try: "" "child processes for processing"""self._child_process (Wrap.service)exceptSignalexit as Exc:signame= {signal. SIGTERM:'SIGTERM', Signal. SIGINT:'SIGINT'}[exc.signo] Log.info (_ ('caught%s, exiting'), signame) status=Exc.codeexceptSystemexit as Exc:status=Exc.codeexceptBaseException:LOG.exception (_ ('Unhandled Exception')) Status= 2finally: "" "Unexpected stop service"""wrap.service.stop () "" "Child Process Exit"""os._exit (status) Log.info (_ ('Started Child%d'), PID) Wrap.children.add (PID) self.children[pid]=WrapreturnPIDdef _child_process(self, Service): ... launcher= Launcher() Launcher. Run_service (service)

/nova/openstack/common/service.py

Launcher class: def __init__ (self):     = Threadgroup. Threadgroup ()    = eventlet_backdoor.initialize_if_enabled () @staticmethoddef   Run_service(Service):    service.start ()    service.wait () 

Because the service here is the Wsgiservice object, as follows:

/nova/service.py

Wsgiservice class:def start(self):
ifself.manager:self.manager.init_host () Self.manager.pre_start_hook ( )ifSelf.backdoor_port is notNone:self.manager.backdoor_port=self.backdoor_port self.server.start () ifSelf.manager:self.manager.post_start_hook ()def wait(self): self.server.wait ()

/nova/wsgi.py

Server class:def start(self): ... Wsgi_kwargs = {        'func': Eventlet.wsgi.server,'sock': Self._socket,'site': Self.app,'Protocol': Self._protocol,'Custom_pool': Self._pool,'Log': Self._wsgi_logger,'Log_format': Conf.wsgi_log_format}ifself._max_url_len:wsgi_kwargs['Url_length_limit'] =Self._max_url_len"""Create a green thread, run the Func function, and pass in the following arguments. Here is a call to the Eventlet.wsgi.server function, passing parameters such as sock=self._socket into the server function. Where the Eventlet.wsgi.server function is responsible for initiating a WSGI server program to accept requests from the client,
Refer to: Http://eventlet.net/doc/modules/wsgi.html?highlight=wsgi.server#eventlet.wsgi.server"""self._server = Eventlet.spawn (**wsgi_kwargs)def wait(self):Try: """Green thread blocking wait"""self._server.wait ()exceptGreenlet. GreenletExit:LOG.info (_ ("WSGI server has stopped."))

Summarize:

We can see the number of processes in the system by the Pstree command, and found that there is a master process in the NOVA-API process, the number of child processes is a EC2, a metadata, workers number Osapi_compute (where workers can pass/ Etc/nova/nova.conf in the Osapi_compute_workers option setting). The number of threads per process is then viewed through PS–LF, and it is found to be single-threaded. As you can see, the process of creating and starting the entire server is that the main process produces several sub-processes that use green thread to start WSGI server and wait for the service.

Nova API Source Analysis (a)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.