/
cosmosdb.go
731 lines (643 loc) · 20 KB
/
cosmosdb.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
/*
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 cosmosdb
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"reflect"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
"github.com/google/uuid"
jsoniter "github.com/json-iterator/go"
"github.com/dapr/components-contrib/common/authentication/azure"
"github.com/dapr/components-contrib/contenttype"
contribmeta "github.com/dapr/components-contrib/metadata"
"github.com/dapr/components-contrib/state"
"github.com/dapr/components-contrib/state/query"
stateutils "github.com/dapr/components-contrib/state/utils"
"github.com/dapr/kit/logger"
kitmd "github.com/dapr/kit/metadata"
"github.com/dapr/kit/ptr"
)
// StateStore is a CosmosDB state store.
type StateStore struct {
state.BulkStore
client *azcosmos.ContainerClient
metadata metadata
contentType string
logger logger.Logger
}
type metadata struct {
URL string `json:"url"`
MasterKey string `json:"masterKey"`
Database string `json:"database"`
Collection string `json:"collection"`
ContentType string `json:"contentType"`
}
type cosmosOperationType string
// CosmosOperation is a wrapper around a CosmosDB operation.
type CosmosOperation struct {
Item CosmosItem `json:"item"`
Type cosmosOperationType `json:"type"`
}
// CosmosItem is a wrapper around a CosmosDB document.
type CosmosItem struct {
ID string `json:"id"`
Value interface{} `json:"value"`
IsBinary bool `json:"isBinary"`
PartitionKey string `json:"partitionKey"`
TTL *int `json:"ttl,omitempty"`
Etag string `json:"_etag"`
TS int64 `json:"_ts"`
}
const (
metadataPartitionKey = "partitionKey"
defaultTimeout = 20 * time.Second
)
// Policy that makes all queries cross-partition
type crossPartitionQueryPolicy struct{}
func (p crossPartitionQueryPolicy) Do(req *policy.Request) (*http.Response, error) {
raw := req.Raw()
// Check if we're performing a query
// In that case, remove the partitionkey header and enable cross-partition queries
if strings.ToLower(raw.Header.Get("x-ms-documentdb-query")) == "true" {
// Only when the partitionKey is fake (true), it will be removed amd enabled the cross partition
if strings.ToLower(raw.Header.Get("x-ms-documentdb-partitionkey")) == "[true]" {
raw.Header.Add("x-ms-documentdb-query-enablecrosspartition", "true")
raw.Header.Del("x-ms-documentdb-partitionkey")
}
}
return req.Next()
}
// NewCosmosDBStateStore returns a new CosmosDB state store.
func NewCosmosDBStateStore(logger logger.Logger) state.Store {
s := &StateStore{
logger: logger,
}
s.BulkStore = state.NewDefaultBulkStore(s)
return s
}
func (c *StateStore) GetComponentMetadata() (metadataInfo contribmeta.MetadataMap) {
metadataStruct := metadata{}
contribmeta.GetMetadataInfoFromStructType(reflect.TypeOf(metadataStruct), &metadataInfo, contribmeta.StateStoreType)
return
}
// Init does metadata and connection parsing.
func (c *StateStore) Init(ctx context.Context, meta state.Metadata) error {
c.logger.Debugf("CosmosDB init start")
m := metadata{
ContentType: "application/json",
}
errDecode := kitmd.DecodeMetadata(meta.Properties, &m)
if errDecode != nil {
return errDecode
}
if m.URL == "" {
return errors.New("url is required")
}
if m.Database == "" {
return errors.New("database is required")
}
if m.Collection == "" {
return errors.New("collection is required")
}
if m.ContentType == "" {
return errors.New("contentType is required")
}
// Internal query policy was created due to lack of cross partition query capability in the current Go sdk
opts := azcosmos.ClientOptions{
ClientOptions: policy.ClientOptions{
PerCallPolicies: []policy.Policy{
crossPartitionQueryPolicy{},
},
Telemetry: policy.TelemetryOptions{
ApplicationID: "dapr-" + logger.DaprVersion,
},
},
}
// Create the client; first, try authenticating with a master key, if present
var client *azcosmos.Client
if m.MasterKey != "" {
var cred azcosmos.KeyCredential
cred, err := azcosmos.NewKeyCredential(m.MasterKey)
if err != nil {
return err
}
client, err = azcosmos.NewClientWithKey(m.URL, cred, &opts)
if err != nil {
return err
}
} else {
// Fallback to using Azure AD
var env azure.EnvironmentSettings
env, err := azure.NewEnvironmentSettings(meta.Properties)
if err != nil {
return err
}
token, tokenErr := env.GetTokenCredential()
if tokenErr != nil {
return tokenErr
}
client, err = azcosmos.NewClient(m.URL, token, &opts)
if err != nil {
return err
}
}
// Create a container client
dbClient, err := client.NewDatabase(m.Database)
if err != nil {
return err
}
// Container is synonymous with collection.
dbContainer, err := dbClient.NewContainer(m.Collection)
if err != nil {
return err
}
c.client = dbContainer
c.metadata = m
c.contentType = m.ContentType
readCtx, cancel := context.WithTimeout(ctx, defaultTimeout)
defer cancel()
_, err = c.client.Read(readCtx, nil)
return err
}
// Features returns the features available in this state store.
func (c *StateStore) Features() []state.Feature {
return []state.Feature{
state.FeatureETag,
state.FeatureTransactional,
state.FeatureQueryAPI,
state.FeatureTTL,
state.FeaturePartitionKey,
}
}
// Get retrieves a CosmosDB item.
func (c *StateStore) Get(ctx context.Context, req *state.GetRequest) (*state.GetResponse, error) {
partitionKey := populatePartitionMetadata(req.Key, req.Metadata)
options := azcosmos.ItemOptions{}
if req.Options.Consistency == state.Strong {
options.ConsistencyLevel = azcosmos.ConsistencyLevelSession.ToPtr()
} else if req.Options.Consistency == state.Eventual {
options.ConsistencyLevel = azcosmos.ConsistencyLevelEventual.ToPtr()
}
readCtx, cancel := context.WithTimeout(ctx, defaultTimeout)
defer cancel()
readItem, err := c.client.ReadItem(readCtx, azcosmos.NewPartitionKeyString(partitionKey), req.Key, &options)
if err != nil {
if isNotFoundError(err) {
return &state.GetResponse{}, nil
}
return nil, err
}
item, err := NewCosmosItemFromResponse(readItem.Value, c.logger)
if err != nil {
return nil, err
}
var metadata map[string]string
if item.TTL != nil {
metadata = map[string]string{
state.GetRespMetaKeyTTLExpireTime: time.Unix(item.TS+int64(*item.TTL), 0).UTC().Format(time.RFC3339),
}
}
// We are sure this is a []byte if not nil
b, _ := item.Value.([]byte)
return &state.GetResponse{
Data: b,
ETag: ptr.Of(item.Etag),
Metadata: metadata,
}, nil
}
// getMulti retrieves multiple items with a cross-partition query, retrieving multiple records with a single query.
func (c *StateStore) getMulti(ctx context.Context, req []state.GetRequest) ([]state.BulkGetResponse, error) {
// The partition key doesn't matter since it will be removed for a cross-partition query
pk := azcosmos.NewPartitionKeyBool(true)
// Build the query
var consistency azcosmos.ConsistencyLevel
keys := make([]string, len(req))
for i, r := range req {
keys[i] = r.Key
// Use the specified consistency if empty
// If there's a strong consistency, it overrides any eventual
if r.Options.Consistency == state.Strong {
consistency = azcosmos.ConsistencyLevelStrong
} else if r.Options.Consistency == state.Eventual && consistency == "" {
consistency = azcosmos.ConsistencyLevelEventual
}
}
// Execute the query
// Source for "ARRAY_CONTAINS": https://stackoverflow.com/a/44639998/192024
queryOpts := &azcosmos.QueryOptions{
QueryParameters: []azcosmos.QueryParameter{
{Name: "@keys", Value: keys},
},
}
if consistency != "" {
queryOpts.ConsistencyLevel = &consistency
}
pager := c.client.NewQueryItemsPager(
"SELECT * FROM r WHERE ARRAY_CONTAINS(@keys, r.id)",
pk, queryOpts,
)
result := make([]state.BulkGetResponse, len(req))
n := 0
for pager.More() {
queryResponse, err := pager.NextPage(ctx)
if err != nil {
return nil, err
}
for _, value := range queryResponse.Items {
item, err := NewCosmosItemFromResponse(value, c.logger)
if err != nil {
// If the error was while unserializing JSON, we don't have an ID, so exit right away
// This should never happen, hopefully
if item.ID == "" {
return nil, err
}
result[n] = state.BulkGetResponse{
Key: item.ID,
Error: err.Error(),
}
n++
continue
}
var metadata map[string]string
if item.TTL != nil {
metadata = map[string]string{
state.GetRespMetaKeyTTLExpireTime: time.Unix(item.TS+int64(*item.TTL), 0).UTC().Format(time.RFC3339),
}
}
// We are sure this is a []byte if not nil
b, _ := item.Value.([]byte)
result[n] = state.BulkGetResponse{
Key: item.ID,
Data: b,
ETag: &item.Etag,
Metadata: metadata,
}
n++
}
}
return result[:n], nil
}
// BulkGet performs a Get operation in bulk.
func (c *StateStore) BulkGet(ctx context.Context, req []state.GetRequest, _ state.BulkGetOpts) ([]state.BulkGetResponse, error) {
if len(req) == 0 {
return []state.BulkGetResponse{}, nil
}
// If we have a single request, execute it as a regular Get request which is more efficient
if len(req) == 1 {
item, err := c.Get(ctx, &req[0])
res := state.BulkGetResponse{
Key: req[0].Key,
}
if err != nil {
res.Error = err.Error()
} else if item != nil {
res.Data = json.RawMessage(item.Data)
res.ETag = item.ETag
res.Metadata = item.Metadata
}
return []state.BulkGetResponse{res}, nil
}
// Execute a single query to retrieve all values, as a cross-partition query
items, err := c.getMulti(ctx, req)
if err != nil {
return nil, err
}
// Save all results
// For consistency with when using Get, we need to include keys that are missing and set an empty value
if len(items) > len(req) {
return nil, errors.New("received more results than expected")
}
result := make([]state.BulkGetResponse, len(req))
foundKeys := make(map[string]struct{}, len(items))
n := 0
for _, res := range items {
result[n] = res
foundKeys[res.Key] = struct{}{}
n++
}
for _, r := range req {
if _, ok := foundKeys[r.Key]; !ok {
// Prevents a panic if out of bounds
if n >= len(result) {
return nil, errors.New("received more results than expected")
}
result[n] = state.BulkGetResponse{
Key: r.Key,
}
n++
}
}
// Prevents a panic if out of bounds
if n > len(result) {
n = len(result)
}
return result[:n], nil
}
// Set saves a CosmosDB item.
func (c *StateStore) Set(ctx context.Context, req *state.SetRequest) error {
err := state.CheckRequestOptions(req.Options)
if err != nil {
return err
}
partitionKey := populatePartitionMetadata(req.Key, req.Metadata)
options := azcosmos.ItemOptions{}
if req.HasETag() {
etag := azcore.ETag(*req.ETag)
options.IfMatchEtag = &etag
} else if req.Options.Concurrency == state.FirstWrite {
var u uuid.UUID
u, err = uuid.NewRandom()
if err != nil {
return err
}
options.IfMatchEtag = ptr.Of(azcore.ETag(u.String()))
}
// Consistency levels can only be relaxed so the session level is used here
if req.Options.Consistency == state.Strong {
options.ConsistencyLevel = azcosmos.ConsistencyLevelSession.ToPtr()
} else if req.Options.Consistency == state.Eventual {
options.ConsistencyLevel = azcosmos.ConsistencyLevelEventual.ToPtr()
}
doc, err := createUpsertItem(c.contentType, *req, partitionKey)
if err != nil {
return err
}
marsh, err := json.Marshal(doc)
if err != nil {
return err
}
upsertCtx, cancel := context.WithTimeout(ctx, defaultTimeout)
defer cancel()
pk := azcosmos.NewPartitionKeyString(partitionKey)
_, err = c.client.UpsertItem(upsertCtx, pk, marsh, &options)
if err != nil {
resErr := &azcore.ResponseError{}
if errors.As(err, &resErr) && resErr.StatusCode == http.StatusPreconditionFailed {
return state.NewETagError(state.ETagMismatch, err)
}
return err
}
return nil
}
// Delete performs a delete operation.
func (c *StateStore) Delete(ctx context.Context, req *state.DeleteRequest) error {
err := state.CheckRequestOptions(req.Options)
if err != nil {
return err
}
partitionKey := populatePartitionMetadata(req.Key, req.Metadata)
options := azcosmos.ItemOptions{}
if req.HasETag() {
etag := azcore.ETag(*req.ETag)
options.IfMatchEtag = &etag
} else if req.Options.Concurrency == state.FirstWrite {
var u uuid.UUID
u, err = uuid.NewRandom()
if err != nil {
return err
}
options.IfMatchEtag = ptr.Of(azcore.ETag(u.String()))
}
if req.Options.Consistency == state.Strong {
options.ConsistencyLevel = azcosmos.ConsistencyLevelSession.ToPtr()
} else if req.Options.Consistency == state.Eventual {
options.ConsistencyLevel = azcosmos.ConsistencyLevelEventual.ToPtr()
}
deleteCtx, cancel := context.WithTimeout(ctx, defaultTimeout)
defer cancel()
pk := azcosmos.NewPartitionKeyString(partitionKey)
_, err = c.client.DeleteItem(deleteCtx, pk, req.Key, &options)
if err != nil && !isNotFoundError(err) {
resErr := &azcore.ResponseError{}
if errors.As(err, &resErr) && resErr.StatusCode == http.StatusPreconditionFailed {
return state.NewETagError(state.ETagMismatch, err)
}
return err
}
return nil
}
// MultiMaxSize returns the maximum number of operations allowed in a transaction.
// For Azure Cosmos DB, that's 100.
func (c StateStore) MultiMaxSize() int {
return 100
}
// Multi performs a transactional operation. Succeeds only if all operations succeed, and fails if one or more operations fail.
// Note that all operations must be in the same partition.
func (c *StateStore) Multi(ctx context.Context, request *state.TransactionalStateRequest) (err error) {
if len(request.Operations) == 0 {
c.logger.Debugf("No operations provided")
return nil
}
partitionKey := request.Metadata[metadataPartitionKey]
batch := c.client.NewTransactionalBatch(azcosmos.NewPartitionKeyString(partitionKey))
numOperations := 0
// Loop through the list of operations. Create and add the operation to the batch
for _, o := range request.Operations {
options := &azcosmos.TransactionalBatchItemOptions{}
switch req := o.(type) {
case state.SetRequest:
var doc CosmosItem
doc, err = createUpsertItem(c.contentType, req, partitionKey)
if err != nil {
return err
}
doc.PartitionKey = partitionKey
if req.HasETag() {
etag := azcore.ETag(*req.ETag)
options.IfMatchETag = &etag
} else if req.Options.Concurrency == state.FirstWrite {
var u uuid.UUID
u, err = uuid.NewRandom()
if err != nil {
return err
}
options.IfMatchETag = ptr.Of(azcore.ETag(u.String()))
}
var marsh []byte
marsh, err = json.Marshal(doc)
if err != nil {
return err
}
batch.UpsertItem(marsh, options)
numOperations++
case state.DeleteRequest:
if req.HasETag() {
etag := azcore.ETag(*req.ETag)
options.IfMatchETag = &etag
} else if req.Options.Concurrency == state.FirstWrite {
var u uuid.UUID
u, err = uuid.NewRandom()
if err != nil {
return err
}
options.IfMatchETag = ptr.Of(azcore.ETag(u.String()))
}
batch.DeleteItem(req.Key, options)
numOperations++
}
}
c.logger.Debugf("#operations=%d,partitionkey=%s", numOperations, partitionKey)
execCtx, cancel := context.WithTimeout(ctx, defaultTimeout)
defer cancel()
batchResponse, err := c.client.ExecuteTransactionalBatch(execCtx, batch, nil)
if err != nil {
return err
}
if !batchResponse.Success {
// Transaction failed, look for the offending operation
for index, operation := range batchResponse.OperationResults {
if operation.StatusCode != http.StatusFailedDependency {
c.logger.Errorf("Transaction failed due to operation %v which failed with status code %d", index, operation.StatusCode)
return fmt.Errorf("transaction failed due to operation %v which failed with status code %d", index, operation.StatusCode)
}
}
return errors.New("transaction failed")
}
// Transaction succeeded
// We can inspect the individual operation results
if c.logger.IsOutputLevelEnabled(logger.DebugLevel) {
for index, operation := range batchResponse.OperationResults {
c.logger.Debugf("Operation %v completed with status code %d", index, operation.StatusCode)
}
}
return nil
}
func (c *StateStore) 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
}
// If present partitionKey, the value will be used in the query disabling the cross partition
q.partitionKey = ""
if val, found := req.Metadata[metadataPartitionKey]; found {
q.partitionKey = val
}
data, token, err := q.execute(ctx, c.client)
if err != nil {
return nil, err
}
return &state.QueryResponse{
Results: data,
Token: token,
}, nil
}
func (c *StateStore) Ping(ctx context.Context) error {
pingCtx, cancel := context.WithTimeout(ctx, defaultTimeout)
defer cancel()
_, err := c.client.Read(pingCtx, nil)
if err != nil {
return err
}
return nil
}
func createUpsertItem(contentType string, req state.SetRequest, partitionKey string) (CosmosItem, error) {
byteArray, isBinary := req.Value.([]byte)
if len(byteArray) == 0 {
isBinary = false
}
ttl, err := stateutils.ParseTTL(req.Metadata)
if err != nil {
return CosmosItem{}, fmt.Errorf("error parsing TTL from metadata: %s", err)
}
if isBinary {
if contenttype.IsJSONContentType(contentType) {
var value map[string]interface{}
err := json.Unmarshal(byteArray, &value)
// if byte array is not a valid JSON, so keep it as-is to be Base64 encoded in CosmosDB.
// otherwise, we save it as JSON
if err == nil {
item := CosmosItem{
ID: req.Key,
Value: value,
PartitionKey: partitionKey,
IsBinary: false,
TTL: ttl,
}
return item, nil
}
} else if contenttype.IsStringContentType(contentType) {
item := CosmosItem{
ID: req.Key,
Value: string(byteArray),
PartitionKey: partitionKey,
IsBinary: false,
TTL: ttl,
}
return item, nil
}
}
item := CosmosItem{
ID: req.Key,
Value: req.Value,
PartitionKey: partitionKey,
IsBinary: isBinary,
TTL: ttl,
}
return item, nil
}
// This is a helper to return the partition key to use. If if metadata["partitionkey"] is present,
// use that, otherwise use what's in "key".
func populatePartitionMetadata(key string, requestMetadata map[string]string) string {
if val, found := requestMetadata[metadataPartitionKey]; found {
return val
}
return key
}
func isNotFoundError(err error) bool {
if err == nil {
return false
}
if requestError, ok := err.(*azcore.ResponseError); ok {
if requestError.StatusCode == http.StatusNotFound {
return true
}
// we previously checked the error code, but unfortunately this is not stable between API versions
}
return false
}
func NewCosmosItemFromResponse(value []byte, logger logger.Logger) (item CosmosItem, err error) {
err = jsoniter.ConfigFastest.Unmarshal(value, &item)
if err != nil {
return item, err
}
if item.IsBinary {
if item.Value == nil {
return item, nil
}
strValue, ok := item.Value.(string)
if !ok || strValue == "" {
return item, nil
}
item.Value, err = base64.StdEncoding.DecodeString(strValue)
if err != nil {
logger.Warnf("CosmosDB state store Get request could not decode binary string: %v. Returning raw string instead.", err)
item.Value = []byte(strValue)
}
return item, nil
}
item.Value, err = jsoniter.ConfigFastest.Marshal(&item.Value)
if err != nil {
return item, err
}
return item, nil
}