Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement max series limit in distributor for /series API #4683

Merged

Conversation

harry671003
Copy link
Contributor

@harry671003 harry671003 commented Mar 22, 2022

Signed-off-by: 🌲 Harry 🌊 John 🏔 [email protected]

What this PR does:
This PR enforces the max_fetched_series_per_query limit for /api/v1/series API.

Which issue(s) this PR fixes:
Fixes #4667

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

@harry671003 harry671003 changed the title Implement max series limit for distributor Implement max series limit in distributor for /series API Mar 22, 2022
@@ -97,10 +98,22 @@ func (q *distributorQuerier) Select(_ bool, sp *storage.SelectHints, matchers ..
// See: https://github.com/prometheus/prometheus/pull/8050
if sp != nil && sp.Func == "series" {
ms, err := q.distributor.MetricsForLabelMatchers(ctx, model.Time(q.mint), model.Time(q.maxt), matchers...)
queryLimiter := limiter.QueryLimiterFromContextWithFallback(ctx)
Copy link
Member

@alanprot alanprot Mar 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change is easier if we do inside the MetricsForLabelMatchers:

https://github.com/aws-observability/cortex-dev/blob/fc862f9c387bbc4f615d79d1a7854d0d3d21aa89/pkg/distributor/distributor.go#L973-L976

and use: queryLimiter.AddSeries(cortexpb.FromMetricsToLabelAdapters(m))

Another possible memory optimization on this code would be to not wait all resps and dedup those resps as soon as it arrive inside the ForReplicationSet

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback.

@harry671003 harry671003 force-pushed the distributor-series-limit branch from f48f299 to 3ddad82 Compare March 23, 2022 15:47
@harry671003 harry671003 force-pushed the distributor-series-limit branch 5 times, most recently from 03956af to 0edc672 Compare April 4, 2022 16:57
@alanprot
Copy link
Member

alanprot commented Apr 4, 2022

LGTM

queryLimiter: limiter.NewQueryLimiter(0, 0, 0),
expectedErr: nil,
},
"should return err if series limit is exhausted": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test where:

  1. There is 5 ingesters
  2. Replication factor is 5
  3. There is exactly 1 time series replicated across the 5 ingesters
  4. Set the max time series to 1

In this case we shouldn't hit the limit. The purpose of this test is to ensure that the limit is applied against de-duped time series. I know the QueryLimiter does de-dupe for you, but it's good to ensure the same from distributor perspective.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing. The replication factor is 3 by default. I added a test to cover the case where one series is queried with limit is set to 1.

if err := queryLimiter.AddSeries(cortexpb.FromMetricsToLabelAdapters(m)); err != nil {
return nil, err
}
mutex.Lock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this would you be able to write some go test -bench (or use existing one if any) to and post before and after?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a benchmark test BenchmarkDistributor_MetricsForLabelMatchers.

Without the locking:

❯ go test -run=^$ -bench ^BenchmarkDistributor_MetricsForLabelMatchers$ github.com/cortexproject/cortex/pkg/distributor -count 10
goos: darwin
goarch: amd64
pkg: github.com/cortexproject/cortex/pkg/distributor
cpu: Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
BenchmarkDistributor_MetricsForLabelMatchers/get_series_within_limits-8         	     775	   1553236 ns/op	  758952 B/op	    8580 allocs/op
BenchmarkDistributor_MetricsForLabelMatchers/get_series_within_limits-8         	     724	   1551052 ns/op	  759031 B/op	    8581 allocs/op
BenchmarkDistributor_MetricsForLabelMatchers/get_series_within_limits-8         	     898	   1281153 ns/op	  759056 B/op	    8581 allocs/op
BenchmarkDistributor_MetricsForLabelMatchers/get_series_within_limits-8         	    1064	   1145841 ns/op	  758964 B/op	    8580 allocs/op
BenchmarkDistributor_MetricsForLabelMatchers/get_series_within_limits-8         	    1102	   1074635 ns/op	  759169 B/op	    8581 allocs/op
BenchmarkDistributor_MetricsForLabelMatchers/get_series_within_limits-8         	    1123	   1068547 ns/op	  759050 B/op	    8580 allocs/op
BenchmarkDistributor_MetricsForLabelMatchers/get_series_within_limits-8         	    1108	   1080608 ns/op	  759064 B/op	    8581 allocs/op
BenchmarkDistributor_MetricsForLabelMatchers/get_series_within_limits-8         	    1129	   1052474 ns/op	  758646 B/op	    8578 allocs/op
BenchmarkDistributor_MetricsForLabelMatchers/get_series_within_limits-8         	    1129	   1123325 ns/op	  758817 B/op	    8579 allocs/op
BenchmarkDistributor_MetricsForLabelMatchers/get_series_within_limits-8         	    1084	   1360482 ns/op	  758859 B/op	    8580 allocs/op
PASS
ok  	github.com/cortexproject/cortex/pkg/distributor	14.253s

With the locking

❯ go test -run=^$ -bench ^BenchmarkDistributor_MetricsForLabelMatchers$ github.com/cortexproject/cortex/pkg/distributor -count 10
goos: darwin
goarch: amd64
pkg: github.com/cortexproject/cortex/pkg/distributor
cpu: Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
BenchmarkDistributor_MetricsForLabelMatchers/get_series_within_limits-8         	     847	   1519934 ns/op	  870517 B/op	    9174 allocs/op
BenchmarkDistributor_MetricsForLabelMatchers/get_series_within_limits-8         	     804	   1789515 ns/op	  870984 B/op	    9178 allocs/op
BenchmarkDistributor_MetricsForLabelMatchers/get_series_within_limits-8         	     542	   2008873 ns/op	  871077 B/op	    9179 allocs/op
BenchmarkDistributor_MetricsForLabelMatchers/get_series_within_limits-8         	     715	   1493901 ns/op	  871080 B/op	    9179 allocs/op
BenchmarkDistributor_MetricsForLabelMatchers/get_series_within_limits-8         	     793	   1619484 ns/op	  871042 B/op	    9179 allocs/op
BenchmarkDistributor_MetricsForLabelMatchers/get_series_within_limits-8         	     712	   1502533 ns/op	  870682 B/op	    9176 allocs/op
BenchmarkDistributor_MetricsForLabelMatchers/get_series_within_limits-8         	     872	   1594814 ns/op	  870655 B/op	    9176 allocs/op
BenchmarkDistributor_MetricsForLabelMatchers/get_series_within_limits-8         	     652	   1608638 ns/op	  870481 B/op	    9175 allocs/op
BenchmarkDistributor_MetricsForLabelMatchers/get_series_within_limits-8         	     806	   1452676 ns/op	  870680 B/op	    9176 allocs/op
BenchmarkDistributor_MetricsForLabelMatchers/get_series_within_limits-8         	     681	   1634886 ns/op	  870830 B/op	    9177 allocs/op
PASS
ok  	github.com/cortexproject/cortex/pkg/distributor	15.305s

I didn't see any significant differences.

@harry671003 harry671003 force-pushed the distributor-series-limit branch 2 times, most recently from f44d52e to eef0643 Compare April 5, 2022 21:40
@pull-request-size pull-request-size bot added size/L and removed size/M labels Apr 5, 2022
@harry671003 harry671003 force-pushed the distributor-series-limit branch from eef0643 to 5292564 Compare April 5, 2022 21:46
@harry671003 harry671003 force-pushed the distributor-series-limit branch from 5292564 to 7f5b13b Compare April 5, 2022 22:02
@harry671003 harry671003 requested a review from alvinlin123 April 6, 2022 15:22
@alvinlin123 alvinlin123 merged commit 4f82673 into cortexproject:master Apr 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

max_fetched_series_per_query limits are not respected for /series API for recent data
3 participants