Skip to content

Commit 23b77c3

Browse files
simonpasquierjohncminglilic
committed
Rule: update manager when all rule files are removed
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>
1 parent 4ef1b8d commit 23b77c3

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ We use *breaking :warning:* word for marking changes that are not backward compa
1111

1212
## Unreleased
1313

14+
### Fixed
15+
16+
* [#xxxx](https://github.com/thanos-io/thanos/pull/xxxx) Rule: update manager when all rule files are removed.
17+
1418
## [v0.15.0-rc.0](https://github.com/thanos-io/thanos/releases/tag/v0.15.0-rc.0) - 2020.08.26
1519

1620
Highlights:

pkg/rules/manager.go

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

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

pkg/rules/manager_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,50 @@ func TestManager_Rules(t *testing.T) {
320320
}()
321321
testRulesAgainstExamples(t, filepath.Join(curr, "../../examples/alerts"), thanosRuleMgr)
322322
}
323+
324+
func TestManagerUpdateWithNoRules(t *testing.T) {
325+
dir, err := ioutil.TempDir("", "test_rule_rule_groups")
326+
testutil.Ok(t, err)
327+
defer func() { testutil.Ok(t, os.RemoveAll(dir)) }()
328+
329+
testutil.Ok(t, ioutil.WriteFile(filepath.Join(dir, "no_strategy.yaml"), []byte(`
330+
groups:
331+
- name: "something1"
332+
rules:
333+
- alert: "some"
334+
expr: "up"
335+
`), os.ModePerm))
336+
337+
thanosRuleMgr := NewManager(
338+
context.Background(),
339+
nil,
340+
dir,
341+
rules.ManagerOptions{
342+
Logger: log.NewLogfmtLogger(os.Stderr),
343+
Queryable: nopQueryable{},
344+
},
345+
func(partialResponseStrategy storepb.PartialResponseStrategy) rules.QueryFunc {
346+
return func(ctx context.Context, q string, t time.Time) (promql.Vector, error) {
347+
return nil, nil
348+
}
349+
},
350+
nil,
351+
)
352+
353+
// We need to run the underlying rule managers to update them more than
354+
// once (otherwise there's a deadlock).
355+
thanosRuleMgr.Run()
356+
defer func() {
357+
thanosRuleMgr.Stop()
358+
}()
359+
360+
err = thanosRuleMgr.Update(1*time.Second, []string{
361+
filepath.Join(dir, "no_strategy.yaml"),
362+
})
363+
testutil.Ok(t, err)
364+
testutil.Equals(t, 1, len(thanosRuleMgr.RuleGroups()))
365+
366+
err = thanosRuleMgr.Update(1*time.Second, []string{})
367+
testutil.Ok(t, err)
368+
testutil.Equals(t, 0, len(thanosRuleMgr.RuleGroups()))
369+
}

0 commit comments

Comments
 (0)