forked from rwynn/gtm
/
gtm.go
258 lines (231 loc) · 6.35 KB
/
gtm.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 gtm
import (
"fmt"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"strings"
"time"
)
type Options struct {
After TimestampGenerator
Filter OpFilter
OpLogDatabaseName *string
OpLogCollectionName *string
CursorTimeout *string
ChannelSize int
}
type Op struct {
Id interface{} `json:"_id"`
Operation string `json:"operation"`
Namespace string `json:"namespace"`
Data map[string]interface{} `json:"data"`
Timestamp bson.MongoTimestamp `json:"timestamp"`
}
type OpLog struct {
Timestamp bson.MongoTimestamp "ts"
HistoryID int64 "h"
MongoVersion int "v"
Operation string "op"
Namespace string "ns"
Object bson.M "o"
QueryObject bson.M "o2"
}
type OpChan chan *Op
type OpLogEntry map[string]interface{}
type OpFilter func(*Op) bool
type TimestampGenerator func(*mgo.Session, *Options) bson.MongoTimestamp
func ChainOpFilters(filters ...OpFilter) OpFilter {
return func(op *Op) bool {
for _, filter := range filters {
if filter(op) == false {
return false
}
}
return true
}
}
func (this *Op) IsInsert() bool {
return this.Operation == "i"
}
func (this *Op) IsUpdate() bool {
return this.Operation == "u"
}
func (this *Op) IsDelete() bool {
return this.Operation == "d"
}
func (this *Op) ParseNamespace() []string {
return strings.SplitN(this.Namespace, ".", 2)
}
func (this *Op) GetDatabase() string {
return this.ParseNamespace()[0]
}
func (this *Op) GetCollection() string {
return this.ParseNamespace()[1]
}
func (this *Op) FetchData(session *mgo.Session) error {
if this.IsDelete() {
return nil
}
collection := session.DB(this.GetDatabase()).C(this.GetCollection())
doc := make(map[string]interface{})
err := collection.FindId(this.Id).One(doc)
if err == nil {
this.Data = doc
return nil
} else {
return err
}
}
func (this *Op) ParseLogEntry(entry OpLogEntry) {
this.Operation = entry["op"].(string)
this.Timestamp = entry["ts"].(bson.MongoTimestamp)
// only parse inserts, deletes, and updates
if this.IsInsert() || this.IsDelete() || this.IsUpdate() {
var objectField OpLogEntry
if this.IsUpdate() {
objectField = entry["o2"].(OpLogEntry)
} else {
objectField = entry["o"].(OpLogEntry)
}
this.Id = objectField["_id"]
this.Namespace = entry["ns"].(string)
}
}
func OpLogCollectionName(session *mgo.Session, options *Options) string {
localDB := session.DB(*options.OpLogDatabaseName)
col_names, err := localDB.CollectionNames()
if err == nil {
var col_name *string = nil
for _, name := range col_names {
if strings.HasPrefix(name, "oplog.") {
col_name = &name
break
}
}
if col_name == nil {
msg := fmt.Sprintf(`
Unable to find oplog collection
in database %v`, *options.OpLogDatabaseName)
panic(msg)
} else {
return *col_name
}
} else {
msg := fmt.Sprintf(`Unable to get collection names
for database %v`, *options.OpLogDatabaseName)
panic(msg)
}
}
func OpLogCollection(session *mgo.Session, options *Options) *mgo.Collection {
localDB := session.DB(*options.OpLogDatabaseName)
return localDB.C(*options.OpLogCollectionName)
}
func ParseTimestamp(timestamp bson.MongoTimestamp) (int32, int32) {
ordinal := (timestamp << 32) >> 32
ts := (timestamp >> 32)
return int32(ts), int32(ordinal)
}
func LastOpTimestamp(session *mgo.Session, options *Options) bson.MongoTimestamp {
var opLog OpLog
collection := OpLogCollection(session, options)
collection.Find(nil).Sort("-$natural").One(&opLog)
return opLog.Timestamp
}
func GetOpLogQuery(session *mgo.Session, after bson.MongoTimestamp, options *Options) *mgo.Query {
query := bson.M{"ts": bson.M{"$gt": after}}
collection := OpLogCollection(session, options)
return collection.Find(query).LogReplay().Sort("$natural")
}
func FillEmptyOptions(session *mgo.Session, options *Options) {
if options.After == nil {
options.After = LastOpTimestamp
}
if options.OpLogDatabaseName == nil {
defaultOpLogDatabaseName := "local"
options.OpLogDatabaseName = &defaultOpLogDatabaseName
}
if options.OpLogCollectionName == nil {
defaultOpLogCollectionName := OpLogCollectionName(session, options)
options.OpLogCollectionName = &defaultOpLogCollectionName
}
if options.CursorTimeout == nil {
defaultCursorTimeout := "100s"
options.CursorTimeout = &defaultCursorTimeout
}
}
func TailOps(session *mgo.Session, channel OpChan, errChan chan error, options *Options) error {
s := session.Copy()
defer s.Close()
FillEmptyOptions(session, options)
duration, err := time.ParseDuration(*options.CursorTimeout)
if err != nil {
panic("Invalid value for CursorTimeout")
}
currTimestamp := options.After(s, options)
iter := GetOpLogQuery(s, currTimestamp, options).Tail(duration)
for {
entry := make(OpLogEntry)
for iter.Next(entry) {
op := &Op{"", "", "", nil, bson.MongoTimestamp(0)}
op.ParseLogEntry(entry)
if op.Id != "" {
if options.Filter == nil || options.Filter(op) {
channel <- op
}
}
currTimestamp = op.Timestamp
}
if err = iter.Close(); err != nil {
errChan <- err
return err
}
if iter.Timeout() {
continue
}
iter = GetOpLogQuery(s, currTimestamp, options).Tail(duration)
}
return nil
}
func FetchDocuments(session *mgo.Session, inOp OpChan, inErr chan error,
outOp OpChan, outErr chan error) error {
s := session.Copy()
defer s.Close()
for {
select {
case err := <-inErr:
outErr <- err
case op := <-inOp:
fetchErr := op.FetchData(s)
if fetchErr == nil || fetchErr == mgo.ErrNotFound {
outOp <- op
} else {
outErr <- fetchErr
}
}
}
return nil
}
func DefaultOptions() *Options {
return &Options{
After: nil,
Filter: nil,
OpLogDatabaseName: nil,
OpLogCollectionName: nil,
CursorTimeout: nil,
ChannelSize: 20,
}
}
func Tail(session *mgo.Session, options *Options) (OpChan, chan error) {
if options == nil {
options = DefaultOptions()
} else if options.ChannelSize < 1 {
options.ChannelSize = 20
}
inErr := make(chan error, options.ChannelSize)
outErr := make(chan error, options.ChannelSize)
inOp := make(OpChan, options.ChannelSize)
outOp := make(OpChan, options.ChannelSize)
go FetchDocuments(session, inOp, inErr, outOp, outErr)
go TailOps(session, inOp, inErr, options)
return outOp, outErr
}