forked from zph/moresql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtail.go
439 lines (402 loc) · 11.5 KB
/
tail.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package moresql
import (
"database/sql"
"expvar"
"fmt"
"regexp"
"time"
log "github.com/Sirupsen/logrus"
"github.com/jmoiron/sqlx"
"github.com/orcaman/concurrent-map"
"github.com/paulbellamy/ratecounter"
"github.com/rwynn/gtm"
"github.com/serialx/hashring"
"github.com/thejerf/suture"
"strconv"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// Tailer is the core struct for performing
// Mongo->Pg streaming.
type Tailer struct {
config Config
pg *sqlx.DB
session *mgo.Session
env Env
counters counters
stop chan bool
fan map[string]gtm.OpChan
checkpoint *cmap.ConcurrentMap
}
// Stop is the func necessary to terminate action
// when using Suture library
func (t *Tailer) Stop() {
fmt.Println("Stopping service")
t.stop <- true
}
func (t *Tailer) startOverflowConsumers(c <-chan *gtm.Op) {
for i := 1; i <= workerCountOverflow; i++ {
go t.consumer(strconv.Itoa(i), c, nil)
}
}
type EpochTimestamp int64
func BuildOptionAfterFromTimestamp(timestamp EpochTimestamp, replayDuration time.Duration) (func(*mgo.Session, *gtm.Options) bson.MongoTimestamp, error) {
if timestamp != EpochTimestamp(0) && int64(timestamp) < time.Now().Unix() {
// We have a starting oplog entry
f := func() time.Time { return time.Unix(int64(timestamp), 0) }
return OpTimestampWrapper(f, time.Duration(0)), nil
} else if replayDuration != time.Duration(0) {
return OpTimestampWrapper(bson.Now, replayDuration), nil
} else {
return OpTimestampWrapper(bson.Now, time.Duration(0)), nil
}
return nil, fmt.Errorf("Unable to calculate tailing start time")
}
func (t *Tailer) NewOptions(timestamp EpochTimestamp, replayDuration time.Duration) (*gtm.Options, error) {
options := gtm.DefaultOptions()
after, err := BuildOptionAfterFromTimestamp(timestamp, replayDuration)
if err != nil {
return nil, err
}
epoch, _ := gtm.ParseTimestamp(after(nil, nil))
log.Infof("Starting from epoch: %+v", epoch)
options.After = after
options.BufferSize = 500
options.BufferDuration = time.Duration(500 * time.Millisecond)
options.Ordering = gtm.Document
return options, nil
}
func (t *Tailer) NewFan() map[string]gtm.OpChan {
fan := make(map[string]gtm.OpChan)
// Register Channels
for dbName, db := range t.config {
for collectionName := range db.Collections {
fan[createFanKey(dbName, collectionName)] = make(gtm.OpChan, 1000)
}
}
return fan
}
func consistentBroker(in gtm.OpChan, ring *hashring.HashRing, workerPool map[string]gtm.OpChan) {
for {
select {
case op := <-in:
node, ok := ring.GetNode(fmt.Sprintf("%s", op.Id))
if !ok {
log.Error("Failed at getting worker node from hashring")
} else {
out := workerPool[node]
out <- op
}
}
}
}
func (t *Tailer) startDedicatedConsumers(fan map[string]gtm.OpChan, overflow gtm.OpChan) {
// Reserved workers for individual channels
for k, c := range fan {
workerPool := make(map[string]gtm.OpChan)
var workers [workerCount]int
for i := range workers {
o := make(gtm.OpChan)
workerPool[strconv.Itoa(i)] = o
}
keys := []string{}
for k := range workerPool {
keys = append(keys, k)
}
ring := hashring.New(keys)
wg.Add(1)
go consistentBroker(c, ring, workerPool)
for k, workerChan := range workerPool {
go t.consumer(k, workerChan, overflow)
}
log.WithFields(log.Fields{
"count": workerCount,
"collection": k,
}).Debug("Starting worker(s)")
}
}
type MoresqlMetadata struct {
AppName string `db:"app_name"`
LastEpoch int64 `db:"last_epoch"`
ProcessedAt time.Time `db:"processed_at"`
}
func NewTailer(config Config, pg *sqlx.DB, session *mgo.Session, env Env) *Tailer {
checkpoint := cmap.New()
return &Tailer{config: config, pg: pg, session: session, env: env, stop: make(chan bool), counters: buildCounters(), checkpoint: &checkpoint}
}
func FetchMetadata(checkpoint bool, pg *sqlx.DB, appName string) MoresqlMetadata {
metadata := MoresqlMetadata{}
if checkpoint {
q := Queries{}
err := pg.Get(&metadata, q.GetMetadata(), appName)
// No rows means this is first time with table
if err != nil && err != sql.ErrNoRows {
log.Errorf("Error while reading moresql_metadata table %+v", err)
c := Commands{}
c.CreateTableSQL()
}
} else {
metadata.LastEpoch = 0
}
return metadata
}
type gtmTail struct {
ops gtm.OpChan
errs chan error
}
func (t *Tailer) Read() {
metadata := FetchMetadata(t.env.checkpoint, t.pg, t.env.appName)
var lastEpoch int64
if t.env.replaySecond != 0 {
lastEpoch = int64(t.env.replaySecond)
} else {
lastEpoch = metadata.LastEpoch
}
options, err := t.NewOptions(EpochTimestamp(lastEpoch), t.env.replayDuration)
if err != nil {
log.Fatal(err.Error())
}
ops, errs := gtm.Tail(t.session, options)
g := gtmTail{ops, errs}
log.Info("Tailing mongo oplog")
go func() {
for {
select {
case <-t.stop:
return
case err := <-g.errs:
if matched, _ := regexp.MatchString("i/o timeout", err.Error()); matched {
// Restart gtm.Tail
// Close existing channels to not leak resources
log.Errorf("Problem connecting to mongo initiating reconnection: %s", err.Error())
close(g.ops)
close(g.errs)
latest, ok := t.checkpoint.Get("latest")
if ok && latest != nil {
metadata = latest.(MoresqlMetadata)
lastEpoch = metadata.LastEpoch
options, err := t.NewOptions(EpochTimestamp(lastEpoch), t.env.replayDuration)
if err != nil {
log.Fatal(err.Error())
}
ops, errs = gtm.Tail(t.session, options)
g = gtmTail{ops, errs}
} else {
log.Fatalf("Exiting: Unable to recover from %s", err.Error())
}
} else {
log.Fatalf("Exiting: Mongo tailer returned error %s", err.Error())
}
case op := <-g.ops:
t.counters.read.Incr(1)
log.WithFields(log.Fields{
"operation": op.Operation,
"collection": op.GetCollection(),
"id": op.Id,
}).Debug("Received operation")
// Check if we're watching for the collection
db := op.GetDatabase()
coll := op.GetCollection()
key := createFanKey(db, coll)
if c := t.fan[key]; c != nil {
collection := t.config[db].Collections[coll]
o := Statement{collection}
c <- EnsureOpHasAllFields(op, o.mongoFields())
} else {
t.counters.skipped.Incr(1)
log.Debug("Missing channel for this collection")
}
for k, v := range t.fan {
if len(v) > 0 {
log.Debugf("Channel %s has %d", k, len(v))
}
}
}
}
}()
}
func (t *Tailer) Write() {
t.fan = t.NewFan()
log.WithField("struct", t.fan).Debug("Fan")
overflow := make(gtm.OpChan)
t.startDedicatedConsumers(t.fan, overflow)
t.startOverflowConsumers(overflow)
}
func (t *Tailer) Report() {
c := time.Tick(time.Duration(reportFrequency) * time.Second)
go func() {
for {
select {
case <-c:
t.ReportCounters()
}
}
}()
}
func (t *Tailer) SaveCheckpoint(m MoresqlMetadata) error {
q := Queries{}
result, err := t.pg.NamedExec(q.SaveMetadata(), m)
if err != nil {
log.Errorf("Unable to save into moresql_metadata: %+v, %+v", result, err.Error())
}
return err
}
func (t *Tailer) Checkpoints() {
go func() {
timer := time.Tick(checkpointFrequency)
for {
select {
case _ = <-timer:
latest, ok := t.checkpoint.Get("latest")
if ok && latest != nil {
t.SaveCheckpoint(latest.(MoresqlMetadata))
log.Debug("Saved checkpointing %+v", latest.(MoresqlMetadata))
}
}
}
}()
}
// Serve is the func necessary to start action
// when using Suture library
func (t *Tailer) Serve() {
t.Write()
t.Read()
t.Report()
if t.env.checkpoint {
t.Checkpoints()
}
<-t.stop
}
type counters struct {
insert *ratecounter.RateCounter
update *ratecounter.RateCounter
delete *ratecounter.RateCounter
read *ratecounter.RateCounter
skipped *ratecounter.RateCounter
}
func (c *counters) All() map[string]*ratecounter.RateCounter {
cx := make(map[string]*ratecounter.RateCounter)
cx["insert"] = c.insert
cx["update"] = c.update
cx["delete"] = c.delete
cx["read"] = c.read
cx["skipped"] = c.skipped
return cx
}
func buildCounters() (c counters) {
c = counters{
ratecounter.NewRateCounter(1 * time.Minute),
ratecounter.NewRateCounter(1 * time.Minute),
ratecounter.NewRateCounter(1 * time.Minute),
ratecounter.NewRateCounter(1 * time.Minute),
ratecounter.NewRateCounter(1 * time.Minute),
}
expvar.Publish("insert/min", c.insert)
expvar.Publish("update/min", c.update)
expvar.Publish("delete/min", c.delete)
expvar.Publish("ops/min", c.read)
expvar.Publish("skipped/min", c.skipped)
return
}
func (t *Tailer) ReportCounters() {
for i, counter := range t.counters.All() {
log.Infof("Rate of %s per min: %d", i, counter.Rate())
}
}
func (t *Tailer) MsLag(epoch int32, nowFunc func() time.Time) int64 {
// TODO: use time.Duration instead of this malarky
ts := time.Unix(int64(epoch), 0)
d := nowFunc().Sub(ts)
nanoToMillisecond := func(t time.Duration) int64 { return t.Nanoseconds() / 1e6 }
return nanoToMillisecond(d)
}
func (t *Tailer) consumer(id string, in <-chan *gtm.Op, overflow chan<- *gtm.Op) {
var workerType string
if overflow != nil {
workerType = "Dedicated"
} else {
workerType = "Generic"
}
for {
if overflow != nil && len(in) > workerCount {
// Siphon off overflow
select {
case op := <-in:
overflow <- op
}
continue
}
select {
case op := <-in:
t.processOp(op, workerType)
if t.env.checkpoint {
t.checkpoint.Set("latest", t.OpToMoresqlMetadata(op))
}
}
}
}
func (t *Tailer) OpToMoresqlMetadata(op *gtm.Op) MoresqlMetadata {
ts, _ := gtm.ParseTimestamp(op.Timestamp)
return MoresqlMetadata{AppName: t.env.appName, ProcessedAt: time.Now(), LastEpoch: int64(ts)}
}
func (t *Tailer) processOp(op *gtm.Op, workerType string) {
collectionName := op.GetCollection()
db := op.GetDatabase()
st := FullSyncer{Config: t.config}
o, c := st.statementFromDbCollection(db, collectionName)
ts1, ts2 := gtm.ParseTimestamp(op.Timestamp)
gtmLag := t.MsLag(ts1, time.Now)
logFn := func(s sql.Result, e error) {
log.WithFields(log.Fields{
"ts": ts1,
"ts2": ts2,
"msLag": gtmLag,
"now": time.Now().Unix(),
"action": op.Operation,
"id": op.Id,
"collection": op.GetCollection(),
"error": e,
}).Debug(fmt.Sprintf("%s worker processed", workerType))
}
data := SanitizeData(c.Fields, op)
switch {
case op.IsInsert():
t.counters.insert.Incr(1)
s, err := t.pg.NamedExec(o.BuildUpsert(), data)
logFn(s, err)
case op.IsUpdate():
t.counters.update.Incr(1)
// Note we're using upsert here vs update
// This imposes a performance penalty but is more robust
// in circumstances where an update would fail due to
// record missing in PG
s, err := t.pg.NamedExec(o.BuildUpsert(), data)
logFn(s, err)
case op.IsDelete() && t.env.allowDeletes:
t.counters.delete.Incr(1)
// Deletes have empty op.Data
// We patch in the op.Id instead for consistent data
// Bad idea? Should we always rely on op.ID? instead?
s, err := t.pg.NamedExec(o.BuildDelete(), data)
logFn(s, err)
}
}
func OpTimestampWrapper(f func() time.Time, ago time.Duration) func(*mgo.Session, *gtm.Options) bson.MongoTimestamp {
return func(*mgo.Session, *gtm.Options) bson.MongoTimestamp {
now := f()
inPast := now.Add(-ago)
var c uint32 = 1
ts, err := NewMongoTimestamp(inPast, c)
if err != nil {
log.Error(err)
}
return ts
}
}
func Tail(config Config, pg *sqlx.DB, session *mgo.Session, env Env) {
supervisor := suture.NewSimple("Supervisor")
service := NewTailer(config, pg, session, env)
supervisor.Add(service)
supervisor.ServeBackground()
<-service.stop
}