forked from Shopify/ghostferry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
203 lines (164 loc) · 5.97 KB
/
status.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
package ghostferry
import (
"fmt"
"math"
"sort"
"time"
"github.com/siddontang/go-mysql/mysql"
)
type TableStatus struct {
TableName string
PrimaryKeyName string
Status string
LastSuccessfulPK uint64
TargetPK uint64
}
type Status struct {
GhostferryVersion string
SourceHostPort string
TargetHostPort string
OverallState string
StartTime time.Time
CurrentTime time.Time
TimeTaken time.Duration
ETA time.Duration
BinlogStreamerLag time.Duration
PKsPerSecond uint64
AutomaticCutover bool
BinlogStreamerStopRequested bool
LastSuccessfulBinlogPos mysql.Position
TargetBinlogPos mysql.Position
Throttled bool
CompletedTableCount int
TotalTableCount int
TableStatuses []*TableStatus
AllTableNames []string
AllDatabaseNames []string
VerifierSupport bool
VerifierAvailable bool
VerificationStarted bool
VerificationDone bool
VerificationResult VerificationResult
VerificationErr error
}
func FetchStatus(f *Ferry, v Verifier) *Status {
status := &Status{}
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 = f.OverallState
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.targetBinlogPosition
status.Throttled = f.Throttler.Throttled()
// Getting all table statuses
status.TableStatuses = make([]*TableStatus, 0, len(f.Tables))
completedTables := f.DataIterator.CurrentState.CompletedTables()
targetPKs := f.DataIterator.CurrentState.TargetPrimaryKeys()
lastSuccessfulPKs := f.DataIterator.CurrentState.LastSuccessfulPrimaryKeys()
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 lastSuccessfulPKs {
if _, ok := completedTables[tableName]; ok {
continue // already completed, therefore not copying
}
copyingTableNames = append(copyingTableNames, tableName)
}
for tableName, _ := range f.Tables {
if lastSuccessfulPK, ok := lastSuccessfulPKs[tableName]; ok && lastSuccessfulPK != 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
// lastSuccessfulPKs 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, &TableStatus{
TableName: tableName,
PrimaryKeyName: f.Tables[tableName].GetPKColumn(0).Name,
Status: "complete",
TargetPK: targetPKs[tableName],
LastSuccessfulPK: lastSuccessfulPKs[tableName],
})
}
for _, tableName := range copyingTableNames {
status.TableStatuses = append(status.TableStatuses, &TableStatus{
TableName: tableName,
PrimaryKeyName: f.Tables[tableName].GetPKColumn(0).Name,
Status: "copying",
TargetPK: targetPKs[tableName],
LastSuccessfulPK: lastSuccessfulPKs[tableName],
})
}
for _, tableName := range waitingTableNames {
status.TableStatuses = append(status.TableStatuses, &TableStatus{
TableName: tableName,
PrimaryKeyName: f.Tables[tableName].GetPKColumn(0).Name,
Status: "waiting",
TargetPK: targetPKs[tableName],
LastSuccessfulPK: 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 totalPKsToCopy uint64 = 0
var completedPKs uint64 = 0
estimatedPKsPerSecond := f.DataIterator.CurrentState.EstimatedPKProcessedPerSecond()
for _, targetPK := range targetPKs {
totalPKsToCopy += targetPK
}
for _, completedPK := range lastSuccessfulPKs {
completedPKs += completedPK
}
status.ETA = time.Duration(math.Ceil(float64(totalPKsToCopy-completedPKs)/estimatedPKsPerSecond)) * time.Second
status.PKsPerSecond = uint64(estimatedPKsPerSecond)
// Verifier display
if v != nil {
status.VerifierSupport = true
result, err := v.Result()
status.VerificationStarted = result.IsStarted()
status.VerificationDone = result.IsDone()
// 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
}