-
Notifications
You must be signed in to change notification settings - Fork 16
/
connect.go
995 lines (869 loc) · 26.3 KB
/
connect.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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
// Credit for The NATS.IO Authors
// Copyright 2021-2022 The Memphis 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 server
package memphis
import (
"context"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"log"
"os"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/gofrs/uuid"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
"github.com/spaolacci/murmur3"
)
const (
sdkClientsUpdatesSubject = "$memphis_sdk_clients_updates"
maxBatchSize = 5000
memphisGlobalAccountName = "$memphis"
SEED = 31
JetstreamOperationTimeout = 30
)
var stationUpdatesSubsLock sync.Mutex
var stationFunctionsSubsLock sync.Mutex
var lockProducersMap sync.Mutex
var applicationId string
// Option is a function on the options for a connection.
type Option func(*Options) error
type ProducersMap map[string]*Producer
type ConsumersMap map[string]*Consumer
type PrefetchedMsgs struct {
msgs map[string]map[string][]*Msg
lock sync.Mutex
}
type TLSOpts struct {
TlsCert string
TlsKey string
CaFile string
}
type Options struct {
Host string
Port int
Username string
AccountId int
ConnectionToken string
Reconnect bool
MaxReconnect int // MaxReconnect is the maximum number of reconnection attempts. The default value is -1 which means reconnect indefinitely.
ReconnectInterval time.Duration
Timeout time.Duration
TLSOpts TLSOpts
Password string
}
type SdkClientsUpdate struct {
StationName string `json:"station_name"`
Type string `json:"type"`
Update bool `json:"update"`
}
// FetchOpts - configuration options for fetch.
type FetchOpts struct {
ConsumerName string
StationName string
ConsumerGroup string
BatchSize int
BatchMaxTimeToWait time.Duration
MaxAckTime time.Duration
MaxMsgDeliveries int
GenUniqueSuffix bool
ErrHandler ConsumerErrHandler
StartConsumeFromSequence uint64
LastMessages int64
Prefetch bool
FetchPartitionKey string
FetchPartitionNumber int
}
type RequestOpts struct {
TimeoutRetries int
}
// getDefaultConsumerOptions - returns default configuration options for consumers.
func getDefaultFetchOptions() FetchOpts {
return FetchOpts{
BatchSize: 10,
ConsumerGroup: "",
BatchMaxTimeToWait: 100 * time.Millisecond,
MaxAckTime: 10 * time.Second,
MaxMsgDeliveries: 2,
GenUniqueSuffix: false,
ErrHandler: DefaultConsumerErrHandler,
StartConsumeFromSequence: 1,
LastMessages: -1,
Prefetch: false,
FetchPartitionKey: "",
FetchPartitionNumber: -1,
}
}
// FetchOpt - a function on the options fetch.
type FetchOpt func(*FetchOpts) error
// RequestOpt - a function on the options request.
type RequestOpt func(*RequestOpts) error
// IsConnected - check if connected to broker - returns boolean
func (c *Conn) IsConnected() bool {
return c.brokerConn.IsConnected()
}
func (c *Conn) getProducersMap() ProducersMap {
return c.producersMap
}
func (c *Conn) setProducersMap(producersMap ProducersMap) {
lockProducersMap.Lock()
c.producersMap = producersMap
lockProducersMap.Unlock()
}
func (c *Conn) getConsumersMap() ConsumersMap {
return c.consumersMap
}
func (c *Conn) setConsumersMap(consumersMap ConsumersMap) {
c.consumersMap = consumersMap
}
func DefaultErrHandler(nc *nats.Conn) {
err := memphisError(nc.LastError())
if err != nil {
fmt.Println(err)
}
}
// Conn - holds the connection with memphis.
type Conn struct {
opts Options
ConnId string
username string
accountId int
brokerConn *nats.Conn
js jetstream.JetStream
stationUpdatesMu sync.RWMutex
stationUpdatesSubs map[string]*stationUpdateSub
stationFunctionSubs map[string]*stationFunctionSub
stationPartitions map[string]*PartitionsUpdate
sdkClientsUpdatesMu sync.RWMutex
clientsUpdatesSub sdkClientsUpdateSub
producersMap ProducersMap
consumersMap ConsumersMap
prefetchedMsgs PrefetchedMsgs
}
type PartitionsUpdate struct {
PartitionsList []int `json:"partitions_list"`
}
type enforceSchemaReq struct {
Name string `json:"name"`
StationName string `json:"station_name"`
Username string `json:"username"`
}
type detachSchemaReq struct {
StationName string `json:"station_name"`
Username string `json:"username"`
}
type RoundRobinProducerConsumerGenerator struct {
NumberOfPartitions int
Partitions []int
Current int
mutex sync.Mutex
}
func newRoundRobinGenerator(partitions []int) *RoundRobinProducerConsumerGenerator {
return &RoundRobinProducerConsumerGenerator{
NumberOfPartitions: len(partitions),
Partitions: partitions,
Current: 0,
}
}
func (rr *RoundRobinProducerConsumerGenerator) Next() int {
rr.mutex.Lock()
defer rr.mutex.Unlock()
partitionNumber := rr.Partitions[rr.Current]
rr.Current = (rr.Current + 1) % rr.NumberOfPartitions
return partitionNumber
}
// getDefaultOptions - returns default configuration options for the client.
func getDefaultOptions() Options {
return Options{
Port: 6666,
Reconnect: true,
MaxReconnect: -1,
ReconnectInterval: 1 * time.Second,
Timeout: 2 * time.Second,
TLSOpts: TLSOpts{
TlsCert: "",
TlsKey: "",
CaFile: "",
},
ConnectionToken: "",
Password: "",
AccountId: 1,
}
}
type sdkClientsUpdateSub struct {
SdkClientsUpdatesCh chan SdkClientsUpdate
SdkClientsUpdateSub *nats.Subscription
ClusterConfigurations map[string]bool
StationSchemaverseToDlsMap map[string]bool
}
// Connect - creates connection with memphis.
func Connect(host, username string, options ...Option) (*Conn, error) {
opts := getDefaultOptions()
opts.Host = normalizeHost(host)
opts.Username = username
for _, opt := range options {
if opt != nil {
if err := opt(&opts); err != nil {
return nil, memphisError(err)
}
}
}
conn, err := opts.connect()
if err != nil {
return nil, err
}
err = conn.listenToSdkClientsUpdates()
if err != nil {
return nil, err
}
return conn, nil
}
func normalizeHost(host string) string {
r := regexp.MustCompile("^http(s?)://")
return r.ReplaceAllString(host, "")
}
func randomHex(n int) (string, error) {
bytes := make([]byte, n)
if _, err := rand.Read(bytes); err != nil {
return "", memphisError(err)
}
return hex.EncodeToString(bytes), nil
}
func (opts Options) connect() (*Conn, error) {
if opts.MaxReconnect > 9 {
opts.MaxReconnect = 9
}
if !opts.Reconnect {
opts.MaxReconnect = 0
}
if opts.ConnectionToken != "" && opts.Password != "" {
return nil, memphisError(errors.New("you have to connect with one of the following methods: connection token / password"))
}
if opts.ConnectionToken == "" && opts.Password == "" {
return nil, memphisError(errors.New("you have to connect with one of the following methods: connection token / password"))
}
connId, err := uuid.NewV4()
if err != nil {
return nil, memphisError(err)
}
c := Conn{
ConnId: connId.String(),
opts: opts,
producersMap: make(ProducersMap),
consumersMap: make(ConsumersMap),
prefetchedMsgs: PrefetchedMsgs{msgs: make(map[string]map[string][]*Msg)},
}
if err := c.startConn(); err != nil {
return nil, memphisError(err)
}
stationUpdatesSubsLock.Lock()
defer stationUpdatesSubsLock.Unlock()
c.stationUpdatesSubs = make(map[string]*stationUpdateSub)
stationFunctionsSubsLock.Lock()
defer stationFunctionsSubsLock.Unlock()
c.stationFunctionSubs = make(map[string]*stationFunctionSub)
c.stationPartitions = make(map[string]*PartitionsUpdate)
return &c, nil
}
func disconnectedError(conn *nats.Conn, err error) {
if err != nil {
fmt.Printf("Error %v", err.Error())
}
}
func (c *Conn) getBrokerConnection(natsOpts nats.Options) (*nats.Conn, error) {
// for backward compatibility.
var err error
opts := &c.opts
if natsOpts.User != "" {
pingNatsOpts := natsOpts
pingNatsOpts.AllowReconnect = false
connection, err := pingNatsOpts.Connect()
if err != nil {
if strings.Contains(err.Error(), "Authorization Violation") {
if strings.Contains(opts.Host, "localhost") { // for handling bad quality networks like port fwd
time.Sleep(1 * time.Second)
}
pingNatsOpts.User = opts.Username
connection, err = pingNatsOpts.Connect()
if err != nil {
return connection, memphisError(err)
}
natsOpts.User = opts.Username
} else {
return connection, memphisError(err)
}
}
connection.Close()
}
if strings.Contains(opts.Host, "localhost") { // for handling bad quality networks like port fwd
time.Sleep(1 * time.Second)
}
c.brokerConn, err = natsOpts.Connect()
if err != nil {
return c.brokerConn, memphisError(err)
}
return c.brokerConn, nil
}
func (c *Conn) startConn() error {
opts := &c.opts
var err error
url := opts.Host + ":" + strconv.Itoa(opts.Port)
natsOpts := nats.Options{
Url: url,
AllowReconnect: opts.Reconnect,
MaxReconnect: opts.MaxReconnect,
ReconnectWait: opts.ReconnectInterval,
Timeout: opts.Timeout,
DisconnectedErrCB: disconnectedError,
Name: c.ConnId + "::" + opts.Username,
ClosedCB: DefaultErrHandler,
RetryOnFailedConnect: false,
}
if opts.ConnectionToken != "" {
natsOpts.Token = opts.ConnectionToken
} else {
natsOpts.Password = opts.Password
natsOpts.User = opts.Username + "$" + strconv.Itoa(opts.AccountId)
}
if (opts.TLSOpts.TlsCert != "") || (opts.TLSOpts.TlsKey != "") || (opts.TLSOpts.CaFile != "") {
if opts.TLSOpts.TlsCert == "" {
return memphisError(errors.New("must provide a TLS cert file"))
}
if opts.TLSOpts.TlsKey == "" {
return memphisError(errors.New("must provide a TLS key file"))
}
if opts.TLSOpts.CaFile == "" {
return memphisError(errors.New("must provide a TLS ca file"))
}
cert, err := tls.LoadX509KeyPair(opts.TLSOpts.TlsCert, opts.TLSOpts.TlsKey)
if err != nil {
return memphisError(errors.New("memphis: error loading client certificate: " + err.Error()))
}
cert.Leaf, err = x509.ParseCertificate(cert.Certificate[0])
if err != nil {
return memphisError(errors.New("memphis: error parsing client certificate: " + err.Error()))
}
TLSConfig := &tls.Config{MinVersion: tls.VersionTLS12}
TLSConfig.Certificates = []tls.Certificate{cert}
certs := x509.NewCertPool()
pemData, err := os.ReadFile(opts.TLSOpts.CaFile)
if err != nil {
return memphisError(errors.New("memphis: error loading ca file: " + err.Error()))
}
certs.AppendCertsFromPEM(pemData)
TLSConfig.RootCAs = certs
natsOpts.TLSConfig = TLSConfig
}
c.brokerConn, err = c.getBrokerConnection(natsOpts)
if err != nil {
return memphisError(err)
}
c.js, err = jetstream.New(c.brokerConn)
if err != nil {
c.brokerConn.Close()
return memphisError(err)
}
c.username = opts.Username
return nil
}
func (c *Conn) Close() {
c.brokerConn.Close()
c.setProducersMap(nil)
c.setConsumersMap(nil)
}
func (c *Conn) brokerPublish(msg *nats.Msg, opts ...jetstream.PublishOpt) (jetstream.PubAckFuture, error) {
return c.js.PublishMsgAsync(msg, opts...)
}
func (c *Conn) jetstreamConsumer(streamName, durable string) (jetstream.Consumer, error) {
ctx, cancelfunc := context.WithTimeout(context.Background(), JetstreamOperationTimeout*time.Second)
defer cancelfunc()
return c.js.Consumer(ctx, streamName, durable)
}
func (c *Conn) brokerQueueSubscribe(subj, queue string, cb nats.MsgHandler) (*nats.Subscription, error) {
return c.brokerConn.QueueSubscribe(subj, queue, cb)
}
func (c *Conn) getSchemaEnforceSubject() string {
return "$memphis_schema_attachments"
}
func (c *Conn) getSchemaDetachSubject() string {
return "$memphis_schema_detachments"
}
// Port - default is 6666.
func Port(port int) Option {
return func(o *Options) error {
o.Port = port
return nil
}
}
// Reconnect - whether to do reconnect while connection is lost.
func Reconnect(reconnect bool) Option {
return func(o *Options) error {
o.Reconnect = reconnect
return nil
}
}
// MaxReconnect - the amount of reconnect attempts.
func MaxReconnect(maxReconnect int) Option {
return func(o *Options) error {
o.MaxReconnect = maxReconnect
return nil
}
}
// ReconnectInterval - interval in miliseconds between reconnect attempts.
func ReconnectInterval(reconnectInterval time.Duration) Option {
return func(o *Options) error {
o.ReconnectInterval = reconnectInterval
return nil
}
}
// Timeout - connection timeout in miliseconds.
func Timeout(timeout time.Duration) Option {
return func(o *Options) error {
o.Timeout = timeout
return nil
}
}
// ConnectionToken - string connection token.
func ConnectionToken(connectionToken string) Option {
return func(o *Options) error {
o.ConnectionToken = connectionToken
return nil
}
}
// Password - string password.
func Password(password string) Option {
return func(o *Options) error {
o.Password = password
return nil
}
}
// Tls - paths to tls cert, key and ca files.
func Tls(TlsCert string, TlsKey string, CaFile string) Option {
return func(o *Options) error {
o.TLSOpts = TLSOpts{
TlsCert: TlsCert,
TlsKey: TlsKey,
CaFile: CaFile,
}
return nil
}
}
// AccountId - default is 1.
func AccountId(accountId int) Option {
return func(o *Options) error {
o.AccountId = accountId
return nil
}
}
// TimeoutRetry - number of retries in case of timeout. default is 5.
func TimeoutRetry(retries int) RequestOpt {
return func(opts *RequestOpts) error {
opts.TimeoutRetries = retries
return nil
}
}
type directObj interface {
getCreationSubject() string
getCreationReq() any
handleCreationResp([]byte) error
getDestructionSubject() string
getDestructionReq() any
}
func defaultHandleCreationResp(resp []byte) error {
if len(resp) > 0 {
return memphisError(errors.New(string(resp)))
}
return nil
}
func getDefaultRequestOptions() RequestOpts {
return RequestOpts{
TimeoutRetries: 5,
}
}
func (c *Conn) request(subj string, data []byte, timeout time.Duration, options ...RequestOpt) (*nats.Msg, error) {
requestOpts := getDefaultRequestOptions()
for _, opt := range options {
if opt != nil {
if err := opt(&requestOpts); err != nil {
return nil, memphisError(err)
}
}
}
msg, err := c.brokerConn.Request(subj, data, timeout)
if err != nil && strings.Contains(err.Error(), "timeout") {
retryCounter := 0
for retryCounter < requestOpts.TimeoutRetries {
msg, err = c.brokerConn.Request(subj, data, timeout)
if err != nil {
if strings.Contains(err.Error(), "timeout") {
retryCounter++
continue
}
return nil, memphisError(err)
}
return msg, nil
}
if err != nil {
return nil, memphisError(err)
}
}
return msg, nil
}
func (c *Conn) create(do directObj, options ...RequestOpt) error {
subject := do.getCreationSubject()
req := do.getCreationReq()
b, err := json.Marshal(req)
if err != nil {
return memphisError(err)
}
msg, err := c.request(subject, b, 20*time.Second, options...)
if err != nil {
return memphisError(err)
}
return do.handleCreationResp(msg.Data)
}
// Depreciated - use EnforceSchema instead
func (c *Conn) AttachSchema(name string, stationName string) error {
return c.EnforceSchema(name, stationName)
}
// EnforceSchema - -Enforcing a schema on a chosen station
func (c *Conn) EnforceSchema(name string, stationName string, options ...RequestOpt) error {
subject := c.getSchemaEnforceSubject()
creationReq := &enforceSchemaReq{
Name: name,
StationName: stationName,
Username: c.username,
}
b, err := json.Marshal(creationReq)
if err != nil {
return memphisError(err)
}
msg, err := c.request(subject, b, 20*time.Second, options...)
if err != nil {
return memphisError(err)
}
if len(msg.Data) > 0 {
return memphisError(errors.New(string(msg.Data)))
}
return nil
}
func (c *Conn) DetachSchema(stationName string, options ...RequestOpt) error {
subject := c.getSchemaDetachSubject()
req := &detachSchemaReq{
StationName: stationName,
Username: c.username,
}
b, err := json.Marshal(req)
if err != nil {
return memphisError(err)
}
msg, err := c.request(subject, b, 20*time.Second, options...)
if err != nil {
return memphisError(err)
}
if len(msg.Data) > 0 {
return memphisError(errors.New(string(msg.Data)))
}
return nil
}
func (c *Conn) destroy(o directObj, option ...RequestOpt) error {
subject := o.getDestructionSubject()
destructionReq := o.getDestructionReq()
b, err := json.Marshal(destructionReq)
if err != nil {
return memphisError(err)
}
msg, err := c.request(subject, b, 20*time.Second, option...)
if err != nil {
return memphisError(err)
}
if msg != nil && len(msg.Data) > 0 && !strings.Contains(string(msg.Data), "not exist") {
return memphisError(errors.New(string(msg.Data)))
}
return nil
}
func getInternalName(name string) string {
name = strings.ToLower(name)
return replaceDelimiters(name)
}
func getLowerCaseName(name string) string {
return strings.ToLower(name)
}
const (
delimToReplace = "."
delimReplacement = "#"
)
func replaceDelimiters(in string) string {
return strings.Replace(in, delimToReplace, delimReplacement, -1)
}
func (c *Conn) listenToSdkClientsUpdates() error {
c.clientsUpdatesSub = sdkClientsUpdateSub{
SdkClientsUpdatesCh: make(chan SdkClientsUpdate),
ClusterConfigurations: make(map[string]bool),
StationSchemaverseToDlsMap: make(map[string]bool),
}
cus := c.clientsUpdatesSub
go cus.sdkClientUpdatesHandler(c)
var err error
cus.SdkClientsUpdateSub, err = c.brokerConn.Subscribe(sdkClientsUpdatesSubject, cus.createUpdatesHandler())
if err != nil {
close(cus.SdkClientsUpdatesCh)
return memphisError(err)
}
return nil
}
func (cus *sdkClientsUpdateSub) createUpdatesHandler() nats.MsgHandler {
return func(msg *nats.Msg) {
var update SdkClientsUpdate
err := json.Unmarshal(msg.Data, &update)
if err != nil {
log.Printf("update unmarshal error: %v\n", memphisError(err))
return
}
cus.SdkClientsUpdatesCh <- update
}
}
func (cus *sdkClientsUpdateSub) sdkClientUpdatesHandler(c *Conn) {
lock := &c.sdkClientsUpdatesMu
for {
update, ok := <-cus.SdkClientsUpdatesCh
if !ok {
return
}
lock.Lock()
switch update.Type {
case "send_notification":
cus.ClusterConfigurations[update.Type] = update.Update
case "schemaverse_to_dls":
cus.StationSchemaverseToDlsMap[getInternalName(update.StationName)] = update.Update
case "remove_station":
pm := c.getProducersMap()
pm.unsetStationProducers(update.StationName)
cm := c.getConsumersMap()
cm.unsetStationConsumers(update.StationName)
c.removeSchemaUpdatesListener(update.StationName)
c.removeFunctionsUpdatesListener(update.StationName)
}
lock.Unlock()
}
}
func (pm *ProducersMap) getProducer(key string) *Producer {
if (*pm) != nil && (*pm)[key] != nil {
return (*pm)[key]
}
return nil
}
func (pm *ProducersMap) setProducer(p *Producer) {
lockProducersMap.Lock()
stationName := getInternalName(p.stationName.(string))
pn := fmt.Sprintf("%s_%s", stationName, p.realName)
if pm.getProducer(pn) != nil {
lockProducersMap.Unlock()
return
}
(*pm)[pn] = p
lockProducersMap.Unlock()
}
func (pm *ProducersMap) unsetProducer(key string) {
lockProducersMap.Lock()
delete(*pm, key)
lockProducersMap.Unlock()
}
func (pm *ProducersMap) unsetStationProducers(stationName string) {
internalStationName := getInternalName(stationName)
for k, v := range *pm {
intetnalStationV := getInternalName(v.stationName.(string))
if intetnalStationV == internalStationName {
pm.unsetProducer(k)
}
}
}
func (cm *ConsumersMap) getConsumer(key string) *Consumer {
if (*cm) != nil && (*cm)[key] != nil {
return (*cm)[key]
}
return nil
}
func (cm *ConsumersMap) setConsumer(c *Consumer) {
internalStationName := getInternalName(c.stationName)
cn := fmt.Sprintf("%s_%s", internalStationName, c.realName)
if cm.getConsumer(cn) != nil {
return
}
(*cm)[cn] = c
}
func (cm *ConsumersMap) unsetConsumer(key string) {
delete(*cm, key)
}
func (cm *ConsumersMap) unsetStationConsumers(stationName string) {
internalStationName := getInternalName(stationName)
for k, v := range *cm {
intetnalStationV := getInternalName(v.stationName)
if intetnalStationV == internalStationName {
cm.unsetConsumer(k)
}
}
}
// FetchMessages - Consume a batch of messages.
func (c *Conn) FetchMessages(stationName string, consumerName string, opts ...FetchOpt) ([]*Msg, error) {
var consumer *Consumer
cm := c.getConsumersMap()
internalStationName := getInternalName(strings.ToLower(stationName))
cons := cm.getConsumer(fmt.Sprintf("%s_%s", internalStationName, strings.ToLower(consumerName)))
defaultOpts := getDefaultFetchOptions()
defaultOpts.ConsumerName = consumerName
defaultOpts.StationName = stationName
for _, opt := range opts {
if opt != nil {
if err := opt(&defaultOpts); err != nil {
return nil, memphisError(err)
}
}
}
if defaultOpts.BatchSize > maxBatchSize || defaultOpts.BatchSize < 1 {
return nil, memphisError(errors.New("Batch size can not be greater than " + strconv.Itoa(maxBatchSize) + " or less than 1"))
}
if cons == nil {
if defaultOpts.GenUniqueSuffix {
co, err := c.CreateConsumer(stationName, consumerName, BatchMaxWaitTime(defaultOpts.BatchMaxTimeToWait), BatchSize(defaultOpts.BatchSize), ConsumerGroup(defaultOpts.ConsumerGroup), ConsumerErrorHandler(defaultOpts.ErrHandler), LastMessages(defaultOpts.LastMessages), MaxAckTime(defaultOpts.MaxAckTime), MaxMsgDeliveries(defaultOpts.MaxMsgDeliveries), StartConsumeFromSequence(defaultOpts.StartConsumeFromSequence), ConsumerGenUniqueSuffix())
if err != nil {
return nil, err
}
consumer = co
} else {
con, err := c.CreateConsumer(stationName, consumerName, BatchMaxWaitTime(defaultOpts.BatchMaxTimeToWait), BatchSize(defaultOpts.BatchSize), ConsumerGroup(defaultOpts.ConsumerGroup), ConsumerErrorHandler(defaultOpts.ErrHandler), LastMessages(defaultOpts.LastMessages), MaxAckTime(defaultOpts.MaxAckTime), MaxMsgDeliveries(defaultOpts.MaxMsgDeliveries), StartConsumeFromSequence(defaultOpts.StartConsumeFromSequence))
if err != nil {
return nil, err
}
consumer = con
}
} else {
consumer = cons
}
msgs, err := consumer.Fetch(defaultOpts.BatchSize, defaultOpts.Prefetch, ConsumerPartitionKey(defaultOpts.FetchPartitionKey), ConsumerPartitionNumber(defaultOpts.FetchPartitionNumber))
if err != nil {
return nil, err
}
return msgs, nil
}
// ConsumerGroup - consumer group name, default is "".
func FetchConsumerGroup(cg string) FetchOpt {
return func(opts *FetchOpts) error {
opts.ConsumerGroup = cg
return nil
}
}
// PartitionKey - partition key to consume from.
func FetchPartitionKey(partitionKey string) FetchOpt {
return func(opts *FetchOpts) error {
opts.FetchPartitionKey = partitionKey
return nil
}
}
// PartitionNumber - partition number to consume from.
func FetchPartitionNumber(partitionNumber int) FetchOpt {
return func(opts *FetchOpts) error {
opts.FetchPartitionNumber = partitionNumber
return nil
}
}
// BatchSize - pull batch size.
func FetchBatchSize(batchSize int) FetchOpt {
return func(opts *FetchOpts) error {
opts.BatchSize = batchSize
return nil
}
}
// BatchMaxWaitTime - max time to wait between pulls, defauls is 5 seconds.
func FetchBatchMaxWaitTime(batchMaxWaitTime time.Duration) FetchOpt {
return func(opts *FetchOpts) error {
if batchMaxWaitTime < 100*time.Millisecond {
batchMaxWaitTime = 100 * time.Millisecond
}
opts.BatchMaxTimeToWait = batchMaxWaitTime
return nil
}
}
// MaxAckTime - max time for ack a message, in case a message not acked within this time period memphis will resend it.
func FetchMaxAckTime(maxAckTime time.Duration) FetchOpt {
return func(opts *FetchOpts) error {
opts.MaxAckTime = maxAckTime
return nil
}
}
// MaxMsgDeliveries - max number of message deliveries, by default is 2.
func FetchMaxMsgDeliveries(maxMsgDeliveries int) FetchOpt {
return func(opts *FetchOpts) error {
opts.MaxMsgDeliveries = maxMsgDeliveries
return nil
}
}
// Deprecated: will be stopped to be supported after November 1'st, 2023.
// ConsumerGenUniqueSuffix - whether to generate a unique suffix for this consumer.
func FetchConsumerGenUniqueSuffix() FetchOpt {
return func(opts *FetchOpts) error {
log.Printf("Deprecation warning: FetchConsumerGenUniqueSuffix will be stopped to be supported after November 1'st, 2023.")
opts.GenUniqueSuffix = true
return nil
}
}
// FetchConsumerErrorHandler - handler for consumer errors.
func FetchConsumerErrorHandler(ceh ConsumerErrHandler) FetchOpt {
return func(opts *FetchOpts) error {
opts.ErrHandler = ceh
return nil
}
}
// FetchPrefetch - whether to prefetch next batch for consumption
func FetchPrefetch() FetchOpt {
return func(opts *FetchOpts) error {
opts.Prefetch = true
return nil
}
}
func init() {
appId, err := uuid.NewV4()
if err != nil {
applicationId = "unknown"
} else {
applicationId = appId.String()
}
}
func (c *Conn) GetPartitionFromKey(key string, stationName string) (int, error) {
mur3 := murmur3.New32WithSeed(SEED)
_, err := mur3.Write([]byte(key))
if err != nil {
return -1, err
}
PartitionIndex := int(mur3.Sum32()) % len(c.stationPartitions[stationName].PartitionsList)
return c.stationPartitions[stationName].PartitionsList[PartitionIndex], nil
}
func (c *Conn) ValidatePartitionNumber(partitionNumber int, stationName string) error {
if partitionNumber < 0 || partitionNumber > len(c.stationPartitions[stationName].PartitionsList) {
return errors.New("Partition number is out of range")
}
for _, partition := range c.stationPartitions[stationName].PartitionsList {
if partition == partitionNumber {
return nil
}
}
return fmt.Errorf("Partition %v does not exist in station %v", partitionNumber, stationName)
}