2019-12-25 19:35:47 +00:00
|
|
|
package metricsql
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
2019-12-25 19:35:47 +00:00
|
|
|
// CompileRegexpAnchored returns compiled regexp `^re$`.
|
|
|
|
func CompileRegexpAnchored(re string) (*regexp.Regexp, error) {
|
2019-05-23 13:14:01 +00:00
|
|
|
reAnchored := "^(?:" + re + ")$"
|
2019-12-25 19:35:47 +00:00
|
|
|
return CompileRegexp(reAnchored)
|
2019-05-23 13:14:01 +00:00
|
|
|
}
|
|
|
|
|
2019-12-25 19:35:47 +00:00
|
|
|
// CompileRegexp returns compile regexp re.
|
|
|
|
func CompileRegexp(re string) (*regexp.Regexp, error) {
|
2019-05-22 21:16:55 +00:00
|
|
|
rcv := regexpCacheV.Get(re)
|
|
|
|
if rcv != nil {
|
|
|
|
return rcv.r, rcv.err
|
|
|
|
}
|
2019-05-23 13:14:01 +00:00
|
|
|
r, err := regexp.Compile(re)
|
2019-05-22 21:16:55 +00:00
|
|
|
rcv = ®expCacheValue{
|
|
|
|
r: r,
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
regexpCacheV.Put(re, rcv)
|
|
|
|
return rcv.r, rcv.err
|
|
|
|
}
|
|
|
|
|
|
|
|
var regexpCacheV = func() *regexpCache {
|
|
|
|
rc := ®expCache{
|
|
|
|
m: make(map[string]*regexpCacheValue),
|
|
|
|
}
|
|
|
|
metrics.NewGauge(`vm_cache_requests_total{type="promql/regexp"}`, func() float64 {
|
|
|
|
return float64(rc.Requests())
|
|
|
|
})
|
|
|
|
metrics.NewGauge(`vm_cache_misses_total{type="promql/regexp"}`, func() float64 {
|
|
|
|
return float64(rc.Misses())
|
|
|
|
})
|
|
|
|
metrics.NewGauge(`vm_cache_entries{type="promql/regexp"}`, func() float64 {
|
|
|
|
return float64(rc.Len())
|
|
|
|
})
|
|
|
|
return rc
|
|
|
|
}()
|
|
|
|
|
|
|
|
const regexpCacheMaxLen = 10e3
|
|
|
|
|
|
|
|
type regexpCacheValue struct {
|
|
|
|
r *regexp.Regexp
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
type regexpCache struct {
|
2019-10-17 15:22:56 +00:00
|
|
|
// Move atomic counters to the top of struct for 8-byte alignment on 32-bit arch.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/212
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
requests uint64
|
|
|
|
misses uint64
|
2019-10-17 15:22:56 +00:00
|
|
|
|
|
|
|
m map[string]*regexpCacheValue
|
|
|
|
mu sync.RWMutex
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *regexpCache) Requests() uint64 {
|
|
|
|
return atomic.LoadUint64(&rc.requests)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *regexpCache) Misses() uint64 {
|
|
|
|
return atomic.LoadUint64(&rc.misses)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *regexpCache) Len() uint64 {
|
|
|
|
rc.mu.RLock()
|
|
|
|
n := len(rc.m)
|
|
|
|
rc.mu.RUnlock()
|
|
|
|
return uint64(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *regexpCache) Get(regexp string) *regexpCacheValue {
|
|
|
|
atomic.AddUint64(&rc.requests, 1)
|
|
|
|
|
|
|
|
rc.mu.RLock()
|
|
|
|
rcv := rc.m[regexp]
|
|
|
|
rc.mu.RUnlock()
|
|
|
|
|
2019-06-20 11:33:38 +00:00
|
|
|
if rcv == nil {
|
2019-05-22 21:16:55 +00:00
|
|
|
atomic.AddUint64(&rc.misses, 1)
|
|
|
|
}
|
|
|
|
return rcv
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *regexpCache) Put(regexp string, rcv *regexpCacheValue) {
|
|
|
|
rc.mu.Lock()
|
|
|
|
overflow := len(rc.m) - regexpCacheMaxLen
|
|
|
|
if overflow > 0 {
|
|
|
|
// Remove 10% of items from the cache.
|
|
|
|
overflow = int(float64(len(rc.m)) * 0.1)
|
|
|
|
for k := range rc.m {
|
|
|
|
delete(rc.m, k)
|
|
|
|
overflow--
|
|
|
|
if overflow <= 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rc.m[regexp] = rcv
|
|
|
|
rc.mu.Unlock()
|
|
|
|
}
|