-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[opt](fqdn) Add DNS Cache for FE and BE #32869
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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()) { | ||||||
|
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. not joinable, so DNECache will be destoried immediately |
||||||
| 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) { | ||||||
yiguolei marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| 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() { | ||||||
|
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: method '_refresh_cache' can be made const [readability-make-member-function-const]
Suggested change
be/src/util/dns_cache.h:41: - void _refresh_cache();
+ void _refresh_cache() const;
yiguolei marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| while (!stop_refresh) { | ||||||
| // refresh every 1 min | ||||||
| std::this_thread::sleep_for(std::chrono::minutes(1)); | ||||||
|
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. btw, after |
||||||
| 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()), | ||||||
|
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. it is very likely that object cache has already been destroyed, if main thread has already exited with --grace enabled. |
||||||
| [](const auto& pair) { return pair.first; }); | ||||||
| } | ||||||
| Status st; | ||||||
| for (auto& key : keys) { | ||||||
| st = _update(key); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| } // end of namespace doris | ||||||
| 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); | ||||||
|
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: unknown type name 'Status' [clang-diagnostic-error] Status get(const std::string& hostname, std::string* ip);
^ |
||||||
|
|
||||||
| private: | ||||||
| // update the ip of hostname in cache | ||||||
| Status _update(const std::string& hostname); | ||||||
|
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: unknown type name 'Status' [clang-diagnostic-error] 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:35: previously declared here 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 | ||||||
| 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); | ||
| } | ||
| } | ||
| } | ||
| } |
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.
the detached thread can not be join by main thread
https://stackoverflow.com/questions/39174761/pthreads-can-i-detach-from-a-thread-and-then-join-in-main
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.
I will fix it