Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions internal/oplog/oplog_tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
type chanDataTye []byte

type OplogTail struct {
session *mgo.Session
oplogCollection string
lastOplogEntry *mdbstructs.Oplog
session *mgo.Session
oplogCollection string
lastOplogTimestamp *bson.MongoTimestamp

totalSize int64
docsCount int64
Expand Down Expand Up @@ -114,8 +114,14 @@ func (ot *OplogTail) tail() {
}
result := bson.Raw{}
if iter.Next(&result) {
ot.dataChan <- result.Data
continue
oplog := mdbstructs.OplogTimestampOnly{}
err := result.Unmarshal(&oplog)
if err == nil {
ot.dataChan <- result.Data
ot.lastOplogTimestamp = &oplog.Timestamp
continue
}
iter.Close()
}
if iter.Timeout() {
continue
Expand All @@ -128,8 +134,8 @@ func (ot *OplogTail) tail() {
}

func (ot *OplogTail) getOplogTailTimestamp(col *mgo.Collection) bson.MongoTimestamp {
oplog := &mdbstructs.Oplog{}
err := col.Find(nil).Sort("$natural").Limit(1).One(oplog)
oplog := mdbstructs.OplogTimestampOnly{}
err := col.Find(nil).Sort("$natural").Limit(1).One(&oplog)
if err != nil {
return bson.MongoTimestamp(0)
}
Expand All @@ -138,8 +144,8 @@ func (ot *OplogTail) getOplogTailTimestamp(col *mgo.Collection) bson.MongoTimest

func (ot *OplogTail) tailQuery(col *mgo.Collection) bson.M {
query := bson.M{"op": bson.M{"$ne": mdbstructs.OperationNoop}}
if ot.lastOplogEntry != nil {
query["ts"] = bson.M{"$gt": ot.lastOplogEntry.Timestamp}
if ot.lastOplogTimestamp != nil {
query["ts"] = bson.M{"$gt": *ot.lastOplogTimestamp}
} else {
query["ts"] = bson.M{"$gte": ot.getOplogTailTimestamp(col)}
}
Expand Down
4 changes: 4 additions & 0 deletions mdbstructs/oplog.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ type Oplog struct {
WallTime time.Time `bson:"wall,omitempty"`
}

type OplogTimestampOnly struct {
Timestamp bson.MongoTimestamp `bson:"ts"`
}

type OplogPosition struct {
Timestamp bson.MongoTimestamp
HistoryID int64
Expand Down