From 61cc852807a8ede65b6e82eda24e5ee7ee6b8943 Mon Sep 17 00:00:00 2001 From: spinlock Date: Thu, 15 Dec 2016 12:21:40 +0800 Subject: [PATCH] proxy: fix bug of issue #1078 & #1080 json doesn't support map[int]string, using map[string]string instead --- pkg/proxy/proxy.go | 18 +++++++++++++----- pkg/topom/topom.go | 16 ++++++++++------ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/pkg/proxy/proxy.go b/pkg/proxy/proxy.go index 1f3da1ebd..a94da7594 100644 --- a/pkg/proxy/proxy.go +++ b/pkg/proxy/proxy.go @@ -11,6 +11,7 @@ import ( "os/exec" "path/filepath" "runtime" + "strconv" "strings" "sync" "time" @@ -455,9 +456,9 @@ type Stats struct { Closed bool `json:"closed"` Sentinels struct { - Servers []string `json:"servers,omitempty"` - Masters map[int]string `json:"masters,omitempty"` - Switched bool `json:"switched,omitempty"` + Servers []string `json:"servers,omitempty"` + Masters map[string]string `json:"masters,omitempty"` + Switched bool `json:"switched,omitempty"` } `json:"sentinels"` Ops struct { @@ -552,8 +553,15 @@ func (s *Proxy) Stats(flags StatsFlags) *Stats { stats.Closed = s.IsClosed() servers, masters := s.GetSentinels() - stats.Sentinels.Servers = servers - stats.Sentinels.Masters = masters + if servers != nil { + stats.Sentinels.Servers = servers + } + if masters != nil { + stats.Sentinels.Masters = make(map[string]string) + for gid, addr := range masters { + stats.Sentinels.Masters[strconv.Itoa(gid)] = addr + } + } stats.Sentinels.Switched = s.HasSwitched() stats.Ops.Total = OpTotal() diff --git a/pkg/topom/topom.go b/pkg/topom/topom.go index 5861ce9c9..8aba26eba 100644 --- a/pkg/topom/topom.go +++ b/pkg/topom/topom.go @@ -9,6 +9,7 @@ import ( "net/http" "os" "os/exec" + "strconv" "strings" "sync" "time" @@ -309,8 +310,12 @@ func (s *Topom) Stats() (*Stats, error) { stats.HA.Stats[server] = v } } - stats.HA.Masters = s.ha.masters - + stats.HA.Masters = make(map[string]string) + if s.ha.masters != nil { + for gid, addr := range s.ha.masters { + stats.HA.Masters[strconv.Itoa(gid)] = addr + } + } return stats, nil } @@ -342,10 +347,9 @@ type Stats struct { } `json:"slot_action"` HA struct { - Model *models.Sentinel `json:"model"` - Stats map[string]*RedisStats `json:"stats"` - - Masters map[int]string `json:"masters,omitempty"` + Model *models.Sentinel `json:"model"` + Stats map[string]*RedisStats `json:"stats"` + Masters map[string]string `json:"masters"` } `json:"sentinels"` }