Loading...
Searching...
No Matches
stats.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
24#include "common/utils/utils.h"
25
26namespace resdb {
27
28std::mutex g_mutex;
30 std::unique_lock<std::mutex> lk(g_mutex);
31 static Stats stats(seconds);
32 return &stats;
33}
34
35Stats::Stats(int sleep_time) {
36 monitor_sleep_time_ = sleep_time;
37#ifdef TEST_MODE
38 monitor_sleep_time_ = 1;
39#endif
40 num_call_ = 0;
41 num_commit_ = 0;
42 run_time_ = 0;
43 run_call_ = 0;
44 run_call_time_ = 0;
45 server_call_ = 0;
46 server_process_ = 0;
47 run_req_num_ = 0;
48 run_req_run_time_ = 0;
49 seq_gap_ = 0;
50 total_request_ = 0;
51 total_geo_request_ = 0;
52 geo_request_ = 0;
53
54 stop_ = false;
55 begin_ = false;
56
57 socket_recv_ = 0;
58 broad_cast_msg_ = 0;
59 send_broad_cast_msg_ = 0;
60
61 prometheus_ = nullptr;
62
63 global_thread_ =
64 std::thread(&Stats::MonitorGlobal, this); // pass by reference
65}
66
67void Stats::Stop() { stop_ = true; }
68
70 stop_ = true;
71 if (global_thread_.joinable()) {
72 global_thread_.join();
73 }
74}
75
77 LOG(ERROR) << "monitor:" << name_ << " sleep time:" << monitor_sleep_time_;
78
79 uint64_t seq_fail = 0;
80 uint64_t client_call = 0, socket_recv = 0;
81 uint64_t num_client_req = 0, num_propose = 0, num_prepare = 0, num_commit = 0,
82 pending_execute = 0, execute = 0, execute_done = 0;
83 uint64_t broad_cast_msg = 0, send_broad_cast_msg = 0;
84 uint64_t send_broad_cast_msg_per_rep = 0;
85 uint64_t server_call = 0, server_process = 0;
86 uint64_t seq_gap = 0;
87 uint64_t total_request = 0, total_geo_request = 0, geo_request = 0;
88
89 // ====== for client proxy ======
90 uint64_t run_req_num = 0, run_req_run_time = 0;
91
92 uint64_t last_run_req_num = 0, last_run_req_run_time = 0;
93 // =============================
94
95 uint64_t last_seq_fail = 0;
96 uint64_t last_num_client_req = 0, last_num_propose = 0, last_num_prepare = 0,
97 last_num_commit = 0;
98 uint64_t last_pending_execute = 0, last_execute = 0, last_execute_done = 0;
99 uint64_t last_client_call = 0, last_socket_recv = 0;
100 uint64_t last_broad_cast_msg = 0, last_send_broad_cast_msg = 0;
101 uint64_t last_send_broad_cast_msg_per_rep = 0;
102 uint64_t last_server_call = 0, last_server_process = 0;
103 uint64_t last_total_request = 0, last_total_geo_request = 0,
104 last_geo_request = 0;
105 uint64_t time = 0;
106
107 while (!stop_) {
108 sleep(monitor_sleep_time_);
109 time += monitor_sleep_time_;
110 seq_fail = seq_fail_;
111 socket_recv = socket_recv_;
112 client_call = client_call_;
113 num_client_req = num_client_req_;
114 num_propose = num_propose_;
115 num_prepare = num_prepare_;
116 num_commit = num_commit_;
117 pending_execute = pending_execute_;
118 execute = execute_;
119 execute_done = execute_done_;
120 broad_cast_msg = broad_cast_msg_;
121 send_broad_cast_msg = send_broad_cast_msg_;
122 send_broad_cast_msg_per_rep = send_broad_cast_msg_per_rep_;
123 server_call = server_call_;
124 server_process = server_process_;
125 seq_gap = seq_gap_;
126 total_request = total_request_;
127 total_geo_request = total_geo_request_;
128 geo_request = geo_request_;
129
130 run_req_num = run_req_num_;
131 run_req_run_time = run_req_run_time_;
132
133 LOG(ERROR) << "=========== monitor =========\n"
134 << "server call:" << server_call - last_server_call
135 << " server process:" << server_process - last_server_process
136 << " socket recv:" << socket_recv - last_socket_recv
137 << " "
138 "client call:"
139 << client_call - last_client_call
140 << " "
141 "client req:"
142 << num_client_req - last_num_client_req
143 << " "
144 "broad_cast:"
145 << broad_cast_msg - last_broad_cast_msg
146 << " "
147 "send broad_cast:"
148 << send_broad_cast_msg - last_send_broad_cast_msg
149 << " "
150 "per send broad_cast:"
151 << send_broad_cast_msg_per_rep - last_send_broad_cast_msg_per_rep
152 << " "
153 "propose:"
154 << num_propose - last_num_propose
155 << " "
156 "prepare:"
157 << (num_prepare - last_num_prepare)
158 << " "
159 "commit:"
160 << (num_commit - last_num_commit)
161 << " "
162 "pending execute:"
163 << pending_execute - last_pending_execute
164 << " "
165 "execute:"
166 << execute - last_execute
167 << " "
168 "execute done:"
169 << execute_done - last_execute_done << " seq gap:" << seq_gap
170 << " total request:" << total_request - last_total_request
171 << " txn:" << (total_request - last_total_request) / 5
172 << " total geo request:"
173 << total_geo_request - last_total_geo_request
174 << " total geo request per:"
175 << (total_geo_request - last_total_geo_request) / 5
176 << " geo request:" << (geo_request - last_geo_request)
177 << " "
178 "seq fail:"
179 << seq_fail - last_seq_fail << " time:" << time
180 << " "
181 "\n--------------- monitor ------------";
182 if (run_req_num - last_run_req_num > 0) {
183 LOG(ERROR) << " req client latency:"
184 << static_cast<double>(run_req_run_time -
185 last_run_req_run_time) /
186 (run_req_num - last_run_req_num) / 1000000000.0;
187 }
188
189 last_seq_fail = seq_fail;
190 last_socket_recv = socket_recv;
191 last_client_call = client_call;
192 last_num_client_req = num_client_req;
193 last_num_propose = num_propose;
194 last_num_prepare = num_prepare;
195 last_num_commit = num_commit;
196 last_pending_execute = pending_execute;
197 last_execute = execute;
198 last_execute_done = execute_done;
199
200 last_broad_cast_msg = broad_cast_msg;
201 last_send_broad_cast_msg = send_broad_cast_msg;
202 last_send_broad_cast_msg_per_rep = send_broad_cast_msg_per_rep;
203
204 last_server_call = server_call;
205 last_server_process = server_process;
206
207 last_run_req_num = run_req_num;
208 last_run_req_run_time = run_req_run_time;
209 last_total_request = total_request;
210 last_total_geo_request = total_geo_request;
211 last_geo_request = geo_request;
212 }
213}
214
216 if (prometheus_) {
217 prometheus_->Inc(CLIENT_CALL, 1);
218 }
219 client_call_++;
220}
221
223 if (prometheus_) {
224 prometheus_->Inc(CLIENT_REQ, 1);
225 }
226 num_client_req_++;
227}
228
230 if (prometheus_) {
231 prometheus_->Inc(PROPOSE, 1);
232 }
233 num_propose_++;
234}
235
237 if (prometheus_) {
238 prometheus_->Inc(PREPARE, 1);
239 }
240 num_prepare_++;
241}
242
244 if (prometheus_) {
245 prometheus_->Inc(COMMIT, 1);
246 }
247 num_commit_++;
248}
249
250void Stats::IncPendingExecute() { pending_execute_++; }
251
252void Stats::IncExecute() { execute_++; }
253
255 if (prometheus_) {
256 prometheus_->Inc(EXECUTE, 1);
257 }
258 execute_done_++;
259}
260
262 if (prometheus_) {
263 prometheus_->Inc(BROAD_CAST, 1);
264 }
265 broad_cast_msg_++;
266}
267
268void Stats::SendBroadCastMsg(uint32_t num) { send_broad_cast_msg_ += num; }
269
270void Stats::SendBroadCastMsgPerRep() { send_broad_cast_msg_per_rep_++; }
271
272void Stats::SeqFail() { seq_fail_++; }
273
274void Stats::IncTotalRequest(uint32_t num) {
275 if (prometheus_) {
276 prometheus_->Inc(NUM_EXECUTE_TX, num);
277 }
278 total_request_ += num;
279}
280
281void Stats::IncTotalGeoRequest(uint32_t num) { total_geo_request_ += num; }
282
283void Stats::IncGeoRequest() { geo_request_++; }
284
286 if (prometheus_) {
287 prometheus_->Inc(SERVER_CALL_NAME, 1);
288 }
289 server_call_++;
290}
291
293 if (prometheus_) {
294 prometheus_->Inc(SERVER_PROCESS, 1);
295 }
296 server_process_++;
297}
298
299void Stats::SeqGap(uint64_t seq_gap) { seq_gap_ = seq_gap; }
300
301void Stats::AddLatency(uint64_t run_time) {
302 run_req_num_++;
303 run_req_run_time_ += run_time;
304}
305
306void Stats::SetPrometheus(const std::string& prometheus_address) {
307 prometheus_ = std::make_unique<PrometheusHandler>(prometheus_address);
308}
309
310} // namespace resdb
void IncExecuteDone()
Definition stats.cpp:254
void BroadCastMsg()
Definition stats.cpp:261
void SeqFail()
Definition stats.cpp:272
void IncTotalGeoRequest(uint32_t num)
Definition stats.cpp:281
static Stats * GetGlobalStats(int sleep_seconds=5)
Definition stats.cpp:29
void IncClientRequest()
Definition stats.cpp:222
void MonitorGlobal()
Definition stats.cpp:76
void ServerProcess()
Definition stats.cpp:292
void Stop()
Definition stats.cpp:67
void IncTotalRequest(uint32_t num)
Definition stats.cpp:274
void SendBroadCastMsg(uint32_t num)
Definition stats.cpp:268
void IncPrepare()
Definition stats.cpp:236
void IncExecute()
Definition stats.cpp:252
void IncPendingExecute()
Definition stats.cpp:250
void IncCommit()
Definition stats.cpp:243
void SendBroadCastMsgPerRep()
Definition stats.cpp:270
void AddLatency(uint64_t run_time)
Definition stats.cpp:301
void IncPropose()
Definition stats.cpp:229
void IncGeoRequest()
Definition stats.cpp:283
void SeqGap(uint64_t seq_gap)
Definition stats.cpp:299
void SetPrometheus(const std::string &prometheus_address)
Definition stats.cpp:306
void IncClientCall()
Definition stats.cpp:215
Stats(int sleep_time=5)
Definition stats.cpp:35
void ServerCall()
Definition stats.cpp:285
std::mutex g_mutex
Definition stats.cpp:28