Fix judge get index addrs
This commit is contained in:
parent
1d2e183839
commit
56d1f7b6eb
|
@ -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
|
||||
}
|
|
@ -28,4 +28,6 @@ func Init(cfg SeriesQuerySection) {
|
|||
TransferConnPools = pools.NewConnPools(
|
||||
Config.MaxConn, Config.MaxIdle, Config.ConnTimeout, Config.CallTimeout, address.GetRPCAddresses("transfer"),
|
||||
)
|
||||
|
||||
go GetIndexLoop()
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue