这是indexloc提供的服务,不要输入任何密码
Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions cmd/bosun/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ type Cache struct {
lru *lru.Cache
}

// obj is an LRU object tracking data and a corresponding error.
type obj struct {
Val interface{}
Err error
}

func New(MaxEntries int) *Cache {
return &Cache{
lru: lru.New(MaxEntries),
Expand All @@ -34,16 +28,17 @@ func (c *Cache) Get(key string, getFn func() (interface{}, error)) (interface{},
result, ok := c.lru.Get(key)
c.Unlock()
if ok {
res := result.(*obj)
return res.Val, res.Err
return result, nil
}
// our lock only serves to protect the lru.
// we can (and should!) do singleflight requests concurently
return c.g.Do(key, func() (interface{}, error) {
v, err := getFn()
c.Lock()
c.lru.Add(key, &obj{v, err})
c.Unlock()
if err == nil {
c.Lock()
c.lru.Add(key, v)
c.Unlock()
}
return v, err
})
}
26 changes: 19 additions & 7 deletions cmd/bosun/expr/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"bosun.org/cmd/bosun/expr/parse"
"bosun.org/graphite"
"bosun.org/opentsdb"
"bosun.org/slog"
)

func logstashTagQuery(args []parse.Node) (parse.Tags, error) {
Expand Down Expand Up @@ -848,6 +849,8 @@ func timeGraphiteRequest(e *State, T miniprofiler.Timer, req *graphite.Request)
return
}

const tsdbMaxTries = 3

func timeTSDBRequest(e *State, T miniprofiler.Timer, req *opentsdb.Request) (s opentsdb.ResponseSet, err error) {
e.tsdbQueries = append(e.tsdbQueries, *req)
if e.autods > 0 {
Expand All @@ -860,14 +863,23 @@ func timeTSDBRequest(e *State, T miniprofiler.Timer, req *opentsdb.Request) (s o
}
}
b, _ := json.MarshalIndent(req, "", " ")
T.StepCustomTiming("tsdb", "query", string(b), func() {
getFn := func() (interface{}, error) {
return e.tsdbContext.Query(req)
tries := 1
for {
T.StepCustomTiming("tsdb", "query", string(b), func() {
getFn := func() (interface{}, error) {
return e.tsdbContext.Query(req)
}
var val interface{}
val, err = e.cache.Get(string(b), getFn)
s = val.(opentsdb.ResponseSet).Copy()

})
if err == nil || tries == tsdbMaxTries {
break
}
var val interface{}
val, err = e.cache.Get(string(b), getFn)
s = val.(opentsdb.ResponseSet).Copy()
})
slog.Errorf("Error on tsdb query %d: %s", tries, err.Error())
tries++
}
return
}

Expand Down