Skip to content

Commit

Permalink
[coordinator] Add WriteNamespaces API to rules store
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleyk committed Dec 10, 2020
1 parent 232ab06 commit e3be5df
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
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

0 comments on commit e3be5df

Please sign in to comment.