23#include <boost/filesystem.hpp>
25#include "grpc++/grpc++.h"
26#include "yaml-cpp/yaml.h"
32#include "modules/map/tools/map_datachecker/proto/collection_service.grpc.pb.h"
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()));
49 if (!boost::filesystem::exists(record_path)) {
50 AERROR <<
"record_path [" << record_path <<
"]does not exist";
53 int ret = Start(record_path);
55 AERROR <<
"start check channel failed, record_path [" << record_path <<
"]";
63 std::ofstream ofs(stop_flag_file_);
65 AERROR <<
"Create Flag File [" << stop_flag_file_ <<
"] Failed";
75 while (!boost::filesystem::exists(stop_flag_file_)) {
80 AINFO <<
"channel checker sleep " << check_period_ <<
" seconds";
81 std::this_thread::sleep_for(std::chrono::seconds(check_period_));
84 AINFO <<
"detected stop flag file, periodically checking will exit";
86 AINFO <<
"periodically checking will exit because of some errors";
93 grpc::ClientContext context;
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");
108int ChannelChecker::Start(
const std::string& record_path) {
110 request.set_path(record_path);
112 AINFO <<
"channel check request: "
113 <<
"record_path: [" << request.
path() <<
"], "
114 <<
"cmd: [" << request.
cmd() <<
"]";
116 return GrpcStub(&request, &response);
119int ChannelChecker::Check() {
120 ChannelVerifyRequest request;
122 AINFO <<
"channel check request: "
123 <<
"cmd: [" << request.
cmd() <<
"]";
124 ChannelVerifyResponse response;
125 int ret =
GrpcStub(&request, &response);
127 ProcessAbnormal(&response);
132int ChannelChecker::Stop() {
133 ChannelVerifyRequest request;
135 AINFO <<
"channel check request: "
136 <<
"cmd: [" << request.cmd() <<
"]";
137 ChannelVerifyResponse response;
138 return GrpcStub(&request, &response);
141int ChannelChecker::ProcessAbnormal(ChannelVerifyResponse* response) {
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());
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());
158 AERROR <<
"response content from server is inconsistent";
162 if (response->has_result() && response->result().has_topics()) {
163 const TopicResult& topics = response->result().topics();
165 for (
int i = 0; i < topics.topic_lack_size(); ++i) {
166 fprintf(
USER_STREAM,
"-%s:\n", topics.topic_lack(i).c_str());
169 AERROR <<
"response content from server is inconsistent";
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)
@ ERROR_CHANNEL_VERIFY_RATES_ABNORMAL
@ ERROR_CHANNEL_VERIFY_TOPIC_LACK