-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.go
403 lines (352 loc) · 9.88 KB
/
db.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
// Copyright 2023 DoltHub, 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 main
import (
"context"
"database/sql"
"errors"
"fmt"
"net/url"
"os"
"strings"
"time"
"github.com/cenkalti/backoff/v4"
)
func OpenDB(ctx context.Context, cfg *Config, instance Instance) (*sql.DB, error) {
hostname := instance.Hostname()
port := instance.Port()
dsn := RenderDSN(cfg, hostname, port)
return sql.Open("mysql", dsn)
}
func RenderDSN(cfg *Config, hostname string, port int) string {
user := os.Getenv("DOLT_USERNAME")
if user == "" {
user = "root"
}
authority := user
pass := os.Getenv("DOLT_PASSWORD")
if pass != "" {
authority += ":" + pass
}
params := make(url.Values)
params["parseTime"] = []string{"true"}
if cfg.TLSInsecure {
params["tls"] = []string{"skip-verify"}
} else if cfg.TLSConfig != nil {
// TODO: This is spookily coupled to the config name in main
params["tls"] = []string{"custom"}
} else if cfg.TLSVerified {
params["tls"] = []string{"true"}
}
return fmt.Sprintf("%s@tcp(%s:%d)/dolt_cluster?%s", authority, hostname, port, params.Encode())
}
func CallAssumeRole(ctx context.Context, cfg *Config, instance Instance, role string, epoch int) error {
db, err := OpenDB(ctx, cfg, instance)
if err != nil {
return err
}
defer db.Close()
conn, err := db.Conn(ctx)
if err != nil {
return err
}
defer conn.Close()
var status int
q := fmt.Sprintf("CALL DOLT_ASSUME_CLUSTER_ROLE('%s', %d)", role, epoch)
rows, err := conn.QueryContext(ctx, q)
if err != nil {
return err
}
defer rows.Close()
if rows.Next() {
err = rows.Scan(&status)
if err != nil {
return err
}
if status != 0 {
return fmt.Errorf("result from call dolt_assume_role('%s', %d) was %d, not 0", role, epoch, status)
}
}
if rows.Err() != nil {
return rows.Err()
}
return nil
}
func CallTransitionToStandby(ctx context.Context, cfg *Config, instance Instance, epoch int, dbstates []DBState) (int, error) {
db, err := OpenDB(ctx, cfg, instance)
if err != nil {
return -1, err
}
defer db.Close()
conn, err := db.Conn(ctx)
if err != nil {
return -1, err
}
defer conn.Close()
type TransitionResult struct {
CaughtUp int
Database string
Remote string
RemoteURL string
Parsed *url.URL
}
var results []TransitionResult
q := fmt.Sprintf("CALL DOLT_CLUSTER_TRANSITION_TO_STANDBY('%d', '%d')", epoch, cfg.MinCaughtUpStandbys)
rows, err := conn.QueryContext(ctx, q)
if err != nil {
return -1, err
}
defer rows.Close()
for rows.Next() {
var res TransitionResult
err = rows.Scan(&res.CaughtUp, &res.Database, &res.Remote, &res.RemoteURL)
if err != nil {
return -1, err
}
results = append(results, res)
}
if rows.Err() != nil {
return -1, rows.Err()
}
numCaughtUp := make(map[string]int)
for i := range results {
var err error
results[i].Parsed, err = url.Parse(results[i].RemoteURL)
if err != nil {
return -1, err
}
if results[i].CaughtUp == 1 {
numCaughtUp[results[i].Parsed.Host] = numCaughtUp[results[i].Parsed.Host] + 1
}
}
var maxCaughtUpHost string
var maxCaughtUp int
for k, v := range numCaughtUp {
if v > maxCaughtUp {
maxCaughtUpHost = k
maxCaughtUp = v
}
}
var maxCaughtUpParsedURL *url.URL
for _, res := range results {
if res.Parsed.Host == maxCaughtUpHost {
maxCaughtUpParsedURL = res.Parsed
break
}
}
if maxCaughtUpParsedURL == nil {
return -1, fmt.Errorf("internal error: did not find caught up URL of the caught up host: %s", maxCaughtUpHost)
}
caughtUpHostname := maxCaughtUpParsedURL.Hostname()
for i, dbs := range dbstates {
instanceHostname := dbs.Instance.Hostname()
if instanceHostname == caughtUpHostname || strings.HasPrefix(instanceHostname, caughtUpHostname) {
return i, nil
}
}
return -1, fmt.Errorf("internal error: did not find caught up URL of the caught up host: %s", maxCaughtUpHost)
}
func LoadDBState(ctx context.Context, cfg *Config, instance Instance) DBState {
errf := func(err error) error {
return fmt.Errorf("error loading role and epoch for %s: %w", instance.Name(), err)
}
var res DBState
bo := backoff.NewExponentialBackOff()
bo.MaxElapsedTime = time.Second * 10
backoff.Retry(func() error {
res = DBState{Instance: instance}
db, err := OpenDB(ctx, cfg, instance)
if err != nil {
res.Err = errf(err)
return res.Err
}
defer db.Close()
conn, err := db.Conn(ctx)
if err != nil {
res.Err = errf(err)
return res.Err
}
defer conn.Close()
role, epoch, err := loadRoleAndEpoch(ctx, conn)
if err != nil {
res.Err = errf(err)
return res.Err
}
res.Role = role
res.Epoch = epoch
loadVersion(ctx, conn, &res)
loadStatusRows(ctx, conn, &res)
loadDBRemotes(ctx, conn, &res)
return res.Err
}, bo)
return res
}
func loadVersion(ctx context.Context, conn *sql.Conn, state *DBState) {
if state.Err != nil {
return
}
row := conn.QueryRowContext(ctx, "SELECT dolt_version()")
if row.Err() != nil {
state.Err = fmt.Errorf("error loading dolt_version table function: %w", row.Err())
return
}
err := row.Scan(&state.Version)
if err != nil {
state.Err = fmt.Errorf("error scanning column of dolt_version table as string: %w", err)
return
}
}
func loadStatusRows(ctx context.Context, conn *sql.Conn, state *DBState) {
if state.Err != nil {
return
}
rows, err := conn.QueryContext(ctx, "SELECT `database`, role, epoch, standby_remote, replication_lag_millis, last_update, current_error FROM `dolt_cluster`.`dolt_cluster_status`")
if err != nil {
state.Err = fmt.Errorf("error loading dolt_cluster_status table: %w", err)
return
}
defer rows.Close()
for rows.Next() {
var status StatusRow
err = rows.Scan(&status.Database, &status.Role, &status.Epoch, &status.Remote, &status.ReplicationLag, &status.LastUpdate, &status.CurrentError)
if err != nil {
state.Err = fmt.Errorf("error scanning status row: %w", err)
return
}
state.Status = append(state.Status, status)
}
if rows.Err() != nil {
state.Err = fmt.Errorf("error loading dolt_cluster_status table: %w", err)
return
}
}
func loadDBRemotes(ctx context.Context, conn *sql.Conn, state *DBState) {
if state.Err != nil {
return
}
type key struct {
db string
remote string
}
keys := make(map[key]struct{})
for _, v := range state.Status {
keys[key{v.Database, v.Remote}] = struct{}{}
}
for k := range keys {
remote, err := loadDBRemote(ctx, conn, k.db, k.remote)
if err != nil {
state.Err = fmt.Errorf("error loading remote url for database: %v, remote %v: %w", k.db, k.remote, err)
return
}
state.Remotes = append(state.Remotes, remote)
}
}
func loadDBRemote(ctx context.Context, conn *sql.Conn, db, remote string) (DBRemote, error) {
_, err := conn.ExecContext(ctx, fmt.Sprintf("USE %s", db))
if err != nil {
return DBRemote{}, err
}
rows, err := conn.QueryContext(ctx, "SELECT url FROM dolt_remotes WHERE name = ?", remote)
if err != nil {
return DBRemote{}, err
}
var url string
if rows.Next() {
err = rows.Scan(&url)
if err != nil {
return DBRemote{}, fmt.Errorf("error loading db remote: could not scan url: %w", err)
}
} else if rows.Err() == nil {
return DBRemote{}, errors.New("error loading db remote: did not find a remote matching the name in the database")
}
if rows.Next() {
return DBRemote{}, errors.New("error loading db remote: found more than one a remote matching the name in the database")
}
if rows.Err() != nil {
return DBRemote{}, rows.Err()
}
return DBRemote{db, remote, url}, nil
}
func loadRoleAndEpoch(ctx context.Context, conn *sql.Conn) (string, int, error) {
var role string
var epoch int
rows, err := conn.QueryContext(ctx, "SELECT @@global.dolt_cluster_role, @@global.dolt_cluster_role_epoch")
if err != nil {
}
defer rows.Close()
if rows.Next() {
err = rows.Scan(&role, &epoch)
if err != nil {
return "", 0, err
}
} else if rows.Err() == nil {
return "", 0, errors.New("querying cluster_role and epoch should have return values, but did not")
}
if rows.Err() != nil {
return "", 0, rows.Err()
}
return role, epoch, nil
}
type StatusRow struct {
Database string
Role string
Epoch int
Remote string
ReplicationLag sql.NullInt64
LastUpdate sql.NullTime
CurrentError sql.NullString
}
type DBRemote struct {
Database string
Name string
URL string
}
type DBState struct {
Role string
Epoch int
Instance Instance
Status []StatusRow
Remotes []DBRemote
Version string
Err error
}
func LoadDBStates(ctx context.Context, cfg *Config, cluster Cluster) []DBState {
ret := make([]DBState, cluster.NumReplicas())
for i := 0; i < cluster.NumReplicas(); i++ {
instance := cluster.Instance(i)
ret[i] = LoadDBState(ctx, cfg, instance)
}
return ret
}
// Returns the current valid primary based on the dbstates. Returns an error if
// there is no primary or if there is more than one primary.
func CurrentPrimaryAndEpoch(dbstates []DBState) (int, int, error) {
highestepoch := 0
currentprimary := -1
for i := range dbstates {
if dbstates[i].Role == "primary" {
if currentprimary != -1 {
return -1, -1, fmt.Errorf("more than one reachable pod was in role primary: %s and %s", dbstates[currentprimary].Instance.Name(), dbstates[i].Instance.Name())
}
currentprimary = i
}
if dbstates[i].Epoch > highestepoch {
highestepoch = dbstates[i].Epoch
}
}
if currentprimary == -1 {
return -1, -1, errors.New("no reachable pod was in role primary")
}
return currentprimary, highestepoch, nil
}