This repository was archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Upgrade to modules, pgx/v4 & accoutrements - WIP #36
Open
mattTruebill
wants to merge
1
commit into
mkabilov:master
Choose a base branch
from
mattTruebill:matt/upgrade-to-modules-and-pgx/v4
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,17 @@ | ||
| module github.com/mkabilov/pg2ch | ||
|
|
||
| go 1.15 | ||
|
|
||
| require ( | ||
| github.com/ClickHouse/clickhouse-go v1.4.3 | ||
| github.com/google/btree v1.0.0 // indirect | ||
| github.com/jackc/pgconn v1.8.0 | ||
| github.com/jackc/pglogrepl v0.0.0-20210109153808-a78a685a0bff | ||
| github.com/jackc/pgproto3/v2 v2.0.6 | ||
| github.com/jackc/pgtype v1.6.2 | ||
| github.com/jackc/pgx v3.6.2+incompatible | ||
| github.com/jackc/pgx/v4 v4.10.1 | ||
| github.com/peterbourgon/diskv v2.0.1+incompatible | ||
| github.com/tidwall/redcon v1.4.0 | ||
| gopkg.in/yaml.v2 v2.4.0 | ||
| ) |
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
|
|
@@ -7,7 +7,9 @@ import ( | |
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/jackc/pgx" | ||
| "github.com/jackc/pgconn" | ||
| "github.com/jackc/pglogrepl" | ||
| "github.com/jackc/pgproto3/v2" | ||
|
|
||
| "github.com/mkabilov/pg2ch/pkg/decoder" | ||
| "github.com/mkabilov/pg2ch/pkg/message" | ||
|
|
@@ -35,16 +37,16 @@ type Interface interface { | |
| type consumer struct { | ||
| waitGr *sync.WaitGroup | ||
| ctx context.Context | ||
| conn *pgx.ReplicationConn | ||
| dbCfg pgx.ConnConfig | ||
| conn *pgconn.PgConn | ||
| dbCfg pgconn.Config | ||
|
Author
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. Lots of changes like this where will now be referencing repos that were spun-out of the main |
||
| slotName string | ||
| publicationName string | ||
| currentLSN utils.LSN | ||
| errCh chan error | ||
| } | ||
|
|
||
| // New instantiates the consumer | ||
| func New(ctx context.Context, errCh chan error, dbCfg pgx.ConnConfig, slotName, publicationName string, startLSN utils.LSN) *consumer { | ||
| func New(ctx context.Context, errCh chan error, dbCfg pgconn.Config, slotName, publicationName string, startLSN utils.LSN) *consumer { | ||
| return &consumer{ | ||
| waitGr: &sync.WaitGroup{}, | ||
| ctx: ctx, | ||
|
|
@@ -75,7 +77,7 @@ func (c *consumer) close(err error) { | |
|
|
||
| // Run runs consumer | ||
| func (c *consumer) Run(handler Handler) error { | ||
| rc, err := pgx.ReplicationConnect(c.dbCfg) | ||
| rc, err := pgconn.ConnectConfig(c.ctx, &c.dbCfg) | ||
| if err != nil { | ||
| return fmt.Errorf("could not connect using replication protocol: %v", err) | ||
| } | ||
|
|
@@ -100,8 +102,13 @@ func (c *consumer) Run(handler Handler) error { | |
| func (c *consumer) startDecoding() error { | ||
| log.Printf("Starting from %s lsn", c.currentLSN) | ||
|
|
||
| err := c.conn.StartReplication(c.slotName, uint64(c.currentLSN), -1, | ||
| `"proto_version" '1'`, fmt.Sprintf(`"publication_names" '%s'`, c.publicationName)) | ||
| startReplOptions := pglogrepl.StartReplicationOptions{ | ||
| Timeline: -1, | ||
| Mode: pglogrepl.LogicalReplication, | ||
| PluginArgs: []string{`"proto_version" '1'`, fmt.Sprintf(`"publication_names" '%s'`, c.publicationName)}, | ||
| } | ||
|
|
||
| err := pglogrepl.StartReplication(c.ctx, c.conn, c.slotName, pglogrepl.LSN(uint64(c.currentLSN)), startReplOptions) | ||
|
|
||
| if err != nil { | ||
| c.closeDbConnection() | ||
|
|
@@ -112,7 +119,7 @@ func (c *consumer) startDecoding() error { | |
| } | ||
|
|
||
| func (c *consumer) closeDbConnection() { | ||
| if err := c.conn.Close(); err != nil { | ||
| if err := c.conn.Close(c.ctx); err != nil { | ||
| log.Printf("could not close replication connection: %v", err) | ||
| } | ||
| } | ||
|
|
@@ -121,6 +128,7 @@ func (c *consumer) processReplicationMessage(handler Handler) { | |
| defer c.waitGr.Done() | ||
|
|
||
| statusTicker := time.NewTicker(statusTimeout) | ||
|
|
||
| for { | ||
| select { | ||
| case <-c.ctx.Done(): | ||
|
|
@@ -134,7 +142,7 @@ func (c *consumer) processReplicationMessage(handler Handler) { | |
| } | ||
| default: | ||
| wctx, cancel := context.WithTimeout(c.ctx, replWaitTimeout) | ||
| repMsg, err := c.conn.WaitForReplicationMessage(wctx) | ||
| repMsg, err := c.conn.ReceiveMessage(wctx) | ||
| cancel() | ||
|
|
||
| if err == context.DeadlineExceeded { | ||
|
|
@@ -153,40 +161,63 @@ func (c *consumer) processReplicationMessage(handler Handler) { | |
| continue | ||
| } | ||
|
|
||
| if repMsg.WalMessage != nil { | ||
| msg, err := decoder.Parse(repMsg.WalMessage.WalData) | ||
| if err != nil { | ||
| c.close(fmt.Errorf("invalid pgoutput message: %s", err)) | ||
| return | ||
| } | ||
|
|
||
| if err := handler.HandleMessage(utils.LSN(repMsg.WalMessage.WalStart), msg); err != nil { | ||
| c.close(fmt.Errorf("error handling waldata: %s", err)) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| if repMsg.ServerHeartbeat != nil && repMsg.ServerHeartbeat.ReplyRequested == 1 { | ||
| log.Println("server wants a reply") | ||
| if err := c.SendStatus(); err != nil { | ||
| c.close(fmt.Errorf("could not send replay progress: %v", err)) | ||
| return | ||
| //NEW | ||
| switch repMsg := repMsg.(type) { | ||
| case *pgproto3.CopyData: | ||
| switch repMsg.Data[0] { | ||
| case pglogrepl.PrimaryKeepaliveMessageByteID: | ||
| pkm, err := pglogrepl.ParsePrimaryKeepaliveMessage(repMsg.Data[1:]) | ||
| if err != nil { | ||
| log.Fatalln("ParsePrimaryKeepaliveMessage failed:", err) | ||
| } | ||
| log.Println("Primary Keepalive Message =>", "ServerWALEnd:", pkm.ServerWALEnd, "ServerTime:", pkm.ServerTime, "ReplyRequested:", pkm.ReplyRequested) | ||
|
|
||
| //if pkm.ReplyRequested { | ||
| // nextStandbyMessageDeadline = time.Time{} | ||
| //} | ||
|
|
||
| case pglogrepl.XLogDataByteID: | ||
| xld, err := pglogrepl.ParseXLogData(repMsg.Data[1:]) | ||
| if err != nil { | ||
| log.Fatalln("ParseXLogData failed:", err) | ||
| } | ||
| log.Println("XLogData =>", "WALStart", xld.WALStart, "ServerWALEnd", xld.ServerWALEnd, "ServerTime:", xld.ServerTime, "WALData size", len(xld.WALData)) | ||
|
|
||
| //MAYBE clientXLogPos = xld.WALStart + pglogrepl.LSN(len(xld.WALData)) | ||
| msg, err := decoder.Parse(xld.WALData) | ||
| if err != nil { | ||
| c.close(fmt.Errorf("invalid pgoutput message: %s", err)) | ||
| return | ||
| } | ||
|
|
||
| if err := handler.HandleMessage(utils.LSN(xld.WALStart), msg); err != nil { | ||
| c.close(fmt.Errorf("error handling waldata: %s", err)) | ||
| return | ||
| } | ||
|
|
||
| } | ||
| default: | ||
| log.Printf("Received unexpected message: %#v\n", repMsg) | ||
|
Author
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. This is copypasta from an example in |
||
| } | ||
| } | ||
| // END NEW | ||
|
|
||
| //if repMsg.ServerHeartbeat != nil && repMsg.ServerHeartbeat.ReplyRequested == 1 { | ||
| // log.Println("server wants a reply") | ||
| // if err := c.SendStatus(); err != nil { | ||
| // c.close(fmt.Errorf("could not send replay progress: %v", err)) | ||
| // return | ||
| // } | ||
| //} | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // SendStatus sends the status | ||
| func (c *consumer) SendStatus() error { | ||
| // log.Printf("sending status: %v", c.currentLSN) //TODO: move to debug log level | ||
| status, err := pgx.NewStandbyStatus(uint64(c.currentLSN)) | ||
|
|
||
| if err != nil { | ||
| return fmt.Errorf("error creating standby status: %s", err) | ||
| } | ||
| status := pglogrepl.StandbyStatusUpdate{WALWritePosition: pglogrepl.LSN(uint64(c.currentLSN))} | ||
|
|
||
| if err := c.conn.SendStandbyStatus(status); err != nil { | ||
| if err := pglogrepl.SendStandbyStatusUpdate(c.ctx, c.conn, status); err != nil { | ||
| return fmt.Errorf("failed to send standy status: %s", err) | ||
| } | ||
|
|
||
|
|
||
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
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.
pgx/v4favors taking pg connection as input via a connection string in either url or dsn format.