From 87ee275d9593c294bc887536615b80478f9db59f Mon Sep 17 00:00:00 2001 From: Ivan Kozlovic Date: Wed, 25 Sep 2024 17:44:23 -0600 Subject: [PATCH] [FIXED] LeafNode: Don't report cluster name in varz/banner when none defined. 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 --- server/leafnode_test.go | 35 +++++++++++++++++++++++++++++++++++ server/monitor.go | 5 +++++ server/monitor_test.go | 8 ++++++++ server/server.go | 14 +++++++++++++- 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/server/leafnode_test.go b/server/leafnode_test.go index 1408892613b..eeba7737d95 100644 --- a/server/leafnode_test.go +++ b/server/leafnode_test.go @@ -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() +} diff --git a/server/monitor.go b/server/monitor.go index 6ee36614d05..6d7b58721f9 100644 --- a/server/monitor.go +++ b/server/monitor.go @@ -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) } diff --git a/server/monitor_test.go b/server/monitor_test.go index 8be9e7d8b4a..a3c09136167 100644 --- a/server/monitor_test.go +++ b/server/monitor_test.go @@ -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) } diff --git a/server/server.go b/server/server.go index 5b3408c7b5b..31fa376b043 100644 --- a/server/server.go +++ b/server/server.go @@ -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{} @@ -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 } @@ -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)