From b4a3d5c53c8e61370079401cbf64b0f709c74acf Mon Sep 17 00:00:00 2001 From: Andrey Kiskyak Date: Wed, 6 Oct 2021 14:08:38 +0300 Subject: [PATCH] Add hostname's to stolonctl output && Status.go get clusterdata only once --- cmd/keeper/cmd/keeper.go | 2 +- cmd/proxy/cmd/proxy.go | 2 +- cmd/sentinel/cmd/sentinel.go | 2 +- cmd/stolonctl/cmd/status.go | 46 ++++++++++++++++++++---------------- internal/common/common.go | 16 ++++++++----- 5 files changed, 38 insertions(+), 30 deletions(-) diff --git a/cmd/keeper/cmd/keeper.go b/cmd/keeper/cmd/keeper.go index d56ae06be..55ee0c54e 100644 --- a/cmd/keeper/cmd/keeper.go +++ b/cmd/keeper/cmd/keeper.go @@ -606,7 +606,7 @@ func (p *PostgresKeeper) updateKeeperInfo() error { UID: keeperUID, ClusterUID: clusterUID, BootUUID: p.bootUUID, - Hostname: common.GetHostname(), + Hostname: common.Hostname.Get(), PostgresBinaryVersion: cluster.PostgresBinaryVersion{ Maj: maj, Min: min, diff --git a/cmd/proxy/cmd/proxy.go b/cmd/proxy/cmd/proxy.go index 8d959408a..27cddf320 100644 --- a/cmd/proxy/cmd/proxy.go +++ b/cmd/proxy/cmd/proxy.go @@ -176,7 +176,7 @@ func (c *ClusterChecker) SetProxyInfo(e store.Store, generation int64, proxyTime InfoUID: common.UID(), UID: c.uid, Generation: generation, - Hostname: common.GetHostname(), + Hostname: common.Hostname.Get(), ProxyTimeout: proxyTimeout, } log.Debugf("proxyInfo dump: %s", spew.Sdump(proxyInfo)) diff --git a/cmd/sentinel/cmd/sentinel.go b/cmd/sentinel/cmd/sentinel.go index afe2c452b..868fb1c68 100644 --- a/cmd/sentinel/cmd/sentinel.go +++ b/cmd/sentinel/cmd/sentinel.go @@ -130,7 +130,7 @@ func (s *Sentinel) syncRepl(spec *cluster.ClusterSpec) bool { func (s *Sentinel) setSentinelInfo(ctx context.Context, ttl time.Duration) error { sentinelInfo := &cluster.SentinelInfo{ UID: s.uid, - Hostname: common.GetHostname(), + Hostname: common.Hostname.Get(), } log.Debugw("sentinelInfo dump", "sentinelInfo", sentinelInfo) diff --git a/cmd/stolonctl/cmd/status.go b/cmd/stolonctl/cmd/status.go index 0073de39f..c52a24535 100644 --- a/cmd/stolonctl/cmd/status.go +++ b/cmd/stolonctl/cmd/status.go @@ -51,6 +51,7 @@ type Status struct { Proxies []ProxyStatus `json:"proxies"` Keepers []KeeperStatus `json:"keepers"` Cluster ClusterStatus `json:"cluster"` + DBs cluster.DBs `json:"-"` } type SentinelStatus struct { @@ -72,6 +73,7 @@ type KeeperStatus struct { PgHealthy bool `json:"pg_healthy"` PgWantedGeneration int64 `json:"pg_wanted_generation"` PgCurrentGeneration int64 `json:"pg_current_generation"` + Hostname string `json:"hostname"` } type ClusterStatus struct { @@ -150,9 +152,9 @@ func renderText(status Status, generateErr error) { stdout("No keepers available") stdout("") } else { - fmt.Fprintf(tabOut, "UID\tHEALTHY\tPG LISTENADDRESS\tPG HEALTHY\tPG WANTEDGENERATION\tPG CURRENTGENERATION\n") + fmt.Fprintf(tabOut, "UID\tHEALTHY\tPG LISTENADDRESS\tPG HEALTHY\tPG WANTEDGENERATION\tPG CURRENTGENERATION\tHOSTNAME\n") for _, k := range status.Keepers { - fmt.Fprintf(tabOut, "%s\t%t\t%s\t%t\t%d\t%d\t\n", k.UID, k.Healthy, k.ListenAddress, k.PgHealthy, k.PgWantedGeneration, k.PgCurrentGeneration) + fmt.Fprintf(tabOut, "%s\t%t\t%s\t%t\t%d\t%d\t%s\n", k.UID, k.Healthy, k.ListenAddress, k.PgHealthy, k.PgWantedGeneration, k.PgCurrentGeneration,k.Hostname) tabOut.Flush() } } @@ -170,29 +172,20 @@ func renderText(status Status, generateErr error) { } } - // This tree data isn't currently available in the Status struct - e, err := cmdcommon.NewStore(&cfg.CommonConfig) - if err != nil { - die("%v", err) - } - cd, _, err := getClusterData(e) - if err != nil { - die("%v", err) - } if status.Cluster.MasterDBUID != "" { stdout("") stdout("===== Keepers/DB tree =====") stdout("") - printTree(status.Cluster.MasterDBUID, cd, 0, "", true) + printTree(status.Cluster.MasterDBUID, status, 0, "", true) } stdout("") } -func printTree(dbuid string, cd *cluster.ClusterData, level int, prefix string, tail bool) { +func printTree(dbuid string, status Status, level int, prefix string, tail bool) { // skip not existing db: specified as a follower but not available in the // cluster spec (this should happen only when doing a stolonctl // removekeeper) - if _, ok := cd.DBs[dbuid]; !ok { + if _, ok := status.DBs[dbuid]; !ok { return } out := prefix @@ -203,12 +196,12 @@ func printTree(dbuid string, cd *cluster.ClusterData, level int, prefix string, out += "├─" } } - out += cd.DBs[dbuid].Spec.KeeperUID - if dbuid == cd.Cluster.Status.Master { + out += status.DBs[dbuid].Spec.KeeperUID + if dbuid == status.Cluster.MasterDBUID { out += " (master)" } stdout(out) - db := cd.DBs[dbuid] + db := status.DBs[dbuid] followers := db.Spec.Followers c := len(followers) for i, f := range followers { @@ -219,15 +212,15 @@ func printTree(dbuid string, cd *cluster.ClusterData, level int, prefix string, linespace := "│ " if i < c-1 { if tail { - printTree(f, cd, level+1, prefix+emptyspace, false) + printTree(f, status, level+1, prefix+emptyspace, false) } else { - printTree(f, cd, level+1, prefix+linespace, false) + printTree(f, status, level+1, prefix+linespace, false) } } else { if tail { - printTree(f, cd, level+1, prefix+emptyspace, true) + printTree(f, status, level+1, prefix+emptyspace, true) } else { - printTree(f, cd, level+1, prefix+linespace, true) + printTree(f, status, level+1, prefix+linespace, true) } } } @@ -284,6 +277,11 @@ func generateStatus() (Status, error) { return status, err } + keeInfo, err := e.GetKeepersInfo(context.TODO()) + if err != nil { + keeInfo = map[string]*cluster.KeeperInfo{} + } + keepers := make([]KeeperStatus, 0) kssKeys := cd.Keepers.SortedKeys() for _, kuid := range kssKeys { @@ -305,6 +303,10 @@ func generateStatus() (Status, error) { dbListenAddress = fmt.Sprintf("%s:%s", db.Status.ListenAddress, db.Status.Port) } } + hostName:="" + if ki,ok := keeInfo[kuid]; ok && ki != nil { + hostName=ki.Hostname + } keeper := KeeperStatus{ UID: kuid, ListenAddress: dbListenAddress, @@ -312,6 +314,7 @@ func generateStatus() (Status, error) { PgHealthy: pgHealthy, PgWantedGeneration: pgWantedGeneration, PgCurrentGeneration: pgCurrentGeneration, + Hostname: hostName, } keepers = append(keepers, keeper) } @@ -331,5 +334,6 @@ func generateStatus() (Status, error) { } status.Cluster = cluster + status.DBs = cd.DBs return status, nil } diff --git a/internal/common/common.go b/internal/common/common.go index 78fc0fcb8..14cbd9738 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -140,17 +140,21 @@ func WriteFileAtomic(filename string, perm os.FileMode, data []byte) error { } var ( + Hostname hostNameT +) + +type hostNameT struct { once sync.Once hostname string -) +} -func GetHostname() string { - once.Do(func() { +func (h *hostNameT)Get() string { + h.once.Do(func() { var err error - hostname, err = os.Hostname() + h.hostname, err = os.Hostname() if err != nil { - hostname = "" + h.hostname = "" } }) - return hostname + return h.hostname }