/
subscriptions.go
193 lines (159 loc) · 5.45 KB
/
subscriptions.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
package database
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/database/cosmosdb"
)
type subscriptions struct {
c cosmosdb.SubscriptionDocumentClient
uuid string
}
// Subscriptions is the database interface for SubscriptionDocuments
type Subscriptions interface {
Create(context.Context, *api.SubscriptionDocument) (*api.SubscriptionDocument, error)
Get(context.Context, string) (*api.SubscriptionDocument, error)
Update(context.Context, *api.SubscriptionDocument) (*api.SubscriptionDocument, error)
Dequeue(context.Context) (*api.SubscriptionDocument, error)
Lease(context.Context, string) (*api.SubscriptionDocument, error)
EndLease(context.Context, string, bool, bool) (*api.SubscriptionDocument, error)
}
// NewSubscriptions returns a new Subscriptions
func NewSubscriptions(ctx context.Context, uuid string, dbc cosmosdb.DatabaseClient, dbid, collid string) (Subscriptions, error) {
collc := cosmosdb.NewCollectionClient(dbc, dbid)
triggers := []*cosmosdb.Trigger{
{
ID: "renewLease",
TriggerOperation: cosmosdb.TriggerOperationAll,
TriggerType: cosmosdb.TriggerTypePre,
Body: `function trigger() {
var request = getContext().getRequest();
var body = request.getBody();
var date = new Date();
body["leaseExpires"] = Math.floor(date.getTime() / 1000) + 60;
request.setBody(body);
}`,
},
{
ID: "retryLater",
TriggerOperation: cosmosdb.TriggerOperationAll,
TriggerType: cosmosdb.TriggerTypePre,
Body: `function trigger() {
var request = getContext().getRequest();
var body = request.getBody();
var date = new Date();
body["leaseExpires"] = Math.floor(date.getTime() / 1000) + 600;
request.setBody(body);
}`,
},
}
triggerc := cosmosdb.NewTriggerClient(collc, collid)
for _, trigger := range triggers {
_, err := triggerc.Create(ctx, trigger)
if err != nil && !cosmosdb.IsErrorStatusCode(err, http.StatusConflict) {
return nil, err
}
}
return &subscriptions{
c: cosmosdb.NewSubscriptionDocumentClient(collc, collid),
uuid: uuid,
}, nil
}
func (c *subscriptions) Create(ctx context.Context, doc *api.SubscriptionDocument) (*api.SubscriptionDocument, error) {
if doc.ID != strings.ToLower(doc.ID) {
return nil, fmt.Errorf("id %q is not lower case", doc.ID)
}
doc, err := c.c.Create(ctx, doc.ID, doc, nil)
if err, ok := err.(*cosmosdb.Error); ok && err.StatusCode == http.StatusConflict {
err.StatusCode = http.StatusPreconditionFailed
}
return doc, err
}
func (c *subscriptions) Get(ctx context.Context, id string) (*api.SubscriptionDocument, error) {
if id != strings.ToLower(id) {
return nil, fmt.Errorf("id %q is not lower case", id)
}
return c.c.Get(ctx, id, id)
}
func (c *subscriptions) patch(ctx context.Context, id string, f func(*api.SubscriptionDocument) error, options *cosmosdb.Options) (*api.SubscriptionDocument, error) {
var doc *api.SubscriptionDocument
err := cosmosdb.RetryOnPreconditionFailed(func() (err error) {
doc, err = c.Get(ctx, id)
if err != nil {
return
}
err = f(doc)
if err != nil {
return
}
doc, err = c.update(ctx, doc, options)
return
})
return doc, err
}
func (c *subscriptions) patchWithLease(ctx context.Context, key string, f func(*api.SubscriptionDocument) error, options *cosmosdb.Options) (*api.SubscriptionDocument, error) {
return c.patch(ctx, key, func(doc *api.SubscriptionDocument) error {
if doc.LeaseOwner != c.uuid {
return fmt.Errorf("lost lease")
}
return f(doc)
}, nil)
}
func (c *subscriptions) Update(ctx context.Context, doc *api.SubscriptionDocument) (*api.SubscriptionDocument, error) {
return c.update(ctx, doc, nil)
}
func (c *subscriptions) update(ctx context.Context, doc *api.SubscriptionDocument, options *cosmosdb.Options) (*api.SubscriptionDocument, error) {
if doc.ID != strings.ToLower(doc.ID) {
return nil, fmt.Errorf("id %q is not lower case", doc.ID)
}
return c.c.Replace(ctx, doc.ID, doc, options)
}
func (c *subscriptions) Dequeue(ctx context.Context) (*api.SubscriptionDocument, error) {
i := c.c.Query("", &cosmosdb.Query{
Query: `SELECT * FROM Subscriptions doc WHERE (doc.deleting ?? false) AND (doc.leaseExpires ?? 0) < GetCurrentTimestamp() / 1000`,
})
for {
docs, err := i.Next(ctx)
if err != nil {
return nil, err
}
if docs == nil {
return nil, nil
}
for _, doc := range docs.SubscriptionDocuments {
doc.LeaseOwner = c.uuid
doc.Dequeues++
doc, err = c.update(ctx, doc, &cosmosdb.Options{PreTriggers: []string{"renewLease"}})
if cosmosdb.IsErrorStatusCode(err, http.StatusPreconditionFailed) { // someone else got there first
continue
}
return doc, err
}
}
}
func (c *subscriptions) Lease(ctx context.Context, id string) (*api.SubscriptionDocument, error) {
return c.patchWithLease(ctx, id, func(doc *api.SubscriptionDocument) error {
return nil
}, &cosmosdb.Options{PreTriggers: []string{"renewLease"}})
}
func (c *subscriptions) EndLease(ctx context.Context, id string, done, retryLater bool) (*api.SubscriptionDocument, error) {
var options *cosmosdb.Options
if retryLater {
options = &cosmosdb.Options{PreTriggers: []string{"retryLater"}}
}
return c.patchWithLease(ctx, id, func(doc *api.SubscriptionDocument) error {
if done {
doc.Deleting = false
}
doc.LeaseOwner = ""
doc.LeaseExpires = 0
if done || retryLater {
doc.Dequeues = 0
}
return nil
}, options)
}