forked from pingcap/tidb-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.go
135 lines (118 loc) · 3.12 KB
/
check.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2018 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package check
import (
"context"
"sync"
)
// Checker is interface that defines checker to check configurations of system.
// It is mainly used for configuration checking of data synchronization between database systems.
type Checker interface {
Name() string
Check(ctx context.Context) *Result
}
// State is state of check
type State string
const (
// StateSuccess indicates that the check was successful
StateSuccess State = "success"
// StateFailure indicates that the check was failed
StateFailure State = "fail"
// StateWarning indicates that the check had warnings
StateWarning State = "warn"
)
// Result is result of check
type Result struct {
ID uint64 `json:"id"`
Name string `json:"name"`
Desc string `json:"desc"`
State State `json:"state"`
ErrorMsg string `json:"errorMsg"`
Instruction string `json:"instruction"`
Extra string `json:"extra"`
}
// ResultSummary is summary of all check results
type ResultSummary struct {
Passed bool `json:"passed"`
Total int64 `json:"total"`
Successful int64 `json:"successful"`
Failed int64 `json:"failed"`
Warning int64 `json:"warning"`
}
// Results contains all check results and summary
type Results struct {
Results []*Result `json:"results"`
Summary *ResultSummary `json:"summary"`
}
// Do executes several checkers.
func Do(ctx context.Context, checkers []Checker) (*Results, error) {
results := &Results{
Results: make([]*Result, 0, len(checkers)),
}
if len(checkers) == 0 {
results.Summary = &ResultSummary{Passed: true}
return results, nil
}
var (
wg sync.WaitGroup
finished bool
total int64
successful int64
failed int64
warning int64
)
total = int64(len(checkers))
resultCh := make(chan *Result)
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case result := <-resultCh:
switch result.State {
case StateSuccess:
successful++
case StateFailure:
failed++
case StateWarning:
warning++
}
// if total == successful + warning + failed, it's finished
finished = total == successful+warning+failed
results.Results = append(results.Results, result)
if finished {
return
}
}
}
}()
for i, checker := range checkers {
wg.Add(1)
go func(i int, checker Checker) {
defer wg.Done()
result := checker.Check(ctx)
result.ID = uint64(i)
resultCh <- result
}(i, checker)
}
wg.Wait()
passed := finished && (failed == 0)
results.Summary = &ResultSummary{
Passed: passed,
Total: total,
Successful: successful,
Failed: failed,
Warning: warning,
}
return results, nil
}