Skip to content

Commit

Permalink
Add hostname's to stolonctl output && Status.go get clusterdata only …
Browse files Browse the repository at this point in the history
…once
  • Loading branch information
deepdivenow committed Oct 6, 2021
1 parent 9b2d75f commit b4a3d5c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 30 deletions.
2 changes: 1 addition & 1 deletion cmd/keeper/cmd/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion cmd/proxy/cmd/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion cmd/sentinel/cmd/sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
46 changes: 25 additions & 21 deletions cmd/stolonctl/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
}
}
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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)
}
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -305,13 +303,18 @@ 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,
Healthy: k.Status.Healthy,
PgHealthy: pgHealthy,
PgWantedGeneration: pgWantedGeneration,
PgCurrentGeneration: pgCurrentGeneration,
Hostname: hostName,
}
keepers = append(keepers, keeper)
}
Expand All @@ -331,5 +334,6 @@ func generateStatus() (Status, error) {
}
status.Cluster = cluster

status.DBs = cd.DBs
return status, nil
}
16 changes: 10 additions & 6 deletions internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit b4a3d5c

Please sign in to comment.