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

Small fix for off-by-one rules limit enforcement #3616

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* [BUGFIX] Query-frontend: Fix issue where cached entry size keeps increasing when making tiny query repeatedly. #3968
* [BUGFIX] Compactor: `-compactor.blocks-retention-period` now supports weeks (`w`) and years (`y`). #4027
* [BUGFIX] Querier: returning 422 (instead of 500) when query hits `max_chunks_per_query` limit with block storage, when the limit is hit in the store-gateway. #3937
* [BUGFIX] Ruler: Rule group limit enforcement should now allow the same number of rules in a group as the limit. #3615

## Blocksconvert

Expand Down
2 changes: 1 addition & 1 deletion pkg/ruler/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ func (a *API) CreateRuleGroup(w http.ResponseWriter, req *http.Request) {
return
}

if err := a.ruler.AssertMaxRuleGroups(userID, len(rgs)); err != nil {
if err := a.ruler.AssertMaxRuleGroups(userID, len(rgs)+1); err != nil {
level.Error(logger).Log("msg", "limit validation failure", "err", err.Error(), "user", userID)
http.Error(w, err.Error(), http.StatusBadRequest)
return
Expand Down
62 changes: 56 additions & 6 deletions pkg/ruler/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func TestRuler_DeleteNamespace(t *testing.T) {
require.Equal(t, "{\"status\":\"error\",\"data\":null,\"errorType\":\"server_error\",\"error\":\"unable to delete rg\"}", w.Body.String())
}

func TestRuler_Limits(t *testing.T) {
func TestRuler_LimitsPerGroup(t *testing.T) {
cfg, cleanup := defaultRulerConfig(newMockRuleStore(make(map[string]rulespb.RuleGroupList)))
defer cleanup()

Expand Down Expand Up @@ -338,24 +338,74 @@ rules:
`,
output: "per-user rules per rule group limit (limit: 1 actual: 2) exceeded\n",
},
}

for _, tt := range tc {
t.Run(tt.name, func(t *testing.T) {
router := mux.NewRouter()
router.Path("/api/v1/rules/{namespace}").Methods("POST").HandlerFunc(a.CreateRuleGroup)
// POST
req := requestFor(t, http.MethodPost, "https://localhost:8080/api/v1/rules/namespace", strings.NewReader(tt.input), "user1")
w := httptest.NewRecorder()

router.ServeHTTP(w, req)
require.Equal(t, tt.status, w.Code)
require.Equal(t, tt.output, w.Body.String())
})
}
}

func TestRuler_RulerGroupLimits(t *testing.T) {
cfg, cleanup := defaultRulerConfig(newMockRuleStore(make(map[string]rulespb.RuleGroupList)))
defer cleanup()

r, rcleanup := newTestRuler(t, cfg)
defer rcleanup()
defer services.StopAndAwaitTerminated(context.Background(), r) //nolint:errcheck

r.limits = &ruleLimits{maxRuleGroups: 1, maxRulesPerRuleGroup: 1}

a := NewAPI(r, r.store, log.NewNopLogger())

tc := []struct {
name string
input string
output string
err error
status int
}{
{
name: "when pushing the first group within bounds of the limit",
status: 202,
input: `
name: test_first_group_will_succeed
interval: 15s
rules:
- record: up_rule
expr: up{}
`,
output: "{\"status\":\"success\",\"data\":null,\"errorType\":\"\",\"error\":\"\"}",
},
{
name: "when exceeding the rule group limit",
name: "when exceeding the rule group limit after sending the first group",
status: 400,
input: `
name: test
name: test_second_group_will_fail
interval: 15s
rules:
- record: up_rule
expr: up{}
`,
output: "per-user rules per rule group limit (limit: 1 actual: 1) exceeded\n",
output: "per-user rule groups limit (limit: 1 actual: 2) exceeded\n",
},
}

// define once so the requests build on each other so the number of rules can be tested
router := mux.NewRouter()
router.Path("/api/v1/rules/{namespace}").Methods("POST").HandlerFunc(a.CreateRuleGroup)

for _, tt := range tc {
t.Run(tt.name, func(t *testing.T) {
router := mux.NewRouter()
router.Path("/api/v1/rules/{namespace}").Methods("POST").HandlerFunc(a.CreateRuleGroup)
// POST
req := requestFor(t, http.MethodPost, "https://localhost:8080/api/v1/rules/namespace", strings.NewReader(tt.input), "user1")
w := httptest.NewRecorder()
Expand Down
4 changes: 2 additions & 2 deletions pkg/ruler/ruler.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ func (r *Ruler) AssertMaxRuleGroups(userID string, rg int) error {
return nil
}

if rg < limit {
if rg <= limit {
return nil
}

Expand All @@ -786,7 +786,7 @@ func (r *Ruler) AssertMaxRulesPerRuleGroup(userID string, rules int) error {
return nil
}

if rules < limit {
if rules <= limit {
return nil
}
return fmt.Errorf(errMaxRulesPerRuleGroupPerUserLimitExceeded, limit, rules)
Expand Down