Instantiate wsgi app for each worker

A single WSGI app, and therefore a single sqlalchemy engine (mysql
connection), was being used across all API workers. This patch
instantiates the WSGI app once per worker rather than up front to
give a separate database connection to each worker.

Fixes bug 1036193

Change-Id: I2cbb61fd88a6ec4fb03fa84196ba0a380b3842d0
This commit is contained in:
Brian Waldon 2012-08-18 16:50:15 -04:00
parent b7b2a0997b
commit 112fc71ba4
7 changed files with 53 additions and 61 deletions

View File

@ -52,10 +52,8 @@ if __name__ == '__main__':
config.parse_args()
log.setup('glance')
app = config.load_paste_app()
server = wsgi.Server()
server.start(app, default_port=9292)
server.start(config.load_paste_app, default_port=9292)
server.wait()
except exception.WorkerCreationFailure, e:
fail(2, e)

View File

@ -46,10 +46,8 @@ if __name__ == '__main__':
config.parse_args()
log.setup('glance')
app = config.load_paste_app()
server = wsgi.Server()
server.start(app, default_port=9191)
server.start(config.load_paste_app, default_port=9191)
server.wait()
except RuntimeError, e:
sys.exit("ERROR: %s" % e)

View File

@ -66,6 +66,6 @@ if __name__ == '__main__':
else:
import eventlet
pool = eventlet.greenpool.GreenPool(1000)
scrubber = app.run(pool)
scrubber = app().run(pool)
except RuntimeError, e:
sys.exit("ERROR: %s" % e)

View File

@ -165,7 +165,8 @@ class Server(object):
"""
Run a WSGI server with the given application.
:param application: The application to run in the WSGI server
:param application: A function that can be called with no arguments
that will return the application to run in the WSGI server
:param default_port: Port to bind to if none is specified in conf
"""
def kill_children(*args):
@ -184,7 +185,7 @@ class Server(object):
signal.signal(signal.SIGHUP, signal.SIG_IGN)
self.running = False
self.application = application
self.app_func = application
self.sock = get_socket(default_port)
os.umask(027) # ensure files are created with the correct privileges
@ -193,15 +194,15 @@ class Server(object):
if CONF.workers == 0:
# Useful for profiling, test, debug etc.
self.pool = eventlet.GreenPool(size=self.threads)
self.pool.spawn_n(self._single_run, application, self.sock)
self.pool.spawn_n(self._single_run, self.app_func(), self.sock)
return
self.logger.info(_("Starting %d workers") % CONF.workers)
signal.signal(signal.SIGTERM, kill_children)
signal.signal(signal.SIGINT, kill_children)
signal.signal(signal.SIGHUP, hup)
while len(self.children) < CONF.workers:
self.run_child()
else:
self.logger.info(_("Starting %d workers") % CONF.workers)
signal.signal(signal.SIGTERM, kill_children)
signal.signal(signal.SIGINT, kill_children)
signal.signal(signal.SIGHUP, hup)
while len(self.children) < CONF.workers:
self.run_child()
def wait_on_children(self):
while self.running:
@ -266,7 +267,7 @@ class Server(object):
eventlet.patcher.monkey_patch(all=False, socket=True)
self.pool = eventlet.GreenPool(size=self.threads)
try:
eventlet.wsgi.server(self.sock, self.application,
eventlet.wsgi.server(self.sock, self.app_func(),
log=WritableLogger(self.logger), custom_pool=self.pool)
except socket.error, err:
if err[0] != errno.EINVAL:

View File

@ -70,7 +70,7 @@ class TestClientExceptions(functional.FunctionalTest):
self.port = utils.get_unused_port()
server = wsgi.Server()
self.config(bind_host='127.0.0.1')
server.start(ExceptionTestApp(), self.port)
server.start(ExceptionTestApp, self.port)
self.client = client.BaseClient("127.0.0.1", self.port)
def _do_test_exception(self, path, exc_type):

View File

@ -31,51 +31,53 @@ from glance.tests import utils
eventlet.patcher.monkey_patch(socket=True)
class RedirectTestApp(object):
"""
Test WSGI application which can respond with multiple kinds of HTTP
redirects and is used to verify Glance client redirects.
"""
def __init__(self, name):
def RedirectTestApp(name):
class App(object):
"""
Initialize app with a name and port.
Test WSGI application which can respond with multiple kinds of HTTP
redirects and is used to verify Glance client redirects.
"""
self.name = name
def __init__(self):
"""
Initialize app with a name and port.
"""
self.name = name
@webob.dec.wsgify
def __call__(self, request):
"""
Handles all requests to the application.
"""
base = "http://%s" % request.host
path = request.path_qs
@webob.dec.wsgify
def __call__(self, request):
"""
Handles all requests to the application.
"""
base = "http://%s" % request.host
path = request.path_qs
if path == "/":
return "root"
if path == "/":
return "root"
elif path == "/302":
url = "%s/success" % base
raise webob.exc.HTTPFound(location=url)
elif path == "/302":
url = "%s/success" % base
raise webob.exc.HTTPFound(location=url)
elif path == "/302?with_qs=yes":
url = "%s/success?with_qs=yes" % base
raise webob.exc.HTTPFound(location=url)
elif path == "/302?with_qs=yes":
url = "%s/success?with_qs=yes" % base
raise webob.exc.HTTPFound(location=url)
elif path == "/infinite_302":
raise webob.exc.HTTPFound(location=request.url)
elif path == "/infinite_302":
raise webob.exc.HTTPFound(location=request.url)
elif path.startswith("/redirect-to"):
url = "http://127.0.0.1:%s/success" % path.split("-")[-1]
raise webob.exc.HTTPFound(location=url)
elif path.startswith("/redirect-to"):
url = "http://127.0.0.1:%s/success" % path.split("-")[-1]
raise webob.exc.HTTPFound(location=url)
elif path == "/success":
return "success_from_host_%s" % self.name
elif path == "/success":
return "success_from_host_%s" % self.name
elif path == "/success?with_qs=yes":
return "success_with_qs"
elif path == "/success?with_qs=yes":
return "success_with_qs"
return "fail"
return "fail"
return App
class TestClientRedirects(functional.FunctionalTest):

View File

@ -133,14 +133,7 @@ class TestRespawn(functional.FunctionalTest):
self.api_server.server_control_options += ' --respawn'
self.api_server.default_store = 'shouldnotexist'
# start API server, allowing glance-control to continue running
self.start_with_retry(self.api_server,
'api_port',
1,
expect_launch=False,
expect_exit=False,
expect_confirmation=False,
**self.__dict__.copy())
exitcode, out, err = self.api_server.start(**self.__dict__.copy())
# ensure the service pid has been cached
pid_cached = lambda: os.path.exists(self.api_server.pid_file)