From 12eb1f5b00764e7e021b25f71a37425473d7cab3 Mon Sep 17 00:00:00 2001 From: Ivan Kozlovic Date: Fri, 25 Oct 2019 16:34:07 -0600 Subject: [PATCH] [ADDED] Server name in the RouteStat for statsz Add the remote server name for a route in the statsz event Signed-off-by: Ivan Kozlovic --- server/events.go | 4 ++++ server/events_test.go | 23 +++++++++++++++++++++++ server/route.go | 3 +++ 3 files changed, 30 insertions(+) diff --git a/server/events.go b/server/events.go index 2c0ad09f..d34c728b 100644 --- a/server/events.go +++ b/server/events.go @@ -165,6 +165,7 @@ type ServerStats struct { // RouteStat holds route statistics. type RouteStat struct { ID uint64 `json:"rid"` + Name string `json:"name,omitempty"` Sent DataStats `json:"sent"` Received DataStats `json:"received"` Pending int `json:"pending"` @@ -413,6 +414,9 @@ func routeStat(r *client) *RouteStat { }, Pending: int(r.out.pb), } + if r.route != nil { + rs.Name = r.route.remoteName + } r.mu.Unlock() return rs } diff --git a/server/events_test.go b/server/events_test.go index e4a2dc20..b48d4943 100644 --- a/server/events_test.go +++ b/server/events_test.go @@ -99,6 +99,7 @@ func runTrustedCluster(t *testing.T) (*Server, *Options, *Server, *Options, nkey optsA.TrustedKeys = []string{pub} optsA.AccountResolver = mr optsA.SystemAccount = apub + optsA.ServerName = "A" // Add in dummy gateway optsA.Gateway.Name = "TEST CLUSTER 22" optsA.Gateway.Host = "127.0.0.1" @@ -108,6 +109,7 @@ func runTrustedCluster(t *testing.T) (*Server, *Options, *Server, *Options, nkey sa := RunServer(optsA) optsB := nextServerOpts(optsA) + optsB.ServerName = "B" optsB.Routes = RoutesFromStr(fmt.Sprintf("nats://%s:%d", optsA.Cluster.Host, optsA.Cluster.Port)) sb := RunServer(optsB) @@ -1322,6 +1324,27 @@ func TestServerEventsStatsZ(t *testing.T) { if lr := len(m3.Stats.Routes); lr != 1 { t.Fatalf("Expected a route, but got %d", lr) } + if sr := m3.Stats.Routes[0]; sr.Name != "B" { + t.Fatalf("Expected server A's route to B to have Name set to %q, got %q", "B", sr.Name) + } + + // Now query B and check that route's name is "A" + subj = fmt.Sprintf(serverStatsReqSubj, sb.ID()) + ncs.SubscribeSync(subj) + msg, err = ncs.Request(subj, nil, time.Second) + if err != nil { + t.Fatalf("Error trying to request statsz: %v", err) + } + m = ServerStatsMsg{} + if err := json.Unmarshal(msg.Data, &m); err != nil { + t.Fatalf("Error unmarshalling the statz json: %v", err) + } + if lr := len(m.Stats.Routes); lr != 1 { + t.Fatalf("Expected a route, but got %d", lr) + } + if sr := m.Stats.Routes[0]; sr.Name != "A" { + t.Fatalf("Expected server B's route to A to have Name set to %q, got %q", "A", sr.Name) + } } func TestServerEventsPingStatsZ(t *testing.T) { diff --git a/server/route.go b/server/route.go index 8b589c75..47750344 100644 --- a/server/route.go +++ b/server/route.go @@ -63,6 +63,7 @@ var testRouteProto = RouteProtoV2 type route struct { remoteID string + remoteName string didSolicit bool retry bool routeType RouteType @@ -411,6 +412,7 @@ func (c *client) processRouteInfo(info *Info) { c.route.authRequired = info.AuthRequired c.route.tlsRequired = info.TLSRequired c.route.gatewayURL = info.GatewayURL + c.route.remoteName = info.Name // When sent through route INFO, if the field is set, it should be of size 1. if len(info.LeafNodeURLs) == 1 { c.route.leafnodeURL = info.LeafNodeURLs[0] @@ -1472,6 +1474,7 @@ func (s *Server) routeAcceptLoop(ch chan struct{}) { tlsReq := opts.Cluster.TLSConfig != nil info := Info{ ID: s.info.ID, + Name: s.info.Name, Version: s.info.Version, GoVersion: runtime.Version(), AuthRequired: false,