-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[opt](fqdn) Add DNS Cache for FE and BE (#32869)
In previously, when enabling FQDN, Doris will call dns resolver to get IP from hostname each time when 1) FE gets BE's grpc client. 2) BE gets other BE's brpc client. So when in high concurrency case, the dns resolver be overloaded and failed to resolve hostname. This PR mainly changes: 1. Add DNSCache for both FE and BE. The DNSCache will run on every FE and BE node. It has a cache, key is hostname and value is IP. Caller can get IP by hostname from this cache, and if hostname does not exist, it will try to resolve it and update the cache. In addition, DNSCache has a daemon thread to refresh the cache every 1 min, in case that the IP may be changed at anytime. There are other implements of this dns cache: 1. kaka11chen@36fed13 This is for BE side, but it does not handle the IP change case. 3. #28479 This is for FE side, but it can only work with Master FE. Other FE node will not be aware of the IP change. And there are a bunch of BackendServiceProxy, this PR only handle cache in one of them.
- Loading branch information
1 parent
4aa2111
commit 2039016
Showing
12 changed files
with
303 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#include "util/dns_cache.h" | ||
|
||
#include "service/backend_options.h" | ||
#include "util/network_util.h" | ||
|
||
namespace doris { | ||
|
||
DNSCache::DNSCache() { | ||
refresh_thread = std::thread(&DNSCache::_refresh_cache, this); | ||
refresh_thread.detach(); | ||
} | ||
|
||
DNSCache::~DNSCache() { | ||
stop_refresh = true; | ||
if (refresh_thread.joinable()) { | ||
refresh_thread.join(); | ||
} | ||
} | ||
|
||
Status DNSCache::get(const std::string& hostname, std::string* ip) { | ||
{ | ||
std::shared_lock<std::shared_mutex> lock(mutex); | ||
auto it = cache.find(hostname); | ||
if (it != cache.end()) { | ||
*ip = it->second; | ||
return Status::OK(); | ||
} | ||
} | ||
// Update if not found | ||
RETURN_IF_ERROR(_update(hostname)); | ||
{ | ||
std::shared_lock<std::shared_mutex> lock(mutex); | ||
*ip = cache[hostname]; | ||
return Status::OK(); | ||
} | ||
} | ||
|
||
Status DNSCache::_update(const std::string& hostname) { | ||
std::string real_ip = ""; | ||
RETURN_IF_ERROR(hostname_to_ip(hostname, real_ip, BackendOptions::is_bind_ipv6())); | ||
std::unique_lock<std::shared_mutex> lock(mutex); | ||
auto it = cache.find(hostname); | ||
if (it == cache.end() || it->second != real_ip) { | ||
cache[hostname] = real_ip; | ||
LOG(INFO) << "update hostname " << hostname << "'s ip to " << real_ip; | ||
} | ||
return Status::OK(); | ||
} | ||
|
||
void DNSCache::_refresh_cache() { | ||
while (!stop_refresh) { | ||
// refresh every 1 min | ||
std::this_thread::sleep_for(std::chrono::minutes(1)); | ||
std::unordered_set<std::string> keys; | ||
{ | ||
std::shared_lock<std::shared_mutex> lock(mutex); | ||
std::transform(cache.begin(), cache.end(), std::inserter(keys, keys.end()), | ||
[](const auto& pair) { return pair.first; }); | ||
} | ||
Status st; | ||
for (auto& key : keys) { | ||
st = _update(key); | ||
} | ||
} | ||
} | ||
|
||
} // end of namespace doris |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#pragma once | ||
|
||
#include <chrono> | ||
#include <iostream> | ||
#include <shared_mutex> | ||
#include <string> | ||
#include <thread> | ||
#include <unordered_map> | ||
|
||
#include "common/status.h" | ||
|
||
namespace doris { | ||
|
||
// Same as | ||
// fe/fe-core/src/main/java/org/apache/doris/common/DNSCache.java | ||
class DNSCache { | ||
public: | ||
DNSCache(); | ||
~DNSCache(); | ||
|
||
// get ip by hostname | ||
Status get(const std::string& hostname, std::string* ip); | ||
|
||
private: | ||
// update the ip of hostname in cache | ||
Status _update(const std::string& hostname); | ||
|
||
// a function for refresh daemon thread | ||
// update cache at fix internal | ||
void _refresh_cache(); | ||
|
||
private: | ||
// hostname -> ip | ||
std::unordered_map<std::string, std::string> cache; | ||
mutable std::shared_mutex mutex; | ||
std::thread refresh_thread; | ||
bool stop_refresh = false; | ||
}; | ||
|
||
} // end of namespace doris |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.