-
Notifications
You must be signed in to change notification settings - Fork 466
/
mongodb.go
461 lines (379 loc) · 12.8 KB
/
mongodb.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
Copyright 2021 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mongodb
// mongodb package is an implementation of StateStore interface to perform operations on store
import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"strconv"
"time"
"github.com/google/uuid"
"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"
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
"github.com/dapr/components-contrib/metadata"
"github.com/dapr/components-contrib/state"
"github.com/dapr/components-contrib/state/query"
"github.com/dapr/kit/logger"
"github.com/dapr/kit/ptr"
)
const (
host = "host"
username = "username"
password = "password"
databaseName = "databaseName"
collectionName = "collectionName"
server = "server"
operationTimeout = "operationTimeout"
params = "params"
id = "_id"
value = "value"
etag = "_etag"
defaultTimeout = 5 * time.Second
defaultDatabaseName = "daprStore"
defaultCollectionName = "daprCollection"
// mongodb://<username>:<password@<host>/<database><params>
connectionURIFormatWithAuthentication = "mongodb://%s:%s@%s/%s%s"
// mongodb://<host>/<database><params>
connectionURIFormat = "mongodb://%s/%s%s"
// mongodb+srv://<server>/<params>
connectionURIFormatWithSrv = "mongodb+srv://%s/%s"
// mongodb+srv://<username>:<password>@<server>/<params>
connectionURIFormatWithSrvAndCredentials = "mongodb+srv://%s:%s@%s/%s%s" //nolint:gosec
)
// MongoDB is a state store implementation for MongoDB.
type MongoDB struct {
state.DefaultBulkStore
client *mongo.Client
collection *mongo.Collection
operationTimeout time.Duration
metadata mongoDBMetadata
features []state.Feature
logger logger.Logger
}
type mongoDBMetadata struct {
Host string
Username string
Password string
DatabaseName string
CollectionName string
Server string
Writeconcern string
Readconcern string
Params string
OperationTimeout time.Duration
}
// Item is Mongodb document wrapper.
type Item struct {
Key string `bson:"_id"`
Value interface{} `bson:"value"`
Etag string `bson:"_etag"`
}
// NewMongoDB returns a new MongoDB state store.
func NewMongoDB(logger logger.Logger) state.Store {
s := &MongoDB{
features: []state.Feature{state.FeatureETag, state.FeatureTransactional, state.FeatureQueryAPI},
logger: logger,
}
s.DefaultBulkStore = state.NewDefaultBulkStore(s)
return s
}
// Init establishes connection to the store based on the metadata.
func (m *MongoDB) Init(metadata state.Metadata) error {
meta, err := getMongoDBMetaData(metadata)
if err != nil {
return err
}
m.operationTimeout = meta.OperationTimeout
client, err := getMongoDBClient(meta)
if err != nil {
return fmt.Errorf("error in creating mongodb client: %s", err)
}
if err = client.Ping(context.Background(), nil); err != nil {
return fmt.Errorf("error in connecting to mongodb, host: %s error: %s", meta.Host, err)
}
m.client = client
// get the write concern
wc, err := getWriteConcernObject(meta.Writeconcern)
if err != nil {
return fmt.Errorf("error in getting write concern object: %s", err)
}
// get the read concern
rc, err := getReadConcernObject(meta.Readconcern)
if err != nil {
return fmt.Errorf("error in getting read concern object: %s", err)
}
m.metadata = *meta
opts := options.Collection().SetWriteConcern(wc).SetReadConcern(rc)
collection := m.client.Database(meta.DatabaseName).Collection(meta.CollectionName, opts)
m.collection = collection
return nil
}
// Features returns the features available in this state store.
func (m *MongoDB) Features() []state.Feature {
return m.features
}
// Set saves state into MongoDB.
func (m *MongoDB) Set(ctx context.Context, req *state.SetRequest) error {
err := m.setInternal(ctx, req)
if err != nil {
return err
}
return nil
}
func (m *MongoDB) Ping() error {
if err := m.client.Ping(context.Background(), nil); err != nil {
return fmt.Errorf("mongoDB store: error connecting to mongoDB at %s: %s", m.metadata.Host, err)
}
return nil
}
func (m *MongoDB) setInternal(ctx context.Context, req *state.SetRequest) error {
var v interface{}
switch obj := req.Value.(type) {
case []byte:
v = string(obj)
case string:
v = fmt.Sprintf("%q", obj)
default:
v = req.Value
}
// create a document based on request key and value
filter := bson.M{id: req.Key}
if req.ETag != nil {
filter[etag] = *req.ETag
} else if req.Options.Concurrency == state.FirstWrite {
filter[etag] = uuid.NewString()
}
update := bson.M{"$set": bson.M{id: req.Key, value: v, etag: uuid.NewString()}}
_, err := m.collection.UpdateOne(ctx, filter, update, options.Update().SetUpsert(true))
return err
}
// Get retrieves state from MongoDB with a key.
func (m *MongoDB) Get(ctx context.Context, req *state.GetRequest) (*state.GetResponse, error) {
var result Item
filter := bson.M{id: req.Key}
err := m.collection.FindOne(ctx, filter).Decode(&result)
if err != nil {
if err == mongo.ErrNoDocuments {
// Key not found, not an error.
// To behave the same as other state stores in conf tests.
return &state.GetResponse{}, nil
}
return &state.GetResponse{}, err
}
var data []byte
switch obj := result.Value.(type) {
case string:
data = []byte(obj)
case primitive.D:
// Setting canonical to `false`.
// See https://docs.mongodb.com/manual/reference/mongodb-extended-json/#bson-data-types-and-associated-representations
// Having bson marshalled into Relaxed JSON instead of canonical JSON, this way type preservation is lost but
// interoperability is preserved
// See https://mongodb.github.io/swift-bson/docs/current/SwiftBSON/json-interop.html
// A decimal value stored as BSON will be returned as {"d": 5.5} if canonical is set to false instead of
// {"d": {"$numberDouble": 5.5}} when canonical JSON is returned.
if data, err = bson.MarshalExtJSON(obj, false, true); err != nil {
return &state.GetResponse{}, err
}
case primitive.A:
newobj := bson.D{{Key: value, Value: obj}}
if data, err = bson.MarshalExtJSON(newobj, false, true); err != nil {
return &state.GetResponse{}, err
}
var input interface{}
json.Unmarshal(data, &input)
value := input.(map[string]interface{})[value]
if data, err = json.Marshal(value); err != nil {
return &state.GetResponse{}, err
}
default:
if data, err = json.Marshal(result.Value); err != nil {
return &state.GetResponse{}, err
}
}
return &state.GetResponse{
Data: data,
ETag: ptr.Of(result.Etag),
}, nil
}
// Delete performs a delete operation.
func (m *MongoDB) Delete(ctx context.Context, req *state.DeleteRequest) error {
err := m.deleteInternal(ctx, req)
if err != nil {
return err
}
return nil
}
func (m *MongoDB) deleteInternal(ctx context.Context, req *state.DeleteRequest) error {
filter := bson.M{id: req.Key}
if req.ETag != nil {
filter[etag] = *req.ETag
}
result, err := m.collection.DeleteOne(ctx, filter)
if err != nil {
return err
}
if result.DeletedCount == 0 && req.ETag != nil {
return errors.New("key or etag not found")
}
return nil
}
// Multi performs a transactional operation. succeeds only if all operations succeed, and fails if one or more operations fail.
func (m *MongoDB) Multi(ctx context.Context, request *state.TransactionalStateRequest) error {
sess, err := m.client.StartSession()
txnOpts := options.Transaction().SetReadConcern(readconcern.Snapshot()).
SetWriteConcern(writeconcern.New(writeconcern.WMajority()))
defer sess.EndSession(ctx)
if err != nil {
return fmt.Errorf("error in starting the transaction: %s", err)
}
sess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
err = m.doTransaction(sessCtx, request.Operations)
return nil, err
}, txnOpts)
return err
}
func (m *MongoDB) doTransaction(sessCtx mongo.SessionContext, operations []state.TransactionalStateOperation) error {
for _, o := range operations {
var err error
if o.Operation == state.Upsert {
req := o.Request.(state.SetRequest)
err = m.setInternal(sessCtx, &req)
} else if o.Operation == state.Delete {
req := o.Request.(state.DeleteRequest)
err = m.deleteInternal(sessCtx, &req)
}
if err != nil {
sessCtx.AbortTransaction(sessCtx)
return fmt.Errorf("error during transaction, aborting the transaction: %s", err)
}
}
return nil
}
// Query executes a query against store.
func (m *MongoDB) Query(ctx context.Context, req *state.QueryRequest) (*state.QueryResponse, error) {
q := &Query{}
qbuilder := query.NewQueryBuilder(q)
if err := qbuilder.BuildQuery(&req.Query); err != nil {
return &state.QueryResponse{}, err
}
data, token, err := q.execute(ctx, m.collection)
if err != nil {
return &state.QueryResponse{}, err
}
return &state.QueryResponse{
Results: data,
Token: token,
}, nil
}
func getMongoURI(metadata *mongoDBMetadata) string {
if len(metadata.Server) != 0 {
if metadata.Username != "" && metadata.Password != "" {
return fmt.Sprintf(connectionURIFormatWithSrvAndCredentials, metadata.Username, metadata.Password, metadata.Server, metadata.DatabaseName, metadata.Params)
}
return fmt.Sprintf(connectionURIFormatWithSrv, metadata.Server, metadata.Params)
}
if metadata.Username != "" && metadata.Password != "" {
return fmt.Sprintf(connectionURIFormatWithAuthentication, metadata.Username, metadata.Password, metadata.Host, metadata.DatabaseName, metadata.Params)
}
return fmt.Sprintf(connectionURIFormat, metadata.Host, metadata.DatabaseName, metadata.Params)
}
func getMongoDBClient(metadata *mongoDBMetadata) (*mongo.Client, error) {
uri := getMongoURI(metadata)
// Set client options
clientOptions := options.Client().ApplyURI(uri)
// Connect to MongoDB
ctx, cancel := context.WithTimeout(context.Background(), metadata.OperationTimeout)
defer cancel()
daprUserAgent := "dapr-" + logger.DaprVersion
if clientOptions.AppName != nil {
clientOptions.SetAppName(daprUserAgent + ":" + *clientOptions.AppName)
} else {
clientOptions.SetAppName(daprUserAgent)
}
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
return nil, err
}
return client, nil
}
func getMongoDBMetaData(meta state.Metadata) (*mongoDBMetadata, error) {
m := mongoDBMetadata{
DatabaseName: defaultDatabaseName,
CollectionName: defaultCollectionName,
OperationTimeout: defaultTimeout,
}
decodeErr := metadata.DecodeMetadata(meta.Properties, &m)
if decodeErr != nil {
return nil, decodeErr
}
if len(m.Host) == 0 && len(m.Server) == 0 {
return nil, errors.New("must set 'host' or 'server' fields in metadata")
}
if len(m.Host) != 0 && len(m.Server) != 0 {
return nil, errors.New("'host' or 'server' fields are mutually exclusive")
}
var err error
if val, ok := meta.Properties[operationTimeout]; ok && val != "" {
m.OperationTimeout, err = time.ParseDuration(val)
if err != nil {
return nil, errors.New("incorrect operationTimeout field from metadata")
}
}
return &m, nil
}
func getWriteConcernObject(cn string) (*writeconcern.WriteConcern, error) {
var wc *writeconcern.WriteConcern
if cn != "" {
if cn == "majority" {
wc = writeconcern.New(writeconcern.WMajority(), writeconcern.J(true), writeconcern.WTimeout(defaultTimeout))
} else {
w, err := strconv.Atoi(cn)
wc = writeconcern.New(writeconcern.W(w), writeconcern.J(true), writeconcern.WTimeout(defaultTimeout))
return wc, err
}
} else {
wc = writeconcern.New(writeconcern.W(1), writeconcern.J(true), writeconcern.WTimeout(defaultTimeout))
}
return wc, nil
}
func getReadConcernObject(cn string) (*readconcern.ReadConcern, error) {
switch cn {
case "local":
return readconcern.Local(), nil
case "majority":
return readconcern.Majority(), nil
case "available":
return readconcern.Available(), nil
case "linearizable":
return readconcern.Linearizable(), nil
case "snapshot":
return readconcern.Snapshot(), nil
case "":
return readconcern.Local(), nil
}
return nil, fmt.Errorf("readConcern %s not found", cn)
}
func (m *MongoDB) GetComponentMetadata() map[string]string {
metadataStruct := mongoDBMetadata{}
metadataInfo := map[string]string{}
metadata.GetMetadataInfoFromStructType(reflect.TypeOf(metadataStruct), &metadataInfo)
return metadataInfo
}