-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
K8SPS-266: Refactor Replicator interface #517
Merged
Merged
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
415e8e8
Refactored getting topology.
inelpandzic 6363872
Stop using Replicator interface in bootstrap and heathcheck binaries.
inelpandzic cdfaef5
Remove unnecessary methods from Replicator interface.
inelpandzic 7b250a1
Minor cleanup.
inelpandzic 2d8b05e
Refactor UserManager and Replication interfaces.
inelpandzic eee9adc
Refactor getting topology in psbackup controller.
inelpandzic f7448f4
Fix cmd/mysql.go
inelpandzic 2ede8b6
Merge branch 'main' into K8SPS-266-refactor-replicator
inelpandzic 5891f75
Fix and refactor cmd/mysql.
inelpandzic f99c349
fix go-licenses and golicense
hors afe50c2
Merge branch 'main' into K8SPS-266-refactor-replicator
hors 52161a2
Don't return error from GetGroupReplicationReplicas if query returns …
inelpandzic ace2667
Merge branch 'main' into K8SPS-266-refactor-replicator
inelpandzic 8831094
Refactor getting topology.
inelpandzic b184ccf
Remove sql command from the logs.
inelpandzic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,240 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
|
||
"github.com/go-sql-driver/mysql" | ||
"github.com/pkg/errors" | ||
|
||
apiv1alpha1 "github.com/percona/percona-server-mysql-operator/api/v1alpha1" | ||
"github.com/percona/percona-server-mysql-operator/pkg/db" | ||
) | ||
|
||
const defaultChannelName = "" | ||
|
||
var ErrRestartAfterClone = errors.New("Error 3707: Restart server failed (mysqld is not managed by supervisor process).") | ||
|
||
type ReplicationStatus int8 | ||
|
||
type DB struct { | ||
db *sql.DB | ||
} | ||
|
||
func NewDatabase(ctx context.Context, user apiv1alpha1.SystemUser, pass, host string, port int32) (*DB, error) { | ||
config := mysql.NewConfig() | ||
|
||
config.User = string(user) | ||
config.Passwd = pass | ||
config.Net = "tcp" | ||
config.Addr = fmt.Sprintf("%s:%d", host, port) | ||
config.DBName = "performance_schema" | ||
config.Params = map[string]string{ | ||
"interpolateParams": "true", | ||
"timeout": "10s", | ||
"readTimeout": "10s", | ||
"writeTimeout": "10s", | ||
"tls": "preferred", | ||
} | ||
|
||
db, err := sql.Open("mysql", config.FormatDSN()) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "connect to MySQL") | ||
} | ||
|
||
if err := db.PingContext(ctx); err != nil { | ||
return nil, errors.Wrap(err, "ping DB") | ||
} | ||
|
||
return &DB{db}, nil | ||
} | ||
|
||
func (d *DB) StartReplication(ctx context.Context, host, replicaPass string, port int32) error { | ||
// TODO: Make retries configurable | ||
_, err := d.db.ExecContext(ctx, ` | ||
CHANGE REPLICATION SOURCE TO | ||
SOURCE_USER=?, | ||
SOURCE_PASSWORD=?, | ||
SOURCE_HOST=?, | ||
SOURCE_PORT=?, | ||
SOURCE_SSL=1, | ||
SOURCE_CONNECTION_AUTO_FAILOVER=1, | ||
SOURCE_AUTO_POSITION=1, | ||
SOURCE_RETRY_COUNT=3, | ||
SOURCE_CONNECT_RETRY=60 | ||
`, apiv1alpha1.UserReplication, replicaPass, host, port) | ||
if err != nil { | ||
return errors.Wrap(err, "exec CHANGE REPLICATION SOURCE TO") | ||
} | ||
|
||
_, err = d.db.ExecContext(ctx, "START REPLICA") | ||
return errors.Wrap(err, "start replication") | ||
} | ||
|
||
func (d *DB) StopReplication(ctx context.Context) error { | ||
_, err := d.db.ExecContext(ctx, "STOP REPLICA") | ||
return errors.Wrap(err, "stop replication") | ||
} | ||
|
||
func (d *DB) ResetReplication(ctx context.Context) error { | ||
_, err := d.db.ExecContext(ctx, "RESET REPLICA ALL") | ||
return errors.Wrap(err, "reset replication") | ||
} | ||
|
||
func (d *DB) ReplicationStatus(ctx context.Context) (db.ReplicationStatus, string, error) { | ||
row := d.db.QueryRowContext(ctx, ` | ||
SELECT | ||
connection_status.SERVICE_STATE, | ||
applier_status.SERVICE_STATE, | ||
HOST | ||
FROM replication_connection_status connection_status | ||
JOIN replication_connection_configuration connection_configuration | ||
ON connection_status.channel_name = connection_configuration.channel_name | ||
JOIN replication_applier_status applier_status | ||
ON connection_status.channel_name = applier_status.channel_name | ||
WHERE connection_status.channel_name = ? | ||
`, defaultChannelName) | ||
|
||
var ioState, sqlState, host string | ||
if err := row.Scan(&ioState, &sqlState, &host); err != nil { | ||
if errors.Is(err, sql.ErrNoRows) { | ||
return db.ReplicationStatusNotInitiated, "", nil | ||
} | ||
return db.ReplicationStatusError, "", errors.Wrap(err, "scan replication status") | ||
} | ||
|
||
if ioState == "ON" && sqlState == "ON" { | ||
return db.ReplicationStatusActive, host, nil | ||
} | ||
|
||
return db.ReplicationStatusNotInitiated, "", nil | ||
} | ||
|
||
func (d *DB) IsReplica(ctx context.Context) (bool, error) { | ||
status, _, err := d.ReplicationStatus(ctx) | ||
return status == db.ReplicationStatusActive, errors.Wrap(err, "get replication status") | ||
} | ||
|
||
func (d *DB) DisableSuperReadonly(ctx context.Context) error { | ||
_, err := d.db.ExecContext(ctx, "SET GLOBAL SUPER_READ_ONLY=0") | ||
return errors.Wrap(err, "set global super_read_only param to 0") | ||
} | ||
|
||
func (d *DB) IsReadonly(ctx context.Context) (bool, error) { | ||
var readonly int | ||
err := d.db.QueryRowContext(ctx, "select @@read_only and @@super_read_only").Scan(&readonly) | ||
return readonly == 1, errors.Wrap(err, "select global read_only param") | ||
} | ||
|
||
func (d *DB) ReportHost(ctx context.Context) (string, error) { | ||
var reportHost string | ||
err := d.db.QueryRowContext(ctx, "select @@report_host").Scan(&reportHost) | ||
return reportHost, errors.Wrap(err, "select report_host param") | ||
} | ||
|
||
func (d *DB) Close() error { | ||
return d.db.Close() | ||
} | ||
|
||
func (d *DB) CloneInProgress(ctx context.Context) (bool, error) { | ||
rows, err := d.db.QueryContext(ctx, "SELECT STATE FROM clone_status") | ||
if err != nil { | ||
return false, errors.Wrap(err, "fetch clone status") | ||
} | ||
defer rows.Close() | ||
|
||
for rows.Next() { | ||
var state string | ||
if err := rows.Scan(&state); err != nil { | ||
return false, errors.Wrap(err, "scan rows") | ||
} | ||
|
||
if state != "Completed" && state != "Failed" { | ||
return true, nil | ||
} | ||
} | ||
|
||
return false, nil | ||
} | ||
|
||
func (d *DB) Clone(ctx context.Context, donor, user, pass string, port int32) error { | ||
_, err := d.db.ExecContext(ctx, "SET GLOBAL clone_valid_donor_list=?", fmt.Sprintf("%s:%d", donor, port)) | ||
if err != nil { | ||
return errors.Wrap(err, "set clone_valid_donor_list") | ||
} | ||
|
||
_, err = d.db.ExecContext(ctx, "CLONE INSTANCE FROM ?@?:? IDENTIFIED BY ?", user, donor, port, pass) | ||
|
||
mErr, ok := err.(*mysql.MySQLError) | ||
if !ok { | ||
return errors.Wrap(err, "clone instance") | ||
} | ||
|
||
// Error 3707: Restart server failed (mysqld is not managed by supervisor process). | ||
if mErr.Number == uint16(3707) { | ||
return ErrRestartAfterClone | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (d *DB) DumbQuery(ctx context.Context) error { | ||
_, err := d.db.ExecContext(ctx, "SELECT 1") | ||
return errors.Wrap(err, "SELECT 1") | ||
} | ||
|
||
func (d *DB) GetMemberState(ctx context.Context, host string) (db.MemberState, error) { | ||
var state db.MemberState | ||
|
||
err := d.db.QueryRowContext(ctx, "SELECT MEMBER_STATE FROM replication_group_members WHERE MEMBER_HOST=?", host).Scan(&state) | ||
if err != nil { | ||
if errors.Is(err, sql.ErrNoRows) { | ||
return db.MemberStateOffline, nil | ||
} | ||
return db.MemberStateError, errors.Wrap(err, "query member state") | ||
} | ||
|
||
return state, nil | ||
} | ||
|
||
func (d *DB) CheckIfInPrimaryPartition(ctx context.Context) (bool, error) { | ||
var in bool | ||
|
||
err := d.db.QueryRowContext(ctx, ` | ||
SELECT | ||
MEMBER_STATE = 'ONLINE' | ||
AND ( | ||
( | ||
SELECT | ||
COUNT(*) | ||
FROM | ||
performance_schema.replication_group_members | ||
WHERE | ||
MEMBER_STATE NOT IN ('ONLINE', 'RECOVERING') | ||
) >= ( | ||
( | ||
SELECT | ||
COUNT(*) | ||
FROM | ||
performance_schema.replication_group_members | ||
) / 2 | ||
) = 0 | ||
) | ||
FROM | ||
performance_schema.replication_group_members | ||
JOIN performance_schema.replication_group_member_stats USING(member_id) | ||
WHERE | ||
member_id = @@global.server_uuid; | ||
`).Scan(&in) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return in, nil | ||
} | ||
|
||
func (d *DB) EnableSuperReadonly(ctx context.Context) error { | ||
_, err := d.db.ExecContext(ctx, "SET GLOBAL SUPER_READ_ONLY=1") | ||
return errors.Wrap(err, "set global super_read_only param to 1") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,3 @@ BSD-2-Clause | |
BSD-3-Clause | ||
ISC | ||
MIT | ||
MPL-2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m not sure about this
cmd/db
. For mecmd
should be consumers of the API not providers and I find depending on a cmd package from another cmd package a bit counterintuitive.what do you think of something like this?
and consumers can create instances like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you can see the structure of these three components, there are three different and distinct things, meaning, there is nothing in common between them so we could have a single interface with different implementations. If we want some interfaces they would be like three different ones with implementation for each of them and that is not necessary. If we want something that you suggested, we would more-or-less have something similar that we had before, and I wouldn't like that.
Thats why I still suggest a structure like this, with only types that expose certain methods for these three use cases. The thing that I think is good from the suggestion is the location of everything. I like more to have
pkg/mysql/client/...
rather thanpkg/db
, much better.And regarding your concern Ege about like a cmd importing
cmd/db
, that is OK and it should not necesseraliy come frompkg/...
, because it is related only tocmd
. Imagine having some common util that is needed for different stuff incmd
, you wouldn't put it inpkg
. And because I also wanted to confirm this opinion of mine, I looked at Docker and Kubernetes source code and this is a common thing there as well.With all of this, everything in here is nicely separated and clear, and most importantly, simple without unnecessary complications.