github-actions[bot] commented on code in PR #29591:
URL: https://github.com/apache/doris/pull/29591#discussion_r1443960893


##########
be/src/runtime/runtime_query_statistics_mgr.cpp:
##########
@@ -0,0 +1,136 @@
+// 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 "runtime/runtime_query_statistics_mgr.h"
+
+#include "runtime/client_cache.h"
+#include "runtime/exec_env.h"
+#include "util/debug_util.h"
+
+namespace doris {
+
+void RuntimeQueryStatiticsMgr::register_query_statistics(std::string query_id,
+                                                         
std::shared_ptr<QueryStatistics> qs_ptr,
+                                                         TNetworkAddress 
fe_addr) {
+    std::lock_guard<std::shared_mutex> write_lock(_qs_ctx_map_lock);
+    if (_query_statistics_ctx_map.find(query_id) == 
_query_statistics_ctx_map.end()) {
+        _query_statistics_ctx_map[query_id] = 
std::make_unique<QueryStatisticsCtx>(fe_addr);
+    }
+    _query_statistics_ctx_map.at(query_id)->qs_list.push_back(qs_ptr);
+}
+
+void RuntimeQueryStatiticsMgr::report_runtime_query_statistics() {

Review Comment:
   warning: method 'report_runtime_query_statistics' can be made static 
[readability-convert-member-functions-to-static]
   
   be/src/runtime/runtime_query_statistics_mgr.h:45:
   ```diff
   -     void report_runtime_query_statistics();
   +     static void report_runtime_query_statistics();
   ```
   



##########
be/src/runtime/query_context.cpp:
##########
@@ -116,4 +119,20 @@ bool QueryContext::cancel(bool v, std::string msg, Status 
new_status, int fragme
     }
     return true;
 }
+
+void QueryContext::register_query_statistics(std::shared_ptr<QueryStatistics> 
qs) {
+    
_exec_env->runtime_query_statistics_mgr()->register_query_statistics(print_id(_query_id),
 qs,
+                                                                         
coord_addr);
+}
+
+std::shared_ptr<QueryStatistics> QueryContext::dml_query_statistics() {

Review Comment:
   warning: method 'dml_query_statistics' can be made static 
[readability-convert-member-functions-to-static]
   
   be/src/runtime/query_context.h:214:
   ```diff
   -     std::shared_ptr<QueryStatistics> dml_query_statistics();
   +     static std::shared_ptr<QueryStatistics> dml_query_statistics();
   ```
   



##########
be/src/runtime/runtime_query_statistics_mgr.cpp:
##########
@@ -0,0 +1,136 @@
+// 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 "runtime/runtime_query_statistics_mgr.h"
+
+#include "runtime/client_cache.h"
+#include "runtime/exec_env.h"
+#include "util/debug_util.h"
+
+namespace doris {
+
+void RuntimeQueryStatiticsMgr::register_query_statistics(std::string query_id,
+                                                         
std::shared_ptr<QueryStatistics> qs_ptr,
+                                                         TNetworkAddress 
fe_addr) {
+    std::lock_guard<std::shared_mutex> write_lock(_qs_ctx_map_lock);
+    if (_query_statistics_ctx_map.find(query_id) == 
_query_statistics_ctx_map.end()) {
+        _query_statistics_ctx_map[query_id] = 
std::make_unique<QueryStatisticsCtx>(fe_addr);
+    }
+    _query_statistics_ctx_map.at(query_id)->qs_list.push_back(qs_ptr);
+}
+
+void RuntimeQueryStatiticsMgr::report_runtime_query_statistics() {
+    int64_t be_id = ExecEnv::GetInstance()->master_info()->backend_id;
+    // 1 get query statistics map
+    std::map<TNetworkAddress, std::map<std::string, TQueryStatistics>> 
fe_qs_map;
+    std::map<std::string, bool> query_finished;
+    {
+        std::lock_guard<std::shared_mutex> write_lock(_qs_ctx_map_lock);
+        for (auto& [query_id, qs_ctx_ptr] : _query_statistics_ctx_map) {
+            if (fe_qs_map.find(qs_ctx_ptr->fe_addr) == fe_qs_map.end()) {
+                std::map<std::string, TQueryStatistics> tmp_map;
+                fe_qs_map[qs_ctx_ptr->fe_addr] = std::move(tmp_map);
+            }
+
+            QueryStatistics tmp_qs;
+            for (auto& qs_ptr : qs_ctx_ptr->qs_list) {
+                tmp_qs.merge(*qs_ptr);
+            }
+            TQueryStatistics ret_t_qs;
+            tmp_qs.to_thrift(&ret_t_qs);
+            fe_qs_map.at(qs_ctx_ptr->fe_addr)[query_id] = ret_t_qs;
+            query_finished[query_id] = qs_ctx_ptr->is_query_finished;
+        }
+    }
+
+    // 2 report query statistics to fe
+    std::map<TNetworkAddress, bool> rpc_result;
+    for (auto& [addr, qs_map] : fe_qs_map) {
+        rpc_result[addr] = false;
+        // 2.1 get client
+        Status coord_status;
+        FrontendServiceConnection 
coord(ExecEnv::GetInstance()->frontend_client_cache(), addr,
+                                        &coord_status);
+        std::string add_str = PrintThriftNetworkAddress(addr);
+        if (!coord_status.ok()) {
+            std::stringstream ss;
+            LOG(WARNING) << "could not get client " << add_str
+                         << " when report workload runtime stats, reason is "
+                         << coord_status.to_string();
+            continue;
+        }
+
+        // 2.2 send report
+        TReportWorkloadRuntimeStatusParams report_runtime_params;
+        report_runtime_params.__set_backend_id(be_id);
+        report_runtime_params.__set_query_statistics_map(qs_map);
+
+        TReportExecStatusParams params;
+        params.report_workload_runtime_stats = report_runtime_params;
+        // remote it later, just for test
+        for (const auto& [query_id, qs] : 
report_runtime_params.query_statistics_map) {
+            LOG(INFO) << "send rpc " << query_id << ", " << qs.scan_rows;
+        }
+
+        TReportExecStatusResult res;
+        try {
+            coord->reportExecStatus(res, params);
+        } catch (apache::thrift::transport::TTransportException& e) {
+            LOG(WARNING) << "report workload runtime stats to " << add_str
+                         << " failed,  err: " << e.what();
+        }
+        Status rpc_status = Status::create<false>(res.status);
+        if (rpc_status.ok()) {
+            rpc_result[addr] = true;
+        }
+    }
+
+    //  3 when query is finished and (last rpc is send or timeout), remove 
finished query statistics
+    {
+        std::lock_guard<std::shared_mutex> write_lock(_qs_ctx_map_lock);
+        for (auto& [addr, qs_map] : fe_qs_map) {
+            if (rpc_result[addr]) {
+                for (auto& [query_id, qs] : qs_map) {
+                    if (query_finished[query_id]) {
+                        _query_statistics_ctx_map.erase(query_id);
+                    }
+                }
+            }
+        }
+    }
+}
+
+void RuntimeQueryStatiticsMgr::set_query_finished(std::string query_id) {
+    // NOTE: here must be a write lock
+    std::lock_guard<std::shared_mutex> write_lock(_qs_ctx_map_lock);
+    _query_statistics_ctx_map[query_id]->is_query_finished = true;
+}
+
+std::shared_ptr<QueryStatistics> 
RuntimeQueryStatiticsMgr::get_runtime_query_statistics(

Review Comment:
   warning: method 'get_runtime_query_statistics' can be made static 
[readability-convert-member-functions-to-static]
   
   ```suggestion
   static std::shared_ptr<QueryStatistics> 
RuntimeQueryStatiticsMgr::get_runtime_query_statistics(
   ```
   



##########
be/src/runtime/runtime_query_statistics_mgr.cpp:
##########
@@ -0,0 +1,136 @@
+// 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 "runtime/runtime_query_statistics_mgr.h"
+
+#include "runtime/client_cache.h"
+#include "runtime/exec_env.h"
+#include "util/debug_util.h"
+
+namespace doris {
+
+void RuntimeQueryStatiticsMgr::register_query_statistics(std::string query_id,

Review Comment:
   warning: method 'register_query_statistics' can be made static 
[readability-convert-member-functions-to-static]
   
   ```suggestion
   static void RuntimeQueryStatiticsMgr::register_query_statistics(std::string 
query_id,
   ```
   



##########
be/src/runtime/runtime_query_statistics_mgr.cpp:
##########
@@ -0,0 +1,136 @@
+// 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 "runtime/runtime_query_statistics_mgr.h"
+
+#include "runtime/client_cache.h"
+#include "runtime/exec_env.h"
+#include "util/debug_util.h"
+
+namespace doris {
+
+void RuntimeQueryStatiticsMgr::register_query_statistics(std::string query_id,
+                                                         
std::shared_ptr<QueryStatistics> qs_ptr,
+                                                         TNetworkAddress 
fe_addr) {
+    std::lock_guard<std::shared_mutex> write_lock(_qs_ctx_map_lock);
+    if (_query_statistics_ctx_map.find(query_id) == 
_query_statistics_ctx_map.end()) {
+        _query_statistics_ctx_map[query_id] = 
std::make_unique<QueryStatisticsCtx>(fe_addr);
+    }
+    _query_statistics_ctx_map.at(query_id)->qs_list.push_back(qs_ptr);
+}
+
+void RuntimeQueryStatiticsMgr::report_runtime_query_statistics() {
+    int64_t be_id = ExecEnv::GetInstance()->master_info()->backend_id;
+    // 1 get query statistics map
+    std::map<TNetworkAddress, std::map<std::string, TQueryStatistics>> 
fe_qs_map;
+    std::map<std::string, bool> query_finished;
+    {
+        std::lock_guard<std::shared_mutex> write_lock(_qs_ctx_map_lock);
+        for (auto& [query_id, qs_ctx_ptr] : _query_statistics_ctx_map) {
+            if (fe_qs_map.find(qs_ctx_ptr->fe_addr) == fe_qs_map.end()) {
+                std::map<std::string, TQueryStatistics> tmp_map;
+                fe_qs_map[qs_ctx_ptr->fe_addr] = std::move(tmp_map);
+            }
+
+            QueryStatistics tmp_qs;
+            for (auto& qs_ptr : qs_ctx_ptr->qs_list) {
+                tmp_qs.merge(*qs_ptr);
+            }
+            TQueryStatistics ret_t_qs;
+            tmp_qs.to_thrift(&ret_t_qs);
+            fe_qs_map.at(qs_ctx_ptr->fe_addr)[query_id] = ret_t_qs;
+            query_finished[query_id] = qs_ctx_ptr->is_query_finished;
+        }
+    }
+
+    // 2 report query statistics to fe
+    std::map<TNetworkAddress, bool> rpc_result;
+    for (auto& [addr, qs_map] : fe_qs_map) {
+        rpc_result[addr] = false;
+        // 2.1 get client
+        Status coord_status;
+        FrontendServiceConnection 
coord(ExecEnv::GetInstance()->frontend_client_cache(), addr,
+                                        &coord_status);
+        std::string add_str = PrintThriftNetworkAddress(addr);
+        if (!coord_status.ok()) {
+            std::stringstream ss;
+            LOG(WARNING) << "could not get client " << add_str
+                         << " when report workload runtime stats, reason is "
+                         << coord_status.to_string();
+            continue;
+        }
+
+        // 2.2 send report
+        TReportWorkloadRuntimeStatusParams report_runtime_params;
+        report_runtime_params.__set_backend_id(be_id);
+        report_runtime_params.__set_query_statistics_map(qs_map);
+
+        TReportExecStatusParams params;
+        params.report_workload_runtime_stats = report_runtime_params;
+        // remote it later, just for test
+        for (const auto& [query_id, qs] : 
report_runtime_params.query_statistics_map) {
+            LOG(INFO) << "send rpc " << query_id << ", " << qs.scan_rows;
+        }
+
+        TReportExecStatusResult res;
+        try {
+            coord->reportExecStatus(res, params);
+        } catch (apache::thrift::transport::TTransportException& e) {
+            LOG(WARNING) << "report workload runtime stats to " << add_str
+                         << " failed,  err: " << e.what();
+        }
+        Status rpc_status = Status::create<false>(res.status);
+        if (rpc_status.ok()) {
+            rpc_result[addr] = true;
+        }
+    }
+
+    //  3 when query is finished and (last rpc is send or timeout), remove 
finished query statistics
+    {
+        std::lock_guard<std::shared_mutex> write_lock(_qs_ctx_map_lock);
+        for (auto& [addr, qs_map] : fe_qs_map) {
+            if (rpc_result[addr]) {
+                for (auto& [query_id, qs] : qs_map) {
+                    if (query_finished[query_id]) {
+                        _query_statistics_ctx_map.erase(query_id);
+                    }
+                }
+            }
+        }
+    }
+}
+
+void RuntimeQueryStatiticsMgr::set_query_finished(std::string query_id) {

Review Comment:
   warning: method 'set_query_finished' can be made static 
[readability-convert-member-functions-to-static]
   
   ```suggestion
   static void RuntimeQueryStatiticsMgr::set_query_finished(std::string 
query_id) {
   ```
   



-- 
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: commits-unsubscr...@doris.apache.org

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


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

Reply via email to