-
Notifications
You must be signed in to change notification settings - Fork 2
/
benchmark-insert.go
363 lines (343 loc) · 9.58 KB
/
benchmark-insert.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
package main
import (
"flag"
"fmt"
"time"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"gopkg.in/mgo.v2/txn"
)
var (
URL = flag.String("mongo", "localhost", "URL for dialing mongo")
Bulk = flag.Bool("bulk", false, "use a single bulk call")
Count = flag.Int("count", 1000, "number to insert")
MaxRetry = flag.Int("max-retry", 3, "retry an operation at most this many times")
Start = flag.Int("start", 0, "starting offset")
Unsafe = flag.Bool("unsafe", false, "set safe mode to non-blocking and not-checking errors")
Sync = flag.Bool("sync", false, "Force an fsync for writes to be considered committed (ignored if Unsafe is set)")
ClientTrans = flag.Bool("client-trans", false, "Use client-side Mongo Transactions (mgo/txn) to update the database.")
OneTrans = flag.Bool("one-trans", false, "Use beginTransaction/commitTransaction across all inserts.")
EachTrans = flag.Bool("each-trans", false, "Use beginTransaction/commitTransaction across each single call (bulk or one-by-one each gets a separate transaction).")
Verify = flag.Bool("verify", false, "After inserting, verify all documents are correct and new docs are inserted.")
Verbose = flag.Bool("verbose", false, "Print more information.")
)
var RetryCount = 0
const ( // error codes
fdbOperationFailed = 1000
// These are all codes that "should be retried"
fdbTimedOut = 1004
fdbPastVersion = 1007
fdbFutureVersion = 1009
fdbNotCommitted = 1020
// Toku Error Codes that can be retried
tokuLockNotGranted = 16759
)
var retryCodes = map[int]bool{
fdbTimedOut: true,
fdbPastVersion: true,
fdbFutureVersion: true,
fdbNotCommitted: true,
tokuLockNotGranted: true,
}
func isCodeRetry(code int) bool {
return retryCodes[code]
}
func shouldRetry(err error) bool {
if err == nil {
return false
}
if mgoErr, ok := err.(*mgo.LastError); ok {
fmt.Printf("LastError: %#v\n", mgoErr)
return isCodeRetry(mgoErr.Code)
}
if mgoErr, ok := err.(*mgo.QueryError); ok {
fmt.Printf("QueryError: %#v\n", mgoErr)
return isCodeRetry(mgoErr.Code)
}
return false
}
func transactionCommand(db *mgo.Database, cmd interface{}) error {
var result bson.M
err := db.Run(cmd, &result)
if err != nil {
return err
} else if result["ok"].(float64) != 1 {
return fmt.Errorf("%s did not return ok=1: %v\n", cmd, result)
}
return nil
}
func maybeBegin(db *mgo.Database, cond bool, retries *int) error {
//maybeBegin takes 'retries' because we know no other work has been
//done that we might want to clean up. so we just retry this specific
//operation.
if cond {
// FoundationDB has a built-in 5s timeout for find() operations
// operating inside of a transaction. So just set the max
// timeout to 5s so we don't think we can get away with it when
// running in single insert mode.
// We set it to 6s so that we can see past_version failures
// rather than transaction_timeout ones.
for {
if err := transactionCommand(db, map[string]interface{}{"beginTransaction": 1, "timeout": 6000}); err != nil {
fmt.Printf("failed to beginTransaction: %s\n", err)
if shouldRetry(err) {
if retries != nil && *retries < *MaxRetry {
*retries++
RetryCount++
continue
}
}
return err
}
return nil
}
}
return nil
}
func maybeCommit(db *mgo.Database, cond bool) error {
if cond {
if err := transactionCommand(db, "commitTransaction"); err != nil {
fmt.Printf("failed to commitTransaction: %s\n", err)
return err
}
}
return nil
}
func maybeRollback(db *mgo.Database, cond bool) error {
if cond {
if err := transactionCommand(db, "rollbackTransaction"); err != nil {
// error is not printed, because we are probably already failing
// fmt.Printf("failed to commitTransaction: %s\n", err)
return err
}
}
return nil
}
func oneByOneDirect(db *mgo.Database) error {
collection := db.C("foo")
var retries int = 0
if err := maybeBegin(db, *OneTrans, &retries); err != nil {
return err
}
for i := 0; i < *Count; i++ {
val := *Start + i
strVal := fmt.Sprintf("%07d", val)
// reset retries per document
for retries = 0; retries < *MaxRetry; retries++ {
if err := maybeBegin(db, *EachTrans, &retries); err != nil {
return err
}
if err := collection.Insert(bson.M{"_id": strVal, "val": strVal}); err != nil {
fmt.Printf("could not insert doc %d\n%s\n", val, err)
// ignore the error because we are returning anyway
maybeRollback(db, *OneTrans || *EachTrans)
if shouldRetry(err) {
RetryCount += 1
continue
}
return err
}
if err := maybeCommit(db, *EachTrans); err != nil {
if shouldRetry(err) {
RetryCount += 1
continue
}
return err
} else {
break
}
}
}
return maybeCommit(db, *OneTrans)
}
func bulkDirect(db *mgo.Database) error {
collection := db.C("foo")
var retries int = 0
docs := make([]interface{}, 0, *Count)
for i := 0; i < *Count; i++ {
val := *Start + i
strVal := fmt.Sprintf("%07d", val)
docs = append(docs, bson.M{"_id": strVal, "val": strVal})
}
if *Verbose {
fmt.Printf("First doc: %v\n", docs[0])
}
for ; retries < *MaxRetry; retries++ {
if err := maybeBegin(db, *OneTrans || *EachTrans, &retries); err != nil {
return err
}
if err := collection.Insert(docs...); err != nil {
fmt.Printf("could not insert all docs\n%s\n", err.Error())
// ignore the error because we are returning anyway
maybeRollback(db, *OneTrans || *EachTrans)
if shouldRetry(err) {
RetryCount += 1
continue
}
return err
}
if err := maybeCommit(db, *OneTrans || *EachTrans); err != nil {
if shouldRetry(err) {
RetryCount += 1
continue
}
return err
} else {
break
}
}
return nil
}
func oneDocOp(val int) txn.Op {
strVal := fmt.Sprintf("%07d", val)
return txn.Op{
C: "foo",
Id: strVal,
Assert: txn.DocMissing,
Insert: bson.M{"_id": strVal, "val": strVal},
}
}
func oneByOneClientTrans(db *mgo.Database) error {
txnCollection := db.C("transactions")
var retries int = 0
if err := maybeBegin(db, *OneTrans, &retries); err != nil {
return err
}
for i := 0; i < *Count; i++ {
val := *Start + i
// Reset retries per document
for retries = 0; retries < *MaxRetry; retries++ {
if err := maybeBegin(db, *EachTrans, &retries); err != nil {
return err
}
runner := txn.NewRunner(txnCollection)
op := oneDocOp(val)
txnId := bson.NewObjectId()
if err := runner.Run([]txn.Op{op}, txnId, nil); err != nil {
fmt.Printf("could not insert %d\n%s\n", val, err)
// ignore the error because we are returning anyway
maybeRollback(db, *OneTrans || *EachTrans)
if shouldRetry(err) {
RetryCount += 1
continue
}
return err
}
if err := maybeCommit(db, *EachTrans); err != nil {
if shouldRetry(err) {
RetryCount += 1
continue
}
return err
} else {
break
}
}
}
// ignore the error because we are returning anyway
return maybeCommit(db, *OneTrans)
}
func bulkClientTrans(db *mgo.Database) error {
txnCollection := db.C("transactions")
var retries int = 0
ops := make([]txn.Op, 0, *Count)
for i := 0; i < *Count; i++ {
ops = append(ops, oneDocOp(*Start+i))
}
for ; retries < *MaxRetry; retries++ {
if err := maybeBegin(db, *OneTrans || *EachTrans, &retries); err != nil {
return err
}
runner := txn.NewRunner(txnCollection)
txnId := bson.NewObjectId()
if err := runner.Run(ops, txnId, nil); err != nil {
fmt.Printf("could not insert all\n%s\n", err)
// ignore the error because we are returning anyway
maybeRollback(db, *OneTrans || *EachTrans)
if shouldRetry(err) {
RetryCount += 1
continue
}
return err
}
// ignore the error because we are returning anyway
if err := maybeCommit(db, *OneTrans || *EachTrans); err != nil {
if shouldRetry(err) {
RetryCount += 1
continue
}
return err
} else {
break
}
}
return nil
}
type ValDoc struct {
id string `bson:"_id"`
val string `bson:"val"`
}
func verifyNewDocs(db *mgo.Database) error {
collection := db.C("foo")
for i := 0; i < *Count; i++ {
val := *Start + i
var result ValDoc
strVal := fmt.Sprintf("%07d", val)
if err := collection.FindId(strVal).One(&result); err != nil {
return fmt.Errorf("could not find document %d\n", val)
}
if result.id != result.val {
return fmt.Errorf("document %s != %s", result.id, result.val)
}
}
return nil
}
func main() {
flag.Parse()
session, err := mgo.DialWithTimeout(*URL, 10*time.Second)
if err != nil {
panic(err)
}
if *Unsafe {
session.SetSafe(nil)
} else if *Sync {
// Turns out you can't set WMode: "majority" if you aren't in a replica set...
session.SetSafe(&mgo.Safe{FSync: true})
} else {
session.SetSafe(&mgo.Safe{FSync: false})
}
if *OneTrans && *EachTrans {
fmt.Printf("--one-trans and --each-trans are mutually exclusive\n")
return
}
db := session.DB("test")
start := time.Now()
if *Verbose {
fmt.Printf("inserting docs from %d to %d\n", *Start, *Start+*Count-1)
}
if *Bulk {
if *ClientTrans {
err = bulkClientTrans(db)
} else {
err = bulkDirect(db)
}
} else {
if *ClientTrans {
err = oneByOneClientTrans(db)
} else {
err = oneByOneDirect(db)
}
}
if err != nil {
fmt.Printf("%.3fms to insert %d documents (%d retries, FAILED: %s)\n", float64(time.Since(start))/float64(time.Millisecond), *Count, RetryCount, err)
} else {
fmt.Printf("%.3fms to insert %d documents (%d retries)\n", float64(time.Since(start))/float64(time.Millisecond), *Count, RetryCount)
}
if *Verify {
if err := verifyNewDocs(db); err != nil {
fmt.Printf("verification failed: %s\n", err.Error())
return
}
fmt.Printf("verification succeeded.\n")
}
}