Skip to content

Commit

Permalink
cluster: deduplicate the check result (#1737)
Browse files Browse the repository at this point in the history
  • Loading branch information
srstack authored Jan 26, 2022
1 parent d2e2b40 commit 5a7693d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
37 changes: 37 additions & 0 deletions pkg/cluster/manager/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
}
21 changes: 21 additions & 0 deletions pkg/cluster/manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit 5a7693d

Please sign in to comment.