]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
tools: modify connect implement of RO cache
authorshangdehao1 <dehao.shang@intel.com>
Sun, 26 May 2019 22:22:21 +0000 (06:22 +0800)
committerJason Dillaman <dillaman@redhat.com>
Mon, 24 Jun 2019 21:35:40 +0000 (17:35 -0400)
- In order to support re-connect, add async connect at CacheClient.
- sync connect is based on C_SaferCond + async_connect.

Signed-off-by: Dehao Shang <dehao.shang@intel.com>
src/tools/immutable_object_cache/CacheClient.cc
src/tools/immutable_object_cache/CacheClient.h

index 20b2627c59bf42e100c1a913679fd0671f382674..bba3820f48868745c1fe32b87c7c344ade9c5841 100644 (file)
@@ -73,15 +73,39 @@ namespace immutable_obj_cache {
     }
   }
 
+  // sync connect
   int CacheClient::connect() {
-    boost::system::error_code ec;
-    m_dm_socket.connect(m_ep, ec);
-    if (ec) {
-      fault(ASIO_ERROR_CONNECT, ec);
-      return -1;
+    int ret = -1;
+    C_SaferCond cond;
+    Context* on_finish = new FunctionContext([&cond, &ret](int err) {
+      ret = err;
+      cond.complete(err);
+    });
+
+    connect(on_finish);
+    cond.wait();
+
+    return ret;
+  }
+
+  // async connect
+  void CacheClient::connect(Context* on_finish) {
+    m_dm_socket.async_connect(m_ep,
+      boost::bind(&CacheClient::handle_connect, this,
+                  on_finish, boost::asio::placeholders::error));
+  }
+
+  void CacheClient::handle_connect(Context* on_finish,
+                                   const boost::system::error_code& err) {
+    if (err) {
+      ldout(m_cct, 20) << "fails to connect to cache server." << dendl;
+      fault(ASIO_ERROR_CONNECT, err);
+      on_finish->complete(-1);
+      return;
     }
-    ldout(m_cct, 20) <<"connect success"<< dendl;
-    return 0;
+
+    ldout(m_cct, 20) << "successfully connected to cache server." << dendl;
+    on_finish->complete(0);
   }
 
   void CacheClient::lookup_object(std::string pool_nspace, uint64_t pool_id,
index f4003cc518ede2cc583c296833622ec75025a007..d437ed0c9a6a49f15f9f02469cb62a56ebffd583 100644 (file)
@@ -12,6 +12,7 @@
 
 #include "include/ceph_assert.h"
 #include "include/Context.h"
+#include "common/Cond.h"
 #include "common/Mutex.h"
 #include "Types.h"
 #include "SocketCommon.h"
@@ -31,6 +32,7 @@ class CacheClient {
   void close();
   int stop();
   int connect();
+  void connect(Context* on_finish);
   void lookup_object(std::string pool_nspace, uint64_t pool_id,
                      uint64_t snap_id, std::string oid,
                      CacheGenContextURef&& on_finish);
@@ -40,6 +42,7 @@ class CacheClient {
   void send_message();
   void try_send();
   void fault(const int err_type, const boost::system::error_code& err);
+  void handle_connect(Context* on_finish, const boost::system::error_code& err);
   void try_receive();
   void receive_message();
   void process(ObjectCacheRequest* reply, uint64_t seq_id);