forked from facebookarchive/flashback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ops_reader.go
286 lines (241 loc) · 6.33 KB
/
ops_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
package flashback
import (
"errors"
"io"
"os"
"strings"
"time"
"gopkg.in/mgo.v2/bson"
"github.com/mongodb/mongo-tools/common/db"
)
// OpsReader Reads the ops from a source and present a interface for consumers
// to fetch these ops sequentially.
type OpsReader interface {
// Move to next op and return it. Nil will be returned if the last ops had
// already been read, or there is any error occurred.
// TODO change from Document to Op
Next() *Op
// Allow skipping the first N ops in the source file
SkipOps(int) error
// Start at a specific time in the set of ops
// Return an error if we get to EOF without finding an op
// Can be used with SkipOps, but you should call SkipOps after SetStartTime
SetStartTime(int64) (int64, error)
// How many ops are read so far
OpsRead() int
// Have all the ops been read?
AllLoaded() bool
// indicate the latest error occurs when reading ops.
Err() error
Close()
}
// ByLineOpsReader reads ops from a json file that is exported from python's
// json_util module, where each line is a json-represented op.
//
// Note: After parse each json-represented op, we need perform post-process to
// convert some "metadata" into MongoDB specific data structures, like "Object
// Id" and datetime.
type ByLineOpsReader struct {
err error
opsRead int
closeFunc func()
logger *Logger
opFilters []OpType
src *db.DecodedBSONSource
}
func NewByLineOpsReader(reader io.ReadCloser, logger *Logger, opFilter string) (error, *ByLineOpsReader) {
opFilters := make([]OpType, 0)
if opFilter != "" {
filterList := strings.Split(opFilter, ",")
for _, filter := range filterList {
opFilters = append(opFilters, OpType(filter))
}
}
return nil, &ByLineOpsReader{
src: db.NewDecodedBSONSource(db.NewBSONSource(reader)),
err: nil,
opsRead: 0,
logger: logger,
opFilters: opFilters,
}
}
func NewFileByLineOpsReader(filename string, logger *Logger, opFilter string) (error, *ByLineOpsReader) {
file, err := os.Open(filename)
if err != nil {
return err, nil
}
err, reader := NewByLineOpsReader(file, logger, opFilter)
if err != nil {
return err, reader
}
reader.closeFunc = func() {
file.Close()
}
return nil, reader
}
func (r *ByLineOpsReader) SkipOps(numSkipOps int) error {
var op Op
for numSkipped := 0; numSkipped < numSkipOps; numSkipped++ {
if ok := r.src.Next(&op); !ok {
return r.src.Err()
}
}
r.logger.Infof("Done skipping %d ops.\n", numSkipOps)
return nil
}
func (r *ByLineOpsReader) SetStartTime(startTime int64) (int64, error) {
var numSkipped int64
searchTime := time.Unix(startTime/1000, startTime%1000*1000000)
var op Op
for {
// The nature of this function is that it will discard the first op
if ok := r.src.Next(&op); !ok {
return numSkipped, r.src.Err()
}
numSkipped++
if op.Timestamp.After(searchTime) || op.Timestamp.Equal(searchTime) {
actualTime := op.Timestamp
r.logger.Infof("Skipped %d ops to begin at timestamp %d.", numSkipped, actualTime)
return numSkipped, nil
}
}
return numSkipped, errors.New("no ops found after specified start_time")
}
func (r *ByLineOpsReader) Next() *Op {
// we may need to skip certain type of ops
var op Op
for {
if ok := r.src.Next(&op); !ok {
return nil
}
r.opsRead++
// filter out unwanted ops
if shouldFilterOp(&op, r.opFilters) {
continue
}
normalizeOp(&op)
// Clean up empty keys on specific ops
emptyKeysToPrune := []string{"$set", "$unset"}
switch op.Type {
case Command:
if op.CommandDoc[0].Name == "findandmodify" {
for i := range op.CommandDoc {
if op.CommandDoc[i].Name == "update" {
if updateDoc, ok := op.CommandDoc[i].Value.(bson.D); ok {
updateDoc = pruneEmptyKeys(updateDoc, emptyKeysToPrune)
op.CommandDoc[i].Value = updateDoc
}
}
}
}
case Update:
op.UpdateDoc = pruneEmptyKeys(op.UpdateDoc, emptyKeysToPrune)
}
return &op
}
}
func (r *ByLineOpsReader) OpsRead() int {
return r.opsRead
}
func (r *ByLineOpsReader) AllLoaded() bool {
return r.err == io.EOF
}
func (r *ByLineOpsReader) Err() error {
return r.err
}
func (r *ByLineOpsReader) Close() {
if r.closeFunc != nil {
r.closeFunc()
}
}
func shouldFilterOp(op *Op, filters []OpType) bool {
for _, opFilter := range filters {
if op.Type == opFilter {
return true
}
}
return false
}
func normalizeOp(op *Op) {
// populate db and collection name
parts := strings.SplitN(op.Ns, ".", 2)
dbName, collName := parts[0], parts[1]
op.Database = dbName
op.Collection = collName
}
type CyclicOpsReader struct {
maker func() OpsReader
reader OpsReader
previousRead int
err error
logger *Logger
}
func NewCyclicOpsReader(maker func() OpsReader, logger *Logger) *CyclicOpsReader {
reader := maker()
if reader == nil {
return nil
}
return &CyclicOpsReader{
maker,
reader,
0,
nil,
logger,
}
}
func (c *CyclicOpsReader) Next() *Op {
var op *Op = nil
if op = c.reader.Next(); op == nil {
c.logger.Info("Recycle starts")
c.previousRead += c.reader.OpsRead()
c.reader.Close()
c.reader = c.maker()
op = c.reader.Next()
}
if op == nil {
c.err = errors.New("The underlying ops reader is empty or invalid")
}
return op
}
func (c *CyclicOpsReader) OpsRead() int {
return c.reader.OpsRead() + c.previousRead
}
func (c *CyclicOpsReader) AllLoaded() bool {
return false
}
func (c *CyclicOpsReader) SkipOps(numSkipOps int) error {
return c.reader.SkipOps(numSkipOps)
}
func (c *CyclicOpsReader) SetStartTime(startTime int64) (int64, error) {
return c.reader.SetStartTime(startTime)
}
func (c *CyclicOpsReader) Err() error {
if c.err != nil {
return c.err
}
return c.reader.Err()
}
func (c *CyclicOpsReader) Close() {
c.reader.Close()
}
// Some operations are recorded with empty values for $set, $unset
// When these are replayed against a mongo instance, they generate an error and do not execute
// This method will detect and remove these empty blocks before the query is executed
func pruneEmptyKeys(doc bson.D, keys []string) bson.D {
keyMap := map[string]struct{}{}
for _, key := range keys {
keyMap[key] = struct{}{}
}
prunedDoc := bson.D{}
for _, elem := range doc {
if _, ok := keyMap[elem.Name]; ok {
opValue := elem.Value.(bson.D)
if len(opValue) > 0 {
prunedDoc = append(prunedDoc, elem)
}
} else {
prunedDoc = append(prunedDoc, elem)
}
}
return prunedDoc
}