Skip to content

fix(fileservice): prevent expensive S3 range fan-out - #26758

Merged
mergify[bot] merged 7 commits into
matrixorigin:mainfrom
LeftHandCold:fix/issue-26172-cost-aware-s3-fallback
Aug 7, 2026
Merged

mergify[bot] merged 7 commits into
matrixorigin:mainfrom
LeftHandCold:fix/issue-26172-cost-aware-s3-fallback

Conversation

@LeftHandCold

@LeftHandCold LeftHandCold commented Aug 6, 2026 •

Copy link
Copy Markdown
Collaborator

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

fixes #26172

What this PR does / why we need it:

Root cause

When a full-object disk-cache fill is still running, followers wait only 200ms and then use readMinimalRange. For a multi-column IOVector, that function reads one continuous [min offset, max end] envelope rather than the logical entry bytes. Sparse columns can therefore turn a few MiB of requested data into a 100+ MiB S3 GET per follower. Under high concurrency this bypasses the existing I/O merge, saturates the 100-connection HTTP pool, and amplifies heap/network pressure until transactions and client connections time out.

Fix

  • Compute the logical bytes, span bytes, and amplification of unfinished entries before bypassing a full-object merge.
  • Preserve the existing 200ms fallback for single entries, small spans, and low-amplification ranges.
  • Record on each active merger generation whether its actual leader can publish a reusable full-object disk-cache artifact; followers use the capability of the exact generation they joined, rather than inferring it from their own vector.
  • When a cache-producing full-object generation is active and the fallback span is larger than 8MiB with amplification above 8x, wait for that generation instead of issuing another broad range GET.
  • Bound this second-stage wait with both the caller context and the existing one-minute I/O merge limit.
  • If the leader is not cache-producing, or the local limit expires, read unfinished entries as sequential exact ranges instead of one large sparse envelope. The timeout path rechecks caches once first.
  • Record the long wait in existing S3FS/IOMerger timing metrics and slow-event details.

Safety boundaries

  • No change to on-disk/wire formats, configuration defaults, cache-hit paths, or reads without merge contention.
  • No new goroutine, persistent per-object state, queue, semaphore, or retry loop.
  • The cache-producer capability is immutable and scoped to one merger generation. Completion checks and second-stage waits remain bound to that same generation even if a new leader starts for the key.
  • WriterForRead and ReadCloserForRead leaders do not extend follower waits because they cannot publish the shared full-file cache result.
  • Leader completion, caller cancellation, and the local wait limit are independent terminal events.
  • A wait timeout only changes the current follower's local read mode. It does not cancel, delete, or otherwise mutate the active merge generation.
  • Fallback is sequential and fetches each unfinished entry's exact range, preserving follower progress without recreating the large envelope.

Regression evidence

Three deterministic regressions cover the expensive sparse branch:

  • On clean base/main, a held full-object merge causes the sparse follower to issue one extra broad ObjectStorage read before its caller deadline (expected 0, actual 1). The patch keeps the remote read count at zero until cancellation.
  • On the first PR head, a no-deadline caller remains blocked behind a held cache-producing leader and fails a one-second progress guard. The final patch reaches the bounded fallback and issues exactly [0,1) and [9MiB,9MiB+1), never the 9MiB envelope.
  • On the previous PR head, a real WriterForRead leader blocked in its writer causes an ordinary sparse follower to lose its deadline without issuing a read. The final patch identifies that generation as non-cache-producing and performs exactly the two requested range reads after the original short wait.

Verified on final commit:

  • GOWORK=off go build -mod=readonly ./pkg/fileservice
  • GOWORK=off go vet -mod=readonly ./pkg/fileservice
  • mo-cgo-test -count=1 -timeout=300s ./pkg/fileservice
  • Relevant merger and sparse-range tests with -race -count=100, including generation ownership, cancellation, no-deadline fallback, non-cache-producing leaders, and historical cheap-range fallback
  • Full pkg/fileservice package with -race -count=1

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Requesting changes for one liveness regression.

[P1] Bound the second-stage full-object merge wait independently of caller deadlines.

After the existing Merge(..., shortIOWaitDuration) returns at about 200 ms, the new expensive-range branch calls waitContext(ctx, mergeKey). That helper has no local bound: it returns only when the leader closes its merge channel or ctx.Done() fires. FileService accepts valid contexts with no deadline (for example context.Background()). If the leader's full-object remote read stalls, a sparse follower that previously fell back to its own range read after the bounded wait can now block forever behind the same stalled data path.

The new regression test supplies a 100 ms context deadline, so it proves cancellation propagation but does not cover the no-deadline liveness regression.

Please preserve a bounded follower-progress contract while still avoiding the large sparse envelope/fan-out (the exact fallback/admission design can remain local). Add a stuck-leader regression using a no-deadline caller context and prove the follower reaches a bounded fallback without issuing the huge envelope.

@LeftHandCold

Copy link
Copy Markdown
Collaborator Author

已确认该 review 指出的是实际可达的 P1 活性问题,并已在 05a8a051d6 修复:

  • 第二阶段 follower 等待同时受 caller context 和现有 maxIOWaitDuration(1 分钟)约束;
  • 本地等待超时后先重新检查 memory/disk/remote cache 一次,覆盖 leader 恰好完成的竞态;
  • cache 仍未命中时绕过当前 merger generation,按每个 IOEntry 的精确 range 顺序读取,不再退回大范围 envelope;
  • 超时路径不会取消、删除或修改仍在执行的 leader,也不新增 goroutine、队列或共享生命周期状态。

新增 context.Background() 无 deadline 的确定性回归:修复前稳定在 1 秒 guard 超时;修复后约 10ms 进入 fallback,并只产生两个精确 range 请求。相关 focused test、-race -count=100、全包 test/race、build/vet 均通过。

@LeftHandCold
LeftHandCold requested a review from XuPeng-SH August 6, 2026 14:31
Comment thread pkg/fileservice/s3_fs.go Resolved

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The previous no-deadline liveness issue is bounded in the new commit, and the timeout path itself has bounded timer, connection, and goroutine ownership. One blocking producer/consumer classification issue remains: the second-stage wait is not restricted to a merge generation that can actually populate the cache the follower plans to reuse. See the inline P1 and the deterministic WriterForRead counterexample.

@mergify mergify Bot added the queued label Aug 7, 2026
@mergify

mergify Bot commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-07 08:49 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-08-07 09:41 UTC · at 12b7fd454ff70a7257769886ad73b02cc912db78 · squash

This pull request spent 51 minutes 28 seconds in the queue, including 41 minutes 10 seconds running CI.

Required conditions to merge
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Linux/arm64
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86

@mergify
mergify Bot merged commit 47c8302 into matrixorigin:main Aug 7, 2026
24 of 25 checks passed
@mergify mergify Bot removed the queued label Aug 7, 2026
mergify Bot pushed a commit that referenced this pull request Aug 9, 2026
Backport the complete FileService range fan-out fix to `4.2-dev`.

`4.2-dev` contains the range-bypass and 200 ms fallback introduced by #24759
and #24871, but does not contain #26758. Therefore backporting only the
single-entry follow-up from #26834 would leave the multi-entry sparse-range
fan-out unchanged. This PR applies both fixes in dependency order:

1. #26758: avoid wide sparse fallback reads and bind waiters to the exact
cache-producing merge generation.
2. #26834: coalesce duplicate single-entry exact-range fallbacks after the
full-object generation exceeds its short wait.

The normal cache-hit and uncontended read paths are unchanged. All waits remain
caller-cancelable and bounded. Merge generations have no background goroutine
or persistent queue and are deleted on leader completion. Followers recheck
cache publication before falling back to a bounded exact read.

Validation on the `4.2-dev` base (`4066fffaf7`):

- focused FileService regression set, `-count=3`
- six concurrency/lifecycle tests under `-race` (`20` or `100` repetitions)
- full `pkg/fileservice` test
- full `pkg/fileservice` race test
- `go build` and `go vet` for `pkg/fileservice`

Backported commits retain `-x` provenance for #26758 and #26834.

Approved by: @XuPeng-SH
XuPeng-SH pushed a commit that referenced this pull request Aug 9, 2026
## What type of PR is this?

- [ ] API-change
- [x] BUG
- [x] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue #26172

Follow-up to #26758.

## What this PR does / why we need it:

### Root cause

#26758 prevents a stalled full-object cache fill from turning sparse
multi-entry reads into large amplified S3 ranges. It intentionally
preserves the 200ms fallback for a single bounded entry.

That remaining path bypasses the I/O merger. When many requests need the
same entry while the full-object fill is still running, every follower
starts the same exact S3 range GET after 200ms. A small hot range can
therefore fan out across the object-storage connection pool and increase
network, heap, and query latency pressure.

### Fix

- After the existing 200ms full-object wait expires, create a second
merger generation keyed by the exact single-entry range.
- Admit this merge only when the leader can publish a reusable
memory-cache or per-entry disk-cache result.
- Bind followers to the exact generation and wait at most the existing
one-minute I/O limit, also respecting the caller context.
- Recheck caches after leader completion. If publication fails or the
wait expires, continue with the existing exact-range fallback without
rejoining either merge generation.
- Keep leader completion ordered after deferred cache updates, so
followers cannot wake before the reusable result is published.

### Safety boundaries

- Cache hits, uncontended reads, multi-entry reads, stream reads, and
no-cache-write reads are unchanged.
- Different ranges use different merge keys and proceed independently.
- No goroutine, queue, retry loop, wire format, disk format, or
configuration is added.
- Cancellation and the existing local I/O wait limit remain terminal
bounds.
- Cache admission failure remains a normal cache miss and falls back to
one exact range read.

### Tests

- A deterministic blocked-storage test starts 16 same-range followers
and proves they do not issue duplicate object-storage reads, honor
caller cancellation, and reuse the leader's published cache result.
- A second deterministic test proves different exact ranges do not block
each other.
- Focused tests passed with `-race -count=20`.
- Full `pkg/fileservice` tests and full package race tests passed.
- `go build`, `go vet`, and `git diff --check` passed.

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
)

### Root cause

When a full-object disk-cache fill is still running, followers wait only 200ms and then use `readMinimalRange`. For a multi-column `IOVector`, that function reads one continuous `[min offset, max end]` envelope rather than the logical entry bytes. Sparse columns can therefore turn a few MiB of requested data into a 100+ MiB S3 GET per follower. Under high concurrency this bypasses the existing I/O merge, saturates the 100-connection HTTP pool, and amplifies heap/network pressure until transactions and client connections time out.

### Fix

- Compute the logical bytes, span bytes, and amplification of unfinished entries before bypassing a full-object merge.
- Preserve the existing 200ms fallback for single entries, small spans, and low-amplification ranges.
- Record on each active merger generation whether its actual leader can publish a reusable full-object disk-cache artifact; followers use the capability of the exact generation they joined, rather than inferring it from their own vector.
- When a cache-producing full-object generation is active and the fallback span is larger than 8MiB with amplification above 8x, wait for that generation instead of issuing another broad range GET.
- Bound this second-stage wait with both the caller context and the existing one-minute I/O merge limit.
- If the leader is not cache-producing, or the local limit expires, read unfinished entries as sequential exact ranges instead of one large sparse envelope. The timeout path rechecks caches once first.
- Record the long wait in existing S3FS/IOMerger timing metrics and slow-event details.

### Safety boundaries

- No change to on-disk/wire formats, configuration defaults, cache-hit paths, or reads without merge contention.
- No new goroutine, persistent per-object state, queue, semaphore, or retry loop.
- The cache-producer capability is immutable and scoped to one merger generation. Completion checks and second-stage waits remain bound to that same generation even if a new leader starts for the key.
- `WriterForRead` and `ReadCloserForRead` leaders do not extend follower waits because they cannot publish the shared full-file cache result.
- Leader completion, caller cancellation, and the local wait limit are independent terminal events.
- A wait timeout only changes the current follower's local read mode. It does not cancel, delete, or otherwise mutate the active merge generation.
- Fallback is sequential and fetches each unfinished entry's exact range, preserving follower progress without recreating the large envelope.

### Regression evidence

Three deterministic regressions cover the expensive sparse branch:

- On clean `base/main`, a held full-object merge causes the sparse follower to issue one extra broad ObjectStorage read before its caller deadline (`expected 0, actual 1`). The patch keeps the remote read count at zero until cancellation.
- On the first PR head, a no-deadline caller remains blocked behind a held cache-producing leader and fails a one-second progress guard. The final patch reaches the bounded fallback and issues exactly `[0,1)` and `[9MiB,9MiB+1)`, never the 9MiB envelope.
- On the previous PR head, a real `WriterForRead` leader blocked in its writer causes an ordinary sparse follower to lose its deadline without issuing a read. The final patch identifies that generation as non-cache-producing and performs exactly the two requested range reads after the original short wait.

Verified on final commit:

- `GOWORK=off go build -mod=readonly ./pkg/fileservice`
- `GOWORK=off go vet -mod=readonly ./pkg/fileservice`
- `mo-cgo-test -count=1 -timeout=300s ./pkg/fileservice`
- Relevant merger and sparse-range tests with `-race -count=100`, including generation ownership, cancellation, no-deadline fallback, non-cache-producing leaders, and historical cheap-range fallback
- Full `pkg/fileservice` package with `-race -count=1`

Approved by: @XuPeng-SH
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…igin#26834)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [x] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#26172

Follow-up to matrixorigin#26758.

## What this PR does / why we need it:

### Root cause

matrixorigin#26758 prevents a stalled full-object cache fill from turning sparse
multi-entry reads into large amplified S3 ranges. It intentionally
preserves the 200ms fallback for a single bounded entry.

That remaining path bypasses the I/O merger. When many requests need the
same entry while the full-object fill is still running, every follower
starts the same exact S3 range GET after 200ms. A small hot range can
therefore fan out across the object-storage connection pool and increase
network, heap, and query latency pressure.

### Fix

- After the existing 200ms full-object wait expires, create a second
merger generation keyed by the exact single-entry range.
- Admit this merge only when the leader can publish a reusable
memory-cache or per-entry disk-cache result.
- Bind followers to the exact generation and wait at most the existing
one-minute I/O limit, also respecting the caller context.
- Recheck caches after leader completion. If publication fails or the
wait expires, continue with the existing exact-range fallback without
rejoining either merge generation.
- Keep leader completion ordered after deferred cache updates, so
followers cannot wake before the reusable result is published.

### Safety boundaries

- Cache hits, uncontended reads, multi-entry reads, stream reads, and
no-cache-write reads are unchanged.
- Different ranges use different merge keys and proceed independently.
- No goroutine, queue, retry loop, wire format, disk format, or
configuration is added.
- Cancellation and the existing local I/O wait limit remain terminal
bounds.
- Cache admission failure remains a normal cache miss and falls back to
one exact range read.

### Tests

- A deterministic blocked-storage test starts 16 same-range followers
and proves they do not issue duplicate object-storage reads, honor
caller cancellation, and reuse the leader's published cache result.
- A second deterministic test proves different exact ranges do not block
each other.
- Focused tests passed with `-race -count=20`.
- Full `pkg/fileservice` tests and full package race tests passed.
- `go build`, `go vet`, and `git diff --check` passed.

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
)

### Root cause

When a full-object disk-cache fill is still running, followers wait only 200ms and then use `readMinimalRange`. For a multi-column `IOVector`, that function reads one continuous `[min offset, max end]` envelope rather than the logical entry bytes. Sparse columns can therefore turn a few MiB of requested data into a 100+ MiB S3 GET per follower. Under high concurrency this bypasses the existing I/O merge, saturates the 100-connection HTTP pool, and amplifies heap/network pressure until transactions and client connections time out.

### Fix

- Compute the logical bytes, span bytes, and amplification of unfinished entries before bypassing a full-object merge.
- Preserve the existing 200ms fallback for single entries, small spans, and low-amplification ranges.
- Record on each active merger generation whether its actual leader can publish a reusable full-object disk-cache artifact; followers use the capability of the exact generation they joined, rather than inferring it from their own vector.
- When a cache-producing full-object generation is active and the fallback span is larger than 8MiB with amplification above 8x, wait for that generation instead of issuing another broad range GET.
- Bound this second-stage wait with both the caller context and the existing one-minute I/O merge limit.
- If the leader is not cache-producing, or the local limit expires, read unfinished entries as sequential exact ranges instead of one large sparse envelope. The timeout path rechecks caches once first.
- Record the long wait in existing S3FS/IOMerger timing metrics and slow-event details.

### Safety boundaries

- No change to on-disk/wire formats, configuration defaults, cache-hit paths, or reads without merge contention.
- No new goroutine, persistent per-object state, queue, semaphore, or retry loop.
- The cache-producer capability is immutable and scoped to one merger generation. Completion checks and second-stage waits remain bound to that same generation even if a new leader starts for the key.
- `WriterForRead` and `ReadCloserForRead` leaders do not extend follower waits because they cannot publish the shared full-file cache result.
- Leader completion, caller cancellation, and the local wait limit are independent terminal events.
- A wait timeout only changes the current follower's local read mode. It does not cancel, delete, or otherwise mutate the active merge generation.
- Fallback is sequential and fetches each unfinished entry's exact range, preserving follower progress without recreating the large envelope.

### Regression evidence

Three deterministic regressions cover the expensive sparse branch:

- On clean `base/main`, a held full-object merge causes the sparse follower to issue one extra broad ObjectStorage read before its caller deadline (`expected 0, actual 1`). The patch keeps the remote read count at zero until cancellation.
- On the first PR head, a no-deadline caller remains blocked behind a held cache-producing leader and fails a one-second progress guard. The final patch reaches the bounded fallback and issues exactly `[0,1)` and `[9MiB,9MiB+1)`, never the 9MiB envelope.
- On the previous PR head, a real `WriterForRead` leader blocked in its writer causes an ordinary sparse follower to lose its deadline without issuing a read. The final patch identifies that generation as non-cache-producing and performs exactly the two requested range reads after the original short wait.

Verified on final commit:

- `GOWORK=off go build -mod=readonly ./pkg/fileservice`
- `GOWORK=off go vet -mod=readonly ./pkg/fileservice`
- `mo-cgo-test -count=1 -timeout=300s ./pkg/fileservice`
- Relevant merger and sparse-range tests with `-race -count=100`, including generation ownership, cancellation, no-deadline fallback, non-cache-producing leaders, and historical cheap-range fallback
- Full `pkg/fileservice` package with `-race -count=1`

Approved by: @XuPeng-SH
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…igin#26834)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [x] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#26172

Follow-up to matrixorigin#26758.

## What this PR does / why we need it:

### Root cause

matrixorigin#26758 prevents a stalled full-object cache fill from turning sparse
multi-entry reads into large amplified S3 ranges. It intentionally
preserves the 200ms fallback for a single bounded entry.

That remaining path bypasses the I/O merger. When many requests need the
same entry while the full-object fill is still running, every follower
starts the same exact S3 range GET after 200ms. A small hot range can
therefore fan out across the object-storage connection pool and increase
network, heap, and query latency pressure.

### Fix

- After the existing 200ms full-object wait expires, create a second
merger generation keyed by the exact single-entry range.
- Admit this merge only when the leader can publish a reusable
memory-cache or per-entry disk-cache result.
- Bind followers to the exact generation and wait at most the existing
one-minute I/O limit, also respecting the caller context.
- Recheck caches after leader completion. If publication fails or the
wait expires, continue with the existing exact-range fallback without
rejoining either merge generation.
- Keep leader completion ordered after deferred cache updates, so
followers cannot wake before the reusable result is published.

### Safety boundaries

- Cache hits, uncontended reads, multi-entry reads, stream reads, and
no-cache-write reads are unchanged.
- Different ranges use different merge keys and proceed independently.
- No goroutine, queue, retry loop, wire format, disk format, or
configuration is added.
- Cancellation and the existing local I/O wait limit remain terminal
bounds.
- Cache admission failure remains a normal cache miss and falls back to
one exact range read.

### Tests

- A deterministic blocked-storage test starts 16 same-range followers
and proves they do not issue duplicate object-storage reads, honor
caller cancellation, and reuse the leader's published cache result.
- A second deterministic test proves different exact ranges do not block
each other.
- Focused tests passed with `-race -count=20`.
- Full `pkg/fileservice` tests and full package race tests passed.
- `go build`, `go vet`, and `git diff --check` passed.

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
)

### Root cause

When a full-object disk-cache fill is still running, followers wait only 200ms and then use `readMinimalRange`. For a multi-column `IOVector`, that function reads one continuous `[min offset, max end]` envelope rather than the logical entry bytes. Sparse columns can therefore turn a few MiB of requested data into a 100+ MiB S3 GET per follower. Under high concurrency this bypasses the existing I/O merge, saturates the 100-connection HTTP pool, and amplifies heap/network pressure until transactions and client connections time out.

### Fix

- Compute the logical bytes, span bytes, and amplification of unfinished entries before bypassing a full-object merge.
- Preserve the existing 200ms fallback for single entries, small spans, and low-amplification ranges.
- Record on each active merger generation whether its actual leader can publish a reusable full-object disk-cache artifact; followers use the capability of the exact generation they joined, rather than inferring it from their own vector.
- When a cache-producing full-object generation is active and the fallback span is larger than 8MiB with amplification above 8x, wait for that generation instead of issuing another broad range GET.
- Bound this second-stage wait with both the caller context and the existing one-minute I/O merge limit.
- If the leader is not cache-producing, or the local limit expires, read unfinished entries as sequential exact ranges instead of one large sparse envelope. The timeout path rechecks caches once first.
- Record the long wait in existing S3FS/IOMerger timing metrics and slow-event details.

### Safety boundaries

- No change to on-disk/wire formats, configuration defaults, cache-hit paths, or reads without merge contention.
- No new goroutine, persistent per-object state, queue, semaphore, or retry loop.
- The cache-producer capability is immutable and scoped to one merger generation. Completion checks and second-stage waits remain bound to that same generation even if a new leader starts for the key.
- `WriterForRead` and `ReadCloserForRead` leaders do not extend follower waits because they cannot publish the shared full-file cache result.
- Leader completion, caller cancellation, and the local wait limit are independent terminal events.
- A wait timeout only changes the current follower's local read mode. It does not cancel, delete, or otherwise mutate the active merge generation.
- Fallback is sequential and fetches each unfinished entry's exact range, preserving follower progress without recreating the large envelope.

### Regression evidence

Three deterministic regressions cover the expensive sparse branch:

- On clean `base/main`, a held full-object merge causes the sparse follower to issue one extra broad ObjectStorage read before its caller deadline (`expected 0, actual 1`). The patch keeps the remote read count at zero until cancellation.
- On the first PR head, a no-deadline caller remains blocked behind a held cache-producing leader and fails a one-second progress guard. The final patch reaches the bounded fallback and issues exactly `[0,1)` and `[9MiB,9MiB+1)`, never the 9MiB envelope.
- On the previous PR head, a real `WriterForRead` leader blocked in its writer causes an ordinary sparse follower to lose its deadline without issuing a read. The final patch identifies that generation as non-cache-producing and performs exactly the two requested range reads after the original short wait.

Verified on final commit:

- `GOWORK=off go build -mod=readonly ./pkg/fileservice`
- `GOWORK=off go vet -mod=readonly ./pkg/fileservice`
- `mo-cgo-test -count=1 -timeout=300s ./pkg/fileservice`
- Relevant merger and sparse-range tests with `-race -count=100`, including generation ownership, cancellation, no-deadline fallback, non-cache-producing leaders, and historical cheap-range fallback
- Full `pkg/fileservice` package with `-race -count=1`

Approved by: @XuPeng-SH
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…igin#26834)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [x] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#26172

Follow-up to matrixorigin#26758.

## What this PR does / why we need it:

### Root cause

matrixorigin#26758 prevents a stalled full-object cache fill from turning sparse
multi-entry reads into large amplified S3 ranges. It intentionally
preserves the 200ms fallback for a single bounded entry.

That remaining path bypasses the I/O merger. When many requests need the
same entry while the full-object fill is still running, every follower
starts the same exact S3 range GET after 200ms. A small hot range can
therefore fan out across the object-storage connection pool and increase
network, heap, and query latency pressure.

### Fix

- After the existing 200ms full-object wait expires, create a second
merger generation keyed by the exact single-entry range.
- Admit this merge only when the leader can publish a reusable
memory-cache or per-entry disk-cache result.
- Bind followers to the exact generation and wait at most the existing
one-minute I/O limit, also respecting the caller context.
- Recheck caches after leader completion. If publication fails or the
wait expires, continue with the existing exact-range fallback without
rejoining either merge generation.
- Keep leader completion ordered after deferred cache updates, so
followers cannot wake before the reusable result is published.

### Safety boundaries

- Cache hits, uncontended reads, multi-entry reads, stream reads, and
no-cache-write reads are unchanged.
- Different ranges use different merge keys and proceed independently.
- No goroutine, queue, retry loop, wire format, disk format, or
configuration is added.
- Cancellation and the existing local I/O wait limit remain terminal
bounds.
- Cache admission failure remains a normal cache miss and falls back to
one exact range read.

### Tests

- A deterministic blocked-storage test starts 16 same-range followers
and proves they do not issue duplicate object-storage reads, honor
caller cancellation, and reuse the leader's published cache result.
- A second deterministic test proves different exact ranges do not block
each other.
- Focused tests passed with `-race -count=20`.
- Full `pkg/fileservice` tests and full package race tests passed.
- `go build`, `go vet`, and `git diff --check` passed.

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working kind/enhancement size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] sysbench INSERT DUPLICATE 高并发路径内存分配(1000 并发 heap +16G 导致连接超时)

3 participants