-
Notifications
You must be signed in to change notification settings - Fork 2k
/
fakemysqldaemon.go
664 lines (554 loc) · 21.6 KB
/
fakemysqldaemon.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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/*
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 fakemysqldaemon
import (
"fmt"
"reflect"
"strings"
"sync"
"time"
"context"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/mysql/fakesqldb"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/sync2"
"vitess.io/vitess/go/vt/dbconnpool"
"vitess.io/vitess/go/vt/mysqlctl"
"vitess.io/vitess/go/vt/mysqlctl/tmutils"
querypb "vitess.io/vitess/go/vt/proto/query"
tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata"
)
// FakeMysqlDaemon implements MysqlDaemon and allows the user to fake
// everything.
type FakeMysqlDaemon struct {
mu sync.Mutex
// db is the fake SQL DB we may use for some queries.
db *fakesqldb.DB
// appPool is set if db is set.
appPool *dbconnpool.ConnectionPool
// Running is used by Start / Shutdown
Running bool
// StartupTime is used to simulate mysqlds that take some time to respond
// to a "start" command. It is used by Start.
StartupTime time.Duration
// ShutdownTime is used to simulate mysqlds that take some time to respond
// to a "stop" request (i.e. a wedged systemd unit). It is used by Shutdown.
ShutdownTime time.Duration
// MysqlPort will be returned by GetMysqlPort(). Set to -1 to
// return an error.
MysqlPort sync2.AtomicInt32
// Replicating is updated when calling StartReplication / StopReplication
// (it is not used at all when calling ReplicationStatus, it is the
// test owner responsibility to have these two match)
Replicating bool
// IOThreadRunning is always true except in one testcase
// where we want to test error handling during SetReplicationSource
IOThreadRunning bool
// CurrentPrimaryPosition is returned by PrimaryPosition
// and ReplicationStatus
CurrentPrimaryPosition mysql.Position
// CurrentSourceFilePosition is used to determine the executed file based positioning of the replication source.
CurrentSourceFilePosition mysql.Position
// ReplicationStatusError is used by ReplicationStatus
ReplicationStatusError error
// StartReplicationError is used by StartReplication
StartReplicationError error
// PromoteLag is the time for which Promote will stall
PromoteLag time.Duration
// PrimaryStatusError is used by PrimaryStatus
PrimaryStatusError error
// CurrentSourceHost is returned by ReplicationStatus
CurrentSourceHost string
// CurrentSourcePort is returned by ReplicationStatus
CurrentSourcePort int
// ReplicationLagSeconds is returned by ReplicationStatus
ReplicationLagSeconds uint
// ReadOnly is the current value of the flag
ReadOnly bool
// SuperReadOnly is the current value of the flag
SuperReadOnly bool
// SetReplicationPositionPos is matched against the input of SetReplicationPosition.
// If it doesn't match, SetReplicationPosition will return an error.
SetReplicationPositionPos mysql.Position
// StartReplicationUntilAfterPos is matched against the input
StartReplicationUntilAfterPos mysql.Position
// SetReplicationSourceInputs are matched against the input of SetReplicationSource
// (as "%v:%v"). If all of them don't match, SetReplicationSource will return an error.
SetReplicationSourceInputs []string
// SetReplicationSourceError is used by SetReplicationSource
SetReplicationSourceError error
// WaitPrimaryPositions is checked by WaitSourcePos, if the value is found
// in it, then the function returns nil, else the function returns an error
WaitPrimaryPositions []mysql.Position
// PromoteResult is returned by Promote
PromoteResult mysql.Position
// PromoteError is used by Promote
PromoteError error
// 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
// 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 (useful 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 sync2.AtomicBool
// SemiSyncPrimaryEnabled represents the state of rpl_semi_sync_master_enabled.
SemiSyncPrimaryEnabled bool
// SemiSyncReplicaEnabled represents the state of rpl_semi_sync_slave_enabled.
SemiSyncReplicaEnabled bool
// TimeoutHook is a func that can be called at the beginning of any method to fake a timeout.
// all a test needs to do is make it { return context.DeadlineExceeded }
TimeoutHook func() error
}
// NewFakeMysqlDaemon returns a FakeMysqlDaemon where mysqld appears
// to be running, based on a fakesqldb.DB.
// 'db' can be nil if the test doesn't use a database at all.
func NewFakeMysqlDaemon(db *fakesqldb.DB) *FakeMysqlDaemon {
result := &FakeMysqlDaemon{
db: db,
Running: true,
IOThreadRunning: true,
}
if db != nil {
result.appPool = dbconnpool.NewConnectionPool("AppConnPool", 5, time.Minute, 0)
result.appPool.Open(db.ConnParams())
}
return result
}
// Start is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) Start(ctx context.Context, cnf *mysqlctl.Mycnf, mysqldArgs ...string) error {
if fmd.Running {
return fmt.Errorf("fake mysql daemon already running")
}
if fmd.StartupTime > 0 {
select {
case <-time.After(fmd.StartupTime):
case <-ctx.Done():
return ctx.Err()
}
}
fmd.Running = true
return nil
}
// Shutdown is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) Shutdown(ctx context.Context, cnf *mysqlctl.Mycnf, waitForMysqld bool) error {
if !fmd.Running {
return fmt.Errorf("fake mysql daemon not running")
}
if fmd.ShutdownTime > 0 {
select {
case <-time.After(fmd.ShutdownTime):
case <-ctx.Done():
return ctx.Err()
}
}
fmd.Running = false
return nil
}
// RunMysqlUpgrade is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) RunMysqlUpgrade() error {
return nil
}
// ReinitConfig is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) ReinitConfig(ctx context.Context, cnf *mysqlctl.Mycnf) error {
return nil
}
// RefreshConfig is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) RefreshConfig(ctx context.Context, cnf *mysqlctl.Mycnf) error {
return nil
}
// Wait is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) Wait(ctx context.Context, cnf *mysqlctl.Mycnf) error {
return nil
}
// GetMysqlPort is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) GetMysqlPort() (int32, error) {
if fmd.MysqlPort.Get() == -1 {
return 0, fmt.Errorf("FakeMysqlDaemon.GetMysqlPort returns an error")
}
return fmd.MysqlPort.Get(), nil
}
// GetServerID is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) GetServerID(ctx context.Context) (uint32, error) {
return 1, nil
}
// GetServerUUID is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) GetServerUUID(ctx context.Context) (string, error) {
return "000000", nil
}
// CurrentPrimaryPositionLocked is thread-safe
func (fmd *FakeMysqlDaemon) CurrentPrimaryPositionLocked(pos mysql.Position) {
fmd.mu.Lock()
defer fmd.mu.Unlock()
fmd.CurrentPrimaryPosition = pos
}
// ReplicationStatus is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) ReplicationStatus() (mysql.ReplicationStatus, error) {
if fmd.ReplicationStatusError != nil {
return mysql.ReplicationStatus{}, fmd.ReplicationStatusError
}
fmd.mu.Lock()
defer fmd.mu.Unlock()
return mysql.ReplicationStatus{
Position: fmd.CurrentPrimaryPosition,
FilePosition: fmd.CurrentSourceFilePosition,
RelayLogSourceBinlogEquivalentPosition: fmd.CurrentSourceFilePosition,
ReplicationLagSeconds: fmd.ReplicationLagSeconds,
// implemented as AND to avoid changing all tests that were
// previously using Replicating = false
IOState: mysql.ReplicationStatusToState(fmt.Sprintf("%v", fmd.Replicating && fmd.IOThreadRunning)),
SQLState: mysql.ReplicationStatusToState(fmt.Sprintf("%v", fmd.Replicating)),
SourceHost: fmd.CurrentSourceHost,
SourcePort: fmd.CurrentSourcePort,
}, nil
}
// PrimaryStatus is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) PrimaryStatus(ctx context.Context) (mysql.PrimaryStatus, error) {
if fmd.PrimaryStatusError != nil {
return mysql.PrimaryStatus{}, fmd.PrimaryStatusError
}
return mysql.PrimaryStatus{
Position: fmd.CurrentPrimaryPosition,
FilePosition: fmd.CurrentSourceFilePosition,
}, nil
}
// GetGTIDPurged is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) GetGTIDPurged(ctx context.Context) (mysql.Position, error) {
return mysql.Position{}, nil
}
// ResetReplication is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) ResetReplication(ctx context.Context) error {
return fmd.ExecuteSuperQueryList(ctx, []string{
"FAKE RESET ALL REPLICATION",
})
}
// ResetReplicationParameters is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) ResetReplicationParameters(ctx context.Context) error {
return fmd.ExecuteSuperQueryList(ctx, []string{
"FAKE RESET REPLICA ALL",
})
}
// GetBinlogInformation is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) GetBinlogInformation(ctx context.Context) (binlogFormat string, logEnabled bool, logReplicaUpdate bool, binlogRowImage string, err error) {
return "ROW", true, true, "FULL", fmd.ExecuteSuperQueryList(ctx, []string{
"FAKE select @@global",
})
}
// GetGTIDMode is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) GetGTIDMode(ctx context.Context) (gtidMode string, err error) {
return "ON", fmd.ExecuteSuperQueryList(ctx, []string{
"FAKE select @@global",
})
}
// PrimaryPosition is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) PrimaryPosition() (mysql.Position, error) {
return fmd.CurrentPrimaryPosition, 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
}
// SetSuperReadOnly is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) SetSuperReadOnly(on bool) error {
fmd.SuperReadOnly = on
fmd.ReadOnly = on
return nil
}
// StartReplication is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) StartReplication(hookExtraEnv map[string]string) error {
if fmd.StartReplicationError != nil {
return fmd.StartReplicationError
}
return fmd.ExecuteSuperQueryList(context.Background(), []string{
"START SLAVE",
})
}
// RestartReplication is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) RestartReplication(hookExtraEnv map[string]string) error {
return fmd.ExecuteSuperQueryList(context.Background(), []string{
"STOP SLAVE",
"RESET SLAVE",
"START SLAVE",
})
}
// StartReplicationUntilAfter is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) StartReplicationUntilAfter(ctx context.Context, pos mysql.Position) error {
if !reflect.DeepEqual(fmd.StartReplicationUntilAfterPos, pos) {
return fmt.Errorf("wrong pos for StartReplicationUntilAfter: expected %v got %v", fmd.SetReplicationPositionPos, pos)
}
return fmd.ExecuteSuperQueryList(context.Background(), []string{
"START SLAVE UNTIL AFTER",
})
}
// StopReplication is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) StopReplication(hookExtraEnv map[string]string) error {
return fmd.ExecuteSuperQueryList(context.Background(), []string{
"STOP SLAVE",
})
}
// StopIOThread is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) StopIOThread(ctx context.Context) error {
return fmd.ExecuteSuperQueryList(context.Background(), []string{
"STOP SLAVE IO_THREAD",
})
}
// SetReplicationPosition is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) SetReplicationPosition(ctx context.Context, pos mysql.Position) error {
if !reflect.DeepEqual(fmd.SetReplicationPositionPos, pos) {
return fmt.Errorf("wrong pos for SetReplicationPosition: expected %v got %v", fmd.SetReplicationPositionPos, pos)
}
return fmd.ExecuteSuperQueryList(ctx, []string{
"FAKE SET SLAVE POSITION",
})
}
// SetReplicationSource is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) SetReplicationSource(ctx context.Context, host string, port int, stopReplicationBefore bool, startReplicationAfter bool) error {
input := fmt.Sprintf("%v:%v", host, port)
found := false
for _, sourceInput := range fmd.SetReplicationSourceInputs {
if sourceInput == input {
found = true
}
}
if !found {
return fmt.Errorf("wrong input for SetReplicationSourceCommands: expected a value in %v got %v", fmd.SetReplicationSourceInputs, input)
}
if fmd.SetReplicationSourceError != nil {
return fmd.SetReplicationSourceError
}
cmds := []string{}
if stopReplicationBefore {
cmds = append(cmds, "STOP SLAVE")
}
cmds = append(cmds, "FAKE SET MASTER")
if startReplicationAfter {
cmds = append(cmds, "START SLAVE")
}
return fmd.ExecuteSuperQueryList(ctx, cmds)
}
// WaitForReparentJournal is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) WaitForReparentJournal(ctx context.Context, timeCreatedNS int64) error {
return nil
}
// WaitSourcePos is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) WaitSourcePos(_ context.Context, pos mysql.Position) error {
if fmd.TimeoutHook != nil {
return fmd.TimeoutHook()
}
for _, position := range fmd.WaitPrimaryPositions {
if reflect.DeepEqual(position, pos) {
return nil
}
}
return fmt.Errorf("wrong input for WaitSourcePos: expected a value in %v got %v", fmd.WaitPrimaryPositions, pos)
}
// Promote is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) Promote(hookExtraEnv map[string]string) (mysql.Position, error) {
if fmd.PromoteLag > 0 {
time.Sleep(fmd.PromoteLag)
}
if fmd.PromoteError != nil {
return mysql.Position{}, fmd.PromoteError
}
return fmd.PromoteResult, 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 "START SLAVE":
fmd.Replicating = true
case "STOP SLAVE":
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
}
// EnableBinlogPlayback is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) EnableBinlogPlayback() error {
fmd.BinlogPlayerEnabled.Set(true)
return nil
}
// DisableBinlogPlayback disable playback of binlog events
func (fmd *FakeMysqlDaemon) DisableBinlogPlayback() error {
fmd.BinlogPlayerEnabled.Set(false)
return nil
}
// Close is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) Close() {
if fmd.appPool != nil {
fmd.appPool.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(ctx context.Context, 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)
}
// GetColumns is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) GetColumns(ctx context.Context, dbName, table string) ([]*querypb.Field, []string, error) {
return []*querypb.Field{}, []string{}, nil
}
// GetPrimaryKeyColumns is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) GetPrimaryKeyColumns(ctx context.Context, dbName, table string) ([]string, error) {
return []string{}, nil
}
// GetPrimaryKeyEquivalentColumns is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) GetPrimaryKeyEquivalentColumns(ctx context.Context, dbName, table string) ([]string, error) {
return []string{}, nil
}
// PreflightSchemaChange is part of the MysqlDaemon interface
func (fmd *FakeMysqlDaemon) PreflightSchemaChange(ctx context.Context, 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(ctx context.Context, dbName string, change *tmutils.SchemaChange) (*tabletmanagerdatapb.SchemaChangeResult, error) {
beforeSchema, err := fmd.SchemaFunc()
if err != nil {
return nil, err
}
dbaCon, err := fmd.GetDbaConnection(ctx)
if err != nil {
return nil, err
}
if err = fmd.db.HandleQuery(dbaCon.Conn, change.SQL, func(*sqltypes.Result) error { return nil }); err != nil {
return nil, err
}
afterSchema, err := fmd.SchemaFunc()
if err != nil {
return nil, err
}
return &tabletmanagerdatapb.SchemaChangeResult{
BeforeSchema: beforeSchema,
AfterSchema: afterSchema}, nil
}
// GetAppConnection is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) GetAppConnection(ctx context.Context) (*dbconnpool.PooledDBConnection, error) {
return fmd.appPool.Get(ctx)
}
// GetDbaConnection is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) GetDbaConnection(ctx context.Context) (*dbconnpool.DBConnection, error) {
return dbconnpool.NewDBConnection(ctx, fmd.db.ConnParams())
}
// GetAllPrivsConnection is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) GetAllPrivsConnection(ctx context.Context) (*dbconnpool.DBConnection, error) {
return dbconnpool.NewDBConnection(ctx, fmd.db.ConnParams())
}
// SetSemiSyncEnabled is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) SetSemiSyncEnabled(primary, replica bool) error {
fmd.SemiSyncPrimaryEnabled = primary
fmd.SemiSyncReplicaEnabled = replica
return nil
}
// SemiSyncEnabled is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) SemiSyncEnabled() (primary, replica bool) {
return fmd.SemiSyncPrimaryEnabled, fmd.SemiSyncReplicaEnabled
}
// SemiSyncStatus is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) SemiSyncStatus() (bool, bool) {
// The fake assumes the status worked.
if fmd.SemiSyncPrimaryEnabled {
return true, false
}
return false, fmd.SemiSyncReplicaEnabled
}
// SemiSyncClients is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) SemiSyncClients() uint32 {
return 0
}
// SemiSyncSettings is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) SemiSyncSettings() (timeout uint64, numReplicas uint32) {
return 10000000, 1
}
// SemiSyncReplicationStatus is part of the MysqlDaemon interface.
func (fmd *FakeMysqlDaemon) SemiSyncReplicationStatus() (bool, error) {
// The fake assumes the status worked.
return fmd.SemiSyncReplicaEnabled, nil
}
// GetVersionString is part of the MysqlDeamon interface.
func (fmd *FakeMysqlDaemon) GetVersionString() string {
return ""
}
// GetVersionComment is part of the MysqlDeamon interface.
func (fmd *FakeMysqlDaemon) GetVersionComment(ctx context.Context) string {
return ""
}