forked from Shopify/ghostferry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_iterator.go
208 lines (169 loc) · 5.86 KB
/
data_iterator.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
package ghostferry
import (
"fmt"
"math"
"sync"
sql "github.com/Shopify/ghostferry/sqlwrapper"
"github.com/sirupsen/logrus"
)
type DataIterator struct {
DB *sql.DB
Concurrency int
SelectFingerprint bool
ErrorHandler ErrorHandler
CursorConfig *CursorConfig
StateTracker *StateTracker
TableSorter DataIteratorSorter
targetPaginationKeys *sync.Map
batchListeners []func(*RowBatch) error
doneListeners []func() error
logger *logrus.Entry
}
type TableMaxPaginationKey struct {
Table *TableSchema
MaxPaginationKey uint64
}
func (d *DataIterator) Run(tables []*TableSchema) {
d.logger = logrus.WithField("tag", "data_iterator")
d.targetPaginationKeys = &sync.Map{}
// If a state tracker is not provided, then the caller doesn't care about
// tracking state. However, some methods are still useful so we initialize
// a minimal local instance.
if d.StateTracker == nil {
d.StateTracker = NewStateTracker(0)
}
d.logger.WithField("tablesCount", len(tables)).Info("starting data iterator run")
tablesWithData, emptyTables, err := MaxPaginationKeys(d.DB, tables, d.logger)
if err != nil {
d.ErrorHandler.Fatal("data_iterator", err)
}
for _, table := range emptyTables {
d.StateTracker.MarkTableAsCompleted(table.String())
}
for table, maxPaginationKey := range tablesWithData {
tableName := table.String()
if d.StateTracker.IsTableComplete(tableName) {
// In a previous run, the table may have been completed.
// We don't need to reiterate those tables as it has already been done.
delete(tablesWithData, table)
} else {
d.targetPaginationKeys.Store(tableName, maxPaginationKey)
}
}
tablesQueue := make(chan *TableSchema)
wg := &sync.WaitGroup{}
wg.Add(d.Concurrency)
for i := 0; i < d.Concurrency; i++ {
go func() {
defer wg.Done()
for {
table, ok := <-tablesQueue
if !ok {
break
}
logger := d.logger.WithField("table", table.String())
targetPaginationKeyInterface, found := d.targetPaginationKeys.Load(table.String())
if !found {
err := fmt.Errorf("%s not found in targetPaginationKeys, this is likely a programmer error", table.String())
logger.WithError(err).Error("this is definitely a bug")
d.ErrorHandler.Fatal("data_iterator", err)
return
}
startPaginationKey := d.StateTracker.LastSuccessfulPaginationKey(table.String())
if startPaginationKey == math.MaxUint64 {
err := fmt.Errorf("%v has been marked as completed but a table iterator has been spawned, this is likely a programmer error which resulted in the inconsistent starting state", table.String())
logger.WithError(err).Error("this is definitely a bug")
d.ErrorHandler.Fatal("data_iterator", err)
return
}
cursor := d.CursorConfig.NewCursor(table, startPaginationKey, targetPaginationKeyInterface.(uint64))
if d.SelectFingerprint {
if len(cursor.ColumnsToSelect) == 0 {
cursor.ColumnsToSelect = []string{"*"}
}
cursor.ColumnsToSelect = append(cursor.ColumnsToSelect, table.RowMd5Query())
}
err := cursor.Each(func(batch *RowBatch) error {
metrics.Count("RowEvent", int64(batch.Size()), []MetricTag{
MetricTag{"table", table.Name},
MetricTag{"source", "table"},
}, 1.0)
if d.SelectFingerprint {
fingerprints := make(map[uint64][]byte)
rows := make([]RowData, batch.Size())
for i, rowData := range batch.Values() {
paginationKey, err := rowData.GetUint64(batch.PaginationKeyIndex())
if err != nil {
logger.WithError(err).Error("failed to get paginationKey data")
return err
}
fingerprints[paginationKey] = rowData[len(rowData)-1].([]byte)
rows[i] = rowData[:len(rowData)-1]
}
batch = &RowBatch{
values: rows,
paginationKeyIndex: batch.PaginationKeyIndex(),
table: table,
fingerprints: fingerprints,
columns: batch.columns[:len(batch.columns)-1],
}
}
for _, listener := range d.batchListeners {
err := listener(batch)
if err != nil {
logger.WithError(err).Error("failed to process row batch with listeners")
return err
}
}
return nil
})
if err != nil {
switch e := err.(type) {
case BatchWriterVerificationFailed:
logger.WithField("incorrect_tables", e.table).Error(e.Error())
d.ErrorHandler.Fatal("inline_verifier", err)
default:
logger.WithError(err).Error("failed to iterate table")
d.ErrorHandler.Fatal("data_iterator", err)
}
}
logger.Debug("table iteration completed")
// Right now the BatchWriter.WriteRowBatch happens synchronously in
// this method. If it ever becomes async, this MarkTableAsCompleted
// call MUST be done in WriteRowBatch somehow.
d.StateTracker.MarkTableAsCompleted(table.String())
}
}()
}
sorter := MaxTableSizeSorter{DataIterator: d}
sortedTableData, err := sorter.Sort(tablesWithData)
if err != nil {
d.logger.WithError(err).Error("failed to retrieve sorted tables")
d.ErrorHandler.Fatal("data_iterator", err)
return
}
loggingIncrement := len(tablesWithData) / 50
if loggingIncrement == 0 {
loggingIncrement = 1
}
i := 0
for _, tableData := range sortedTableData {
tablesQueue <- tableData.Table
i++
if i%loggingIncrement == 0 {
d.logger.WithField("table", tableData.Table.String()).Infof("queued table for processing (%d/%d)", i, len(sortedTableData))
}
}
d.logger.Info("done queueing tables to be iterated, closing table channel")
close(tablesQueue)
wg.Wait()
for _, listener := range d.doneListeners {
listener()
}
}
func (d *DataIterator) AddBatchListener(listener func(*RowBatch) error) {
d.batchListeners = append(d.batchListeners, listener)
}
func (d *DataIterator) AddDoneListener(listener func() error) {
d.doneListeners = append(d.doneListeners, listener)
}