From 5a7693db7d8db7c63552de0b24a4378507e1a03d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Qi=CE=BC=24hi=D0=AFu=C3=AD?= <39378935+srstack@users.noreply.github.com> Date: Wed, 26 Jan 2022 19:18:45 +0800 Subject: [PATCH] cluster: deduplicate the check result (#1737) --- pkg/cluster/manager/check.go | 37 +++++++++++++++++++++++++++++ pkg/cluster/manager/manager_test.go | 21 ++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/pkg/cluster/manager/check.go b/pkg/cluster/manager/check.go index b90fa1d8f3..633de511ec 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) + } + // 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() + } + tmpResultMap[result.Node][msgKey].Insert(result.Message) + } + + 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 +} 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") + } +}