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

[coordinator] Add WriteNamespaces API to rules store #3001

Merged
merged 1 commit into from
Dec 10, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/metrics/rules/rules_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/metrics/rules/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type Store interface {
// WriteRuleSet saves the given ruleset to the backing store.
WriteRuleSet(rs MutableRuleSet) error

// WriteNamespaces saves the given namespaces to the backing store.
WriteNamespaces(nss *Namespaces) error

// WriteAll saves both the given ruleset and namespace to the backing store.
WriteAll(nss *Namespaces, rs MutableRuleSet) error

Expand Down
10 changes: 10 additions & 0 deletions src/metrics/rules/store/kv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ func (s *store) WriteRuleSet(rs rules.MutableRuleSet) error {
return wrapWriteError(err)
}

func (s *store) WriteNamespaces(nss *rules.Namespaces) error {
namespacesCond, namespacesOp, err := s.namespacesTransaction(nss)
if err != nil {
return err
}
conditions, ops := []kv.Condition{namespacesCond}, []kv.Op{namespacesOp}
_, err = s.kvStore.Commit(conditions, ops)
return wrapWriteError(err)
}

func (s *store) WriteAll(nss *rules.Namespaces, rs rules.MutableRuleSet) error {
if s.opts.Validator != nil {
if err := s.opts.Validator.Validate(rs); err != nil {
Expand Down
50 changes: 50 additions & 0 deletions src/metrics/rules/store/kv/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,56 @@ func TestWriteRuleSetStaleDataError(t *testing.T) {
require.IsType(t, merrors.NewStaleDataError(""), err)
}

func TestWriteNamespace(t *testing.T) {
s := testStore()
defer s.Close()

nss, err := rules.NewNamespaces(0, testNamespaces)
require.NoError(t, err)

err = s.WriteNamespaces(&nss)
require.NoError(t, err)

existing, err := s.ReadNamespaces()
require.NoError(t, err)

revived, err := existing.AddNamespace(
"new",
rules.NewRuleSetUpdateHelper(0).NewUpdateMetadata(time.Now().UnixNano(), "test"),
)
require.NoError(t, err)
require.False(t, revived)

// Update should succeed
err = s.WriteNamespaces(existing)
require.NoError(t, err)
}

func TestWriteNamespaceError(t *testing.T) {
s := testStore()
defer s.Close()

err := s.WriteNamespaces(nil)
require.Error(t, err)
}

func TestWriteNamespacesStaleDataError(t *testing.T) {
s := testStore()
defer s.Close()

nss, err := rules.NewNamespaces(0, testNamespaces)
require.NoError(t, err)

// First write should succeed
err = s.WriteNamespaces(&nss)
require.NoError(t, err)

// writing again will encounter stale version
err = s.WriteNamespaces(&nss)
require.Error(t, err)
require.IsType(t, merrors.NewStaleDataError(""), err)
}

func TestWriteAllNoNamespace(t *testing.T) {
s := testStore()
defer s.Close()
Expand Down