Skip to content

Commit 0ac12db

Browse files
simonpasquierjohncminglilic
authored andcommitted
Rule: update manager when all rule files are removed (thanos-io#3095)
This bug was already fixed in thanos-io#2615 but it got lost when we merged thanos-io#2200. Co-authored-by: johncming <johncming@yahoo.com> Co-authored-by: Lili Cosic <cosiclili@gmail.com> Signed-off-by: Simon Pasquier <spasquie@redhat.com> Co-authored-by: johncming <johncming@yahoo.com> Co-authored-by: Lili Cosic <cosiclili@gmail.com>
1 parent 47520bc commit 0ac12db

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

pkg/rules/manager.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,12 @@ func (m *Manager) Update(evalInterval time.Duration, files []string) error {
313313
ruleFiles = map[string]string{}
314314
)
315315

316+
// Initialize filesByStrategy for existing managers' strategies to make
317+
// sure that managers are updated when they have no rules configured.
318+
for strategy := range m.mgrs {
319+
filesByStrategy[strategy] = make([]string, 0)
320+
}
321+
316322
if err := os.RemoveAll(m.workDir); err != nil {
317323
return errors.Wrapf(err, "remove %s", m.workDir)
318324
}

pkg/rules/manager_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,50 @@ func TestManager_Rules(t *testing.T) {
338338
}()
339339
testRulesAgainstExamples(t, filepath.Join(curr, "../../examples/alerts"), thanosRuleMgr)
340340
}
341+
342+
func TestManagerUpdateWithNoRules(t *testing.T) {
343+
dir, err := ioutil.TempDir("", "test_rule_rule_groups")
344+
testutil.Ok(t, err)
345+
defer func() { testutil.Ok(t, os.RemoveAll(dir)) }()
346+
347+
testutil.Ok(t, ioutil.WriteFile(filepath.Join(dir, "no_strategy.yaml"), []byte(`
348+
groups:
349+
- name: "something1"
350+
rules:
351+
- alert: "some"
352+
expr: "up"
353+
`), os.ModePerm))
354+
355+
thanosRuleMgr := NewManager(
356+
context.Background(),
357+
nil,
358+
dir,
359+
rules.ManagerOptions{
360+
Logger: log.NewLogfmtLogger(os.Stderr),
361+
Queryable: nopQueryable{},
362+
},
363+
func(partialResponseStrategy storepb.PartialResponseStrategy) rules.QueryFunc {
364+
return func(ctx context.Context, q string, t time.Time) (promql.Vector, error) {
365+
return nil, nil
366+
}
367+
},
368+
nil,
369+
)
370+
371+
// We need to run the underlying rule managers to update them more than
372+
// once (otherwise there's a deadlock).
373+
thanosRuleMgr.Run()
374+
defer func() {
375+
thanosRuleMgr.Stop()
376+
}()
377+
378+
err = thanosRuleMgr.Update(1*time.Second, []string{
379+
filepath.Join(dir, "no_strategy.yaml"),
380+
})
381+
testutil.Ok(t, err)
382+
testutil.Equals(t, 1, len(thanosRuleMgr.RuleGroups()))
383+
384+
err = thanosRuleMgr.Update(1*time.Second, []string{})
385+
testutil.Ok(t, err)
386+
testutil.Equals(t, 0, len(thanosRuleMgr.RuleGroups()))
387+
}

0 commit comments

Comments
 (0)