Skip to content

Commit 76d570f

Browse files
committed
Modify logic to accept potential third account in future
Signed-off-by: Neil Twigg <[email protected]>
1 parent 2a2e60e commit 76d570f

8 files changed

+58
-53
lines changed

server/accounts.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -3686,16 +3686,19 @@ func (s *Server) updateAccountClaimsWithRefresh(a *Account, ac *jwt.AccountClaim
36863686
if a.js != nil {
36873687
// Check whether the account NRG status changed. If it has then we need to notify the
36883688
// Raft groups running on the system so that they can move their subs if needed.
3689-
wantAccountNRG := a.js.accountNRG.Load()
3689+
a.mu.Lock()
3690+
previous := a.js.nrgAccount
36903691
switch strings.ToLower(ac.NRGAccount) {
36913692
case "account":
3692-
wantAccountNRG = true
3693+
a.js.nrgAccount = a.Name
36933694
case "system":
3694-
wantAccountNRG = false
3695+
a.js.nrgAccount = ""
36953696
default:
36963697
s.Errorf("Account claim for %q has invalid value %q for account NRG status", a.Name, ac.NRGAccount)
36973698
}
3698-
if wasAccountNRG := a.js.accountNRG.Swap(wantAccountNRG); wasAccountNRG != wantAccountNRG {
3699+
changed := a.js.nrgAccount != previous
3700+
a.mu.Unlock()
3701+
if changed {
36993702
s.updateNRGAccountStatus()
37003703
}
37013704
}

server/jetstream.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ type jsAccount struct {
174174
lupdate time.Time
175175
utimer *time.Timer
176176

177-
// Should we send NRG traffic inside this account instead of the system account.
178-
accountNRG atomic.Bool
177+
// Which account to send NRG traffic into. Empty string is system account.
178+
nrgAccount string
179179
}
180180

181181
// Track general usage for this account.

server/jetstream_cluster_4_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -2479,7 +2479,7 @@ func TestJetStreamClusterAccountNRG(t *testing.T) {
24792479
}
24802480
time.Sleep(time.Millisecond * 100)
24812481
for _, s := range c.servers {
2482-
s.GlobalAccount().js.accountNRG.Store(false)
2482+
s.GlobalAccount().js.nrgAccount = ""
24832483
s.updateNRGAccountStatus()
24842484
}
24852485

@@ -2512,7 +2512,11 @@ func TestJetStreamClusterAccountNRG(t *testing.T) {
25122512
}
25132513
time.Sleep(time.Millisecond * 100)
25142514
for i, s := range c.servers {
2515-
s.GlobalAccount().js.accountNRG.Store(i == 0)
2515+
if i == 0 {
2516+
s.GlobalAccount().js.nrgAccount = globalAccountName
2517+
} else {
2518+
s.GlobalAccount().js.nrgAccount = ""
2519+
}
25162520
s.updateNRGAccountStatus()
25172521
}
25182522

@@ -2544,7 +2548,7 @@ func TestJetStreamClusterAccountNRG(t *testing.T) {
25442548
}
25452549
time.Sleep(time.Millisecond * 100)
25462550
for _, s := range c.servers {
2547-
s.GlobalAccount().js.accountNRG.Store(true)
2551+
s.GlobalAccount().js.nrgAccount = globalAccountName
25482552
s.updateNRGAccountStatus()
25492553
}
25502554

server/jetstream_jwt_test.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -1586,7 +1586,12 @@ func TestJetStreamJWTClusterAccountNRG(t *testing.T) {
15861586
// Check that everything looks like it should.
15871587
require_True(t, acc != nil)
15881588
require_True(t, acc.js != nil)
1589-
require_Equal(t, acc.js.accountNRG.Load(), state == "account")
1589+
switch state {
1590+
case "account":
1591+
require_Equal(t, acc.js.nrgAccount, aExpPub)
1592+
case "system":
1593+
require_Equal(t, acc.js.nrgAccount, _EMPTY_)
1594+
}
15901595

15911596
// Now get a list of all of the Raft nodes that should
15921597
// have been updated by now.
@@ -1605,10 +1610,14 @@ func TestJetStreamJWTClusterAccountNRG(t *testing.T) {
16051610
// in-account or not.
16061611
for _, rg := range raftNodes {
16071612
rg.Lock()
1608-
inAcc := rg.inAcc
1613+
rgAcc := rg.acc
16091614
rg.Unlock()
1610-
1611-
require_Equal(t, inAcc, state == "account")
1615+
switch state {
1616+
case "account":
1617+
require_Equal(t, rgAcc.Name, aExpPub)
1618+
case "system":
1619+
require_Equal(t, rgAcc.Name, syspub)
1620+
}
16121621
}
16131622
}
16141623
}

server/monitor.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -2802,12 +2802,11 @@ type JSInfo struct {
28022802
Config JetStreamConfig `json:"config,omitempty"`
28032803
Limits *JSLimitOpts `json:"limits,omitempty"`
28042804
JetStreamStats
2805-
Streams int `json:"streams"`
2806-
Consumers int `json:"consumers"`
2807-
Messages uint64 `json:"messages"`
2808-
Bytes uint64 `json:"bytes"`
2809-
Meta *MetaClusterInfo `json:"meta_cluster,omitempty"`
2810-
AccountNRGActive bool `json:"account_nrg_active,omitempty"`
2805+
Streams int `json:"streams"`
2806+
Consumers int `json:"consumers"`
2807+
Messages uint64 `json:"messages"`
2808+
Bytes uint64 `json:"bytes"`
2809+
Meta *MetaClusterInfo `json:"meta_cluster,omitempty"`
28112810

28122811
// aggregate raft info
28132812
AccountDetails []*AccountDetail `json:"account_details,omitempty"`

server/opts.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2135,9 +2135,9 @@ func parseJetStreamForAccount(v any, acc *Account, errors *[]error) error {
21352135
}
21362136
switch strings.ToLower(vv) {
21372137
case "system":
2138-
acc.js.accountNRG.Store(false)
2138+
acc.js.nrgAccount = ""
21392139
case "account":
2140-
acc.js.accountNRG.Store(true)
2140+
acc.js.nrgAccount = acc.Name
21412141
default:
21422142
return &configErr{tk, fmt.Sprintf("Expected either 'system' or 'account' string value for %q, got %v", mk, mv)}
21432143
}

server/raft.go

+22-31
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ type raft struct {
131131
created time.Time // Time that the group was created
132132
accName string // Account name of the asset this raft group is for
133133
acc *Account // Account that NRG traffic will be sent/received in
134-
inAcc bool // Is the NRG traffic in-account right now?
135134
group string // Raft group
136135
sd string // Store directory
137136
id string // Node ID
@@ -530,10 +529,11 @@ func (s *Server) startRaftNode(accName string, cfg *RaftConfig, labels pprofLabe
530529
// Returns whether peers within this group claim to support
531530
// moving NRG traffic into the asset account.
532531
// Lock must be held.
533-
func (n *raft) checkAccountNRGStatus(enabled bool) bool {
534-
if !enabled {
532+
func (n *raft) checkAccountNRGStatus() bool {
533+
if !n.s.accountNRGAllowed.Load() {
535534
return false
536535
}
536+
enabled := true
537537
for pn := range n.peers {
538538
if si, ok := n.s.nodeToInfo.Load(pn); ok && si != nil {
539539
enabled = enabled && si.(nodeInfo).accountNRG
@@ -549,26 +549,31 @@ func (n *raft) RecreateInternalSubs() error {
549549
}
550550

551551
func (n *raft) recreateInternalSubsLocked() error {
552-
// Is account NRG enabled in this account?
553-
acc := n.s.accountNRGAllowed.Load()
554-
if acc {
555-
// Check whether the specific account has account NRG enabled.
552+
// Default is the system account.
553+
nrgAcc := n.s.sys.account
554+
555+
// Is account NRG enabled in this account and do all group
556+
// peers claim to also support account NRG?
557+
if n.checkAccountNRGStatus() {
558+
// Check whether the account that the asset belongs to
559+
// has volunteered a different NRG account.
560+
target := nrgAcc.Name
556561
if a, _ := n.s.lookupAccount(n.accName); a != nil {
557562
a.mu.RLock()
558-
ajs := a.js
563+
if a.js != nil {
564+
target = a.js.nrgAccount
565+
}
559566
a.mu.RUnlock()
560-
// Check whether the specific account has JetStream enabled.
561-
if ajs != nil {
562-
acc = ajs.accountNRG.Load()
567+
}
568+
569+
// If the target account exists, then we'll use that.
570+
if target != _EMPTY_ {
571+
if a, _ := n.s.lookupAccount(target); a != nil {
572+
nrgAcc = a
563573
}
564574
}
565575
}
566-
if acc {
567-
// Check whether the peers in this group all claim to support
568-
// moving the NRG traffic into the account.
569-
acc = n.checkAccountNRGStatus(acc)
570-
}
571-
if n.aesub != nil && n.inAcc == acc {
576+
if n.aesub != nil && n.acc == nrgAcc {
572577
// Subscriptions already exist and the account NRG state
573578
// hasn't changed.
574579
return nil
@@ -594,19 +599,6 @@ func (n *raft) recreateInternalSubsLocked() error {
594599
c.closeConnection(InternalClient)
595600
}
596601

597-
// Look up which account we think we should be participating
598-
// on. This will either be the system account (default) or it
599-
// will be the account that the asset is resident in.
600-
var nrgAcc *Account
601-
if n.s.sys != nil {
602-
nrgAcc = n.s.sys.account
603-
}
604-
if acc { // Should we setup in the asset account?
605-
var err error
606-
if nrgAcc, err = n.s.lookupAccount(n.accName); err != nil {
607-
return err
608-
}
609-
}
610602
if n.acc != nrgAcc {
611603
n.debug("Subscribing in '%s'", nrgAcc.GetName())
612604
}
@@ -619,7 +611,6 @@ func (n *raft) recreateInternalSubsLocked() error {
619611
n.c = c
620612
n.sq = nrgAcc.sq
621613
n.acc = nrgAcc
622-
n.inAcc = acc
623614

624615
// Recreate any internal subscriptions for voting, append
625616
// entries etc in the new account.

server/sendq.go

-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ func (sq *sendq) internalLoop() {
4545

4646
defer s.grWG.Done()
4747

48-
//c := s.createInternalAccountClient()
4948
c := s.createInternalSystemClient()
5049
c.registerWithAccount(sq.a)
5150
c.noIcb = true

0 commit comments

Comments
 (0)