Browse Source

Support magnum-api multiple process workers

Multiple process workers support for magnum-api.
Adds new option 'workers' to group [api] of magnum.conf.

Change-Id: I0e8327ada6926602d577d1f36d384dd49426c7ee
Implements: blueprint magnum-multiple-process-workers
wangbo 2 years ago
parent
commit
b462a3b8cd
3 changed files with 35 additions and 3 deletions
  1. 7
    1
      magnum/cmd/api.py
  2. 4
    1
      magnum/conf/api.py
  3. 24
    1
      magnum/tests/unit/cmd/test_api.py

+ 7
- 1
magnum/cmd/api.py View File

@@ -17,6 +17,7 @@
17 17
 import os
18 18
 import sys
19 19
 
20
+from oslo_concurrency import processutils
20 21
 from oslo_log import log as logging
21 22
 from oslo_reports import guru_meditation_report as gmr
22 23
 from werkzeug import serving
@@ -75,5 +76,10 @@ def main():
75 76
     LOG.info(_LI('Serving on %(proto)s://%(host)s:%(port)s'),
76 77
              dict(proto="https" if use_ssl else "http", host=host, port=port))
77 78
 
78
-    serving.run_simple(host, port, app,
79
+    workers = CONF.api.workers
80
+    if not workers:
81
+        workers = processutils.get_worker_count()
82
+    LOG.info(_LI('Server will handle each request in a new process up to'
83
+                 ' %s concurrent processes'), workers)
84
+    serving.run_simple(host, port, app, processes=workers,
79 85
                        ssl_context=_get_ssl_configs(use_ssl))

+ 4
- 1
magnum/conf/api.py View File

@@ -39,7 +39,10 @@ api_service_opts = [
39 39
                     "effect. "),
40 40
     cfg.BoolOpt('enabled_ssl',
41 41
                 default=False,
42
-                help='Enable SSL Magnum API service')
42
+                help='Enable SSL Magnum API service'),
43
+    cfg.IntOpt('workers',
44
+               help='The maximum number of magnum-api processes to '
45
+                    'fork and run. Default to number of CPUs on the host.')
43 46
 ]
44 47
 
45 48
 

+ 24
- 1
magnum/tests/unit/cmd/test_api.py View File

@@ -14,6 +14,8 @@
14 14
 
15 15
 import mock
16 16
 
17
+from oslo_concurrency import processutils
18
+
17 19
 from magnum.cmd import api
18 20
 from magnum.tests import base
19 21
 
@@ -29,12 +31,31 @@ class TestMagnumAPI(base.TestCase):
29 31
     def test_api_http(self, mock_prep, mock_app, mock_run, mock_base):
30 32
         api.main()
31 33
 
34
+        app = mock_app.load_app.return_value
35
+        mock_prep.assert_called_once_with(mock.ANY)
36
+        mock_app.load_app.assert_called_once_with()
37
+        workers = processutils.get_worker_count()
38
+        mock_run.assert_called_once_with(base.CONF.api.host,
39
+                                         base.CONF.api.port,
40
+                                         app, processes=workers,
41
+                                         ssl_context=None)
42
+
43
+    @mock.patch('werkzeug.serving.run_simple')
44
+    @mock.patch.object(api, 'api_app')
45
+    @mock.patch('magnum.common.service.prepare_service')
46
+    def test_api_http_config_workers(self, mock_prep, mock_app,
47
+                                     mock_run, mock_base):
48
+        fake_workers = 8
49
+        self.config(workers=fake_workers, group='api')
50
+        api.main()
51
+
32 52
         app = mock_app.load_app.return_value
33 53
         mock_prep.assert_called_once_with(mock.ANY)
34 54
         mock_app.load_app.assert_called_once_with()
35 55
         mock_run.assert_called_once_with(base.CONF.api.host,
36 56
                                          base.CONF.api.port,
37
-                                         app, ssl_context=None)
57
+                                         app, processes=fake_workers,
58
+                                         ssl_context=None)
38 59
 
39 60
     @mock.patch('os.path.exists')
40 61
     @mock.patch('werkzeug.serving.run_simple')
@@ -91,6 +112,8 @@ class TestMagnumAPI(base.TestCase):
91 112
         mock_app.load_app.assert_called_once_with()
92 113
         mock_exist.assert_has_calls([mock.call('tmp_crt'),
93 114
                                      mock.call('tmp_key')])
115
+        workers = processutils.get_worker_count()
94 116
         mock_run.assert_called_once_with(base.CONF.api.host,
95 117
                                          base.CONF.api.port, app,
118
+                                         processes=workers,
96 119
                                          ssl_context=('tmp_crt', 'tmp_key'))

Loading…
Cancel
Save