-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
vttablet_process.go
567 lines (504 loc) · 19 KB
/
vttablet_process.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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
Copyright 2019 The Vitess Authors.
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 cluster
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path"
"reflect"
"strings"
"syscall"
"testing"
"time"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/log"
)
// VttabletProcess is a generic handle for a running vttablet .
// It can be spawned manually
type VttabletProcess struct {
Name string
Binary string
FileToLogQueries string
TabletUID int
TabletPath string
Cell string
Port int
GrpcPort int
Shard string
CommonArg VtctlProcess
LogDir string
TabletHostname string
Keyspace string
TabletType string
HealthCheckInterval int
BackupStorageImplementation string
FileBackupStorageRoot string
ServiceMap string
VtctldAddress string
Directory string
VerifyURL string
QueryzURL string
StatusDetailsURL string
EnableSemiSync bool
SupportsBackup bool
ServingStatus string
DbPassword string
DbPort int
VreplicationTabletType string
DbFlavor string
//Extra Args to be set before starting the vttablet process
ExtraArgs []string
proc *exec.Cmd
exit chan error
}
// Setup starts vttablet process with required arguements
func (vttablet *VttabletProcess) Setup() (err error) {
vttablet.proc = exec.Command(
vttablet.Binary,
"-topo_implementation", vttablet.CommonArg.TopoImplementation,
"-topo_global_server_address", vttablet.CommonArg.TopoGlobalAddress,
"-topo_global_root", vttablet.CommonArg.TopoGlobalRoot,
"-log_queries_to_file", vttablet.FileToLogQueries,
"-tablet-path", vttablet.TabletPath,
"-port", fmt.Sprintf("%d", vttablet.Port),
"-grpc_port", fmt.Sprintf("%d", vttablet.GrpcPort),
"-init_shard", vttablet.Shard,
"-log_dir", vttablet.LogDir,
"-tablet_hostname", vttablet.TabletHostname,
"-init_keyspace", vttablet.Keyspace,
"-init_tablet_type", vttablet.TabletType,
"-health_check_interval", fmt.Sprintf("%ds", vttablet.HealthCheckInterval),
"-enable_replication_reporter",
"-backup_storage_implementation", vttablet.BackupStorageImplementation,
"-file_backup_storage_root", vttablet.FileBackupStorageRoot,
"-service_map", vttablet.ServiceMap,
"-vtctld_addr", vttablet.VtctldAddress,
"-vtctld_addr", vttablet.VtctldAddress,
"-vreplication_tablet_type", vttablet.VreplicationTabletType,
)
if *isCoverage {
vttablet.proc.Args = append(vttablet.proc.Args, "-test.coverprofile="+getCoveragePath("vttablet.out"))
}
if *PerfTest {
vttablet.proc.Args = append(vttablet.proc.Args, "-pprof", fmt.Sprintf("cpu,waitSig,path=vttablet_pprof_%s", vttablet.Name))
}
if vttablet.SupportsBackup {
vttablet.proc.Args = append(vttablet.proc.Args, "-restore_from_backup")
}
if vttablet.EnableSemiSync {
vttablet.proc.Args = append(vttablet.proc.Args, "-enable_semi_sync")
}
if vttablet.DbFlavor != "" {
vttablet.proc.Args = append(vttablet.proc.Args, fmt.Sprintf("-db_flavor=%s", vttablet.DbFlavor))
}
vttablet.proc.Args = append(vttablet.proc.Args, vttablet.ExtraArgs...)
fname := path.Join(vttablet.LogDir, vttablet.TabletPath+"-vttablet-stderr.txt")
errFile, _ := os.Create(fname)
vttablet.proc.Stderr = errFile
vttablet.proc.Env = append(vttablet.proc.Env, os.Environ()...)
log.Infof("Running vttablet with command: %v", strings.Join(vttablet.proc.Args, " "))
err = vttablet.proc.Start()
if err != nil {
return
}
vttablet.exit = make(chan error)
go func() {
if vttablet.proc != nil {
vttablet.exit <- vttablet.proc.Wait()
}
}()
if vttablet.ServingStatus != "" {
if err = vttablet.WaitForTabletStatus(vttablet.ServingStatus); err != nil {
errFileContent, _ := os.ReadFile(fname)
if errFileContent != nil {
log.Infof("vttablet error:\n%s\n", string(errFileContent))
}
return fmt.Errorf("process '%s' timed out after 10s (err: %s)", vttablet.Name, err)
}
}
return nil
}
// GetStatus returns /debug/status endpoint result
func (vttablet *VttabletProcess) GetStatus() string {
URL := fmt.Sprintf("http://%s:%d/debug/status", vttablet.TabletHostname, vttablet.Port)
resp, err := http.Get(URL)
if err != nil {
return ""
}
if resp.StatusCode == 200 {
respByte, _ := io.ReadAll(resp.Body)
defer resp.Body.Close()
return string(respByte)
}
return ""
}
// GetVars gets the debug vars as map
func (vttablet *VttabletProcess) GetVars() map[string]interface{} {
resp, err := http.Get(vttablet.VerifyURL)
if err != nil {
return nil
}
if resp.StatusCode == 200 {
resultMap := make(map[string]interface{})
respByte, _ := io.ReadAll(resp.Body)
err := json.Unmarshal(respByte, &resultMap)
if err != nil {
return nil
}
return resultMap
}
return nil
}
// GetStatusDetails gets the status details
func (vttablet *VttabletProcess) GetStatusDetails() string {
resp, err := http.Get(vttablet.StatusDetailsURL)
if err != nil {
return fmt.Sprintf("Status details failed: %v", err.Error())
}
respByte, _ := io.ReadAll(resp.Body)
return string(respByte)
}
// WaitForStatus waits till desired status of tablet is reached
func (vttablet *VttabletProcess) WaitForStatus(status string) bool {
return vttablet.GetTabletStatus() == status
}
// GetTabletStatus returns the tablet state as seen in /debug/vars TabletStateName
func (vttablet *VttabletProcess) GetTabletStatus() string {
resultMap := vttablet.GetVars()
if resultMap != nil {
return reflect.ValueOf(resultMap["TabletStateName"]).String()
}
return ""
}
// GetTabletType returns the tablet type as seen in /debug/vars TabletType
func (vttablet *VttabletProcess) GetTabletType() string {
resultMap := vttablet.GetVars()
if resultMap != nil {
return reflect.ValueOf(resultMap["TabletType"]).String()
}
return ""
}
// WaitForTabletStatus waits for 10 second till expected status is reached
func (vttablet *VttabletProcess) WaitForTabletStatus(expectedStatus string) error {
return vttablet.WaitForTabletStatusesForTimeout([]string{expectedStatus}, 10*time.Second)
}
// WaitForTabletStatuses waits for 10 second till one of expected statuses is reached
func (vttablet *VttabletProcess) WaitForTabletStatuses(expectedStatuses []string) error {
return vttablet.WaitForTabletStatusesForTimeout(expectedStatuses, 10*time.Second)
}
// WaitForTabletTypes waits for 10 second till one of expected statuses is reached
func (vttablet *VttabletProcess) WaitForTabletTypes(expectedTypes []string) error {
return vttablet.WaitForTabletTypesForTimeout(expectedTypes, 10*time.Second)
}
// WaitForTabletStatusesForTimeout waits till the tablet reaches to any of the provided statuses
func (vttablet *VttabletProcess) WaitForTabletStatusesForTimeout(expectedStatuses []string, timeout time.Duration) error {
waitUntil := time.Now().Add(timeout)
var status string
for time.Now().Before(waitUntil) {
status = vttablet.GetTabletStatus()
if contains(expectedStatuses, status) {
return nil
}
select {
case err := <-vttablet.exit:
return fmt.Errorf("process '%s' exited prematurely (err: %s)", vttablet.Name, err)
default:
time.Sleep(300 * time.Millisecond)
}
}
return fmt.Errorf("Vttablet %s, current status = %s, expected status [%s] not reached, details: %v",
vttablet.TabletPath, status, strings.Join(expectedStatuses, ","), vttablet.GetStatusDetails())
}
// WaitForTabletTypesForTimeout waits till the tablet reaches to any of the provided types
func (vttablet *VttabletProcess) WaitForTabletTypesForTimeout(expectedTypes []string, timeout time.Duration) error {
waitUntil := time.Now().Add(timeout)
var tabletType string
for time.Now().Before(waitUntil) {
tabletType = vttablet.GetTabletType()
if contains(expectedTypes, tabletType) {
return nil
}
select {
case err := <-vttablet.exit:
return fmt.Errorf("process '%s' exited prematurely (err: %s)", vttablet.Name, err)
default:
time.Sleep(300 * time.Millisecond)
}
}
return fmt.Errorf("Vttablet %s, current type = %s, expected type [%s] not reached, status details: %v",
vttablet.TabletPath, tabletType, strings.Join(expectedTypes, ","), vttablet.GetStatusDetails())
}
func contains(arr []string, str string) bool {
for _, a := range arr {
if a == str {
return true
}
}
return false
}
// WaitForBinLogPlayerCount waits till binlog player count var matches
func (vttablet *VttabletProcess) WaitForBinLogPlayerCount(expectedCount int) error {
timeout := time.Now().Add(10 * time.Second)
for time.Now().Before(timeout) {
if vttablet.getVReplStreamCount() == fmt.Sprintf("%d", expectedCount) {
return nil
}
select {
case err := <-vttablet.exit:
return fmt.Errorf("process '%s' exited prematurely (err: %s)", vttablet.Name, err)
default:
time.Sleep(300 * time.Millisecond)
}
}
return fmt.Errorf("vttablet %s, expected status not reached", vttablet.TabletPath)
}
// WaitForBinlogServerState wait for the tablet's binlog server to be in the provided state.
func (vttablet *VttabletProcess) WaitForBinlogServerState(expectedStatus string) error {
timeout := time.Now().Add(10 * time.Second)
for time.Now().Before(timeout) {
if vttablet.getVarValue("UpdateStreamState") == expectedStatus {
return nil
}
select {
case err := <-vttablet.exit:
return fmt.Errorf("process '%s' exited prematurely (err: %s)", vttablet.Name, err)
default:
time.Sleep(300 * time.Millisecond)
}
}
return fmt.Errorf("vttablet %s, expected status not reached", vttablet.TabletPath)
}
func (vttablet *VttabletProcess) getVReplStreamCount() string {
return vttablet.getVarValue("VReplicationStreamCount")
}
func (vttablet *VttabletProcess) getVarValue(keyname string) string {
resultMap := vttablet.GetVars()
object := reflect.ValueOf(resultMap[keyname])
return fmt.Sprintf("%v", object)
}
// TearDown shuts down the running vttablet service and fails after 10 seconds
func (vttablet *VttabletProcess) TearDown() error {
return vttablet.TearDownWithTimeout(10 * time.Second)
}
// TearDownWithTimeout shuts down the running vttablet service and fails once the given
// duration has elapsed.
func (vttablet *VttabletProcess) TearDownWithTimeout(timeout time.Duration) error {
if vttablet.proc == nil || vttablet.exit == nil {
return nil
}
// Attempt graceful shutdown with SIGTERM first
vttablet.proc.Process.Signal(syscall.SIGTERM)
select {
case <-vttablet.exit:
vttablet.proc = nil
return nil
case <-time.After(timeout):
proc := vttablet.proc
if proc != nil {
vttablet.proc.Process.Kill()
vttablet.proc = nil
}
return <-vttablet.exit
}
}
// CreateDB creates the database for keyspace
func (vttablet *VttabletProcess) CreateDB(keyspace string) error {
_, _ = vttablet.QueryTablet(fmt.Sprintf("drop database IF EXISTS vt_%s", keyspace), keyspace, false)
_, err := vttablet.QueryTablet(fmt.Sprintf("create database IF NOT EXISTS vt_%s", keyspace), keyspace, false)
return err
}
// QueryTablet lets you execute a query in this tablet and get the result
func (vttablet *VttabletProcess) QueryTablet(query string, keyspace string, useDb bool) (*sqltypes.Result, error) {
if !useDb {
keyspace = ""
}
dbParams := NewConnParams(vttablet.DbPort, vttablet.DbPassword, path.Join(vttablet.Directory, "mysql.sock"), keyspace)
conn, err := vttablet.conn(&dbParams)
if err != nil {
return nil, err
}
defer conn.Close()
return executeQuery(conn, query)
}
func (vttablet *VttabletProcess) defaultConn(dbname string) (*mysql.Conn, error) {
dbParams := mysql.ConnParams{
Uname: "vt_dba",
UnixSocket: path.Join(vttablet.Directory, "mysql.sock"),
DbName: dbname,
}
if vttablet.DbPassword != "" {
dbParams.Pass = vttablet.DbPassword
}
return vttablet.conn(&dbParams)
}
func (vttablet *VttabletProcess) conn(dbParams *mysql.ConnParams) (*mysql.Conn, error) {
ctx := context.Background()
return mysql.Connect(ctx, dbParams)
}
// QueryTabletWithDB lets you execute query on a specific DB in this tablet and get the result
func (vttablet *VttabletProcess) QueryTabletWithDB(query string, dbname string) (*sqltypes.Result, error) {
conn, err := vttablet.defaultConn(dbname)
if err != nil {
return nil, err
}
defer conn.Close()
return executeQuery(conn, query)
}
func executeQuery(dbConn *mysql.Conn, query string) (*sqltypes.Result, error) {
return dbConn.ExecuteFetch(query, 10000, true)
}
// GetDBVar returns first matching database variable's value
func (vttablet *VttabletProcess) GetDBVar(varName string, ksName string) (string, error) {
return vttablet.getDBSystemValues("variables", varName, ksName)
}
// GetDBStatus returns first matching database variable's value
func (vttablet *VttabletProcess) GetDBStatus(status string, ksName string) (string, error) {
return vttablet.getDBSystemValues("status", status, ksName)
}
func (vttablet *VttabletProcess) getDBSystemValues(placeholder string, value string, ksName string) (string, error) {
output, err := vttablet.QueryTablet(fmt.Sprintf("show %s like '%s'", placeholder, value), ksName, true)
if err != nil || output.Rows == nil {
return "", err
}
if len(output.Rows) > 0 {
rowBytes, err := output.Rows[0][1].ToBytes()
if err != nil {
return "", err
}
return string(rowBytes), nil
}
return "", nil
}
// ToggleProfiling enables or disables the configured CPU profiler on this vttablet
func (vttablet *VttabletProcess) ToggleProfiling() error {
return vttablet.proc.Process.Signal(syscall.SIGUSR1)
}
// WaitForVReplicationToCatchup waits for "workflow" to finish copying
func (vttablet *VttabletProcess) WaitForVReplicationToCatchup(t testing.TB, workflow, database string, duration time.Duration) {
queries := [3]string{
fmt.Sprintf(`select count(*) from _vt.vreplication where workflow = "%s" and db_name = "%s" and pos = ''`, workflow, database),
"select count(*) from information_schema.tables where table_schema='_vt' and table_name='copy_state' limit 1;",
fmt.Sprintf(`select count(*) from _vt.copy_state where vrepl_id in (select id from _vt.vreplication where workflow = "%s" and db_name = "%s" )`, workflow, database),
}
results := [3]string{"[INT64(0)]", "[INT64(1)]", "[INT64(0)]"}
conn, err := vttablet.defaultConn("")
if err != nil {
t.Fatal(err)
}
defer conn.Close()
var lastChecked time.Time
for ind, query := range queries {
waitDuration := 500 * time.Millisecond
for duration > 0 {
log.Infof("Executing query %s on %s", query, vttablet.Name)
lastChecked = time.Now()
qr, err := conn.ExecuteFetch(query, 1000, true)
if err != nil {
t.Fatal(err)
}
if qr != nil && qr.Rows != nil && len(qr.Rows) > 0 && fmt.Sprintf("%v", qr.Rows[0]) == string(results[ind]) {
break
} else {
log.Infof("In WaitForVReplicationToCatchup: %s %+v", query, qr.Rows)
}
time.Sleep(waitDuration)
duration -= waitDuration
}
if duration <= 0 {
t.Fatalf("WaitForVReplicationToCatchup timed out for workflow %s, keyspace %s", workflow, database)
}
}
log.Infof("WaitForVReplicationToCatchup succeeded at %v", lastChecked)
}
// BulkLoad performs a bulk load of rows into a given vttablet.
func (vttablet *VttabletProcess) BulkLoad(t testing.TB, db, table string, bulkInsert func(io.Writer)) {
tmpbulk, err := os.CreateTemp(path.Join(vttablet.Directory, "tmp"), "bulk_load")
if err != nil {
t.Fatalf("failed to create tmp file for loading: %v", err)
}
defer os.Remove(tmpbulk.Name())
log.Infof("create temporary file for bulk loading %q", tmpbulk.Name())
bufStart := time.Now()
bulkBuffer := bufio.NewWriter(tmpbulk)
bulkInsert(bulkBuffer)
bulkBuffer.Flush()
pos, _ := tmpbulk.Seek(0, 1)
bufFinish := time.Now()
log.Infof("bulk loading %d bytes from %q...", pos, tmpbulk.Name())
if err := tmpbulk.Close(); err != nil {
t.Fatal(err)
}
conn, err := vttablet.defaultConn("vt_" + db)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
query := fmt.Sprintf("LOAD DATA INFILE '%s' INTO TABLE `%s` FIELDS TERMINATED BY ',' ENCLOSED BY '\"'", tmpbulk.Name(), table)
_, err = conn.ExecuteFetch(query, 1, false)
if err != nil {
t.Fatal(err)
}
end := time.Now()
log.Infof("bulk insert successful (write tmp file = %v, mysql bulk load = %v, total = %v",
bufFinish.Sub(bufStart), end.Sub(bufFinish), end.Sub(bufStart))
}
// IsShutdown returns whether a vttablet is shutdown or not
func (vttablet *VttabletProcess) IsShutdown() bool {
return vttablet.proc == nil
}
// VttabletProcessInstance returns a VttabletProcess handle for vttablet process
// configured with the given Config.
// The process must be manually started by calling setup()
func VttabletProcessInstance(port int, grpcPort int, tabletUID int, cell string, shard string, keyspace string, vtctldPort int, tabletType string, topoPort int, hostname string, tmpDirectory string, extraArgs []string, enableSemiSync bool) *VttabletProcess {
vtctl := VtctlProcessInstance(topoPort, hostname)
vttablet := &VttabletProcess{
Name: "vttablet",
Binary: "vttablet",
FileToLogQueries: path.Join(tmpDirectory, fmt.Sprintf("/vt_%010d_querylog.txt", tabletUID)),
Directory: path.Join(os.Getenv("VTDATAROOT"), fmt.Sprintf("/vt_%010d", tabletUID)),
TabletPath: fmt.Sprintf("%s-%010d", cell, tabletUID),
ServiceMap: "grpc-queryservice,grpc-tabletmanager,grpc-updatestream,grpc-throttler",
LogDir: tmpDirectory,
Shard: shard,
TabletHostname: hostname,
Keyspace: keyspace,
TabletType: "replica",
CommonArg: *vtctl,
HealthCheckInterval: 5,
Port: port,
GrpcPort: grpcPort,
VtctldAddress: fmt.Sprintf("http://%s:%d", hostname, vtctldPort),
ExtraArgs: extraArgs,
EnableSemiSync: enableSemiSync,
SupportsBackup: true,
ServingStatus: "NOT_SERVING",
BackupStorageImplementation: "file",
FileBackupStorageRoot: path.Join(os.Getenv("VTDATAROOT"), "/backups"),
VreplicationTabletType: "replica",
TabletUID: tabletUID,
}
if tabletType == "rdonly" {
vttablet.TabletType = tabletType
}
vttablet.VerifyURL = fmt.Sprintf("http://%s:%d/debug/vars", hostname, port)
vttablet.QueryzURL = fmt.Sprintf("http://%s:%d/queryz", hostname, port)
vttablet.StatusDetailsURL = fmt.Sprintf("http://%s:%d/debug/status_details", hostname, port)
return vttablet
}