-
Notifications
You must be signed in to change notification settings - Fork 2
/
txnreader.go
521 lines (473 loc) · 16.5 KB
/
txnreader.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
package main
import (
"context"
"fmt"
"log"
"log/slog"
"time"
"github.com/jackc/pglogrepl"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgproto3"
"github.com/jackc/pgx/v5/pgtype"
)
type txnreaderConfig struct {
protocolVersion int
csvlog bool
csvlogInterval int
csvlogFilePrefix string
}
type txnStatus int
const (
txnUnknown txnStatus = iota
txnNone
txnV1
txnV2
)
type txnReader struct {
ctx context.Context
config *polorexConfig
// used to read slot size when sending standby messages
slotSizeConn *pgx.Conn
// used for START_REPLICATION, gets messages in a loop
replConn *pgconn.PgConn
protocolVersionV2 bool
// for sending standby messages
clientXLogPos pglogrepl.LSN
// global -> used only for transactions confirmed to be committed
relation *pglogrepl.RelationMessage
csvlogInterval time.Duration
nextStandbyMessageDeadline time.Time
typeMap *pgtype.Map
// set between StreamStart and StreamStop, needs to be passed to pglogrepl message parsing logic
// all InsertMessages that come when this is set are part of the same txn
inStreamV2 bool
// set between Begin and Commit, all InsertMessages that come when this is set are part of the same txn
inTxnReadV1 bool
// we could be reading multiple streamed transactions at the same time, key is xid
uncommittedTxnsV2 map[uint32]*txnInfo
// to hold the transaction we are fully reading from Begin to Commit
inprogressTxnV1 *txnInfo
// txnNone if no transaction has finished processing
// txnV1 if a committed transaction is finished reading
// txnV2 if a transaction has finished streaming and was committed via StreamCommit
txnStatus txnStatus
// for logging in StreamStop, and if txnV2 status the committed txn is this
currentXid uint32
// not sure how much of an optimization this is
insertValues map[string]interface{}
// used for timing how much time each transaction takes to read
txnStartTime time.Time
// for logging to CSV file
txnreadCSVLogger *txnreadCSVLogger
currentSecond time.Time
recordsReadInCurrentDuration int
}
type polorexRow struct {
id int64
txt string
}
func newTxnReader(ctx context.Context, config *polorexConfig, txnreaderConfig *txnreaderConfig) (*txnReader, error) {
replConn, err := config.createPostgresConn(ctx, true)
if err != nil {
return nil, fmt.Errorf("unable to establish replication connection: %w", err)
}
slotSizeConn, err := config.createPostgresConn(ctx, false)
if err != nil {
return nil, fmt.Errorf("unable to establish connection to query slot size: %w", err)
}
pluginArgs := []string{
fmt.Sprintf("proto_version '%d'", txnreaderConfig.protocolVersion),
fmt.Sprintf("publication_names '%s'", config.pubname),
"messages 'true'",
}
protocolVersionV2 := (txnreaderConfig.protocolVersion >= 2)
if protocolVersionV2 {
pluginArgs = append(pluginArgs, "streaming 'true'")
}
sysident, err := pglogrepl.IdentifySystem(ctx, replConn.PgConn())
if err != nil {
return nil, fmt.Errorf("IdentifySystem failed: %w", err)
}
err = pglogrepl.StartReplication(ctx, replConn.PgConn(), config.slotname, sysident.XLogPos,
pglogrepl.StartReplicationOptions{
PluginArgs: pluginArgs,
Mode: pglogrepl.LogicalReplication,
})
if err != nil {
return nil, fmt.Errorf("unable to start logical replication: %w", err)
}
var txnreadCSVLogger *txnreadCSVLogger
if txnreaderConfig.csvlog {
txnreadCSVLogger, err = newTxnReadCSVLogger(ctx, txnreaderConfig.csvlogFilePrefix)
if err != nil {
return nil, err
}
}
return &txnReader{
ctx: ctx,
config: config,
slotSizeConn: slotSizeConn,
replConn: replConn.PgConn(),
protocolVersionV2: protocolVersionV2,
clientXLogPos: sysident.XLogPos,
relation: nil,
csvlogInterval: time.Duration(txnreaderConfig.csvlogInterval) * time.Second,
nextStandbyMessageDeadline: time.Time{},
typeMap: pgtype.NewMap(),
inStreamV2: false,
inTxnReadV1: false,
uncommittedTxnsV2: map[uint32]*txnInfo{},
inprogressTxnV1: nil,
txnStatus: txnUnknown,
currentXid: 0,
insertValues: make(map[string]interface{}, 2),
txnStartTime: time.Time{},
currentSecond: time.Now(),
recordsReadInCurrentDuration: 0,
txnreadCSVLogger: txnreadCSVLogger,
}, nil
}
func (t *txnReader) decodeTextColumnData(data []byte, dataType uint32) (interface{}, error) {
if dt, ok := t.typeMap.TypeForOID(dataType); ok {
return dt.Codec.DecodeValue(t.typeMap, dataType, pgtype.TextFormatCode, data)
}
return string(data), nil
}
func (t *txnReader) getSlotSizeText() (string, error) {
// little extra for better formatting in logs
row := t.slotSizeConn.QueryRow(t.ctx,
`SELECT
regexp_replace(
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(),restart_lsn)),
' ',
'',
'g'
)
FROM
pg_replication_slots
WHERE
slot_name=$1`,
t.config.slotname)
var slotSizeText string
err := row.Scan(&slotSizeText)
if err != nil {
return "", fmt.Errorf("failed to check size of slot: %w", err)
}
return slotSizeText, nil
}
func (t *txnReader) getSlotSizeInMB() (int, error) {
row := t.slotSizeConn.QueryRow(t.ctx,
`SELECT
(pg_wal_lsn_diff(pg_current_wal_lsn(),restart_lsn) / 1024 / 1024)::bigint
FROM
pg_replication_slots
WHERE
slot_name=$1`,
t.config.slotname)
var slotSizeInMB int
err := row.Scan(&slotSizeInMB)
if err != nil {
return 0, fmt.Errorf("failed to check size of slot: %w", err)
}
return slotSizeInMB, nil
}
func (t *txnReader) processInsertMessage(
insertMsg *pglogrepl.InsertMessage,
relationMsg *pglogrepl.RelationMessage,
) (*polorexRow, error) {
for idx, col := range insertMsg.Tuple.Columns {
colName := relationMsg.Columns[idx].Name
if colName != "id" && colName != "txt" {
slog.Warn("received unexpected column in InsertMessage", "colName", colName)
}
switch col.DataType {
case 'n': // null
slog.Warn("received unexpected null value in InsertMessage", "colName", colName)
case 'u': // unchanged toast
slog.Warn("received unexpected unchanged toast value in InsertMessage", "colName", colName)
case 't': // text
val, err := t.decodeTextColumnData(col.Data, relationMsg.Columns[idx].DataType)
if err != nil {
return nil, fmt.Errorf("failed to decode column data: %w", err)
}
t.insertValues[colName] = val
}
}
id, ok := t.insertValues["id"].(int64)
if !ok {
return nil, fmt.Errorf("did not receive column %s in InsertMessage ", "id")
}
txt, ok := t.insertValues["txt"].(string)
if !ok {
return nil, fmt.Errorf("did not receive column %s in InsertMessage", "txt")
}
clear(t.insertValues)
t.recordsReadInCurrentDuration++
return &polorexRow{id: id, txt: txt}, nil
}
func (t *txnReader) processMessage(walData []byte) error {
t.txnStatus = txnNone
var logicalMsg pglogrepl.Message
var err error
if t.protocolVersionV2 {
logicalMsg, err = pglogrepl.ParseV2(walData, t.inStreamV2)
} else {
logicalMsg, err = pglogrepl.Parse(walData)
}
if err != nil {
slog.Error("error parsing logical replication message", "err", err)
return fmt.Errorf("error parsing logical replication message")
}
switch logicalMsg := logicalMsg.(type) {
case *pglogrepl.RelationMessage:
if logicalMsg.RelationName != t.config.tablename {
slog.Warn("received RelationMessage for unexpected relation",
"relationID", logicalMsg.RelationID,
"relationName", logicalMsg.RelationName)
} else {
slog.Info("received RelationMessage for table",
"relationID", logicalMsg.RelationID,
"relationName", logicalMsg.RelationName)
t.relation = logicalMsg
}
case *pglogrepl.RelationMessageV2:
if logicalMsg.RelationName != t.config.tablename {
slog.Warn("received RelationMessage for unexpected relation",
"relationID", logicalMsg.RelationID,
"relationName", logicalMsg.RelationName,
"xid", logicalMsg.Xid)
return nil
} else {
// receiving RelationMessage with xid as part of a stream, only use for the same txn
if logicalMsg.Xid > 0 {
slog.Info("received RelationMessage for table",
"relationID", logicalMsg.RelationID,
"relationName", logicalMsg.RelationName,
"xid", logicalMsg.Xid)
_, ok := t.uncommittedTxnsV2[logicalMsg.Xid]
if !ok {
slog.Error("received RelationMessage for unknown transaction",
"relationID", logicalMsg.RelationID,
"xid", logicalMsg.Xid)
return fmt.Errorf("received RelationMessage for unknown streaming transaction")
}
t.uncommittedTxnsV2[logicalMsg.Xid].relationV2 = logicalMsg
} else {
// received RelationMessage without xid as part of committed txn, use globally
slog.Info("received RelationMessage for table",
"relationID", logicalMsg.RelationID,
"relationName", logicalMsg.RelationName)
t.relation = &logicalMsg.RelationMessage
}
}
case *pglogrepl.BeginMessage:
slog.Info("received BeginMessage, beginning reading of committed transanction", "xid", logicalMsg.Xid)
t.inprogressTxnV1 = newTxnInfo(logicalMsg.Xid)
t.inTxnReadV1 = true
t.txnStartTime = time.Now()
case *pglogrepl.CommitMessage:
slog.Info("received CommitMessage, finished reading of committed transanction", "commitLSN", logicalMsg.CommitLSN)
t.inprogressTxnV1.committed = true
t.inprogressTxnV1.commitLSN = logicalMsg.CommitLSN
t.inTxnReadV1 = false
t.txnStatus = txnV1
t.inprogressTxnV1.txnReadTime += time.Since(t.txnStartTime)
return nil
case *pglogrepl.InsertMessage:
if t.relation == nil {
slog.Error("received InsertMessage without corresponding RelationMessage", "relationID", logicalMsg.RelationID)
return fmt.Errorf("received InsertMessage without corresponding RelationMessage")
}
row, err := t.processInsertMessage(logicalMsg, t.relation)
if err != nil {
return err
}
t.inprogressTxnV1.updateTxnInfoWithRow(row)
case *pglogrepl.InsertMessageV2:
var relationMessage *pglogrepl.RelationMessage
// part of a stream, process with its RelationMessage
if logicalMsg.Xid > 0 {
_, ok := t.uncommittedTxnsV2[logicalMsg.Xid]
if !ok {
slog.Error("received InsertMessage for unknown streaming transaction",
"relationID", logicalMsg.RelationID,
"xid", logicalMsg.Xid)
return fmt.Errorf("received InsertMessage for unknown streaming transaction")
}
relationMessage = &t.uncommittedTxnsV2[logicalMsg.Xid].relationV2.RelationMessage
} else {
if t.relation == nil {
slog.Error("received InsertMessage without corresponding RelationMessage", "relationID", logicalMsg.RelationID)
return fmt.Errorf("received InsertMessage without corresponding RelationMessage")
}
relationMessage = t.relation
}
row, err := t.processInsertMessage(&logicalMsg.InsertMessage, relationMessage)
if err != nil {
return err
}
if logicalMsg.Xid == 0 {
t.inprogressTxnV1.updateTxnInfoWithRow(row)
} else {
uncommittedTxn, ok := t.uncommittedTxnsV2[logicalMsg.Xid]
if ok {
uncommittedTxn.updateTxnInfoWithRow(row)
} else {
return fmt.Errorf("received InsertMessageV2 for xid %d without transaction information", t.currentXid)
}
}
case *pglogrepl.StreamStartMessageV2:
// for decoding messages while reading the stream
t.inStreamV2 = true
_, ok := t.uncommittedTxnsV2[logicalMsg.Xid]
if !ok {
// should be 1 if we don't have a map entry
if logicalMsg.FirstSegment == 0 {
slog.Warn("received new transaction with FirstSegment!=1", "xid", logicalMsg.Xid)
}
t.uncommittedTxnsV2[logicalMsg.Xid] = newTxnInfo(logicalMsg.Xid)
}
t.currentXid = logicalMsg.Xid
t.txnStartTime = time.Now()
slog.Info("received StreamStartMessage, reading messages for transaction", "xid", logicalMsg.Xid)
case *pglogrepl.StreamStopMessageV2:
t.inStreamV2 = false
uncommittedTxn, ok := t.uncommittedTxnsV2[t.currentXid]
if !ok {
return fmt.Errorf("received StreamStopMessage for xid %d without transaction information", t.currentXid)
}
uncommittedTxn.txnReadTime += time.Since(t.txnStartTime)
slog.Info("received StreamStopMessage, continuing", "xid", t.currentXid, "currentCount", uncommittedTxn.count)
case *pglogrepl.StreamCommitMessageV2:
uncommittedTxn, ok := t.uncommittedTxnsV2[logicalMsg.Xid]
if ok {
uncommittedTxn.committed = true
uncommittedTxn.commitLSN = logicalMsg.CommitLSN
// flag a transaction as being fully processed
t.txnStatus = txnV2
t.currentXid = uncommittedTxn.xid
slog.Info("received StreamCommitMessage, transaction finalized", "xid", logicalMsg.Xid)
} else {
return fmt.Errorf("received StreamStopMessage for xid %d without transaction information", t.currentXid)
}
case *pglogrepl.StreamAbortMessageV2:
_, ok := t.uncommittedTxnsV2[logicalMsg.Xid]
if ok {
t.uncommittedTxnsV2[logicalMsg.Xid] = nil
slog.Info("received StreamAbortMessage, transaction destroyed", "xid", logicalMsg.Xid)
} else {
return fmt.Errorf("received StreamStopMessage for xid %d without transaction information", t.currentXid)
}
default:
slog.Warn("unexpected message type in pgoutput stream:", "logicalMsg", logicalMsg)
}
return nil
}
func (t *txnReader) processTxn() (*txnInfo, error) {
for {
if time.Now().After(t.nextStandbyMessageDeadline) {
err := pglogrepl.SendStandbyStatusUpdate(t.ctx, t.replConn,
pglogrepl.StandbyStatusUpdate{WALWritePosition: t.clientXLogPos})
if err != nil {
return nil, fmt.Errorf("failed to send StandbyStatusUpdate: %w", err)
}
slotSizeText, err := t.getSlotSizeText()
if err != nil {
return nil, err
}
if t.txnreadCSVLogger != nil {
slotSizeInMB, err := t.getSlotSizeInMB()
if err != nil {
return nil, err
}
// fetching seconds independently as t.currentSecond only updates when we get inserts
t.txnreadCSVLogger.writeSlotSize(time.Now().Format(time.RFC3339), slotSizeInMB)
if time.Since(t.currentSecond) > t.csvlogInterval {
err := t.txnreadCSVLogger.writeRecordsRead(t.currentSecond.Format(time.RFC3339),
t.recordsReadInCurrentDuration)
if err != nil {
return nil, err
}
t.recordsReadInCurrentDuration = 0
t.currentSecond = time.Now()
}
}
slog.Info("sent StandbyStatusUpdate", "clientXLogPos", t.clientXLogPos.String(), "slotSize", slotSizeText)
t.nextStandbyMessageDeadline = time.Now().Add(t.csvlogInterval)
}
ctx, cancel := context.WithDeadline(t.ctx, t.nextStandbyMessageDeadline)
rawMsg, err := t.replConn.ReceiveMessage(ctx)
cancel()
if err != nil {
if pgconn.Timeout(err) {
continue
}
return nil, fmt.Errorf("ReceiveMessage failed: %w", err)
}
if errMsg, ok := rawMsg.(*pgproto3.ErrorResponse); ok {
return nil, fmt.Errorf("received Postgres WAL error: %+v", errMsg)
}
msg, ok := rawMsg.(*pgproto3.CopyData)
if !ok {
log.Printf("Received unexpected message: %T\n", rawMsg)
continue
}
switch msg.Data[0] {
case pglogrepl.PrimaryKeepaliveMessageByteID:
pkm, err := pglogrepl.ParsePrimaryKeepaliveMessage(msg.Data[1:])
if err != nil {
return nil, fmt.Errorf("ParsePrimaryKeepaliveMessage failed: %w", err)
}
slog.Debug("Primary Keepalive Message",
"serverWALEnd", pkm.ServerWALEnd,
"serverTime", pkm.ServerTime,
"replyRequested", pkm.ReplyRequested)
if pkm.ServerWALEnd > t.clientXLogPos {
t.clientXLogPos = pkm.ServerWALEnd
}
if pkm.ReplyRequested {
t.nextStandbyMessageDeadline = time.Time{}
}
case pglogrepl.XLogDataByteID:
xld, err := pglogrepl.ParseXLogData(msg.Data[1:])
if err != nil {
return nil, fmt.Errorf("ParseXLogData failed: %w", err)
}
err = t.processMessage(xld.WALData)
if err != nil {
return nil, err
}
if t.txnStatus == txnV1 {
return t.inprogressTxnV1, nil
} else if t.txnStatus == txnV2 {
return t.uncommittedTxnsV2[t.currentXid], nil
}
if xld.WALStart > t.clientXLogPos {
t.clientXLogPos = xld.WALStart
}
}
}
}
func txnreaderMain(ctx context.Context, config *polorexConfig, txnreaderConfig *txnreaderConfig) error {
txnreader, err := newTxnReader(ctx, config, txnreaderConfig)
if err != nil {
return err
}
if txnreader.txnreadCSVLogger != nil {
defer txnreader.txnreadCSVLogger.close()
}
for {
// blocks until we have a transacion
txn, err := txnreader.processTxn()
if err != nil {
return err
}
slog.Info("Finished reading transaction",
"minID", txn.minID,
"maxID", txn.maxID,
"count", txn.count,
"commitLSN", txn.commitLSN,
"txnReadTime", txn.txnReadTime)
}
}