From 2581afa4aafa7722d8105e822fb5968de804d142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20S=CC=8Ctibrany=CC=81?= Date: Fri, 9 Apr 2021 15:23:43 +0200 Subject: [PATCH] Use error variables. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Peter Štibraný --- pkg/ingester/ingester.go | 2 +- pkg/ingester/ingester_v2.go | 9 ++---- pkg/ingester/ingester_v2_test.go | 8 ++--- pkg/ingester/instance_limits.go | 50 +++++++------------------------- 4 files changed, 18 insertions(+), 51 deletions(-) diff --git a/pkg/ingester/ingester.go b/pkg/ingester/ingester.go index 27197a80b9..1ff82cfa02 100644 --- a/pkg/ingester/ingester.go +++ b/pkg/ingester/ingester.go @@ -466,7 +466,7 @@ func (i *Ingester) Push(ctx context.Context, req *cortexpb.WriteRequest) (*corte gl := i.getInstanceLimits() if gl != nil && gl.MaxInflightPushRequests > 0 { if inflight > gl.MaxInflightPushRequests { - return nil, errTooManyInflightPushRequests{requests: inflight, limit: gl.MaxInflightPushRequests} + return nil, errTooManyInflightPushRequests } } diff --git a/pkg/ingester/ingester_v2.go b/pkg/ingester/ingester_v2.go index e5cd409944..bc0838d4fd 100644 --- a/pkg/ingester/ingester_v2.go +++ b/pkg/ingester/ingester_v2.go @@ -221,10 +221,7 @@ func (u *userTSDB) PreCreation(metric labels.Labels) error { gl := u.instanceLimitsFn() if gl != nil && gl.MaxInMemorySeries > 0 { if series := u.instanceSeriesCount.Load(); series >= gl.MaxInMemorySeries { - return errMaxSeriesLimitReached{ - series: series, - limit: gl.MaxInMemorySeries, - } + return errMaxSeriesLimitReached } } @@ -710,7 +707,7 @@ func (i *Ingester) v2Push(ctx context.Context, req *cortexpb.WriteRequest) (*cor il := i.getInstanceLimits() if il != nil && il.MaxIngestionRate > 0 { if rate := i.ingestionRate.rate(); rate >= il.MaxIngestionRate { - return nil, errMaxSamplesPushRateLimitReached{rate: rate, limit: il.MaxIngestionRate} + return nil, errMaxSamplesPushRateLimitReached } } @@ -1420,7 +1417,7 @@ func (i *Ingester) getOrCreateTSDB(userID string, force bool) (*userTSDB, error) gl := i.getInstanceLimits() if gl != nil && gl.MaxInMemoryTenants > 0 { if users := int64(len(i.TSDBState.dbs)); users >= gl.MaxInMemoryTenants { - return nil, errMaxUsersLimitReached{users: users, limit: gl.MaxInMemoryTenants} + return nil, errMaxUsersLimitReached } } diff --git a/pkg/ingester/ingester_v2_test.go b/pkg/ingester/ingester_v2_test.go index 4e443bfeb3..3724783b68 100644 --- a/pkg/ingester/ingester_v2_test.go +++ b/pkg/ingester/ingester_v2_test.go @@ -3414,7 +3414,7 @@ func TestIngester_v2PushInstanceLimits(t *testing.T) { }, }, - expectedErr: wrapWithUser(errMaxSeriesLimitReached{limit: 1, series: 1}, "test"), + expectedErr: wrapWithUser(errMaxSeriesLimitReached, "test"), }, "should fail creating two users": { @@ -3437,7 +3437,7 @@ func TestIngester_v2PushInstanceLimits(t *testing.T) { cortexpb.API), }, }, - expectedErr: wrapWithUser(errMaxUsersLimitReached{users: 1, limit: 1}, "user2"), + expectedErr: wrapWithUser(errMaxUsersLimitReached, "user2"), }, "should fail pushing samples in two requests due to rate limit": { @@ -3458,7 +3458,7 @@ func TestIngester_v2PushInstanceLimits(t *testing.T) { cortexpb.API), }, }, - expectedErrType: &errMaxSamplesPushRateLimitReached{}, + expectedErr: errMaxSamplesPushRateLimitReached, }, } @@ -3630,7 +3630,7 @@ func TestIngester_inflightPushRequests(t *testing.T) { req := generateSamplesForLabel(labels.FromStrings(labels.MetricName, "testcase"), 1024) _, err := i.Push(ctx, req) - require.Equal(t, errTooManyInflightPushRequests{requests: 2, limit: 1}, err) + require.Equal(t, errTooManyInflightPushRequests, err) return nil }) diff --git a/pkg/ingester/instance_limits.go b/pkg/ingester/instance_limits.go index a1cfd0be19..db834bb8f0 100644 --- a/pkg/ingester/instance_limits.go +++ b/pkg/ingester/instance_limits.go @@ -1,5 +1,15 @@ package ingester +import "github.com/pkg/errors" + +var ( + // We don't include values in the message to avoid leaking Cortex cluster configuration to users. + errMaxSamplesPushRateLimitReached = errors.New("cannot push more samples: ingester's max samples push rate reached") + errMaxUsersLimitReached = errors.New("cannot create TSDB: ingesters's max tenants limit reached") + errMaxSeriesLimitReached = errors.New("cannot add series: ingesters's max series limit reached") + errTooManyInflightPushRequests = errors.New("cannot push: too many inflight push requests") +) + // InstanceLimits describes limits used by ingester. Reaching any of these will result in Push method to return // (internal) error. type InstanceLimits struct { @@ -20,43 +30,3 @@ func (l *InstanceLimits) UnmarshalYAML(unmarshal func(interface{}) error) error type plain InstanceLimits // type indirection to make sure we don't go into recursive loop return unmarshal((*plain)(l)) } - -type errMaxSamplesPushRateLimitReached struct { - rate float64 - limit float64 -} - -func (e errMaxSamplesPushRateLimitReached) Error() string { - // We don't include values in the message to avoid leaking Cortex cluster configuration to users. - return "cannot push more samples: ingester's max samples push rate reached" -} - -type errMaxUsersLimitReached struct { - users int64 - limit int64 -} - -func (e errMaxUsersLimitReached) Error() string { - // We don't include values in the message to avoid leaking Cortex cluster configuration to users. - return "cannot create TSDB: ingesters's max tenants limit reached" -} - -type errMaxSeriesLimitReached struct { - series int64 - limit int64 -} - -func (e errMaxSeriesLimitReached) Error() string { - // We don't include values in the message to avoid leaking Cortex cluster configuration to users. - return "cannot add series: ingesters's max series limit reached" -} - -type errTooManyInflightPushRequests struct { - requests int64 - limit int64 -} - -func (e errTooManyInflightPushRequests) Error() string { - // We don't include values in the message to avoid leaking Cortex cluster configuration to users. - return "cannot push: too many inflight push requests" -}