-
Notifications
You must be signed in to change notification settings - Fork 441
/
oplog_reader.go
279 lines (236 loc) · 7.66 KB
/
oplog_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
package sourceReader
// read oplog from source mongodb
import (
"context"
"errors"
"fmt"
"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"
"sync"
"time"
conf "github.com/alibaba/MongoShake/v2/collector/configure"
utils "github.com/alibaba/MongoShake/v2/common"
"github.com/alibaba/MongoShake/v2/oplog"
LOG "github.com/vinllen/log4go"
)
const (
QueryTs = "ts"
QueryGid = "g"
QueryOpGT = "$gt"
QueryOpGTE = "$gte"
tailTimeout = 7
localDB = "local"
)
// TimeoutError. mongodb query executed timeout
var TimeoutError = errors.New("read next log timeout, It shouldn't be happen")
var CollectionCappedError = errors.New("collection capped error")
// OplogReader represents stream reader from mongodb that specified
// by an url. And with query options. user can iterate oplogs.
type OplogReader struct {
// source mongo address url
src string
replset string
// mongo oplog reader
conn *utils.MongoCommunityConn
oplogsCursor *mongo.Cursor
// query statement and current max cursor
query bson.M
// oplog channel
oplogChan chan *retOplog
fetcherExist bool
fetcherLock sync.Mutex
firstRead bool
}
// NewOplogReader creates reader with mongodb url
func NewOplogReader(src string, replset string) *OplogReader {
return &OplogReader{
src: src,
replset: replset,
query: bson.M{},
oplogChan: make(chan *retOplog, ChannelSize), // ten times of batchSize
firstRead: true,
}
}
func (or *OplogReader) String() string {
return fmt.Sprintf("oplogReader[src:%s replset:%s]", utils.BlockMongoUrlPassword(or.src, "***"),
or.replset)
}
func (or *OplogReader) Name() string {
return utils.VarIncrSyncMongoFetchMethodOplog
}
// SetQueryTimestampOnEmpty set internal timestamp if
// not exist in this or. initial stage most of the time
func (or *OplogReader) SetQueryTimestampOnEmpty(ts interface{}) {
tsB := ts.(int64)
if _, exist := or.query[QueryTs]; !exist {
LOG.Info("set query timestamp: %v", utils.ExtractTimestampForLog(tsB))
or.UpdateQueryTimestamp(tsB)
}
}
func (or *OplogReader) UpdateQueryTimestamp(ts int64) {
or.query[QueryTs] = bson.M{QueryOpGT: utils.Int64ToTimestamp(ts)}
LOG.Info("update or.query to %v", or.query)
}
func (or *OplogReader) getQueryTimestamp() int64 {
return utils.TimeStampToInt64(or.query[QueryTs].(bson.M)[QueryOpGT].(primitive.Timestamp))
}
// Next returns an oplog by raw bytes which is []byte
func (or *OplogReader) Next() ([]byte, error) {
return or.get()
}
// NextOplog returns an oplog by oplog.GenericOplog struct
func (or *OplogReader) NextOplog() (log *oplog.GenericOplog, err error) {
var raw []byte
if raw, err = or.Next(); err != nil {
return nil, err
}
log = &oplog.GenericOplog{Raw: raw, Parsed: new(oplog.PartialLog)}
bson.Unmarshal(raw, log.Parsed)
return log, nil
}
// internal get next oplog. Used in Next() and NextOplog(). The channel and current function may both return
// timeout which is acceptable.
func (or *OplogReader) get() (log []byte, err error) {
select {
case ret := <-or.oplogChan:
return ret.log, ret.err
case <-time.After(time.Second * time.Duration(conf.Options.IncrSyncReaderBufferTime)):
return nil, TimeoutError
}
}
// start fetcher if not exist
func (or *OplogReader) StartFetcher() {
if or.fetcherExist == true {
return
}
or.fetcherLock.Lock()
if or.fetcherExist == false { // double check
or.fetcherExist = true
go or.fetcher()
}
or.fetcherLock.Unlock()
}
// fetch oplog tp store disk queue or memory
func (or *OplogReader) fetcher() {
LOG.Info("start %s fetcher with src[%v] replica-name[%v] query-ts[%v]",
or.String(), utils.BlockMongoUrlPassword(or.src, "***"), or.replset,
or.query[QueryTs].(bson.M)[QueryOpGT].(primitive.Timestamp))
for {
if err := or.EnsureNetwork(); err != nil {
or.oplogChan <- &retOplog{nil, err}
continue
}
if !or.oplogsCursor.Next(context.Background()) {
if err := or.oplogsCursor.Err(); err != nil {
// some internal error. need rebuild the oplogsCursor
or.releaseCursor()
if utils.IsCollectionCappedError(err) { // print it
LOG.Error("oplog collection capped may happen: %v", err)
or.oplogChan <- &retOplog{nil, CollectionCappedError}
} else {
or.oplogChan <- &retOplog{nil, fmt.Errorf("get next oplog failed, release oplogsIterator, %s", err.Error())}
}
// wait a moment
time.Sleep(1 * time.Second)
} else {
// query timeout
or.oplogChan <- &retOplog{nil, TimeoutError}
}
continue
}
or.oplogChan <- &retOplog{or.oplogsCursor.Current, nil}
}
}
// ensureNetwork establish the mongodb connection at first
// if current connection is not ready or disconnected
func (or *OplogReader) EnsureNetwork() (err error) {
if or.oplogsCursor != nil {
return nil
}
LOG.Info("%s ensure network", or.String())
if or.conn == nil || (or.conn != nil && !or.conn.IsGood()) {
if or.conn != nil {
or.conn.Close()
}
// reconnect
if or.conn, err = utils.NewMongoCommunityConn(or.src, conf.Options.MongoConnectMode, true,
utils.ReadWriteConcernDefault, utils.ReadWriteConcernDefault,
conf.Options.MongoSslRootCaFile); or.conn == nil || err != nil {
err = fmt.Errorf("oplog_reader reconnect mongo instance [%s] error. %s", or.src, err.Error())
return err
}
}
findOptions := options.Find().SetBatchSize(int32(BatchSize)).
SetNoCursorTimeout(true).
SetCursorType(options.Tailable).
SetOplogReplay(true)
var queryTs int64
// the given oplog timestamp shouldn't bigger than the newest
if or.firstRead == true {
// check whether the starting fetching timestamp is less than the newest timestamp exist in the oplog
newestTs := or.getNewestTimestamp()
queryTs = or.getQueryTimestamp()
if newestTs < queryTs {
LOG.Warn("oplog_reader current starting point[%v] is bigger than the newest timestamp[%v]!",
utils.ExtractTimestampForLog(queryTs), utils.ExtractTimestampForLog(newestTs))
queryTs = newestTs
}
}
/*
* the given oplog timestamp shouldn't smaller than the oldest.
* this may happen when collection capped.
*/
oldestTs := or.getOldestTimestamp()
queryTs = or.getQueryTimestamp()
if oldestTs > queryTs {
if !or.firstRead {
return CollectionCappedError
} else {
LOG.Warn("oplog_reader current starting point[%v] is smaller than the oldest timestamp[%v]!",
utils.ExtractTimestampForLog(queryTs), utils.ExtractTimestampForLog(oldestTs))
}
}
or.firstRead = false
or.oplogsCursor, err = or.conn.Client.Database(localDB).Collection(utils.OplogNS).Find(context.Background(),
or.query, findOptions)
if or.oplogsCursor == nil || err != nil {
err = fmt.Errorf("oplog_reader Find mongo instance [%s] error. %s", or.src, err.Error())
LOG.Warn("oplog_reader failed err[%v] or.query[%v]", err, or.query)
return err
}
LOG.Info("%s generates new cursor query[%v]", or.String(), or.query)
return
}
// get newest oplog
func (or *OplogReader) getNewestTimestamp() int64 {
ts, _ := utils.GetNewestTimestampByConn(or.conn)
return ts
}
// get oldest oplog
func (or *OplogReader) getOldestTimestamp() int64 {
ts, _ := utils.GetOldestTimestampByConn(or.conn)
return ts
}
func (or *OplogReader) releaseCursor() {
if or.oplogsCursor != nil {
or.oplogsCursor.Close(context.Background())
}
or.oplogsCursor = nil
}
func (or *OplogReader) FetchNewestTimestamp() (interface{}, error) {
return nil, fmt.Errorf("interface not implement")
}
// GidOplogReader. query along with gid
type GidOplogReader struct {
OplogReader
}
func (reader *GidOplogReader) SetQueryGid(gid string) {
reader.query[QueryGid] = gid
}
func NewGidOplogReader(src string) *GidOplogReader {
return &GidOplogReader{
OplogReader: OplogReader{src: src, query: bson.M{}},
}
}