Skip to content
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

feat(fqdn): Limit the cache size on dns_resolve #1892

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,7 +29,9 @@
#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"
#include "utils/ports.h"

METRIC_DEFINE_gauge_int64(server,
dns_resolver_cache_size,
Expand All @@ -48,6 +50,12 @@ METRIC_DEFINE_percentile_int64(server,
"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
Loading