Skip to content

Commit

Permalink
Use error variables.
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>
  • Loading branch information
pstibrany committed Apr 9, 2021
1 parent 6693661 commit 2581afa
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 51 deletions.
2 changes: 1 addition & 1 deletion pkg/ingester/ingester.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
9 changes: 3 additions & 6 deletions pkg/ingester/ingester_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
}
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/ingester/ingester_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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": {
Expand All @@ -3458,7 +3458,7 @@ func TestIngester_v2PushInstanceLimits(t *testing.T) {
cortexpb.API),
},
},
expectedErrType: &errMaxSamplesPushRateLimitReached{},
expectedErr: errMaxSamplesPushRateLimitReached,
},
}

Expand Down Expand Up @@ -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
})

Expand Down
50 changes: 10 additions & 40 deletions pkg/ingester/instance_limits.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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"
}

0 comments on commit 2581afa

Please sign in to comment.