21#include <boost/algorithm/string.hpp>
22#include "grpc++/grpc++.h"
23#include "yaml-cpp/yaml.h"
29 : time_flag_file_(time_flag_file) {
30 YAML::Node node = YAML::LoadFile(FLAGS_client_conf_yaml);
31 std::string server_addr =
32 node[
"grpc_host_port"][
"grpc_host"].as<std::string>() +
":" +
33 node[
"grpc_host_port"][
"grpc_port"].as<std::string>();
34 check_period_ = node[
"loops_check"][
"check_period"].as<
int>();
35 service_stub_ = CollectionCheckerService::NewStub(
36 grpc::CreateChannel(server_addr, grpc::InsecureChannelCredentials()));
40 std::vector<std::pair<double, double>> time_ranges = GetTimeRanges();
41 size_t pair_count = time_ranges.size();
42 if (pair_count == 0) {
43 AINFO <<
"there is no time range";
46 int ret = Start(time_ranges);
48 AINFO <<
"loops check start failed";
52 return PeriodicCheck(reached);
55int LoopsChecker::PeriodicCheck(
bool* reached) {
58 double progress = 0.0;
59 ret = Check(&progress, reached);
61 AERROR <<
"loops check failed";
64 AINFO <<
"loops check progress: [" << progress <<
"]";
65 fprintf(
USER_STREAM,
"loops check progress: %.2lf%%\n", progress * 100);
66 if (fabs(progress - 1.0) < 1e-8) {
69 std::this_thread::sleep_for(std::chrono::seconds(check_period_));
76 AERROR <<
"loops check stop failed";
82std::vector<std::pair<double, double>> LoopsChecker::GetTimeRanges() {
83 std::vector<std::pair<double, double>> result;
84 std::vector<std::pair<double, double>> empty;
85 std::vector<std::string> lines =
GetFileLines(time_flag_file_);
86 size_t record_count = lines.size();
87 if (record_count == 0 || (record_count & 1) != 0) {
88 AINFO <<
"record_count should be even number";
90 "The command start and stop should be appear in pairs\n");
93 for (
size_t i = 0; i < record_count; i += 2) {
94 std::vector<std::string> split_str;
95 boost::split(split_str, lines[i], boost::is_any_of(
" ,\t\n"));
96 double start_time = 0.0;
97 sscanf(split_str[0].c_str(),
"%lf", &start_time);
98 if (split_str[1] !=
"start") {
99 AERROR <<
"state machine was broken";
101 "The data collect command start and stop should be appear in "
105 boost::split(split_str, lines[i + 1], boost::is_any_of(
" ,\t\n"));
106 double end_time = 0.0;
107 sscanf(split_str[0].c_str(),
"%lf", &end_time);
108 if (split_str[1] !=
"stop") {
109 AERROR <<
"state machine was broken";
111 "The data collect command start and stop should be appear in "
115 result.push_back(std::make_pair(start_time, end_time));
120int LoopsChecker::GrpcStub(LoopsVerifyRequest* request,
121 LoopsVerifyResponse* response) {
122 grpc::ClientContext context;
124 status = service_stub_->ServiceLoopsVerify(&context, *request, response);
125 if (status.error_code() == grpc::StatusCode::UNAVAILABLE) {
126 AERROR <<
"FATAL Error. Map grpc service is UNAVAILABLE.";
127 fprintf(
USER_STREAM,
"You should start server first\n");
137int LoopsChecker::Start(
138 const std::vector<std::pair<double, double>>& time_ranges) {
139 LoopsVerifyRequest request;
141 for (
size_t i = 0; i < time_ranges.size(); i++) {
142 VerifyRange* range = request.add_range();
143 range->set_start_time(time_ranges[i].first);
144 range->set_end_time(time_ranges[i].second);
145 AINFO <<
"range[" << i <<
"] is [" << time_ranges[i].first <<
","
146 << time_ranges[i].second <<
"]";
148 LoopsVerifyResponse response;
149 return GrpcStub(&request, &response);
151int LoopsChecker::Check(
double* progress,
bool* reached) {
152 LoopsVerifyRequest request;
154 AINFO <<
"loops check request: "
155 <<
"cmd: [" << request.cmd() <<
"]";
156 LoopsVerifyResponse response;
157 int ret = GrpcStub(&request, &response);
158 *progress = response.progress();
159 if (response.has_loop_result() && response.loop_result().has_is_reached()) {
160 *reached = response.loop_result().is_reached();
165int LoopsChecker::Stop() {
166 LoopsVerifyRequest request;
168 AINFO <<
"loops check request: "
169 <<
"cmd: [" << request.cmd() <<
"]";
170 LoopsVerifyResponse response;
171 return GrpcStub(&request, &response);
static int ExceptionHandlerFun(ErrorCode error_code)
int SyncStart(bool *reached)
LoopsChecker(const std::string &time_flag_file)
std::vector< std::string > GetFileLines(const std::string &path)