-
Notifications
You must be signed in to change notification settings - Fork 170
/
state.go
405 lines (345 loc) · 10.4 KB
/
state.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
package client
import (
"context"
"time"
v1 "github.com/dapr/go-sdk/dapr/proto/common/v1"
pb "github.com/dapr/go-sdk/dapr/proto/runtime/v1"
"github.com/golang/protobuf/ptypes/duration"
"github.com/pkg/errors"
)
const (
// StateConsistencyUndefined is the undefined value for state consistency.
StateConsistencyUndefined StateConsistency = 0
// StateConsistencyEventual represents eventual state consistency value.
StateConsistencyEventual StateConsistency = 1
// StateConsistencyStrong represents strong state consistency value.
StateConsistencyStrong StateConsistency = 2
// StateConcurrencyUndefined is the undefined value for state concurrency.
StateConcurrencyUndefined StateConcurrency = 0
// StateConcurrencyFirstWrite represents first write concurrency value.
StateConcurrencyFirstWrite StateConcurrency = 1
// StateConcurrencyLastWrite represents last write concurrency value.
StateConcurrencyLastWrite StateConcurrency = 2
// StateOperationTypeUndefined is the undefined value for state operation type.
StateOperationTypeUndefined OperationType = 0
// StateOperationTypeUpsert represents upsert operation type value.
StateOperationTypeUpsert OperationType = 1
// StateOperationTypeDelete represents delete operation type value.
StateOperationTypeDelete OperationType = 2
// UndefinedType represents undefined type value
UndefinedType = "undefined"
)
type (
// StateConsistency is the consistency enum type.
StateConsistency int
// StateConcurrency is the concurrency enum type.
StateConcurrency int
// OperationType is the operation enum type.
OperationType int
)
// String returns the string value of the OperationType.
func (o OperationType) String() string {
names := [...]string{
UndefinedType,
"upsert",
"delete",
}
if o < StateOperationTypeUpsert || o > StateOperationTypeDelete {
return UndefinedType
}
return names[o]
}
// String returns the string value of the StateConsistency.
func (c StateConsistency) String() string {
names := [...]string{
UndefinedType,
"strong",
"eventual",
}
if c < StateConsistencyStrong || c > StateConsistencyEventual {
return UndefinedType
}
return names[c]
}
// String returns the string value of the StateConcurrency.
func (c StateConcurrency) String() string {
names := [...]string{
UndefinedType,
"first-write",
"last-write",
}
if c < StateConcurrencyFirstWrite || c > StateConcurrencyLastWrite {
return UndefinedType
}
return names[c]
}
var (
stateOptionDefault = &v1.StateOptions{
Concurrency: v1.StateOptions_CONCURRENCY_LAST_WRITE,
Consistency: v1.StateOptions_CONSISTENCY_STRONG,
}
)
// StateOperation is a collection of StateItems with a store name.
type StateOperation struct {
Type OperationType
Item *SetStateItem
}
// StateItem represents a single state item.
type StateItem struct {
Key string
Value []byte
Etag string
Metadata map[string]string
}
// BulkStateItem represents a single state item.
type BulkStateItem struct {
Key string
Value []byte
Etag string
Metadata map[string]string
Error string
}
// SetStateItem represents a single state to be persisted.
type SetStateItem struct {
Key string
Value []byte
Etag *ETag
Metadata map[string]string
Options *StateOptions
}
// DeleteStateItem represents a single state to be deleted.
type DeleteStateItem SetStateItem
// ETag represents an versioned record information
type ETag struct {
Value string
}
// StateOptions represents the state store persistence policy.
type StateOptions struct {
Concurrency StateConcurrency
Consistency StateConsistency
}
func toProtoSaveStateItem(si *SetStateItem) (item *v1.StateItem) {
s := &v1.StateItem{
Key: si.Key,
Metadata: si.Metadata,
Value: si.Value,
Options: toProtoStateOptions(si.Options),
}
if si.Etag != nil {
s.Etag = &v1.Etag{
Value: si.Etag.Value,
}
}
return s
}
func toProtoStateOptions(so *StateOptions) (opts *v1.StateOptions) {
if so == nil {
return stateOptionDefault
}
return &v1.StateOptions{
Concurrency: v1.StateOptions_StateConcurrency(so.Concurrency),
Consistency: v1.StateOptions_StateConsistency(so.Consistency),
}
}
func toProtoDuration(d time.Duration) *duration.Duration {
nanos := d.Nanoseconds()
secs := nanos / 1e9
nanos -= secs * 1e9
return &duration.Duration{
Seconds: int64(secs),
Nanos: int32(nanos),
}
}
// ExecuteStateTransaction provides way to execute multiple operations on a specified store.
func (c *GRPCClient) ExecuteStateTransaction(ctx context.Context, storeName string, meta map[string]string, ops []*StateOperation) error {
if storeName == "" {
return errors.New("nil storeName")
}
if len(ops) == 0 {
return nil
}
items := make([]*pb.TransactionalStateOperation, 0)
for _, op := range ops {
item := &pb.TransactionalStateOperation{
OperationType: op.Type.String(),
Request: toProtoSaveStateItem(op.Item),
}
items = append(items, item)
}
req := &pb.ExecuteStateTransactionRequest{
Metadata: meta,
StoreName: storeName,
Operations: items,
}
_, err := c.protoClient.ExecuteStateTransaction(c.withAuthToken(ctx), req)
if err != nil {
return errors.Wrap(err, "error executing state transaction")
}
return nil
}
// SaveState saves the raw data into store using default state options.
func (c *GRPCClient) SaveState(ctx context.Context, storeName, key string, data []byte) error {
item := &SetStateItem{Key: key, Value: data}
return c.SaveBulkState(ctx, storeName, item)
}
// SaveBulkState saves the multiple state item to store.
func (c *GRPCClient) SaveBulkState(ctx context.Context, storeName string, items ...*SetStateItem) error {
if storeName == "" {
return errors.New("nil store")
}
if items == nil {
return errors.New("nil item")
}
req := &pb.SaveStateRequest{
StoreName: storeName,
States: make([]*v1.StateItem, 0),
}
for _, si := range items {
item := toProtoSaveStateItem(si)
req.States = append(req.States, item)
}
_, err := c.protoClient.SaveState(c.withAuthToken(ctx), req)
if err != nil {
return errors.Wrap(err, "error saving state")
}
return nil
}
// GetBulkState retrieves state for multiple keys from specific store.
func (c *GRPCClient) GetBulkState(ctx context.Context, storeName string, keys []string, meta map[string]string, parallelism int32) ([]*BulkStateItem, error) {
if storeName == "" {
return nil, errors.New("nil store")
}
if len(keys) == 0 {
return nil, errors.New("keys required")
}
items := make([]*BulkStateItem, 0)
req := &pb.GetBulkStateRequest{
StoreName: storeName,
Keys: keys,
Metadata: meta,
Parallelism: parallelism,
}
results, err := c.protoClient.GetBulkState(c.withAuthToken(ctx), req)
if err != nil {
return nil, errors.Wrap(err, "error getting state")
}
if results == nil || results.Items == nil {
return items, nil
}
for _, r := range results.Items {
item := &BulkStateItem{
Key: r.Key,
Etag: r.Etag,
Value: r.Data,
Metadata: r.Metadata,
Error: r.Error,
}
items = append(items, item)
}
return items, nil
}
// GetState retrieves state from specific store using default consistency option.
func (c *GRPCClient) GetState(ctx context.Context, storeName, key string) (item *StateItem, err error) {
return c.GetStateWithConsistency(ctx, storeName, key, nil, StateConsistencyStrong)
}
// GetStateWithConsistency retrieves state from specific store using provided state consistency.
func (c *GRPCClient) GetStateWithConsistency(ctx context.Context, storeName, key string, meta map[string]string, sc StateConsistency) (item *StateItem, err error) {
if err := hasRequiredStateArgs(storeName, key); err != nil {
return nil, errors.Wrap(err, "missing required arguments")
}
req := &pb.GetStateRequest{
StoreName: storeName,
Key: key,
Consistency: v1.StateOptions_StateConsistency(sc),
Metadata: meta,
}
result, err := c.protoClient.GetState(c.withAuthToken(ctx), req)
if err != nil {
return nil, errors.Wrap(err, "error getting state")
}
return &StateItem{
Etag: result.Etag,
Key: key,
Value: result.Data,
Metadata: result.Metadata,
}, nil
}
// DeleteState deletes content from store using default state options.
func (c *GRPCClient) DeleteState(ctx context.Context, storeName, key string) error {
return c.DeleteStateWithETag(ctx, storeName, key, nil, nil, nil)
}
// DeleteStateWithETag deletes content from store using provided state options and etag.
func (c *GRPCClient) DeleteStateWithETag(ctx context.Context, storeName, key string, etag *ETag, meta map[string]string, opts *StateOptions) error {
if err := hasRequiredStateArgs(storeName, key); err != nil {
return errors.Wrap(err, "missing required arguments")
}
req := &pb.DeleteStateRequest{
StoreName: storeName,
Key: key,
Options: toProtoStateOptions(opts),
Metadata: meta,
}
if etag != nil {
req.Etag = &v1.Etag{
Value: etag.Value,
}
}
_, err := c.protoClient.DeleteState(c.withAuthToken(ctx), req)
if err != nil {
return errors.Wrap(err, "error deleting state")
}
return nil
}
// DeleteBulkState deletes content for multiple keys from store.
func (c *GRPCClient) DeleteBulkState(ctx context.Context, storeName string, keys []string) error {
if len(keys) == 0 {
return nil
}
items := make([]*DeleteStateItem, 0, len(keys))
for i := 0; i < len(keys); i++ {
item := &DeleteStateItem{
Key: keys[i],
}
items = append(items, item)
}
return c.DeleteBulkStateItems(ctx, storeName, items)
}
// DeleteBulkState deletes content for multiple keys from store.
func (c *GRPCClient) DeleteBulkStateItems(ctx context.Context, storeName string, items []*DeleteStateItem) error {
if len(items) == 0 {
return nil
}
states := make([]*v1.StateItem, 0, len(items))
for i := 0; i < len(items); i++ {
item := items[i]
if err := hasRequiredStateArgs(storeName, item.Key); err != nil {
return errors.Wrap(err, "missing required arguments")
}
state := &v1.StateItem{
Key: item.Key,
Metadata: item.Metadata,
Options: toProtoStateOptions(item.Options),
}
if item.Etag != nil {
state.Etag = &v1.Etag{
Value: item.Etag.Value,
}
}
states = append(states, state)
}
req := &pb.DeleteBulkStateRequest{
StoreName: storeName,
States: states,
}
_, err := c.protoClient.DeleteBulkState(c.withAuthToken(ctx), req)
return err
}
func hasRequiredStateArgs(storeName, key string) error {
if storeName == "" {
return errors.New("store")
}
if key == "" {
return errors.New("key")
}
return nil
}