-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[opt](fqdn) Add DNS Cache for FE and BE (#32869) #32995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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: | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: redundant access specifier has the same accessibility as the previous access specifier [readability-redundant-access-specifiers]
Suggested change
Additional contextbe/src/util/dns_cache.h:40: previously declared here 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 hidden or 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
95 changes: 95 additions & 0 deletions
95
fe/fe-core/src/main/java/org/apache/doris/common/DNSCache.java
This file contains hidden or 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,95 @@ | ||
| // 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. | ||
|
|
||
| package org.apache.doris.common; | ||
|
|
||
| import org.apache.doris.common.util.NetUtils; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
|
|
||
| import java.net.UnknownHostException; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ScheduledExecutorService; | ||
|
|
||
| /** | ||
| * DNSCache is a class that caches DNS lookups and periodically refreshes them. | ||
| * It uses a ConcurrentHashMap to store the hostname to IP address mappings and a ScheduledExecutorService | ||
| * to periodically refresh these mappings. | ||
| */ | ||
| public class DNSCache { | ||
| private static final Logger LOG = LogManager.getLogger(DNSCache.class); | ||
|
|
||
| private final ConcurrentHashMap<String, String> cache = new ConcurrentHashMap<>(); | ||
| private final ScheduledExecutorService executor = ThreadPoolManager.newDaemonScheduledThreadPool(1, | ||
| "dns_cache_pool", true); | ||
|
|
||
| /** | ||
| * Check if the enable_fqdn_mode configuration is set. | ||
| * If it is, it schedules a task to refresh the DNS cache every 60 seconds, | ||
| * starting after an initial delay of 120 seconds. | ||
| */ | ||
| public void start() { | ||
| if (Config.enable_fqdn_mode) { | ||
| executor.scheduleAtFixedRate(this::refresh, 120, 60, java.util.concurrent.TimeUnit.SECONDS); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The get method retrieves the IP address for a given hostname from the cache. | ||
| * If the hostname is not in the cache, it resolves the hostname to an IP address and stores it in the cache. | ||
| * | ||
| * @param hostname The hostname for which to get the IP address. | ||
| * @return The IP address for the given hostname. | ||
| */ | ||
| public String get(String hostname) { | ||
| return cache.computeIfAbsent(hostname, this::resolveHostname); | ||
| } | ||
|
|
||
| /** | ||
| * The resolveHostname method resolves a hostname to an IP address. | ||
| * If the hostname cannot be resolved, it returns an empty string. | ||
| * | ||
| * @param hostname The hostname to resolve. | ||
| * @return The IP address for the given hostname, or an empty string if the hostname cannot be resolved. | ||
| */ | ||
| private String resolveHostname(String hostname) { | ||
| try { | ||
| return NetUtils.getIpByHost(hostname, 0); | ||
| } catch (UnknownHostException e) { | ||
| return ""; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The refresh method periodically refreshes the DNS cache. | ||
| * It iterates over each hostname in the cache, resolves the hostname to an IP address, | ||
| * and compares it with the current IP address in the cache. | ||
| * If they are different, it updates the cache with the new IP address and logs the change. | ||
| */ | ||
| private void refresh() { | ||
| for (String hostname : cache.keySet()) { | ||
| String resolvedHostname = resolveHostname(hostname); | ||
| String currentHostname = cache.get(hostname); | ||
| if (!resolvedHostname.equals(currentHostname)) { | ||
| cache.put(hostname, resolvedHostname); | ||
| LOG.info("IP for hostname {} has changed from {} to {}", hostname, currentHostname, | ||
| resolvedHostname); | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning: method '_refresh_cache' can be made const [readability-make-member-function-const]
be/src/util/dns_cache.h:46: