Apollo 11.0
自动驾驶开放平台
client_loops_check.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <utility>
20
21#include <boost/algorithm/string.hpp>
22#include "grpc++/grpc++.h"
23#include "yaml-cpp/yaml.h"
24
25namespace apollo {
26namespace hdmap {
27
28LoopsChecker::LoopsChecker(const std::string& time_flag_file)
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()));
37}
38
39int LoopsChecker::SyncStart(bool* reached) {
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";
44 return -1;
45 }
46 int ret = Start(time_ranges);
47 if (ret != 0) {
48 AINFO << "loops check start failed";
49 fprintf(USER_STREAM, "loops check start failed\n");
50 return -1;
51 }
52 return PeriodicCheck(reached);
53}
54
55int LoopsChecker::PeriodicCheck(bool* reached) {
56 int ret = 0;
57 while (true) {
58 double progress = 0.0;
59 ret = Check(&progress, reached);
60 if (ret != 0) {
61 AERROR << "loops check failed";
62 break;
63 }
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) {
67 break;
68 }
69 std::this_thread::sleep_for(std::chrono::seconds(check_period_));
70 }
71 if (ret != 0) {
72 return -1;
73 }
74 ret = Stop();
75 if (ret != 0) {
76 AERROR << "loops check stop failed";
77 return -1;
78 }
79 return 0;
80}
81
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";
89 fprintf(USER_STREAM,
90 "The command start and stop should be appear in pairs\n");
91 return empty;
92 }
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";
100 fprintf(USER_STREAM,
101 "The data collect command start and stop should be appear in "
102 "pairs\n");
103 return empty;
104 }
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";
110 fprintf(USER_STREAM,
111 "The data collect command start and stop should be appear in "
112 "pairs\n");
113 return empty;
114 }
115 result.push_back(std::make_pair(start_time, end_time));
116 }
117 return result;
118}
119
120int LoopsChecker::GrpcStub(LoopsVerifyRequest* request,
121 LoopsVerifyResponse* response) {
122 grpc::ClientContext context;
123 grpc::Status status;
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");
128 return -1;
129 }
130 ErrorCode error_code = response->code();
131 if (error_code != ErrorCode::SUCCESS) {
132 return ExceptionHandler::ExceptionHandlerFun(error_code);
133 }
134 return 0;
135}
136
137int LoopsChecker::Start(
138 const std::vector<std::pair<double, double>>& time_ranges) {
139 LoopsVerifyRequest request;
140 request.set_cmd(CmdType::START);
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 << "]";
147 }
148 LoopsVerifyResponse response;
149 return GrpcStub(&request, &response);
150}
151int LoopsChecker::Check(double* progress, bool* reached) {
152 LoopsVerifyRequest request;
153 request.set_cmd(CmdType::CHECK);
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();
161 }
162 return ret;
163}
164
165int LoopsChecker::Stop() {
166 LoopsVerifyRequest request;
167 request.set_cmd(CmdType::STOP);
168 AINFO << "loops check request: "
169 << "cmd: [" << request.cmd() << "]";
170 LoopsVerifyResponse response;
171 return GrpcStub(&request, &response);
172}
173
174} // namespace hdmap
175} // namespace apollo
static int ExceptionHandlerFun(ErrorCode error_code)
LoopsChecker(const std::string &time_flag_file)
#define USER_STREAM
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
std::vector< std::string > GetFileLines(const std::string &path)
class register implement
Definition arena_queue.h:37