/
types.go
677 lines (564 loc) · 19.7 KB
/
types.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
package esdb
import (
"encoding/json"
"fmt"
"strings"
"time"
)
const SubscriberCountUnlimited = 0
// ConsumerStrategy named consumer strategies for use with persistent subscriptions.
type ConsumerStrategy string
const (
// ConsumerStrategyRoundRobin Distributes events to all clients evenly. If the client buffer-size is reached, the
// client is ignored until events are (not) acknowledged.
ConsumerStrategyRoundRobin ConsumerStrategy = "RoundRobin"
// ConsumerStrategyDispatchToSingle Distributes events to a single client until the buffer size is reached. After
// which the next client is selected in a round-robin style, and the process is repeated.
ConsumerStrategyDispatchToSingle ConsumerStrategy = "DispatchToSingle"
// ConsumerStrategyPinned For use with an indexing projection such as the system '$by_category' projection.
// EventStoreDB inspects event for its source stream id, hashing the id to one of 1024 buckets assigned to
// individual clients. When a client disconnects, its buckets are assigned to other clients. When a client connects,
// it is assigned some existing buckets. This naively attempts to maintain a balanced workload. The main goal of
// this strategy is to decrease the likelihood of concurrency and ordering issues while maintaining load balancing.
// This is not a guarantee, and you should handle the usual ordering and concurrency issues.
ConsumerStrategyPinned ConsumerStrategy = "Pinned"
)
// PersistentSubscriptionSettings persistent subscription settings.
type PersistentSubscriptionSettings struct {
// Where to start the subscription from.
StartFrom interface{}
// Resolve linkTo event to their linked events.
ResolveLinkTos bool
// Enable tracking of latency statistics on this subscription.
ExtraStatistics bool
// The maximum number of retries (due to timeout) before a message is considered to be parked. Default: 10.
MaxRetryCount int32
// The minimum number of messages to process before a checkpoint may be written. Default: 10.
CheckpointLowerBound int32
// The maximum number of messages not checkpointed before forcing a checkpoint. Default: 1000.
CheckpointUpperBound int32
// The maximum number of subscribers allowed. Default: 0 (Unbounded).
MaxSubscriberCount int32
// The size of the buffer (in-memory) listening to live messages as they happen before paging occurs. Default: 500.
LiveBufferSize int32
// The number of events read at a time when catching up. Default: 20.
ReadBatchSize int32
// The number of events to cache when catching up. Default: 500.
HistoryBufferSize int32
// The strategy to use for distributing events to client consumers.
ConsumerStrategyName ConsumerStrategy
// The amount of time after which to consider a message as timed out and retried. Default 30_000ms.
MessageTimeout int32
// The amount of time to try to checkpoint after. Default: 2_000ms.
CheckpointAfter int32
}
// SubscriptionSettingsDefault returns a persistent subscription settings with default values.
func SubscriptionSettingsDefault() PersistentSubscriptionSettings {
return PersistentSubscriptionSettings{
ResolveLinkTos: false,
ExtraStatistics: false,
MaxRetryCount: 10,
CheckpointLowerBound: 10,
CheckpointUpperBound: 1_000,
MaxSubscriberCount: SubscriberCountUnlimited,
LiveBufferSize: 500,
ReadBatchSize: 20,
HistoryBufferSize: 500,
ConsumerStrategyName: ConsumerStrategyRoundRobin,
MessageTimeout: 30 * 1000,
CheckpointAfter: 2 * 1000,
}
}
// Position transaction log position.
type Position struct {
// Commit position.
Commit uint64
// Prepare position.
Prepare uint64
}
// Direction Read direction.
type Direction int
const (
// Forwards Reads from the start to the end.
Forwards Direction = iota
// Backwards Reads from the end to the start.
Backwards
)
const (
// UserStreamAcl Default system ACL.
UserStreamAcl = "$userStreamAcl"
// SystemStreamAcl Default users ACL.
SystemStreamAcl = "$systemStreamAcl"
)
// Acl Access Control List (ACL).
type Acl struct {
readRoles []string
writeRoles []string
deleteRoles []string
metaReadRoles []string
metaWriteRoles []string
}
// AddReadRoles Adds read roles.
func (a *Acl) AddReadRoles(roles ...string) {
a.readRoles = append(a.readRoles, roles...)
}
// AddWriteRoles Adds write roles.
func (a *Acl) AddWriteRoles(roles ...string) {
a.writeRoles = append(a.writeRoles, roles...)
}
// AddDeleteRoles Adds delete roles.
func (a *Acl) AddDeleteRoles(roles ...string) {
a.deleteRoles = append(a.deleteRoles, roles...)
}
// AddMetaWriteRoles Adds metadata write roles.
func (a *Acl) AddMetaWriteRoles(roles ...string) {
a.metaWriteRoles = append(a.metaWriteRoles, roles...)
}
// ReadRoles Returns read roles.
func (a *Acl) ReadRoles() []string {
return a.readRoles
}
// WriteRoles Returns write roles.
func (a *Acl) WriteRoles() []string {
return a.writeRoles
}
// DeleteRoles Returns delete roles.
func (a *Acl) DeleteRoles() []string {
return a.deleteRoles
}
// MetaReadRoles Returns metadata read roles.
func (a *Acl) MetaReadRoles() []string {
return a.metaReadRoles
}
// MetaWriteRoles Returns metadata write roles.
func (a *Acl) MetaWriteRoles() []string {
return a.metaWriteRoles
}
// AddMetaReadRoles Adds metadata roles.
func (a *Acl) AddMetaReadRoles(roles ...string) {
a.metaReadRoles = append(a.metaReadRoles, roles...)
}
// StreamMetadata Represents stream metadata with strongly typed properties for system values and a dictionary-like
// interface for custom values.
type StreamMetadata struct {
maxCount []uint64
maxAge []time.Duration
truncateBefore []uint64
cacheControl []time.Duration
acl []interface{}
customProperties map[string]interface{}
}
// SetMaxCount The maximum number of events allowed in the stream.
func (m *StreamMetadata) SetMaxCount(value uint64) {
m.maxCount = []uint64{value}
}
// SetMaxAge The maximum age of events allowed in the stream.
func (m *StreamMetadata) SetMaxAge(value time.Duration) {
m.maxAge = []time.Duration{value}
}
// SetTruncateBefore The event number from which previous events can be scavenged. This is used to implement
// deletions of streams.
func (m *StreamMetadata) SetTruncateBefore(value uint64) {
m.truncateBefore = []uint64{value}
}
// SetCacheControl The amount of time for which the stream head is cacheable (in seconds).
func (m *StreamMetadata) SetCacheControl(value time.Duration) {
m.cacheControl = []time.Duration{value}
}
// SetAcl The Access Control List of the stream (ACL).
func (m *StreamMetadata) SetAcl(value interface{}) {
m.acl = []interface{}{value}
}
// AddCustomProperty Key-value pair of a key to JSON for user-provider metadata.
func (m *StreamMetadata) AddCustomProperty(name string, value interface{}) {
if m.customProperties == nil {
m.customProperties = make(map[string]interface{})
}
m.customProperties[name] = value
}
// MaxCount The maximum number of events allowed in the stream.
func (m *StreamMetadata) MaxCount() *uint64 {
if len(m.maxCount) == 0 {
return nil
}
return &m.maxCount[0]
}
// MaxAge The maximum age of events allowed in the stream.
func (m *StreamMetadata) MaxAge() *time.Duration {
if len(m.maxAge) == 0 {
return nil
}
return &m.maxAge[0]
}
// TruncateBefore The event number from which previous events can be scavenged. This is used to implement deletions
// of streams.
func (m *StreamMetadata) TruncateBefore() *uint64 {
if len(m.truncateBefore) == 0 {
return nil
}
return &m.truncateBefore[0]
}
// CacheControl The amount of time for which the stream head is cacheable (in seconds).
func (m *StreamMetadata) CacheControl() *time.Duration {
if len(m.cacheControl) == 0 {
return nil
}
return &m.cacheControl[0]
}
// Acl The Access Control List of the stream (ACL).
func (m *StreamMetadata) Acl() interface{} {
if len(m.acl) == 0 {
return nil
}
return m.acl[0]
}
// StreamAcl The Access Control List of the stream (ACL).
func (m *StreamMetadata) StreamAcl() *Acl {
acl := m.Acl()
if acl != nil {
if streamAcl, ok := acl.(Acl); ok {
return &streamAcl
}
}
return nil
}
// IsUserStreamAcl Checks if the ACL is set to users default.
func (m *StreamMetadata) IsUserStreamAcl() bool {
acl := m.Acl()
if acl != nil {
if str, ok := acl.(string); ok {
return str == UserStreamAcl
}
}
return false
}
// IsSystemStreamAcl Checks if the ACL is set to system default.
func (m *StreamMetadata) IsSystemStreamAcl() bool {
acl := m.Acl()
if acl != nil {
if str, ok := acl.(string); ok {
return str == SystemStreamAcl
}
}
return false
}
func flattenRoles(props map[string]interface{}, key string, roles []string) {
len_r := len(roles)
if len_r == 0 {
return
}
if len_r == 1 {
props[key] = roles[0]
return
}
props[key] = roles
}
func collectRoles(value interface{}) ([]string, error) {
switch roleValue := value.(type) {
case string:
return []string{roleValue}, nil
case []string:
return roleValue, nil
default:
return nil, fmt.Errorf("invalid acl role value: %v", roleValue)
}
}
func (a Acl) toMap() map[string]interface{} {
props := make(map[string]interface{})
flattenRoles(props, "$r", a.readRoles)
flattenRoles(props, "$w", a.writeRoles)
flattenRoles(props, "$d", a.deleteRoles)
flattenRoles(props, "$mr", a.metaReadRoles)
flattenRoles(props, "$mw", a.metaWriteRoles)
return props
}
func aclFromMap(props map[string]interface{}) (Acl, error) {
acl := Acl{}
for key, value := range props {
switch key {
case "$r":
roles, err := collectRoles(value)
if err != nil {
return acl, err
}
acl.readRoles = roles
case "$w":
roles, err := collectRoles(value)
if err != nil {
return acl, err
}
acl.writeRoles = roles
case "$d":
roles, err := collectRoles(value)
if err != nil {
return acl, err
}
acl.deleteRoles = roles
case "$mr":
roles, err := collectRoles(value)
if err != nil {
return acl, err
}
acl.metaReadRoles = roles
case "$mw":
roles, err := collectRoles(value)
if err != nil {
return acl, err
}
acl.metaWriteRoles = roles
default:
return acl, fmt.Errorf("unknown acl key: %v", key)
}
}
return acl, nil
}
func (m *StreamMetadata) toMap() (map[string]interface{}, error) {
props := make(map[string]interface{})
if maxCount := m.MaxCount(); maxCount != nil {
props["$maxCount"] = *maxCount
}
if maxAge := m.MaxAge(); maxAge != nil {
props["$maxAge"] = int64(maxAge.Seconds())
}
if truncateBefore := m.TruncateBefore(); truncateBefore != nil {
props["$tb"] = *truncateBefore
}
if cacheControl := m.CacheControl(); cacheControl != nil {
props["$cacheControl"] = int64(cacheControl.Seconds())
}
acl := m.Acl()
if acl != nil {
switch value := acl.(type) {
case string:
if value != UserStreamAcl && value != SystemStreamAcl {
return nil, fmt.Errorf("unsupported acl string value: %s", value)
}
props["$acl"] = value
case Acl:
props["$acl"] = value.toMap()
}
}
for key, value := range m.customProperties {
// We ignore properties that can conflict with internal metatadata names.
if strings.HasPrefix(key, "$") {
continue
}
props[key] = value
}
return props, nil
}
// ToJson serializes StreamMetadata to JSON.
func (m *StreamMetadata) ToJson() ([]byte, error) {
mm, err := m.toMap()
if err != nil {
return nil, err
}
return json.Marshal(mm)
}
// StreamMetadataFromJson deserializes a JSON byte array into a StreamMetadata object.
func StreamMetadataFromJson(bytes []byte) (*StreamMetadata, error) {
var outProps map[string]interface{}
err := json.Unmarshal(bytes, &outProps)
if err != nil {
return nil, err
}
return streamMetadataFromMap(outProps)
}
func lookForUint64(value interface{}) (uint64, bool) {
if i, ok := value.(uint64); ok {
return i, true
}
if i, ok := value.(uint32); ok {
return uint64(i), true
}
if i, ok := value.(float64); ok {
return uint64(i), true
}
return 0, false
}
func streamMetadataFromMap(props map[string]interface{}) (*StreamMetadata, error) {
meta := StreamMetadata{}
for key, value := range props {
switch key {
case "$maxCount":
if i, ok := lookForUint64(value); ok {
meta.SetMaxCount(i)
continue
}
return nil, fmt.Errorf("invalid $maxCount value: %v", value)
case "$maxAge":
if secs, ok := lookForUint64(value); ok {
meta.SetMaxAge(time.Duration(secs) * time.Second)
continue
}
return nil, fmt.Errorf("invalid $maxAge value: %v", value)
case "$tb":
if i, ok := lookForUint64(value); ok {
meta.SetTruncateBefore(i)
continue
}
return nil, fmt.Errorf("invalid $tb value: %v", value)
case "$cacheControl":
if secs, ok := lookForUint64(value); ok {
meta.SetCacheControl(time.Duration(secs) * time.Second)
continue
}
return nil, fmt.Errorf("invalid $cacheControl value: %v, type: %t", value, value)
case "$acl":
switch aclValue := value.(type) {
case string:
if aclValue != UserStreamAcl && aclValue != SystemStreamAcl {
return nil, fmt.Errorf("invalid string $acl value: %v", aclValue)
}
meta.SetAcl(value)
case map[string]interface{}:
acl, err := aclFromMap(aclValue)
if err != nil {
return nil, err
}
meta.SetAcl(acl)
default:
return nil, fmt.Errorf("invalid $acl object value: %v", value)
}
default:
meta.AddCustomProperty(key, value)
}
}
return &meta, nil
}
// FilterType represents the type filters supported by EventStoreDB.
type FilterType int
const (
// EventFilterType the filter will be applied on an event's type.
EventFilterType FilterType = 0
// StreamFilterType the filter will be applied on a stream name.
StreamFilterType FilterType = 1
// NoMaxSearchWindow disables the max search window.
NoMaxSearchWindow int = -1
)
// SubscriptionFilter is a filter that targets $all stream.
type SubscriptionFilter struct {
// Type of filter.
Type FilterType
// String prefixes.
Prefixes []string
// Regex expression.
Regex string
}
// ExcludeSystemEventsFilter filters out event's with system's event types, i.e. event types starting with `$`.
func ExcludeSystemEventsFilter() *SubscriptionFilter {
return &SubscriptionFilter{
Type: EventFilterType,
Regex: "^[^\\$].*",
}
}
type persistentSubscriptionInfoHttpJson struct {
EventStreamId string `json:"eventStreamId"`
GroupName string `json:"groupName"`
Status string `json:"status"`
AverageItemsPerSecond float64 `json:"averageItemsPerSecond"`
TotalItemsProcessed int64 `json:"totalItemsProcessed"`
LastProcessedEventNumber int64 `json:"lastProcessedEventNumber"`
LastKnownEventNumber int64 `json:"lastKnownEventNumber"`
LastCheckpointedEventPosition string `json:"lastCheckpointedEventPosition,omitempty"`
LastKnownEventPosition string `json:"lastKnownEventPosition,omitempty"`
ConnectionCount int64 `json:"connectionCount,omitempty"`
TotalInFlightMessages int64 `json:"totalInFlightMessages"`
Config *persistentSubscriptionConfig `json:"config,omitempty"`
Connections []PersistentSubscriptionConnectionInfo `json:"connections,omitempty"`
ReadBufferCount int64 `json:"readBufferCount"`
RetryBufferCount int64 `json:"retryBufferCount"`
LiveBufferCount int64 `json:"liveBufferCount"`
OutstandingMessagesCount int64 `json:"OutstandingMessagesCount"`
ParkedMessageCount int64 `json:"parkedMessageCount"`
CountSinceLastMeasurement int64 `json:"countSinceLastMeasurement"`
}
// PersistentSubscriptionInfo represents persistent subscription info.
type PersistentSubscriptionInfo struct {
// The source of events for the subscription.
EventSource string
// The group name given on creation.
GroupName string
// The current status of the subscription.
Status string
// Active connections to the subscription.
Connections []PersistentSubscriptionConnectionInfo
// Persistent subscription's settings.
Settings *PersistentSubscriptionSettings
// Persistent subscription's stats.
Stats *PersistentSubscriptionStats
}
// PersistentSubscriptionStats represents processing-related persistent subscription statistics.
type PersistentSubscriptionStats struct {
// Average number of events per seconds.
AveragePerSecond int64
// Total number of events processed by this subscription.
TotalItems int64
// Number of events seen since last measurement on this subscription.
CountSinceLastMeasurement int64
// The revision number of the last checkpoint.
LastCheckpointedEventRevision *uint64
// The revision number of the last known.
LastKnownEventRevision *uint64
// The transaction log position of the last checkpoint.
LastCheckpointedPosition *Position
// The transaction log position of the last known event.
LastKnownPosition *Position
// Number of events in the read buffer.
ReadBufferCount int64
// Number of events in the live buffer.
LiveBufferCount int64
// Number of events in the retry buffer.
RetryBufferCount int64
// Current in flight messages across the persistent subscription group.
TotalInFlightMessages int64
// Current number of outstanding messages.
OutstandingMessagesCount int64
// The current number of parked messages.
ParkedMessagesCount int64
}
type persistentSubscriptionConfig struct {
ResolveLinkTos bool `json:"resolveLinktos"`
StartFrom int64 `json:"startFrom"`
StartPosition string `json:"startPosition,omitempty"`
MessageTimeout int64 `json:"messageTimeoutMilliseconds"`
ExtraStatistics bool `json:"extraStatistics"`
MaxRetryCount int64 `json:"maxRetryCount"`
LiveBufferSize int64 `json:"liveBufferSize"`
BufferSize int64 `json:"bufferSize"`
ReadBatchSize int64 `json:"readBatchSize"`
PreferRoundRobin bool `json:"preferRoundRobin"`
CheckpointAfter int64 `json:"checkPointAfterMilliseconds"`
CheckpointLowerBound int64 `json:"minCheckPointCount"`
CheckpointUpperBound int64 `json:"maxCheckPointCount"`
MaxSubscriberCount int64 `json:"maxSubscriberCount"`
ConsumerStrategyName string `json:"consumerStrategyName"`
}
// PersistentSubscriptionConnectionInfo holds an active persistent subscription connection info.
type PersistentSubscriptionConnectionInfo struct {
// Origin of this connection.
From string `json:"from"`
// Connection's username.
Username string `json:"username"`
// Average events per second on this connection.
AverageItemsPerSecond float64 `json:"averageItemsPerSecond"`
// Total items on this connection.
TotalItemsProcessed int64 `json:"totalItemsProcessed"`
// Number of items seen since last measurement on this connection.
CountSinceLastMeasurement int64 `json:"countSinceLastMeasurement"`
// Number of available slots.
AvailableSlots int64 `json:"availableSlots"`
// Number of in flight messages on this connection.
InFlightMessages int64 `json:"inFlightMessages"`
// Connection's name.
ConnectionName string `json:"connectionName"`
// Timing measurements for the connection.
ExtraStatistics []PersistentSubscriptionMeasurement `json:"extraStatistics"`
}
// PersistentSubscriptionMeasurement key-value pair of a metric and its value.
type PersistentSubscriptionMeasurement struct {
// Metric name
Key string `json:"key"`
// Metric value.
Value int64 `json:"value"`
}