Skip to content

Commit 5a690fa

Browse files
committed
Change condition to check validation.LimitError
Signed-off-by: Justin Jung <[email protected]>
1 parent ef8473f commit 5a690fa

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

pkg/ring/replication_set.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ package ring
33
import (
44
"context"
55
"fmt"
6-
"net/http"
76
"sort"
87
"time"
98

10-
"github.com/weaveworks/common/httpgrpc"
11-
129
"github.com/cortexproject/cortex/pkg/querier/partialdata"
10+
"github.com/cortexproject/cortex/pkg/util/validation"
1311
)
1412

1513
// ReplicationSet describes the instances to talk to for a given key, and how
@@ -83,10 +81,8 @@ func (r ReplicationSet) Do(ctx context.Context, delay time.Duration, zoneResults
8381
return nil, res.err
8482
}
8583

86-
if httpRes, ok := httpgrpc.HTTPResponseFromError(res.err); ok {
87-
if httpRes.Code == http.StatusUnprocessableEntity {
88-
return nil, res.err
89-
}
84+
if validation.IsLimitError(res.err) {
85+
return nil, res.err
9086
}
9187

9288
// force one of the delayed requests to start

pkg/ring/replication_set_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ package ring
33
import (
44
"context"
55
"errors"
6-
"net/http"
76
"testing"
87
"time"
98

109
"github.com/stretchr/testify/assert"
1110
"github.com/stretchr/testify/require"
12-
"github.com/weaveworks/common/httpgrpc"
1311
"go.uber.org/atomic"
1412

1513
"github.com/cortexproject/cortex/pkg/querier/partialdata"
14+
"github.com/cortexproject/cortex/pkg/util/validation"
1615
)
1716

1817
func TestReplicationSet_GetAddresses(t *testing.T) {
@@ -221,16 +220,16 @@ func TestReplicationSet_Do(t *testing.T) {
221220
queryPartialData: true,
222221
},
223222
{
224-
name: "with partial data enabled, should fail on instances returning 422 in two zones",
225-
instances: []InstanceDesc{{Zone: "zone1"}, {Zone: "zone2"}, {Zone: "zone3"}},
223+
name: "with partial data enabled, should fail on instances returning 422",
224+
instances: []InstanceDesc{{Addr: "1", Zone: "zone1"}, {Addr: "2", Zone: "zone2"}, {Addr: "3", Zone: "zone3"}, {Addr: "4", Zone: "zone1"}, {Addr: "5", Zone: "zone2"}, {Addr: "6", Zone: "zone3"}},
226225
f: func(ctx context.Context, ing *InstanceDesc) (interface{}, error) {
227-
if ing.Zone == "zone1" || ing.Zone == "zone2" {
228-
return nil, httpgrpc.Errorf(http.StatusUnprocessableEntity, "breached limit")
226+
if ing.Addr == "1" || ing.Addr == "2" {
227+
return nil, validation.LimitError("limit breached")
229228
}
230229
return 1, nil
231230
},
232231
maxUnavailableZones: 1,
233-
expectedError: httpgrpc.Errorf(http.StatusUnprocessableEntity, "breached limit"),
232+
expectedError: validation.LimitError("limit breached"),
234233
queryPartialData: true,
235234
},
236235
{

pkg/util/validation/limits.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ func (e LimitError) Error() string {
5050
return string(e)
5151
}
5252

53+
func IsLimitError(e error) bool {
54+
var limitError LimitError
55+
return errors.As(e, &limitError)
56+
}
57+
5358
type DisabledRuleGroup struct {
5459
Namespace string `yaml:"namespace" doc:"nocli|description=namespace in which the rule group belongs"`
5560
Name string `yaml:"name" doc:"nocli|description=name of the rule group"`

pkg/util/validation/limits_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package validation
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"reflect"
67
"regexp"
78
"strings"
@@ -885,3 +886,8 @@ func TestLimitsPerLabelSetsForSeries(t *testing.T) {
885886
})
886887
}
887888
}
889+
890+
func TestIsLimitsError(t *testing.T) {
891+
assert.False(t, IsLimitError(fmt.Errorf("test error")))
892+
assert.True(t, IsLimitError(LimitError("test error")))
893+
}

0 commit comments

Comments
 (0)