Apollo 11.0
自动驾驶开放平台
client_channel_checker.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 *****************************************************************************/
17
18#include <chrono>
19#include <fstream>
20#include <memory>
21#include <string>
22
23#include <boost/filesystem.hpp>
24
25#include "grpc++/grpc++.h"
26#include "yaml-cpp/yaml.h"
27
28#include "cyber/cyber.h"
32#include "modules/map/tools/map_datachecker/proto/collection_service.grpc.pb.h"
33
34namespace apollo {
35namespace hdmap {
36
37ChannelChecker::ChannelChecker(const std::string& stop_flag_file)
38 : stop_flag_file_(stop_flag_file) {
39 YAML::Node node = YAML::LoadFile(FLAGS_client_conf_yaml);
40 std::string server_addr =
41 node["grpc_host_port"]["grpc_host"].as<std::string>() + ":" +
42 node["grpc_host_port"]["grpc_port"].as<std::string>();
43 check_period_ = node["channel_check"]["check_period"].as<int>();
44 service_stub_ = CollectionCheckerService::NewStub(
45 grpc::CreateChannel(server_addr, grpc::InsecureChannelCredentials()));
46}
47
48int ChannelChecker::SyncStart(const std::string& record_path) {
49 if (!boost::filesystem::exists(record_path)) {
50 AERROR << "record_path [" << record_path << "]does not exist";
51 return -1;
52 }
53 int ret = Start(record_path);
54 if (ret != 0) {
55 AERROR << "start check channel failed, record_path [" << record_path << "]";
56 return -1;
57 }
58 return PeriodicCheck();
59}
60
62 // stop client
63 std::ofstream ofs(stop_flag_file_);
64 if (!ofs) {
65 AERROR << "Create Flag File [" << stop_flag_file_ << "] Failed";
66 return -1;
67 }
68 ofs.close();
69 // stop server
70 return Stop();
71}
72
74 int ret = 0;
75 while (!boost::filesystem::exists(stop_flag_file_)) {
76 ret = Check();
77 if (ret != 0) {
78 break;
79 }
80 AINFO << "channel checker sleep " << check_period_ << " seconds";
81 std::this_thread::sleep_for(std::chrono::seconds(check_period_));
82 }
83 if (ret == 0) {
84 AINFO << "detected stop flag file, periodically checking will exit";
85 } else {
86 AINFO << "periodically checking will exit because of some errors";
87 }
88 return ret;
89}
90
92 ChannelVerifyResponse* response) {
93 grpc::ClientContext context;
94 grpc::Status status;
95 status = service_stub_->ServiceChannelVerify(&context, *request, response);
96 if (status.error_code() == grpc::StatusCode::UNAVAILABLE) {
97 AERROR << "FATAL Error. Map grpc service is UNAVAILABLE.";
98 fprintf(USER_STREAM, "You should start server first\n");
99 return -1;
100 }
101 ErrorCode error_code = response->code();
102 if (error_code != ErrorCode::SUCCESS) {
103 return ExceptionHandler::ExceptionHandlerFun(error_code);
104 }
105 return 0;
106}
107
108int ChannelChecker::Start(const std::string& record_path) {
109 ChannelVerifyRequest request;
110 request.set_path(record_path);
111 request.set_cmd(CmdType::START);
112 AINFO << "channel check request: "
113 << "record_path: [" << request.path() << "], "
114 << "cmd: [" << request.cmd() << "]";
115 ChannelVerifyResponse response;
116 return GrpcStub(&request, &response);
117}
118
119int ChannelChecker::Check() {
120 ChannelVerifyRequest request;
121 request.set_cmd(CmdType::CHECK);
122 AINFO << "channel check request: "
123 << "cmd: [" << request.cmd() << "]";
124 ChannelVerifyResponse response;
125 int ret = GrpcStub(&request, &response);
126 if (ret != 0) {
127 ProcessAbnormal(&response);
128 }
129 return ret;
130}
131
132int ChannelChecker::Stop() {
133 ChannelVerifyRequest request;
134 request.set_cmd(CmdType::STOP);
135 AINFO << "channel check request: "
136 << "cmd: [" << request.cmd() << "]";
137 ChannelVerifyResponse response;
138 return GrpcStub(&request, &response);
139}
140
141int ChannelChecker::ProcessAbnormal(ChannelVerifyResponse* response) {
142 ErrorCode code = response->code();
144 if (response->has_result()) {
145 const VerifyResult& result = response->result();
146 for (int i = 0; i < result.rates_size(); ++i) {
147 const FrameRate& rate = result.rates(i);
148 fprintf(USER_STREAM, "Channels with insufficient frame rate:%s\n",
149 rate.topic().c_str());
150 fprintf(USER_STREAM, "-expected_rate:%lf\n", rate.expected_rate());
151 fprintf(USER_STREAM, "-current_rate:%lf\n", rate.current_rate());
152 fprintf(USER_STREAM, "-bad_record_name:\n");
153 for (int j = 0; j < rate.bad_record_name_size(); ++j) {
154 fprintf(USER_STREAM, "--%s\n", rate.bad_record_name(j).c_str());
155 }
156 }
157 } else {
158 AERROR << "response content from server is inconsistent";
159 return -1;
160 }
162 if (response->has_result() && response->result().has_topics()) {
163 const TopicResult& topics = response->result().topics();
164 fprintf(USER_STREAM, "Missing channel(s):\n");
165 for (int i = 0; i < topics.topic_lack_size(); ++i) {
166 fprintf(USER_STREAM, "-%s:\n", topics.topic_lack(i).c_str());
167 }
168 } else {
169 AERROR << "response content from server is inconsistent";
170 return -1;
171 }
172 }
173 return 0;
174}
175
176} // namespace hdmap
177} // namespace apollo
ChannelChecker(const std::string &stop_flag_file)
int GrpcStub(ChannelVerifyRequest *request, ChannelVerifyResponse *response)
int SyncStart(const std::string &record_path)
static int ExceptionHandlerFun(ErrorCode error_code)
#define USER_STREAM
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37