You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by mo...@apache.org on 2022/04/07 03:36:05 UTC

[incubator-doris] branch master updated: [fix] access parallel_flat_hash_map via thread safely methods (#8854)

This is an automated email from the ASF dual-hosted git repository.

morningman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 02be8176c3 [fix] access parallel_flat_hash_map via thread safely methods (#8854)
02be8176c3 is described below

commit 02be8176c3736e57ef61e2756be81d07dca1c196
Author: dataroaring <98...@users.noreply.github.com>
AuthorDate: Thu Apr 7 11:35:59 2022 +0800

    [fix] access parallel_flat_hash_map via thread safely methods (#8854)
    
    Iterator of parallel_flat_hash_map is not thread safely, so
    we should use if_contains instead.
---
 be/src/util/brpc_client_cache.h | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/be/src/util/brpc_client_cache.h b/be/src/util/brpc_client_cache.h
index d0d8b73689..b15a3c6aeb 100644
--- a/be/src/util/brpc_client_cache.h
+++ b/be/src/util/brpc_client_cache.h
@@ -30,9 +30,10 @@
 #include "util/doris_metrics.h"
 
 template <typename T>
-using SubMap = phmap::parallel_flat_hash_map<
+using StubMap = phmap::parallel_flat_hash_map<
         std::string, std::shared_ptr<T>, std::hash<std::string>, std::equal_to<std::string>,
         std::allocator<std::pair<const std::string, std::shared_ptr<T>>>, 8, std::mutex>;
+
 namespace doris {
 
 template <class T>
@@ -63,10 +64,12 @@ public:
     }
 
     inline std::shared_ptr<T> get_client(const std::string& host_port) {
-        auto stub_ptr = _stub_map.find(host_port);
-        if (LIKELY(stub_ptr != _stub_map.end())) {
-            return stub_ptr->second;
+        std::shared_ptr<T> stub_ptr;
+        auto get_value = [&stub_ptr](typename StubMap<T>::mapped_type& v) { stub_ptr = v; };
+        if(LIKELY(_stub_map.if_contains(host_port, get_value))) {
+            return stub_ptr;
         }
+
         // new one stub and insert into map
         brpc::ChannelOptions options;
         if constexpr (std::is_same_v<T, PFunctionService_Stub>) {
@@ -85,7 +88,9 @@ public:
         }
         auto stub = std::make_shared<T>(channel.release(),
                                         google::protobuf::Service::STUB_OWNS_CHANNEL);
-        _stub_map[host_port] = stub;
+        _stub_map.try_emplace_l(host_port,
+                                [&stub](typename StubMap<T>::mapped_type& v) { stub = v; },
+                                stub);
         return stub;
     }
 
@@ -149,7 +154,7 @@ public:
     }
 
 private:
-    SubMap<T> _stub_map;
+    StubMap<T> _stub_map;
 };
 
 using InternalServiceClientCache = BrpcClientCache<PBackendService_Stub>;


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org