-
Notifications
You must be signed in to change notification settings - Fork 2k
/
mysql_daemon.go
475 lines (389 loc) · 16 KB
/
mysql_daemon.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
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mysqlctl
import (
"fmt"
"reflect"
"strings"
"github.com/youtube/vitess/go/sqldb"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/stats"
"github.com/youtube/vitess/go/vt/dbconnpool"
"github.com/youtube/vitess/go/vt/mysqlctl/replication"
"github.com/youtube/vitess/go/vt/mysqlctl/tmutils"
"github.com/youtube/vitess/go/vt/vttest/fakesqldb"
"golang.org/x/net/context"
tabletmanagerdatapb "github.com/youtube/vitess/go/vt/proto/tabletmanagerdata"
)
// MysqlDaemon is the interface we use for abstracting Mysqld.
type MysqlDaemon interface {
// Cnf returns the underlying mycnf
Cnf() *Mycnf
// TabletDir returns the tablet directory.
TabletDir() string
// methods related to mysql running or not
Start(ctx context.Context) error
Shutdown(ctx context.Context, waitForMysqld bool) error
RunMysqlUpgrade() error
Wait(ctx context.Context) error
// GetMysqlPort returns the current port mysql is listening on.
GetMysqlPort() (int32, error)
// replication related methods
SlaveStatus() (replication.Status, error)
SetSemiSyncEnabled(master, slave bool) error
SemiSyncEnabled() (master, slave bool)
// reparenting related methods
ResetReplicationCommands() ([]string, error)
MasterPosition() (replication.Position, error)
IsReadOnly() (bool, error)
SetReadOnly(on bool) error
SetSlavePositionCommands(pos replication.Position) ([]string, error)
SetMasterCommands(masterHost string, masterPort int) ([]string, error)
WaitForReparentJournal(ctx context.Context, timeCreatedNS int64) error
// DemoteMaster waits for all current transactions to finish,
// and returns the current replication position. It will not
// change the read_only state of the server.
DemoteMaster() (replication.Position, error)
WaitMasterPos(context.Context, replication.Position) error
// PromoteSlave makes the slave the new master. It will not change
// the read_only state of the server.
PromoteSlave(map[string]string) (replication.Position, error)
// Schema related methods
GetSchema(dbName string, tables, excludeTables []string, includeViews bool) (*tabletmanagerdatapb.SchemaDefinition, error)
PreflightSchemaChange(dbName string, changes []string) ([]*tabletmanagerdatapb.SchemaChangeResult, error)
ApplySchemaChange(dbName string, change *tmutils.SchemaChange) (*tabletmanagerdatapb.SchemaChangeResult, error)
// GetAppConnection returns a app connection to be able to talk to the database.
GetAppConnection(ctx context.Context) (dbconnpool.PoolConnection, error)
// GetDbaConnection returns a dba connection.
GetDbaConnection() (*dbconnpool.DBConnection, error)
// ExecuteSuperQueryList executes a list of queries, no result
ExecuteSuperQueryList(ctx context.Context, queryList []string) error
// FetchSuperQuery executes one query, returns the result
FetchSuperQuery(ctx context.Context, query string) (*sqltypes.Result, error)
// NewSlaveConnection returns a SlaveConnection to the database.
NewSlaveConnection() (*SlaveConnection, error)
// EnableBinlogPlayback enables playback of binlog events
EnableBinlogPlayback() error
// DisableBinlogPlayback disable playback of binlog events
DisableBinlogPlayback() error
// Close will close this instance of Mysqld. It will wait for all dba
// queries to be finished.
Close()
}
// FakeMysqlDaemon implements MysqlDaemon and allows the user to fake
// everything.
type FakeMysqlDaemon struct {
db *fakesqldb.DB
// Mycnf will be returned by Cnf()
Mycnf *Mycnf
// Running is used by Start / Shutdown
Running bool
// MysqlPort will be returned by GetMysqlPort(). Set to -1 to
// return an error.
MysqlPort int32
// Replicating is updated when calling StartSlave / StopSlave
// (it is not used at all when calling SlaveStatus, it is the
// test owner responsability to have these two match)
Replicating bool
// ResetReplicationResult is returned by ResetReplication
ResetReplicationResult []string
// ResetReplicationError is returned by ResetReplication
ResetReplicationError error
// CurrentMasterPosition is returned by MasterPosition
// and SlaveStatus
CurrentMasterPosition replication.Position
// SlaveStatusError is used by SlaveStatus
SlaveStatusError error
// CurrentMasterHost is returned by SlaveStatus
CurrentMasterHost string
// CurrentMasterport is returned by SlaveStatus
CurrentMasterPort int
// SecondsBehindMaster is returned by SlaveStatus
SecondsBehindMaster uint
// ReadOnly is the current value of the flag
ReadOnly bool
// SetSlavePositionCommandsPos is matched against the input
// of SetSlavePositionCommands. If it doesn't match,
// SetSlavePositionCommands will return an error.
SetSlavePositionCommandsPos replication.Position
// SetSlavePositionCommandsResult is what
// SetSlavePositionCommands will return
SetSlavePositionCommandsResult []string
// SetMasterCommandsInput is matched against the input
// of SetMasterCommands (as "%v:%v"). If it doesn't match,
// SetMasterCommands will return an error.
SetMasterCommandsInput string
// SetMasterCommandsResult is what
// SetMasterCommands will return
SetMasterCommandsResult []string
// DemoteMasterPosition is returned by DemoteMaster
DemoteMasterPosition replication.Position
// WaitMasterPosition is checked by WaitMasterPos, if the
// same it returns nil, if different it returns an error
WaitMasterPosition replication.Position
// PromoteSlaveResult is returned by PromoteSlave
PromoteSlaveResult replication.Position
// SchemaFunc provides the return value for GetSchema.
// If not defined, the "Schema" field will be used instead, see below.
SchemaFunc func() (*tabletmanagerdatapb.SchemaDefinition, error)
// Schema will be returned by GetSchema. If nil we'll
// return an error.
Schema *tabletmanagerdatapb.SchemaDefinition
// PreflightSchemaChangeResult will be returned by PreflightSchemaChange.
// If nil we'll return an error.
PreflightSchemaChangeResult []*tabletmanagerdatapb.SchemaChangeResult
// ApplySchemaChangeResult will be returned by ApplySchemaChange.
// If nil we'll return an error.
ApplySchemaChangeResult *tabletmanagerdatapb.SchemaChangeResult
// DbAppConnectionFactory is the factory for making fake db app connections
DbAppConnectionFactory func() (dbconnpool.PoolConnection, error)
// ExpectedExecuteSuperQueryList is what we expect
// ExecuteSuperQueryList to be called with. If it doesn't
// match, ExecuteSuperQueryList will return an error.
// Note each string is just a substring if it begins with SUB,
// so we support partial queries (usefull when queries contain
// data fields like timestamps)
ExpectedExecuteSuperQueryList []string
// ExpectedExecuteSuperQueryCurrent is the current index of the queries
// we expect
ExpectedExecuteSuperQueryCurrent int
// FetchSuperQueryResults is used by FetchSuperQuery
FetchSuperQueryMap map[string]*sqltypes.Result
// BinlogPlayerEnabled is used by {Enable,Disable}BinlogPlayer
BinlogPlayerEnabled bool
// SemiSyncMasterEnabled represents the state of rpl_semi_sync_master_enabled.
SemiSyncMasterEnabled bool
// SemiSyncSlaveEnabled represents the state of rpl_semi_sync_slave_enabled.
SemiSyncSlaveEnabled bool
}
// NewFakeMysqlDaemon returns a FakeMysqlDaemon where mysqld appears
// to be running
func NewFakeMysqlDaemon(db *fakesqldb.DB) *FakeMysqlDaemon {
return &FakeMysqlDaemon{
db: db,
Running: true,
}
}
// Cnf is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) Cnf() *Mycnf {
return fmd.Mycnf
}
// TabletDir is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) TabletDir() string {
return ""
}
// Start is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) Start(ctx context.Context) error {
if fmd.Running {
return fmt.Errorf("fake mysql daemon already running")
}
fmd.Running = true
return nil
}
// Shutdown is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) Shutdown(ctx context.Context, waitForMysqld bool) error {
if !fmd.Running {
return fmt.Errorf("fake mysql daemon not running")
}
fmd.Running = false
return nil
}
// RunMysqlUpgrade is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) RunMysqlUpgrade() error {
return nil
}
// Wait is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) Wait(ctx context.Context) error {
return nil
}
// GetMysqlPort is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) GetMysqlPort() (int32, error) {
if fmd.MysqlPort == -1 {
return 0, fmt.Errorf("FakeMysqlDaemon.GetMysqlPort returns an error")
}
return fmd.MysqlPort, nil
}
// SlaveStatus is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) SlaveStatus() (replication.Status, error) {
if fmd.SlaveStatusError != nil {
return replication.Status{}, fmd.SlaveStatusError
}
return replication.Status{
Position: fmd.CurrentMasterPosition,
SecondsBehindMaster: fmd.SecondsBehindMaster,
SlaveIORunning: fmd.Replicating,
SlaveSQLRunning: fmd.Replicating,
MasterHost: fmd.CurrentMasterHost,
MasterPort: fmd.CurrentMasterPort,
}, nil
}
// ResetReplicationCommands is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) ResetReplicationCommands() ([]string, error) {
return fmd.ResetReplicationResult, fmd.ResetReplicationError
}
// MasterPosition is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) MasterPosition() (replication.Position, error) {
return fmd.CurrentMasterPosition, nil
}
// IsReadOnly is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) IsReadOnly() (bool, error) {
return fmd.ReadOnly, nil
}
// SetReadOnly is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) SetReadOnly(on bool) error {
fmd.ReadOnly = on
return nil
}
// SetSlavePositionCommands is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) SetSlavePositionCommands(pos replication.Position) ([]string, error) {
if !reflect.DeepEqual(fmd.SetSlavePositionCommandsPos, pos) {
return nil, fmt.Errorf("wrong pos for SetSlavePositionCommands: expected %v got %v", fmd.SetSlavePositionCommandsPos, pos)
}
return fmd.SetSlavePositionCommandsResult, nil
}
// SetMasterCommands is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) SetMasterCommands(masterHost string, masterPort int) ([]string, error) {
input := fmt.Sprintf("%v:%v", masterHost, masterPort)
if fmd.SetMasterCommandsInput != input {
return nil, fmt.Errorf("wrong input for SetMasterCommands: expected %v got %v", fmd.SetMasterCommandsInput, input)
}
return fmd.SetMasterCommandsResult, nil
}
// WaitForReparentJournal is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) WaitForReparentJournal(ctx context.Context, timeCreatedNS int64) error {
return nil
}
// DemoteMaster is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) DemoteMaster() (replication.Position, error) {
return fmd.DemoteMasterPosition, nil
}
// WaitMasterPos is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) WaitMasterPos(_ context.Context, pos replication.Position) error {
if reflect.DeepEqual(fmd.WaitMasterPosition, pos) {
return nil
}
return fmt.Errorf("wrong input for WaitMasterPos: expected %v got %v", fmd.WaitMasterPosition, pos)
}
// PromoteSlave is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) PromoteSlave(hookExtraEnv map[string]string) (replication.Position, error) {
return fmd.PromoteSlaveResult, nil
}
// ExecuteSuperQueryList is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) ExecuteSuperQueryList(ctx context.Context, queryList []string) error {
for _, query := range queryList {
// test we still have a query to compare
if fmd.ExpectedExecuteSuperQueryCurrent >= len(fmd.ExpectedExecuteSuperQueryList) {
return fmt.Errorf("unexpected extra query in ExecuteSuperQueryList: %v", query)
}
// compare the query
expected := fmd.ExpectedExecuteSuperQueryList[fmd.ExpectedExecuteSuperQueryCurrent]
fmd.ExpectedExecuteSuperQueryCurrent++
if strings.HasPrefix(expected, "SUB") {
// remove the SUB from the expected,
// and truncate the query to length(expected)
expected = expected[3:]
if len(query) > len(expected) {
query = query[:len(expected)]
}
}
if expected != query {
return fmt.Errorf("wrong query for ExecuteSuperQueryList: expected %v got %v", expected, query)
}
// intercept some queries to update our status
switch query {
case SQLStartSlave:
fmd.Replicating = true
case SQLStopSlave:
fmd.Replicating = false
}
}
return nil
}
// FetchSuperQuery returns the results from the map, if any
func (fmd *FakeMysqlDaemon) FetchSuperQuery(ctx context.Context, query string) (*sqltypes.Result, error) {
if fmd.FetchSuperQueryMap == nil {
return nil, fmt.Errorf("unexpected query: %v", query)
}
qr, ok := fmd.FetchSuperQueryMap[query]
if !ok {
return nil, fmt.Errorf("unexpected query: %v", query)
}
return qr, nil
}
// NewSlaveConnection is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) NewSlaveConnection() (*SlaveConnection, error) {
panic(fmt.Errorf("not implemented on FakeMysqlDaemon"))
}
// EnableBinlogPlayback is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) EnableBinlogPlayback() error {
if fmd.BinlogPlayerEnabled {
return fmt.Errorf("binlog player already enabled")
}
fmd.BinlogPlayerEnabled = true
return nil
}
// DisableBinlogPlayback disable playback of binlog events
func (fmd *FakeMysqlDaemon) DisableBinlogPlayback() error {
if fmd.BinlogPlayerEnabled {
return fmt.Errorf("binlog player already disabled")
}
fmd.BinlogPlayerEnabled = false
return nil
}
// Close is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) Close() {
}
// CheckSuperQueryList returns an error if all the queries we expected
// haven't been seen.
func (fmd *FakeMysqlDaemon) CheckSuperQueryList() error {
if fmd.ExpectedExecuteSuperQueryCurrent != len(fmd.ExpectedExecuteSuperQueryList) {
return fmt.Errorf("SuperQueryList wasn't consumed, saw %v queries, was expecting %v", fmd.ExpectedExecuteSuperQueryCurrent, len(fmd.ExpectedExecuteSuperQueryList))
}
return nil
}
// GetSchema is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) GetSchema(dbName string, tables, excludeTables []string, includeViews bool) (*tabletmanagerdatapb.SchemaDefinition, error) {
if fmd.SchemaFunc != nil {
return fmd.SchemaFunc()
}
if fmd.Schema == nil {
return nil, fmt.Errorf("no schema defined")
}
return tmutils.FilterTables(fmd.Schema, tables, excludeTables, includeViews)
}
// PreflightSchemaChange is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) PreflightSchemaChange(dbName string, changes []string) ([]*tabletmanagerdatapb.SchemaChangeResult, error) {
if fmd.PreflightSchemaChangeResult == nil {
return nil, fmt.Errorf("no preflight result defined")
}
return fmd.PreflightSchemaChangeResult, nil
}
// ApplySchemaChange is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) ApplySchemaChange(dbName string, change *tmutils.SchemaChange) (*tabletmanagerdatapb.SchemaChangeResult, error) {
if fmd.ApplySchemaChangeResult == nil {
return nil, fmt.Errorf("no apply schema defined")
}
return fmd.ApplySchemaChangeResult, nil
}
// GetAppConnection is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) GetAppConnection(ctx context.Context) (dbconnpool.PoolConnection, error) {
if fmd.DbAppConnectionFactory == nil {
return nil, fmt.Errorf("no DbAppConnectionFactory set in this FakeMysqlDaemon")
}
return fmd.DbAppConnectionFactory()
}
// GetDbaConnection is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) GetDbaConnection() (*dbconnpool.DBConnection, error) {
return dbconnpool.NewDBConnection(&sqldb.ConnParams{Engine: fmd.db.Name}, stats.NewTimings(""))
}
// SetSemiSyncEnabled is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) SetSemiSyncEnabled(master, slave bool) error {
fmd.SemiSyncMasterEnabled = master
fmd.SemiSyncSlaveEnabled = slave
return nil
}
// SemiSyncEnabled is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) SemiSyncEnabled() (master, slave bool) {
return fmd.SemiSyncMasterEnabled, fmd.SemiSyncSlaveEnabled
}