MCPcopy Index your code
hub / github.com/cortexproject/cortex / instanceOwnsRuleGroup

Method instanceOwnsRuleGroup

pkg/ruler/ruler.go:561–598  ·  view source on GitHub ↗
(rr ring.ReadRing, g *rulespb.RuleGroupDesc, disabledRuleGroups validation.DisabledRuleGroups, forBackup bool)

Source from the content-addressed store, hash-verified

559}
560
561func (r *Ruler) instanceOwnsRuleGroup(rr ring.ReadRing, g *rulespb.RuleGroupDesc, disabledRuleGroups validation.DisabledRuleGroups, forBackup bool) (bool, error) {
562
563 hash := tokenForGroup(g)
564
565 rlrs, err := rr.Get(hash, RingOp, nil, nil, nil)
566 if err != nil {
567 return false, errors.Wrap(err, "error reading ring to verify rule group ownership")
568 }
569
570 instanceAddr := r.lifecycler.GetInstanceAddr()
571 if forBackup {
572 // Only the second up to the last replica are used as backup
573 for i := 1; i < len(rlrs.Instances); i++ {
574 if rlrs.Instances[i].Addr == instanceAddr {
575 return ownsRuleGroupOrDisable(g, disabledRuleGroups)
576 }
577 }
578 return false, nil
579 }
580 if r.Config().EnableHAEvaluation {
581 for i, ruler := range rlrs.Instances {
582 if ruler.Addr == instanceAddr && i == 0 {
583 level.Debug(r.Logger()).Log("msg", "primary taking ownership", "user", g.User, "group", g.Name, "namespace", g.Namespace, "ruler", instanceAddr)
584 return ownsRuleGroupOrDisable(g, disabledRuleGroups)
585 }
586 if ruler.Addr == instanceAddr && r.nonPrimaryInstanceOwnsRuleGroup(g, rlrs.GetAddresses()[:i]) {
587 level.Info(r.Logger()).Log("msg", "non-primary ruler taking ownership", "user", g.User, "group", g.Name, "namespace", g.Namespace, "ruler", instanceAddr)
588 return ownsRuleGroupOrDisable(g, disabledRuleGroups)
589 }
590 }
591 return false, nil
592 }
593 // Even if the replication factor is set to a number bigger than 1, only the first ruler evaluates the rule group
594 if rlrs.Instances[0].Addr == instanceAddr {
595 return ownsRuleGroupOrDisable(g, disabledRuleGroups)
596 }
597 return false, nil
598}
599
600func ownsRuleGroupOrDisable(g *rulespb.RuleGroupDesc, disabledRuleGroups validation.DisabledRuleGroups) (bool, error) {
601 if ruleGroupDisabled(g, disabledRuleGroups) {

Callers 2

filterRuleGroupsMethod · 0.95

Calls 10

ConfigMethod · 0.95
LoggerMethod · 0.95
tokenForGroupFunction · 0.85
ownsRuleGroupOrDisableFunction · 0.85
GetInstanceAddrMethod · 0.80
GetMethod · 0.65
WrapMethod · 0.65
LogMethod · 0.45
GetAddressesMethod · 0.45

Tested by

no test coverage detected