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

Ruler: Add pagination for api/v1/rules #9563

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Conversation

fayzal-g
Copy link
Contributor

@fayzal-g fayzal-g commented Oct 8, 2024

No description provided.

@fayzal-g fayzal-g force-pushed the rule-pagination-replica branch 2 times, most recently from 6e07e93 to f832fe0 Compare October 9, 2024 09:54
@fayzal-g fayzal-g changed the title Pagination for api/v1/rules v2 Ruler: Add pagination for api/v1/rules Oct 11, 2024
@@ -951,10 +953,10 @@ func filterRuleGroupsByNotMissing(configs map[string]rulespb.RuleGroupList, miss
}

// GetRules retrieves the running rules from this ruler and all running rulers in the ring.
func (r *Ruler) GetRules(ctx context.Context, req RulesRequest) ([]*GroupStateDesc, error) {
func (r *Ruler) GetRules(ctx context.Context, req RulesRequest) ([]*GroupStateDesc, string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice if function could return RulesResponse, just so we add the token to that, just so it's named.

Not sure how invasive that is, so it could be done in a separate or follow up PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Discussed offline, but decided against this for now to prevent an API level struct being introduced to this function.

Comment on lines 1002 to 1010
slices.SortFunc(merged, func(a, b *GroupStateDesc) int {
nsCmp := strings.Compare(a.Group.Namespace, b.Group.Namespace)
if nsCmp != 0 {
return nsCmp
}

// If Namespaces are equal, check the group names
return strings.Compare(a.Group.Name, b.Group.Name)
})
Copy link
Contributor

Choose a reason for hiding this comment

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

Non-blocking nit: this might be over-engineering..... but I see a potential issue where someone updates the sort strategy in one place, and not the other (in api.go). Could we share them somehow? Define a "SortableGroup" interface or something?

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've removed the sorting in api.go - the groups will now get sorted after they're merged in GetRules()

// The mapped filename is url path escaped encoded to make handling `/` characters easier
decodedNamespace, err := url.PathUnescape(strings.TrimPrefix(group.File(), prefix))
if err != nil {
return nil, errors.Wrap(err, "unable to decode rule filename")
}

if req.NextToken != "" && !foundToken {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if req.NextToken != "" && !foundToken {
// If a pagination token is provided, skip past groups until we reach the point of that token.
if req.NextToken != "" && !foundToken {

@@ -1068,18 +1094,27 @@ func (r *Ruler) getLocalRules(ctx context.Context, userID string, req RulesReque
groupSet := makeStringFilterSet(req.RuleGroup)
ruleSet := makeStringFilterSet(req.RuleName)

foundToken := false
for _, group := range groups {
Copy link
Contributor

Choose a reason for hiding this comment

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

The rules are sorted at this point, right? From GetRules?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, it's done here:

slices.SortFunc(rgs, func(a, b *Group) int {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants