-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathclient.go
More file actions
441 lines (390 loc) · 14.4 KB
/
client.go
File metadata and controls
441 lines (390 loc) · 14.4 KB
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
package main
import (
"bytes"
"compress/gzip"
"context"
stdsql "database/sql"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"io"
"net/http"
"path"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
awsHttp "github.com/aws/smithy-go/transport/http"
"github.com/estuary/connectors/go/common"
cerrors "github.com/estuary/connectors/go/connector-errors"
boilerplate "github.com/estuary/connectors/materialize-boilerplate"
sql "github.com/estuary/connectors/materialize-sql"
pf "github.com/estuary/flow/go/protocols/flow"
pm "github.com/estuary/flow/go/protocols/materialize"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
log "github.com/sirupsen/logrus"
)
var _ sql.SchemaManager = (*client)(nil)
type client struct {
db *stdsql.DB
cfg config
ep *sql.Endpoint[config]
}
func newClient(ctx context.Context, materializationName string, ep *sql.Endpoint[config]) (sql.Client, error) {
cfg := ep.Config
db, err := stdsql.Open("pgx", cfg.toURI())
if err != nil {
return nil, err
}
return &client{
db: db,
cfg: cfg,
ep: ep,
}, nil
}
func (c *client) PopulateInfoSchema(ctx context.Context, is *boilerplate.InfoSchema, resourcePaths [][]string) error {
catalog := c.cfg.Database
if catalog == "" {
// An endpoint-level database configuration is not required, so query for the active
// database if that's the case.
if err := c.db.QueryRowContext(ctx, "select current_database();").Scan(&catalog); err != nil {
return fmt.Errorf("querying for connected database: %w", err)
}
}
return sql.StdPopulateInfoSchema(ctx, is, c.db, c.ep.Dialect, catalog, resourcePaths)
}
func (c *client) CreateTable(ctx context.Context, tc sql.TableCreate) error {
_, err := c.db.ExecContext(ctx, tc.TableCreateSql)
return err
}
func (c *client) DeleteTable(ctx context.Context, path []string) (string, boilerplate.ActionApplyFn, error) {
stmt := fmt.Sprintf("DROP TABLE %s;", c.ep.Dialect.Identifier(path...))
return stmt, func(ctx context.Context) error {
_, err := c.db.ExecContext(ctx, stmt)
return err
}, nil
}
func (c *client) TruncateTable(ctx context.Context, path []string) (string, boilerplate.ActionApplyFn, error) {
stmt := fmt.Sprintf("TRUNCATE TABLE %s;", c.ep.Dialect.Identifier(path...))
return stmt, func(ctx context.Context) error {
_, err := c.db.ExecContext(ctx, stmt)
return err
}, nil
}
func (c *client) AlterTable(ctx context.Context, ta sql.TableAlter) (string, boilerplate.ActionApplyFn, error) {
// Redshift only allows a single column to be added per ALTER TABLE statement. Also, we will
// never need to drop nullability constraints, since Redshift does not allow dropping
// nullability and we don't ever create columns as NOT NULL as a result.
if len(ta.DropNotNulls) != 0 { // sanity check
return "", nil, fmt.Errorf("redshift cannot drop nullability constraints but got %d DropNotNulls for table %s", len(ta.DropNotNulls), ta.Identifier)
}
stmts := []string{}
for _, c := range ta.AddColumns {
stmts = append(stmts, fmt.Sprintf(
"ALTER TABLE %s ADD COLUMN %s %s;",
ta.Identifier,
c.Identifier,
c.NullableDDL,
))
}
if len(ta.ColumnTypeChanges) > 0 {
if steps, err := sql.StdColumnTypeMigrations(ctx, c.ep.Dialect, ta.Table, ta.ColumnTypeChanges); err != nil {
return "", nil, fmt.Errorf("rendering column migration steps: %w", err)
} else {
stmts = append(stmts, steps...)
}
}
return strings.Join(stmts, "\n"), func(ctx context.Context) error {
for _, stmt := range stmts {
_, err := c.db.ExecContext(ctx, stmt)
if err != nil {
return err
}
}
return nil
}, nil
}
func (c *client) ListSchemas(ctx context.Context) ([]string, error) {
rows, err := c.db.QueryContext(ctx, "select schema_name from svv_all_schemas")
if err != nil {
return nil, fmt.Errorf("querying svv_all_schemas: %w", err)
}
defer rows.Close()
out := []string{}
for rows.Next() {
var schema string
if err := rows.Scan(&schema); err != nil {
return nil, fmt.Errorf("scanning row: %w", err)
}
out = append(out, schema)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("iterating rows: %w", err)
}
return out, nil
}
func (c *client) CreateSchema(ctx context.Context, schemaName string) (string, error) {
return sql.StdCreateSchema(ctx, c.db, c.ep.Dialect, schemaName)
}
func preReqs(ctx context.Context, cfg config) *cerrors.PrereqErr {
errs := &cerrors.PrereqErr{}
db, err := stdsql.Open("pgx", cfg.toURI())
if err != nil {
errs.Err(err)
return errs
}
// Use a reasonable timeout for this connection test. It is not uncommon for a misconfigured
// connection (wrong host, wrong port, etc.) to hang for several minutes on Ping and we want to
// bail out well before then.
pingCtx, cancel := context.WithTimeout(ctx, 20*time.Second)
defer cancel()
if err := db.PingContext(pingCtx); err != nil {
// Provide a more user-friendly representation of some common error causes.
var pgErr *pgconn.ConnectError
if errors.As(err, &pgErr) {
err = pgErr.Unwrap()
if errStr := err.Error(); strings.Contains(errStr, "(SQLSTATE 28000)") {
err = fmt.Errorf("incorrect username or password")
} else if strings.Contains(errStr, "(SQLSTATE 3D000") {
err = fmt.Errorf("database %q does not exist", cfg.Database)
} else if strings.Contains(errStr, "context deadline exceeded") {
errStr := `connection to host at address %q timed out, possible causes:
* Redshift endpoint is not set to be publicly accessible
* there is no inbound rule allowing Estuary's IP address to connect through the Redshift VPC security group
* the configured address is incorrect, possibly with an incorrect host or port
* if connecting through an SSH tunnel, the SSH bastion server may not be operational, or the connection details are incorrect`
err = fmt.Errorf(errStr, cfg.Address)
}
}
errs.Err(err)
}
parsedFlags := common.ParseFeatureFlags(cfg.Advanced.FeatureFlags, featureFlagDefaults)
s3client, err := cfg.toS3Client(ctx, parsedFlags)
if err != nil {
// This is not caused by invalid S3 credentials, and would most likely be a logic error in
// the connector code.
errs.Err(err)
return errs
}
// Test creating, reading, and deleting an object from the configured bucket and bucket path.
testKey := path.Join(cfg.effectiveBucketPath(), uuid.NewString())
var awsErr *awsHttp.ResponseError
if _, err := s3client.PutObject(ctx, &s3.PutObjectInput{
Bucket: aws.String(cfg.Bucket),
Key: aws.String(testKey),
Body: strings.NewReader("testing"),
}); err != nil {
if errors.As(err, &awsErr) {
// Handling for the two most common cases: The bucket doesn't exist, or the bucket does
// exist but the configured credentials aren't authorized to write to it.
if awsErr.Response.Response.StatusCode == http.StatusNotFound {
err = fmt.Errorf("bucket %q does not exist", cfg.Bucket)
} else if awsErr.Response.Response.StatusCode == http.StatusForbidden {
err = fmt.Errorf("not authorized to write to %q", path.Join(cfg.Bucket, cfg.effectiveBucketPath()))
}
}
errs.Err(err)
} else if _, err := s3client.GetObject(ctx, &s3.GetObjectInput{
Bucket: aws.String(cfg.Bucket),
Key: aws.String(testKey),
}); err != nil {
if errors.As(err, &awsErr) && awsErr.Response.Response.StatusCode == http.StatusForbidden {
err = fmt.Errorf("not authorized to read from %q", path.Join(cfg.Bucket, cfg.effectiveBucketPath()))
}
errs.Err(err)
} else if _, err := s3client.DeleteObject(ctx, &s3.DeleteObjectInput{
Bucket: aws.String(cfg.Bucket),
Key: aws.String(testKey),
}); err != nil {
if errors.As(err, &awsErr) && awsErr.Response.Response.StatusCode == http.StatusForbidden {
err = fmt.Errorf("not authorized to delete from %q", path.Join(cfg.Bucket, cfg.effectiveBucketPath()))
}
errs.Err(err)
}
return errs
}
func (c *client) ExecStatements(ctx context.Context, statements []string) error {
return c.withDB(func(db *stdsql.DB) error { return sql.StdSQLExecStatements(ctx, db, statements) })
}
func (c *client) InstallFence(ctx context.Context, checkpoints sql.Table, fence sql.Fence) (sql.Fence, error) {
if err := c.withDB(func(db *stdsql.DB) error {
var err error
fence, err = installFence(ctx, db, checkpoints, fence)
if err != nil {
return fmt.Errorf("installing fence: %w", err)
}
return nil
}); err != nil {
return sql.Fence{}, err
}
return fence, nil
}
func (c *client) MustRecreateResource(req *pm.Request_Apply, lastBinding, newBinding *pf.MaterializationSpec_Binding) (bool, error) {
return false, nil
}
func (c *client) Close() {
c.db.Close()
}
func (c *client) withDB(fn func(*stdsql.DB) error) error {
var db, err = stdsql.Open("pgx", c.cfg.toURI())
if err != nil {
return err
}
defer db.Close()
return fn(db)
}
func compressBytes(b []byte) ([]byte, error) {
var gzb bytes.Buffer
w := gzip.NewWriter(&gzb)
if _, err := w.Write(b); err != nil {
return nil, fmt.Errorf("compressing bytes: %w", err)
} else if err := w.Close(); err != nil {
return nil, fmt.Errorf("closing gzip writer: %w", err)
}
return gzb.Bytes(), nil
}
func maybeDecompressBytes(b []byte) ([]byte, error) {
if b[0] == 0x1f && b[1] == 0x8b { // Valid gzip header bytes
var out bytes.Buffer
if r, err := gzip.NewReader(bytes.NewReader(b)); err != nil {
return nil, fmt.Errorf("decompressing bytes: %w", err)
} else if _, err = io.Copy(&out, r); err != nil {
return nil, fmt.Errorf("reading decompressed bytes: %w", err)
} else if err := r.Close(); err != nil {
return nil, fmt.Errorf("closing gzip reader: %w", err)
}
return out.Bytes(), nil
} else {
log.Info("loaded uncompressed bytes")
return b, nil
}
}
// installFence is a modified version of sql.StdInstallFence that handles
// compression of the checkpoint and reading varbyte values from Redshift.
func installFence(ctx context.Context, db *stdsql.DB, checkpoints sql.Table, fence sql.Fence) (sql.Fence, error) {
// TODO(whb): With the historical usage of sql.StdInstallFence, we were actually
// base64 encoding the checkpoint bytes and then sending that UTF8 string to
// Redshift, which stores those characters as bytes in the VARBYTE column. A
// slightly more direct & efficient way to handle this would be to store the
// bytes directly using TO_VARBYTE(checkpoint, 'base64'). This would require
// handling for the pre-existing checkpoints that were encoded in the previous
// way, and is not being implemented right now.
var txn, err = db.BeginTx(ctx, nil)
if err != nil {
return sql.Fence{}, fmt.Errorf("db.BeginTx: %w", err)
}
defer func() {
if txn != nil {
_ = txn.Rollback()
}
}()
// Increment the fence value of _any_ checkpoint which overlaps our key range.
if _, err = txn.Exec(
fmt.Sprintf(`
UPDATE %s
SET fence=fence+1
WHERE materialization=%s
AND key_end>=%s
AND key_begin<=%s
;
`,
checkpoints.Identifier,
checkpoints.Keys[0].Placeholder,
checkpoints.Keys[1].Placeholder,
checkpoints.Keys[2].Placeholder,
),
fence.Materialization,
fence.KeyBegin,
fence.KeyEnd,
); err != nil {
return sql.Fence{}, fmt.Errorf("incrementing fence: %w", err)
}
// Read the checkpoint with the narrowest [key_begin, key_end] which fully overlaps our range.
var readBegin, readEnd uint32
var checkpoint string
if err = txn.QueryRow(
fmt.Sprintf(`
SELECT fence, key_begin, key_end, checkpoint
FROM %s
WHERE materialization=%s
AND key_begin<=%s
AND key_end>=%s
ORDER BY key_end - key_begin ASC
LIMIT 1
;
`,
checkpoints.Identifier,
checkpoints.Keys[0].Placeholder,
checkpoints.Keys[1].Placeholder,
checkpoints.Keys[2].Placeholder,
),
fence.Materialization,
fence.KeyBegin,
fence.KeyEnd,
).Scan(&fence.Fence, &readBegin, &readEnd, &checkpoint); err == stdsql.ErrNoRows {
// Set an invalid range, which compares as unequal to trigger an insertion below.
readBegin, readEnd = 1, 0
} else if err != nil {
return sql.Fence{}, fmt.Errorf("scanning fence and checkpoint: %w", err)
} else if hexBytes, err := hex.DecodeString(checkpoint); err != nil {
return sql.Fence{}, fmt.Errorf("hex.DecodeString(checkpoint): %w", err)
} else if base64Bytes, err := base64.StdEncoding.DecodeString(string(hexBytes)); err != nil {
return sql.Fence{}, fmt.Errorf("base64.Decode(string(decompressed)): %w", err)
} else if fence.Checkpoint, err = maybeDecompressBytes(base64Bytes); err != nil {
return sql.Fence{}, fmt.Errorf("maybeDecompressBytes(fenceHexBytes): %w", err)
}
// If a checkpoint for this exact range doesn't exist then insert it now.
if readBegin == fence.KeyBegin && readEnd == fence.KeyEnd {
// Exists; no-op.
} else if compressedCheckpoint, err := compressBytes(fence.Checkpoint); err != nil {
return sql.Fence{}, fmt.Errorf("compressing checkpoint: %w", err)
} else if _, err = txn.Exec(
fmt.Sprintf(
"INSERT INTO %s (materialization, key_begin, key_end, fence, checkpoint) VALUES (%s, %s, %s, %s, %s);",
checkpoints.Identifier,
checkpoints.Keys[0].Placeholder,
checkpoints.Keys[1].Placeholder,
checkpoints.Keys[2].Placeholder,
checkpoints.Values[0].Placeholder,
checkpoints.Values[1].Placeholder,
),
fence.Materialization,
fence.KeyBegin,
fence.KeyEnd,
fence.Fence,
base64.StdEncoding.EncodeToString(compressedCheckpoint),
); err != nil {
return sql.Fence{}, fmt.Errorf("inserting fence: %w", err)
}
err = txn.Commit()
txn = nil // Disable deferred rollback.
if err != nil {
return sql.Fence{}, fmt.Errorf("txn.Commit: %w", err)
}
return fence, nil
}
// updateFence updates a fence and reports if the materialization instance was
// fenced off. It handles compression of the checkpoint, and is used instead of
// the typical templated fence update query because of that.
func updateFence(ctx context.Context, txn pgx.Tx, dialect sql.Dialect, fence sql.Fence) error {
if compressedCheckpoint, err := compressBytes(fence.Checkpoint); err != nil {
return fmt.Errorf("compressing checkpoint: %w", err)
} else if res, err := txn.Exec(ctx, fmt.Sprintf(
"UPDATE %s SET checkpoint = $1 WHERE materialization = $2 AND key_begin = $3 AND key_end = $4 AND fence = $5;",
dialect.Identifier(fence.TablePath...),
),
base64.StdEncoding.EncodeToString(compressedCheckpoint),
fence.Materialization,
fence.KeyBegin,
fence.KeyEnd,
fence.Fence,
); err != nil {
return fmt.Errorf("fetching fence update rows: %w", err)
} else if res.RowsAffected() != 1 {
return fmt.Errorf("this instance was fenced off by another")
}
return nil
}