From 5bc5b5dff2ec4059df9345e65883ca69eb8392a8 Mon Sep 17 00:00:00 2001 From: shangdehao1 Date: Fri, 19 Apr 2019 06:29:58 +0800 Subject: [PATCH] librbd: introduce new context type at RO - CacheGenContextURef is wrapper GenContextURef - use make_gen_lambda_context to create context. - smart pointer unique_ptr to auto release object. Signed-off-by: Dehao Shang --- src/librbd/cache/SharedReadOnlyObjectDispatch.cc | 8 ++++---- src/test/immutable_object_cache/MockCacheDaemon.h | 10 +++++++--- .../immutable_object_cache/test_DomainSocket.cc | 14 ++++++++------ .../immutable_object_cache/test_multi_session.cc | 6 +++--- .../librbd/cache/test_mock_ParentImageCache.cc | 10 +++++----- src/tools/immutable_object_cache/CacheClient.cc | 8 ++++---- src/tools/immutable_object_cache/CacheClient.h | 2 +- src/tools/immutable_object_cache/SocketCommon.h | 2 ++ src/tools/immutable_object_cache/Types.h | 2 +- 9 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/librbd/cache/SharedReadOnlyObjectDispatch.cc b/src/librbd/cache/SharedReadOnlyObjectDispatch.cc index d7887192b3d4..c4b6ca6c5bf8 100644 --- a/src/librbd/cache/SharedReadOnlyObjectDispatch.cc +++ b/src/librbd/cache/SharedReadOnlyObjectDispatch.cc @@ -98,10 +98,10 @@ bool SharedReadOnlyObjectDispatch::read( return true; } - auto ctx = new LambdaGenContext, - ObjectCacheRequest*>([this, snap_id, read_data, dispatch_result, on_dispatched, + CacheGenContextURef ctx = make_gen_lambda_context> + ([this, snap_id, read_data, dispatch_result, on_dispatched, oid, object_off, object_len](ObjectCacheRequest* ack) { - if (ack->type == RBDSC_READ_REPLY) { std::string file_path = ((ObjectCacheReadReplyData*)ack)->cache_path; ceph_assert(file_path != ""); @@ -118,7 +118,7 @@ bool SharedReadOnlyObjectDispatch::read( m_cache_client->lookup_object(m_image_ctx->data_ctx.get_namespace(), m_image_ctx->data_ctx.get_id(), - (uint64_t)snap_id, oid, ctx); + (uint64_t)snap_id, oid, std::move(ctx)); } return true; } diff --git a/src/test/immutable_object_cache/MockCacheDaemon.h b/src/test/immutable_object_cache/MockCacheDaemon.h index b5c934e24480..c189dd127de4 100644 --- a/src/test/immutable_object_cache/MockCacheDaemon.h +++ b/src/test/immutable_object_cache/MockCacheDaemon.h @@ -23,10 +23,14 @@ class MockCacheClient { MOCK_METHOD0(close, void()); MOCK_METHOD0(stop, void()); MOCK_METHOD0(connect, int()); - MOCK_METHOD5(lookup_object, void(std::string pool_nspace, + void lookup_object(std::string pool_nspace, uint64_t pool_id, uint64_t snap_id, + std::string oid, CacheGenContextURef&& on_finish) { + // gmock don't support move + internal_lookup(pool_nspace, pool_id, snap_id, oid); + }; + MOCK_METHOD4(internal_lookup, void(std::string pool_nspace, uint64_t pool_id, uint64_t snap_id, - std::string oid, - GenContext* on_finish)); + std::string oid)); MOCK_METHOD1(register_client, int(Context*)); }; diff --git a/src/test/immutable_object_cache/test_DomainSocket.cc b/src/test/immutable_object_cache/test_DomainSocket.cc index dd9ad3bfd53e..89a0fa6c4493 100644 --- a/src/test/immutable_object_cache/test_DomainSocket.cc +++ b/src/test/immutable_object_cache/test_DomainSocket.cc @@ -106,8 +106,8 @@ public: m_send_request_index.store(0); m_recv_ack_index.store(0); for (uint64_t index = 0; index < times; index++) { - auto ctx = new LambdaGenContext, - ObjectCacheRequest*>([this, thinking, times](ObjectCacheRequest* ack){ + auto ctx = make_gen_lambda_context> + ([this, thinking, times](ObjectCacheRequest* ack){ if (thinking != 0) { usleep(thinking); // handling message } @@ -122,7 +122,7 @@ public: usleep(1); } - m_cache_client->lookup_object("pool_nspace", 1, 2, "object_name", ctx); + m_cache_client->lookup_object("pool_nspace", 1, 2, "object_name", std::move(ctx)); m_send_request_index++; } m_wait_event.wait(); @@ -130,12 +130,14 @@ public: bool startup_lookupobject_testing(std::string pool_nspace, std::string object_id) { bool hit; - auto ctx = new LambdaGenContext, - ObjectCacheRequest*>([this, &hit](ObjectCacheRequest* ack){ + //auto ctx = new LambdaGenContext, + // ObjectCacheRequest*>([this, &hit](ObjectCacheRequest* ack){ + auto ctx = make_gen_lambda_context> + ([this, &hit](ObjectCacheRequest* ack){ hit = ack->type == RBDSC_READ_REPLY; m_wait_event.signal(); }); - m_cache_client->lookup_object(pool_nspace, 1, 2, object_id, ctx); + m_cache_client->lookup_object(pool_nspace, 1, 2, object_id, std::move(ctx)); m_wait_event.wait(); return hit; } diff --git a/src/test/immutable_object_cache/test_multi_session.cc b/src/test/immutable_object_cache/test_multi_session.cc index cdbb17fb16b6..f00e5149ef65 100644 --- a/src/test/immutable_object_cache/test_multi_session.cc +++ b/src/test/immutable_object_cache/test_multi_session.cc @@ -120,13 +120,13 @@ public: uint64_t request_num, bool is_last) { for (uint64_t i = 0; i < request_num; i++) { - auto ctx = new LambdaGenContext, - ObjectCacheRequest*>([this](ObjectCacheRequest* ack) { + auto ctx = make_gen_lambda_context>([this](ObjectCacheRequest* ack) { m_recv_ack_index++; }); m_send_request_index++; // here just for concurrently testing register + lookup, so fix object id. - m_cache_client_vec[index]->lookup_object(pool_nspace, 1, 2, "1234", ctx); + m_cache_client_vec[index]->lookup_object(pool_nspace, 1, 2, "1234", std::move(ctx)); } if (is_last) { diff --git a/src/test/librbd/cache/test_mock_ParentImageCache.cc b/src/test/librbd/cache/test_mock_ParentImageCache.cc index 1566bc5a1544..f26383cabafa 100644 --- a/src/test/librbd/cache/test_mock_ParentImageCache.cc +++ b/src/test/librbd/cache/test_mock_ParentImageCache.cc @@ -78,12 +78,12 @@ class TestMockParentImageCache : public TestMockFixture { void expect_cache_lookup_object(MockParentImageCache& mparent_image_cache, Context* on_finish) { - auto& expect = EXPECT_CALL(*(mparent_image_cache.m_cache_client), lookup_object( - _, _, _, _, _)); + auto& expect = EXPECT_CALL(*(mparent_image_cache.m_cache_client), + internal_lookup(_, _, _, _)); - expect.WillOnce(WithArg<4>(Invoke([on_finish](GenContext* ctx) { - on_finish->complete(0); - }))); + expect.WillOnce(WithArg<3>(Invoke([on_finish](std::string oid) { + on_finish->complete(0); + }))); } void expect_cache_close(MockParentImageCache& mparent_image_cache, int ret_val) { diff --git a/src/tools/immutable_object_cache/CacheClient.cc b/src/tools/immutable_object_cache/CacheClient.cc index 02606b81e0cf..fa533029efa9 100644 --- a/src/tools/immutable_object_cache/CacheClient.cc +++ b/src/tools/immutable_object_cache/CacheClient.cc @@ -86,11 +86,11 @@ namespace immutable_obj_cache { void CacheClient::lookup_object(std::string pool_nspace, uint64_t pool_id, uint64_t snap_id, std::string oid, - GenContext* on_finish) { + CacheGenContextURef&& on_finish) { ObjectCacheRequest* req = new ObjectCacheReadData(RBDSC_READ, ++m_sequence_id, 0, 0, pool_id, snap_id, oid, pool_nspace); - req->process_msg = on_finish; + req->process_msg = std::move(on_finish); req->encode(); { @@ -250,8 +250,8 @@ namespace immutable_obj_cache { // dedicated thrad to execute this context. } current_request->process_msg->complete(reply); - delete current_request; - delete reply; + //delete current_request; + //delete reply; }); if (m_worker_thread_num != 0) { diff --git a/src/tools/immutable_object_cache/CacheClient.h b/src/tools/immutable_object_cache/CacheClient.h index 54a35dec761b..f4003cc518ed 100644 --- a/src/tools/immutable_object_cache/CacheClient.h +++ b/src/tools/immutable_object_cache/CacheClient.h @@ -33,7 +33,7 @@ class CacheClient { int connect(); void lookup_object(std::string pool_nspace, uint64_t pool_id, uint64_t snap_id, std::string oid, - GenContext* on_finish); + CacheGenContextURef&& on_finish); int register_client(Context* on_finish); private: diff --git a/src/tools/immutable_object_cache/SocketCommon.h b/src/tools/immutable_object_cache/SocketCommon.h index 341550a5ff30..99acf3609041 100644 --- a/src/tools/immutable_object_cache/SocketCommon.h +++ b/src/tools/immutable_object_cache/SocketCommon.h @@ -22,6 +22,8 @@ static const int ASIO_ERROR_MSG_INCOMPLETE = 0X05; class ObjectCacheRequest; class CacheSession; +typedef GenContextURef CacheGenContextURef; + typedef std::function ProcessMsg; } // namespace immutable_obj_cache diff --git a/src/tools/immutable_object_cache/Types.h b/src/tools/immutable_object_cache/Types.h index d08cdfedcf8e..30324d5e898d 100644 --- a/src/tools/immutable_object_cache/Types.h +++ b/src/tools/immutable_object_cache/Types.h @@ -35,7 +35,7 @@ class ObjectCacheRequest { bufferlist payload; - GenContext* process_msg; + CacheGenContextURef process_msg; ObjectCacheRequest(); ObjectCacheRequest(uint16_t type, uint64_t seq); -- 2.47.3