Skip to content

Commit

Permalink
feat(fqdn): Limit the cache size on dns_resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
GehaFearless committed Feb 4, 2024
1 parent 4e7525b commit 7c3f37b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
18 changes: 14 additions & 4 deletions src/runtime/rpc/dns_resolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "runtime/rpc/group_address.h"
#include "runtime/rpc/group_host_port.h"
#include "utils/autoref_ptr.h"
#include "utils/flags.h"
#include "utils/fmt_logging.h"

METRIC_DEFINE_gauge_int64(server,
Expand All @@ -46,8 +47,15 @@ METRIC_DEFINE_percentile_int64(server,
dns_resolver_resolve_by_dns_duration_ns,
dsn::metric_unit::kNanoSeconds,
"The duration of resolving a host port by DNS lookup");

namespace dsn {

DSN_DEFINE_int32(network,
max_count_for_dns_cache,
100,
"The size of dns_cache on dns_resolver. The part exceeding the "
"cache size needs to be resolved from the system level each time");

dns_resolver::dns_resolver()
: METRIC_VAR_INIT_server(dns_resolver_cache_size),
METRIC_VAR_INIT_server(dns_resolver_resolve_duration_ns),
Expand Down Expand Up @@ -95,10 +103,12 @@ error_s dns_resolver::resolve_addresses(const host_port &hp, std::vector<rpc_add
resolved_addresses[0]);
}

utils::auto_write_lock l(_lock);
const auto it = _dns_cache.insert(std::make_pair(hp, resolved_addresses[0]));
if (it.second) {
METRIC_VAR_INCREMENT(dns_resolver_cache_size);
if (dsn_likely(_dns_cache.size() < FLAGS_max_count_for_dns_cache)) {
utils::auto_write_lock l(_lock);
const auto it = _dns_cache.insert(std::make_pair(hp, resolved_addresses[0]));
if (it.second) {
METRIC_VAR_INCREMENT(dns_resolver_cache_size);
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/runtime/rpc/dns_resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ namespace dsn {
// the resolved result list.
// If some host_port's rpc_address changes, you need to restart the Pegasus process to make it take
// effect.
// TODO(yingchun): Now the cache is unlimited, the cache size may be huge. Implement an expiration
// mechanism to limit the cache size and make it possible to update the resolve result.
class dns_resolver
{
public:
Expand Down

0 comments on commit 7c3f37b

Please sign in to comment.