A Preliminary Study of Openstack Swift Source Code -- server.py Under Proxy

As an open source cloud storage tool, OpenStack Swift is used by more and more companies. In order to record and consolidate the open source source code for learning swift, a series of source code open source study notes are made for beginners to quickly learn and understand the internal functions of swift.

The server.py module under proxy is the general entry on the proxy side of swift for all management operations on account, container, object and other objects. After the swift system receives the url request, it is first verified and processed by the middleware processing chain, and then it enters the server module under the proxy. After entering the _call_ method call, the corresponding request is distributed to different controllers for processing, and the controller then Call the respective nodeStroage service program for processing, return the respective resp results to the server module, and finally return the final request processing result through the mimmdleware processing chain in the opposite direction



1. First of all, the entire structure of server.py is as follows: It includes 4 parts: a bunch of import references, a dictionary of required_filters, a class named "Application(object)", an app_factory(global_conf, **local_conf) method



2. Mainly introduce the "Application(object)" class, which contains all the main functional methods





2.1 _init_ method, the initialization method of the Application class, mainly initializes some objects, including: initialization of conf configuration file parameters, log log initialization, memcache object initialization, account_ring, container_ring, object_ring object initialization, etc.



2.2 The check_config(self) method mainly checks whether the "read_affinity" and "sorting_method" attribute values ​​configured in the configuration file proxy-server.conf are correct. This method is called when the app_factory(global_conf, **local_conf): method is called



2.3 The get_controller(self, path) method mainly parses and returns the corresponding control class and a dictionary object according to the incoming urlPath, where the value of the dictionary object returns different values ​​according to the incoming url format



[python] view plaincopy
def get_controller(self, path):
"""
Get the controller to handle a request.

:param path: path from request
:returns: tuple of (controller class, path dictionary)

:raises: ValueError (thrown by split_path) if given invalid path
"""
if path == '/info': #url is /info then return InfoController and a dictionary including version, exposed_info, disallowed_sections, admin_key
d = dict(version=None,
expose_info=self.expose_info,
disallowed_sections=self.disallowed_sections,
admin_key=self.admin_key)
return InfoController, d

version, account, container, obj = split_path(path, 1, 4, True) #Use/split url as a sequence, and take the corresponding 1 to 4 bits of data and return it to the corresponding variable
d = dict(version=version,
account_name=account,
container_name=container,
object_name=obj)
if obj and container and account: #According to the parsed account value, whether congtainer and object are worthwhile, determine the applicable Controller
return ObjectController, d
elif container and account:
return ContainerController, d
elif account and not container and not obj:
return AccountController, d
return None, d





2.4 The __call__(self, env, start_response) method is the actual function entry for the server module to call and process objects such as account, container, and object.



[python] view plaincopy
def __call__(self, env, start_response):
"""
WSGI entry point.
Wraps env in swob.Request object and passes it down.

:param env: WSGI environment dictionary
:param start_response: WSGI callable
"""
try:
if self.memcache is None: #First determine whether the memcache value exists, and if it does not exist, go to get it again
self.memcache = cache_from_env(env)
req = self.update_request(Request(env)) #Determine whether there are x-storage-token and x-auth-token in the header
return self.handle_request(req)(env, start_response) #Call the handle_request method and return the processed result resp object
except UnicodeError:
err = HTTPPreconditionFailed(
request=req, body='Invalid UTF8 or contains NULL')
return err(env, start_response)
except (Exception, Timeout):
start_response('500 Server Error',
[('Content-Type', 'text/plain')])

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us