Loading...
Searching...
No Matches
geo_global_executor.cpp
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
21
22#include <glog/logging.h>
23
24namespace resdb {
25
27 std::unique_ptr<TransactionManager> global_transaction_manager,
28 const ResDBConfig& config)
29 : global_transaction_manager_(std::move(global_transaction_manager)),
30 config_(config),
31 is_stop_(false) {
33 region_size_ = config_.GetConfigData().region().size();
34 order_thread_ = std::thread(&GeoGlobalExecutor::OrderRound, this);
35 my_region_ = config.GetConfigData().self_region_id();
36}
37
39
41 is_stop_ = true;
42 if (order_thread_.joinable()) {
43 order_thread_.join();
44 }
45}
46
47void GeoGlobalExecutor::Execute(std::unique_ptr<Request> request) {
48 BatchUserRequest batch_request;
49 if (!batch_request.ParseFromString(request->data())) {
50 LOG(ERROR) << "[GeoGlobalExecutor] parse data fail!";
51 return;
52 }
53 if (request->data().empty()) {
54 LOG(ERROR) << "[GeoGlobalExecutor] request->data() is empty ";
55 return;
56 }
57
58 if (global_stats_) {
59 global_stats_->IncTotalGeoRequest(batch_request.user_requests_size());
60 }
62 auto batch_response =
63 global_transaction_manager_->ExecuteBatch(batch_request);
64 if (batch_response != nullptr &&
65 request->region_info().region_id() == my_region_) {
66 batch_response->set_createtime(batch_request.createtime());
67 batch_response->set_local_id(batch_request.local_id());
68 batch_response->set_proxy_id(batch_request.proxy_id());
69 batch_response->set_seq(batch_request.seq());
70 resp_queue_.Push(std::move(batch_response));
71 }
72 }
73}
74
75bool GeoGlobalExecutor::IsStop() { return is_stop_; }
76
77int GeoGlobalExecutor::OrderGeoRequest(std::unique_ptr<Request> request) {
78 order_queue_.Push(std::move(request));
79 return 0;
80}
81
82void GeoGlobalExecutor::AddData() {
83 auto request = order_queue_.Pop();
84 if (request == nullptr) {
85 return;
86 }
88 uint64_t seq_num = request->seq();
89 int region_id = request->region_info().region_id();
90 execute_map_[std::make_pair(seq_num, region_id)] = std::move(request);
91}
92
93std::unique_ptr<Request> GeoGlobalExecutor::GetNextMap() {
94 if (execute_map_.empty()) {
95 return nullptr;
96 }
97 // LOG(ERROR) << "Next round seq: " << next_seq_ << ", current First in map: "
98 // << execute_map_.begin()->first << ", size: " <<
99
100 // LOG(ERROR)<<"get next seq:"<<next_seq_<<" region:"<<next_region_;
101 if (execute_map_.begin()->first == std::make_pair(next_seq_, next_region_)) {
102 std::unique_ptr<Request> res = std::move(execute_map_.begin()->second);
103 execute_map_.erase(execute_map_.begin());
104 next_region_++;
105 if (next_region_ > static_cast<int64_t>(region_size_)) {
106 next_region_ = 1;
107 next_seq_++;
108 }
109 return res;
110 }
111 return nullptr;
112}
113
114void GeoGlobalExecutor::OrderRound() {
115 while (!IsStop()) {
116 AddData();
117 while (!IsStop()) {
118 std::unique_ptr<Request> seq_map = GetNextMap();
119 if (seq_map == nullptr) {
120 break;
121 }
122 Execute(std::move(seq_map));
123 }
124 }
125}
126
127std::unique_ptr<BatchUserResponse> GeoGlobalExecutor::GetResponseMsg() {
128 return resp_queue_.Pop();
129}
130
131} // namespace resdb
virtual void Execute(std::unique_ptr< Request > request)
std::unique_ptr< BatchUserResponse > GetResponseMsg()
LockFreeQueue< Request > order_queue_
virtual int OrderGeoRequest(std::unique_ptr< Request > request)
LockFreeQueue< BatchUserResponse > resp_queue_
std::unique_ptr< TransactionManager > global_transaction_manager_
GeoGlobalExecutor(std::unique_ptr< TransactionManager > global_transaction_manager, const ResDBConfig &config)
std::atomic< bool > is_stop_
void Push(std::unique_ptr< T > data)
std::unique_ptr< T > Pop(int timeout_ms=100)
ResConfigData GetConfigData() const
void IncTotalGeoRequest(uint32_t num)
Definition stats.cpp:281
static Stats * GetGlobalStats(int sleep_seconds=5)
Definition stats.cpp:29
void IncGeoRequest()
Definition stats.cpp:283
res
Definition addr.py:25
std::unique_ptr< ResDBConfig > config_