From 56d1f7b6ebca2a93b4faf7c21aa6be463089c5c0 Mon Sep 17 00:00:00 2001 From: 710leo <710leo@gmail.com> Date: Sat, 27 Jun 2020 15:34:49 +0800 Subject: [PATCH] Fix judge get index addrs --- src/modules/judge/backend/query/index.go | 57 ++++++++++++++++++++++++ src/modules/judge/backend/query/init.go | 2 + src/modules/judge/backend/query/query.go | 3 +- 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 src/modules/judge/backend/query/index.go diff --git a/src/modules/judge/backend/query/index.go b/src/modules/judge/backend/query/index.go new file mode 100644 index 00000000..18b4a48d --- /dev/null +++ b/src/modules/judge/backend/query/index.go @@ -0,0 +1,57 @@ +package query + +import ( + "fmt" + "sync" + "time" + + "github.com/didi/nightingale/src/toolkits/report" + "github.com/didi/nightingale/src/toolkits/stats" + + "github.com/toolkits/pkg/logger" +) + +var IndexList IndexAddrs + +type IndexAddrs struct { + sync.RWMutex + Data []string +} + +func (i *IndexAddrs) Set(addrs []string) { + i.Lock() + defer i.Unlock() + i.Data = addrs +} + +func (i *IndexAddrs) Get() []string { + i.RLock() + defer i.RUnlock() + return i.Data +} + +func GetIndexLoop() { + t1 := time.NewTicker(time.Duration(9) * time.Second) + GetIndex() + for { + <-t1.C + GetIndex() + } +} + +func GetIndex() { + instances, err := report.GetAlive("index", "monapi") + if err != nil { + stats.Counter.Set("get.index.err", 1) + logger.Warningf("get index list err:%v", err) + return + } + + activeIndexs := []string{} + for _, instance := range instances { + activeIndexs = append(activeIndexs, fmt.Sprintf("%s:%s", instance.Identity, instance.HTTPPort)) + } + + IndexList.Set(activeIndexs) + return +} diff --git a/src/modules/judge/backend/query/init.go b/src/modules/judge/backend/query/init.go index a11a7d8a..3f12361a 100644 --- a/src/modules/judge/backend/query/init.go +++ b/src/modules/judge/backend/query/init.go @@ -28,4 +28,6 @@ func Init(cfg SeriesQuerySection) { TransferConnPools = pools.NewConnPools( Config.MaxConn, Config.MaxIdle, Config.ConnTimeout, Config.CallTimeout, address.GetRPCAddresses("transfer"), ) + + go GetIndexLoop() } diff --git a/src/modules/judge/backend/query/query.go b/src/modules/judge/backend/query/query.go index 4ef54658..99bbe8de 100644 --- a/src/modules/judge/backend/query/query.go +++ b/src/modules/judge/backend/query/query.go @@ -9,7 +9,6 @@ import ( "github.com/didi/nightingale/src/dataobj" "github.com/didi/nightingale/src/modules/judge/cache" - "github.com/didi/nightingale/src/toolkits/address" "github.com/didi/nightingale/src/toolkits/stats" "github.com/didi/nightingale/src/toolkits/str" @@ -186,7 +185,7 @@ type IndexResp struct { // index的xclude 不支持批量查询, 暂时不做 func Xclude(request *IndexReq) ([]IndexData, error) { - addrs := address.GetHTTPAddresses("index") + addrs := IndexList.Get() if len(addrs) == 0 { return nil, errors.New("empty index addr") } -- GitLab