-
Notifications
You must be signed in to change notification settings - Fork 72
/
state_tracker.go
255 lines (204 loc) · 7.94 KB
/
state_tracker.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package ghostferry
import (
"container/ring"
"math"
"sync"
"time"
"github.com/siddontang/go-mysql/mysql"
)
// StateTracker design
// ===================
//
// General Overview
// ----------------
//
// The state tracker keeps track of the progress of Ghostferry so it can be
// interrupted and resumed. The state tracker is supposed to be initialized and
// managed by the Ferry. Each Ghostferry components, such as the `BatchWriter`,
// will get passed an instance of the StateTracker. During the run, the
// components will update their last successful components to the state tracker
// instance given via the state tracker API defined here.
//
// The states stored in the state tracker can be copied into a
// serialization-friendly struct (`SerializableState`), which can then be
// dumped using something like JSON. Assuming the rest of Ghostferry used the
// API of the state tracker correctlym this can be done at any point during the
// Ghostferry run and the resulting state can be resumed from without data
// loss. The same `SerializableState` is used as an input to `Ferry`, which
// will instruct the `Ferry` to resume a previously interrupted run.
type SerializableState struct {
GhostferryVersion string
LastKnownTableSchemaCache TableSchemaCache
LastSuccessfulPaginationKeys map[string]uint64
CompletedTables map[string]bool
LastWrittenBinlogPosition mysql.Position
BinlogVerifyStore BinlogVerifySerializedStore
LastStoredBinlogPositionForInlineVerifier mysql.Position
LastStoredBinlogPositionForTargetVerifier mysql.Position
}
func (s *SerializableState) MinSourceBinlogPosition() mysql.Position {
nilPosition := mysql.Position{}
if s.LastWrittenBinlogPosition == nilPosition {
return s.LastStoredBinlogPositionForInlineVerifier
}
if s.LastStoredBinlogPositionForInlineVerifier == nilPosition {
return s.LastWrittenBinlogPosition
}
if s.LastWrittenBinlogPosition.Compare(s.LastStoredBinlogPositionForInlineVerifier) >= 0 {
return s.LastStoredBinlogPositionForInlineVerifier
} else {
return s.LastWrittenBinlogPosition
}
}
// For tracking the speed of the copy
type PaginationKeyPositionLog struct {
Position uint64
At time.Time
}
func newSpeedLogRing(speedLogCount int) *ring.Ring {
if speedLogCount <= 0 {
return nil
}
speedLog := ring.New(speedLogCount)
speedLog.Value = PaginationKeyPositionLog{
Position: 0,
At: time.Now(),
}
return speedLog
}
type StateTracker struct {
BinlogRWMutex *sync.RWMutex
CopyRWMutex *sync.RWMutex
lastWrittenBinlogPosition mysql.Position
lastStoredBinlogPositionForInlineVerifier mysql.Position
lastStoredBinlogPositionForTargetVerifier mysql.Position
lastSuccessfulPaginationKeys map[string]uint64
completedTables map[string]bool
iterationSpeedLog *ring.Ring
}
func NewStateTracker(speedLogCount int) *StateTracker {
return &StateTracker{
BinlogRWMutex: &sync.RWMutex{},
CopyRWMutex: &sync.RWMutex{},
lastSuccessfulPaginationKeys: make(map[string]uint64),
completedTables: make(map[string]bool),
iterationSpeedLog: newSpeedLogRing(speedLogCount),
}
}
// serializedState is a state the tracker should start from, as opposed to
// starting from the beginning.
func NewStateTrackerFromSerializedState(speedLogCount int, serializedState *SerializableState) *StateTracker {
s := NewStateTracker(speedLogCount)
s.lastSuccessfulPaginationKeys = serializedState.LastSuccessfulPaginationKeys
s.completedTables = serializedState.CompletedTables
s.lastWrittenBinlogPosition = serializedState.LastWrittenBinlogPosition
s.lastStoredBinlogPositionForInlineVerifier = serializedState.LastStoredBinlogPositionForInlineVerifier
s.lastStoredBinlogPositionForTargetVerifier = serializedState.LastStoredBinlogPositionForInlineVerifier
return s
}
func (s *StateTracker) UpdateLastResumableSourceBinlogPosition(pos mysql.Position) {
s.BinlogRWMutex.Lock()
defer s.BinlogRWMutex.Unlock()
s.lastWrittenBinlogPosition = pos
}
func (s *StateTracker) UpdateLastResumableSourceBinlogPositionForInlineVerifier(pos mysql.Position) {
s.BinlogRWMutex.Lock()
defer s.BinlogRWMutex.Unlock()
s.lastStoredBinlogPositionForInlineVerifier = pos
}
func (s *StateTracker) UpdateLastResumableBinlogPositionForTargetVerifier(pos mysql.Position) {
s.BinlogRWMutex.Lock()
defer s.BinlogRWMutex.Unlock()
s.lastStoredBinlogPositionForTargetVerifier = pos
}
func (s *StateTracker) UpdateLastSuccessfulPaginationKey(table string, paginationKey uint64) {
s.CopyRWMutex.Lock()
defer s.CopyRWMutex.Unlock()
deltaPaginationKey := paginationKey - s.lastSuccessfulPaginationKeys[table]
s.lastSuccessfulPaginationKeys[table] = paginationKey
s.updateSpeedLog(deltaPaginationKey)
}
func (s *StateTracker) LastSuccessfulPaginationKey(table string) uint64 {
s.CopyRWMutex.RLock()
defer s.CopyRWMutex.RUnlock()
_, found := s.completedTables[table]
if found {
return math.MaxUint64
}
paginationKey, found := s.lastSuccessfulPaginationKeys[table]
if !found {
return 0
}
return paginationKey
}
func (s *StateTracker) MarkTableAsCompleted(table string) {
s.CopyRWMutex.Lock()
defer s.CopyRWMutex.Unlock()
s.completedTables[table] = true
}
func (s *StateTracker) IsTableComplete(table string) bool {
s.CopyRWMutex.RLock()
defer s.CopyRWMutex.RUnlock()
return s.completedTables[table]
}
// This is reasonably accurate if the rows copied are distributed uniformly
// between paginationKey = 0 -> max(paginationKey). It would not be accurate if the distribution is
// concentrated in a particular region.
func (s *StateTracker) EstimatedPaginationKeysPerSecond() float64 {
if s.iterationSpeedLog == nil {
return 0.0
}
s.CopyRWMutex.RLock()
defer s.CopyRWMutex.RUnlock()
if s.iterationSpeedLog.Value.(PaginationKeyPositionLog).Position == 0 {
return 0.0
}
earliest := s.iterationSpeedLog
for earliest.Prev() != nil && earliest.Prev() != s.iterationSpeedLog && earliest.Prev().Value.(PaginationKeyPositionLog).Position != 0 {
earliest = earliest.Prev()
}
currentValue := s.iterationSpeedLog.Value.(PaginationKeyPositionLog)
earliestValue := earliest.Value.(PaginationKeyPositionLog)
deltaPaginationKey := currentValue.Position - earliestValue.Position
deltaT := currentValue.At.Sub(earliestValue.At).Seconds()
return float64(deltaPaginationKey) / deltaT
}
func (s *StateTracker) updateSpeedLog(deltaPaginationKey uint64) {
if s.iterationSpeedLog == nil {
return
}
currentTotalPaginationKey := s.iterationSpeedLog.Value.(PaginationKeyPositionLog).Position
s.iterationSpeedLog = s.iterationSpeedLog.Next()
s.iterationSpeedLog.Value = PaginationKeyPositionLog{
Position: currentTotalPaginationKey + deltaPaginationKey,
At: time.Now(),
}
}
func (s *StateTracker) Serialize(lastKnownTableSchemaCache TableSchemaCache, binlogVerifyStore *BinlogVerifyStore) *SerializableState {
s.BinlogRWMutex.RLock()
defer s.BinlogRWMutex.RUnlock()
s.CopyRWMutex.RLock()
defer s.CopyRWMutex.RUnlock()
state := &SerializableState{
GhostferryVersion: VersionString,
LastKnownTableSchemaCache: lastKnownTableSchemaCache,
LastSuccessfulPaginationKeys: make(map[string]uint64),
CompletedTables: make(map[string]bool),
LastWrittenBinlogPosition: s.lastWrittenBinlogPosition,
LastStoredBinlogPositionForInlineVerifier: s.lastStoredBinlogPositionForInlineVerifier,
LastStoredBinlogPositionForTargetVerifier: s.lastStoredBinlogPositionForTargetVerifier,
}
if binlogVerifyStore != nil {
state.BinlogVerifyStore = binlogVerifyStore.Serialize()
}
// Need a copy because lastSuccessfulPaginationKeys may change after Serialize
// returns. This would inaccurately reflect the state of Ghostferry when
// Serialize is called.
for k, v := range s.lastSuccessfulPaginationKeys {
state.LastSuccessfulPaginationKeys[k] = v
}
for k, v := range s.completedTables {
state.CompletedTables[k] = v
}
return state
}