-
Notifications
You must be signed in to change notification settings - Fork 953
postgres_cdc: add signalling support (without the need to restart) #4624
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| // Copyright 2026 Redpanda Data, Inc. | ||
| // | ||
| // Licensed as a Redpanda Enterprise file under the Redpanda Community | ||
| // License (the "License"); you may not use this file except in compliance with | ||
| // the License. You may obtain a copy of the License at | ||
| // | ||
| // https://github.com/redpanda-data/connect/blob/main/licenses/rcl.md | ||
|
|
||
| package pgstream | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/Jeffail/checkpoint" | ||
|
|
||
| "github.com/redpanda-data/benthos/v4/public/service" | ||
| "github.com/redpanda-data/connect/v4/internal/impl/postgresql/pglogicalstream" | ||
| "github.com/redpanda-data/connect/v4/internal/replication" | ||
| ) | ||
|
|
||
| var _ replication.Signaller = (*pgSignaller)(nil) | ||
|
|
||
| type pgSignaller struct { | ||
| *replication.ControlSignaller | ||
|
|
||
| schema string | ||
| signalTableName string | ||
| } | ||
|
|
||
| // newPGSignaller creates a replication.Signaller that detects signal INSERTs on the given schema.tableName. | ||
| func newPGSignaller(schema, tableName string, log *service.Logger) *pgSignaller { | ||
| s := replication.NewControlSignaller(log) | ||
| return &pgSignaller{ControlSignaller: s, schema: schema, signalTableName: tableName} | ||
| } | ||
|
|
||
| // Listen returns any actionable signal found. Signal rows are always | ||
| // forwarded downstream as normal messages regardless of the outcome here. | ||
| // | ||
| // An empty tableName means signalling is disabled: every message is ignored. | ||
| // | ||
| // Only validated execute-snapshot signals return non-nil - everything else | ||
| // (an unsupported type, or a validated no-op) returns (nil, nil) and is | ||
| // acked immediately like any other message. | ||
| func (s *pgSignaller) Listen(_ context.Context, signal any) (*replication.ControlSignal, error) { | ||
| if s.signalTableName == "" { | ||
| return nil, nil | ||
| } | ||
| msg, ok := signal.(pglogicalstream.StreamMessage) | ||
| if !ok { | ||
| return nil, nil | ||
| } | ||
| if msg.Schema != s.schema || msg.Table != s.signalTableName { | ||
| return nil, nil | ||
| } | ||
| if msg.Operation != pglogicalstream.InsertOpType { | ||
| return nil, nil | ||
| } | ||
|
|
||
| // deserialise control signal | ||
| row, ok := msg.Data.(map[string]any) | ||
| if !ok { | ||
| return nil, fmt.Errorf("expected map for %s message data, got %T", s.signalTableName, msg.Data) | ||
| } | ||
| dataStr, ok := row["data"].(string) | ||
| if !ok { | ||
| return nil, fmt.Errorf("expected string for %s.data column, got %T", s.signalTableName, row["data"]) | ||
| } | ||
| var sig replication.ControlSignal | ||
| if err := json.Unmarshal([]byte(dataStr), &sig); err != nil { | ||
| return nil, fmt.Errorf("unmarshaling signal %s.data: %w", s.signalTableName, err) | ||
| } | ||
| sig.ID = fmt.Sprintf("%v", row["id"]) | ||
| if sig.Type, ok = row["type"].(string); !ok { | ||
| return nil, errors.New("parsing 'type' data") | ||
| } | ||
|
|
||
| log := s.Log.With("id", sig.ID, "type", sig.Type) | ||
|
|
||
| if !sig.IsSnapshot() { | ||
| log.Infof("Signal %q received but not a recognized action, forwarding as a regular message", sig.Type) | ||
| return nil, nil | ||
| } | ||
|
|
||
| // Invalid or no-op signals are not returned as actionable. | ||
| if len(sig.DataCollections) == 0 { | ||
| log.Warnf("Signal %q received but data-collections is empty — ignoring, streaming continues uninterrupted", sig.Type) | ||
| return nil, nil | ||
| } | ||
| if len(tableNamesFromSchema(sig.DataCollections, s.schema)) == 0 { | ||
| log.Warnf("Signal %q received but data-collections %v matched no tables for schema %q — ignoring, streaming continues uninterrupted", sig.Type, sig.DataCollections, s.schema) | ||
| return nil, nil | ||
| } | ||
|
|
||
| log.Infof("Signal %q received: operation=%s lsn=%v", sig.Type, msg.Operation, msg.LSN) | ||
|
|
||
| if msg.LSN != nil { | ||
| sig.LSN = []byte(*msg.LSN) | ||
| } | ||
| return &sig, nil | ||
| } | ||
|
|
||
| // awaitCheckpointLSN blocks until checkpointer's highest resolved offset has reached or passed target, | ||
| // meaning every message up to and including it has been acknowledged downstream. An empty target is a no-op. | ||
| func awaitCheckpointLSN(ctx context.Context, cp *checkpoint.Capped[*string], target []byte, waitInterval time.Duration) error { | ||
| if len(target) == 0 { | ||
| return nil | ||
| } | ||
| targetLSN, err := pglogicalstream.ParseLSN(string(target)) | ||
| if err != nil { | ||
| return fmt.Errorf("unable to parse target LSN: %w", err) | ||
| } | ||
|
|
||
| ticker := time.NewTicker(waitInterval) | ||
| defer ticker.Stop() | ||
| for { | ||
| if highest := cp.Highest(); highest != nil && *highest != nil { | ||
| if gotLSN, err := pglogicalstream.ParseLSN(**highest); err == nil && gotLSN >= targetLSN { | ||
| return nil | ||
| } | ||
| } | ||
| select { | ||
| case <-ticker.C: | ||
| case <-ctx.Done(): | ||
| return ctx.Err() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func tableNamesFromSchema(collections []string, schema string) []string { | ||
| if len(collections) == 0 { | ||
| return nil | ||
| } | ||
| tables := make([]string, 0, len(collections)) | ||
| for _, dc := range collections { | ||
| table := dc | ||
| if idx := strings.LastIndex(dc, "."); idx >= 0 { | ||
| if !strings.EqualFold(dc[:idx], schema) { | ||
| continue | ||
| } | ||
| table = dc[idx+1:] | ||
| } | ||
| tables = append(tables, table) | ||
| } | ||
| return tables | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Unrelated benchmark output change looks like a committed local debugging tweak. Alongside the intended
signal_table_nameaddition, this file also switches the benchmark output fromdrop: {}+ thebenchmarkprocessor (which measures throughput) to writing rows into./benchmark_results.json, and drops thecount: 1000batching bound (diff). These changes are unrelated to the signalling feature and disable the throughput measurement the bench harness exists for (§1.3.4). This looks like a local iteration left in by accident — worth confirming it's intended before merging.