Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule: update manager when all rule files are removed #3095

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,10 @@ We use *breaking :warning:* word for marking changes that are not backward compa

## Unreleased

### Fixed

* [#3095](https://github.com/thanos-io/thanos/pull/3095) Rule: update manager when all rule files are removed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a proper place, but that's fine I will fix.


## [v0.15.0-rc.0](https://github.com/thanos-io/thanos/releases/tag/v0.15.0-rc.0) - 2020.08.26

Highlights:
Expand Down
6 changes: 6 additions & 0 deletions pkg/rules/manager.go
Expand Up @@ -314,6 +314,12 @@ func (m *Manager) Update(evalInterval time.Duration, files []string) error {
ruleFiles = map[string]string{}
)

// Initialize filesByStrategy for existing managers' strategies to make
// sure that managers are updated when they have no rules configured.
for strategy := range m.mgrs {
filesByStrategy[strategy] = make([]string, 0)
}

if err := os.RemoveAll(m.workDir); err != nil {
return errors.Wrapf(err, "remove %s", m.workDir)
}
Expand Down
47 changes: 47 additions & 0 deletions pkg/rules/manager_test.go
Expand Up @@ -320,3 +320,50 @@ func TestManager_Rules(t *testing.T) {
}()
testRulesAgainstExamples(t, filepath.Join(curr, "../../examples/alerts"), thanosRuleMgr)
}

func TestManagerUpdateWithNoRules(t *testing.T) {
dir, err := ioutil.TempDir("", "test_rule_rule_groups")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(dir)) }()

testutil.Ok(t, ioutil.WriteFile(filepath.Join(dir, "no_strategy.yaml"), []byte(`
groups:
- name: "something1"
rules:
- alert: "some"
expr: "up"
`), os.ModePerm))

thanosRuleMgr := NewManager(
context.Background(),
nil,
dir,
rules.ManagerOptions{
Logger: log.NewLogfmtLogger(os.Stderr),
Queryable: nopQueryable{},
},
func(partialResponseStrategy storepb.PartialResponseStrategy) rules.QueryFunc {
return func(ctx context.Context, q string, t time.Time) (promql.Vector, error) {
return nil, nil
}
},
nil,
)

// We need to run the underlying rule managers to update them more than
// once (otherwise there's a deadlock).
thanosRuleMgr.Run()
defer func() {
thanosRuleMgr.Stop()
}()

err = thanosRuleMgr.Update(1*time.Second, []string{
filepath.Join(dir, "no_strategy.yaml"),
})
testutil.Ok(t, err)
testutil.Equals(t, 1, len(thanosRuleMgr.RuleGroups()))

err = thanosRuleMgr.Update(1*time.Second, []string{})
testutil.Ok(t, err)
testutil.Equals(t, 0, len(thanosRuleMgr.RuleGroups()))
}