Like Prometheus, but for logs.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
loki/pkg/storage/chunk/client/util/parallel_chunk_fetch.go

85 lines
1.8 KiB

package util
import (
"context"
"sync"
"github.com/opentracing/opentracing-go"
otlog "github.com/opentracing/opentracing-go/log"
"github.com/grafana/loki/pkg/storage/chunk"
"github.com/grafana/loki/pkg/util/spanlogger"
)
var decodeContextPool = sync.Pool{
New: func() interface{} {
return chunk.NewDecodeContext()
},
}
// GetParallelChunks fetches chunks in parallel (up to maxParallel).
func GetParallelChunks(ctx context.Context, maxParallel int, chunks []chunk.Chunk, f func(context.Context, *chunk.DecodeContext, chunk.Chunk) (chunk.Chunk, error)) ([]chunk.Chunk, error) {
sp, ctx := opentracing.StartSpanFromContext(ctx, "GetParallelChunks")
defer sp.Finish()
log := spanlogger.FromContext(ctx)
defer log.Finish()
log.LogFields(otlog.Int("requested", len(chunks)))
Handle `context` cancellation in some of the `querier` downstream requests (#5080) * Fix deadlock in disconnecting querier Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * todo Signed-off-by: Kaviraj <kavirajkanagaraj@gmail.com> * Add more logs to queriers cancellation Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * new span Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Fixe log Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Exit earlier for batch iterator Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Add missing return Signed-off-by: Kaviraj <kavirajkanagaraj@gmail.com> * Add store context cancellation Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Fixes a possible cancellation issue Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Rmove code to find issues Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Add splitMiddleware back to handler Signed-off-by: Kaviraj <kavirajkanagaraj@gmail.com> * Remove query split Signed-off-by: Kaviraj <kavirajkanagaraj@gmail.com> * Remove split middleware and context cancel check for chunks * Handle context cancel properly on `getChunk()` via select. Signed-off-by: Kaviraj <kavirajkanagaraj@gmail.com> * Use context in getChunk without starting new goroutine Signed-off-by: Kaviraj <kavirajkanagaraj@gmail.com> * Just normal ctx.Err() check instead of using Done channel Signed-off-by: Kaviraj <kavirajkanagaraj@gmail.com> * Remove debug logs Signed-off-by: Kaviraj <kavirajkanagaraj@gmail.com> * Update pkg/querier/http.go Co-authored-by: Cyril Tovena <cyril.tovena@gmail.com> * Remove unused imports Co-authored-by: Cyril Tovena <cyril.tovena@gmail.com>
4 years ago
if ctx.Err() != nil {
return nil, ctx.Err()
}
queuedChunks := make(chan chunk.Chunk)
go func() {
for _, c := range chunks {
queuedChunks <- c
}
close(queuedChunks)
}()
processedChunks := make(chan chunk.Chunk)
errors := make(chan error)
for i := 0; i < min(maxParallel, len(chunks)); i++ {
go func() {
decodeContext := decodeContextPool.Get().(*chunk.DecodeContext)
for c := range queuedChunks {
c, err := f(ctx, decodeContext, c)
if err != nil {
errors <- err
} else {
processedChunks <- c
}
}
decodeContextPool.Put(decodeContext)
}()
}
result := make([]chunk.Chunk, 0, len(chunks))
var lastErr error
for i := 0; i < len(chunks); i++ {
select {
case chunk := <-processedChunks:
result = append(result, chunk)
case err := <-errors:
lastErr = err
}
}
log.LogFields(otlog.Int("fetched", len(result)))
if lastErr != nil {
log.Error(lastErr)
}
// Return any chunks we did receive: a partial result may be useful
return result, lastErr
}
func min(a, b int) int {
if a < b {
return a
}
return b
}