forked from nats-io/jsm.go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streams.go
700 lines (570 loc) · 19.1 KB
/
streams.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
// Copyright 2020 The NATS 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 jsm
import (
"fmt"
"strings"
"sync"
"time"
"github.com/nats-io/jsm.go/api"
)
// DefaultStream is a template configuration with StreamPolicy retention and 1 years maximum age. No storage type or subjects are set
var DefaultStream = api.StreamConfig{
Retention: api.LimitsPolicy,
Discard: api.DiscardOld,
MaxConsumers: -1,
MaxMsgs: -1,
MaxMsgsPer: -1,
MaxBytes: -1,
MaxAge: 24 * 365 * time.Hour,
MaxMsgSize: -1,
Replicas: 1,
NoAck: false,
}
// DefaultWorkQueue is a template configuration with WorkQueuePolicy retention and 1 years maximum age. No storage type or subjects are set
var DefaultWorkQueue = api.StreamConfig{
Retention: api.WorkQueuePolicy,
Discard: api.DiscardOld,
MaxConsumers: -1,
MaxMsgs: -1,
MaxMsgsPer: -1,
MaxBytes: -1,
MaxAge: 24 * 365 * time.Hour,
MaxMsgSize: -1,
Replicas: api.StreamDefaultReplicas,
NoAck: false,
}
// DefaultStreamConfiguration is the configuration that will be used to create new Streams in NewStream
var DefaultStreamConfiguration = DefaultStream
// StreamOption configures a stream
type StreamOption func(o *api.StreamConfig) error
// Stream represents a JetStream Stream
type Stream struct {
cfg *api.StreamConfig
lastInfo *api.StreamInfo
mgr *Manager
sync.Mutex
}
// NewStreamFromDefault creates a new stream based on a supplied template and options
func (m *Manager) NewStreamFromDefault(name string, dflt api.StreamConfig, opts ...StreamOption) (stream *Stream, err error) {
if !IsValidName(name) {
return nil, fmt.Errorf("%q is not a valid stream name", name)
}
cfg, err := NewStreamConfiguration(dflt, opts...)
if err != nil {
return nil, err
}
cfg.Name = name
valid, errs := cfg.Validate(m.validator)
if !valid {
return nil, fmt.Errorf("configuration validation failed: %s", strings.Join(errs, ", "))
}
var resp api.JSApiStreamCreateResponse
err = m.jsonRequest(fmt.Sprintf(api.JSApiStreamCreateT, name), &cfg, &resp)
if err != nil {
return nil, err
}
return m.streamFromConfig(&resp.Config, resp.StreamInfo), nil
}
func (m *Manager) streamFromConfig(cfg *api.StreamConfig, info *api.StreamInfo) (stream *Stream) {
s := &Stream{cfg: cfg, mgr: m}
if info != nil {
s.lastInfo = info
}
return s
}
// LoadOrNewStreamFromDefault loads an existing stream or creates a new one matching opts and template
func (m *Manager) LoadOrNewStreamFromDefault(name string, dflt api.StreamConfig, opts ...StreamOption) (stream *Stream, err error) {
if !IsValidName(name) {
return nil, fmt.Errorf("%q is not a valid stream name", name)
}
for _, o := range opts {
o(&dflt)
}
s, err := m.LoadStream(name)
if IsNatsError(err, 10059) {
return m.NewStreamFromDefault(name, dflt)
}
return s, err
}
// NewStream creates a new stream using DefaultStream as a starting template allowing adjustments to be made using options
func (m *Manager) NewStream(name string, opts ...StreamOption) (stream *Stream, err error) {
return m.NewStreamFromDefault(name, DefaultStream, opts...)
}
// LoadOrNewStream loads an existing stream or creates a new one matching opts
func (m *Manager) LoadOrNewStream(name string, opts ...StreamOption) (stream *Stream, err error) {
return m.LoadOrNewStreamFromDefault(name, DefaultStream, opts...)
}
// LoadStream loads a stream by name
func (m *Manager) LoadStream(name string) (stream *Stream, err error) {
if !IsValidName(name) {
return nil, fmt.Errorf("%q is not a valid stream name", name)
}
stream = &Stream{
mgr: m,
cfg: &api.StreamConfig{Name: name},
}
err = m.loadConfigForStream(stream)
if err != nil {
return nil, err
}
return stream, nil
}
// NewStreamConfiguration generates a new configuration based on template modified by opts
func (m *Manager) NewStreamConfiguration(template api.StreamConfig, opts ...StreamOption) (*api.StreamConfig, error) {
return NewStreamConfiguration(template, opts...)
}
// NewStreamConfiguration generates a new configuration based on template modified by opts
func NewStreamConfiguration(template api.StreamConfig, opts ...StreamOption) (*api.StreamConfig, error) {
cfg := &template
for _, o := range opts {
err := o(cfg)
if err != nil {
return cfg, err
}
}
return cfg, nil
}
func (m *Manager) loadConfigForStream(stream *Stream) (err error) {
info, err := m.loadStreamInfo(stream.cfg.Name, nil)
if err != nil {
return err
}
stream.Lock()
stream.cfg = &info.Config
stream.lastInfo = info
stream.Unlock()
return nil
}
func (m *Manager) loadStreamInfo(stream string, req *api.JSApiStreamInfoRequest) (info *api.StreamInfo, err error) {
var resp api.JSApiStreamInfoResponse
err = m.jsonRequest(fmt.Sprintf(api.JSApiStreamInfoT, stream), req, &resp)
if err != nil {
return nil, err
}
return resp.StreamInfo, nil
}
func Subjects(s ...string) StreamOption {
return func(o *api.StreamConfig) error {
o.Subjects = s
return nil
}
}
// StreamDescription is a textual description of this stream to provide additional context
func StreamDescription(d string) StreamOption {
return func(o *api.StreamConfig) error {
o.Description = d
return nil
}
}
func LimitsRetention() StreamOption {
return func(o *api.StreamConfig) error {
o.Retention = api.LimitsPolicy
return nil
}
}
func InterestRetention() StreamOption {
return func(o *api.StreamConfig) error {
o.Retention = api.InterestPolicy
return nil
}
}
func WorkQueueRetention() StreamOption {
return func(o *api.StreamConfig) error {
o.Retention = api.WorkQueuePolicy
return nil
}
}
func MaxConsumers(m int) StreamOption {
return func(o *api.StreamConfig) error {
o.MaxConsumers = m
return nil
}
}
func MaxMessages(m int64) StreamOption {
return func(o *api.StreamConfig) error {
o.MaxMsgs = m
return nil
}
}
func MaxMessagesPerSubject(m int64) StreamOption {
return func(o *api.StreamConfig) error {
o.MaxMsgsPer = m
return nil
}
}
func MaxBytes(m int64) StreamOption {
return func(o *api.StreamConfig) error {
o.MaxBytes = m
return nil
}
}
func MaxAge(m time.Duration) StreamOption {
return func(o *api.StreamConfig) error {
o.MaxAge = m
return nil
}
}
func MaxMessageSize(m int32) StreamOption {
return func(o *api.StreamConfig) error {
o.MaxMsgSize = m
return nil
}
}
func FileStorage() StreamOption {
return func(o *api.StreamConfig) error {
o.Storage = api.FileStorage
return nil
}
}
func MemoryStorage() StreamOption {
return func(o *api.StreamConfig) error {
o.Storage = api.MemoryStorage
return nil
}
}
func Replicas(r int) StreamOption {
return func(o *api.StreamConfig) error {
o.Replicas = r
return nil
}
}
func NoAck() StreamOption {
return func(o *api.StreamConfig) error {
o.NoAck = true
return nil
}
}
func DiscardNew() StreamOption {
return func(o *api.StreamConfig) error {
o.Discard = api.DiscardNew
return nil
}
}
func DiscardOld() StreamOption {
return func(o *api.StreamConfig) error {
o.Discard = api.DiscardOld
return nil
}
}
func DuplicateWindow(d time.Duration) StreamOption {
return func(o *api.StreamConfig) error {
o.Duplicates = d
return nil
}
}
func PlacementCluster(cluster string) StreamOption {
return func(o *api.StreamConfig) error {
if o.Placement == nil {
o.Placement = &api.Placement{}
}
o.Placement.Cluster = cluster
return nil
}
}
func PlacementTags(tags ...string) StreamOption {
return func(o *api.StreamConfig) error {
if o.Placement == nil {
o.Placement = &api.Placement{}
}
o.Placement.Tags = tags
return nil
}
}
func Mirror(stream *api.StreamSource) StreamOption {
return func(o *api.StreamConfig) error {
o.Mirror = stream
return nil
}
}
func AppendSource(source *api.StreamSource) StreamOption {
return func(o *api.StreamConfig) error {
o.Sources = append(o.Sources, source)
return nil
}
}
func Sources(streams ...*api.StreamSource) StreamOption {
return func(o *api.StreamConfig) error {
o.Sources = streams
return nil
}
}
func DenyDelete() StreamOption {
return func(o *api.StreamConfig) error {
o.DenyDelete = true
return nil
}
}
func DenyPurge() StreamOption {
return func(o *api.StreamConfig) error {
o.DenyPurge = true
return nil
}
}
func AllowRollup() StreamOption {
return func(o *api.StreamConfig) error {
o.RollupAllowed = true
return nil
}
}
// PageContents creates a StreamPager used to traverse the contents of the stream,
// Close() should be called to dispose of the background consumer and resources
func (s *Stream) PageContents(opts ...PagerOption) (*StreamPager, error) {
pgr := &StreamPager{}
err := pgr.start(s.Name(), s.mgr, opts...)
if err != nil {
return nil, err
}
return pgr, err
}
// UpdateConfiguration updates the stream using cfg modified by opts, reloads configuration from the server post update
func (s *Stream) UpdateConfiguration(cfg api.StreamConfig, opts ...StreamOption) error {
ncfg, err := NewStreamConfiguration(cfg, opts...)
if err != nil {
return err
}
var resp api.JSApiStreamUpdateResponse
err = s.mgr.jsonRequest(fmt.Sprintf(api.JSApiStreamUpdateT, s.Name()), ncfg, &resp)
if err != nil {
return err
}
return s.Reset()
}
// Reset reloads the Stream configuration from the JetStream server
func (s *Stream) Reset() error {
return s.mgr.loadConfigForStream(s)
}
// LoadConsumer loads a named consumer related to this Stream
func (s *Stream) LoadConsumer(name string) (*Consumer, error) {
return s.mgr.LoadConsumer(s.cfg.Name, name)
}
// NewConsumer creates a new consumer in this Stream based on DefaultConsumer
func (s *Stream) NewConsumer(opts ...ConsumerOption) (consumer *Consumer, err error) {
return s.mgr.NewConsumer(s.Name(), opts...)
}
// LoadOrNewConsumer loads or creates a consumer based on these options
func (s *Stream) LoadOrNewConsumer(name string, opts ...ConsumerOption) (consumer *Consumer, err error) {
return s.mgr.LoadOrNewConsumer(s.Name(), name, opts...)
}
// NewConsumerFromDefault creates a new consumer in this Stream based on a supplied template config
func (s *Stream) NewConsumerFromDefault(dflt api.ConsumerConfig, opts ...ConsumerOption) (consumer *Consumer, err error) {
return s.mgr.NewConsumerFromDefault(s.Name(), dflt, opts...)
}
// LoadOrNewConsumerFromDefault loads or creates a consumer based on these options that adjust supplied template
func (s *Stream) LoadOrNewConsumerFromDefault(name string, deflt api.ConsumerConfig, opts ...ConsumerOption) (consumer *Consumer, err error) {
return s.mgr.LoadOrNewConsumerFromDefault(s.Name(), name, deflt, opts...)
}
// ConsumerNames is a list of all known consumers for this Stream
func (s *Stream) ConsumerNames() (names []string, err error) {
return s.mgr.ConsumerNames(s.Name())
}
// EachConsumer calls cb with each known consumer for this stream, error on any error to load consumers
func (s *Stream) EachConsumer(cb func(consumer *Consumer)) error {
consumers, err := s.mgr.Consumers(s.Name())
if err != nil {
return err
}
for _, c := range consumers {
cb(c)
}
return nil
}
// LatestInformation returns the most recently fetched stream information
func (s *Stream) LatestInformation() (info *api.StreamInfo, err error) {
s.Lock()
nfo := s.lastInfo
s.Unlock()
if nfo != nil {
return nfo, nil
}
return s.Information()
}
// Information loads the current stream information
func (s *Stream) Information(req ...api.JSApiStreamInfoRequest) (info *api.StreamInfo, err error) {
if len(req) > 1 {
return nil, fmt.Errorf("only one request info is accepted")
}
var ireq api.JSApiStreamInfoRequest
if len(req) == 1 {
ireq = req[0]
}
info, err = s.mgr.loadStreamInfo(s.Name(), &ireq)
if err != nil {
return nil, err
}
s.Lock()
s.lastInfo = info
s.Unlock()
return info, nil
}
// LatestState returns the most recently fetched stream state
func (s *Stream) LatestState() (state api.StreamState, err error) {
nfo, err := s.LatestInformation()
if err != nil {
return api.StreamState{}, err
}
return nfo.State, nil
}
// State retrieves the Stream State
func (s *Stream) State(req ...api.JSApiStreamInfoRequest) (stats api.StreamState, err error) {
info, err := s.Information(req...)
if err != nil {
return api.StreamState{}, err
}
return info.State, nil
}
// Delete deletes the Stream, after this the Stream object should be disposed
func (s *Stream) Delete() error {
var resp api.JSApiStreamDeleteResponse
err := s.mgr.jsonRequest(fmt.Sprintf(api.JSApiStreamDeleteT, s.Name()), nil, &resp)
if err != nil {
return err
}
if !resp.Success {
return fmt.Errorf("unknown failure")
}
return nil
}
// Seal updates a stream so that messages can not be added or removed using the API and limits will not be processed - messages will never age out.
// A sealed stream can not be unsealed.
func (s *Stream) Seal() error {
cfg := s.Configuration()
cfg.Sealed = true
return s.UpdateConfiguration(cfg)
}
// Purge deletes messages from the Stream, an optional JSApiStreamPurgeRequest can be supplied to limit the purge to a subset of messages
func (s *Stream) Purge(opts ...*api.JSApiStreamPurgeRequest) error {
if len(opts) > 1 {
return fmt.Errorf("only one purge option allowed")
}
var req *api.JSApiStreamPurgeRequest
if len(opts) == 1 {
req = opts[0]
}
var resp api.JSApiStreamPurgeResponse
err := s.mgr.jsonRequest(fmt.Sprintf(api.JSApiStreamPurgeT, s.Name()), req, &resp)
if err != nil {
return err
}
if !resp.Success {
return fmt.Errorf("unknown failure")
}
return nil
}
// ReadLastMessageForSubject reads the last message stored in the stream for a specific subject
func (s *Stream) ReadLastMessageForSubject(subj string) (*api.StoredMsg, error) {
return s.mgr.ReadLastMessageForSubject(s.Name(), subj)
}
// ReadMessage loads a message from the stream by its sequence number
func (s *Stream) ReadMessage(seq uint64) (msg *api.StoredMsg, err error) {
var resp api.JSApiMsgGetResponse
err = s.mgr.jsonRequest(fmt.Sprintf(api.JSApiMsgGetT, s.Name()), api.JSApiMsgGetRequest{Seq: seq}, &resp)
if err != nil {
return nil, err
}
return resp.Message, nil
}
// FastDeleteMessage deletes a specific message from the Stream without erasing the data, see DeleteMessage() for a safe delete
func (s *Stream) FastDeleteMessage(seq uint64) error {
return s.mgr.DeleteStreamMessage(s.Name(), seq, true)
}
// DeleteMessage deletes a specific message from the Stream by overwriting it with random data, see FastDeleteMessage() to remove the message without over writing data
func (s *Stream) DeleteMessage(seq uint64) (err error) {
var resp api.JSApiMsgDeleteResponse
err = s.mgr.jsonRequest(fmt.Sprintf(api.JSApiMsgDeleteT, s.Name()), api.JSApiMsgDeleteRequest{Seq: seq}, &resp)
if err != nil {
return err
}
if !resp.Success {
return fmt.Errorf("unknown error while deleting message %d", seq)
}
return nil
}
// AdvisorySubject is a wildcard subscription subject that subscribes to all advisories for this stream
func (s *Stream) AdvisorySubject() string {
return api.JSAdvisoryPrefix + ".*.*." + s.Name() + ".>"
}
// MetricSubject is a wildcard subscription subject that subscribes to all advisories for this stream
func (s *Stream) MetricSubject() string {
return api.JSMetricPrefix + ".*.*." + s.Name() + ".*"
}
// RemoveRAFTPeer removes a peer from the group indicating it will not return
func (s *Stream) RemoveRAFTPeer(peer string) error {
var resp api.JSApiStreamRemovePeerResponse
err := s.mgr.jsonRequest(fmt.Sprintf(api.JSApiStreamRemovePeerT, s.Name()), api.JSApiStreamRemovePeerRequest{Peer: peer}, &resp)
if err != nil {
return err
}
if !resp.Success {
return fmt.Errorf("unknown error while removing peer %q", peer)
}
return nil
}
// LeaderStepDown requests the current RAFT group leader in a clustered JetStream to stand down forcing a new election
func (s *Stream) LeaderStepDown() error {
var resp api.JSApiStreamLeaderStepDownResponse
err := s.mgr.jsonRequest(fmt.Sprintf(api.JSApiStreamLeaderStepDownT, s.Name()), nil, &resp)
if err != nil {
return err
}
if !resp.Success {
return fmt.Errorf("unknown error while requesting leader step down")
}
return nil
}
// IsTemplateManaged determines if this stream is managed by a template
func (s *Stream) IsTemplateManaged() bool { return s.Template() != "" }
// IsMirror determines if this stream is a mirror of another
func (s *Stream) IsMirror() bool { return s.cfg.Mirror != nil }
// IsSourced determines if this stream is sourcing data from another stream. Other streams
// could be synced to this stream and it would not be reported by this property
func (s *Stream) IsSourced() bool { return len(s.cfg.Sources) > 0 }
// IsInternal indicates if a stream is considered 'internal' by the NATS team,
// that is, it's a backing stream for KV, Object or MQTT state
func (s *Stream) IsInternal() bool {
return IsInternalStream(s.Name())
}
// IsKVBucket determines if a stream is a KV bucket
func (s *Stream) IsKVBucket() bool {
return IsKVBucketStream(s.Name())
}
// IsObjectBucket determines if a stream is a Object bucket
func (s *Stream) IsObjectBucket() bool {
return IsObjectBucketStream(s.Name())
}
// IsMQTTState determines if a stream holds internal MQTT state
func (s *Stream) IsMQTTState() bool {
return IsMQTTStateStream(s.Name())
}
func (s *Stream) Configuration() api.StreamConfig { return *s.cfg }
func (s *Stream) Name() string { return s.cfg.Name }
func (s *Stream) Description() string { return s.cfg.Description }
func (s *Stream) Subjects() []string { return s.cfg.Subjects }
func (s *Stream) Retention() api.RetentionPolicy { return s.cfg.Retention }
func (s *Stream) MaxConsumers() int { return s.cfg.MaxConsumers }
func (s *Stream) MaxMsgs() int64 { return s.cfg.MaxMsgs }
func (s *Stream) MaxMsgsPerSubject() int64 { return s.cfg.MaxMsgsPer }
func (s *Stream) MaxBytes() int64 { return s.cfg.MaxBytes }
func (s *Stream) MaxAge() time.Duration { return s.cfg.MaxAge }
func (s *Stream) MaxMsgSize() int32 { return s.cfg.MaxMsgSize }
func (s *Stream) Storage() api.StorageType { return s.cfg.Storage }
func (s *Stream) Replicas() int { return s.cfg.Replicas }
func (s *Stream) NoAck() bool { return s.cfg.NoAck }
func (s *Stream) Template() string { return s.cfg.Template }
func (s *Stream) DuplicateWindow() time.Duration { return s.cfg.Duplicates }
func (s *Stream) Mirror() *api.StreamSource { return s.cfg.Mirror }
func (s *Stream) Sources() []*api.StreamSource { return s.cfg.Sources }
func (s *Stream) Sealed() bool { return s.cfg.Sealed }
func (s *Stream) DeleteAllow() bool { return !s.cfg.DenyDelete }
func (s *Stream) PurgeAllowed() bool { return !s.cfg.DenyPurge }
func (s *Stream) RollupAllowed() bool { return s.cfg.RollupAllowed }