-
Notifications
You must be signed in to change notification settings - Fork 0
/
planetscale_edge_database.go
322 lines (280 loc) · 9.89 KB
/
planetscale_edge_database.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
package internal
import (
"context"
"fmt"
"io"
"strings"
"time"
querypb "vitess.io/vitess/go/vt/proto/query"
"github.com/pkg/errors"
psdbconnect "github.com/planetscale/airbyte-source/proto/psdbconnect/v1alpha1"
"github.com/planetscale/psdb/auth"
grpcclient "github.com/planetscale/psdb/core/pool"
clientoptions "github.com/planetscale/psdb/core/pool/options"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"vitess.io/vitess/go/sqltypes"
_ "vitess.io/vitess/go/vt/vtctl/grpcvtctlclient"
_ "vitess.io/vitess/go/vt/vtgate/grpcvtgateconn"
)
type (
OnResult func(*sqltypes.Result) error
OnCursor func(*psdbconnect.TableCursor) error
)
type ReadParams struct {
Source PlanetScaleSource
Table Stream
LastKnownPosition *psdbconnect.TableCursor
Columns []string
OnResult OnResult
OnCursor OnCursor
TabletType psdbconnect.TabletType
Cells []string
}
var binlogsPurgedMessage = "Cannot replicate because the master purged required binary logs"
// PlanetScaleDatabase is a general purpose interface
// that defines all the data access methods needed for the PlanetScale Singer Tap to function.
type PlanetScaleDatabase interface {
CanConnect(ctx context.Context, ps PlanetScaleSource) error
Read(ctx context.Context, params ReadParams) (*SerializedCursor, error)
Close() error
}
func NewEdge(mysql PlanetScaleEdgeMysqlAccess, logger Logger) PlanetScaleDatabase {
return &PlanetScaleEdgeDatabase{
Mysql: mysql,
Logger: logger,
}
}
// PlanetScaleEdgeDatabase is an implementation of the PlanetScaleDatabase interface defined above.
// It uses the mysql interface provided by PlanetScale for all schema/shard/tablet discovery and
// the grpc API for incrementally syncing rows from PlanetScale.
type PlanetScaleEdgeDatabase struct {
Logger Logger
Mysql PlanetScaleEdgeMysqlAccess
clientFn func(ctx context.Context, ps PlanetScaleSource) (psdbconnect.ConnectClient, error)
}
func (p PlanetScaleEdgeDatabase) CanConnect(ctx context.Context, psc PlanetScaleSource) error {
return p.Mysql.PingContext(ctx, psc)
}
func (p PlanetScaleEdgeDatabase) Close() error {
return p.Mysql.Close()
}
// Read streams rows from a table given a starting cursor.
// 1. We will get the latest vgtid for a given table in a shard when a sync session starts.
// 2. This latest vgtid is now the stopping point for this sync session.
// 3. Ask vstream to stream from the last known vgtid
// 4. When we reach the stopping point, read all rows available at this vgtid
// 5. End the stream when (a) a vgtid newer than latest vgtid is encountered or (b) the timeout kicks in.
func (p PlanetScaleEdgeDatabase) Read(ctx context.Context, params ReadParams) (*SerializedCursor, error) {
var (
err error
sErr error
currentSerializedCursor *SerializedCursor
)
currentPosition := params.LastKnownPosition
readDuration := 90 * time.Second
preamble := fmt.Sprintf("[table: %v, shard : %v, tablet: %v, cells : %v ] ", params.Table.Name, currentPosition.Shard, params.TabletType, params.Cells)
for {
p.Logger.Info(preamble + "peeking to see if there's any new rows")
latestCursorPosition, lcErr := p.getLatestCursorPosition(ctx, currentPosition.Shard, currentPosition.Keyspace, params.Table, params.Source, params.TabletType, params.Cells)
if lcErr != nil {
return currentSerializedCursor, errors.Wrap(err, "Unable to get latest cursor position")
}
// the current vgtid is the same as the last synced vgtid, no new rows.
if latestCursorPosition == currentPosition.Position {
p.Logger.Info(preamble + "no new rows found, exiting")
return TableCursorToSerializedCursor(currentPosition)
}
p.Logger.Info(fmt.Sprintf(preamble+"syncing rows with cursor [%v]", currentPosition))
p.Logger.Info(fmt.Sprintf(preamble+"latest database position is [%v]", latestCursorPosition))
currentPosition, err = p.sync(ctx, currentPosition, latestCursorPosition, readDuration, params)
if currentPosition.Position != "" {
currentSerializedCursor, sErr = TableCursorToSerializedCursor(currentPosition)
if sErr != nil {
// if we failed to serialize here, we should bail.
return currentSerializedCursor, errors.Wrap(sErr, "unable to serialize current position")
}
}
if err != nil {
if s, ok := status.FromError(err); ok {
// if the error is unknown, it might be because the binlogs are purged, check for known error message
if s.Code() == codes.Unknown && params.LastKnownPosition != nil {
if strings.Contains(err.Error(), binlogsPurgedMessage) {
p.Logger.Info("Binlogs are purged, state is stale")
return currentSerializedCursor, fmt.Errorf("state for this sync operation [%v] is stale, please restart a full sync to get the latest state", params.LastKnownPosition.Position)
}
}
// if the error is anything other than server timeout, keep going
if s.Code() != codes.DeadlineExceeded {
p.Logger.Info(fmt.Sprintf("%v Got error [%v], Returning with cursor :[%v] after server timeout", preamble, s.Code(), currentPosition))
return currentSerializedCursor, nil
} else {
p.Logger.Info(preamble + "Continuing with cursor after server timeout")
}
} else if errors.Is(err, io.EOF) {
p.Logger.Info(fmt.Sprintf("%vFinished reading all rows for table [%v]", preamble, params.Table.Name))
return currentSerializedCursor, nil
} else {
p.Logger.Info(fmt.Sprintf("non-grpc error [%v]]", err))
return currentSerializedCursor, err
}
}
}
}
func (p PlanetScaleEdgeDatabase) sync(ctx context.Context, tc *psdbconnect.TableCursor, stopPosition string, readDuration time.Duration, params ReadParams) (*psdbconnect.TableCursor, error) {
defer p.Logger.Flush(params.Table)
ctx, cancel := context.WithTimeout(ctx, readDuration)
defer cancel()
var (
err error
client psdbconnect.ConnectClient
)
if p.clientFn == nil {
conn, err := grpcclient.Dial(ctx, params.Source.Host,
clientoptions.WithDefaultTLSConfig(),
clientoptions.WithCompression(true),
clientoptions.WithConnectionPool(1),
clientoptions.WithExtraCallOption(
auth.NewBasicAuth(params.Source.Username, params.Source.Password).CallOption(),
),
)
if err != nil {
return tc, err
}
defer conn.Close()
client = psdbconnect.NewConnectClient(conn)
} else {
client, err = p.clientFn(ctx, params.Source)
if err != nil {
return tc, err
}
}
if tc.LastKnownPk != nil {
filterFields(tc.LastKnownPk, params.Table)
tc.Position = ""
}
sReq := &psdbconnect.SyncRequest{
TableName: params.Table.Name,
Cursor: tc,
TabletType: params.TabletType,
Columns: params.Columns,
Cells: params.Cells,
}
c, err := client.Sync(ctx, sReq)
if err != nil {
return tc, err
}
// stop when we've reached the well known stop position for this sync session.
watchForVgGtidChange := false
for {
res, err := c.Recv()
if err != nil {
return tc, err
}
if res.Cursor != nil {
tc = res.Cursor
}
// Because of the ordering of events in a vstream
// we receive the vgtid event first and then the rows.
// the vgtid event might repeat, but they're ordered.
// so we once we reach the desired stop vgtid, we stop the sync session
// if we get a newer vgtid.
watchForVgGtidChange = watchForVgGtidChange || tc.Position == stopPosition
for _, result := range res.Result {
qr := sqltypes.Proto3ToResult(result)
for _, row := range qr.Rows {
sqlResult := &sqltypes.Result{
Fields: result.Fields,
}
sqlResult.Rows = append(sqlResult.Rows, row)
if params.OnResult != nil {
if err := params.OnResult(sqlResult); err != nil {
return tc, err
}
}
}
}
if params.OnCursor != nil && res.Cursor != nil {
params.OnCursor(res.Cursor)
}
if watchForVgGtidChange && tc.Position != stopPosition {
return tc, io.EOF
}
}
}
// filterFields removes all fields that are not part of the primary key of a given stream
// the `Fields` collection in the LastKnownPK QueryResult might contain _ALL_ the
// fields in the table and not just the fields that have values assigned to them.
// Because the field -> value mapping is ordinal based in Vitess,
// we can depend on the original Fields collection preserving the order.
func filterFields(lastKnownPK *querypb.QueryResult, s Stream) {
var fields []*querypb.Field
for _, field := range lastKnownPK.Fields {
if contains(s.KeyProperties, field.Name) {
fields = append(fields, field)
}
}
lastKnownPK.Fields = fields
}
// contains checks if a string searchTerm is present in the list.
func contains(list []string, searchTerm string) bool {
for _, val := range list {
if searchTerm == val {
return true
}
}
return false
}
func (p PlanetScaleEdgeDatabase) getLatestCursorPosition(ctx context.Context, shard, keyspace string, s Stream, ps PlanetScaleSource, tabletType psdbconnect.TabletType, cells []string) (string, error) {
defer p.Logger.Flush(s)
timeout := 45 * time.Second
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
var (
err error
client psdbconnect.ConnectClient
)
if p.clientFn == nil {
conn, err := grpcclient.Dial(ctx, ps.Host,
clientoptions.WithDefaultTLSConfig(),
clientoptions.WithCompression(true),
clientoptions.WithConnectionPool(1),
clientoptions.WithExtraCallOption(
auth.NewBasicAuth(ps.Username, ps.Password).CallOption(),
),
)
if err != nil {
return "", err
}
defer conn.Close()
client = psdbconnect.NewConnectClient(conn)
} else {
client, err = p.clientFn(ctx, ps)
if err != nil {
return "", err
}
}
sReq := &psdbconnect.SyncRequest{
TableName: s.Name,
Cursor: &psdbconnect.TableCursor{
Shard: shard,
Keyspace: keyspace,
Position: "current",
},
Cells: cells,
TabletType: tabletType,
}
c, err := client.Sync(ctx, sReq)
if err != nil {
return "", nil
}
for {
res, err := c.Recv()
if err != nil {
return "", err
}
if res.Cursor != nil {
return res.Cursor.Position, nil
}
}
}