-
Notifications
You must be signed in to change notification settings - Fork 10
/
source.go
258 lines (223 loc) · 7.45 KB
/
source.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
// 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 postgres
import (
"context"
"errors"
"fmt"
"time"
"github.com/conduitio/conduit-commons/config"
"github.com/conduitio/conduit-commons/csync"
"github.com/conduitio/conduit-commons/lang"
"github.com/conduitio/conduit-commons/opencdc"
"github.com/conduitio/conduit-connector-postgres/source"
"github.com/conduitio/conduit-connector-postgres/source/cpool"
"github.com/conduitio/conduit-connector-postgres/source/logrepl"
sdk "github.com/conduitio/conduit-connector-sdk"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
// Source is a Postgres source plugin.
type Source struct {
sdk.UnimplementedSource
iterator source.Iterator
config source.Config
pool *pgxpool.Pool
tableKeys map[string]string
}
func NewSource() sdk.Source {
return sdk.SourceWithMiddleware(
&Source{
tableKeys: make(map[string]string),
},
sdk.DefaultSourceMiddleware(
// disable schema extraction by default, postgres will build its own schema
sdk.SourceWithSchemaExtractionConfig{
PayloadEnabled: lang.Ptr(false),
KeyEnabled: lang.Ptr(false),
},
)...,
)
}
func (s *Source) Parameters() config.Parameters {
return s.config.Parameters()
}
func (s *Source) Configure(ctx context.Context, cfg config.Config) error {
err := sdk.Util.ParseConfig(ctx, cfg, &s.config, NewSource().Parameters())
if err != nil {
return err
}
s.config = s.config.Init()
return s.config.Validate()
}
func (s *Source) Open(ctx context.Context, pos opencdc.Position) error {
pool, err := cpool.New(ctx, s.config.URL)
if err != nil {
return fmt.Errorf("failed to create a connection pool to database: %w", err)
}
s.pool = pool
logger := sdk.Logger(ctx)
if s.readingAllTables() {
logger.Info().Msg("Detecting all tables...")
s.config.Tables, err = s.getAllTables(ctx)
if err != nil {
return fmt.Errorf("failed to connect to get all tables: %w", err)
}
logger.Info().
Strs("tables", s.config.Tables).
Int("count", len(s.config.Tables)).
Msg("Successfully detected tables")
}
// ensure we have keys for all tables
for _, tableName := range s.config.Tables {
s.tableKeys[tableName], err = s.getPrimaryKey(ctx, tableName)
if err != nil {
return fmt.Errorf("failed to find primary key for table %s: %w", tableName, err)
}
}
switch s.config.CDCMode {
case source.CDCModeAuto:
// TODO add logic that checks if the DB supports logical replication
// (since that's the only thing we support at the moment)
fallthrough
case source.CDCModeLogrepl:
i, err := logrepl.NewCombinedIterator(ctx, s.pool, logrepl.Config{
Position: pos,
SlotName: s.config.LogreplSlotName,
PublicationName: s.config.LogreplPublicationName,
Tables: s.config.Tables,
TableKeys: s.tableKeys,
WithSnapshot: s.config.SnapshotMode == source.SnapshotModeInitial,
SnapshotFetchSize: s.config.SnapshotFetchSize,
})
if err != nil {
return fmt.Errorf("failed to create logical replication iterator: %w", err)
}
s.iterator = i
default:
// shouldn't happen, config was validated
return fmt.Errorf("unsupported CDC mode %q", s.config.CDCMode)
}
return nil
}
func (s *Source) Read(ctx context.Context) (opencdc.Record, error) {
return s.iterator.Next(ctx)
}
func (s *Source) Ack(ctx context.Context, pos opencdc.Position) error {
return s.iterator.Ack(ctx, pos)
}
func (s *Source) Teardown(ctx context.Context) error {
logger := sdk.Logger(ctx)
var errs []error
if s.iterator != nil {
logger.Debug().Msg("Tearing down iterator...")
if err := s.iterator.Teardown(ctx); err != nil {
logger.Warn().Err(err).Msg("Failed to tear down iterator")
errs = append(errs, fmt.Errorf("failed to tear down iterator: %w", err))
}
}
if s.pool != nil {
logger.Debug().Msg("Closing connection pool...")
err := csync.RunTimeout(ctx, s.pool.Close, time.Minute)
if err != nil {
errs = append(errs, fmt.Errorf("failed to close DB connection pool: %w", err))
}
}
return errors.Join(errs...)
}
func (s *Source) LifecycleOnDeleted(ctx context.Context, cfg config.Config) error {
if err := s.Configure(ctx, cfg); err != nil {
return fmt.Errorf("fail to handle lifecycle delete event: %w", err)
}
// N.B. This should not stay in here for long, enrich the default.
// Events are not passed enriched config with defaults.
params := s.config.Parameters()
if _, ok := cfg["logrepl.autoCleanup"]; !ok { // not set
s.config.LogreplAutoCleanup = params["logrepl.autoCleanup"].Default == "true"
}
if _, ok := cfg["logrepl.slotName"]; !ok {
s.config.LogreplSlotName = params["logrepl.slotName"].Default
}
if _, ok := cfg["logrepl.publicationName"]; !ok {
s.config.LogreplPublicationName = params["logrepl.publicationName"].Default
}
switch s.config.CDCMode {
case source.CDCModeAuto:
fallthrough // TODO: Adjust as `auto` changes.
case source.CDCModeLogrepl:
if !s.config.LogreplAutoCleanup {
sdk.Logger(ctx).Warn().Msg("Skipping logrepl auto cleanup")
return nil
}
return logrepl.Cleanup(ctx, logrepl.CleanupConfig{
URL: s.config.URL,
SlotName: s.config.LogreplSlotName,
PublicationName: s.config.LogreplPublicationName,
})
default:
sdk.Logger(ctx).Warn().Msgf("cannot handle CDC mode %q", s.config.CDCMode)
return nil
}
}
func (s *Source) readingAllTables() bool {
return len(s.config.Tables) == 1 && s.config.Tables[0] == source.AllTablesWildcard
}
func (s *Source) getAllTables(ctx context.Context) ([]string, error) {
query := "SELECT tablename FROM pg_tables WHERE schemaname = 'public'"
rows, err := s.pool.Query(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()
var tables []string
for rows.Next() {
var tableName string
if err := rows.Scan(&tableName); err != nil {
return nil, fmt.Errorf("failed to scan table name: %w", err)
}
tables = append(tables, tableName)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("rows error: %w", err)
}
return tables, nil
}
// getPrimaryKey queries the db for the name of the primary key column for a
// table if one exists and returns it.
func (s *Source) getPrimaryKey(ctx context.Context, tableName string) (string, error) {
query := `SELECT a.attname FROM pg_index i
JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey)
WHERE i.indrelid = $1::regclass AND i.indisprimary`
rows, err := s.pool.Query(ctx, query, tableName)
if err != nil {
return "", fmt.Errorf("failed to query table keys: %w", err)
}
defer rows.Close()
if !rows.Next() {
if rows.Err() != nil {
return "", fmt.Errorf("query failed: %w", rows.Err())
}
return "", fmt.Errorf("no table keys found: %w", pgx.ErrNoRows)
}
var colName string
err = rows.Scan(&colName)
if err != nil {
return "", fmt.Errorf("failed to scan row: %w", err)
}
if rows.Next() {
// we only support single column primary keys for now
return "", errors.New("composite keys are not supported")
}
return colName, nil
}