From fb7548c7203fd69e5367b533e110ac4f31a4a1ae Mon Sep 17 00:00:00 2001 From: shangdehao1 Date: Mon, 27 May 2019 06:22:21 +0800 Subject: [PATCH] tools: modify connect implement of RO cache - 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 --- .../immutable_object_cache/CacheClient.cc | 38 +++++++++++++++---- .../immutable_object_cache/CacheClient.h | 3 ++ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/tools/immutable_object_cache/CacheClient.cc b/src/tools/immutable_object_cache/CacheClient.cc index 20b2627c59b..bba3820f488 100644 --- a/src/tools/immutable_object_cache/CacheClient.cc +++ b/src/tools/immutable_object_cache/CacheClient.cc @@ -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, diff --git a/src/tools/immutable_object_cache/CacheClient.h b/src/tools/immutable_object_cache/CacheClient.h index f4003cc518e..d437ed0c9a6 100644 --- a/src/tools/immutable_object_cache/CacheClient.h +++ b/src/tools/immutable_object_cache/CacheClient.h @@ -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); -- 2.39.5