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

Maintenance: improved stat5minClientRequests() naming #275

Closed

Conversation

eduard-bagdasaryan
Copy link

@eduard-bagdasaryan eduard-bagdasaryan commented Nov 19, 2024

stat5minClientRequests() was to meant to return the number of recent
client requests. However, the function did not provide implied 5 minute
precision. It returned, roughly speaking, the number of requests during
the last 0-6 minutes. The new, less strict function name and boolean
type avoid this false precision implication.

TODO: This function tracks the number of already processed/logged
requests, instead of the number of received/incoming client requests.
The time gap between receiving a request and logging it may be
significant, especially when bad/stale cache_peer addresses lead to
various retries and timeouts. In such situations, incoming requests
would benefit from fresh cache_peer addresses, so the function should
return true sooner, based on incoming (rather than processed) request
counters.

Also removed unused stat5minCPUUsage().

src/stat.cc Outdated Show resolved Hide resolved
src/stat.cc Outdated Show resolved Hide resolved
src/stat.cc Outdated

if (NCountHist > recentMinutes)
return statCounter.client_http.requests - CountHist[recentMinutes].client_http.requests;
return 0;

Choose a reason for hiding this comment

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

The number of recent requests within the first few minutes of Squid process live is not always zero, right?

Suggested change
return 0;
return statCounter.client_http.requests;

Alternatively, we can use this pattern (that reduces statCounter duplication):

const auto oldRequests = (NCountHist > recentMinutes) ? CountHist[recentMinutes].client_http.requests : 0;
return statCounter.client_http.requests - oldRequests;

Your call.

Copy link
Author

Choose a reason for hiding this comment

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

The number of recent requests within the first few minutes of Squid process live is not always zero

Right, but I thought that since this function cannot produce a meaningful result within these initial few minutes, it should not confuse the caller (which will proceed refreshing DNS). Theoretically, this function could return an 'optional', and the caller would wait in 'zero' and 'undefined' outcomes.

Choose a reason for hiding this comment

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

this function cannot produce a meaningful result within these initial few minutes

I disagree with that assertion. Both true and false statSawRecentRequests() outcomes during the first few minutes seem perfectly meaningful to me (regardless of how an existing or future caller may use those outcomes).

it should not confuse the caller (which will proceed refreshing DNS).

Refreshing code that triggers a refresh just because statSawRecentRequests() returned true is confused for other reasons. No reasonable caller will refresh just because statSawRecentRequests() returned true!

Refreshing code that avoids a refresh just because statSawRecentRequests() returned false is reasonable and will not malfunction (or, to be more precise, any malfunction is outside this discussion scope).

In both cases, this function will not cause or create confusion where none existed.

Theoretically, this function could return an 'optional', and the caller would wait in 'zero' and 'undefined' outcomes.

That would be a good strategy for a function designed to classify cases where there were no requests for the first X minutes as "cannot tell; have to wait for X minutes to pass". Such a function should be named something like statSawRequestsDuringLastXminutes() so that the caller can correctly handle its dependency on X.

I see no reason to complicate our function right now. The vagueness of "recent" allows it to produce correct boolean result at any time. The caller is not dependent on any specific X value -- the caller should work correctly after sleeping for any reasonable (for that caller!) time between calls as long as that caller is OK with vague "recent" definition (e.g., because the caller sleeps for periods that ought to exceed statistics collection periods by a lot). And changing statistics collection interval to any reasonable value will not affect any such caller.

Copy link
Author

Choose a reason for hiding this comment

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

Ok.

src/stat.cc Outdated
static const int recentMinutes = 5;
assert(N_COUNT_HIST > recentMinutes);

if (NCountHist > recentMinutes)

Choose a reason for hiding this comment

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

Let's document why this math is not precise:

Suggested change
if (NCountHist > recentMinutes)
// Math below computes the number of requests during the last 0-6 minutes.
// CountHist is based on "minutes passed since Squid start" periods. It cannot
// deliver precise info for "last N minutes", but we do not need to be precise.
if (NCountHist > recentMinutes)

Copy link
Author

Choose a reason for hiding this comment

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

Done.

src/stat.cc Outdated
{
assert(N_COUNT_HIST > 5);
return statCounter.client_http.requests - CountHist[5].client_http.requests;
static const int recentMinutes = 5;

Choose a reason for hiding this comment

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

AAA if possible:

Suggested change
static const int recentMinutes = 5;
static const auto recentMinutes = 5;

Copy link
Author

Choose a reason for hiding this comment

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

Done.

@rousskov
Copy link

This work became official PR 1951.

@rousskov rousskov closed this Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants