-
Notifications
You must be signed in to change notification settings - Fork 806
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow rules to be loaded to rulers as backup for List rules API HA
Signed-off-by: Emmanuel Lodovice <[email protected]>
- Loading branch information
1 parent
9704cc3
commit a37e449
Showing
11 changed files
with
1,124 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package ruler | ||
|
||
import ( | ||
"time" | ||
|
||
promRules "github.com/prometheus/prometheus/rules" | ||
) | ||
|
||
// mergeGroupStateDesc removes duplicates from the provided []*GroupStateDesc by keeping the GroupStateDesc with the | ||
// latest information. It uses the EvaluationTimestamp of the GroupStateDesc and the EvaluationTimestamp of the | ||
// ActiveRules in a GroupStateDesc to determine the which GroupStateDesc has the latest information. | ||
func mergeGroupStateDesc(in []*GroupStateDesc) []*GroupStateDesc { | ||
states := make(map[string]*GroupStateDesc) | ||
rgTime := make(map[string]time.Time) | ||
for _, state := range in { | ||
latestTs := state.EvaluationTimestamp | ||
for _, r := range state.ActiveRules { | ||
if latestTs.Before(r.EvaluationTimestamp) { | ||
latestTs = r.EvaluationTimestamp | ||
} | ||
} | ||
key := promRules.GroupKey(state.Group.Namespace, state.Group.Name) | ||
ts, ok := rgTime[key] | ||
if !ok || ts.Before(latestTs) { | ||
states[key] = state | ||
rgTime[key] = latestTs | ||
} | ||
} | ||
groups := make([]*GroupStateDesc, 0, len(states)) | ||
for _, state := range states { | ||
groups = append(groups, state) | ||
} | ||
return groups | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package ruler | ||
|
||
import ( | ||
"reflect" | ||
"slices" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cortexproject/cortex/pkg/ruler/rulespb" | ||
) | ||
|
||
func TestMergeGroupStateDesc(t *testing.T) { | ||
curTime := time.Now() | ||
r := rulespb.RuleDesc{ | ||
Expr: "1 > 1", | ||
} | ||
g1 := rulespb.RuleGroupDesc{ | ||
Name: "g1", | ||
Namespace: "ns1", | ||
} | ||
g2 := rulespb.RuleGroupDesc{ | ||
Name: "g2", | ||
Namespace: "ns1", | ||
} | ||
rs1 := RuleStateDesc{ | ||
Rule: &r, | ||
EvaluationTimestamp: curTime, | ||
} | ||
rs1NotRun := RuleStateDesc{ | ||
Rule: &r, | ||
} | ||
rs2 := RuleStateDesc{ | ||
Rule: &r, | ||
EvaluationTimestamp: curTime, | ||
} | ||
rs2NotRun := RuleStateDesc{ | ||
Rule: &r, | ||
} | ||
rs3 := RuleStateDesc{ | ||
Rule: &r, | ||
EvaluationTimestamp: curTime.Add(10 * time.Second), | ||
} | ||
|
||
gs1 := GroupStateDesc{ | ||
Group: &g1, | ||
ActiveRules: []*RuleStateDesc{&rs1, &rs2}, | ||
EvaluationTimestamp: curTime, | ||
} | ||
gs1NotRun := GroupStateDesc{ | ||
Group: &g1, | ||
ActiveRules: []*RuleStateDesc{&rs1NotRun, &rs2NotRun}, | ||
} | ||
gs2 := GroupStateDesc{ | ||
Group: &g2, | ||
ActiveRules: []*RuleStateDesc{&rs1, &rs2}, | ||
EvaluationTimestamp: curTime, | ||
} | ||
gs2NotRun := GroupStateDesc{ | ||
Group: &g2, | ||
ActiveRules: []*RuleStateDesc{&rs1NotRun, &rs2NotRun}, | ||
} | ||
gs3 := GroupStateDesc{ | ||
Group: &g2, | ||
ActiveRules: []*RuleStateDesc{&rs1, &rs3}, | ||
EvaluationTimestamp: curTime, | ||
} | ||
|
||
type testCase struct { | ||
input []*GroupStateDesc | ||
expectedOutput []*GroupStateDesc | ||
} | ||
|
||
testCases := map[string]testCase{ | ||
"No duplicate": { | ||
input: []*GroupStateDesc{&gs1, &gs2}, | ||
expectedOutput: []*GroupStateDesc{&gs1, &gs2}, | ||
}, | ||
"No duplicate but not evaluated": { | ||
input: []*GroupStateDesc{&gs1NotRun, &gs2NotRun}, | ||
expectedOutput: []*GroupStateDesc{&gs1NotRun, &gs2NotRun}, | ||
}, | ||
"With exact duplicate": { | ||
input: []*GroupStateDesc{&gs1, &gs2NotRun, &gs1, &gs2NotRun}, | ||
expectedOutput: []*GroupStateDesc{&gs1, &gs2NotRun}, | ||
}, | ||
"With duplicates that are not evaluated": { | ||
input: []*GroupStateDesc{&gs1, &gs2, &gs1NotRun, &gs2NotRun}, | ||
expectedOutput: []*GroupStateDesc{&gs1, &gs2}, | ||
}, | ||
"With duplicate with a new newer rule evaluation": { | ||
input: []*GroupStateDesc{&gs3, &gs1, &gs2, &gs1NotRun}, | ||
expectedOutput: []*GroupStateDesc{&gs1, &gs3}, | ||
}, | ||
} | ||
|
||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
out := mergeGroupStateDesc(tc.input) | ||
slices.SortFunc(out, func(a, b *GroupStateDesc) int { | ||
fileCompare := strings.Compare(a.Group.Namespace, b.Group.Namespace) | ||
if fileCompare != 0 { | ||
return fileCompare | ||
} | ||
return strings.Compare(a.Group.Name, b.Group.Name) | ||
}) | ||
require.Equal(t, len(tc.expectedOutput), len(out)) | ||
require.True(t, reflect.DeepEqual(tc.expectedOutput, out)) | ||
}) | ||
} | ||
|
||
} |
Oops, something went wrong.