wwbmmm commented on code in PR #2215:
URL: https://github.com/apache/brpc/pull/2215#discussion_r1195028211


##########
src/brpc/usercode_thread_pool.h:
##########
@@ -0,0 +1,142 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#ifndef BRPC_USERCODE_THREAD_POOL_H
+#define BRPC_USERCODE_THREAD_POOL_H
+
+#include <deque>
+#include <mutex>
+#include <functional>
+#include <thread>
+#include <condition_variable>
+#include <gflags/gflags_declare.h>
+#include "butil/atomicops.h"
+#include "bvar/bvar.h"
+#include "bthread/task_meta.h"
+#include "butil/containers/case_ignored_flat_map.h"  // [CaseIgnored]FlatMap
+
+namespace brpc {
+// Store pending user code.
+struct UserCodeTask {
+    void (*fn)(void*);
+    void* arg;
+    void* assigned_data;
+};
+
+class UserCodeThreadAssignPolicy {
+public:
+    UserCodeThreadAssignPolicy() {}
+    virtual ~UserCodeThreadAssignPolicy() {}
+    virtual size_t Index(void* arg, size_t range) = 0;
+
+private:
+    DISALLOW_COPY_AND_ASSIGN(UserCodeThreadAssignPolicy);
+};
+
+class UserCodeThreadRandomAssignPolicy : public UserCodeThreadAssignPolicy {
+public:
+    UserCodeThreadRandomAssignPolicy() {}
+    virtual ~UserCodeThreadRandomAssignPolicy() {}
+    size_t Index(void* arg, size_t range) override;
+
+private:
+    DISALLOW_COPY_AND_ASSIGN(UserCodeThreadRandomAssignPolicy);
+};
+
+class UserCodeThreadPool;
+class UserCodeThreadWorker {
+public:
+    UserCodeThreadWorker(UserCodeThreadPool* pool);
+    void UserCodeRun(UserCodeTask&& usercode);
+    void UserCodeLoop();
+    void Start();
+    void Stop();
+    void Join();
+
+private:
+    UserCodeThreadPool* _pool;
+    std::deque<UserCodeTask> _queue;
+    std::mutex _mutex;
+    std::condition_variable _cond;
+    std::thread _worker;
+    std::atomic<bool> _running;  // running flag
+};
+// "user code thread pool" configuration

Review Comment:
   加个空行



##########
src/brpc/usercode_thread_pool.cpp:
##########
@@ -0,0 +1,230 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "brpc/usercode_thread_pool.h"
+#include <deque>
+#include <vector>
+#include <gflags/gflags.h>
+#include "butil/threading/platform_thread.h"
+
+namespace bthread {
+// Bthread local storage
+extern __thread bthread::LocalStorage tls_bls;
+// Defined in bthread/task_control.cpp
+void run_worker_startfn();
+}  // namespace bthread
+
+namespace brpc {
+
+DEFINE_int32(usercode_thread_pool_map_nbucket, 64 * 2,
+             "usercode thread pool map bucket size");
+
+static void* UserCodeRunner(void* args) {
+    static_cast<UserCodeThreadWorker*>(args)->UserCodeLoop();
+    return NULL;
+}
+
+UserCodeThreadWorker::UserCodeThreadWorker(UserCodeThreadPool* pool)
+    : _pool(pool), _running(true) {}
+
+// Entry of backup thread for running user code.
+void UserCodeThreadWorker::UserCodeLoop() {
+    auto pool_name = _pool->pool_name();
+    auto worker_id = _pool->NextWorkerId();
+    std::string thread_name =
+        butil::string_printf("usercode_%s:%ld", pool_name.c_str(), worker_id);
+    butil::PlatformThread::SetName(thread_name.c_str());
+    auto startfn = _pool->thread_startfn();
+    if (startfn) {
+        startfn();
+    } else {
+        bthread::run_worker_startfn();
+    }
+
+    VLOG(1) << "start thread " << thread_name;
+
+    int64_t last_time = butil::cpuwide_time_us();
+    while (true) {
+        bool blocked = false;
+        std::deque<UserCodeTask> usercodes;
+        {
+            std::unique_lock<std::mutex> lk(_mutex);
+            _cond.wait(lk, [&]() {
+                if (!_queue.empty() ||
+                    !_running.load(std::memory_order_relaxed)) {
+                    return true;
+                } else {
+                    blocked = true;
+                    return false;
+                }
+            });
+            if (!_running.load(std::memory_order_relaxed)) {
+                break;
+            }
+            usercodes = std::move(_queue);
+            _queue = {};
+        }
+        const int64_t begin_time =
+            (blocked ? butil::cpuwide_time_us() : last_time);
+        for (auto& usercode : usercodes) {
+            bthread::tls_bls.assigned_data = usercode.assigned_data;
+            usercode.fn(usercode.arg);
+        }
+        const int64_t end_time = butil::cpuwide_time_us();
+        _pool->inpool_count << usercodes.size();
+        _pool->inpool_elapse_us << (end_time - begin_time);
+        last_time = end_time;
+    }
+
+    VLOG(1) << "exit thread " << thread_name;
+}
+
+void UserCodeThreadWorker::UserCodeRun(UserCodeTask&& usercode) {
+    std::unique_lock<std::mutex> lk(_mutex);
+    _queue.emplace_back(std::move(usercode));
+    _cond.notify_one();
+}
+
+void UserCodeThreadWorker::Start() {
+    _worker = std::thread(UserCodeRunner, this);
+}
+
+void UserCodeThreadWorker::Stop() {
+    _running.store(false, std::memory_order_relaxed);
+    std::unique_lock<std::mutex> lk(_mutex);
+    _cond.notify_one();
+}
+
+void UserCodeThreadWorker::Join() {
+    if (_worker.joinable()) {
+        _worker.join();
+    }
+}
+
+double UserCodeThreadPool::GetInPoolElapseInSecond(void* arg) {
+    return static_cast<bvar::Adder<int64_t>*>(arg)->get_value() / 1000000.0;
+}
+
+size_t UserCodeThreadPool::GetUserCodeThreadSize(void* arg) {
+    auto pool = static_cast<UserCodeThreadPool*>(arg);
+    return pool->_workers.size();
+}
+
+UserCodeThreadPool::UserCodeThreadPool(const std::string& pool_name,
+                                       const std::function<void()>& startfn,
+                                       UserCodeThreadAssignPolicy* policy)
+    : inpool_per_second("rpc_usercode_thread_pool_second", pool_name,
+                        &inpool_count),
+      inpool_elapse_s(GetInPoolElapseInSecond, &inpool_elapse_us),
+      pool_usage("rpc_usercode_thread_pool_usage", pool_name, &inpool_elapse_s,
+                 1),
+      thread_count("rpc_usercode_thread_num_threads", pool_name,
+                   GetUserCodeThreadSize, this),
+      _pool_name(pool_name),
+      _thread_startfn(startfn),
+      _assign_policy(policy),
+      _next_worker_id(0) {}
+
+UserCodeThreadPool::~UserCodeThreadPool() { StopAndJoin(); }
+
+bool UserCodeThreadPool::Init(size_t num_threads) {
+    if (num_threads <= 0) {
+        LOG(ERROR) << "Wrong parameter for usercode thread pool init";
+        return false;
+    }
+    SetNumThreads(num_threads);
+    return true;
+}
+
+void UserCodeThreadPool::RunUserCode(void (*fn)(void*), void* arg) {
+    auto range = GetNumThreads();
+    auto index = _assign_policy->Index(arg, range);
+    auto& worker = _workers[index % range];
+    UserCodeTask usercode{fn, arg, bthread::tls_bls.assigned_data};
+    worker->UserCodeRun(std::move(usercode));
+}
+
+void UserCodeThreadPool::StopAndJoin() {
+    std::unique_lock<std::mutex> lk(_mutex);
+    for (auto& worker : _workers) {
+        worker->Stop();
+    }
+    for (auto& worker : _workers) {
+        worker->Join();
+    }
+}
+
+void UserCodeThreadPool::SetNumThreads(size_t num_threads) {
+    std::unique_lock<std::mutex> lk(_mutex);
+    if (num_threads <= _workers.size()) {
+        return;
+    }
+    for (size_t i = _workers.size(); i < num_threads; ++i) {
+        auto worker = new UserCodeThreadWorker(this);
+        worker->Start();
+        _workers.emplace_back(worker);
+    }
+}
+
+size_t UserCodeThreadPool::NextWorkerId() {
+    return _next_worker_id.fetch_add(1, butil::memory_order_relaxed);
+}
+
+size_t UserCodeThreadPool::GetNumThreads() {
+    std::unique_lock<std::mutex> lk(_mutex);
+    return _workers.size();
+}
+
+size_t UserCodeThreadRandomAssignPolicy::Index(void*, size_t range) {
+    return butil::fast_rand() % range;
+}
+
+UserCodeThreadPoolMap UserCodeThreadPool::_thread_pool_map;
+std::once_flag UserCodeThreadPool::_thread_pool_map_once;
+
+UserCodeThreadPool* UserCodeThreadPool::GetOrCreatePool(
+    const UserCodeThreadPoolConf& conf) {
+    std::call_once(_thread_pool_map_once, [&]() {
+        if (_thread_pool_map.init(FLAGS_usercode_thread_pool_map_nbucket) !=
+            0) {
+            LOG(ERROR) << "Fail to init usercode thread pool map";
+            exit(1);
+        }
+    });
+
+    auto p = _thread_pool_map.seek(conf.pool_name);
+    if (p == nullptr) {
+        std::unique_ptr<UserCodeThreadPool> pool(new UserCodeThreadPool(
+            conf.pool_name, conf.thread_startfn, conf.assign_policy));
+        if (pool->Init(conf.num_threads) == false) {
+            return nullptr;
+        }
+        _thread_pool_map[conf.pool_name].swap(pool);
+    }
+    return _thread_pool_map.seek(conf.pool_name)->get();
+}
+
+void UserCodeThreadPool::SetPoolThreads(const std::string& pool_name,

Review Comment:
   这个方法是不是有个返回值比较好,比如pool_name找不到,num_threads值范围不对,可以返回一个错误



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org
For additional commands, e-mail: dev-h...@brpc.apache.org

Reply via email to