Skip to content
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

PullRecords: update replState.Offset in defer #1481

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions flow/activities/flowable.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ func (a *FlowableActivity) SyncFlow(
logger.Info(fmt.Sprintf("pushed %d records in %d seconds", numRecords, int(syncDuration.Seconds())))

lastCheckpoint := recordBatch.GetLastCheckpoint()
srcConn.UpdateReplStateLastOffset(lastCheckpoint)

err = monitoring.UpdateNumRowsAndEndLSNForCDCBatch(
ctx,
Expand Down
3 changes: 3 additions & 0 deletions flow/connectors/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ type CDCPullConnector interface {
// This method should be idempotent, and should be able to be called multiple times with the same request.
PullRecords(ctx context.Context, catalogPool *pgxpool.Pool, req *model.PullRecordsRequest) error

// Called when offset has been confirmed to destination
UpdateReplStateLastOffset(lastOffset int64)

// PullFlowCleanup drops both the Postgres publication and replication slot, as a part of DROP MIRROR
PullFlowCleanup(ctx context.Context, jobName string) error

Expand Down
18 changes: 14 additions & 4 deletions flow/connectors/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"regexp"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -49,6 +50,7 @@ type ReplState struct {
Slot string
Publication string
Offset int64
LastOffset atomic.Int64
}

func NewPostgresConnector(ctx context.Context, pgConfig *protos.PostgresConfig) (*PostgresConnector, error) {
Expand Down Expand Up @@ -133,7 +135,7 @@ func (c *PostgresConnector) ReplPing(ctx context.Context) error {
return pglogrepl.SendStandbyStatusUpdate(
ctx,
c.replConn.PgConn(),
pglogrepl.StandbyStatusUpdate{WALWritePosition: pglogrepl.LSN(c.replState.Offset)},
pglogrepl.StandbyStatusUpdate{WALWritePosition: pglogrepl.LSN(c.replState.LastOffset.Load())},
)
}
}
Expand Down Expand Up @@ -184,7 +186,9 @@ func (c *PostgresConnector) MaybeStartReplication(
Slot: slotName,
Publication: publicationName,
Offset: req.LastOffset,
LastOffset: atomic.Int64{},
}
c.replState.LastOffset.Store(req.LastOffset)
}
return nil
}
Expand Down Expand Up @@ -308,6 +312,9 @@ func (c *PostgresConnector) SetLastOffset(ctx context.Context, jobName string, l
func (c *PostgresConnector) PullRecords(ctx context.Context, catalogPool *pgxpool.Pool, req *model.PullRecordsRequest) error {
defer func() {
req.RecordStream.Close()
if c.replState != nil {
c.replState.Offset = req.RecordStream.GetLastCheckpoint()
}
}()

// Slotname would be the job name prefixed with "peerflow_slot_"
Expand Down Expand Up @@ -371,9 +378,6 @@ func (c *PostgresConnector) PullRecords(ctx context.Context, catalogPool *pgxpoo
return err
}

req.RecordStream.Close()
c.replState.Offset = req.RecordStream.GetLastCheckpoint()

latestLSN, err := c.getCurrentLSN(ctx)
if err != nil {
c.logger.Error("error getting current LSN", slog.Any("error", err))
Expand All @@ -389,6 +393,12 @@ func (c *PostgresConnector) PullRecords(ctx context.Context, catalogPool *pgxpoo
return nil
}

func (c *PostgresConnector) UpdateReplStateLastOffset(lastOffset int64) {
if c.replState != nil {
c.replState.LastOffset.Store(lastOffset)
}
}

// SyncRecords pushes records to the destination.
func (c *PostgresConnector) SyncRecords(ctx context.Context, req *model.SyncRecordsRequest) (*model.SyncResponse, error) {
rawTableIdentifier := getRawTableIdentifier(req.FlowJobName)
Expand Down
Loading