Skip to content

resolver_wrapper: add early return in addChannelzTraceEvent #7437

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

Merged
merged 1 commit into from
Jul 30, 2024
Merged

resolver_wrapper: add early return in addChannelzTraceEvent #7437

merged 1 commit into from
Jul 30, 2024

Conversation

jsm
Copy link
Contributor

@jsm jsm commented Jul 23, 2024

Note: this is a degradation introduced in this PR: #5192

Which used to wrap calls to addChannelzTraceEvent in a if channelz.IsOn() {

Problem

Large and frequent calls to this method in UpdateState can cause an extremely high usage of CPU time and Memory due to underlying json.Marshal calls that are not used or needed if were not Info level logging as it is.

Alternative approach to: #7436

Pprofs

image

image

RELEASE NOTES: none

@jsm jsm changed the title resolver_wrapper: early return addChannelzTraceEvent on !logger.Z(0) resolver_wrapper: early return addChannelzTraceEvent on !logger.V(0) Jul 23, 2024
Copy link

codecov bot commented Jul 23, 2024

Codecov Report

Attention: Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.

Project coverage is 81.31%. Comparing base (ac5a7fe) to head (9ec7522).
Report is 12 commits behind head on master.

Files Patch % Lines
resolver_wrapper.go 0.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7437      +/-   ##
==========================================
- Coverage   81.33%   81.31%   -0.03%     
==========================================
  Files         352      354       +2     
  Lines       26919    27080     +161     
==========================================
+ Hits        21895    22020     +125     
- Misses       3824     3844      +20     
- Partials     1200     1216      +16     
Files Coverage Δ
resolver_wrapper.go 83.33% <0.00%> (-1.78%) ⬇️

... and 28 files with indirect coverage changes

@aranjans aranjans self-requested a review July 29, 2024 06:21
@aranjans aranjans self-assigned this Jul 29, 2024
@jsm
Copy link
Contributor Author

jsm commented Jul 29, 2024

Note: this is a degradation introduced in this PR: #5192

Which used to wrap calls to addChannelzTraceEvent in a if channelz.IsOn() {

@aranjans aranjans added this to the 1.66 Release milestone Jul 30, 2024
@aranjans aranjans requested review from arjan-bal and removed request for aranjans July 30, 2024 08:16
@aranjans aranjans assigned arjan-bal and unassigned aranjans Jul 30, 2024
@arjan-bal
Copy link
Contributor

Hi @jsm, thanks for reporting the issue and contributing the fix!
The method that is called through addChannelzTraceEvent is AddTraceEvent:

// AddTraceEvent adds trace related to the entity with specified id, using the
// provided TraceEventDesc.
//
// If channelz is not turned ON, this will simply log the event descriptions.
func AddTraceEvent(l grpclog.DepthLoggerV2, e Entity, depth int, desc *TraceEvent) {
// Log only the trace description associated with the bottom most entity.
d := fmt.Sprintf("[%s]%s", e, desc.Desc)
switch desc.Severity {
case CtUnknown, CtInfo:
l.InfoDepth(depth+1, d)
case CtWarning:
l.WarningDepth(depth+1, d)
case CtError:
l.ErrorDepth(depth+1, d)
}
if getMaxTraceEntry() == 0 {
return
}
if IsOn() {
db.traceEvent(e.id(), desc)
}
}

AddTraceEvent logs the event even if channelz is disabled. To retain the same behaviour, you should combine the conditions from both the PRs: if !logger.V(0) && !channelz.IsOn() before returning early.

@arjan-bal arjan-bal assigned jsm and unassigned arjan-bal Jul 30, 2024
@arjan-bal arjan-bal added Type: Performance Performance improvements (CPU, network, memory, etc) and removed Type: Bug labels Jul 30, 2024
…&& !channelz.IsOn()

Large and frequent calls to this method in UpdateState can cause an extremely high usage of CPU time and Memory
due to underlying json.Marshal calls that are not used or needed if channelz is not on and  were not Info level logging as it is.
Copy link
Contributor

@arjan-bal arjan-bal left a comment

Choose a reason for hiding this comment

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

LGTM, adding another reviewer for a second approval.

@arjan-bal arjan-bal assigned dfawley and unassigned jsm Jul 30, 2024
@arjan-bal arjan-bal requested a review from dfawley July 30, 2024 18:14
@arjan-bal arjan-bal changed the title resolver_wrapper: early return addChannelzTraceEvent on !logger.V(0) resolver_wrapper: early return in addChannelzTraceEvent Jul 30, 2024
Copy link
Member

@dfawley dfawley left a comment

Choose a reason for hiding this comment

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

Logging is typically enabled. Maybe we should also be looking to remove the use of pretty.ToJSON for the purpose of printing this struct? Would %+v or %#v not work just as well?

@dfawley dfawley changed the title resolver_wrapper: early return in addChannelzTraceEvent resolver_wrapper: add early return in addChannelzTraceEvent Jul 30, 2024
@dfawley dfawley merged commit 1b1230b into grpc:master Jul 30, 2024
13 checks passed
@jsm
Copy link
Contributor Author

jsm commented Aug 5, 2024

Would %+v or %#v not work just as well?

I looked into this. The format call is still made before any checks to the log level or if channelz is on. We would have to refactor how channelz works for it to return before doing any format calls.

It could prevent the json Marshalling expense, but I'm not sure if %+v or %#v are particularly cheaper?

@dfawley
Copy link
Member

dfawley commented Aug 5, 2024

I suspect %v is faster than JSON marshalling, but I haven't benchmarked it. It also should be able to avoid any formatting unless the logging is actually happening.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 2, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Type: Performance Performance improvements (CPU, network, memory, etc)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants