Change in vdsm[master]: jsonrpc: executor based thread factory

piotr.kliczewski at gmail.com piotr.kliczewski at gmail.com
Fri Jul 17 08:20:28 UTC 2015


Piotr Kliczewski has uploaded a new change for review.

Change subject: jsonrpc: executor based thread factory
......................................................................

jsonrpc: executor based thread factory

Creating new thread for every request is not efficient so we introduce
usage of the executor for request processing.


Change-Id: I56b307633a8bf7e4aad8f87cc97a4129c9ed0970
Signed-off-by: pkliczewski <piotr.kliczewski at gmail.com>
---
M lib/vdsm/config.py.in
M vdsm/rpc/bindingjsonrpc.py
2 files changed, 49 insertions(+), 5 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/59/43759/1

diff --git a/lib/vdsm/config.py.in b/lib/vdsm/config.py.in
index 3221aab..bffa949 100644
--- a/lib/vdsm/config.py.in
+++ b/lib/vdsm/config.py.in
@@ -201,6 +201,12 @@
 
         ('connection_stats_timeout', '3600',
             'Time in seconds defining how frequently we log transport stats'),
+
+        ('worker_threads', '8',
+            'Number of worker threads to serve jsonrpc server.'),
+
+        ('tasks_per_worker', '100',
+            'Max number of tasks which can be queued per workers.'),
     ]),
 
     # Section: [mom]
diff --git a/vdsm/rpc/bindingjsonrpc.py b/vdsm/rpc/bindingjsonrpc.py
index 80e3388..ca15fde 100644
--- a/vdsm/rpc/bindingjsonrpc.py
+++ b/vdsm/rpc/bindingjsonrpc.py
@@ -19,18 +19,53 @@
 from yajsonrpc import JsonRpcServer
 from yajsonrpc.stompreactor import StompReactor
 
+from vdsm import executor
+from vdsm import schedule
+from vdsm.config import config
+from vdsm.utils import monotonic_time
 
-def _simpleThreadFactory(func):
-    t = threading.Thread(target=func)
-    t.setDaemon(False)
-    t.start()
+
+# TODO test what should be the default values
+_THREADS = config.getint('vars', 'worker_threads')
+_TASK_PER_WORKER = config.getint('vars', 'tasks_per_worker')
+_TASKS = _THREADS * _TASK_PER_WORKER
+
+
+_scheduler = schedule.Scheduler(name="jsonrpc.Scheduler",
+                                clock=monotonic_time)
+
+_executor = executor.Executor(name="jsonrpc.Executor",
+                              workers_count=_THREADS,
+                              max_tasks=_TASKS,
+                              scheduler=_scheduler)
+
+
+class _RequestWorker(object):
+
+    _log = logging.getLogger("jsonrpc._RequestWorker")
+
+    def __init__(self, func):
+        self._func = func
+
+    def __call__(self):
+        try:
+            self._func()
+        except Exception:
+            self._log.exception("%s processing request failed", self._func)
+
+
+def _executorFactory(func):
+    _executor.dispatch(_RequestWorker(func))
 
 
 class BindingJsonRpc(object):
     log = logging.getLogger('BindingJsonRpc')
 
     def __init__(self, bridge, subs, timeout):
-        self._server = JsonRpcServer(bridge, timeout, _simpleThreadFactory)
+        _scheduler.start()
+        _executor.start()
+
+        self._server = JsonRpcServer(bridge, timeout, _executorFactory)
         self._reactor = StompReactor(subs)
         self.startReactor()
 
@@ -60,3 +95,6 @@
     def stop(self):
         self._server.stop()
         self._reactor.stop()
+
+        _scheduler.stop()
+        _executor.stop()


-- 
To view, visit https://gerrit.ovirt.org/43759
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I56b307633a8bf7e4aad8f87cc97a4129c9ed0970
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Piotr Kliczewski <piotr.kliczewski at gmail.com>


More information about the vdsm-patches mailing list