]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/cephadm: create thread to start event loop for ssh.py, and return results of...
authorMelissa <li.melissa.kun@gmail.com>
Tue, 20 Jul 2021 23:02:57 +0000 (19:02 -0400)
committerMelissa Li <li.melissa.kun@gmail.com>
Fri, 20 Aug 2021 18:27:45 +0000 (14:27 -0400)
The EventLoopThread class starts a thread and an event loop which runs forever. Coroutines are scheduled on the event loop by the `get_result` method which uses `run_coroutine_threadsafe` to return a concurrent.futures.Future, and ultimately the result with .result()

Fixes: https://tracker.ceph.com/issues/44676
Signed-off-by: Melissa Li <li.melissa.kun@gmail.com>
src/pybind/mgr/cephadm/module.py
src/pybind/mgr/cephadm/ssh.py

index 13d93a23941fefd11a84e6c11d1a459d8b5477b5..09ec70af788998bcb988a0c2076680fa6cceee2e 100644 (file)
@@ -506,6 +506,9 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule,
         A command handler will typically change the declarative state
         of cephadm. This loop will then attempt to apply this new state.
         """
+        # for ssh in serve
+        self.event_loop = ssh.EventLoopThread()
+
         serve = CephadmServe(self)
         serve.serve()
 
index caf8379801191cc30b45f0116e2bdc8c6281f1e4..3751ae1750b5ccb28d5671fe9439420d3877ca97 100644 (file)
@@ -1,6 +1,8 @@
 import logging
 import os
+import asyncio
 from tempfile import NamedTemporaryFile
+from threading import Thread
 from contextlib import contextmanager
 from io import StringIO
 from shlex import quote
@@ -29,6 +31,22 @@ Host *
   ConnectTimeout=30
 """
 
+
+class EventLoopThread(Thread):
+
+    def __init__(self) -> None:
+
+        self._loop = asyncio.new_event_loop()
+        asyncio.set_event_loop(self._loop)
+        self._loop.set_debug(True)
+
+        super().__init__(target=self._loop.run_forever)
+        self.start()
+
+    def get_result(self, coro) -> Any:  # type: ignore
+        return asyncio.run_coroutine_threadsafe(coro, self._loop).result()
+
+
 class SSHManager:
 
     def __init__(self, mgr: "CephadmOrchestrator"):