Skip to content

Commit

Permalink
[IMPROVED] Check consumer leader status without locks. (#5386)
Browse files Browse the repository at this point in the history
Saw TestJetStreamClusterWorkQueueAfterScaleUp locking up, so adjusted
the leader check to the atomic bool.

Signed-off-by: Derek Collison <derek@nats.io>
  • Loading branch information
derekcollison committed May 5, 2024
2 parents 09d3449 + cc52a56 commit 1bd2846
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion server/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5394,9 +5394,14 @@ func (o *consumer) signalSubs() []*subscription {
// but we must check if we are leader.
// We do need the sequence of the message however and we use the msg as the encoded seq.
func (o *consumer) processStreamSignal(_ *subscription, _ *client, _ *Account, subject, _ string, seqb []byte) {
// We can get called here now when not leader, so bail fast
// and without acquiring any locks.
if !o.leader.Load() {
return
}
o.mu.Lock()
defer o.mu.Unlock()
if o.mset == nil || !o.isLeader() {
if o.mset == nil {
return
}

Expand Down
7 changes: 6 additions & 1 deletion server/jetstream_cluster_3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3067,7 +3067,12 @@ func TestJetStreamClusterWorkQueueAfterScaleUp(t *testing.T) {
c.waitOnStreamLeader(globalAccountName, "TEST")

sendStreamMsg(t, nc, "WQ", "SOME WORK")
<-wch

select {
case <-wch:
case <-time.After(5 * time.Second):
t.Fatalf("Did not receive ack signal")
}

checkFor(t, time.Second, 200*time.Millisecond, func() error {
si, err := js.StreamInfo("TEST")
Expand Down

0 comments on commit 1bd2846

Please sign in to comment.