Skip to content

Commit

Permalink
[FIXED] LeafNode: Don't report cluster name in varz/banner when none …
Browse files Browse the repository at this point in the history
…defined (#5931)

In situation where a Leaf server has no cluster specified, we internally
use the server name as the cluster name. We may need it in the protocol
so that the hub can suppress some messages to avoid duplicates.

We were however still reporting a cluster name in `/varz` and in the
banner on startup. This PR fixes both.

Resolves #5913

Signed-off-by: Ivan Kozlovic [email protected]
  • Loading branch information
derekcollison authored Sep 26, 2024
2 parents 4c21aa3 + 87ee275 commit 74ef475
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
35 changes: 35 additions & 0 deletions server/leafnode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7968,3 +7968,38 @@ func TestLeafNodeServerKickClient(t *testing.T) {
require_True(t, lid != ln.Cid)
require_True(t, ln.Start.After(disconnectTime))
}

func TestLeafNodeBannerNoClusterNameIfNoCluster(t *testing.T) {
u, err := url.Parse("nats://127.0.0.1:1234")
require_NoError(t, err)

opts := DefaultOptions()
opts.ServerName = "LEAF_SERVER"
opts.Cluster.Name = _EMPTY_
opts.Cluster.Port = 0
opts.LeafNode.Remotes = []*RemoteLeafOpts{{URLs: []*url.URL{u}}}
opts.NoLog = false

s, err := NewServer(opts)
require_NoError(t, err)
defer func() {
s.Shutdown()
s.WaitForShutdown()
}()
l := &captureNoticeLogger{}
s.SetLogger(l, false, false)
s.Start()

if !s.ReadyForConnections(time.Second) {
t.Fatal("Server not ready!")
}

l.Lock()
for _, n := range l.notices {
if strings.Contains(n, "Cluster: ") {
l.Unlock()
t.Fatalf("Cluster name should not be displayed, got %q", n)
}
}
l.Unlock()
}
5 changes: 5 additions & 0 deletions server/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,11 @@ func (s *Server) createVarz(pcpu float64, rss int64) *Varz {
TrustedOperatorsJwt: opts.operatorJWT,
TrustedOperatorsClaim: opts.TrustedOperators,
}
// If this is a leaf without cluster, reset the cluster name (that is otherwise
// set to the server name).
if s.leafNoCluster {
varz.Cluster.Name = _EMPTY_
}
if len(opts.Routes) > 0 {
varz.Cluster.URLs = urlsToStrings(opts.Routes)
}
Expand Down
8 changes: 8 additions & 0 deletions server/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3138,6 +3138,14 @@ func TestMonitorLeafNode(t *testing.T) {
for mode := 0; mode < 2; mode++ {
check := func(t *testing.T, v *Varz) {
t.Helper()
// Issue 5913. When we have solicited leafnodes but no clustering
// and no clustername, we may need a stable clustername so we use
// the server name as cluster name. However, we should not expose
// it in /varz.
if v.Cluster.Name != _EMPTY_ {
t.Fatalf("mode=%v - unexpected cluster name: %s", mode, v.Cluster.Name)
}
// Check rest is as expected.
if !reflect.DeepEqual(v.LeafNode, expected) {
t.Fatalf("mode=%v - expected %+v, got %+v", mode, expected, v.LeafNode)
}
Expand Down
14 changes: 13 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ type Server struct {
leafRemoteAccounts sync.Map
leafNodeEnabled bool
leafDisableConnect bool // Used in test only
leafNoCluster bool // Indicate that this server has only remotes and no cluster defined

quitCh chan struct{}
startupComplete chan struct{}
Expand Down Expand Up @@ -750,6 +751,7 @@ func NewServer(opts *Options) (*Server, error) {
// If we have solicited leafnodes but no clustering and no clustername.
// However we may need a stable clustername so use the server name.
if len(opts.LeafNode.Remotes) > 0 && opts.Cluster.Port == 0 && opts.Cluster.Name == _EMPTY_ {
s.leafNoCluster = true
opts.Cluster.Name = opts.ServerName
}

Expand Down Expand Up @@ -2178,7 +2180,17 @@ func (s *Server) Start() {

// Snapshot server options.
opts := s.getOpts()
clusterName := s.ClusterName()

// Capture if this server is a leaf that has no cluster, so we don't
// display the cluster name if that is the case.
s.mu.RLock()
leafNoCluster := s.leafNoCluster
s.mu.RUnlock()

var clusterName string
if !leafNoCluster {
clusterName = s.ClusterName()
}

s.Noticef(" Version: %s", VERSION)
s.Noticef(" Git: [%s]", gc)
Expand Down

0 comments on commit 74ef475

Please sign in to comment.