Skip to content

Commit 0dd8b4e

Browse files
committed
Fix SetPurgedGTID in case of old masters
1 parent 181909f commit 0dd8b4e

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

pkg/controller/node/node_controller.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ import (
4747
var log = logf.Log.WithName(controllerName)
4848

4949
const controllerName = "controller.mysqlNode"
50-
const mysqlReconciliationTimeout = 30 * time.Second
50+
51+
// mysqlReconciliationTimeout the time that should last a reconciliation (this is used as a MySQL timout too)
52+
const mysqlReconciliationTimeout = 10 * time.Second
5153

5254
// Add creates a new MysqlCluster Controller and adds it to the Manager with default RBAC. The Manager will set fields on the Controller
5355
// and Start it when the Manager is Started.

pkg/controller/node/sql.go

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -198,21 +198,25 @@ func (r *nodeSQLRunner) dbConn() (*sql.DB, func(), error) {
198198
}
199199

200200
func (r *nodeSQLRunner) SetPurgedGTID(ctx context.Context) error {
201-
// first check if the GTID should be set, if in the status table is a key with the GTID that was set
202-
// nolint: gosec
203-
qq := fmt.Sprintf("SELECT value FROM %s.%s WHERE name='%s'",
204-
constants.OperatorDbName, constants.OperatorStatusTableName, "set_gtid_purged")
205-
206-
var value string
207-
if err := r.readFromMysql(ctx, qq, &value); err != nil {
208-
// if no rows found then continue to add GTID purged
209-
if err != sql.ErrNoRows {
210-
return err
211-
}
201+
var (
202+
err error
203+
setGTID string
204+
backupGTID string
205+
)
206+
207+
// first check if the GTID was set before
208+
if setGTID, err = r.readStatusValue(ctx, "set_gtid_purged"); err != nil {
209+
return err
210+
} else if len(setGTID) != 0 {
211+
log.V(1).Info("GTID purged was already set", "host", r.Host(), "gtid_purged", setGTID)
212+
return nil
212213
}
213214

214-
if len(value) != 0 {
215-
log.V(1).Info("GTID purged was already set", "host", r.Host(), "gtid_purged", value)
215+
// check if there is any GTID to set
216+
if backupGTID, err = r.readStatusValue(ctx, "backup_gtid_purged"); err != nil {
217+
return err
218+
} else if len(backupGTID) == 0 {
219+
log.V(1).Info("no GTID to set", "host", r.Host())
216220
return nil
217221
}
218222

@@ -235,6 +239,22 @@ func (r *nodeSQLRunner) SetPurgedGTID(ctx context.Context) error {
235239
return nil
236240
}
237241

242+
func (r *nodeSQLRunner) readStatusValue(ctx context.Context, key string) (string, error) {
243+
// nolint: gosec
244+
qq := fmt.Sprintf("SELECT value FROM %s.%s WHERE name='%s'",
245+
constants.OperatorDbName, constants.OperatorStatusTableName, key)
246+
247+
var value string
248+
if err := r.readFromMysql(ctx, qq, &value); err != nil {
249+
// if no rows found then continue to add GTID purged
250+
if err != sql.ErrNoRows {
251+
return "", err
252+
}
253+
}
254+
255+
return value, nil
256+
}
257+
238258
// isMySQLError checks if a mysql error is of the given code.
239259
// more information about mysql error codes can be found here:
240260
// https://dev.mysql.com/doc/refman/8.0/en/server-error-reference.html

0 commit comments

Comments
 (0)