Skip to content

Commit a4f913c

Browse files
authored
Check context cancellation every 128 iterations (#6159)
1 parent 24edba0 commit a4f913c

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

pkg/ingester/ingester.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1737,9 +1737,11 @@ func (i *Ingester) metricsForLabelMatchersCommon(ctx context.Context, req *clien
17371737
Metric: make([]*cortexpb.Metric, 0),
17381738
}
17391739

1740+
cnt := 0
17401741
for mergedSet.Next() {
1742+
cnt++
17411743
// Interrupt if the context has been canceled.
1742-
if ctx.Err() != nil {
1744+
if cnt%util.CheckContextEveryNIterations == 0 && ctx.Err() != nil {
17431745
return nil, cleanup, ctx.Err()
17441746
}
17451747

pkg/querier/series/series_set.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/prometheus/prometheus/util/annotations"
2828

2929
"github.com/cortexproject/cortex/pkg/querier/iterators"
30+
"github.com/cortexproject/cortex/pkg/util"
3031
)
3132

3233
// ConcreteSeriesSet implements storage.SeriesSet.
@@ -143,8 +144,8 @@ func MatrixToSeriesSet(sortSeries bool, m model.Matrix) storage.SeriesSet {
143144
// MetricsToSeriesSet creates a storage.SeriesSet from a []metric.Metric
144145
func MetricsToSeriesSet(ctx context.Context, sortSeries bool, ms []model.Metric) storage.SeriesSet {
145146
series := make([]storage.Series, 0, len(ms))
146-
for _, m := range ms {
147-
if ctx.Err() != nil {
147+
for i, m := range ms {
148+
if (i+1)%util.CheckContextEveryNIterations == 0 && ctx.Err() != nil {
148149
return storage.ErrSeriesSet(ctx.Err())
149150
}
150151
series = append(series, &ConcreteSeries{

pkg/storage/tsdb/inmemory_index_cache.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616

1717
storecache "github.com/thanos-io/thanos/pkg/store/cache"
1818
"github.com/thanos-io/thanos/pkg/tenancy"
19+
20+
"github.com/cortexproject/cortex/pkg/util"
1921
)
2022

2123
type InMemoryIndexCache struct {
@@ -147,8 +149,8 @@ func (c *InMemoryIndexCache) FetchMultiPostings(ctx context.Context, blockID uli
147149
blockIDKey := blockID.String()
148150
requests := 0
149151
hit := 0
150-
for _, key := range keys {
151-
if ctx.Err() != nil {
152+
for i, key := range keys {
153+
if (i+1)%util.CheckContextEveryNIterations == 0 && ctx.Err() != nil {
152154
c.commonMetrics.RequestTotal.WithLabelValues(storecache.CacheTypePostings, tenant).Add(float64(requests))
153155
c.commonMetrics.HitsTotal.WithLabelValues(storecache.CacheTypePostings, tenant).Add(float64(hit))
154156
return hits, misses
@@ -208,8 +210,8 @@ func (c *InMemoryIndexCache) FetchMultiSeries(ctx context.Context, blockID ulid.
208210
blockIDKey := blockID.String()
209211
requests := 0
210212
hit := 0
211-
for _, id := range ids {
212-
if ctx.Err() != nil {
213+
for i, id := range ids {
214+
if (i+1)%util.CheckContextEveryNIterations == 0 && ctx.Err() != nil {
213215
c.commonMetrics.RequestTotal.WithLabelValues(storecache.CacheTypeSeries, tenant).Add(float64(requests))
214216
c.commonMetrics.HitsTotal.WithLabelValues(storecache.CacheTypeSeries, tenant).Add(float64(hit))
215217
return hits, misses

pkg/util/context.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package util
2+
3+
const (
4+
// CheckContextEveryNIterations is used in some tight loops to check if the context is done.
5+
CheckContextEveryNIterations = 128
6+
)

0 commit comments

Comments
 (0)