From 328f556ad879477c9c5cfa3f11ae98c582741499 Mon Sep 17 00:00:00 2001 From: srstack Date: Thu, 20 Jan 2022 18:46:04 +0800 Subject: [PATCH 1/3] cluster: deduplicate the check result --- pkg/cluster/manager/check.go | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/pkg/cluster/manager/check.go b/pkg/cluster/manager/check.go index b90fa1d8f3..38ed19aa3b 100644 --- a/pkg/cluster/manager/check.go +++ b/pkg/cluster/manager/check.go @@ -478,6 +478,8 @@ func checkSystemInfo( applyFixTasks = append(applyFixTasks, tf.BuildAsStep(fmt.Sprintf(" - Applying changes on %s", host))) } + checkResults = deduplicateCheckResult(checkResults) + if gOpt.DisplayMode == "json" { checkResultStruct := make([]HostCheckResult, 0) @@ -689,3 +691,38 @@ func checkConflict(m *Manager, clusterName string, topo spec.Topology) error { err = spec.CheckClusterDirConflict(clusterList, clusterName, topo) return err } + +// deduplicateCheckResult deduplicate check results +func deduplicateCheckResult(checkResults []HostCheckResult) (uniqueResults []HostCheckResult) { + // node: {name|status: set(msg)} + tmpResultMap := map[string]map[string]set.StringSet{} + + // deduplicate + for _, result := range checkResults { + if tmpResultMap[result.Node] == nil { + tmpResultMap[result.Node] = make(map[string]set.StringSet) + } + tmpSet := tmpResultMap[result.Node][fmt.Sprintf("%s|%s", result.Name, result.Status)] + if tmpSet == nil { + tmpSet = set.NewStringSet() + } + tmpSet.Insert(result.Message) + tmpResultMap[result.Node][fmt.Sprintf("%s|%s", result.Name, result.Status)] = tmpSet + } + + for node, msgMap := range tmpResultMap { + for checkInfo, msgSet := range msgMap { + nameAndstatus := strings.Split(checkInfo, "|") + for _, msg := range msgSet.Slice() { + uniqueResults = append(uniqueResults, + HostCheckResult{ + Node: node, + Name: nameAndstatus[0], + Status: nameAndstatus[1], + Message: msg, + }) + } + } + } + return +} From 468a82cd65964264ad5a9b3dc85cfe204ea282aa Mon Sep 17 00:00:00 2001 From: srstack Date: Fri, 21 Jan 2022 15:39:34 +0800 Subject: [PATCH 2/3] perfect code --- pkg/cluster/manager/check.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/cluster/manager/check.go b/pkg/cluster/manager/check.go index 38ed19aa3b..633de511ec 100644 --- a/pkg/cluster/manager/check.go +++ b/pkg/cluster/manager/check.go @@ -702,12 +702,12 @@ func deduplicateCheckResult(checkResults []HostCheckResult) (uniqueResults []Hos if tmpResultMap[result.Node] == nil { tmpResultMap[result.Node] = make(map[string]set.StringSet) } - tmpSet := tmpResultMap[result.Node][fmt.Sprintf("%s|%s", result.Name, result.Status)] - if tmpSet == nil { - tmpSet = set.NewStringSet() + // insert msg into set + msgKey := fmt.Sprintf("%s|%s", result.Name, result.Status) + if tmpResultMap[result.Node][msgKey] == nil { + tmpResultMap[result.Node][msgKey] = set.NewStringSet() } - tmpSet.Insert(result.Message) - tmpResultMap[result.Node][fmt.Sprintf("%s|%s", result.Name, result.Status)] = tmpSet + tmpResultMap[result.Node][msgKey].Insert(result.Message) } for node, msgMap := range tmpResultMap { From 8632f1325ce9e6c44d148d62fadc894bf8e7401e Mon Sep 17 00:00:00 2001 From: srstack Date: Fri, 21 Jan 2022 16:06:00 +0800 Subject: [PATCH 3/3] add unit test --- pkg/cluster/manager/manager_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pkg/cluster/manager/manager_test.go b/pkg/cluster/manager/manager_test.go index 87f045422a..e80d1d1c64 100644 --- a/pkg/cluster/manager/manager_test.go +++ b/pkg/cluster/manager/manager_test.go @@ -86,3 +86,24 @@ pd_servers: err = validateNewTopo(&topo) assert.NotNil(err) } + +func TestDeduplicateCheckResult(t *testing.T) { + checkResults := []HostCheckResult{} + + for i := 0; i <= 10; i++ { + checkResults = append(checkResults, + HostCheckResult{ + Node: "127.0.0.1", + Status: "Warn", + Name: "disk", + Message: "mount point /home does not have 'noatime' option set", + }, + ) + } + + checkResults = deduplicateCheckResult(checkResults) + + if len(checkResults) != 1 { + t.Errorf("Deduplicate Check Result Failed") + } +}