8000 feat(metrics): flush pending batch on metrics ingester shutdown by AntonTyb · Pull Request #1004 · google/xmc · GitHub
[go: up one dir, main page]

Skip to content

feat(metrics): flush pending batch on metrics ingester shutdown - #1004

Merged
AntonTyb merged 5 commits into
feat/pubsub-ingester-configfrom
feat/pubsub-ingester-hardening
Oct 23, 2026
Merged

AntonTyb merged 5 commits into
feat/pubsub-ingester-configfrom
feat/pubsub-ingester-hardening

Conversation

@AntonTyb
@AntonTyb AntonTyb 8000 commented Sep 28, 2026
Copy link
Copy Markdown
Collaborator

Stack 3/3: core (#1002) ← config (#1003) ← this PR (hardening). The base branch is feat/pubsub-ingester-config.

Summary

On SIGINT/SIGTERM the metrics ingester now writes its pending batch instead of nacking it. This PR also documents the service and updates the Ingestion Gateway README, which said the Pub/Sub path had no consumer.

Motivation & Context

Nacking on shutdown is correct but wasteful: every rollout makes Pub/Sub redeliver up to a batch of messages per pod. Writing the pending batch makes shutdown cheap.

Pub/Sub's Receive doesn't return until every delivered message has been settled. So the batch loop has to act on context cancellation itself, not wait for the channel to close.

Key Changes

  • ingester.go:
    • On ctx.Done, drainBuffered pulls the messages already buffered by receive callbacks into the pending batch without blocking.
    • flushOnShutdown then writes that batch on a context detached from the cancelled one. The write is bounded by shutdownFlushTimeout = 10s, well inside the default 30s termination grace period.
    • Messages that arrive after the flush are still nacked.
  • Tests cover:
    • flushing the pending batch on shutdown
    • drainBuffered
    • nacking a message that arrives after the shutdown flush
  • cmd/metrics_ingester/README.md: data flow, delivery semantics, configuration, authentication, probes and testing.
  • cmd/ingestion_gateway/README.md: the "bring your own subscriber" warning is replaced with a pointer to the ingester.

Assumptions

  • A 10s shutdown flush budget is enough — one batch of 1000 small rows inserts in well under a second — if an insert does time out, the batch is nacked and redelivered, so no data is lost.
  • The README says deployment wiring isn't in place yet — Helm is a follow-up, and Terraform/CTK waits for feat(components): migrate ingestion-gateway to 1P native component blueprint #969 — that sentence needs removing once the wiring lands.

Verification & Testing

  • Commands Executed: in the scratch module, go build, go vet, and go test -race -count=30 -cover on the package. ./.githooks/pre-commit passes.
  • Coverage & Test Status: 100% statement coverage, and all tests pass 30 consecutive race-detector runs.

Related Issues / Bugs

N/A

🤖 Generated with Claude Code

TAG=agy
CONV=c72e85d4-da3a-4185-b361-f01b12d0e3b4

@gemini-code-assist gemini-code-assist Bot left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request updates the metrics ingester to flush pending messages on shutdown instead of nacking them, ensuring at-least-once delivery during termination. It introduces a bounded shutdown flush timeout, drains buffered messages non-blockingly, and updates the documentation and tests accordingly. The reviewer suggested an optimization to return early in flushOnShutdown if there are no pending messages to avoid the overhead of creating a new context.

@AntonTyb
AntonTyb added this pull request to stack #1005 September 28, 2026 13:43
@AntonTyb
AntonTyb marked this pull request as ready for review September 29, 2026 11:33
@github-actions
github-actions Bot requested a review from pachopepe September 29, 2026 11:33
Comment thread cmd/metrics_ingester/README.md Resolved
@AntonTyb
AntonTyb requested a review from andrzejfudro October 22, 2026 06:59
andrzejfudro
andrzejfudro previously approved these changes Oct 22, 2026
@andrzejfudro andrzejfudro left a comment
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM but please consider addressing comments left by pachopepe marked as medium.

On SIGINT/SIGTERM the ingester used to nack its pending messages for
redelivery. It now writes them in a final batch, bounded to 10s so
shutdown stays inside the default Kubernetes grace period. Messages
already buffered when shutdown begins are included in that batch.
Messages that arrive after it are still nacked.

Document the ingester, and point the ingestion gateway README at it
instead of saying the Pub/Sub path has no consumer.

TAG=agy
CONV=c72e85d4-da3a-4185-b361-f01b12d0e3b4
…k settlement

Route all batchLoop cancellation paths through a single shutdown helper
that drains buffered messages and flushes them in batchSize chunks
sharing a bounded 10s context detached from Run's signal cancellation.

Update fakeWriter in ingester_test.go to propagate ctx.Err() and record
per-call context deadlines so shutdown and in-flight write context
guarantees are verified non-vacuously, and drive post-drain message
arrival through pstest to assert explicit Nack settlement without Ack.

Synchronize cmd/metrics_ingester/README.md for shutdown drain/chunking,
terminationGracePeriodSeconds (>15s) timing budget, subscription
RetryPolicy recommendation, unbounded malformed payload drops, and
target -batch_interval wait semantics.

Comment link: #1004 (comment)
Comment link: #1004 (comment)
Comment link: #1004 (comment)
Comment link: #1004 (comment)
Comment link: #1004 (comment)
Comment link: #1004 (comment)

TAG=agy
CONV=0fafd02b-f3b1-4563-a73d-49ec0845afbd

Change-Id: Ia80f0878c92a0c928233c40a1e60628b66bcdb0e
… separation

Merge updated feat/pubsub-ingester-config and add a brief rationale in
cmd/metrics_ingester/README.md explaining why metrics_ingester runs as a
separate service from ingestion_gateway (ClickHouse credential/egress
isolation and independent batch-insert replica scaling).

Comment link: #1002 (comment)

TAG=agy
CONV=0fafd02b-f3b1-4563-a73d-49ec0845afbd

Change-Id: I751d6c78cf1088313833074a4f95fcd0a5c7df2e
andrzejfudro
andrzejfudro previously approved these changes Oct 23, 2026
…tal docs

Merge updated feat/pubsub-ingester-config (caa080a) into #1004, retaining
#1004's shutdown-flush README details and referencing the dropped_total log
field in place of the removed DroppedMessages getter.

Comment link: #1002 (comment)
Comment link: #1002 (comment)

TAG=agy
CONV=0fafd02b-f3b1-4563-a73d-49ec0845afbd

Change-Id: I3ea0c7f2900179baeb5927323bb3b619331f87c5
@github-actions github-actions Bot left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🤖 Incremental PR Approval

Clean rebase detected with zero semantic patch lines changed.

Preserving prior approval by @andrzejfudro on commit 128a02c.

Merge updated feat/pubsub-ingester-config (38a16cc) into #1004 so the branch
is fully up to date with #1003 and main.

TAG=agy
CONV=0fafd02b-f3b1-4563-a73d-49ec0845afbd

Change-Id: I39d2bb1012f053f2016b201fec7ede27a5b32ac4
@github-actions github-actions Bot left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🤖 Incremental PR Approval

Clean rebase detected with zero semantic patch lines changed. PR touches protected files, but the approved patches are unchanged.

Preserving prior approval by @andrzejfudro on commit 128a02c.

@AntonTyb
AntonTyb added this pull request to the merge queue Oct 23, 2026
Merged via the queue into main with commit 1e1b1fc Oct 23, 2026
14 checks passed
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.

3 participants

0