Skip to content

Commit 6d136d8

Browse files
committed
rangefeed: reuse annotated context in ScheduledProcessor.process()
Context construction is expensive enough to show up in CPU profiles. With 20k rangefeeds/node on an idle cluster, this made up 1% of overall CPU usage, or 4% of rangefeed scheduler CPU usage. Epic: none Release note: None
1 parent d773874 commit 6d136d8

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

pkg/kv/kvserver/rangefeed/scheduled_processor.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ type ScheduledProcessor struct {
4444
reg registry
4545
rts resolvedTimestamp
4646

47+
// processCtx is the annotated background context used for process(). It is
48+
// stored here to avoid reconstructing it on every call.
49+
processCtx context.Context
4750
requestQueue chan request
4851
eventC chan *event
4952
// If true, processor is not processing data anymore and waiting for registrations
@@ -65,10 +68,11 @@ func NewScheduledProcessor(cfg Config) *ScheduledProcessor {
6568
cfg.SetDefaults()
6669
cfg.AmbientContext.AddLogTag("rangefeed", nil)
6770
p := &ScheduledProcessor{
68-
Config: cfg,
69-
scheduler: NewClientScheduler(cfg.Scheduler),
70-
reg: makeRegistry(cfg.Metrics),
71-
rts: makeResolvedTimestamp(),
71+
Config: cfg,
72+
scheduler: NewClientScheduler(cfg.Scheduler),
73+
reg: makeRegistry(cfg.Metrics),
74+
rts: makeResolvedTimestamp(),
75+
processCtx: cfg.AmbientContext.AnnotateCtx(context.Background()),
7276

7377
requestQueue: make(chan request, 20),
7478
eventC: make(chan *event, cfg.EventChanCap),
@@ -121,7 +125,7 @@ func (p *ScheduledProcessor) Start(
121125
// process is a scheduler callback that is processing scheduled events and
122126
// requests.
123127
func (p *ScheduledProcessor) process(e processorEventType) processorEventType {
124-
ctx := p.Config.AmbientContext.AnnotateCtx(context.Background())
128+
ctx := p.processCtx
125129
if e&RequestQueued != 0 {
126130
p.processRequests(ctx)
127131
}

0 commit comments

Comments
 (0)