(rr ring.ReadRing, g *rulespb.RuleGroupDesc, disabledRuleGroups validation.DisabledRuleGroups, forBackup bool)
| 559 | } |
| 560 | |
| 561 | func (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 | |
| 600 | func ownsRuleGroupOrDisable(g *rulespb.RuleGroupDesc, disabledRuleGroups validation.DisabledRuleGroups) (bool, error) { |
| 601 | if ruleGroupDisabled(g, disabledRuleGroups) { |
no test coverage detected