-
Notifications
You must be signed in to change notification settings - Fork 957
postgres_cdc: fail the snapshot barrier on nack (CON-504) #4685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+151
−2
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -418,11 +418,43 @@ type pgStreamInput struct { | |
| // acknowledged. The snapshot->stream handoff blocks until it drains so the | ||
| // replication slot is not promoted before snapshot rows are durable. | ||
| snapshotAckWG sync.WaitGroup | ||
| // snapshotNackErr records the first snapshot batch nack. auto_replay_nacks | ||
| // is user-toggleable, so a nack can be terminal: the barrier must fail | ||
| // rather than promote the replication slot over undelivered rows. Cleared | ||
| // per connection attempt (the input outlives reconnects). | ||
| snapshotNackMu sync.Mutex | ||
| snapshotNackErr error | ||
|
|
||
| // IAM authentication fields | ||
| iamAuthEnabled bool | ||
| } | ||
|
|
||
| func (p *pgStreamInput) recordSnapshotNack(err error) { | ||
| p.snapshotNackMu.Lock() | ||
| defer p.snapshotNackMu.Unlock() | ||
| if p.snapshotNackErr == nil { | ||
| p.snapshotNackErr = err | ||
| } | ||
| } | ||
|
|
||
| func (p *pgStreamInput) snapshotNackError() error { | ||
| p.snapshotNackMu.Lock() | ||
| defer p.snapshotNackMu.Unlock() | ||
| return p.snapshotNackErr | ||
| } | ||
|
|
||
| // resetSnapshotGate clears any nack recorded by a previous connection attempt | ||
| // so the promotion barrier judges only the current run: the input outlives | ||
| // reconnects, and a stale error would fail every retry even after a clean | ||
| // re-run. The WaitGroup is deliberately left untouched — batches from a | ||
| // previous attempt that are still in flight can yet be acked or nacked, and | ||
| // both must keep counting. | ||
| func (p *pgStreamInput) resetSnapshotGate() { | ||
| p.snapshotNackMu.Lock() | ||
| defer p.snapshotNackMu.Unlock() | ||
| p.snapshotNackErr = nil | ||
| } | ||
|
|
||
| func (p *pgStreamInput) Connect(ctx context.Context) error { | ||
| // If IAM authentication is enabled, generate a new token | ||
| if p.iamAuthEnabled && p.streamConfig.RefreshAuthToken != nil { | ||
|
|
@@ -431,6 +463,10 @@ func (p *pgStreamInput) Connect(ctx context.Context) error { | |
| } | ||
| } | ||
|
|
||
| // The input outlives reconnects: clear any nack recorded by a previous | ||
| // snapshot attempt so the promotion barrier judges only this run. | ||
|
Comment on lines
+466
to
+467
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feels somewhat of a redundant comment. |
||
| p.resetSnapshotGate() | ||
|
|
||
| pgStream, err := pglogicalstream.NewPgStream(ctx, p.streamConfig) | ||
| if err != nil { | ||
| return fmt.Errorf("unable to create replication stream: %w", err) | ||
|
|
@@ -518,6 +554,16 @@ func (p *pgStreamInput) processStream(pgStream *pglogicalstream.Stream, batcher | |
| }() | ||
| select { | ||
| case <-drained: | ||
| if nackErr := p.snapshotNackError(); nackErr != nil { | ||
| // A snapshot batch was rejected downstream and | ||
| // auto_replay_nacks may be disabled: promoting the slot | ||
| // would skip the rejected rows forever. Restart instead; | ||
| // the temporary slot is discarded and the snapshot | ||
| // re-runs. | ||
| p.logger.Errorf("Snapshot batch was rejected downstream, restarting without promoting the replication slot (the snapshot will re-run): %v", nackErr) | ||
| p.stopSig.TriggerSoftStop() | ||
| break | ||
| } | ||
| pgStream.MarkSnapshotAcknowledged() | ||
| case <-p.stopSig.SoftStopChan(): | ||
| } | ||
|
|
@@ -605,10 +651,22 @@ func (p *pgStreamInput) flushBatch( | |
| // in the read loop). | ||
| isSnapshot := lsn == nil | ||
|
|
||
| ackFn := func(ctx context.Context, _ error) error { | ||
| ackFn := func(ctx context.Context, err error) error { | ||
| if isSnapshot { | ||
| defer p.snapshotAckWG.Done() | ||
| } | ||
| if err != nil { | ||
| // auto_replay_nacks is user-toggleable, so a nack can be terminal. | ||
| // Never resolve: the checkpoint stays pinned before this batch so | ||
| // no LSN can be acknowledged past its undelivered rows. Snapshot | ||
| // nacks additionally fail the promotion barrier so the replication | ||
| // slot is not promoted and the snapshot re-runs on restart. | ||
| if isSnapshot { | ||
| p.recordSnapshotNack(err) | ||
| } | ||
| p.logger.Errorf("Batch rejected downstream (snapshot=%v): the checkpoint is now pinned before this batch and the input will stall once checkpoint_limit is reached, unless the batch is redelivered (auto_replay_nacks) or the pipeline restarts: %v", isSnapshot, err) | ||
| return err | ||
| } | ||
| maxOffset := resolveFn() | ||
| if maxOffset == nil { | ||
| return nil | ||
|
|
@@ -617,7 +675,7 @@ func (p *pgStreamInput) flushBatch( | |
| if maxLSN == nil { | ||
| return nil | ||
| } | ||
| if err = pgStream.AckLSN(ctx, *maxLSN); err != nil { | ||
| if err := pgStream.AckLSN(ctx, *maxLSN); err != nil { | ||
| return fmt.Errorf("unable to ack LSN to postgres: %w", err) | ||
| } | ||
| return nil | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth moving
recordSnapshotNack,snapshotNackErrorandresetSnapshotGateto the bottom of the file? Keeping the important, relevant or public ones (Connect,ReadBatchandClose) at the top?