forked from pingcap/br
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
218 lines (185 loc) · 5.5 KB
/
collector.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0.
package summary
import (
"fmt"
"sync"
"time"
"github.com/pingcap/log"
"go.uber.org/zap"
)
const (
// BackupUnit tells summary in backup
BackupUnit = "backup"
// RestoreUnit tells summary in restore
RestoreUnit = "restore"
// TotalKV is a field we collect during backup/restore
TotalKV = "total kv"
// TotalBytes is a field we collect during backup/restore
TotalBytes = "total bytes"
)
// LogCollector collects infos into summary log.
type LogCollector interface {
SetUnit(unit string)
CollectSuccessUnit(name string, unitCount int, arg interface{})
CollectFailureUnit(name string, reason error)
CollectDuration(name string, t time.Duration)
CollectInt(name string, t int)
CollectUInt(name string, t uint64)
SetSuccessStatus(success bool)
Summary(name string)
}
type logFunc func(msg string, fields ...zap.Field)
var collector LogCollector = NewLogCollector(log.Info)
// InitCollector initilize global collector instance.
func InitCollector( // revive:disable-line:flag-parameter
hasLogFile bool,
) {
logF := log.L().Info
if hasLogFile {
conf := new(log.Config)
// Always duplicate summary to stdout.
logger, _, err := log.InitLogger(conf)
if err == nil {
logF = func(msg string, fields ...zap.Field) {
logger.Info(msg, fields...)
log.Info(msg, fields...)
}
}
}
collector = NewLogCollector(logF)
}
type logCollector struct {
mu sync.Mutex
unit string
successUnitCount int
failureUnitCount int
successCosts map[string]time.Duration
successData map[string]uint64
failureReasons map[string]error
durations map[string]time.Duration
ints map[string]int
uints map[string]uint64
successStatus bool
startTime time.Time
log logFunc
}
// NewLogCollector returns a new LogCollector.
func NewLogCollector(log logFunc) LogCollector {
return &logCollector{
successUnitCount: 0,
failureUnitCount: 0,
successCosts: make(map[string]time.Duration),
successData: make(map[string]uint64),
failureReasons: make(map[string]error),
durations: make(map[string]time.Duration),
ints: make(map[string]int),
uints: make(map[string]uint64),
log: log,
startTime: time.Now(),
}
}
func (tc *logCollector) SetUnit(unit string) {
tc.mu.Lock()
defer tc.mu.Unlock()
tc.unit = unit
}
func (tc *logCollector) CollectSuccessUnit(name string, unitCount int, arg interface{}) {
tc.mu.Lock()
defer tc.mu.Unlock()
switch v := arg.(type) {
case time.Duration:
tc.successUnitCount += unitCount
tc.successCosts[name] += v
case uint64:
tc.successData[name] += v
}
}
func (tc *logCollector) CollectFailureUnit(name string, reason error) {
tc.mu.Lock()
defer tc.mu.Unlock()
if _, ok := tc.failureReasons[name]; !ok {
tc.failureReasons[name] = reason
tc.failureUnitCount++
}
}
func (tc *logCollector) CollectDuration(name string, t time.Duration) {
tc.mu.Lock()
defer tc.mu.Unlock()
tc.durations[name] += t
}
func (tc *logCollector) CollectInt(name string, t int) {
tc.mu.Lock()
defer tc.mu.Unlock()
tc.ints[name] += t
}
func (tc *logCollector) CollectUInt(name string, t uint64) {
tc.mu.Lock()
defer tc.mu.Unlock()
tc.uints[name] += t
}
func (tc *logCollector) SetSuccessStatus(success bool) {
tc.mu.Lock()
defer tc.mu.Unlock()
tc.successStatus = success
}
func (tc *logCollector) Summary(name string) {
tc.mu.Lock()
defer func() {
tc.durations = make(map[string]time.Duration)
tc.ints = make(map[string]int)
tc.successCosts = make(map[string]time.Duration)
tc.failureReasons = make(map[string]error)
tc.mu.Unlock()
}()
var msg string
switch tc.unit {
case BackupUnit:
msg = fmt.Sprintf("total backup ranges: %d, total success: %d, total failed: %d",
tc.failureUnitCount+tc.successUnitCount, tc.successUnitCount, tc.failureUnitCount)
case RestoreUnit:
msg = fmt.Sprintf("total restore files: %d, total success: %d, total failed: %d",
tc.failureUnitCount+tc.successUnitCount, tc.successUnitCount, tc.failureUnitCount)
}
logFields := make([]zap.Field, 0, len(tc.durations)+len(tc.ints))
for key, val := range tc.durations {
logFields = append(logFields, zap.Duration(key, val))
}
for key, val := range tc.ints {
logFields = append(logFields, zap.Int(key, val))
}
for key, val := range tc.uints {
logFields = append(logFields, zap.Uint64(key, val))
}
if len(tc.failureReasons) != 0 || !tc.successStatus {
for unitName, reason := range tc.failureReasons {
logFields = append(logFields, zap.String("unitName", unitName), zap.Error(reason))
}
log.Info(name+" Failed summary : "+msg, logFields...)
return
}
totalCost := time.Duration(0)
for _, cost := range tc.successCosts {
totalCost += cost
}
msg += fmt.Sprintf(", total take(%s time): %s", name, totalCost)
msg += fmt.Sprintf(", total take(real time): %s", time.Since(tc.startTime))
for name, data := range tc.successData {
if name == TotalBytes {
fData := float64(data) / 1024 / 1024
if fData > 1 {
msg += fmt.Sprintf(", total size(MB): %.2f", fData)
msg += fmt.Sprintf(", avg speed(MB/s): %.2f", fData/totalCost.Seconds())
} else {
msg += fmt.Sprintf(", total size(Byte): %d", data)
msg += fmt.Sprintf(", avg speed(Byte/s): %.2f", float64(data)/totalCost.Seconds())
}
continue
}
msg += fmt.Sprintf(", %s: %d", name, data)
}
tc.log(name+" Success summary: "+msg, logFields...)
}
// SetLogCollector allow pass LogCollector outside.
func SetLogCollector(l LogCollector) {
collector = l
}