forked from compose/transporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreader.go
389 lines (337 loc) · 12.2 KB
/
reader.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
package mongodb
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/compose/transporter/client"
"github.com/compose/transporter/commitlog"
"github.com/compose/transporter/log"
"github.com/compose/transporter/message"
"github.com/compose/transporter/message/data"
"github.com/compose/transporter/message/ops"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"gopkg.in/mgo.v2"
)
var (
_ client.Reader = &Reader{}
// DefaultCollectionFilter is an empty map of empty maps
DefaultCollectionFilter = map[string]CollectionFilter{}
)
// CollectionFilter is just a typed map of strings of map[string]interface{}
type CollectionFilter map[string]interface{}
// Reader implements the behavior defined by client.Reader for interfacing with MongoDB.
type Reader struct {
tail bool
collectionFilters map[string]CollectionFilter
oplogTimeout time.Duration
}
func newReader(tail bool, filters map[string]CollectionFilter) client.Reader {
return &Reader{tail, filters, 5 * time.Second}
}
type resultDoc struct {
doc bson.M
c string
}
type iterationComplete struct {
oplogTime primitive.Timestamp
c string
}
func (r *Reader) Read(resumeMap map[string]client.MessageSet, filterFn client.NsFilterFunc) client.MessageChanFunc {
return func(s client.Session, done chan struct{}) (chan client.MessageSet, error) {
out := make(chan client.MessageSet)
client := s.(*Session).client
go func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Hour)
defer cancel()
defer func() {
close(out)
}()
var dbname = s.(*Session).dbname
log.With("db", dbname).Infoln("starting Read func")
collections, err := r.listCollections(ctx, client, dbname, filterFn)
if err != nil {
log.With("db", dbname).Errorf("unable to list collections, %s", err)
return
}
var wg sync.WaitGroup
for _, collectionName := range collections {
var lastID interface{}
oplogTime := timeAsMongoTimestamp(time.Now())
var mode commitlog.Mode // default to Copy
if m, ok := resumeMap[collectionName]; ok {
lastID = m.Msg.Data().Get("_id")
mode = m.Mode
oplogTime = timeAsMongoTimestamp(time.Unix(m.Timestamp, 0))
}
if mode == commitlog.Copy {
if err := r.iterateCollection(r.iterate(lastID, ctx, client, dbname, collectionName), out, done, int64(oplogTime.T)); err != nil {
log.With("db", dbname).Errorln(err)
return
}
log.With("db", dbname).With("collection", collectionName).Infoln("iterating complete")
}
if r.tail {
wg.Add(1)
log.With("collection", collectionName).Infof("oplog start timestamp: %d", oplogTime)
go func(wg *sync.WaitGroup, collectionName string, o primitive.Timestamp) {
defer wg.Done()
// c - collection
errc := r.tailCollection(collectionName, ctx, client, dbname, o, out, done)
for err := range errc {
log.With("db", dbname).With("collection", collectionName).Errorln(err)
return
}
}(&wg, collectionName, oplogTime)
}
}
log.With("db", dbname).Infoln("Read completed")
// this will block if we're tailing
wg.Wait()
return
}()
return out, nil
}
}
func (r *Reader) listCollections(ctx context.Context, client *mongo.Client, dbName string, filterFn func(name string) bool) ([]string, error) {
var colls []string
// Retrieve all collection names first, without MongoDB-side filtering.
collections, err := client.Database(dbName).ListCollectionNames(ctx, bson.D{{}})
if err != nil {
return nil, fmt.Errorf("failed to list collections in '%s' database: %v", dbName, err)
}
for _, c := range collections {
if filterFn(c) && !strings.HasPrefix(c, "system.") {
log.With("db", dbName).With("collection", c).Infoln("adding for iteration...")
colls = append(colls, c)
} else {
log.With("db", dbName).With("collection", c).Infoln("skipping iteration...")
}
}
return colls, nil
}
func (r *Reader) iterateCollection(in <-chan message.Msg, out chan<- client.MessageSet, done chan struct{}, origOplogTime int64) error {
for {
select {
case msg, ok := <-in:
if !ok {
return nil
}
out <- client.MessageSet{
Msg: msg,
Timestamp: origOplogTime,
}
case <-done:
return errors.New("iteration cancelled")
}
}
}
func (r *Reader) iterate(lastID interface{}, ctx context.Context, client *mongo.Client, dbName string, collectionName string) <-chan message.Msg {
msgChan := make(chan message.Msg)
go func() {
defer close(msgChan)
canReissueQuery := r.requeryable(collectionName, ctx, client, dbName)
// Create the query with catQuery, assuming it's adapted to return a cursor and an error
cursor, err := r.catQuery(collectionName, lastID, ctx, client, dbName)
if err != nil {
log.With("database", dbName).With("collection", collectionName).Errorf("error setting up query, %s", err)
return
}
defer cursor.Close(ctx)
for {
if !cursor.Next(ctx) {
if err := cursor.Err(); err != nil {
log.With("database", dbName).With("collection", collectionName).Errorf("error iterating results, %s", err)
if canReissueQuery {
time.Sleep(5 * time.Second)
cursor, err = r.catQuery(collectionName, lastID, ctx, client, dbName) // Attempt to recreate the cursor
if err != nil {
log.With("database", dbName).With("collection", collectionName).Errorf("error reissuing query, %s", err)
return
}
continue
}
return
}
break // Exit loop if no error but iteration is done
}
var result bson.M
if err := cursor.Decode(&result); err != nil {
log.With("database", dbName).With("collection", collectionName).Errorf("error decoding result, %s", err)
return
}
if id, ok := result["_id"]; ok {
lastID = id
}
// Assuming a conversion function exists that properly creates a message.Msg from result
msgChan <- message.From(ops.Insert, collectionName, data.Data(result))
}
}()
return msgChan
}
// catQuery assembles the query for fetching documents from a collection based on the lastID seen
// and any filters applied specifically to that collection. It has been adapted
// for the MongoDB Go Driver, thus it needs a tweak in the return type.
func (r *Reader) catQuery(c string, lastID interface{}, ctx context.Context, client *mongo.Client, dbName string) (*mongo.Cursor, error) {
coll := client.Database(dbName).Collection(c)
query := bson.M{}
// Apply any collection-specific filters if present
if f, ok := r.collectionFilters[c]; ok {
for key, value := range f {
query[key] = value
}
}
// Condition to fetch documents newer than the lastID, if provided
if lastID != nil {
query["_id"] = bson.M{"$gt": lastID}
}
// Executing the find operation with the constructed query and sorting by "_id"
findOptions := options.Find().SetSort(bson.D{{Key: "_id", Value: 1}})
cursor, err := coll.Find(ctx, query, findOptions)
if err != nil {
return nil, err
}
return cursor, nil // Returning the cursor to iterate over the query results
}
func (r *Reader) requeryable(c string, ctx context.Context, client *mongo.Client, dbName string) bool {
coll := client.Database(dbName).Collection(c)
indexesCursor, err := coll.Indexes().List(ctx)
if err != nil {
log.With("database", dbName).With("collection", c).Errorf("unable to list indexes, %s", err)
return false
}
defer indexesCursor.Close(ctx)
var indexesList []bson.M
if err = indexesCursor.All(ctx, &indexesList); err != nil {
log.With("database", dbName).With("collection", c).Errorf("unable to decode indexes list, %s", err)
return false
}
for _, index := range indexesList {
if keys, ok := index["key"].(bson.M); ok {
if _, ok := keys["_id"]; ok {
var result bson.M
err := coll.FindOne(ctx, bson.M{"_id": bson.M{"$exists": true}}, options.FindOne().SetProjection(bson.M{"_id": 1})).Decode(&result)
if err != nil {
log.With("database", dbName).With("collection", c).Errorf("unable to sample document, %s", err)
return false
}
// Assuming a function 'sortable' exists to check the type of _id
if _, ok := result["_id"]; ok {
return true
}
return false
}
}
}
log.With("database", dbName).With("collection", c).Infoln("invalid _id, any issues copying will be aborted")
return false
}
func sortable(id interface{}) bool {
switch id.(type) {
case primitive.ObjectID, string, float64, int64, time.Time:
return true
}
return false
}
func (r *Reader) tailCollection(collectionName string, ctx context.Context, mongoClient *mongo.Client, dbName string, oplogTime primitive.Timestamp, out chan<- client.MessageSet, done chan struct{}) chan error {
errc := make(chan error)
go func() {
defer close(errc)
// Initialize the change stream for a given collection.
pipeline := mongo.Pipeline{{{"$match", bson.D{{"operationType", bson.D{{"$in", []string{"insert", "update", "delete"}}}}}}}}
opts := options.ChangeStream().SetFullDocument(options.UpdateLookup).SetStartAtOperationTime(&oplogTime)
changeStream, err := mongoClient.Database(dbName).Collection(collectionName).Watch(ctx, pipeline, opts)
if err != nil {
log.With("database", dbName).With("collection", collectionName).Errorf("error opening change stream, %s", err)
errc <- err
return
}
defer changeStream.Close(ctx)
for {
select {
case <-ctx.Done():
log.With("database", dbName).With("collection", collectionName).Infoln("context done, stopping tailing...")
return
case <-done:
log.With("database", dbName).With("collection", collectionName).Infoln("done signal received, stopping tailing...")
return
default:
if changeStream.Next(ctx) {
var event bson.M
if err := changeStream.Decode(&event); err != nil {
log.With("database", dbName).With("collection", collectionName).Errorf("error decoding change stream event, %s", err)
continue
}
opType, opFound := event["operationType"].(string)
if !opFound || (opType != "insert" && opType != "update" && opType != "delete") {
// If operation type is not valid, skip this event.
continue
}
// Process valid operation
doc := event["fullDocument"].(bson.M) // For insert and update operations
var op ops.Op
switch opType {
case "insert":
op = ops.Insert
case "update":
op = ops.Update
case "delete":
op = ops.Delete
// For delete operation, you might want to handle it differently,
// as the full document might not be available.
}
msg := message.From(op, collectionName, data.Data(doc)).(*message.Base)
// Construct and send the message set.
out <- client.MessageSet{
Msg: msg,
Timestamp: msg.Timestamp(), // Assuming a conversion method exists from the event to your timestamp format.
Mode: commitlog.Sync, // Mode based on your logic.
}
}
}
}
}()
return errc
}
// getOriginalDoc retrieves the original document from the database.
// transporter has no knowledge of update operations, all updates work as wholesale document replaces
func (r *Reader) getOriginalDoc(doc bson.M, c string, s *mgo.Session) (result bson.M, err error) {
id, exists := doc["_id"]
if !exists {
return result, fmt.Errorf("can't get _id from document")
}
query := bson.M{}
if f, ok := r.collectionFilters[c]; ok {
query = bson.M(f)
}
query["_id"] = id
err = s.DB("").C(c).Find(query).One(&result)
if err != nil {
err = fmt.Errorf("%s.%s %v %v", s.DB("").Name, c, id, err)
}
return
}
// oplogDoc are representations of the mongodb oplog document
// detailed here, among other places. http://www.kchodorow.com/blog/2010/10/12/replication-internals/
type oplogDoc struct {
Ts primitive.Timestamp `bson:"ts"`
H int64 `bson:"h"`
V int `bson:"v"`
Op string `bson:"op"`
Ns string `bson:"ns"`
O bson.M `bson:"o"`
O2 bson.M `bson:"o2"`
}
// validOp checks to see if we're an insert, delete, or update, otherwise the
// document is skilled.
func (o *oplogDoc) validOp(ns string) bool {
return ns == o.Ns && (o.Op == "i" || o.Op == "d" || o.Op == "u")
}
func timeAsMongoTimestamp(t time.Time) primitive.Timestamp {
return primitive.Timestamp{T: uint32(t.Unix()), I: uint32(1)} // Setting the increment (I) as 1 by default
}