-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
187 lines (167 loc) · 4.28 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"context"
"errors"
cdc "github.com/Trendyol/go-pq-cdc"
"github.com/Trendyol/go-pq-cdc/config"
"github.com/Trendyol/go-pq-cdc/pq/message/format"
"github.com/Trendyol/go-pq-cdc/pq/publication"
"github.com/Trendyol/go-pq-cdc/pq/replication"
"github.com/Trendyol/go-pq-cdc/pq/slot"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"log/slog"
"os"
"time"
)
/*
psql "postgres://cdc_user:cdc_pass@127.0.0.1:5433/cdc_db"
CREATE TABLE users (
user_id integer PRIMARY KEY,
name text NOT NULL
);
*/
/*
psql "postgres://cdc_user:cdc_pass@127.0.0.1/cdc_db?replication=database"
CREATE TABLE users (
id serial PRIMARY KEY,
name text NOT NULL,
created_on timestamptz
);
INSERT INTO users (name)
SELECT
'Oyleli' || i
FROM generate_series(1, 1000000) AS i;
*/
type Message struct {
Query string
Args []any
Ack func() error
}
var (
UpsertQuery = "INSERT INTO users (user_id, name) VALUES ($1, $2) ON CONFLICT (user_id) DO UPDATE SET name = excluded.name;"
DeleteQuery = "DELETE FROM users WHERE user_id = $1;"
)
func main() {
ctx := context.Background()
pool, err := pgxpool.New(ctx, "postgres://cdc_user:cdc_pass@127.0.0.1:5433/cdc_db")
if err != nil {
slog.Error("new pool", "error", err)
os.Exit(1)
}
messages := make(chan Message, 10000)
go Produce(ctx, pool, messages)
cfg := config.Config{
Host: "127.0.0.1",
Username: "cdc_user",
Password: "cdc_pass",
Database: "cdc_db",
Publication: publication.Config{
CreateIfNotExists: true,
Name: "cdc_publication",
Operations: publication.Operations{
publication.OperationInsert,
publication.OperationDelete,
publication.OperationTruncate,
publication.OperationUpdate,
},
Tables: publication.Tables{publication.Table{
Name: "users",
ReplicaIdentity: publication.ReplicaIdentityDefault,
}},
},
Slot: slot.Config{
CreateIfNotExists: true,
Name: "cdc_slot",
SlotActivityCheckerInterval: 3000,
},
Metric: config.MetricConfig{
Port: 8081,
},
Logger: config.LoggerConfig{
LogLevel: slog.LevelInfo,
},
}
connector, err := cdc.NewConnector(ctx, cfg, FilteredMapper(messages))
if err != nil {
slog.Error("new connector", "error", err)
os.Exit(1)
}
connector.Start(ctx)
}
func FilteredMapper(messages chan Message) replication.ListenerFunc {
return func(ctx *replication.ListenerContext) {
switch msg := ctx.Message.(type) {
case *format.Insert:
messages <- Message{
Query: UpsertQuery,
Args: []any{msg.Decoded["id"].(int32), msg.Decoded["name"].(string)},
Ack: ctx.Ack,
}
case *format.Delete:
messages <- Message{
Query: DeleteQuery,
Args: []any{msg.OldDecoded["id"].(int32)},
Ack: ctx.Ack,
}
case *format.Update:
messages <- Message{
Query: UpsertQuery,
Args: []any{msg.NewDecoded["id"].(int32), msg.NewDecoded["name"].(string)},
Ack: ctx.Ack,
}
}
}
}
func Produce(ctx context.Context, w *pgxpool.Pool, messages <-chan Message) {
var lastAck func() error
counter := 0
bulkSize := 10000
queue := make([]*pgx.QueuedQuery, bulkSize)
for {
select {
case event := <-messages:
lastAck = event.Ack
queue[counter] = &pgx.QueuedQuery{SQL: event.Query, Arguments: event.Args}
counter++
if counter == bulkSize {
batchResults := w.SendBatch(ctx, &pgx.Batch{QueuedQueries: queue})
err := Exec(batchResults, counter)
if err != nil {
slog.Error("batch results", "error", err)
continue
}
slog.Info("postgresql write", "count", counter)
counter = 0
if err = event.Ack(); err != nil {
slog.Error("ack", "error", err)
}
}
case <-time.After(time.Millisecond):
if counter > 0 {
batchResults := w.SendBatch(ctx, &pgx.Batch{QueuedQueries: queue[:counter]})
err := Exec(batchResults, counter)
if err != nil {
slog.Error("batch results", "error", err)
continue
}
slog.Info("postgresql write", "count", counter)
counter = 0
if err = lastAck(); err != nil {
slog.Error("ack", "error", err)
}
}
}
}
}
func Exec(br pgx.BatchResults, sqlCount int) error {
defer br.Close()
var batchErr error
for t := 0; t < sqlCount; t++ {
_, err := br.Exec()
if err != nil {
batchErr = errors.Join(batchErr, err)
}
}
return batchErr
}