Skip to content

Commit 181909f

Browse files
committed
Use only one table to operate - status table
1 parent 2037b0c commit 181909f

File tree

5 files changed

+44
-47
lines changed

5 files changed

+44
-47
lines changed

pkg/controller/mysqlcluster/internal/syncer/statefullset.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,9 @@ func (s *sfsSyncer) ensureContainersSpec() []core.Container {
281281
"mysql",
282282
fmt.Sprintf("--defaults-file=%s", confClientPath),
283283
"-e",
284-
fmt.Sprintf("SELECT * FROM %s.%s", constants.OperatorDbName, constants.OperatorReadinessTableName),
284+
// nolint: gosec
285+
fmt.Sprintf("SELECT * FROM %s.%s WHERE name='configured' AND value='1'",
286+
constants.OperatorDbName, constants.OperatorStatusTableName),
285287
},
286288
},
287289
})

pkg/controller/node/sql.go

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,9 @@ func (r *nodeSQLRunner) ChangeMasterTo(ctx context.Context, masterHost, user, pa
125125

126126
// MarkConfigurationDone write in a MEMORY table value. The readiness probe checks for that value to exist to succeed.
127127
func (r *nodeSQLRunner) MarkConfigurationDone(ctx context.Context) error {
128-
query := `
129-
CREATE TABLE IF NOT EXISTS %s.%s (
130-
ok tinyint(1) NOT NULL
131-
) ENGINE=MEMORY;
132-
133-
INSERT INTO %[1]s.%[2]s VALUES (1);
134-
`
135-
query = fmt.Sprintf(query, constants.OperatorDbName, constants.OperatorReadinessTableName)
128+
// nolint: gosec
129+
query := fmt.Sprintf("REPLACE INTO %s.%s VALUES ('%s', '1');",
130+
constants.OperatorDbName, constants.OperatorStatusTableName, "configured")
136131

137132
if err := r.runQuery(ctx, query); err != nil {
138133
return fmt.Errorf("failed to mark configuration done, err: %s", err)
@@ -203,38 +198,35 @@ func (r *nodeSQLRunner) dbConn() (*sql.DB, func(), error) {
203198
}
204199

205200
func (r *nodeSQLRunner) SetPurgedGTID(ctx context.Context) error {
206-
// first check if the GTID should be set, if the table exists or if the GTID was set before (used)
201+
// first check if the GTID should be set, if in the status table is a key with the GTID that was set
207202
// nolint: gosec
208-
qq := fmt.Sprintf("SELECT used FROM %[1]s.%[2]s WHERE id=1",
209-
constants.OperatorDbName, constants.OperatorGtidsTableName)
210-
211-
var used bool
212-
if err := r.readFromMysql(ctx, qq, &used); err != nil {
213-
// if it's a: "Table doesn't exist" error then GTID should not be set, it's a master case.
214-
if isMySQLError(err, 1146) || err == sql.ErrNoRows {
215-
log.V(1).Info("GTID purged table does not exists", "host", r.Host())
216-
return nil
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
217211
}
218-
219-
return err
220212
}
221213

222-
if used {
223-
log.V(1).Info("GTID purged set", "host", r.Host())
214+
if len(value) != 0 {
215+
log.V(1).Info("GTID purged was already set", "host", r.Host(), "gtid_purged", value)
224216
return nil
225217
}
226218

227219
// GTID exists and should be set in a transaction
228220
// nolint: gosec
229221
query := fmt.Sprintf(`
230-
SET @@SESSION.SQL_LOG_BIN = 0;
231-
START TRANSACTION;
232-
SELECT gtid INTO @gtid FROM %[1]s.%[2]s WHERE id=1 AND used=false;
233-
RESET MASTER;
234-
SET @@GLOBAL.GTID_PURGED = @gtid;
235-
REPLACE INTO %[1]s.%[2]s VALUES (1, @gtid, true);
236-
COMMIT;
237-
`, constants.OperatorDbName, constants.OperatorGtidsTableName)
222+
SET @@SESSION.SQL_LOG_BIN = 0;
223+
START TRANSACTION;
224+
SELECT value INTO @gtid FROM %[1]s.%[2]s WHERE name='%s';
225+
RESET MASTER;
226+
SET @@GLOBAL.GTID_PURGED = @gtid;
227+
REPLACE INTO %[1]s.%[2]s VALUES ('%s', @gtid);
228+
COMMIT;
229+
`, constants.OperatorDbName, constants.OperatorStatusTableName, "backup_gtid_purged", "set_gtid_purged")
238230

239231
if err := r.runQuery(ctx, query); err != nil {
240232
return err

pkg/sidecar/appconf.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,24 @@ func initFileQuery(cfg *Config, gtidPurged string) []byte {
179179
[]string{"CREATE", "SELECT", "DELETE", "UPDATE", "INSERT"}, fmt.Sprintf("%s.%s", toolsDbName, toolsHeartbeatTableName),
180180
[]string{"REPLICATION CLIENT"}, "*.*")...)
181181

182-
if len(gtidPurged) != 0 {
183-
// If the xtrabackup information has GTID_PURGED then insert it into a table
184-
// nolint: gosec
185-
queries = append(queries, fmt.Sprintf(`
182+
// create the status table used by the operator to configure or to mask MySQL node ready
183+
// nolint: gosec
184+
queries = append(queries, fmt.Sprintf(`
186185
CREATE TABLE IF NOT EXISTS %[1]s.%[2]s (
187-
id int PRIMARY KEY,
188-
gtid varchar(512) NOT NULL,
189-
used BOOLEAN DEFAULT false
190-
)`, constants.OperatorDbName, constants.OperatorGtidsTableName))
186+
name varchar(64) PRIMARY KEY,
187+
value varchar(512) NOT NULL
188+
)`, constants.OperatorDbName, constants.OperatorStatusTableName))
191189

190+
// mark node as not configured at startup, the operator will mark it configured
191+
// nolint: gosec
192+
queries = append(queries, fmt.Sprintf("REPLACE INTO %s.%s VALUES ('%s', '0')",
193+
constants.OperatorDbName, constants.OperatorStatusTableName, "configured"))
194+
195+
if len(gtidPurged) != 0 {
196+
// if gtid is found in the backup then set it in the status table to be processed by the operator
192197
// nolint: gosec
193-
queries = append(queries, fmt.Sprintf(`REPLACE INTO %s.%s (id, gtid) VALUES (1, '%s')`,
194-
constants.OperatorDbName, constants.OperatorGtidsTableName, gtidPurged))
198+
queries = append(queries, fmt.Sprintf(`REPLACE INTO %s.%s VALUES ('%s', '%s')`,
199+
constants.OperatorDbName, constants.OperatorStatusTableName, "backup_gtid_purged", gtidPurged))
195200
}
196201

197202
return []byte(strings.Join(queries, ";\n") + ";\n")

pkg/sidecar/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (s *server) backupHandler(w http.ResponseWriter, r *http.Request) {
9090

9191
// nolint: gosec
9292
xtrabackup := exec.Command("xtrabackup", "--backup", "--slave-info", "--stream=xbstream",
93-
fmt.Sprintf("--tables-exclude=%s.%s", constants.OperatorDbName, constants.OperatorGtidsTableName),
93+
fmt.Sprintf("--tables-exclude=%s.%s", constants.OperatorDbName, constants.OperatorStatusTableName),
9494
"--host=127.0.0.1", fmt.Sprintf("--user=%s", s.cfg.ReplicationUser),
9595
fmt.Sprintf("--password=%s", s.cfg.ReplicationPassword),
9696
"--target-dir=/tmp/xtrabackup_backupfiles/")

pkg/util/constants/constants.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,9 @@ const (
4141
// SlaveLagQuery in hack/charts/mysql-operator/values.yaml.
4242
OperatorDbName = "sys_operator"
4343

44-
// OperatorGtidsTableName represents the name of the table that is used to store the GTID
45-
OperatorGtidsTableName = "gtids"
46-
47-
// OperatorReadinessTableName is the name of the table that is used to store a readiness flag (boolean)
48-
OperatorReadinessTableName = "readiness"
44+
// OperatorStatusTableName represents the name of the table that contains information about MySQL status, like:
45+
// if mysql is configure by the operator, if PURGE_GTID is set or not, etc
46+
OperatorStatusTableName = "status"
4947

5048
// ConfVolumeMountPath is the path where mysql configs will be mounted
5149
ConfVolumeMountPath = "/etc/mysql"

0 commit comments

Comments
 (0)