Coverage Report

Created: 2026-03-14 11:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/dns_cache.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "util/dns_cache.h"
19
20
#include "service/backend_options.h"
21
#include "util/network_util.h"
22
23
namespace doris {
24
25
5
DNSCache::DNSCache() {
26
5
    refresh_thread = std::thread(&DNSCache::_refresh_cache, this);
27
5
}
28
29
2
DNSCache::~DNSCache() {
30
2
    stop_refresh = true;
31
2
    if (refresh_thread.joinable()) {
32
2
        refresh_thread.join();
33
2
    }
34
2
}
35
36
96.7k
Status DNSCache::get(const std::string& hostname, std::string* ip) {
37
96.7k
    {
38
96.7k
        std::shared_lock<std::shared_mutex> lock(mutex);
39
96.7k
        auto it = cache.find(hostname);
40
96.9k
        if (it != cache.end()) {
41
96.9k
            *ip = it->second;
42
96.9k
            return Status::OK();
43
96.9k
        }
44
96.7k
    }
45
    // Update if not found
46
18.4E
    RETURN_IF_ERROR(_update(hostname));
47
18.4E
    {
48
18.4E
        std::shared_lock<std::shared_mutex> lock(mutex);
49
18.4E
        *ip = cache[hostname];
50
18.4E
        return Status::OK();
51
18.4E
    }
52
18.4E
}
53
54
// Resolve hostname to IP address, similar to Java's DNSCache.resolveHostname.
55
// If resolution fails, falls back to cached IP if available.
56
// Returns the resolved IP, or cached IP on failure, or empty string if no cache available.
57
83
std::string DNSCache::_resolve_hostname(const std::string& hostname) {
58
    // Get cached IP first (if any)
59
83
    std::string cached_ip;
60
83
    {
61
83
        std::shared_lock<std::shared_mutex> lock(mutex);
62
83
        auto it = cache.find(hostname);
63
83
        if (it != cache.end()) {
64
78
            cached_ip = it->second;
65
78
        }
66
83
    }
67
68
    // Try to resolve hostname
69
83
    std::string resolved_ip;
70
83
    Status status = hostname_to_ip(hostname, resolved_ip, BackendOptions::is_bind_ipv6());
71
72
83
    if (!status.ok() || resolved_ip.empty()) {
73
        // Resolution failed
74
0
        if (!cached_ip.empty()) {
75
0
            LOG(WARNING) << "Failed to resolve hostname " << hostname
76
0
                         << ", use cached ip: " << cached_ip;
77
0
            return cached_ip;
78
0
        } else {
79
0
            LOG(WARNING) << "Failed to resolve hostname " << hostname << ", no cached ip available";
80
0
            return "";
81
0
        }
82
0
    }
83
84
83
    return resolved_ip;
85
83
}
86
87
83
Status DNSCache::_update(const std::string& hostname) {
88
83
    std::string real_ip = _resolve_hostname(hostname);
89
83
    if (real_ip.empty()) {
90
0
        return Status::InternalError("Failed to resolve hostname {} and no cached ip available",
91
0
                                     hostname);
92
0
    }
93
94
83
    std::unique_lock<std::shared_mutex> lock(mutex);
95
83
    auto it = cache.find(hostname);
96
83
    if (it == cache.end() || it->second != real_ip) {
97
5
        cache[hostname] = real_ip;
98
5
        LOG(INFO) << "update hostname " << hostname << "'s ip to " << real_ip;
99
5
    }
100
83
    return Status::OK();
101
83
}
102
103
5
void DNSCache::_refresh_cache() {
104
88
    while (!stop_refresh) {
105
        // refresh every 1 min
106
83
        std::this_thread::sleep_for(std::chrono::minutes(1));
107
83
        std::unordered_set<std::string> keys;
108
83
        {
109
83
            std::shared_lock<std::shared_mutex> lock(mutex);
110
83
            std::transform(cache.begin(), cache.end(), std::inserter(keys, keys.end()),
111
83
                           [](const auto& pair) { return pair.first; });
112
83
        }
113
83
        for (auto& key : keys) {
114
78
            Status st = _update(key);
115
78
            if (!st.ok()) {
116
                LOG(WARNING) << "Failed to update DNS cache for hostname " << key << ": "
117
0
                             << st.to_string();
118
0
            }
119
78
        }
120
83
    }
121
5
}
122
123
} // end of namespace doris