-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathhandler.go
308 lines (272 loc) · 9.33 KB
/
handler.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
// Copyright © 2022 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package logrepl
import (
"context"
"fmt"
"github.com/conduitio/conduit-commons/opencdc"
cschema "github.com/conduitio/conduit-commons/schema"
"github.com/conduitio/conduit-connector-postgres/source/common"
"github.com/conduitio/conduit-connector-postgres/source/logrepl/internal"
"github.com/conduitio/conduit-connector-postgres/source/position"
"github.com/conduitio/conduit-connector-postgres/source/schema"
sdk "github.com/conduitio/conduit-connector-sdk"
sdkschema "github.com/conduitio/conduit-connector-sdk/schema"
"github.com/jackc/pglogrepl"
)
// CDCHandler is responsible for handling logical replication messages,
// converting them to a record and sending them to a channel.
type CDCHandler struct {
tableKeys map[string]string
relationSet *internal.RelationSet
out chan<- opencdc.Record
lastTXLSN pglogrepl.LSN
tableInfo *common.TableInfoFetcher
withAvroSchema bool
keySchemas map[string]cschema.Schema
payloadSchemas map[string]cschema.Schema
}
func NewCDCHandler(
rs *internal.RelationSet,
tableInfo *common.TableInfoFetcher,
tableKeys map[string]string,
out chan<- opencdc.Record,
withAvroSchema bool,
) *CDCHandler {
return &CDCHandler{
tableKeys: tableKeys,
relationSet: rs,
out: out,
tableInfo: tableInfo,
withAvroSchema: withAvroSchema,
keySchemas: make(map[string]cschema.Schema),
payloadSchemas: make(map[string]cschema.Schema),
}
}
// Handle is the handler function that receives all logical replication messages.
// Returns non-zero LSN when a record was emitted for the message.
func (h *CDCHandler) Handle(ctx context.Context, m pglogrepl.Message, lsn pglogrepl.LSN) (pglogrepl.LSN, error) {
sdk.Logger(ctx).Trace().
Str("lsn", lsn.String()).
Str("messageType", m.Type().String()).
Msg("handler received pglogrepl.Message")
switch m := m.(type) {
case *pglogrepl.RelationMessage:
// We have to add the Relations to our Set so that we can decode our own output
h.relationSet.Add(m)
err := h.tableInfo.Refresh(ctx, m.RelationName)
if err != nil {
return 0, fmt.Errorf("failed to refresh table info: %w", err)
}
case *pglogrepl.InsertMessage:
if err := h.handleInsert(ctx, m, lsn); err != nil {
return 0, fmt.Errorf("logrepl handler insert: %w", err)
}
return lsn, nil
case *pglogrepl.UpdateMessage:
if err := h.handleUpdate(ctx, m, lsn); err != nil {
return 0, fmt.Errorf("logrepl handler update: %w", err)
}
return lsn, nil
case *pglogrepl.DeleteMessage:
if err := h.handleDelete(ctx, m, lsn); err != nil {
return 0, fmt.Errorf("logrepl handler delete: %w", err)
}
return lsn, nil
case *pglogrepl.BeginMessage:
h.lastTXLSN = m.FinalLSN
case *pglogrepl.CommitMessage:
if h.lastTXLSN != 0 && h.lastTXLSN != m.CommitLSN {
return 0, fmt.Errorf("out of order commit %s, expected %s", m.CommitLSN, h.lastTXLSN)
}
}
return 0, nil
}
// handleInsert formats a Record with INSERT event data from Postgres and sends
// it to the output channel.
func (h *CDCHandler) handleInsert(
ctx context.Context,
msg *pglogrepl.InsertMessage,
lsn pglogrepl.LSN,
) error {
rel, err := h.relationSet.Get(msg.RelationID)
if err != nil {
return fmt.Errorf("failed getting relation %v: %w", msg.RelationID, err)
}
if err := h.updateAvroSchema(ctx, rel); err != nil {
return fmt.Errorf("failed to update avro schema: %w", err)
}
newValues, err := h.relationSet.Values(msg.RelationID, msg.Tuple, h.tableInfo.GetTable(rel.RelationName))
if err != nil {
return fmt.Errorf("failed to decode new values: %w", err)
}
rec := sdk.Util.Source.NewRecordCreate(
h.buildPosition(lsn),
h.buildRecordMetadata(rel),
h.buildRecordKey(newValues, rel.RelationName),
h.buildRecordPayload(newValues),
)
h.attachSchemas(rec, rel.RelationName)
return h.send(ctx, rec)
}
// handleUpdate formats a record with UPDATE event data from Postgres and sends
// it to the output channel.
func (h *CDCHandler) handleUpdate(
ctx context.Context,
msg *pglogrepl.UpdateMessage,
lsn pglogrepl.LSN,
) error {
rel, err := h.relationSet.Get(msg.RelationID)
if err != nil {
return err
}
newValues, err := h.relationSet.Values(msg.RelationID, msg.NewTuple, h.tableInfo.GetTable(rel.RelationName))
if err != nil {
return fmt.Errorf("failed to decode new values: %w", err)
}
if err := h.updateAvroSchema(ctx, rel); err != nil {
return fmt.Errorf("failed to update avro schema: %w", err)
}
oldValues, err := h.relationSet.Values(msg.RelationID, msg.OldTuple, h.tableInfo.GetTable(rel.RelationName))
if err != nil {
// this is not a critical error, old values are optional, just log it
// we use level "trace" intentionally to not clog up the logs in production
sdk.Logger(ctx).Trace().Err(err).Msg("could not parse old values from UpdateMessage")
}
rec := sdk.Util.Source.NewRecordUpdate(
h.buildPosition(lsn),
h.buildRecordMetadata(rel),
h.buildRecordKey(newValues, rel.RelationName),
h.buildRecordPayload(oldValues),
h.buildRecordPayload(newValues),
)
h.attachSchemas(rec, rel.RelationName)
return h.send(ctx, rec)
}
// handleDelete formats a record with DELETE event data from Postgres and sends
// it to the output channel. Deleted records only contain the key and no payload.
func (h *CDCHandler) handleDelete(
ctx context.Context,
msg *pglogrepl.DeleteMessage,
lsn pglogrepl.LSN,
) error {
rel, err := h.relationSet.Get(msg.RelationID)
if err != nil {
return err
}
oldValues, err := h.relationSet.Values(msg.RelationID, msg.OldTuple, h.tableInfo.GetTable(rel.RelationName))
if err != nil {
return fmt.Errorf("failed to decode old values: %w", err)
}
if err := h.updateAvroSchema(ctx, rel); err != nil {
return fmt.Errorf("failed to update avro schema: %w", err)
}
rec := sdk.Util.Source.NewRecordDelete(
h.buildPosition(lsn),
h.buildRecordMetadata(rel),
h.buildRecordKey(oldValues, rel.RelationName),
h.buildRecordPayload(oldValues),
)
h.attachSchemas(rec, rel.RelationName)
return h.send(ctx, rec)
}
// send the record to the output channel or detect the cancellation of the
// context and return the context error.
func (h *CDCHandler) send(ctx context.Context, rec opencdc.Record) error {
select {
case <-ctx.Done():
return ctx.Err()
case h.out <- rec:
return nil
}
}
func (h *CDCHandler) buildRecordMetadata(rel *pglogrepl.RelationMessage) map[string]string {
m := map[string]string{
opencdc.MetadataCollection: rel.RelationName,
}
return m
}
// buildRecordKey takes the values from the message and extracts the key that
// matches the configured keyColumnName.
func (h *CDCHandler) buildRecordKey(values map[string]any, table string) opencdc.Data {
keyColumn := h.tableKeys[table]
key := make(opencdc.StructuredData)
for k, v := range values {
if keyColumn == k {
key[k] = v
break // TODO add support for composite keys
}
}
return key
}
// buildRecordPayload takes the values from the message and extracts the payload
// for the record.
func (h *CDCHandler) buildRecordPayload(values map[string]any) opencdc.Data {
if len(values) == 0 {
return nil
}
return opencdc.StructuredData(values)
}
// buildPosition stores the LSN in position and converts it to bytes.
func (*CDCHandler) buildPosition(lsn pglogrepl.LSN) opencdc.Position {
return position.Position{
Type: position.TypeCDC,
LastLSN: lsn.String(),
}.ToSDKPosition()
}
// updateAvroSchema generates and stores avro schema based on the relation's row,
// when usage of avro schema is requested.
func (h *CDCHandler) updateAvroSchema(ctx context.Context, rel *pglogrepl.RelationMessage) error {
if !h.withAvroSchema {
return nil
}
// Payload schema
avroPayloadSch, err := schema.Avro.ExtractLogrepl(rel.RelationName+"_payload", rel, h.tableInfo.GetTable(rel.RelationName))
if err != nil {
return fmt.Errorf("failed to extract payload schema: %w", err)
}
ps, err := sdkschema.Create(
ctx,
cschema.TypeAvro,
avroPayloadSch.Name(),
[]byte(avroPayloadSch.String()),
)
if err != nil {
return fmt.Errorf("failed creating payload schema for relation %v: %w", rel.RelationName, err)
}
h.payloadSchemas[rel.RelationName] = ps
// Key schema
avroKeySch, err := schema.Avro.ExtractLogrepl(rel.RelationName+"_key", rel, h.tableInfo.GetTable(rel.RelationName), h.tableKeys[rel.RelationName])
if err != nil {
return fmt.Errorf("failed to extract key schema: %w", err)
}
ks, err := sdkschema.Create(
ctx,
cschema.TypeAvro,
avroKeySch.Name(),
[]byte(avroKeySch.String()),
)
if err != nil {
return fmt.Errorf("failed creating key schema for relation %v: %w", rel.RelationName, err)
}
h.keySchemas[rel.RelationName] = ks
return nil
}
func (h *CDCHandler) attachSchemas(rec opencdc.Record, relationName string) {
if !h.withAvroSchema {
return
}
cschema.AttachPayloadSchemaToRecord(rec, h.payloadSchemas[relationName])
cschema.AttachKeySchemaToRecord(rec, h.keySchemas[relationName])
}