/
progress.go
221 lines (186 loc) · 5.6 KB
/
progress.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
219
220
221
package web
import (
"encoding/json"
"sync"
"github.com/pingcap/errors"
"github.com/wuhuizuo/tidb6/br/pkg/lightning/checkpoints"
"github.com/wuhuizuo/tidb6/br/pkg/lightning/common"
"github.com/wuhuizuo/tidb6/br/pkg/lightning/mydump"
"go.uber.org/atomic"
)
// checkpointsMap is a concurrent map (table name → checkpoints).
//
// Implementation note: Currently the checkpointsMap is only written from a
// single goroutine inside (*RestoreController).listenCheckpointUpdates(), so
// all writes are going to be single threaded. Writing to checkpoint is not
// considered performance critical. The map can be read from any HTTP connection
// goroutine. Therefore, we simply implement the concurrent map using a single
// RWMutex. We may switch to more complicated data structure if contention is
// shown to be a problem.
//
// Do not implement this using a sync.Map, its mutex can't protect the content
// of a pointer.
type checkpointsMap struct {
mu sync.RWMutex
checkpoints map[string]*checkpoints.TableCheckpoint
}
func makeCheckpointsMap() (res checkpointsMap) {
res.checkpoints = make(map[string]*checkpoints.TableCheckpoint)
return
}
func (cpm *checkpointsMap) clear() {
cpm.mu.Lock()
cpm.checkpoints = make(map[string]*checkpoints.TableCheckpoint)
cpm.mu.Unlock()
}
func (cpm *checkpointsMap) insert(key string, cp *checkpoints.TableCheckpoint) {
cpm.mu.Lock()
cpm.checkpoints[key] = cp
cpm.mu.Unlock()
}
type totalWritten struct {
key string
totalWritten int64
}
func (cpm *checkpointsMap) update(diffs map[string]*checkpoints.TableCheckpointDiff) []totalWritten {
totalWrittens := make([]totalWritten, 0, len(diffs))
cpm.mu.Lock()
defer cpm.mu.Unlock()
for key, diff := range diffs {
cp := cpm.checkpoints[key]
cp.Apply(diff)
tw := int64(0)
for _, engine := range cp.Engines {
for _, chunk := range engine.Chunks {
if engine.Status >= checkpoints.CheckpointStatusAllWritten {
tw += chunk.TotalSize()
} else {
tw += chunk.Chunk.Offset - chunk.Key.Offset
}
}
}
totalWrittens = append(totalWrittens, totalWritten{key: key, totalWritten: tw})
}
return totalWrittens
}
func (cpm *checkpointsMap) marshal(key string) ([]byte, error) {
cpm.mu.RLock()
defer cpm.mu.RUnlock()
if cp, ok := cpm.checkpoints[key]; ok {
return json.Marshal(cp)
}
return nil, errors.NotFoundf("table %s", key)
}
type taskStatus uint8
const (
taskStatusRunning taskStatus = 1
taskStatusCompleted taskStatus = 2
)
type tableInfo struct {
TotalWritten int64 `json:"w"`
TotalSize int64 `json:"z"`
Status taskStatus `json:"s"`
Message string `json:"m,omitempty"`
}
type taskProgress struct {
mu sync.RWMutex
Tables map[string]*tableInfo `json:"t"`
Status taskStatus `json:"s"`
Message string `json:"m,omitempty"`
// The contents have their own mutex for protection
checkpoints checkpointsMap
}
var (
currentProgress *taskProgress
// whether progress is enabled
progressEnabled = atomic.NewBool(false)
)
// EnableCurrentProgress init current progress struct on demand.
// NOTE: this call is not thread safe, so it should only be inited once at the very beginning of progress start.
func EnableCurrentProgress() {
currentProgress = &taskProgress{
checkpoints: makeCheckpointsMap(),
}
progressEnabled.Store(true)
}
func BroadcastStartTask() {
if !progressEnabled.Load() {
return
}
currentProgress.mu.Lock()
currentProgress.Status = taskStatusRunning
currentProgress.mu.Unlock()
currentProgress.checkpoints.clear()
}
func BroadcastEndTask(err error) {
if !progressEnabled.Load() {
return
}
errString := errors.ErrorStack(err)
currentProgress.mu.Lock()
currentProgress.Status = taskStatusCompleted
currentProgress.Message = errString
currentProgress.mu.Unlock()
}
func BroadcastInitProgress(databases []*mydump.MDDatabaseMeta) {
if !progressEnabled.Load() {
return
}
tables := make(map[string]*tableInfo, len(databases))
for _, db := range databases {
for _, tbl := range db.Tables {
name := common.UniqueTable(db.Name, tbl.Name)
tables[name] = &tableInfo{TotalSize: tbl.TotalSize}
}
}
currentProgress.mu.Lock()
currentProgress.Tables = tables
currentProgress.mu.Unlock()
}
func BroadcastTableCheckpoint(tableName string, cp *checkpoints.TableCheckpoint) {
if !progressEnabled.Load() {
return
}
currentProgress.mu.Lock()
currentProgress.Tables[tableName].Status = taskStatusRunning
currentProgress.mu.Unlock()
// create a deep copy to avoid false sharing
currentProgress.checkpoints.insert(tableName, cp.DeepCopy())
}
func BroadcastCheckpointDiff(diffs map[string]*checkpoints.TableCheckpointDiff) {
if !progressEnabled.Load() {
return
}
totalWrittens := currentProgress.checkpoints.update(diffs)
currentProgress.mu.Lock()
for _, tw := range totalWrittens {
currentProgress.Tables[tw.key].TotalWritten = tw.totalWritten
}
currentProgress.mu.Unlock()
}
func BroadcastError(tableName string, err error) {
if !progressEnabled.Load() {
return
}
errString := errors.ErrorStack(err)
currentProgress.mu.Lock()
if tbl := currentProgress.Tables[tableName]; tbl != nil {
tbl.Status = taskStatusCompleted
tbl.Message = errString
}
currentProgress.mu.Unlock()
}
func MarshalTaskProgress() ([]byte, error) {
if !progressEnabled.Load() {
return nil, errors.New("progress is not enabled")
}
currentProgress.mu.RLock()
defer currentProgress.mu.RUnlock()
return json.Marshal(¤tProgress)
}
func MarshalTableCheckpoints(tableName string) ([]byte, error) {
if !progressEnabled.Load() {
return nil, errors.New("progress is not enabled")
}
return currentProgress.checkpoints.marshal(tableName)
}