This repository has been archived by the owner on Jun 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
/
ops_executor.go
202 lines (168 loc) · 4.64 KB
/
ops_executor.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
package flashback
import (
"errors"
"fmt"
"time"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
var (
NotSupported = errors.New("op type not supported")
)
type execute func(op *Op, collection *mgo.Collection) error
type OpsExecutor struct {
session *mgo.Session
statsChan chan OpStat
logger *Logger
// keep track of the results retrieved by find(). For verification purpose
// only.
lastResult interface{}
lastLatency time.Duration
subExecutes map[OpType]execute
}
func NewOpsExecutor(session *mgo.Session, statsChan chan OpStat, logger *Logger) *OpsExecutor {
e := &OpsExecutor{
session: session,
statsChan: statsChan,
logger: logger,
}
e.subExecutes = map[OpType]execute{
Query: e.execQuery,
Insert: e.execInsert,
Update: e.execUpdate,
Remove: e.execRemove,
Count: e.execCount,
FindAndModify: e.execFindAndModify,
GetMore: e.skipGetMore,
}
return e
}
func (e *OpsExecutor) execQuery(op *Op, coll *mgo.Collection) error {
query := coll.Find(op.QueryDoc)
if op.NToSkip != 0 {
query.Skip(int(op.NToSkip))
}
if op.NToReturn != 0 {
query.Limit(int(op.NToReturn))
}
result := []Document{}
err := query.All(&result)
e.lastResult = &result
return err
}
func (e *OpsExecutor) execInsert(op *Op, coll *mgo.Collection) error {
return coll.Insert(op.InsertDoc)
}
func (e *OpsExecutor) execUpdate(op *Op, coll *mgo.Collection) error {
return coll.Update(op.QueryDoc, op.UpdateDoc)
}
func (e *OpsExecutor) execRemove(op *Op, coll *mgo.Collection) error {
return coll.Remove(op.QueryDoc)
}
func (e *OpsExecutor) execCount(op *Op, coll *mgo.Collection) error {
_, err := coll.Count()
return err
}
func (e *OpsExecutor) execFindAndModify(op *Op, coll *mgo.Collection) error {
result := Document{}
var query, update bson.D
// Maybe clean this up later using a struct
if value, ok := GetElem(op.CommandDoc, "query"); ok {
if query, ok = value.(bson.D); !ok {
return fmt.Errorf("bad query document in findAndModify operation")
}
} else {
return fmt.Errorf("missing query document in findAndModify operation")
}
if value, ok := GetElem(op.CommandDoc, "update"); ok {
if update, ok = value.(bson.D); !ok {
return fmt.Errorf("bad update document in findAndModify operation")
}
} else {
return fmt.Errorf("missing update document in findAndModify operation")
}
change := mgo.Change{Update: update}
_, err := coll.Find(query).Apply(change, result)
return err
}
// currently not supported
func (e *OpsExecutor) skipGetMore(op *Op, coll *mgo.Collection) error {
return nil
}
// We only support handful op types. This function helps us to process supported
// ops in a universal way.
//
// We do not canonicalize the ops in OpsReader because we hope ops reader to do
// its job honestly and the consumer of these ops decide how to further process
// the original ops.
func CanonicalizeOp(op *Op) *Op {
if op.Type != Command {
return op
}
// the command to be run is the first element in the command document
// TODO: these unprotected type assertions aren't great, but one problem at at time
cmd := op.CommandDoc[0]
if cmd.Name == "count" || cmd.Name == "findandmodify" {
collName := cmd.Value.(string)
op.Type = OpType("command." + cmd.Name)
op.Collection = collName
return op
}
return nil
}
func retryOnSocketFailure(block func() error, session *mgo.Session, logger *Logger) error {
err := block()
if err == nil {
return nil
}
switch err.(type) {
case *mgo.QueryError, *mgo.LastError:
return err
}
switch err {
case mgo.ErrNotFound, NotSupported:
return err
}
// Otherwise it's probably a socket error so we refresh the connection,
// and try again
session.Refresh()
logger.Error("retrying mongo query after error: ", err)
return block()
}
func (e *OpsExecutor) Execute(op *Op) error {
startOp := time.Now()
op = CanonicalizeOp(op)
block := func() error {
coll := e.session.DB(op.Database).C(op.Collection)
return e.subExecutes[op.Type](op, coll)
}
err := retryOnSocketFailure(block, e.session, e.logger)
latencyOp := time.Now().Sub(startOp)
e.lastLatency = latencyOp
if e.statsChan != nil {
if err == nil {
e.statsChan <- OpStat{op.Type, latencyOp, false}
} else {
// error condition
e.statsChan <- OpStat{op.Type, latencyOp, true}
}
}
return err
}
func (e *OpsExecutor) LastLatency() time.Duration {
return e.lastLatency
}
func safeGetInt(i interface{}) (int, error) {
switch i.(type) {
case int32:
return int(i.(int32)), nil
case int64:
return int(i.(int64)), nil
case float32:
return int(i.(float32)), nil
case float64:
return int(i.(float64)), nil
default:
return int(0), fmt.Errorf("unsupported type for %i", i)
}
}