From 2a97f061db533593d43d922cd2e5b95e4ef058d1 Mon Sep 17 00:00:00 2001 From: Yoann La Cancellera Date: Wed, 22 Nov 2023 09:33:49 +0100 Subject: [PATCH] Imp: use strings.builder for conflicts --- src/go/pt-galera-log-explainer/conflicts.go | 46 ++++++++++--- src/go/pt-galera-log-explainer/main_test.go | 6 ++ .../tests/expected/conflict_conflicts | 7 ++ .../tests/logs/conflict/node.log | 68 +++++++++++++++++++ 4 files changed, 117 insertions(+), 10 deletions(-) create mode 100644 src/go/pt-galera-log-explainer/tests/expected/conflict_conflicts create mode 100644 src/go/pt-galera-log-explainer/tests/logs/conflict/node.log diff --git a/src/go/pt-galera-log-explainer/conflicts.go b/src/go/pt-galera-log-explainer/conflicts.go index fde442b1..d61221ca 100644 --- a/src/go/pt-galera-log-explainer/conflicts.go +++ b/src/go/pt-galera-log-explainer/conflicts.go @@ -3,9 +3,11 @@ package main import ( "encoding/json" "fmt" + "strings" "github.com/percona/percona-toolkit/src/go/pt-galera-log-explainer/regex" "github.com/percona/percona-toolkit/src/go/pt-galera-log-explainer/utils" + "golang.org/x/exp/slices" "gopkg.in/yaml.v2" ) @@ -34,33 +36,57 @@ func (c *conflicts) Run() error { } var out string - if c.Yaml { + switch { + case c.Yaml: tmp, err := yaml.Marshal(ctx.Conflicts) if err != nil { return err } out = string(tmp) - } else if c.Json { + case c.Json: tmp, err := json.Marshal(ctx.Conflicts) if err != nil { return err } out = string(tmp) - } else { + default: + var b strings.Builder for _, conflict := range ctx.Conflicts { - out += "\n" - out += "\n" + utils.Paint(utils.BlueText, "seqno: ") + conflict.Seqno - out += "\n\t" + utils.Paint(utils.BlueText, "winner: ") + conflict.Winner - out += "\n\t" + utils.Paint(utils.BlueText, "votes per nodes:") - for node, vote := range conflict.VotePerNode { + b.WriteString("\n\n") + b.WriteString(utils.Paint(utils.BlueText, "seqno: ")) + b.WriteString(conflict.Seqno) + b.WriteString("\n\t") + b.WriteString(utils.Paint(utils.BlueText, "winner: ")) + b.WriteString(conflict.Winner) + b.WriteString("\n\t") + b.WriteString(utils.Paint(utils.BlueText, "votes per nodes:")) + + nodes := []string{} + for node := range conflict.VotePerNode { + nodes = append(nodes, node) + } + // do not iterate over VotePerNode map + // map accesses are random, it will make regression tests harder + slices.Sort(nodes) + + for _, node := range nodes { + vote := conflict.VotePerNode[node] displayVote := utils.Paint(utils.RedText, vote.MD5) if vote.MD5 == conflict.Winner { displayVote = utils.Paint(utils.GreenText, vote.MD5) } - out += "\n\t\t" + utils.Paint(utils.BlueText, node) + ": (" + displayVote + ") " + vote.Error + b.WriteString("\n\t\t") + b.WriteString(utils.Paint(utils.BlueText, node)) + b.WriteString(": (") + b.WriteString(displayVote) + b.WriteString(") ") + b.WriteString(vote.Error) } - out += "\n\t" + utils.Paint(utils.BlueText, "initiated by: ") + fmt.Sprintf("%v", conflict.InitiatedBy) + b.WriteString("\n\t") + b.WriteString(utils.Paint(utils.BlueText, "initiated by: ")) + b.WriteString(fmt.Sprintf("%v", conflict.InitiatedBy)) + out = b.String()[2:] } } diff --git a/src/go/pt-galera-log-explainer/main_test.go b/src/go/pt-galera-log-explainer/main_test.go index bdb74632..e4bf895e 100644 --- a/src/go/pt-galera-log-explainer/main_test.go +++ b/src/go/pt-galera-log-explainer/main_test.go @@ -97,6 +97,12 @@ func TestMain(t *testing.T) { cmd: []string{"list", "--all", "--pxc-operator", "--no-color"}, path: "tests/logs/operator_ambiguous_ips/*", }, + + { + name: "conflict_conflicts", + cmd: []string{"conflicts"}, + path: "tests/logs/conflict/*", + }, } TESTS: diff --git a/src/go/pt-galera-log-explainer/tests/expected/conflict_conflicts b/src/go/pt-galera-log-explainer/tests/expected/conflict_conflicts new file mode 100644 index 00000000..05b7e4f3 --- /dev/null +++ b/src/go/pt-galera-log-explainer/tests/expected/conflict_conflicts @@ -0,0 +1,7 @@ +seqno: 102573168 + winner: 0000000000000000 + votes per nodes: + node1: (cd3bd7de926232d8) File '/var/log/mysqld-slow.log' not found (OS errno 13 - Permission denied) + node2: (0000000000000000) Success + node3: (0000000000000000) Success + initiated by: [node1] diff --git a/src/go/pt-galera-log-explainer/tests/logs/conflict/node.log b/src/go/pt-galera-log-explainer/tests/logs/conflict/node.log new file mode 100644 index 00000000..b379fa3c --- /dev/null +++ b/src/go/pt-galera-log-explainer/tests/logs/conflict/node.log @@ -0,0 +1,68 @@ +2023-10-21T03:01:02.048346Z 480377 [Warning] [MY-013712] [Server] No suitable 'keyring_component_metadata_query' service implementation found to fulfill the request. +2023-10-21T04:01:01.676602Z 443229 [ERROR] [MY-011263] [Server] Could not use /var/log/mysqld-slow.log for logging (error 13 - Permission denied). Turning logging off for the server process. To turn it on again: fix the cause, then either restart the query logging by using "SET GLOBAL SLOW_QUERY_LOG=ON" or restart the MySQL server. +2023-10-21T04:01:01.700706Z 0 [Note] [MY-000000] [Galera] Member 0(node1) initiates vote on e234baca-17b2-11ed-b5e0-9ef13b0a9e4f:102573168,cd3bd7de926232d8: File '/var/log/mysqld-slow.log' not found (OS errno 13 - Permission denied), Error_code: 29; +2023-10-21T04:01:01.700748Z 0 [Note] [MY-000000] [Galera] Votes over e234baca-17b2-11ed-b5e0-9ef13b0a9e4f:102573168: + cd3bd7de926232d8: 1/5 +Waiting for more votes. +2023-10-21T04:01:01.701416Z 0 [Note] [MY-000000] [Galera] Member 2(node3) responds to vote on e234baca-17b2-11ed-b5e0-9ef13b0a9e4f:102573168,0000000000000000: Success +2023-10-21T04:01:01.701427Z 0 [Note] [MY-000000] [Galera] Votes over e234baca-17b2-11ed-b5e0-9ef13b0a9e4f:102573168: + 0000000000000000: 1/5 + cd3bd7de926232d8: 1/5 +Waiting for more votes. +2023-10-21T04:01:01.701436Z 0 [Note] [MY-000000] [Galera] Member 3(node2) responds to vote on e234baca-17b2-11ed-b5e0-9ef13b0a9e4f:102573168,0000000000000000: Success +2023-10-21T04:01:01.701444Z 0 [Note] [MY-000000] [Galera] Votes over e234baca-17b2-11ed-b5e0-9ef13b0a9e4f:102573168: + 0000000000000000: 2/5 + cd3bd7de926232d8: 1/5 +Winner: 0000000000000000 +2023-10-21T04:01:01.701512Z 443229 [ERROR] [MY-000000] [Galera] Inconsistency detected: Inconsistent by consensus on e234baca-17b2-11ed-b5e0-9ef13b0a9e4f:102573168 + at galera/src/replicator_smm.cpp:process_apply_error():1436 +2023-10-21T04:01:01.702957Z 443229 [Note] [MY-000000] [Galera] Closing send monitor... +2023-10-21T04:01:01.702984Z 443229 [Note] [MY-000000] [Galera] Closed send monitor. +2023-10-21T04:01:01.702997Z 443229 [Note] [MY-000000] [Galera] gcomm: terminating thread +2023-10-21T04:01:01.703017Z 443229 [Note] [MY-000000] [Galera] gcomm: joining thread +2023-10-21T04:01:01.703176Z 443229 [Note] [MY-000000] [Galera] gcomm: closing backend +2023-10-21T04:01:01.703866Z 443229 [Note] [MY-000000] [Galera] Current view of cluster as seen by this node +view (view_id(NON_PRIM,40661022-a2c6,94) +memb { + 40661022-a2c6,0 + } +joined { + } +left { + } +partitioned { + 766d0cf0-a7ab,0 + 7c13fd0b-85ac,0 + 855ff0f7-9979,0 + 8ec0e01f-91a0,0 + } +) +2023-10-21T04:01:01.703914Z 443229 [Note] [MY-000000] [Galera] PC protocol downgrade 1 -> 0 +2023-10-21T04:01:01.703925Z 443229 [Note] [MY-000000] [Galera] Current view of cluster as seen by this node +view ((empty)) +2023-10-21T04:01:01.704291Z 443229 [Note] [MY-000000] [Galera] gcomm: closed +2023-10-21T04:01:01.704318Z 0 [Note] [MY-000000] [Galera] New COMPONENT: primary = no, bootstrap = no, my_idx = 0, memb_num = 1 +2023-10-21T04:01:01.704368Z 0 [Note] [MY-000000] [Galera] Flow-control interval: [100, 100] +2023-10-21T04:01:01.704377Z 0 [Note] [MY-000000] [Galera] Received NON-PRIMARY. +2023-10-21T04:01:01.704384Z 0 [Note] [MY-000000] [Galera] Shifting SYNCED -> OPEN (TO: 102573170) +2023-10-21T04:01:01.704399Z 0 [Note] [MY-000000] [Galera] New SELF-LEAVE. +2023-10-21T04:01:01.704436Z 0 [Note] [MY-000000] [Galera] Flow-control interval: [0, 0] +2023-10-21T04:01:01.704456Z 0 [Note] [MY-000000] [Galera] Received SELF-LEAVE. Closing connection. +2023-10-21T04:01:01.704465Z 0 [Note] [MY-000000] [Galera] Shifting OPEN -> CLOSED (TO: 102573170) +2023-10-21T04:01:01.704476Z 0 [Note] [MY-000000] [Galera] RECV thread exiting 0: Success +2023-10-21T04:01:01.704472Z 16 [Note] [MY-000000] [Galera] ================================================ +View: + id: e234baca-17b2-11ed-b5e0-9ef13b0a9e4f:102573170 + status: non-primary + protocol_version: 4 + capabilities: MULTI-MASTER, CERTIFICATION, PARALLEL_APPLYING, REPLAY, ISOLATION, PAUSE, CAUSAL_READ, INCREMENTAL_WS, UNORDERED, PREORDERED, STREAMING, NBO + final: no + own_index: 0 + members(1): + 0: 40661022-3829-11ee-a2c6-26f8b8643033, node1 +================================================= +2023-10-21T04:01:01.704508Z 16 [Note] [MY-000000] [Galera] Non-primary view +2023-10-21T04:01:01.704520Z 16 [Note] [MY-000000] [WSREP] Server status change synced -> connected +2023-10-21T04:01:01.704605Z 443229 [Note] [MY-000000] [Galera] recv_thread() joined. +2023-10-21T04:01:01.704617Z 443229 [Note] [MY-000000] [Galera] Closing replication queue. +2023-10-21T04:01:01.704638Z 443229 [Note] [MY-000000] [Galera] Closing slave action queue.