From 81682f535ce9eef481bfeb26e42048805486bd64 Mon Sep 17 00:00:00 2001 From: noname0443 Date: Fri, 13 Dec 2024 16:47:56 +0300 Subject: [PATCH 1/5] Show only GTID diff in trace queries --- internal/mysql/node.go | 26 ++++++++++++++++++++++++++ internal/mysql/queries.go | 9 ++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/internal/mysql/node.go b/internal/mysql/node.go index 29149ca0..a7d06364 100644 --- a/internal/mysql/node.go +++ b/internal/mysql/node.go @@ -147,12 +147,38 @@ func (n *Node) getQuery(name string) string { func (n *Node) traceQuery(query string, arg interface{}, result interface{}, err error) { query = queryOnliner.ReplaceAllString(query, " ") + strings.HasPrefix(query, DefaultQueries[querySlaveStatus]) + if n.config.ShowOnlyGTIDDiff && IsGtidQuery(query) { + return + } msg := fmt.Sprintf("node %s running query '%s' with args %#v, result: %#v, error: %v", n.host, query, arg, result, err) msg = strings.ReplaceAll(msg, n.config.MySQL.Password, "********") msg = strings.ReplaceAll(msg, n.config.MySQL.ReplicationPassword, "********") n.logger.Debug(msg) } +func IsGtidQuery(query string) bool { + if query == DefaultQueries[queryGTIDExecuted] { + return true + } + + if strings.HasPrefix( + query, + UtilityQueries[querySlaveStatus], + ) { + return true + } + + if strings.HasPrefix( + query, + UtilityQueries[queryReplicaStatus], + ) { + return true + } + + return false +} + //nolint:unparam func (n *Node) queryRow(queryName string, arg interface{}, result interface{}) error { return n.queryRowWithTimeout(queryName, arg, result, n.config.DBTimeout) diff --git a/internal/mysql/queries.go b/internal/mysql/queries.go index d2f3cafc..0eec82ab 100644 --- a/internal/mysql/queries.go +++ b/internal/mysql/queries.go @@ -1,5 +1,7 @@ package mysql +import "strings" + const ( queryPing = "ping" querySlaveStatus = "slave_status" @@ -57,7 +59,7 @@ var DefaultQueries = map[string]string{ querySlaveStatus: `SHOW SLAVE STATUS FOR CHANNEL :channel`, queryReplicaStatus: `SHOW REPLICA STATUS FOR CHANNEL :channel`, queryGetVersion: `SELECT sys.version_major() AS MajorVersion, sys.version_minor() AS MinorVersion, sys.version_patch() AS PatchVersion`, - queryGTIDExecuted: `SELECT @@GLOBAL.gtid_executed as Executed_Gtid_Set`, + queryGTIDExecuted: `SELECT @@GLOBAL.gtid_executed as Executed_Gtid_Set`, queryGetUUID: `SELECT @@server_uuid as server_uuid`, queryShowBinaryLogs: `SHOW BINARY LOGS`, querySlaveHosts: `SHOW SLAVE HOSTS`, @@ -143,3 +145,8 @@ var DefaultQueries = map[string]string{ ) ON DUPLICATE KEY UPDATE ts = CURRENT_TIMESTAMP(3)`, } + +var UtilityQueries = map[string]string{ + querySlaveStatus: strings.ReplaceAll(DefaultQueries[querySlaveStatus], ":channel", ""), + queryReplicaStatus: strings.ReplaceAll(DefaultQueries[queryReplicaStatus], ":channel", ""), +} From b4ea9ac7c3a17b473090a80213e117fc30b84da7 Mon Sep 17 00:00:00 2001 From: noname0443 Date: Fri, 13 Dec 2024 16:55:30 +0300 Subject: [PATCH 2/5] Show only GTID diff in trace queries --- internal/mysql/node.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/mysql/node.go b/internal/mysql/node.go index a7d06364..7cb76967 100644 --- a/internal/mysql/node.go +++ b/internal/mysql/node.go @@ -147,7 +147,6 @@ func (n *Node) getQuery(name string) string { func (n *Node) traceQuery(query string, arg interface{}, result interface{}, err error) { query = queryOnliner.ReplaceAllString(query, " ") - strings.HasPrefix(query, DefaultQueries[querySlaveStatus]) if n.config.ShowOnlyGTIDDiff && IsGtidQuery(query) { return } From 7ad0f48084a22e841aec9b8ed2a9b6e1b942aca9 Mon Sep 17 00:00:00 2001 From: noname0443 Date: Mon, 16 Dec 2024 15:08:35 +0300 Subject: [PATCH 3/5] Refactor the code --- internal/mysql/node.go | 26 ++++++++++---------------- internal/mysql/queries.go | 7 ------- 2 files changed, 10 insertions(+), 23 deletions(-) diff --git a/internal/mysql/node.go b/internal/mysql/node.go index 7cb76967..dbf61902 100644 --- a/internal/mysql/node.go +++ b/internal/mysql/node.go @@ -157,27 +157,21 @@ func (n *Node) traceQuery(query string, arg interface{}, result interface{}, err } func IsGtidQuery(query string) bool { - if query == DefaultQueries[queryGTIDExecuted] { - return true - } - - if strings.HasPrefix( - query, - UtilityQueries[querySlaveStatus], - ) { - return true - } - - if strings.HasPrefix( - query, - UtilityQueries[queryReplicaStatus], - ) { - return true + for _, gtidQuery := range GtidQueries { + if strings.HasPrefix(query, gtidQuery) { + return true + } } return false } +var GtidQueries = []string{ + DefaultQueries[queryGTIDExecuted], + strings.ReplaceAll(DefaultQueries[querySlaveStatus], ":channel", "''"), + strings.ReplaceAll(DefaultQueries[queryReplicaStatus], ":channel", ""), +} + //nolint:unparam func (n *Node) queryRow(queryName string, arg interface{}, result interface{}) error { return n.queryRowWithTimeout(queryName, arg, result, n.config.DBTimeout) diff --git a/internal/mysql/queries.go b/internal/mysql/queries.go index 0eec82ab..0fe31db4 100644 --- a/internal/mysql/queries.go +++ b/internal/mysql/queries.go @@ -1,7 +1,5 @@ package mysql -import "strings" - const ( queryPing = "ping" querySlaveStatus = "slave_status" @@ -145,8 +143,3 @@ var DefaultQueries = map[string]string{ ) ON DUPLICATE KEY UPDATE ts = CURRENT_TIMESTAMP(3)`, } - -var UtilityQueries = map[string]string{ - querySlaveStatus: strings.ReplaceAll(DefaultQueries[querySlaveStatus], ":channel", ""), - queryReplicaStatus: strings.ReplaceAll(DefaultQueries[queryReplicaStatus], ":channel", ""), -} From 8eb8c36dc252802d75de66a6197718531e738dd6 Mon Sep 17 00:00:00 2001 From: noname0443 Date: Tue, 17 Dec 2024 10:43:46 +0300 Subject: [PATCH 4/5] Add message for ignored queries and fix full gtid at healthcheck --- internal/app/app.go | 1 + internal/mysql/node.go | 1 + 2 files changed, 2 insertions(+) diff --git a/internal/app/app.go b/internal/app/app.go index 92435978..82771b62 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -2068,6 +2068,7 @@ func (app *App) getNodeState(host string) *NodeState { node = app.cluster.Get(host) } nodeState := new(NodeState) + nodeState.ShowOnlyGTIDDiff = app.config.ShowOnlyGTIDDiff err := func() error { nodeState.CheckAt = time.Now() nodeState.CheckBy = app.config.Hostname diff --git a/internal/mysql/node.go b/internal/mysql/node.go index dbf61902..0998e9af 100644 --- a/internal/mysql/node.go +++ b/internal/mysql/node.go @@ -148,6 +148,7 @@ func (n *Node) getQuery(name string) string { func (n *Node) traceQuery(query string, arg interface{}, result interface{}, err error) { query = queryOnliner.ReplaceAllString(query, " ") if n.config.ShowOnlyGTIDDiff && IsGtidQuery(query) { + n.logger.Debug("") return } msg := fmt.Sprintf("node %s running query '%s' with args %#v, result: %#v, error: %v", n.host, query, arg, result, err) From 54fcc4609d01d2786e975087e28de0fcb6f205cf Mon Sep 17 00:00:00 2001 From: noname0443 Date: Wed, 18 Dec 2024 08:56:13 +0300 Subject: [PATCH 5/5] Add quotes --- internal/mysql/node.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/mysql/node.go b/internal/mysql/node.go index 0998e9af..7a0a3c5e 100644 --- a/internal/mysql/node.go +++ b/internal/mysql/node.go @@ -170,7 +170,7 @@ func IsGtidQuery(query string) bool { var GtidQueries = []string{ DefaultQueries[queryGTIDExecuted], strings.ReplaceAll(DefaultQueries[querySlaveStatus], ":channel", "''"), - strings.ReplaceAll(DefaultQueries[queryReplicaStatus], ":channel", ""), + strings.ReplaceAll(DefaultQueries[queryReplicaStatus], ":channel", "''"), } //nolint:unparam