2019-05-22 21:16:55 +00:00
package netstorage
import (
"container/heap"
2020-08-10 10:17:12 +00:00
"errors"
2019-05-22 21:16:55 +00:00
"flag"
"fmt"
2022-07-25 06:12:42 +00:00
"math/rand"
2020-11-16 01:58:12 +00:00
"regexp"
2019-05-22 21:16:55 +00:00
"sort"
"sync"
2020-09-26 01:29:45 +00:00
"sync/atomic"
2021-03-16 23:12:28 +00:00
"time"
2019-05-22 21:16:55 +00:00
2020-09-11 10:18:57 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmselect/searchutils"
2019-05-22 21:16:55 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmstorage"
2020-09-23 11:26:39 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmstorage/promdb"
2019-05-22 21:16:55 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
2020-12-08 18:49:32 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/cgroup"
2020-06-24 16:36:55 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
2020-07-22 11:53:54 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
2022-05-31 23:29:19 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/querytracer"
2019-05-22 21:16:55 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
"github.com/VictoriaMetrics/metrics"
2021-07-15 21:34:33 +00:00
"github.com/valyala/fastrand"
2019-05-22 21:16:55 +00:00
)
var (
2022-07-05 21:31:41 +00:00
maxTagKeysPerSearch = flag . Int ( "search.maxTagKeys" , 100e3 , "The maximum number of tag keys returned from /api/v1/labels" )
maxTagValuesPerSearch = flag . Int ( "search.maxTagValues" , 100e3 , "The maximum number of tag values returned from /api/v1/label/<label_name>/values" )
maxSamplesPerSeries = flag . Int ( "search.maxSamplesPerSeries" , 30e6 , "The maximum number of raw samples a single query can scan per each time series. This option allows limiting memory usage" )
maxSamplesPerQuery = flag . Int ( "search.maxSamplesPerQuery" , 1e9 , "The maximum number of raw samples a single query can process across all time series. This protects from heavy queries, which select unexpectedly high number of raw samples. See also -search.maxSamplesPerSeries" )
2019-05-22 21:16:55 +00:00
)
// Result is a single timeseries result.
//
// ProcessSearchQuery returns Result slice.
type Result struct {
// The name of the metric.
MetricName storage . MetricName
// Values are sorted by Timestamps.
Values [ ] float64
Timestamps [ ] int64
}
func ( r * Result ) reset ( ) {
r . MetricName . Reset ( )
r . Values = r . Values [ : 0 ]
r . Timestamps = r . Timestamps [ : 0 ]
}
// Results holds results returned from ProcessSearchQuery.
type Results struct {
2022-06-28 09:55:20 +00:00
tr storage . TimeRange
deadline searchutils . Deadline
2019-05-22 21:16:55 +00:00
packedTimeseries [ ] packedTimeseries
2020-04-27 05:13:41 +00:00
sr * storage . Search
2020-11-04 14:46:10 +00:00
tbf * tmpBlocksFile
2019-05-22 21:16:55 +00:00
}
// Len returns the number of results in rss.
func ( rss * Results ) Len ( ) int {
return len ( rss . packedTimeseries )
}
// Cancel cancels rss work.
func ( rss * Results ) Cancel ( ) {
2020-04-27 05:13:41 +00:00
rss . mustClose ( )
}
func ( rss * Results ) mustClose ( ) {
putStorageSearch ( rss . sr )
rss . sr = nil
2020-11-04 14:46:10 +00:00
putTmpBlocksFile ( rss . tbf )
rss . tbf = nil
2019-05-22 21:16:55 +00:00
}
2020-06-23 17:29:19 +00:00
type timeseriesWork struct {
2021-03-30 10:22:21 +00:00
mustStop * uint32
2020-09-27 20:17:14 +00:00
rss * Results
pts * packedTimeseries
f func ( rs * Result , workerID uint ) error
2022-07-25 06:12:42 +00:00
err error
2020-06-23 17:29:19 +00:00
rowsProcessed int
}
2021-02-16 14:08:37 +00:00
func ( tsw * timeseriesWork ) reset ( ) {
2021-03-30 10:22:21 +00:00
tsw . mustStop = nil
2021-02-16 14:08:37 +00:00
tsw . rss = nil
tsw . pts = nil
tsw . f = nil
2022-07-25 06:12:42 +00:00
tsw . err = nil
2021-02-16 14:08:37 +00:00
tsw . rowsProcessed = 0
}
func getTimeseriesWork ( ) * timeseriesWork {
v := tswPool . Get ( )
if v == nil {
2022-07-25 06:12:42 +00:00
v = & timeseriesWork { }
2021-02-16 14:08:37 +00:00
}
return v . ( * timeseriesWork )
}
func putTimeseriesWork ( tsw * timeseriesWork ) {
tsw . reset ( )
tswPool . Put ( tsw )
}
var tswPool sync . Pool
2021-07-30 09:02:09 +00:00
func ( tsw * timeseriesWork ) do ( r * Result , workerID uint ) error {
if atomic . LoadUint32 ( tsw . mustStop ) != 0 {
return nil
}
rss := tsw . rss
if rss . deadline . Exceeded ( ) {
atomic . StoreUint32 ( tsw . mustStop , 1 )
return fmt . Errorf ( "timeout exceeded during query execution: %s" , rss . deadline . String ( ) )
}
2022-06-28 09:55:20 +00:00
if err := tsw . pts . Unpack ( r , rss . tbf , rss . tr ) ; err != nil {
2021-07-30 09:02:09 +00:00
atomic . StoreUint32 ( tsw . mustStop , 1 )
return fmt . Errorf ( "error during time series unpacking: %w" , err )
}
2022-07-29 21:38:54 +00:00
tsw . rowsProcessed = len ( r . Timestamps )
2022-06-28 09:55:20 +00:00
if len ( r . Timestamps ) > 0 {
2021-07-30 09:02:09 +00:00
if err := tsw . f ( r , workerID ) ; err != nil {
2021-03-30 10:22:21 +00:00
atomic . StoreUint32 ( tsw . mustStop , 1 )
2021-07-30 09:02:09 +00:00
return err
2020-06-23 17:29:19 +00:00
}
}
2021-07-30 09:02:09 +00:00
return nil
2020-06-23 17:29:19 +00:00
}
2022-07-25 06:12:42 +00:00
func timeseriesWorker ( tsws [ ] * timeseriesWork , workerID uint ) {
2021-07-26 12:38:51 +00:00
v := resultPool . Get ( )
if v == nil {
v = & result { }
}
r := v . ( * result )
2022-07-25 06:12:42 +00:00
for _ , tsw := range tsws {
2021-07-30 09:02:09 +00:00
err := tsw . do ( & r . rs , workerID )
2022-07-25 06:12:42 +00:00
tsw . err = err
2021-07-30 09:02:09 +00:00
}
currentTime := fasttime . UnixTimestamp ( )
if cap ( r . rs . Values ) > 1024 * 1024 && 4 * len ( r . rs . Values ) < cap ( r . rs . Values ) && currentTime - r . lastResetTime > 10 {
// Reset r.rs in order to preseve memory usage after processing big time series with millions of rows.
r . rs = Result { }
r . lastResetTime = currentTime
2020-06-23 17:29:19 +00:00
}
2021-07-26 12:38:51 +00:00
resultPool . Put ( r )
}
type result struct {
rs Result
lastResetTime uint64
2020-06-23 17:29:19 +00:00
}
2021-07-26 12:38:51 +00:00
var resultPool sync . Pool
2020-07-23 16:21:49 +00:00
// RunParallel runs f in parallel for all the results from rss.
2019-05-22 21:16:55 +00:00
//
// f shouldn't hold references to rs after returning.
2019-07-12 12:51:02 +00:00
// workerID is the id of the worker goroutine that calls f.
2020-09-27 20:17:14 +00:00
// Data processing is immediately stopped if f returns non-nil error.
2019-05-22 21:16:55 +00:00
//
// rss becomes unusable after the call to RunParallel.
2022-05-31 23:29:19 +00:00
func ( rss * Results ) RunParallel ( qt * querytracer . Tracer , f func ( rs * Result , workerID uint ) error ) error {
2022-06-08 18:05:17 +00:00
qt = qt . NewChild ( "parallel process of fetched data" )
2020-04-27 05:13:41 +00:00
defer rss . mustClose ( )
2019-05-22 21:16:55 +00:00
2022-07-25 06:12:42 +00:00
// Prepare work for workers.
2020-06-23 17:29:19 +00:00
tsws := make ( [ ] * timeseriesWork , len ( rss . packedTimeseries ) )
2021-03-30 10:22:21 +00:00
var mustStop uint32
2019-05-22 21:16:55 +00:00
for i := range rss . packedTimeseries {
2021-02-16 14:08:37 +00:00
tsw := getTimeseriesWork ( )
tsw . rss = rss
tsw . pts = & rss . packedTimeseries [ i ]
tsw . f = f
2021-03-30 10:22:21 +00:00
tsw . mustStop = & mustStop
2020-06-23 17:29:19 +00:00
tsws [ i ] = tsw
2019-05-22 21:16:55 +00:00
}
2022-07-25 06:12:42 +00:00
// Shuffle tsws for providing the equal amount of work among workers.
2022-07-29 21:29:46 +00:00
r := getRand ( )
2022-07-25 06:12:42 +00:00
r . Shuffle ( len ( tsws ) , func ( i , j int ) {
tsws [ i ] , tsws [ j ] = tsws [ j ] , tsws [ i ]
} )
2022-07-29 21:29:46 +00:00
putRand ( r )
2022-07-25 06:12:42 +00:00
// Spin up up to gomaxprocs local workers and split work equally among them.
// This guarantees linear scalability with the increase of gomaxprocs
// (e.g. the number of available CPU cores).
itemsPerWorker := 1
2022-08-04 15:26:27 +00:00
if len ( rss . packedTimeseries ) > gomaxprocs {
itemsPerWorker = 1 + len ( rss . packedTimeseries ) / gomaxprocs
2022-07-25 06:12:42 +00:00
}
var start int
var i uint
var wg sync . WaitGroup
for start < len ( tsws ) {
end := start + itemsPerWorker
if end > len ( tsws ) {
end = len ( tsws )
}
chunk := tsws [ start : end ]
wg . Add ( 1 )
go func ( tswsChunk [ ] * timeseriesWork , workerID uint ) {
defer wg . Done ( )
timeseriesWorker ( tswsChunk , workerID )
} ( chunk , i )
start = end
i ++
}
2019-05-22 21:16:55 +00:00
2020-06-23 17:29:19 +00:00
// Wait until work is complete.
2022-07-25 06:12:42 +00:00
wg . Wait ( )
// Collect results.
2020-06-23 17:29:19 +00:00
var firstErr error
rowsProcessedTotal := 0
for _ , tsw := range tsws {
2022-07-25 06:12:42 +00:00
if err := tsw . err ; err != nil && firstErr == nil {
2021-03-30 10:22:21 +00:00
// Return just the first error, since other errors are likely duplicate the first error.
2020-06-23 17:29:19 +00:00
firstErr = err
2019-05-22 21:16:55 +00:00
}
2022-06-28 17:18:08 +00:00
rowsReadPerSeries . Update ( float64 ( tsw . rowsProcessed ) )
2020-06-23 17:29:19 +00:00
rowsProcessedTotal += tsw . rowsProcessed
2021-02-16 14:08:37 +00:00
putTimeseriesWork ( tsw )
2019-05-22 21:16:55 +00:00
}
2020-06-23 17:29:19 +00:00
2022-07-25 06:12:42 +00:00
seriesProcessedTotal := len ( rss . packedTimeseries )
rss . packedTimeseries = rss . packedTimeseries [ : 0 ]
2022-06-28 17:18:08 +00:00
rowsReadPerQuery . Update ( float64 ( rowsProcessedTotal ) )
seriesReadPerQuery . Update ( float64 ( seriesProcessedTotal ) )
2021-07-26 12:38:51 +00:00
2022-06-08 18:05:17 +00:00
qt . Donef ( "series=%d, samples=%d" , seriesProcessedTotal , rowsProcessedTotal )
2021-07-26 12:38:51 +00:00
2020-06-23 17:29:19 +00:00
return firstErr
2019-05-22 21:16:55 +00:00
}
2022-07-29 21:29:46 +00:00
var randPool sync . Pool
func getRand ( ) * rand . Rand {
v := randPool . Get ( )
if v == nil {
v = rand . New ( rand . NewSource ( int64 ( fasttime . UnixTimestamp ( ) ) ) )
}
return v . ( * rand . Rand )
}
func putRand ( r * rand . Rand ) {
randPool . Put ( r )
}
2022-06-28 17:18:08 +00:00
var (
rowsReadPerSeries = metrics . NewHistogram ( ` vm_rows_read_per_series ` )
rowsReadPerQuery = metrics . NewHistogram ( ` vm_rows_read_per_query ` )
seriesReadPerQuery = metrics . NewHistogram ( ` vm_series_read_per_query ` )
)
2019-11-23 11:22:55 +00:00
2020-12-08 18:49:32 +00:00
var gomaxprocs = cgroup . AvailableCPUs ( )
2019-05-22 21:16:55 +00:00
type packedTimeseries struct {
metricName string
2020-11-04 14:46:10 +00:00
brs [ ] blockRef
2020-09-23 11:26:39 +00:00
pd * promData
}
type promData struct {
values [ ] float64
timestamps [ ] int64
2019-05-22 21:16:55 +00:00
}
2020-08-06 14:42:15 +00:00
type unpackWorkItem struct {
2020-11-04 14:46:10 +00:00
br blockRef
2020-08-06 14:42:15 +00:00
tr storage . TimeRange
}
2020-06-23 17:29:19 +00:00
type unpackWork struct {
2020-11-04 14:46:10 +00:00
tbf * tmpBlocksFile
2020-09-24 17:16:19 +00:00
ws [ ] unpackWorkItem
sbs [ ] * sortBlock
doneCh chan error
2020-06-23 17:29:19 +00:00
}
2019-05-22 21:16:55 +00:00
2020-07-22 11:53:54 +00:00
func ( upw * unpackWork ) reset ( ) {
2020-11-04 14:46:10 +00:00
upw . tbf = nil
2020-08-06 14:42:15 +00:00
ws := upw . ws
for i := range ws {
w := & ws [ i ]
2020-11-04 14:46:10 +00:00
w . br = blockRef { }
2020-08-06 14:42:15 +00:00
w . tr = storage . TimeRange { }
}
upw . ws = upw . ws [ : 0 ]
sbs := upw . sbs
for i := range sbs {
sbs [ i ] = nil
}
upw . sbs = upw . sbs [ : 0 ]
2020-07-22 11:53:54 +00:00
if n := len ( upw . doneCh ) ; n > 0 {
logger . Panicf ( "BUG: upw.doneCh must be empty; it contains %d items now" , n )
}
}
2020-09-15 18:06:04 +00:00
func ( upw * unpackWork ) unpack ( tmpBlock * storage . Block ) {
2020-08-06 14:42:15 +00:00
for _ , w := range upw . ws {
sb := getSortBlock ( )
2020-11-04 14:46:10 +00:00
if err := sb . unpackFrom ( tmpBlock , upw . tbf , w . br , w . tr ) ; err != nil {
2020-08-06 14:42:15 +00:00
putSortBlock ( sb )
upw . doneCh <- fmt . Errorf ( "cannot unpack block: %w" , err )
return
}
upw . sbs = append ( upw . sbs , sb )
}
upw . doneCh <- nil
}
2020-07-22 11:53:54 +00:00
func getUnpackWork ( ) * unpackWork {
v := unpackWorkPool . Get ( )
if v != nil {
return v . ( * unpackWork )
}
return & unpackWork {
doneCh : make ( chan error , 1 ) ,
}
}
func putUnpackWork ( upw * unpackWork ) {
upw . reset ( )
unpackWorkPool . Put ( upw )
}
var unpackWorkPool sync . Pool
2021-07-30 09:02:09 +00:00
func scheduleUnpackWork ( workChs [ ] chan * unpackWork , uw * unpackWork ) {
if len ( workChs ) == 1 {
// Fast path for a single worker
workChs [ 0 ] <- uw
2021-07-15 21:34:33 +00:00
return
}
2021-07-15 12:40:41 +00:00
attempts := 0
for {
2021-07-30 09:02:09 +00:00
idx := fastrand . Uint32n ( uint32 ( len ( workChs ) ) )
2021-07-15 12:40:41 +00:00
select {
2021-07-30 09:02:09 +00:00
case workChs [ idx ] <- uw :
2021-07-15 12:40:41 +00:00
return
default :
attempts ++
2021-07-30 09:02:09 +00:00
if attempts >= len ( workChs ) {
workChs [ idx ] <- uw
2021-07-15 12:40:41 +00:00
return
}
}
2019-05-22 21:16:55 +00:00
}
2020-06-23 17:29:19 +00:00
}
2021-07-15 12:40:41 +00:00
func unpackWorker ( ch <- chan * unpackWork ) {
2021-07-30 09:02:09 +00:00
v := tmpBlockPool . Get ( )
if v == nil {
v = & storage . Block { }
2019-05-22 21:16:55 +00:00
}
2021-07-30 09:02:09 +00:00
tmpBlock := v . ( * storage . Block )
2021-07-15 12:40:41 +00:00
for upw := range ch {
2021-07-30 09:02:09 +00:00
upw . unpack ( tmpBlock )
2019-05-22 21:16:55 +00:00
}
2021-07-30 09:02:09 +00:00
tmpBlockPool . Put ( v )
2020-06-23 17:29:19 +00:00
}
2019-05-22 21:16:55 +00:00
2021-07-30 09:02:09 +00:00
var tmpBlockPool sync . Pool
2020-08-06 14:42:15 +00:00
// unpackBatchSize is the maximum number of blocks that may be unpacked at once by a single goroutine.
//
2021-07-30 09:02:09 +00:00
// It is better to load a single goroutine for up to one second on a system with many CPU cores
// in order to reduce inter-CPU memory ping-pong.
// A single goroutine can unpack up to 40 millions of rows per second, while a single block contains up to 8K rows.
// So the batch size should be 40M / 8K = 5K.
var unpackBatchSize = 5000
2020-08-06 14:42:15 +00:00
2020-06-23 17:29:19 +00:00
// Unpack unpacks pts to dst.
2022-06-28 09:55:20 +00:00
func ( pts * packedTimeseries ) Unpack ( dst * Result , tbf * tmpBlocksFile , tr storage . TimeRange ) error {
2020-06-23 17:29:19 +00:00
dst . reset ( )
if err := dst . MetricName . Unmarshal ( bytesutil . ToUnsafeBytes ( pts . metricName ) ) ; err != nil {
2020-06-30 19:58:18 +00:00
return fmt . Errorf ( "cannot unmarshal metricName %q: %w" , pts . metricName , err )
2019-05-22 21:16:55 +00:00
}
2021-07-30 09:02:09 +00:00
// Spin up local workers.
// Do not use global workers pool, since it increases inter-CPU memory ping-poing,
// which reduces the scalability on systems with many CPU cores.
2020-09-22 20:49:47 +00:00
brsLen := len ( pts . brs )
2021-07-30 09:02:09 +00:00
workers := brsLen / unpackBatchSize
if workers > gomaxprocs {
workers = gomaxprocs
}
if workers < 1 {
workers = 1
}
workChs := make ( [ ] chan * unpackWork , workers )
var workChsWG sync . WaitGroup
for i := 0 ; i < workers ; i ++ {
// Use unbuffered channel on purpose, since there are high chances
// that only a single unpackWork is needed to unpack.
// The unbuffered channel should reduce inter-CPU ping-pong in this case,
// which should improve the performance in a system with many CPU cores.
workChs [ i ] = make ( chan * unpackWork )
workChsWG . Add ( 1 )
go func ( workerID int ) {
defer workChsWG . Done ( )
unpackWorker ( workChs [ workerID ] )
} ( i )
}
// Feed workers with work
2020-09-22 20:49:47 +00:00
upws := make ( [ ] * unpackWork , 0 , 1 + brsLen / unpackBatchSize )
2020-08-06 14:42:15 +00:00
upw := getUnpackWork ( )
2020-11-04 14:46:10 +00:00
upw . tbf = tbf
2020-08-06 14:42:15 +00:00
for _ , br := range pts . brs {
if len ( upw . ws ) >= unpackBatchSize {
2021-07-30 09:02:09 +00:00
scheduleUnpackWork ( workChs , upw )
2020-08-06 14:42:15 +00:00
upws = append ( upws , upw )
upw = getUnpackWork ( )
2020-11-04 14:46:10 +00:00
upw . tbf = tbf
2020-08-06 14:42:15 +00:00
}
upw . ws = append ( upw . ws , unpackWorkItem {
br : br ,
tr : tr ,
} )
2019-05-22 21:16:55 +00:00
}
2021-07-30 09:02:09 +00:00
scheduleUnpackWork ( workChs , upw )
2020-08-06 14:42:15 +00:00
upws = append ( upws , upw )
2020-04-27 05:13:41 +00:00
pts . brs = pts . brs [ : 0 ]
2019-05-22 21:16:55 +00:00
2020-06-23 17:29:19 +00:00
// Wait until work is complete
2021-07-15 13:03:26 +00:00
samples := 0
2020-09-22 20:49:47 +00:00
sbs := make ( [ ] * sortBlock , 0 , brsLen )
2020-06-23 17:29:19 +00:00
var firstErr error
for _ , upw := range upws {
if err := <- upw . doneCh ; err != nil && firstErr == nil {
// Return the first error only, since other errors are likely the same.
firstErr = err
}
if firstErr == nil {
2021-07-15 13:03:26 +00:00
for _ , sb := range upw . sbs {
samples += len ( sb . Timestamps )
}
2021-07-28 14:40:09 +00:00
if * maxSamplesPerSeries <= 0 || samples < * maxSamplesPerSeries {
2021-07-15 13:03:26 +00:00
sbs = append ( sbs , upw . sbs ... )
} else {
firstErr = fmt . Errorf ( "cannot process more than %d samples per series; either increase -search.maxSamplesPerSeries " +
"or reduce time range for the query" , * maxSamplesPerSeries )
}
}
if firstErr != nil {
2020-08-06 14:42:15 +00:00
for _ , sb := range upw . sbs {
putSortBlock ( sb )
}
2019-05-22 21:16:55 +00:00
}
2020-07-22 11:53:54 +00:00
putUnpackWork ( upw )
2019-05-22 21:16:55 +00:00
}
2021-07-30 09:02:09 +00:00
// Shut down local workers
for _ , workCh := range workChs {
close ( workCh )
}
workChsWG . Wait ( )
2020-06-23 17:29:19 +00:00
if firstErr != nil {
return firstErr
2019-05-22 21:16:55 +00:00
}
2020-09-23 11:26:39 +00:00
if pts . pd != nil {
// Add data from Prometheus to dst.
// It usually has smaller timestamps than the data from sbs, so put it first.
dst . Values = append ( dst . Values , pts . pd . values ... )
dst . Timestamps = append ( dst . Timestamps , pts . pd . timestamps ... )
}
2021-12-15 14:23:27 +00:00
dedupInterval := storage . GetDedupInterval ( tr . MinTimestamp )
2021-12-14 18:49:08 +00:00
mergeSortBlocks ( dst , sbs , dedupInterval )
2020-09-23 11:26:39 +00:00
if pts . pd != nil {
if ! sort . IsSorted ( dst ) {
sort . Sort ( dst )
}
pts . pd = nil
}
2019-05-22 21:16:55 +00:00
return nil
}
2020-09-23 11:26:39 +00:00
// sort.Interface implementation for Result
// Len implements sort.Interface
func ( r * Result ) Len ( ) int {
return len ( r . Timestamps )
}
// Less implements sort.Interface
func ( r * Result ) Less ( i , j int ) bool {
timestamps := r . Timestamps
return timestamps [ i ] < timestamps [ j ]
}
// Swap implements sort.Interface
func ( r * Result ) Swap ( i , j int ) {
timestamps := r . Timestamps
values := r . Values
timestamps [ i ] , timestamps [ j ] = timestamps [ j ] , timestamps [ i ]
values [ i ] , values [ j ] = values [ j ] , values [ i ]
}
2019-05-22 21:16:55 +00:00
func getSortBlock ( ) * sortBlock {
v := sbPool . Get ( )
if v == nil {
return & sortBlock { }
}
return v . ( * sortBlock )
}
func putSortBlock ( sb * sortBlock ) {
sb . reset ( )
sbPool . Put ( sb )
}
var sbPool sync . Pool
var metricRowsSkipped = metrics . NewCounter ( ` vm_metric_rows_skipped_total { name="vmselect"} ` )
2021-12-14 18:49:08 +00:00
func mergeSortBlocks ( dst * Result , sbh sortBlocksHeap , dedupInterval int64 ) {
2019-05-22 21:16:55 +00:00
// Skip empty sort blocks, since they cannot be passed to heap.Init.
src := sbh
sbh = sbh [ : 0 ]
for _ , sb := range src {
if len ( sb . Timestamps ) == 0 {
putSortBlock ( sb )
continue
}
sbh = append ( sbh , sb )
}
if len ( sbh ) == 0 {
return
}
heap . Init ( & sbh )
for {
top := sbh [ 0 ]
2022-07-12 09:30:24 +00:00
if len ( sbh ) == 1 {
2019-05-22 21:16:55 +00:00
dst . Timestamps = append ( dst . Timestamps , top . Timestamps [ top . NextIdx : ] ... )
dst . Values = append ( dst . Values , top . Values [ top . NextIdx : ] ... )
putSortBlock ( top )
2020-01-30 23:09:44 +00:00
break
2019-05-22 21:16:55 +00:00
}
2022-07-12 09:30:24 +00:00
sbNext := sbh . getNextBlock ( )
2019-05-22 21:16:55 +00:00
tsNext := sbNext . Timestamps [ sbNext . NextIdx ]
2022-07-08 21:14:48 +00:00
topTimestamps := top . Timestamps
topNextIdx := top . NextIdx
if n := equalTimestampsPrefix ( topTimestamps [ topNextIdx : ] , sbNext . Timestamps [ sbNext . NextIdx : ] ) ; n > 0 && dedupInterval > 0 {
// Skip n replicated samples at top if deduplication is enabled.
top . NextIdx = topNextIdx + n
} else {
// Copy samples from top to dst with timestamps not exceeding tsNext.
top . NextIdx = topNextIdx + binarySearchTimestamps ( topTimestamps [ topNextIdx : ] , tsNext )
dst . Timestamps = append ( dst . Timestamps , topTimestamps [ topNextIdx : top . NextIdx ] ... )
dst . Values = append ( dst . Values , top . Values [ topNextIdx : top . NextIdx ] ... )
2019-05-22 21:16:55 +00:00
}
2022-07-08 21:14:48 +00:00
if top . NextIdx < len ( topTimestamps ) {
2022-07-12 09:30:24 +00:00
heap . Fix ( & sbh , 0 )
2019-05-22 21:16:55 +00:00
} else {
2022-07-12 09:30:24 +00:00
heap . Pop ( & sbh )
2019-05-22 21:16:55 +00:00
putSortBlock ( top )
}
}
2021-12-14 18:49:08 +00:00
timestamps , values := storage . DeduplicateSamples ( dst . Timestamps , dst . Values , dedupInterval )
2020-02-27 21:47:05 +00:00
dedups := len ( dst . Timestamps ) - len ( timestamps )
dedupsDuringSelect . Add ( dedups )
dst . Timestamps = timestamps
dst . Values = values
2019-05-22 21:16:55 +00:00
}
2020-02-27 21:47:05 +00:00
var dedupsDuringSelect = metrics . NewCounter ( ` vm_deduplicated_samples_total { type="select"} ` )
2022-07-08 21:14:48 +00:00
func equalTimestampsPrefix ( a , b [ ] int64 ) int {
for i , v := range a {
if i >= len ( b ) || v != b [ i ] {
return i
}
}
return len ( a )
}
func binarySearchTimestamps ( timestamps [ ] int64 , ts int64 ) int {
// The code has been adapted from sort.Search.
n := len ( timestamps )
2022-07-11 08:57:31 +00:00
if n > 0 && timestamps [ n - 1 ] <= ts {
// Fast path for timestamps scanned in ascending order.
2022-07-08 21:14:48 +00:00
return n
}
i , j := 0 , n
for i < j {
h := int ( uint ( i + j ) >> 1 )
if h >= 0 && h < len ( timestamps ) && timestamps [ h ] <= ts {
i = h + 1
} else {
j = h
}
}
return i
}
2019-05-22 21:16:55 +00:00
type sortBlock struct {
Timestamps [ ] int64
Values [ ] float64
NextIdx int
}
func ( sb * sortBlock ) reset ( ) {
sb . Timestamps = sb . Timestamps [ : 0 ]
sb . Values = sb . Values [ : 0 ]
sb . NextIdx = 0
}
2020-11-04 14:46:10 +00:00
func ( sb * sortBlock ) unpackFrom ( tmpBlock * storage . Block , tbf * tmpBlocksFile , br blockRef , tr storage . TimeRange ) error {
2020-09-15 18:06:04 +00:00
tmpBlock . Reset ( )
2020-11-04 14:46:10 +00:00
brReal := tbf . MustReadBlockRefAt ( br . partRef , br . addr )
2022-06-28 09:55:20 +00:00
brReal . MustReadBlock ( tmpBlock )
2020-09-24 17:16:19 +00:00
if err := tmpBlock . UnmarshalData ( ) ; err != nil {
return fmt . Errorf ( "cannot unmarshal block: %w" , err )
2019-05-22 21:16:55 +00:00
}
2020-09-26 01:29:45 +00:00
sb . Timestamps , sb . Values = tmpBlock . AppendRowsWithTimeRangeFilter ( sb . Timestamps [ : 0 ] , sb . Values [ : 0 ] , tr )
skippedRows := tmpBlock . RowsCount ( ) - len ( sb . Timestamps )
2019-05-22 21:16:55 +00:00
metricRowsSkipped . Add ( skippedRows )
return nil
}
type sortBlocksHeap [ ] * sortBlock
2022-07-12 09:30:24 +00:00
func ( sbh sortBlocksHeap ) getNextBlock ( ) * sortBlock {
if len ( sbh ) < 2 {
return nil
}
if len ( sbh ) < 3 {
return sbh [ 1 ]
}
a := sbh [ 1 ]
b := sbh [ 2 ]
if a . Timestamps [ a . NextIdx ] <= b . Timestamps [ b . NextIdx ] {
return a
}
return b
}
2019-05-22 21:16:55 +00:00
func ( sbh sortBlocksHeap ) Len ( ) int {
return len ( sbh )
}
func ( sbh sortBlocksHeap ) Less ( i , j int ) bool {
a := sbh [ i ]
b := sbh [ j ]
return a . Timestamps [ a . NextIdx ] < b . Timestamps [ b . NextIdx ]
}
func ( sbh sortBlocksHeap ) Swap ( i , j int ) {
sbh [ i ] , sbh [ j ] = sbh [ j ] , sbh [ i ]
}
func ( sbh * sortBlocksHeap ) Push ( x interface { } ) {
* sbh = append ( * sbh , x . ( * sortBlock ) )
}
func ( sbh * sortBlocksHeap ) Pop ( ) interface { } {
a := * sbh
v := a [ len ( a ) - 1 ]
* sbh = a [ : len ( a ) - 1 ]
return v
}
// DeleteSeries deletes time series matching the given tagFilterss.
2022-05-31 23:29:19 +00:00
func DeleteSeries ( qt * querytracer . Tracer , sq * storage . SearchQuery , deadline searchutils . Deadline ) ( int , error ) {
2022-06-08 18:05:17 +00:00
qt = qt . NewChild ( "delete series: %s" , sq )
defer qt . Done ( )
2022-07-05 21:53:03 +00:00
tr := sq . GetTimeRange ( )
2022-06-27 09:53:46 +00:00
tfss , err := setupTfss ( qt , tr , sq . TagFilterss , sq . MaxMetrics , deadline )
2019-05-22 21:16:55 +00:00
if err != nil {
return 0 , err
}
2022-07-05 20:56:31 +00:00
return vmstorage . DeleteSeries ( qt , tfss )
2019-05-22 21:16:55 +00:00
}
2022-06-26 21:37:19 +00:00
// LabelNames returns label names matching the given sq until the given deadline.
func LabelNames ( qt * querytracer . Tracer , sq * storage . SearchQuery , maxLabelNames int , deadline searchutils . Deadline ) ( [ ] string , error ) {
2022-06-12 01:32:13 +00:00
qt = qt . NewChild ( "get labels: %s" , sq )
2022-06-08 18:05:17 +00:00
defer qt . Done ( )
2020-11-04 22:15:43 +00:00
if deadline . Exceeded ( ) {
return nil , fmt . Errorf ( "timeout exceeded before starting the query processing: %s" , deadline . String ( ) )
}
2022-06-12 01:32:13 +00:00
if maxLabelNames > * maxTagKeysPerSearch || maxLabelNames <= 0 {
maxLabelNames = * maxTagKeysPerSearch
2022-06-10 06:50:30 +00:00
}
2022-07-05 21:53:03 +00:00
tr := sq . GetTimeRange ( )
2022-06-27 09:53:46 +00:00
tfss , err := setupTfss ( qt , tr , sq . TagFilterss , sq . MaxMetrics , deadline )
2020-11-04 22:15:43 +00:00
if err != nil {
2022-06-12 01:32:13 +00:00
return nil , err
2020-11-04 22:15:43 +00:00
}
2022-06-12 01:32:13 +00:00
labels , err := vmstorage . SearchLabelNamesWithFiltersOnTimeRange ( qt , tfss , tr , maxLabelNames , sq . MaxMetrics , deadline . Deadline ( ) )
if err != nil {
return nil , fmt . Errorf ( "error during labels search on time range: %w" , err )
2020-11-04 22:15:43 +00:00
}
2020-11-05 00:51:08 +00:00
// Merge labels obtained from Prometheus storage.
promLabels , err := promdb . GetLabelNamesOnTimeRange ( tr , deadline )
if err != nil {
return nil , fmt . Errorf ( "cannot obtain labels from Prometheus storage: %w" , err )
}
2022-06-14 09:26:32 +00:00
qt . Printf ( "get %d label names from Prometheus storage" , len ( promLabels ) )
2020-11-05 00:51:08 +00:00
labels = mergeStrings ( labels , promLabels )
2020-11-04 22:15:43 +00:00
// Sort labels like Prometheus does
sort . Strings ( labels )
2022-05-31 23:29:19 +00:00
qt . Printf ( "sort %d labels" , len ( labels ) )
2020-11-04 22:15:43 +00:00
return labels , nil
}
2022-06-26 21:37:19 +00:00
// GraphiteTags returns Graphite tags until the given deadline.
func GraphiteTags ( qt * querytracer . Tracer , filter string , limit int , deadline searchutils . Deadline ) ( [ ] string , error ) {
2022-06-08 18:05:17 +00:00
qt = qt . NewChild ( "get graphite tags: filter=%s, limit=%d" , filter , limit )
defer qt . Done ( )
2020-11-15 23:25:38 +00:00
if deadline . Exceeded ( ) {
return nil , fmt . Errorf ( "timeout exceeded before starting the query processing: %s" , deadline . String ( ) )
}
2022-06-12 01:32:13 +00:00
sq := storage . NewSearchQuery ( 0 , 0 , nil , 0 )
2022-06-26 21:37:19 +00:00
labels , err := LabelNames ( qt , sq , 0 , deadline )
2020-11-15 23:25:38 +00:00
if err != nil {
2020-11-16 01:58:12 +00:00
return nil , err
}
// Substitute "__name__" with "name" for Graphite compatibility
2020-11-15 23:25:38 +00:00
for i := range labels {
2020-12-06 23:07:03 +00:00
if labels [ i ] != "__name__" {
continue
}
// Prevent from duplicate `name` tag.
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/942
if hasString ( labels , "name" ) {
labels = append ( labels [ : i ] , labels [ i + 1 : ] ... )
} else {
2020-11-15 23:25:38 +00:00
labels [ i ] = "name"
2020-11-16 12:49:46 +00:00
sort . Strings ( labels )
2020-11-15 23:25:38 +00:00
}
2020-12-06 23:07:03 +00:00
break
2020-11-15 23:25:38 +00:00
}
2020-11-16 13:50:48 +00:00
if len ( filter ) > 0 {
labels , err = applyGraphiteRegexpFilter ( filter , labels )
if err != nil {
return nil , err
}
}
2020-11-16 01:58:12 +00:00
if limit > 0 && limit < len ( labels ) {
labels = labels [ : limit ]
}
2020-11-15 23:25:38 +00:00
return labels , nil
}
2020-12-06 23:07:03 +00:00
func hasString ( a [ ] string , s string ) bool {
for _ , x := range a {
if x == s {
return true
}
}
return false
}
2022-06-26 21:37:19 +00:00
// LabelValues returns label values matching the given labelName and sq until the given deadline.
func LabelValues ( qt * querytracer . Tracer , labelName string , sq * storage . SearchQuery , maxLabelValues int , deadline searchutils . Deadline ) ( [ ] string , error ) {
2022-06-12 01:32:13 +00:00
qt = qt . NewChild ( "get values for label %s: %s" , labelName , sq )
2022-06-08 18:05:17 +00:00
defer qt . Done ( )
2020-07-21 15:34:59 +00:00
if deadline . Exceeded ( ) {
return nil , fmt . Errorf ( "timeout exceeded before starting the query processing: %s" , deadline . String ( ) )
}
2022-06-12 01:32:13 +00:00
if maxLabelValues > * maxTagValuesPerSearch || maxLabelValues <= 0 {
maxLabelValues = * maxTagValuesPerSearch
2020-11-04 22:15:43 +00:00
}
2022-07-05 21:53:03 +00:00
tr := sq . GetTimeRange ( )
2022-06-27 09:53:46 +00:00
tfss , err := setupTfss ( qt , tr , sq . TagFilterss , sq . MaxMetrics , deadline )
2019-05-22 21:16:55 +00:00
if err != nil {
2022-06-12 01:32:13 +00:00
return nil , err
2019-05-22 21:16:55 +00:00
}
2022-06-12 01:32:13 +00:00
labelValues , err := vmstorage . SearchLabelValuesWithFiltersOnTimeRange ( qt , labelName , tfss , tr , maxLabelValues , sq . MaxMetrics , deadline . Deadline ( ) )
2020-11-04 22:15:43 +00:00
if err != nil {
2022-06-14 09:26:32 +00:00
return nil , fmt . Errorf ( "error during label values search on time range: %w" , err )
2019-05-22 21:16:55 +00:00
}
2022-06-14 09:26:32 +00:00
// Merge label values obtained from Prometheus storage.
promLabelValues , err := promdb . GetLabelValuesOnTimeRange ( labelName , tr , deadline )
2020-09-23 19:42:44 +00:00
if err != nil {
2022-06-14 09:26:32 +00:00
return nil , fmt . Errorf ( "cannot obtain label values on time range for %q from Prometheus storage: %w" , labelName , err )
2020-09-23 19:42:44 +00:00
}
2022-06-14 09:26:32 +00:00
qt . Printf ( "get %d label values from Prometheus storage" , len ( promLabelValues ) )
labelValues = mergeStrings ( labelValues , promLabelValues )
2020-09-23 19:42:44 +00:00
2020-11-04 22:15:43 +00:00
// Sort labelValues like Prometheus does
sort . Strings ( labelValues )
2022-05-31 23:29:19 +00:00
qt . Printf ( "sort %d label values" , len ( labelValues ) )
2020-11-04 22:15:43 +00:00
return labelValues , nil
2019-05-22 21:16:55 +00:00
}
2020-09-23 19:42:44 +00:00
func mergeStrings ( a , b [ ] string ) [ ] string {
if len ( a ) == 0 {
return b
}
if len ( b ) == 0 {
return a
}
m := make ( map [ string ] struct { } , len ( a ) + len ( b ) )
for _ , s := range a {
m [ s ] = struct { } { }
}
for _ , s := range b {
m [ s ] = struct { } { }
}
result := make ( [ ] string , 0 , len ( m ) )
for s := range m {
result = append ( result , s )
}
return result
}
2022-06-26 21:37:19 +00:00
// GraphiteTagValues returns tag values for the given tagName until the given deadline.
func GraphiteTagValues ( qt * querytracer . Tracer , tagName , filter string , limit int , deadline searchutils . Deadline ) ( [ ] string , error ) {
2022-06-08 18:05:17 +00:00
qt = qt . NewChild ( "get graphite tag values for tagName=%s, filter=%s, limit=%d" , tagName , filter , limit )
defer qt . Done ( )
2020-11-16 01:31:09 +00:00
if deadline . Exceeded ( ) {
return nil , fmt . Errorf ( "timeout exceeded before starting the query processing: %s" , deadline . String ( ) )
}
if tagName == "name" {
tagName = ""
}
2022-06-12 01:32:13 +00:00
sq := storage . NewSearchQuery ( 0 , 0 , nil , 0 )
2022-06-26 21:37:19 +00:00
tagValues , err := LabelValues ( qt , tagName , sq , 0 , deadline )
2020-11-16 01:58:12 +00:00
if err != nil {
return nil , err
2020-11-16 01:31:09 +00:00
}
2020-11-16 01:58:12 +00:00
if len ( filter ) > 0 {
tagValues , err = applyGraphiteRegexpFilter ( filter , tagValues )
if err != nil {
return nil , err
}
2020-11-16 01:31:09 +00:00
}
2020-11-16 01:58:12 +00:00
if limit > 0 && limit < len ( tagValues ) {
tagValues = tagValues [ : limit ]
2020-11-16 01:31:09 +00:00
}
return tagValues , nil
}
2022-06-26 21:37:19 +00:00
// TagValueSuffixes returns tag value suffixes for the given tagKey and the given tagValuePrefix.
2020-09-10 21:28:19 +00:00
//
// It can be used for implementing https://graphite-api.readthedocs.io/en/latest/api.html#metrics-find
2022-07-05 21:31:41 +00:00
func TagValueSuffixes ( qt * querytracer . Tracer , tr storage . TimeRange , tagKey , tagValuePrefix string , delimiter byte , maxSuffixes int , deadline searchutils . Deadline ) ( [ ] string , error ) {
qt = qt . NewChild ( "get tag value suffixes for tagKey=%s, tagValuePrefix=%s, maxSuffixes=%d, timeRange=%s" , tagKey , tagValuePrefix , maxSuffixes , & tr )
2022-06-08 18:05:17 +00:00
defer qt . Done ( )
2020-09-10 21:28:19 +00:00
if deadline . Exceeded ( ) {
return nil , fmt . Errorf ( "timeout exceeded before starting the query processing: %s" , deadline . String ( ) )
}
2022-07-05 21:31:41 +00:00
suffixes , err := vmstorage . SearchTagValueSuffixes ( qt , tr , tagKey , tagValuePrefix , delimiter , maxSuffixes , deadline . Deadline ( ) )
2020-09-10 21:28:19 +00:00
if err != nil {
return nil , fmt . Errorf ( "error during search for suffixes for tagKey=%q, tagValuePrefix=%q, delimiter=%c on time range %s: %w" ,
tagKey , tagValuePrefix , delimiter , tr . String ( ) , err )
}
2022-07-05 21:31:41 +00:00
if len ( suffixes ) >= maxSuffixes {
2021-02-02 22:24:05 +00:00
return nil , fmt . Errorf ( "more than -search.maxTagValueSuffixesPerSearch=%d tag value suffixes found for tagKey=%q, tagValuePrefix=%q, delimiter=%c on time range %s; " +
"either narrow down the query or increase -search.maxTagValueSuffixesPerSearch command-line flag value" ,
2022-07-05 21:31:41 +00:00
maxSuffixes , tagKey , tagValuePrefix , delimiter , tr . String ( ) )
2021-02-02 22:24:05 +00:00
}
2020-09-10 21:28:19 +00:00
return suffixes , nil
}
2022-06-26 21:37:19 +00:00
// TSDBStatus returns tsdb status according to https://prometheus.io/docs/prometheus/latest/querying/api/#tsdb-stats
2021-05-12 13:32:48 +00:00
//
// It accepts aribtrary filters on time series in sq.
2022-06-26 21:37:19 +00:00
func TSDBStatus ( qt * querytracer . Tracer , sq * storage . SearchQuery , focusLabel string , topN int , deadline searchutils . Deadline ) ( * storage . TSDBStatus , error ) {
2022-06-14 14:46:16 +00:00
qt = qt . NewChild ( "get tsdb stats: %s, focusLabel=%q, topN=%d" , sq , focusLabel , topN )
2022-06-08 18:05:17 +00:00
defer qt . Done ( )
2021-05-12 12:18:45 +00:00
if deadline . Exceeded ( ) {
return nil , fmt . Errorf ( "timeout exceeded before starting the query processing: %s" , deadline . String ( ) )
}
2022-07-05 21:53:03 +00:00
tr := sq . GetTimeRange ( )
2022-06-27 09:53:46 +00:00
tfss , err := setupTfss ( qt , tr , sq . TagFilterss , sq . MaxMetrics , deadline )
2021-05-12 12:18:45 +00:00
if err != nil {
return nil , err
}
2021-05-12 13:32:48 +00:00
date := uint64 ( tr . MinTimestamp ) / ( 3600 * 24 * 1000 )
2022-06-14 14:46:16 +00:00
status , err := vmstorage . GetTSDBStatus ( qt , tfss , date , focusLabel , topN , sq . MaxMetrics , deadline . Deadline ( ) )
2021-05-12 12:18:45 +00:00
if err != nil {
2022-06-14 14:46:16 +00:00
return nil , fmt . Errorf ( "error during tsdb status request: %w" , err )
2021-05-12 12:18:45 +00:00
}
return status , nil
2020-04-22 16:57:36 +00:00
}
2022-06-26 21:37:19 +00:00
// SeriesCount returns the number of unique series.
func SeriesCount ( qt * querytracer . Tracer , deadline searchutils . Deadline ) ( uint64 , error ) {
2022-06-08 18:05:17 +00:00
qt = qt . NewChild ( "get series count" )
defer qt . Done ( )
2020-07-21 15:34:59 +00:00
if deadline . Exceeded ( ) {
return 0 , fmt . Errorf ( "timeout exceeded before starting the query processing: %s" , deadline . String ( ) )
}
2020-09-11 10:18:57 +00:00
n , err := vmstorage . GetSeriesCount ( deadline . Deadline ( ) )
2019-05-22 21:16:55 +00:00
if err != nil {
2020-06-30 19:58:18 +00:00
return 0 , fmt . Errorf ( "error during series count request: %w" , err )
2019-05-22 21:16:55 +00:00
}
return n , nil
}
func getStorageSearch ( ) * storage . Search {
v := ssPool . Get ( )
if v == nil {
return & storage . Search { }
}
return v . ( * storage . Search )
}
func putStorageSearch ( sr * storage . Search ) {
sr . MustClose ( )
ssPool . Put ( sr )
}
var ssPool sync . Pool
2020-09-26 01:29:45 +00:00
// ExportBlocks searches for time series matching sq and calls f for each found block.
//
// f is called in parallel from multiple goroutines.
2020-09-27 20:17:14 +00:00
// Data processing is immediately stopped if f returns non-nil error.
2020-09-26 01:29:45 +00:00
// It is the responsibility of f to call b.UnmarshalData before reading timestamps and values from the block.
// It is the responsibility of f to filter blocks according to the given tr.
2022-10-01 19:05:43 +00:00
func ExportBlocks ( qt * querytracer . Tracer , sq * storage . SearchQuery , deadline searchutils . Deadline ,
f func ( mn * storage . MetricName , b * storage . Block , tr storage . TimeRange , workerID uint ) error ) error {
2022-06-08 18:05:17 +00:00
qt = qt . NewChild ( "export blocks: %s" , sq )
defer qt . Done ( )
2020-09-26 01:29:45 +00:00
if deadline . Exceeded ( ) {
return fmt . Errorf ( "timeout exceeded before starting data export: %s" , deadline . String ( ) )
}
2022-07-05 21:53:03 +00:00
tr := sq . GetTimeRange ( )
2020-09-26 01:29:45 +00:00
if err := vmstorage . CheckTimeRange ( tr ) ; err != nil {
return err
}
2022-06-27 09:53:46 +00:00
tfss , err := setupTfss ( qt , tr , sq . TagFilterss , sq . MaxMetrics , deadline )
2021-02-02 22:24:05 +00:00
if err != nil {
return err
}
2020-09-26 01:29:45 +00:00
vmstorage . WG . Add ( 1 )
defer vmstorage . WG . Done ( )
sr := getStorageSearch ( )
defer putStorageSearch ( sr )
2021-03-16 23:12:28 +00:00
startTime := time . Now ( )
2022-05-31 23:29:19 +00:00
sr . Init ( qt , vmstorage . Storage , tfss , tr , sq . MaxMetrics , deadline . Deadline ( ) )
2021-03-16 23:12:28 +00:00
indexSearchDuration . UpdateDuration ( startTime )
2020-09-26 01:29:45 +00:00
// Start workers that call f in parallel on available CPU cores.
2020-12-08 18:49:32 +00:00
gomaxprocs := cgroup . AvailableCPUs ( )
2020-09-26 01:29:45 +00:00
workCh := make ( chan * exportWork , gomaxprocs * 8 )
var (
errGlobal error
errGlobalLock sync . Mutex
mustStop uint32
)
var wg sync . WaitGroup
wg . Add ( gomaxprocs )
for i := 0 ; i < gomaxprocs ; i ++ {
2022-10-01 19:05:43 +00:00
go func ( workerID uint ) {
2020-09-26 01:29:45 +00:00
defer wg . Done ( )
for xw := range workCh {
2022-10-01 19:05:43 +00:00
if err := f ( & xw . mn , & xw . b , tr , workerID ) ; err != nil {
2020-09-26 01:29:45 +00:00
errGlobalLock . Lock ( )
if errGlobal != nil {
errGlobal = err
atomic . StoreUint32 ( & mustStop , 1 )
}
errGlobalLock . Unlock ( )
}
xw . reset ( )
exportWorkPool . Put ( xw )
}
2022-10-01 19:05:43 +00:00
} ( uint ( i ) )
2020-09-26 01:29:45 +00:00
}
// Feed workers with work
blocksRead := 0
2022-05-31 23:29:19 +00:00
samples := 0
2020-09-26 01:29:45 +00:00
for sr . NextMetricBlock ( ) {
blocksRead ++
if deadline . Exceeded ( ) {
return fmt . Errorf ( "timeout exceeded while fetching data block #%d from storage: %s" , blocksRead , deadline . String ( ) )
}
if atomic . LoadUint32 ( & mustStop ) != 0 {
break
}
xw := exportWorkPool . Get ( ) . ( * exportWork )
if err := xw . mn . Unmarshal ( sr . MetricBlockRef . MetricName ) ; err != nil {
return fmt . Errorf ( "cannot unmarshal metricName for block #%d: %w" , blocksRead , err )
}
2022-05-31 23:29:19 +00:00
br := sr . MetricBlockRef . BlockRef
2022-06-28 09:55:20 +00:00
br . MustReadBlock ( & xw . b )
2022-05-31 23:29:19 +00:00
samples += br . RowsCount ( )
2020-09-26 01:29:45 +00:00
workCh <- xw
}
close ( workCh )
// Wait for workers to finish.
wg . Wait ( )
2022-05-31 23:29:19 +00:00
qt . Printf ( "export blocks=%d, samples=%d" , blocksRead , samples )
2020-09-26 01:29:45 +00:00
// Check errors.
err = sr . Error ( )
if err == nil {
err = errGlobal
}
if err != nil {
if errors . Is ( err , storage . ErrDeadlineExceeded ) {
return fmt . Errorf ( "timeout exceeded during the query: %s" , deadline . String ( ) )
}
return fmt . Errorf ( "search error after reading %d data blocks: %w" , blocksRead , err )
}
return nil
}
type exportWork struct {
mn storage . MetricName
b storage . Block
}
func ( xw * exportWork ) reset ( ) {
xw . mn . Reset ( )
xw . b . Reset ( )
}
var exportWorkPool = & sync . Pool {
New : func ( ) interface { } {
return & exportWork { }
} ,
}
2020-11-16 08:55:55 +00:00
// SearchMetricNames returns all the metric names matching sq until the given deadline.
2022-06-28 14:36:27 +00:00
//
// The returned metric names must be unmarshaled via storage.MetricName.UnmarshalString().
func SearchMetricNames ( qt * querytracer . Tracer , sq * storage . SearchQuery , deadline searchutils . Deadline ) ( [ ] string , error ) {
2022-06-08 18:05:17 +00:00
qt = qt . NewChild ( "fetch metric names: %s" , sq )
defer qt . Done ( )
2020-11-16 08:55:55 +00:00
if deadline . Exceeded ( ) {
return nil , fmt . Errorf ( "timeout exceeded before starting to search metric names: %s" , deadline . String ( ) )
}
// Setup search.
2022-07-05 21:53:03 +00:00
tr := sq . GetTimeRange ( )
2020-11-16 08:55:55 +00:00
if err := vmstorage . CheckTimeRange ( tr ) ; err != nil {
return nil , err
}
2022-06-27 09:53:46 +00:00
tfss , err := setupTfss ( qt , tr , sq . TagFilterss , sq . MaxMetrics , deadline )
2021-02-02 22:24:05 +00:00
if err != nil {
return nil , err
}
2020-11-16 08:55:55 +00:00
2022-06-28 14:36:27 +00:00
metricNames , err := vmstorage . SearchMetricNames ( qt , tfss , tr , sq . MaxMetrics , deadline . Deadline ( ) )
2020-11-16 08:55:55 +00:00
if err != nil {
return nil , fmt . Errorf ( "cannot find metric names: %w" , err )
}
2022-06-28 14:36:27 +00:00
sort . Strings ( metricNames )
qt . Printf ( "sort %d metric names" , len ( metricNames ) )
return metricNames , nil
2020-11-16 08:55:55 +00:00
}
2020-09-26 01:29:45 +00:00
// ProcessSearchQuery performs sq until the given deadline.
2020-04-27 05:13:41 +00:00
//
// Results.RunParallel or Results.Cancel must be called on the returned Results.
2022-06-28 09:55:20 +00:00
func ProcessSearchQuery ( qt * querytracer . Tracer , sq * storage . SearchQuery , deadline searchutils . Deadline ) ( * Results , error ) {
qt = qt . NewChild ( "fetch matching series: %s" , sq )
2022-06-08 18:05:17 +00:00
defer qt . Done ( )
2020-07-21 15:34:59 +00:00
if deadline . Exceeded ( ) {
return nil , fmt . Errorf ( "timeout exceeded before starting the query processing: %s" , deadline . String ( ) )
}
2019-05-22 21:16:55 +00:00
// Setup search.
2022-07-05 21:53:03 +00:00
tr := sq . GetTimeRange ( )
2020-06-30 21:20:13 +00:00
if err := vmstorage . CheckTimeRange ( tr ) ; err != nil {
return nil , err
}
2022-06-27 09:53:46 +00:00
tfss , err := setupTfss ( qt , tr , sq . TagFilterss , sq . MaxMetrics , deadline )
2021-02-02 22:24:05 +00:00
if err != nil {
return nil , err
}
2019-05-22 21:16:55 +00:00
vmstorage . WG . Add ( 1 )
defer vmstorage . WG . Done ( )
sr := getStorageSearch ( )
2021-03-16 23:12:28 +00:00
startTime := time . Now ( )
2022-05-31 23:29:19 +00:00
maxSeriesCount := sr . Init ( qt , vmstorage . Storage , tfss , tr , sq . MaxMetrics , deadline . Deadline ( ) )
2021-03-16 23:12:28 +00:00
indexSearchDuration . UpdateDuration ( startTime )
2020-11-04 14:46:10 +00:00
m := make ( map [ string ] [ ] blockRef , maxSeriesCount )
2020-08-06 16:17:51 +00:00
orderedMetricNames := make ( [ ] string , 0 , maxSeriesCount )
2019-07-28 09:12:30 +00:00
blocksRead := 0
2021-07-28 14:40:09 +00:00
samples := 0
2020-11-04 14:46:10 +00:00
tbf := getTmpBlocksFile ( )
var buf [ ] byte
2019-05-22 21:16:55 +00:00
for sr . NextMetricBlock ( ) {
2019-07-28 09:12:30 +00:00
blocksRead ++
2020-07-21 15:34:59 +00:00
if deadline . Exceeded ( ) {
2020-11-04 14:46:10 +00:00
putTmpBlocksFile ( tbf )
2020-09-22 19:56:49 +00:00
putStorageSearch ( sr )
2020-01-22 13:50:34 +00:00
return nil , fmt . Errorf ( "timeout exceeded while fetching data block #%d from storage: %s" , blocksRead , deadline . String ( ) )
2019-05-22 21:16:55 +00:00
}
2021-07-28 14:40:09 +00:00
br := sr . MetricBlockRef . BlockRef
samples += br . RowsCount ( )
if * maxSamplesPerQuery > 0 && samples > * maxSamplesPerQuery {
putTmpBlocksFile ( tbf )
putStorageSearch ( sr )
return nil , fmt . Errorf ( "cannot select more than -search.maxSamplesPerQuery=%d samples; possible solutions: to increase the -search.maxSamplesPerQuery; to reduce time range for the query; to use more specific label filters in order to select lower number of series" , * maxSamplesPerQuery )
}
buf = br . Marshal ( buf [ : 0 ] )
2020-11-04 14:46:10 +00:00
addr , err := tbf . WriteBlockRefData ( buf )
if err != nil {
putTmpBlocksFile ( tbf )
putStorageSearch ( sr )
return nil , fmt . Errorf ( "cannot write %d bytes to temporary file: %w" , len ( buf ) , err )
}
2020-04-27 05:13:41 +00:00
metricName := sr . MetricBlockRef . MetricName
2021-07-30 06:55:08 +00:00
brs := m [ string ( metricName ) ]
2020-11-04 14:46:10 +00:00
brs = append ( brs , blockRef {
2021-07-28 14:40:09 +00:00
partRef : br . PartRef ( ) ,
2020-11-04 14:46:10 +00:00
addr : addr ,
} )
2020-07-23 11:11:48 +00:00
if len ( brs ) > 1 {
2021-07-30 06:55:08 +00:00
m [ string ( metricName ) ] = brs
2020-07-23 10:53:30 +00:00
} else {
// An optimization for big number of time series with long metricName values:
// use only a single copy of metricName for both orderedMetricNames and m.
2020-07-23 14:53:52 +00:00
orderedMetricNames = append ( orderedMetricNames , string ( metricName ) )
m [ orderedMetricNames [ len ( orderedMetricNames ) - 1 ] ] = brs
2020-04-26 13:25:35 +00:00
}
2019-05-22 21:16:55 +00:00
}
if err := sr . Error ( ) ; err != nil {
2020-11-04 14:46:10 +00:00
putTmpBlocksFile ( tbf )
2020-09-22 19:56:49 +00:00
putStorageSearch ( sr )
2020-08-10 10:17:12 +00:00
if errors . Is ( err , storage . ErrDeadlineExceeded ) {
return nil , fmt . Errorf ( "timeout exceeded during the query: %s" , deadline . String ( ) )
}
2020-06-30 19:58:18 +00:00
return nil , fmt . Errorf ( "search error after reading %d data blocks: %w" , blocksRead , err )
2019-05-22 21:16:55 +00:00
}
2020-11-04 14:46:10 +00:00
if err := tbf . Finalize ( ) ; err != nil {
putTmpBlocksFile ( tbf )
putStorageSearch ( sr )
return nil , fmt . Errorf ( "cannot finalize temporary file: %w" , err )
}
2022-05-31 23:29:19 +00:00
qt . Printf ( "fetch unique series=%d, blocks=%d, samples=%d, bytes=%d" , len ( m ) , blocksRead , samples , tbf . Len ( ) )
2019-05-22 21:16:55 +00:00
2020-09-23 11:26:39 +00:00
// Fetch data from promdb.
pm := make ( map [ string ] * promData )
2022-06-28 10:26:58 +00:00
err = promdb . VisitSeries ( sq , deadline , func ( metricName [ ] byte , values [ ] float64 , timestamps [ ] int64 ) {
2020-09-23 11:26:39 +00:00
pd := pm [ string ( metricName ) ]
if pd == nil {
if _ , ok := m [ string ( metricName ) ] ; ! ok {
orderedMetricNames = append ( orderedMetricNames , string ( metricName ) )
}
pd = & promData { }
pm [ string ( metricName ) ] = pd
}
pd . values = append ( pd . values , values ... )
pd . timestamps = append ( pd . timestamps , timestamps ... )
} )
if err != nil {
2021-01-13 09:53:11 +00:00
putTmpBlocksFile ( tbf )
2020-09-23 11:26:39 +00:00
putStorageSearch ( sr )
return nil , fmt . Errorf ( "error when searching in Prometheus data: %w" , err )
}
2019-05-22 21:16:55 +00:00
var rss Results
rss . tr = tr
rss . deadline = deadline
2020-04-26 13:25:35 +00:00
pts := make ( [ ] packedTimeseries , len ( orderedMetricNames ) )
for i , metricName := range orderedMetricNames {
pts [ i ] = packedTimeseries {
metricName : metricName ,
2020-04-27 05:13:41 +00:00
brs : m [ metricName ] ,
2020-09-23 11:26:39 +00:00
pd : pm [ metricName ] ,
2020-04-26 13:25:35 +00:00
}
2019-05-22 21:16:55 +00:00
}
2020-04-26 13:25:35 +00:00
rss . packedTimeseries = pts
2020-04-27 05:13:41 +00:00
rss . sr = sr
2020-11-04 14:46:10 +00:00
rss . tbf = tbf
2019-05-22 21:16:55 +00:00
return & rss , nil
}
2021-03-16 23:12:28 +00:00
var indexSearchDuration = metrics . NewHistogram ( ` vm_index_search_duration_seconds ` )
2020-11-04 14:46:10 +00:00
type blockRef struct {
partRef storage . PartRef
addr tmpBlockAddr
}
2022-06-27 09:53:46 +00:00
func setupTfss ( qt * querytracer . Tracer , tr storage . TimeRange , tagFilterss [ ] [ ] storage . TagFilter , maxMetrics int , deadline searchutils . Deadline ) ( [ ] * storage . TagFilters , error ) {
2019-05-22 21:16:55 +00:00
tfss := make ( [ ] * storage . TagFilters , 0 , len ( tagFilterss ) )
for _ , tagFilters := range tagFilterss {
tfs := storage . NewTagFilters ( )
for i := range tagFilters {
tf := & tagFilters [ i ]
2021-02-02 22:24:05 +00:00
if string ( tf . Key ) == "__graphite__" {
query := tf . Value
2022-06-27 09:53:46 +00:00
paths , err := vmstorage . SearchGraphitePaths ( qt , tr , query , maxMetrics , deadline . Deadline ( ) )
2021-02-02 22:24:05 +00:00
if err != nil {
return nil , fmt . Errorf ( "error when searching for Graphite paths for query %q: %w" , query , err )
}
2022-03-26 08:17:37 +00:00
if len ( paths ) >= maxMetrics {
return nil , fmt . Errorf ( "more than %d time series match Graphite query %q; " +
"either narrow down the query or increase the corresponding -search.max* command-line flag value" , maxMetrics , query )
2021-02-02 22:24:05 +00:00
}
tfs . AddGraphiteQuery ( query , paths , tf . IsNegative )
continue
}
2019-05-22 21:16:55 +00:00
if err := tfs . Add ( tf . Key , tf . Value , tf . IsNegative , tf . IsRegexp ) ; err != nil {
2020-06-30 19:58:18 +00:00
return nil , fmt . Errorf ( "cannot parse tag filter %s: %w" , tf , err )
2019-05-22 21:16:55 +00:00
}
}
tfss = append ( tfss , tfs )
}
return tfss , nil
}
2020-11-16 01:58:12 +00:00
func applyGraphiteRegexpFilter ( filter string , ss [ ] string ) ( [ ] string , error ) {
// Anchor filter regexp to the beginning of the string as Graphite does.
// See https://github.com/graphite-project/graphite-web/blob/3ad279df5cb90b211953e39161df416e54a84948/webapp/graphite/tags/localdatabase.py#L157
filter = "^(?:" + filter + ")"
re , err := regexp . Compile ( filter )
if err != nil {
return nil , fmt . Errorf ( "cannot parse regexp filter=%q: %w" , filter , err )
}
dst := ss [ : 0 ]
for _ , s := range ss {
if re . MatchString ( s ) {
dst = append ( dst , s )
}
}
return dst , nil
}