Commit 50d26f74 authored by zhangyaofu's avatar zhangyaofu

review comments fix

parent ec741437
...@@ -41,14 +41,14 @@ DEFINE_string(consul_url_parameter, "?stale&passing", ...@@ -41,14 +41,14 @@ DEFINE_string(consul_url_parameter, "?stale&passing",
"The query string of request consul for discovering service."); "The query string of request consul for discovering service.");
DEFINE_int32(consul_connect_timeout_ms, 200, DEFINE_int32(consul_connect_timeout_ms, 200,
"Timeout for creating connections to consul in milliseconds"); "Timeout for creating connections to consul in milliseconds");
DEFINE_int32(consul_blocking_query_wait_secs, 600, DEFINE_int32(consul_blocking_query_wait_secs, 60,
"Maximum duration for the blocking request in secs."); "Maximum duration for the blocking request in secs.");
DEFINE_bool(consul_enable_degrade_to_file_naming_service, false, DEFINE_bool(consul_enable_degrade_to_file_naming_service, false,
"Use local backup file when consul cannot connect"); "Use local backup file when consul cannot connect");
DEFINE_string(consul_file_naming_service_dir, "", DEFINE_string(consul_file_naming_service_dir, "",
"When it degraded to file naming service, the file with name of the " "When it degraded to file naming service, the file with name of the "
"service name will be searched in this dir to use."); "service name will be searched in this dir to use.");
DEFINE_int32(consul_retry_interval_ms, 50, DEFINE_int32(consul_retry_interval_ms, 500,
"Wait so many milliseconds before retry when error happens"); "Wait so many milliseconds before retry when error happens");
constexpr char kConsulIndex[] = "X-Consul-Index"; constexpr char kConsulIndex[] = "X-Consul-Index";
...@@ -60,8 +60,8 @@ std::string RapidjsonValueToString(const rapidjson::Value& value) { ...@@ -60,8 +60,8 @@ std::string RapidjsonValueToString(const rapidjson::Value& value) {
return buffer.GetString(); return buffer.GetString();
} }
int ConsulNamingService::DegradeToOtherServiceIfNeed(const char* service_name, int ConsulNamingService::DegradeToOtherServiceIfNeeded(const char* service_name,
std::vector<ServerNode>* servers) { std::vector<ServerNode>* servers) {
if (FLAGS_consul_enable_degrade_to_file_naming_service && !_backup_file_loaded) { if (FLAGS_consul_enable_degrade_to_file_naming_service && !_backup_file_loaded) {
_backup_file_loaded = true; _backup_file_loaded = true;
const std::string file(FLAGS_consul_file_naming_service_dir + service_name); const std::string file(FLAGS_consul_file_naming_service_dir + service_name);
...@@ -81,7 +81,7 @@ int ConsulNamingService::GetServers(const char* service_name, ...@@ -81,7 +81,7 @@ int ConsulNamingService::GetServers(const char* service_name,
opt.timeout_ms = (FLAGS_consul_blocking_query_wait_secs + 10) * butil::Time::kMillisecondsPerSecond; opt.timeout_ms = (FLAGS_consul_blocking_query_wait_secs + 10) * butil::Time::kMillisecondsPerSecond;
if (_channel.Init(FLAGS_consul_agent_addr.c_str(), "rr", &opt) != 0) { if (_channel.Init(FLAGS_consul_agent_addr.c_str(), "rr", &opt) != 0) {
LOG(ERROR) << "Fail to init channel to consul at " << FLAGS_consul_agent_addr; LOG(ERROR) << "Fail to init channel to consul at " << FLAGS_consul_agent_addr;
return DegradeToOtherServiceIfNeed(service_name, servers); return DegradeToOtherServiceIfNeeded(service_name, servers);
} }
_consul_connected = true; _consul_connected = true;
} }
...@@ -105,7 +105,7 @@ int ConsulNamingService::GetServers(const char* service_name, ...@@ -105,7 +105,7 @@ int ConsulNamingService::GetServers(const char* service_name,
if (cntl.Failed()) { if (cntl.Failed()) {
LOG(ERROR) << "Fail to access " << consul_url << ": " LOG(ERROR) << "Fail to access " << consul_url << ": "
<< cntl.ErrorText(); << cntl.ErrorText();
return DegradeToOtherServiceIfNeed(service_name, servers); return DegradeToOtherServiceIfNeeded(service_name, servers);
} }
const std::string* index = cntl.http_response().GetHeader(kConsulIndex); const std::string* index = cntl.http_response().GetHeader(kConsulIndex);
...@@ -146,7 +146,7 @@ int ConsulNamingService::GetServers(const char* service_name, ...@@ -146,7 +146,7 @@ int ConsulNamingService::GetServers(const char* service_name,
!service["Address"].IsString() || !service["Address"].IsString() ||
!service.HasMember("Port") || !service.HasMember("Port") ||
!service["Port"].IsUint()) { !service["Port"].IsUint()) {
LOG(ERROR) << "Invalid service: " LOG(ERROR) << "Service with no valid address or port: "
<< RapidjsonValueToString(service); << RapidjsonValueToString(service);
continue; continue;
} }
...@@ -162,12 +162,23 @@ int ConsulNamingService::GetServers(const char* service_name, ...@@ -162,12 +162,23 @@ int ConsulNamingService::GetServers(const char* service_name,
ServerNode node; ServerNode node;
node.addr = end_point; node.addr = end_point;
// Tags in consul is an array, here we only use the first one. if (service.HasMember("Tags")) {
if (service.HasMember("Tags") && if (service["Tags"].IsArray()) {
service["Tags"].IsArray() && if (service["Tags"].Size() > 0) {
service["Tags"].Size() > 0 && // Tags in consul is an array, here we only use the first one.
service["Tags"][0].IsString()) { if (service["Tags"][0].IsString()) {
node.tag = service["Tags"][0].GetString(); node.tag = service["Tags"][0].GetString();
} else {
LOG(ERROR) << "First tag returned by consul is not string, service: "
<< RapidjsonValueToString(service);
continue;
}
}
} else {
LOG(ERROR) << "Service tags returned by consul is not json array, service: "
<< RapidjsonValueToString(service);
continue;
}
} }
if (presence.insert(node).second) { if (presence.insert(node).second) {
......
...@@ -37,8 +37,8 @@ private: ...@@ -37,8 +37,8 @@ private:
NamingService* New() const; NamingService* New() const;
int DegradeToOtherServiceIfNeed(const char* service_name, int DegradeToOtherServiceIfNeeded(const char* service_name,
std::vector<ServerNode>* servers); std::vector<ServerNode>* servers);
void Destroy(); void Destroy();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment