forked from Shopify/ghostferry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status_deprecated.go
216 lines (173 loc) · 7.01 KB
/
status_deprecated.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
package ghostferry
import (
"fmt"
"math"
"sort"
"time"
"github.com/siddontang/go-mysql/mysql"
)
// NOTE: This file is only used for the ControlServer for now.
// TODO: eventually merge this into the ControlServer and use the Progress struct.
type TableStatusDeprecated struct {
TableName string
PaginationKeyName string
Status string
LastSuccessfulPaginationKey uint64
TargetPaginationKey uint64
}
type StatusDeprecated struct {
GhostferryVersion string
SourceHostPort string
TargetHostPort string
OverallState string
StartTime time.Time
CurrentTime time.Time
TimeTaken time.Duration
ETA time.Duration
BinlogStreamerLag time.Duration
PaginationKeysPerSecond uint64
AutomaticCutover bool
BinlogStreamerStopRequested bool
LastSuccessfulBinlogPos mysql.Position
TargetBinlogPos mysql.Position
Throttled bool
CompletedTableCount int
TotalTableCount int
TableStatuses []*TableStatusDeprecated
AllTableNames []string
AllDatabaseNames []string
VerifierSupport bool
VerifierAvailable bool
VerifierMessage string
VerificationStarted bool
VerificationDone bool
VerificationResult VerificationResult
VerificationErr error
}
func FetchStatusDeprecated(f *Ferry, v Verifier) *StatusDeprecated {
status := &StatusDeprecated{}
status.GhostferryVersion = VersionString
status.SourceHostPort = fmt.Sprintf("%s:%d", f.Source.Host, f.Source.Port)
status.TargetHostPort = fmt.Sprintf("%s:%d", f.Target.Host, f.Target.Port)
status.OverallState = fmt.Sprintf("%s", f.OverallState.Load())
status.StartTime = f.StartTime
status.CurrentTime = time.Now()
if f.DoneTime.IsZero() {
status.TimeTaken = status.CurrentTime.Sub(status.StartTime)
} else {
status.TimeTaken = f.DoneTime.Sub(status.StartTime)
}
status.BinlogStreamerLag = time.Now().Sub(f.BinlogStreamer.lastProcessedEventTime)
status.AutomaticCutover = f.Config.AutomaticCutover
status.BinlogStreamerStopRequested = f.BinlogStreamer.stopRequested
status.LastSuccessfulBinlogPos = f.BinlogStreamer.lastStreamedBinlogPosition
status.TargetBinlogPos = f.BinlogStreamer.stopAtBinlogPosition
status.Throttled = f.Throttler.Throttled()
// Getting all table statuses
status.TableStatuses = make([]*TableStatusDeprecated, 0, len(f.Tables))
serializedState := f.StateTracker.Serialize(nil, nil)
lastSuccessfulPaginationKeys := serializedState.LastSuccessfulPaginationKeys
completedTables := serializedState.CompletedTables
targetPaginationKeys := make(map[string]uint64)
f.DataIterator.targetPaginationKeys.Range(func(k, v interface{}) bool {
targetPaginationKeys[k.(string)] = v.(uint64)
return true
})
status.CompletedTableCount = len(completedTables)
status.TotalTableCount = len(f.Tables)
status.AllTableNames = f.Tables.AllTableNames()
sort.Strings(status.AllTableNames)
dbSet := make(map[string]bool)
for _, table := range f.Tables.AsSlice() {
dbSet[table.Schema] = true
}
status.AllDatabaseNames = make([]string, 0, len(dbSet))
for dbName := range dbSet {
status.AllDatabaseNames = append(status.AllDatabaseNames, dbName)
}
sort.Strings(status.AllDatabaseNames)
// We get the name first because we need to sort them
completedTableNames := make([]string, 0, len(completedTables))
copyingTableNames := make([]string, 0, len(f.Tables))
waitingTableNames := make([]string, 0, len(f.Tables))
for tableName, _ := range completedTables {
completedTableNames = append(completedTableNames, tableName)
}
for tableName, _ := range lastSuccessfulPaginationKeys {
if _, ok := completedTables[tableName]; ok {
continue // already completed, therefore not copying
}
copyingTableNames = append(copyingTableNames, tableName)
}
for tableName, _ := range f.Tables {
if lastSuccessfulPaginationKey, ok := lastSuccessfulPaginationKeys[tableName]; ok && lastSuccessfulPaginationKey != 0 {
continue // already started, therefore not waiting
}
if _, ok := completedTables[tableName]; ok {
// There are no data in that table, thus it does not have an entry in
// lastSuccessfulPaginationKeys but has an entry in completedTables
continue
}
waitingTableNames = append(waitingTableNames, tableName)
}
sort.Strings(completedTableNames)
sort.Strings(copyingTableNames)
sort.Strings(waitingTableNames)
for _, tableName := range completedTableNames {
status.TableStatuses = append(status.TableStatuses, &TableStatusDeprecated{
TableName: tableName,
PaginationKeyName: f.Tables[tableName].GetPaginationColumn().Name,
Status: "complete",
TargetPaginationKey: targetPaginationKeys[tableName],
LastSuccessfulPaginationKey: lastSuccessfulPaginationKeys[tableName],
})
}
for _, tableName := range copyingTableNames {
status.TableStatuses = append(status.TableStatuses, &TableStatusDeprecated{
TableName: tableName,
PaginationKeyName: f.Tables[tableName].GetPaginationColumn().Name,
Status: "copying",
TargetPaginationKey: targetPaginationKeys[tableName],
LastSuccessfulPaginationKey: lastSuccessfulPaginationKeys[tableName],
})
}
for _, tableName := range waitingTableNames {
status.TableStatuses = append(status.TableStatuses, &TableStatusDeprecated{
TableName: tableName,
PaginationKeyName: f.Tables[tableName].GetPaginationColumn().Name,
Status: "waiting",
TargetPaginationKey: targetPaginationKeys[tableName],
LastSuccessfulPaginationKey: 0,
})
}
// ETA estimation
// We do it here rather than in DataIteratorState to give the lock back
// ASAP. It's not supposed to be that accurate anyway.
var totalPaginationKeysToCopy uint64 = 0
var completedPaginationKeys uint64 = 0
estimatedPaginationKeysPerSecond := f.StateTracker.EstimatedPaginationKeysPerSecond()
for _, targetPaginationKey := range targetPaginationKeys {
totalPaginationKeysToCopy += targetPaginationKey
}
for _, completedPaginationKey := range lastSuccessfulPaginationKeys {
completedPaginationKeys += completedPaginationKey
}
status.ETA = time.Duration(math.Ceil(float64(totalPaginationKeysToCopy-completedPaginationKeys)/estimatedPaginationKeysPerSecond)) * time.Second
status.PaginationKeysPerSecond = uint64(estimatedPaginationKeysPerSecond)
// Verifier display
if v != nil {
status.VerifierSupport = true
result, err := v.Result()
status.VerificationStarted = result.IsStarted()
status.VerificationDone = result.IsDone()
status.VerifierMessage = v.Message()
// We can only run the verifier if we're not copying and not verifying
status.VerifierAvailable = status.OverallState != StateStarting && status.OverallState != StateCopying && (!status.VerificationStarted || status.VerificationDone)
status.VerificationResult = result.VerificationResult
status.VerificationErr = err
} else {
status.VerifierSupport = false
status.VerifierAvailable = false
}
return status
}