Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions server/jetstream_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -7429,6 +7429,7 @@ func (cc *jetStreamCluster) createGroupForConsumer(cfg *ConsumerConfig, sa *stre
return nil
}

replicas := cfg.replicas(sa.Config)
peers := copyStrings(sa.Group.Peers)
var _ss [5]string
active := _ss[:0]
Expand All @@ -7441,20 +7442,20 @@ func (cc *jetStreamCluster) createGroupForConsumer(cfg *ConsumerConfig, sa *stre
}
}
}
if quorum := cfg.Replicas/2 + 1; quorum > len(active) {
if quorum := replicas/2 + 1; quorum > len(active) {
// Not enough active to satisfy the request.
return nil
}

// If we want less then our parent stream, select from active.
if cfg.Replicas > 0 && cfg.Replicas < len(peers) {
if replicas > 0 && replicas < len(peers) {
// Pedantic in case stream is say R5 and consumer is R3 and 3 or more offline, etc.
if len(active) < cfg.Replicas {
if len(active) < replicas {
return nil
}
// First shuffle the active peers and then select to account for replica = 1.
rand.Shuffle(len(active), func(i, j int) { active[i], active[j] = active[j], active[i] })
peers = active[:cfg.Replicas]
peers = active[:replicas]
}
storage := sa.Config.Storage
if cfg.MemoryStorage {
Expand Down Expand Up @@ -7640,12 +7641,6 @@ func (s *Server) jsClusteredConsumerRequest(ci *ClientInfo, acc *Account, subjec

// We need to set the ephemeral here before replicating.
if !isDurableConsumer(cfg) {
// We chose to have ephemerals be R=1 unless stream is interest or workqueue.
// Consumer can override.
if sa.Config.Retention == LimitsPolicy && cfg.Replicas <= 1 {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this logic now handled?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's handled as part of: replicas := cfg.replicas(sa.Config)

// Calculate accurate replicas for the consumer config with the parent stream config.
func (consCfg ConsumerConfig) replicas(strCfg *StreamConfig) int {
	if consCfg.Replicas == 0 || consCfg.Replicas > strCfg.Replicas {
		if !isDurableConsumer(&consCfg) && strCfg.Retention == LimitsPolicy && consCfg.Replicas == 0 {
			// Matches old-school ephemerals only, where the replica count is 0.
			return 1
		}
		return strCfg.Replicas
	}
	return consCfg.Replicas
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I more meant the selection of the rg.Peers and the name selection for the consumer..

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's already handled by cc.createGroupForConsumer

	// First shuffle the active peers and then select to account for replica = 1.
	rand.Shuffle(len(active), func(i, j int) { active[i], active[j] = active[j], active[i] })
	peers = active[:replicas]
}
...
return &raftGroup{Name: groupNameForConsumer(peers, storage), Storage: storage, Peers: peers}

rg.Peers = []string{rg.Preferred}
rg.Name = groupNameForConsumer(rg.Peers, rg.Storage)
}
if cfg.Name != _EMPTY_ {
oname = cfg.Name
} else {
Expand Down
19 changes: 19 additions & 0 deletions server/jetstream_cluster_1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9026,6 +9026,25 @@ func TestJetStreamClusterCreateR3StreamWithOfflineNodes(t *testing.T) {
})
}

func TestJetStreamClusterCreateEphemeralConsumerWithOfflineNodes(t *testing.T) {
c := createJetStreamClusterExplicit(t, "R3S", 3)
defer c.shutdown()

ml := c.leader()
nc, js := jsClientConnect(t, ml)
defer nc.Close()
_, err := js.AddStream(&nats.StreamConfig{Name: "TEST", Replicas: 3})
require_NoError(t, err)

// Shutdown a random server.
c.randomNonLeader().Shutdown()

for range 10 {
_, err = js.AddConsumer("TEST", &nats.ConsumerConfig{})
require_NoError(t, err)
}
}

func TestJetStreamClusterSetPreferredToOnlineNode(t *testing.T) {
c := createJetStreamClusterExplicit(t, "R3S", 3)
defer c.shutdown()
Expand Down