Repository navigation
fix: throttle CN S3 writes under memory pressure - #24268
Conversation
Add RSS-aware CNFlushS3 throttling and flush S3 insert writers when the CN memory budget is exhausted. Co-authored-by: Copilot <[email protected]>
Review Summary by QodoThrottle CN S3 writes under memory pressure with RSS-aware limits
WalkthroughsDescription• Add RSS-aware hard-limit checks to CN S3 memory throttling policy • Implement forced refresh path for current RSS memory checks • Reserve memory against throttler before writing S3 batches • Flush S3 writer state when memory budget exhausted, retry once • Expose helper method for safe partial S3 writer flushes Diagramflowchart LR
A["S3 Insert Write"] --> B["acquireS3WriteMemory"]
B --> C["ForceRefresh Throttler"]
C --> D["Acquire Memory"]
D -->|Success| E["Write Batch"]
D -->|Fail| F["flushS3WriterOnMemoryPressure"]
F --> G["SyncAndFillBlockInfoBat"]
G --> H["Release Memory"]
H --> I["Retry Acquire"]
I -->|Success| E
E --> J["Track s3MemGranted"]
J --> K["Release on Completion"]
File Changes1. pkg/common/rscthrottler/resource_throttler.go
|
Code Review by Qodo
1. S3 memory grant leaks
|
There was a problem hiding this comment.
Pull request overview
This PR introduces tighter CN-side memory backpressure for the “write-to-S3 from CN” insert/load path to reduce OOM risk under high RSS scenarios (e.g., large LOAD DATA / INSERT SELECT into wide rows).
Changes:
- Adds RSS-aware hard-limit logic (including already reserved memory) to the CNFlushS3 memory throttler, plus a forced-refresh option.
- Makes
insert.ToWriteS3reserve CNFlushS3 memory per input batch and (on pressure) partially flushes S3 writer state, appends block-info rows, releases the reservation, and retries. - Adds
CNS3Writer.SyncAndFillBlockInfoBatto support partial flushes while preserving insert result semantics.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/sql/colexec/s3util.go | Adds SyncAndFillBlockInfoBat helper for partial S3 writer flushes that produce block-info batches. |
| pkg/sql/colexec/insert/types.go | Tracks and releases CNFlushS3 memory reservations on Reset/Free. |
| pkg/sql/colexec/insert/insert.go | Reserves throttler memory before S3 writes; flushes writer on pressure; adds forced refresh support. |
| pkg/sql/colexec/insert/insert_test.go | Adds unit tests for the new S3 write memory acquisition/retry behavior. |
| pkg/common/rscthrottler/resource_throttler.go | Adds ForceRefresh and RSS-aware acquire policy for CNFlushS3. |
| pkg/common/rscthrottler/resource_throttler_test.go | Adds tests covering the new CNFlushS3 RSS-limit acquisition behavior. |
Comments suppressed due to low confidence (1)
pkg/common/rscthrottler/resource_throttler.go:165
ForceRefresh()currently runs the fullrefresh(true)logic (total memory + cgroup + process RSS) and emits an Info log on every call. With the new S3 path calling forced refreshes frequently, this can become extremely expensive and noisy. Consider makingForceRefreshcheaper (e.g., refresh RSS only) and/or keeping the logging and heavyweight total/cgroup refresh behind an interval even when forced.
func (m *memThrottler) Refresh() {
m.refresh(false)
}
func (m *memThrottler) ForceRefresh() {
m.refresh(true)
}
func (m *memThrottler) refresh(force bool) {
last := m.lastRefresh.Load()
now := time.Now().UnixNano()
if !force && time.Duration(now-last) <= refreshMaxInterval {
return
}
m.lastRefresh.Store(now)
var (
err error
cgroup uint64
info *process.MemoryInfoStat
total uint64
)
defer func() {
logutil.Info(
fmt.Sprintf("%s-Refresh", MemoryThrottlerLogHeader),
zap.String("detail", m.String()),
zap.Error(err),
)
}()
total = objectio.TotalMem()
cgroup, err = memlimit.FromCgroup()
if cgroup != 0 && cgroup < total {
m.actualTotalMemory.Store(cgroup)
} else if total != 0 {
m.actualTotalMemory.Store(total)
} else {
m.actualTotalMemory.Store(math.MaxInt64)
logutil.Info(
fmt.Sprintf("%s-Refresh", MemoryThrottlerLogHeader),
zap.String("err", "cannot get the total memory, unlimited"),
)
}
// if the const limit option is set, we should not change the limit.
if m.options.constLimit < 0 {
m.limit.Store(int64(float64(m.actualTotalMemory.Load()) * m.limitRate))
}
if m.proc == nil {
m.proc, _ = process.NewProcess(int32(os.Getpid()))
}
if info, err = m.proc.MemoryInfo(); err == nil {
m.rss.Store(int64(info.RSS))
}
m.cgroup.Store(cgroup)
m.total.Store(total)
}
Avoid reserving full input batches for normal CN S3 writes and only force-refresh RSS after acquire misses. Keep pipeline flush conservatively accounted because async sink jobs can retain multiple staged batches.\n\nCo-authored-by: Copilot <[email protected]>
Merge Queue Status
This pull request spent 15 seconds in the queue, including 2 seconds running CI. Required conditions to merge
|
What type of PR is this?
Which issue(s) this PR fixes:
Fixes #24267
What this PR does / why we need it:
This PR reduces CN OOM risk for large
LOAD DATA/INSERT SELECTworkloads that write through the CN S3 path.The observed OOM case loaded a large CSV with
PARALLEL 'TRUE'and immediately copied the data into another wide table containingvecf32(1024),text, andjsoncolumns. The CN RSS crossed the CNFlushS3 soft budget and approached the cgroup limit whilepinnedmemory remained low, which means the normalinsert.ToWriteS3path was not applying enough backpressure before feeding batches intoCNS3DataWriter.Changes:
AcquirePolicyForCNFlushS3, including already reserved memory.insert.ToWriteS3reserve memory againstCNMemoryThrottlerbefore writing each batch.CNS3Writer.SyncAndFillBlockInfoBathelper to support safe partial flushes without changing final insert result semantics.Testing
go test ./pkg/common/rscthrottler ./pkg/sql/colexec/insert ./pkg/sql/colexec ./pkg/objectio/ioutil -count=1