Coverage Report

Created: 2024-11-20 16:51

/root/doris/be/src/util/dns_cache.cpp
Line
Count
Source (jump to first uncovered line)
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
0
DNSCache::DNSCache() {
26
0
    refresh_thread = std::thread(&DNSCache::_refresh_cache, this);
27
0
}
28
29
0
DNSCache::~DNSCache() {
30
0
    stop_refresh = true;
31
0
    if (refresh_thread.joinable()) {
32
0
        refresh_thread.join();
33
0
    }
34
0
}
35
36
0
Status DNSCache::get(const std::string& hostname, std::string* ip) {
37
0
    {
38
0
        std::shared_lock<std::shared_mutex> lock(mutex);
39
0
        auto it = cache.find(hostname);
40
0
        if (it != cache.end()) {
41
0
            *ip = it->second;
42
0
            return Status::OK();
43
0
        }
44
0
    }
45
    // Update if not found
46
0
    RETURN_IF_ERROR(_update(hostname));
47
0
    {
48
0
        std::shared_lock<std::shared_mutex> lock(mutex);
49
0
        *ip = cache[hostname];
50
0
        return Status::OK();
51
0
    }
52
0
}
53
54
0
Status DNSCache::_update(const std::string& hostname) {
55
0
    std::string real_ip = "";
56
0
    RETURN_IF_ERROR(hostname_to_ip(hostname, real_ip, BackendOptions::is_bind_ipv6()));
57
0
    std::unique_lock<std::shared_mutex> lock(mutex);
58
0
    auto it = cache.find(hostname);
59
0
    if (it == cache.end() || it->second != real_ip) {
60
0
        cache[hostname] = real_ip;
61
0
        LOG(INFO) << "update hostname " << hostname << "'s ip to " << real_ip;
62
0
    }
63
0
    return Status::OK();
64
0
}
65
66
0
void DNSCache::_refresh_cache() {
67
0
    while (!stop_refresh) {
68
        // refresh every 1 min
69
0
        std::this_thread::sleep_for(std::chrono::minutes(1));
70
0
        std::unordered_set<std::string> keys;
71
0
        {
72
0
            std::shared_lock<std::shared_mutex> lock(mutex);
73
0
            std::transform(cache.begin(), cache.end(), std::inserter(keys, keys.end()),
74
0
                           [](const auto& pair) { return pair.first; });
75
0
        }
76
0
        Status st;
77
0
        for (auto& key : keys) {
78
0
            st = _update(key);
79
0
        }
80
0
    }
81
0
}
82
83
} // end of namespace doris