(ctx context.Context)
| 688 | } |
| 689 | |
| 690 | func (r *Ruler) run(ctx context.Context) error { |
| 691 | level.Info(r.logger).Log("msg", "ruler up and running") |
| 692 | |
| 693 | tick := time.NewTicker(r.cfg.PollInterval) |
| 694 | defer tick.Stop() |
| 695 | |
| 696 | var ringTickerChan <-chan time.Time |
| 697 | var ringLastState ring.ReplicationSet |
| 698 | |
| 699 | if r.cfg.EnableSharding { |
| 700 | ringLastState, _ = r.ring.GetAllHealthy(RingOp) |
| 701 | ringTicker := time.NewTicker(util.DurationWithJitter(r.cfg.RingCheckPeriod, 0.2)) |
| 702 | defer ringTicker.Stop() |
| 703 | ringTickerChan = ringTicker.C |
| 704 | } |
| 705 | |
| 706 | if r.userIndexUpdater != nil { |
| 707 | go r.userIndexUpdateLoop(ctx) |
| 708 | } |
| 709 | |
| 710 | syncRuleErrMsg := func(syncRulesErr error) { |
| 711 | level.Error(r.logger).Log("msg", "failed to sync rules", "err", syncRulesErr) |
| 712 | } |
| 713 | |
| 714 | initialSyncErr := r.syncRules(ctx, rulerSyncReasonInitial) |
| 715 | if initialSyncErr != nil { |
| 716 | syncRuleErrMsg(initialSyncErr) |
| 717 | } |
| 718 | for { |
| 719 | var syncRulesErr error |
| 720 | select { |
| 721 | case <-ctx.Done(): |
| 722 | return nil |
| 723 | case <-tick.C: |
| 724 | syncRulesErr = r.syncRules(ctx, rulerSyncReasonPeriodic) |
| 725 | case <-ringTickerChan: |
| 726 | // We ignore the error because in case of error it will return an empty |
| 727 | // replication set which we use to compare with the previous state. |
| 728 | currRingState, _ := r.ring.GetAllHealthy(RingOp) |
| 729 | |
| 730 | if ring.HasReplicationSetChanged(ringLastState, currRingState) { |
| 731 | ringLastState = currRingState |
| 732 | syncRulesErr = r.syncRules(ctx, rulerSyncReasonRingChange) |
| 733 | } |
| 734 | case err := <-r.subservicesWatcher.Chan(): |
| 735 | return errors.Wrap(err, "ruler subservice failed") |
| 736 | } |
| 737 | if syncRulesErr != nil { |
| 738 | syncRuleErrMsg(syncRulesErr) |
| 739 | } |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | func (r *Ruler) userIndexUpdateLoop(ctx context.Context) { |
| 744 | // Hardcode ID to check which ruler owns updating user index. |
no test coverage detected