forked from Shopify/ghostferry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
258 lines (215 loc) · 5.25 KB
/
utils.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
256
257
258
package ghostferry
import (
"context"
"crypto/rand"
sqlorig "database/sql"
"encoding/binary"
"fmt"
"sync"
"sync/atomic"
"time"
sql "github.com/Shopify/ghostferry/sqlwrapper"
"github.com/go-mysql-org/go-mysql/mysql"
"github.com/go-mysql-org/go-mysql/schema"
"github.com/sirupsen/logrus"
)
func WithRetries(maxRetries int, sleep time.Duration, logger *logrus.Entry, verb string, f func() error) (err error) {
return WithRetriesContext(nil, maxRetries, sleep, logger, verb, f)
}
func WithRetriesContext(ctx context.Context, maxRetries int, sleep time.Duration, logger *logrus.Entry, verb string, f func() error) (err error) {
try := 1
if logger == nil {
logger = logrus.NewEntry(logrus.StandardLogger())
}
for {
if ctx != nil {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
}
err = f()
if err == nil || err == context.Canceled {
return err
}
if maxRetries != 0 && try >= maxRetries {
break
}
logger.WithError(err).Errorf("failed to %s, %d of %d max retries", verb, try, maxRetries)
try++
time.Sleep(sleep)
}
logger.WithError(err).Errorf("failed to %s after %d attempts, retry limit exceeded", verb, try)
return
}
func randomServerId() uint32 {
var buf [4]byte
if _, err := rand.Read(buf[:]); err != nil {
panic(err)
}
return binary.LittleEndian.Uint32(buf[:])
}
type AtomicBoolean int32
func (a *AtomicBoolean) Set(b bool) {
var v int32 = 0
if b {
v = 1
}
atomic.StoreInt32((*int32)(a), v)
}
func (a *AtomicBoolean) Get() bool {
return atomic.LoadInt32((*int32)(a)) == int32(1)
}
type WorkerPool struct {
Concurrency int
Process func(int) (interface{}, error)
}
// Returns a list of results of the size same as the concurrency number.
// Returns the first error that occurs during the run. Also as soon as
// a single worker errors, all workers terminates.
func (p *WorkerPool) Run(n int) ([]interface{}, error) {
results := make([]interface{}, p.Concurrency)
errCh := make(chan error, p.Concurrency)
workQueue := make(chan int)
wg := &sync.WaitGroup{}
wg.Add(p.Concurrency)
for j := 0; j < p.Concurrency; j++ {
go func(j int) {
defer wg.Done()
for workIndex := range workQueue {
result, err := p.Process(workIndex)
results[j] = result
if err != nil {
errCh <- err
return
}
}
errCh <- nil
}(j)
}
var err error = nil
i := 0
loop:
for i < n {
select {
case workQueue <- i:
i++
case err = <-errCh: // abort pool if an error was discovered
if err != nil {
break loop
}
default:
time.Sleep(500 * time.Millisecond)
}
}
close(workQueue)
wg.Wait()
close(errCh)
if err != nil {
return results, err
}
for e := range errCh {
if e != nil {
err = e
}
}
return results, err
}
type StmtCache struct {
mut sync.RWMutex
statements map[string]*sqlorig.Stmt
}
func NewStmtCache() *StmtCache {
return &StmtCache{
statements: make(map[string]*sqlorig.Stmt),
}
}
func (c *StmtCache) StmtFor(p SqlPreparer, query string) (*sqlorig.Stmt, error) {
stmt, exists := c.getStmt(query)
if !exists {
return c.newStmtFor(p, query)
}
return stmt, nil
}
func (c *StmtCache) newStmtFor(p SqlPreparer, query string) (*sqlorig.Stmt, error) {
stmt, err := p.Prepare(query)
if err != nil {
return nil, err
}
c.storeStmt(query, stmt)
return stmt, nil
}
func (c *StmtCache) storeStmt(query string, stmt *sqlorig.Stmt) {
c.mut.Lock()
defer c.mut.Unlock()
c.statements[query] = stmt
}
func (c *StmtCache) getStmt(query string) (*sqlorig.Stmt, bool) {
c.mut.RLock()
defer c.mut.RUnlock()
stmt, exists := c.statements[query]
return stmt, exists
}
func ShowMasterStatusBinlogPosition(db *sql.DB) (mysql.Position, error) {
rows, err := db.Query("SHOW MASTER STATUS")
if err != nil {
return NewMysqlPosition("", 0, err)
}
defer rows.Close()
var file string
var position uint32
var binlog_do_db, binlog_ignore_db, executed_gtid_set string
var cols []string
if rows.Next() {
cols, err = rows.Columns()
if err != nil {
return NewMysqlPosition(file, position, err)
}
switch len(cols) {
case 4:
err = rows.Scan(&file, &position, &binlog_do_db, &binlog_ignore_db)
default:
err = rows.Scan(&file, &position, &binlog_do_db, &binlog_ignore_db, &executed_gtid_set)
}
}
return NewMysqlPosition(file, position, err)
}
func NewMysqlPosition(file string, position uint32, err error) (mysql.Position, error) {
switch {
case err == sqlorig.ErrNoRows:
return mysql.Position{}, fmt.Errorf("no results from show master status")
case err != nil:
return mysql.Position{}, err
default:
if file == "" {
return mysql.Position{}, fmt.Errorf("show master status does not show a file")
}
return mysql.Position{
Name: file,
Pos: position,
}, nil
}
}
func CheckDbIsAReplica(db *sql.DB) (bool, error) {
row := db.QueryRow("SELECT @@read_only")
var isReadOnly bool
err := row.Scan(&isReadOnly)
return isReadOnly, err
}
func ConvertTableColumnsToStrings(columns []schema.TableColumn) []string {
out := make([]string, 0, len(columns))
for _, column := range columns {
out = append(out, column.Name)
}
return out
}
func QuoteFields(fields []string) (out []string) {
for _, field := range fields {
out = append(out, QuoteField(field))
}
return
}
func QuoteField(field string) string {
return fmt.Sprintf("`%s`", field)
}