-
Notifications
You must be signed in to change notification settings - Fork 107
/
server.go
923 lines (803 loc) · 26 KB
/
server.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
package server
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"math/rand"
"net"
"os"
"path/filepath"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/casbin/casbin/v2"
"github.com/hashicorp/raft"
client "github.com/liftbridge-io/liftbridge-api/go"
gnatsd "github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
"github.com/nats-io/nuid"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"github.com/liftbridge-io/liftbridge/server/health"
"github.com/liftbridge-io/liftbridge/server/logger"
proto "github.com/liftbridge-io/liftbridge/server/protocol"
)
const stateFile = "liftbridge"
const (
streamsConnName = "streams"
raftConnName = "raft"
replicationConnName = "replication"
acksConnName = "acks"
publishesConnName = "publishes"
activityStream = "__activity"
cursorsStream = "__cursors"
)
// reservedStreams contains reserved internal stream names.
var reservedStreams = []string{activityStream, cursorsStream}
// RaftLog represents an entry into the Raft log.
type RaftLog struct {
*raft.Log
}
// RaftLogListener is a listener for Raft logs.
type RaftLogListener interface {
Receive(*RaftLog)
}
// authzEnforcer contains a casbin enforcer and a lock, which is used to reload permissions safely
type authzEnforcer struct {
enforcer *casbin.Enforcer
authzLock sync.RWMutex
}
// Server is the main Liftbridge object. Create it by calling New or
// RunServerWithConfig.
type Server struct {
config *Config
listener net.Listener
port int
embeddedNATS *gnatsd.Server
nc *nats.Conn
ncRaft *nats.Conn
ncRepl *nats.Conn
ncAcks *nats.Conn
ncPublishes *nats.Conn
logger logger.Logger
grpcServer *grpc.Server
api *apiServer
metadata *metadataAPI
shutdownCh chan struct{}
raftInitialized chan struct{}
raft atomic.Value
leaderSub *nats.Subscription
recoveryStarted bool
latestRecoveredLog *raft.Log
mu sync.RWMutex
shutdown bool
running bool
goroutineWait sync.WaitGroup
activity *activityManager
cursors *cursorManager
raftLogListenersMu sync.RWMutex
raftLogListeners []RaftLogListener
authzEnforcer *authzEnforcer
}
// RunServerWithConfig creates and starts a new Server with the given
// configuration. It returns an error if the Server failed to start.
func RunServerWithConfig(config *Config) (*Server, error) {
server := New(config)
err := server.Start()
return server, err
}
// New creates a new Server with the given configuration. Call Start to run the
// Server.
func New(config *Config) *Server {
// Default data path to /tmp/liftbridge/<namespace> if not set.
if config.DataDir == "" {
config.DataDir = filepath.Join("/tmp", "liftbridge", config.Clustering.Namespace)
}
logger := logger.NewLogger(config.LogLevel)
if config.LogSilent {
logger.Silent(true)
}
s := &Server{
config: config,
logger: logger,
shutdownCh: make(chan struct{}),
raftInitialized: make(chan struct{}),
}
s.metadata = newMetadataAPI(s)
s.activity = newActivityManager(s)
s.cursors = newCursorManager(s)
return s
}
// Start the Server. This is not a blocking call. It will return an error if
// the Server cannot start properly.
func (s *Server) Start() (err error) {
defer func() {
if err != nil {
s.Stop()
}
}()
rand.Seed(time.Now().UnixNano())
// Create the data directory if it doesn't exist.
if err := os.MkdirAll(s.config.DataDir, os.ModePerm); err != nil {
return errors.Wrap(err, "failed to create data path directories")
}
// Recover and persist metadata state.
if err := s.recoverAndPersistState(); err != nil {
return errors.Wrap(err, "failed to recover or persist metadata state")
}
s.logger.Infof("Liftbridge Version: %s", Version)
s.logger.Infof("Server ID: %s", s.config.Clustering.ServerID)
s.logger.Infof("Namespace: %s", s.config.Clustering.Namespace)
s.logger.Infof("NATS Servers: %s", s.config.NATSServersString())
s.logger.Infof("Default Retention Policy: %s", s.config.Streams.RetentionString())
s.logger.Infof("Default Partition Pausing: %s", s.config.Streams.AutoPauseString())
// Start embedded NATS server if configured.
if s.config.EmbeddedNATS {
if err := s.startEmbeddedNATS(); err != nil {
return errors.Wrap(err, "failed to start embedded NATS server")
}
}
if err := s.createNATSConns(); err != nil {
return errors.Wrap(err, "failed to connect to NATS")
}
listenAddress := s.config.GetListenAddress()
hp := net.JoinHostPort(listenAddress.Host, strconv.Itoa(listenAddress.Port))
l, err := net.Listen("tcp", hp)
if err != nil {
return errors.Wrap(err, "failed starting listener")
}
s.listener = l
s.port = l.Addr().(*net.TCPAddr).Port
s.logger.Infof("Starting Liftbridge server on %s...",
net.JoinHostPort(listenAddress.Host, strconv.Itoa(s.port)))
// Set a lower bound of one second for SegmentMaxAge to avoid frequent log
// rolls which will cause performance problems. This is mainly here because
// SegmentMaxAge defaults to RetentionMaxAge if it's not set explicitly,
// so users could otherwise unknowingly cause frequent log rolls.
if logRollTime := s.config.Streams.SegmentMaxAge; logRollTime != 0 && logRollTime < time.Second {
s.logger.Infof("Defaulting %s to 1 second to avoid frequent log rolls", configStreamsSegmentMaxAge)
s.config.Streams.SegmentMaxAge = time.Second
}
raftNode, err := s.setupMetadataRaft()
if err != nil {
return errors.Wrap(err, "failed to start Raft node")
}
if _, err := s.ncRaft.Subscribe(s.getServerInfoInbox(), s.handleServerInfoRequest); err != nil {
return errors.Wrap(err, "failed to subscribe to server info subject")
}
inbox := s.getPartitionStatusInbox(s.config.Clustering.ServerID)
if _, err := s.ncRaft.Subscribe(inbox, s.handlePartitionStatusRequest); err != nil {
return errors.Wrap(err, "failed to subscribe to partition status subject")
}
inbox = s.getPartitionNotificationInbox(s.config.Clustering.ServerID)
if _, err := s.ncRepl.Subscribe(inbox, s.handlePartitionNotification); err != nil {
return errors.Wrap(err, "failed to subscribe to partition notification subject")
}
s.handleSignals()
if err := s.startAPIServer(); err != nil {
return errors.Wrap(err, "failed to start API server")
}
s.startRaftLeadershipLoop(raftNode)
return nil
}
// Stop will attempt to gracefully shut the Server down by signaling the stop
// and waiting for all goroutines to return.
func (s *Server) Stop() error {
health.SetNotServing()
s.mu.Lock()
if s.shutdown {
s.mu.Unlock()
return nil
}
s.logger.Info("Shutting down...")
// Close the raftInitialized channel in case the Raft node was never
// initialized to prevent a deadlock.
select {
case <-s.raftInitialized:
default:
close(s.raftInitialized)
}
close(s.shutdownCh)
if s.grpcServer != nil {
s.grpcServer.Stop()
}
if s.listener != nil {
s.listener.Close()
}
if s.metadata != nil {
if err := s.metadata.Reset(); err != nil {
s.mu.Unlock()
return err
}
}
if raft := s.getRaft(); raft != nil {
if err := raft.shutdown(); err != nil {
s.mu.Unlock()
return err
}
}
s.closeNATSConns()
if s.embeddedNATS != nil {
s.embeddedNATS.Shutdown()
}
s.running = false
s.shutdown = true
s.mu.Unlock()
// Wait for goroutines to stop.
s.goroutineWait.Wait()
return nil
}
// IsLeader indicates if the server is currently the metadata leader or not. If
// consistency is required for an operation, it should be threaded through the
// Raft cluster since that is the single source of truth. If a server thinks
// it's leader when it's not, the operation it proposes to the Raft cluster
// will fail.
func (s *Server) IsLeader() bool {
raft := s.getRaft()
if raft == nil {
panic("Attempted to access Raft node but it was not initialized")
}
return raft.isLeader()
}
// IsRunning indicates if the server is currently running or has been stopped.
func (s *Server) IsRunning() bool {
s.mu.RLock()
defer s.mu.RUnlock()
return s.running
}
// GetListenPort returns the port the server is listening to. Returns 0 if the
// server is not listening.
func (s *Server) GetListenPort() int {
s.mu.RLock()
defer s.mu.RUnlock()
return s.port
}
// AddRaftLogListener adds a Raft log listener.
func (s *Server) AddRaftLogListener(listener RaftLogListener) {
s.raftLogListenersMu.Lock()
defer s.raftLogListenersMu.Unlock()
s.raftLogListeners = append(s.raftLogListeners, listener)
}
// getConnectionAddress returns the connection address that should be used by
// the server. It uses the port the server is currently listening to if the
// connection port is 0, so that an OS-assigned port can be used as a connection
// port.
func (s *Server) getConnectionAddress() HostPort {
s.mu.RLock()
defer s.mu.RUnlock()
address := s.config.GetConnectionAddress()
if address.Port == 0 {
address.Port = s.port
}
return address
}
// recoverAndPersistState recovers any existing server metadata state from disk
// to initialize the server then writes the metadata back to disk.
func (s *Server) recoverAndPersistState() error {
// Attempt to recover state.
file := filepath.Join(s.config.DataDir, stateFile)
data, err := ioutil.ReadFile(file)
if err == nil {
// Recovered previous state.
state := &proto.ServerState{}
if err := state.Unmarshal(data); err == nil {
s.config.Clustering.ServerID = state.ServerID
}
}
// Persist server state.
state := &proto.ServerState{ServerID: s.config.Clustering.ServerID}
data, err = state.Marshal()
if err != nil {
panic(err)
}
return ioutil.WriteFile(file, data, 0666)
}
// startEmbeddedNATS starts a NATS server embedded in this process. It returns
// once the server is ready to accept connections.
func (s *Server) startEmbeddedNATS() error {
opts, err := gnatsd.ProcessConfigFile(s.config.EmbeddedNATSConfig)
if err != nil {
return err
}
s.embeddedNATS, err = gnatsd.NewServer(opts)
if err != nil {
return err
}
s.embeddedNATS.SetLogger(logger.NewNATSLogger(s.logger, s.config.LogNATS),
opts.Debug, opts.Trace)
s.logger.Infof("Starting embedded NATS server on %s",
net.JoinHostPort(opts.Host, strconv.Itoa(opts.Port)))
s.startGoroutine(s.embeddedNATS.Start)
if !s.embeddedNATS.ReadyForConnections(10 * time.Second) {
return errors.New("unable to start embedded NATS server")
}
return nil
}
// createNATSConns creates various NATS connections used by the server,
// including connections for stream data, Raft, replication, acks, and
// publishes.
func (s *Server) createNATSConns() error {
// NATS connection used for stream data.
nc, err := s.createNATSConn(streamsConnName)
if err != nil {
return err
}
s.nc = nc
// NATS connection used for Raft metadata replication.
ncr, err := s.createNATSConn(raftConnName)
if err != nil {
return err
}
s.ncRaft = ncr
// NATS connection used for stream replication.
ncRepl, err := s.createNATSConn(replicationConnName)
if err != nil {
return err
}
s.ncRepl = ncRepl
// NATS connection used for sending acks.
ncAcks, err := s.createNATSConn(acksConnName)
if err != nil {
return err
}
s.ncAcks = ncAcks
// NATS connection used for publishing messages.
ncPublishes, err := s.createNATSConn(publishesConnName)
if err != nil {
return err
}
s.ncPublishes = ncPublishes
return nil
}
// closeNATSConns closes the various NATS connections used by the server,
// including connections for stream data, Raft, replication, acks, and
// publishes.
func (s *Server) closeNATSConns() {
if s.nc != nil {
s.nc.Close()
}
if s.ncRaft != nil {
s.ncRaft.Close()
}
if s.ncRepl != nil {
s.ncRepl.Close()
}
if s.ncAcks != nil {
s.ncAcks.Close()
}
if s.ncPublishes != nil {
s.ncPublishes.Close()
}
}
// startAPIServer configures and starts the gRPC API server.
func (s *Server) startAPIServer() error {
opts := []grpc.ServerOption{}
// Setup TLS if key/cert is set.
if s.config.TLSKey != "" && s.config.TLSCert != "" {
var (
config tls.Config
)
certificate, err := tls.LoadX509KeyPair(s.config.TLSCert, s.config.TLSKey)
if err != nil {
return errors.Wrap(err, "failed to load TLS key pair")
}
config.Certificates = []tls.Certificate{certificate}
// Configure Authentication
if s.config.TLSClientAuth {
config.ClientAuth = tls.RequireAndVerifyClientCert
if s.config.TLSClientAuthCA != "" {
certPool := x509.NewCertPool()
ca, err := ioutil.ReadFile(s.config.TLSClientAuthCA)
if err != nil {
return errors.Wrap(err, "failed to load TLS client ca certificate")
}
if ok := certPool.AppendCertsFromPEM(ca); !ok {
return errors.Wrap(err, "failed to append TLS client certificate")
}
config.ClientCAs = certPool
}
}
// Configure authorization
if s.config.TLSClientAuthz && s.config.TLSClientAuthzModel != "" && s.config.TLSClientAuthzPolicy != "" {
opts = append(opts, grpc.UnaryInterceptor(AuthzUnaryInterceptor), grpc.StreamInterceptor(AuthzStreamInterceptor))
policyEnforcer, err := casbin.NewEnforcer(s.config.TLSClientAuthzModel, s.config.TLSClientAuthzPolicy)
if err != nil {
return errors.Wrap(err, "failed to initialize authorization policy enforcer")
}
err = policyEnforcer.LoadPolicy()
if err != nil {
return errors.Wrap(err, "failed to load authorization permissions")
}
s.authzEnforcer = &authzEnforcer{enforcer: policyEnforcer}
}
creds := credentials.NewTLS(&config)
opts = append(opts, grpc.Creds(creds))
}
grpcServer := grpc.NewServer(opts...)
s.grpcServer = grpcServer
s.api = &apiServer{s}
client.RegisterAPIServer(grpcServer, s.api)
health.Register(grpcServer)
s.mu.Lock()
s.running = true
s.mu.Unlock()
s.startGoroutine(func() {
health.SetServing()
err := grpcServer.Serve(s.listener)
s.mu.Lock()
s.running = false
s.mu.Unlock()
if err != nil {
select {
case <-s.shutdownCh:
return
default:
s.logger.Fatal(err)
}
}
})
return nil
}
// createNATSConn creates a new NATS connection with the given name.
func (s *Server) createNATSConn(name string) (*nats.Conn, error) {
var err error
opts := s.config.NATS
opts.Name = fmt.Sprintf("LIFT.%s.%s.%s", s.config.Clustering.Namespace, s.config.Clustering.ServerID, name)
// Shorten the time we wait to reconnect. Don't make it too short because
// it may exhaust the number of available FDs.
opts.ReconnectWait = 250 * time.Millisecond
// Try to reconnect indefinitely.
opts.MaxReconnect = -1
// Disable buffering in the NATS client to avoid possible duplicate
// deliveries.
opts.ReconnectBufSize = -1
// Set connection handlers.
if err = nats.ErrorHandler(s.natsErrorHandler)(&opts); err != nil {
return nil, err
}
if err = nats.ReconnectHandler(s.natsReconnectedHandler)(&opts); err != nil {
return nil, err
}
if err = nats.ClosedHandler(s.natsClosedHandler)(&opts); err != nil {
return nil, err
}
if err = nats.DisconnectHandler(s.natsDisconnectedHandler)(&opts); err != nil {
return nil, err
}
var conn *nats.Conn
for i := 0; i < 5; i++ {
conn, err = opts.Connect()
if err == nil {
return conn, nil
}
time.Sleep(5 * time.Millisecond)
}
return nil, err
}
// startRaftLeadershipLoop start a goroutine for automatically responding to
// Raft leadership changes.
func (s *Server) startRaftLeadershipLoop(node *raftNode) {
s.startGoroutine(func() {
for {
select {
case isLeader := <-node.notifyCh:
if isLeader {
if err := s.leadershipAcquired(node); err != nil {
s.logger.Errorf("Error on metadata leadership acquired: %v", err)
switch {
case err == raft.ErrRaftShutdown:
// Node shutdown, just return.
return
case err == raft.ErrLeadershipLost:
// Node lost leadership, continue loop.
continue
default:
// Step down as leader.
s.logger.Warn("Stepping down as metadata leader")
if future := node.LeadershipTransfer(); future.Error() != nil {
panic(errors.Wrap(future.Error(), "error on metadata leadership step down"))
}
continue
}
}
} else {
if err := s.leadershipLost(node); err != nil {
s.logger.Errorf("Error on metadata leadership lost: %v", err)
}
}
case <-s.shutdownCh:
return
}
}
})
}
// setRaft sets the Raft node for the server. This should only be called once
// on server start.
func (s *Server) setRaft(r *raftNode) {
s.raft.Store(r)
close(s.raftInitialized)
s.logger.Debug("Raft node initialized")
}
// getRaft returns the Raft node for the server.
func (s *Server) getRaft() *raftNode {
<-s.raftInitialized
r := s.raft.Load()
if r == nil {
s.logger.Warn("Attempted to access Raft node but it was not initialized")
return nil
}
return r.(*raftNode)
}
// leadershipAcquired should be called when this node is elected leader.
func (s *Server) leadershipAcquired(raft *raftNode) error {
s.logger.Infof("Server became metadata leader, performing leader promotion actions")
// Use a barrier to ensure all preceding operations are applied to the FSM.
if err := raft.Barrier(0).Error(); err != nil {
return err
}
// Subscribe to leader NATS subject for propagated requests.
sub, err := s.nc.Subscribe(s.getPropagateInbox(), s.handlePropagatedRequest)
if err != nil {
return err
}
s.leaderSub = sub
if err := s.activity.BecomeLeader(); err != nil {
return err
}
if err := s.cursors.Initialize(); err != nil {
return err
}
raft.setLeader(true)
return nil
}
// leadershipLost should be called when this node loses leadership.
func (s *Server) leadershipLost(raft *raftNode) error {
s.logger.Warn("Server lost metadata leadership, performing leader stepdown actions")
// Unsubscribe from leader NATS subject for propagated requests.
if s.leaderSub != nil {
if err := s.leaderSub.Unsubscribe(); err != nil {
return err
}
s.leaderSub = nil
}
s.metadata.LostLeadership()
if err := s.activity.BecomeFollower(); err != nil {
return err
}
raft.setLeader(false)
return nil
}
func (s *Server) isShutdown() bool {
s.mu.RLock()
defer s.mu.RUnlock()
return s.shutdown
}
// natsDisconnectedHandler fires when the given NATS connection has been
// disconnected. This may indicate a temporary disconnect, in which case the
// client will automatically attempt to reconnect.
func (s *Server) natsDisconnectedHandler(nc *nats.Conn) {
// If the server was shut down, do nothing since this is an expected
// disconnect.
if s.isShutdown() {
return
}
if nc.LastError() != nil {
s.logger.Errorf("Connection %q has been disconnected from NATS: %v",
nc.Opts.Name, nc.LastError())
} else {
s.logger.Errorf("Connection %q has been disconnected from NATS", nc.Opts.Name)
}
}
// natsReconnectedHandler fires when the given NATS connection has successfully
// reconnected.
func (s *Server) natsReconnectedHandler(nc *nats.Conn) {
s.logger.Infof("Connection %q reconnected to NATS at %q",
nc.Opts.Name, nc.ConnectedUrl())
}
// natsClosedHandler fires when the given NATS connection has been closed, i.e.
// permanently disconnected. At this point, the client will not attempt to
// reconnect to NATS.
func (s *Server) natsClosedHandler(nc *nats.Conn) {
// If the server was shut down, do nothing since this is an expected close.
if s.isShutdown() {
return
}
s.logger.Debugf("Connection %q has been closed", nc.Opts.Name)
}
// natsErrorHandler fires when there is an asynchronous error on the NATS
// connection.
func (s *Server) natsErrorHandler(nc *nats.Conn, sub *nats.Subscription, err error) {
var (
msg = "Asynchronous error on NATS connection"
prefix = " "
)
if nc != nil {
msg += fmt.Sprintf(" %s", nc.Opts.Name)
prefix = ", "
}
if sub != nil {
msg += fmt.Sprintf("%ssubject %s", prefix, sub.Subject)
}
msg += fmt.Sprintf(": %s", err)
s.logger.Errorf(msg)
}
// handleServerInfoRequest is a NATS handler used to process requests for
// server information used in the metadata API.
func (s *Server) handleServerInfoRequest(m *nats.Msg) {
req, err := proto.UnmarshalServerInfoRequest(m.Data)
if err != nil {
s.logger.Warnf("Dropping invalid server info request: %v", err)
return
}
// Ignore requests from ourself.
if req.Id == s.config.Clustering.ServerID {
return
}
connectionAddress := s.getConnectionAddress()
data, err := proto.MarshalServerInfoResponse(&proto.ServerInfoResponse{
Id: s.config.Clustering.ServerID,
Host: connectionAddress.Host,
Port: int32(connectionAddress.Port),
})
if err != nil {
panic(err)
}
if err := m.Respond(data); err != nil {
s.logger.Errorf("Failed to respond to server info request: %v", err)
}
}
// handlePartitionStatusRequest is a NATS handler used to process requests
// querying the status of a partition. This is used as a readiness check to
// determine if a created partition has actually started.
func (s *Server) handlePartitionStatusRequest(m *nats.Msg) {
req, err := proto.UnmarshalPartitionStatusRequest(m.Data)
if err != nil {
s.logger.Warnf("Dropping invalid partition status request: %v", err)
return
}
partition := s.metadata.GetPartition(req.Stream, req.Partition)
resp := &proto.PartitionStatusResponse{Exists: partition != nil}
if partition != nil {
resp.IsLeader = partition.IsLeader()
}
data, err := proto.MarshalPartitionStatusResponse(resp)
if err != nil {
panic(err)
}
if err := m.Respond(data); err != nil {
s.logger.Errorf("Failed to respond to partition status request: %v", err)
}
}
// handlePartitionNotification is a NATS handler used to process notifications
// from a leader that new data is available on a partition for the follower to
// replicate if the follower is idle.
//
// When a follower reaches the end of the log, it starts to sleep in between
// replication requests to avoid overloading the leader. However, this causes
// added commit latency when new messages are published to the log since the
// follower is idle. As a result, the leader will note when a follower is
// caught up and send a notification in order to wake an idle follower back up
// when new data is written to the log.
func (s *Server) handlePartitionNotification(m *nats.Msg) {
req, err := proto.UnmarshalPartitionNotification(m.Data)
if err != nil {
s.logger.Warnf("Dropping invalid partition notification: %v", err)
return
}
partition := s.metadata.GetPartition(req.Stream, req.Partition)
if partition == nil {
s.logger.Warnf("Dropping invalid partition notification: no partition %d for stream %s",
req.Partition, req.Stream)
return
}
// Wake the follower up.
partition.Notify()
}
// getServerInfoInbox returns the NATS subject used for handling server
// information requests.
func (s *Server) getServerInfoInbox() string {
return fmt.Sprintf("%s.info", s.baseMetadataRaftSubject())
}
// getPartitionStatusInbox returns the NATS subject used for handling stream
// status requests.
func (s *Server) getPartitionStatusInbox(id string) string {
return fmt.Sprintf("%s.status.%s", s.baseMetadataRaftSubject(), id)
}
// getMetadataReplyInbox returns a random NATS subject to use for metadata
// responses scoped to the cluster namespace.
func (s *Server) getMetadataReplyInbox() string {
return fmt.Sprintf("%s.fetch.%s", s.baseMetadataRaftSubject(), nuid.Next())
}
// getPartitionNotificationInbox returns the NATS subject used for leaders to
// indicate new data is available on a partition for a follower to replicate if
// the follower is idle.
func (s *Server) getPartitionNotificationInbox(id string) string {
return fmt.Sprintf("%s.notify.%s", s.config.Clustering.Namespace, id)
}
// getAckInbox returns a random NATS subject to use for publish acks scoped to
// the cluster namespace.
func (s *Server) getAckInbox() string {
return fmt.Sprintf("%s.ack.%s", s.config.Clustering.Namespace, nuid.Next())
}
// getActivityStreamSubject returns the NATS subject used for publishing
// activity stream events.
func (s *Server) getActivityStreamSubject() string {
return fmt.Sprintf("%s.activity", s.config.Clustering.Namespace)
}
// getCursorStreamSubject returns the NATS subject used for storing consumer
// partition cursors.
func (s *Server) getCursorStreamSubject() string {
return fmt.Sprintf("%s.cursors", s.config.Clustering.Namespace)
}
// startGoroutine starts a goroutine which is managed by the server. This adds
// the goroutine to a WaitGroup so that the server can wait for all running
// goroutines to stop on shutdown. This should be used instead of a "naked"
// goroutine.
func (s *Server) startGoroutine(f func()) {
select {
case <-s.shutdownCh:
return
default:
}
s.goroutineWait.Add(1)
go func() {
f()
s.goroutineWait.Done()
}()
}
// startGoroutineWG starts a goroutine which is managed by the server and calls
// Done() on the provided WaitGroup upon completion. This adds the goroutine to
// a WaitGroup so that the server can wait for all running goroutines to stop
// on shutdown. This should be used instead of a "naked" goroutine.
func (s *Server) startGoroutineWG(f func(), wg sync.WaitGroup) {
select {
case <-s.shutdownCh:
return
default:
}
s.goroutineWait.Add(1)
wg.Add(1)
go func() {
f()
wg.Done()
s.goroutineWait.Done()
}()
}
// startGoroutineWithArgs starts a goroutine which is managed by the server and
// is passed the provided arguments. This adds the goroutine to a WaitGroup so
// that the server can wait for all running goroutines to stop on shutdown.
// This should be used instead of a "naked" goroutine.
func (s *Server) startGoroutineWithArgs(f func(...interface{}), args ...interface{}) {
select {
case <-s.shutdownCh:
return
default:
}
s.goroutineWait.Add(1)
go func() {
f(args...)
s.goroutineWait.Done()
}()
}
// startGoroutineWithArgsWG starts a goroutine which is managed by the server
// and is passed the provided arguments and calls Done() on the provided
// WaitGroup upon completion. This adds the goroutine to a WaitGroup so that
// the server can wait for all running goroutines to stop on shutdown. This
// should be used instead of a "naked" goroutine.
func (s *Server) startGoroutineWithArgsWG(f func(...interface{}), wg sync.WaitGroup, args ...interface{}) {
select {
case <-s.shutdownCh:
return
default:
}
s.goroutineWait.Add(1)
wg.Add(1)
go func() {
f(args...)
wg.Done()
s.goroutineWait.Done()
}()
}