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

Implement DNS cache recycling #1840

Merged
merged 6 commits into from
Jan 4, 2024
Merged

Implement DNS cache recycling #1840

merged 6 commits into from
Jan 4, 2024

Conversation

DL6ER
Copy link
Member

@DL6ER DL6ER commented Dec 24, 2023

What does this implement/fix?

This PR completes what was started in #1694 - memory recycling. The issue at hand was caused by adding the facility to recycle DNS cache records but then never actually doing it. The missing bits are added in this PR.

This fixes an issue described on Discourse (see link below): After a certain time has passed, clients and domains may be recycled, however the cache records saying that "clientID=123,domainID=234 -> blocked" remained. This was harmless until a new (possibly but not necessarily the old) client/domain combination in exactly this configuration appeared which then lead to a cached reply which was actually intended for something entirely different.

This is fixed by recycling cache records as soon as they are not referenced by any query. This is similar to how we recycle clients and domains, too - we remove them when the last query referencing them disappeared due to aging (> 24h).


Related issue or feature (if applicable): N/A

Pull request in docs with documentation (if applicable): N/A


By submitting this pull request, I confirm the following:

  1. I have read and understood the contributors guide, as well as this entire template. I understand which branch to base my commits and Pull Requests against.
  2. I have commented my proposed changes within the code.
  3. I am willing to help maintain this change if there are issues with it later.
  4. It is compatible with the EUPL 1.2 license
  5. I have squashed any insignificant commits. (git rebase)

Checklist:

  • The code change is tested and works locally.
  • I based my code and PRs against the repositories developmental branch.
  • I signed off all commits. Pi-hole enforces the DCO for all contributions
  • I signed all my commits. Pi-hole requires signatures to verify authorship
  • I have read the above and my PR is ready for review.

…cking of certain domain/client/type combinations

Signed-off-by: DL6ER <dl6er@dl6er.de>
Signed-off-by: DL6ER <dl6er@dl6er.de>
Signed-off-by: DL6ER <dl6er@dl6er.de>
Signed-off-by: DL6ER <dl6er@dl6er.de>
@pralor-bot
Copy link

This pull request has been mentioned on Pi-hole Userspace. There might be relevant details there:

https://discourse.pi-hole.net/t/pi-hole-blocking-domain-it-shouldnt/67111/14

…one already stored in the query, we have to re-lookup the cache ID. This can happen when a CNAME chain is followed and analyzed

Signed-off-by: DL6ER <dl6er@dl6er.de>
@DL6ER
Copy link
Member Author

DL6ER commented Dec 31, 2023

Confirmed working by OP on Discourse

@@ -378,17 +377,17 @@ void runGC(const time_t now, time_t *lastGCrun, const bool flush)

// Update reply counters
counters->reply[query->reply]--;
log_debug(DEBUG_GC, "reply type %d removed (GC), ID = %d, new count = %d", query->reply, query->id, counters->reply[query->reply]);
log_debug(DEBUG_STATUS, "reply type %d removed (GC), ID = %d, new count = %d", query->reply, query->id, counters->reply[query->reply]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change all the instances from DEBUG_GC > DEBUG_STATUS?

Here it even talks about the garbage collection.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the status debug flag is meant specifically to log every single counter change (for every query!) so it's very very verbose. The GC debug flag prints much fewer lines with more condensed content. As these lines make no sense without DEBUG_STATUS being set at the same time (for context), I moved them over there

@@ -482,6 +482,7 @@ void log_counter_info(void)
log_info(" -> Unknown DNS queries: %i", counters->status[QUERY_UNKNOWN]);
log_info(" -> Unique domains: %i", counters->domains);
log_info(" -> Unique clients: %i", counters->clients);
log_info(" -> DNS cache records: %i", counters->dns_cache_size);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does "DNS cache records" tell us? How does it relate to the imported long-term data?

2024-01-04 22:11:36.590 [430399M] INFO: Imported 8396 queries from the long-term database
2024-01-04 22:11:36.598 [430399M] INFO:  -> Total DNS queries: 8396
2024-01-04 22:11:36.599 [430399M] INFO:  -> Cached DNS queries: 5696
2024-01-04 22:11:36.599 [430399M] INFO:  -> Forwarded DNS queries: 1889
2024-01-04 22:11:36.599 [430399M] INFO:  -> Blocked DNS queries: 35
2024-01-04 22:11:36.599 [430399M] INFO:  -> Unknown DNS queries: 4
2024-01-04 22:11:36.599 [430399M] INFO:  -> Unique domains: 583
2024-01-04 22:11:36.599 [430399M] INFO:  -> Unique clients: 3
2024-01-04 22:11:36.599 [430399M] INFO:  -> DNS cache records: 937
2024-01-04 22:11:36.599 [430399M] INFO:  -> Known forward destinations: 2

Copy link
Member Author

@DL6ER DL6ER Jan 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every unique client/domain combination creates a unique DNS cache record. Two queries for the same domain done by the same client use one cache record. Queries for the same domain by three clients create three cache records (because some clients may be blocking a domain while others don't due to groups). Thia is just yet another metric used by FTL for quite some time and which seemed useful to include here.

@DL6ER DL6ER merged commit c24c1e1 into development-v6 Jan 4, 2024
17 checks passed
@DL6ER DL6ER deleted the fix/cache_recycling branch January 4, 2024 22:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants