Skip to content

feat(metrics): expose S3 pool, S3 command, merge, lease and download metrics - #19

Closed
chenrui333 wants to merge 7 commits into
review-base/upstream-devfrom
perf/storage-metrics
Closed

chenrui333 wants to merge 7 commits into
review-base/upstream-devfrom
perf/storage-metrics

Conversation

@chenrui333

Copy link
Copy Markdown
Owner

Review copy of a proposed upstream change, based on falcondev-oss/github-actions-cache-server@dev (6ae06c2). Upstream accepts pull requests from collaborators only, so this branch is offered through an upstream issue.

Stacked on the pump, S3 pool, pool split, merge cap, idle timeout and client-gone branches (this PR shows the whole stack; the last commit is the metrics change).

Pool starvation, merge contention and client behaviour are invisible on /metrics. Adds low-cardinality series for S3 pool state, socket wait and per-command outcome/latency, merges (finished, skipped, in progress, oldest age), lease rows, download outcomes and lookup latency. Nothing is recorded per chunk.

The first download of an unmerged entry tees its parts into the client
response and the background merge upload. A destroyed stream returns false
from write() and never emits 'drain' (nor 'error' when destroyed without
one), so the pump parks forever when:

- the merge upload fails: `download()` destroys the merger stream, and the
  client's download stalls mid-body until the client times out;
- the client hangs up while the pump waits on the merger: the next write to
  the destroyed response stream parks the pump, the merge never completes,
  and its lease keeps renewing.

Either way the current part's backend read stays open (one S3 socket per
occurrence), so enough of them fill the S3 socket pool.

Wait for 'drain' or 'close', skip destroyed sinks, keep feeding the one that
is still open, and stop reading once both are gone so `streamParts` destroys
the current part stream.

The regression tests fail on the previous pump with the filesystem and S3
drivers: the merge never settles after the client leaves, the client never
finishes after the merge upload fails, and the part stream is never
destroyed when both are gone.

Signed-off-by: Rui Chen <rui@chenrui.dev>
…endpoints

The S3 client's agent pool was hard-coded at 50 sockets, and the agent was
passed only as `httpsAgent`. NodeHttpHandler picks the agent by endpoint
scheme, so an http:// endpoint (MinIO, RustFS, an in-cluster gateway)
silently fell back to the SDK's default agent and ignored the configured
pool. Local tests and benchmarks against RustFS therefore never exercised
the pool an https deployment uses.

Add STORAGE_S3_MAX_SOCKETS (default 50, the previous value) and build both
the http and the https agent from the same options.

tests/s3-socket-pool.test.ts holds one GetObject body unread with a pool of
one socket and checks that a second GetObject waits for it; against an
http:// endpoint it did not wait before this change.

Signed-off-by: Rui Chen <rui@chenrui.dev>
All S3 traffic shared one client and one agent pool. A streaming Merge (the
first restore of an unmerged entry, or a streaming Eager Merge) holds a part
GetObject open, paused by backpressure, while lib-storage waits in that same
pool for a socket to upload the merged object. Once concurrent Merges reach
maxSockets, every socket is a paused GET waiting for a socket that never
frees: the pool deadlocks, Merge Leases renew forever, no Merge or restore
completes, and every later S3 call queues behind them. That includes the
HeadObject each cache lookup issues, which also waits behind every bulk
transfer in the agent's FIFO queue even without a deadlock.

Use one S3Client per kind of work:

- reads: GetObject (and presigning) (STORAGE_S3_MAX_SOCKETS)
- writes: PutObject, multipart uploads, UploadPartCopy compose
  (STORAGE_S3_MAX_SOCKETS)
- control: HeadObject, HeadBucket, ListObjectsV2, DeleteObjects
  (STORAGE_S3_CONTROL_MAX_SOCKETS, default 16)

Write-socket holders never wait on reads, and control requests hold a socket
only briefly, so no wait cycle spans pools. The control client also sets
connectionTimeout (STORAGE_S3_CONTROL_QUEUE_TIMEOUT_MS, default 5000), which
bounds the wait for a socket plus the connect, so a starved lookup fails
instead of hanging.

tests/merge-socket-pool.test.ts runs as many concurrent first restores as
pool sockets (4) against RustFS; with the shared pool it never finishes.
tests/s3-socket-pool.test.ts checks that a HeadObject is answered while every
data socket is held by an unread GetObject body.

Signed-off-by: Rui Chen <rui@chenrui.dev>
Every first restore of an unmerged entry and, with EAGER_MERGE, every
finalize started a Merge with no global limit. Each Merge holds storage
connections, a Merge Lease and stream buffers for its whole duration, and a
lazy Merge is paced by the restoring client's read speed. A burst of first
restores or of finalizes therefore grew Merges, sockets and memory without
bound.

Start a Merge only while fewer than MAX_CONCURRENT_MERGES (default 16) run
in this process. Beyond that, startMerge returns false: a restore streams the
parts directly, exactly as when another worker holds the Merge Lease, an
Eager Merge is left to the first download, and a later download merges the
entry. The slot is reserved before the first await, so concurrent callers
cannot overshoot it.

tests/merge-concurrency.test.ts covers a first restore and an Eager Merge
beyond the limit.

Signed-off-by: Rui Chen <rui@chenrui.dev>
Node's HTTP server has no idle timeout for a response in progress and the
download route set none. A client that stays connected but stops reading
parks the pipe forever: the backend read keeps its storage socket, the
Storage Reader Lease keeps renewing, and on the first restore of an
unmerged entry the Merge it paces never finishes and keeps its Merge Lease.
Each such client permanently removes one socket from the storage pool.

Destroy the download when no bytes have moved for DOWNLOAD_IDLE_TIMEOUT_MS
(default 300000, 0 disables). The timer is refreshed on every chunk, so slow
but progressing clients are unaffected. It covers whole and Range responses
and the first restore of an unmerged entry, whose Merge keeps going after the
client's side of the tee is closed.

tests/proxy-backpressure.test.ts parks a client after the first chunk of a
200, a 206 and an unmerged first restore, and checks that the storage read is
released, the response closed, and the Merge completed.

Signed-off-by: Rui Chen <rui@chenrui.dev>
… storage answers

A client can give up on /download while its storage read is still waiting,
for example a GetObject queued behind a saturated socket pool. The body then
arrives for a response that is already closed. `pipeline` rejects a closed
destination with ERR_STREAM_UNABLE_TO_PIPE before it takes ownership of the
source, and the route rethrew without destroying it, so the body pinned its
backend socket and the Storage Reader Lease for the life of the process.
Enough of them fill the pool and every storage call queues.

Check the response before piping and destroy the body if the client is
gone, and destroy it on every error exit of the pipe as well. falcondev-oss#282 and falcondev-oss#291
already cover a client leaving mid-body.

tests/download-client-gone.test.ts holds the storage read until the client
has hung up, then checks that the backend socket and the reader lease are
released; on Node 26.12.1 the socket stayed held before this change.

Signed-off-by: Rui Chen <rui@chenrui.dev>
…metrics

A starved S3 socket pool (paused GetObject sockets, a deep agent queue,
HeadObject lookups timing out, Merge Leases never finishing) was invisible on
/metrics. Add low-cardinality series that separate S3 latency, socket-pool
queueing, Merge contention and client behaviour:

- s3_http_sockets{pool,state}, s3_http_queued_requests{pool},
  s3_http_max_sockets{pool}: agent pool state at scrape time, per pool
  (reads, writes, control).
- s3_socket_wait_seconds{pool}: time a request waited for an agent socket.
- s3_requests_total{command,outcome}, s3_request_duration_seconds{command},
  s3_request_retries_total{command}: per-command outcome and latency from an
  SDK initialize-step middleware (includes retries and socket wait).
- cache_merges_total{kind,result}, cache_merge_duration_seconds{kind,result},
  cache_merges_in_progress, cache_merge_oldest_age_seconds,
  cache_merges_skipped_total (MAX_CONCURRENT_MERGES reached).
- cache_leases{type,state}: Merge and Storage Reader Lease rows, active vs
  expired.
- cache_downloads_total{result}: completed, client_gone, idle_timeout,
  not_found, error.
- cache_lookup_duration_seconds{result}: download-URL lookup latency.

Nothing is recorded per chunk; pool and lease gauges are computed at scrape.
The registry stays per process (ADR-0007).

Signed-off-by: Rui Chen <rui@chenrui.dev>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-24T21:04:55.713439Z 1535514 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chenrui333

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 1535514f16

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chenrui333

Copy link
Copy Markdown
Owner Author

Closing: the TypeScript server this targets is replaced by the Rust rewrite in upstream v10.0.0.

@chenrui333 chenrui333 closed this Oct 29, 2026
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.

1 participant