Skip to content

Commit

Permalink
Add a paramter in state store methods to indicate whether a resource …
Browse files Browse the repository at this point in the history
…insertion is from a snapshot restoration (#9156)

The Catalog, Config Entry, KV and Session resources potentially re-validate the input as its coming in. We need to prevent snapshot restoration failures due to missing namespaces or namespaces that are being deleted in enterprise.
  • Loading branch information
mkeeler authored Nov 11, 2020
1 parent 37b1ab7 commit 71da020
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 79 deletions.
3 changes: 3 additions & 0 deletions .changelog/9156.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
namespace: **(Enterprise Only)** Fixed a bug that could case snapshot restoration to fail when it contained a namespace marked for deletion while still containing other resources in that namespace.
```
3 changes: 3 additions & 0 deletions .changelog/_666.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
namespace: **(Enterprise Only)** Fixed an issue where namespaced services and checks were not being deleted when the containing namespace was deleted.
```
8 changes: 4 additions & 4 deletions agent/consul/state/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (s *Snapshot) Checks(node string) (memdb.ResultIterator, error) {
// performed within a single transaction to avoid race conditions on state
// updates.
func (s *Restore) Registration(idx uint64, req *structs.RegisterRequest) error {
return s.store.ensureRegistrationTxn(s.tx, idx, true, req)
return s.store.ensureRegistrationTxn(s.tx, idx, true, req, true)
}

// EnsureRegistration is used to make sure a node, service, and check
Expand All @@ -273,7 +273,7 @@ func (s *Store) EnsureRegistration(idx uint64, req *structs.RegisterRequest) err
tx := s.db.WriteTxn(idx)
defer tx.Abort()

if err := s.ensureRegistrationTxn(tx, idx, false, req); err != nil {
if err := s.ensureRegistrationTxn(tx, idx, false, req, false); err != nil {
return err
}

Expand All @@ -294,8 +294,8 @@ func (s *Store) ensureCheckIfNodeMatches(tx WriteTxn, idx uint64, preserveIndexe
// ensureRegistrationTxn is used to make sure a node, service, and check
// registration is performed within a single transaction to avoid race
// conditions on state updates.
func (s *Store) ensureRegistrationTxn(tx WriteTxn, idx uint64, preserveIndexes bool, req *structs.RegisterRequest) error {
if _, err := validateRegisterRequestTxn(tx, req); err != nil {
func (s *Store) ensureRegistrationTxn(tx WriteTxn, idx uint64, preserveIndexes bool, req *structs.RegisterRequest, restore bool) error {
if _, err := validateRegisterRequestTxn(tx, req, restore); err != nil {
return err
}

Expand Down
136 changes: 68 additions & 68 deletions agent/consul/state/catalog_events_test.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion agent/consul/state/catalog_oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func catalogChecksForNodeService(tx ReadTxn, node string, service string, entMet
return tx.Get("checks", "node_service", node, service)
}

func validateRegisterRequestTxn(_ ReadTxn, _ *structs.RegisterRequest) (*structs.EnterpriseMeta, error) {
func validateRegisterRequestTxn(_ ReadTxn, _ *structs.RegisterRequest, _ bool) (*structs.EnterpriseMeta, error) {
return nil, nil
}

Expand Down
4 changes: 2 additions & 2 deletions agent/consul/state/kvs.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (s *Snapshot) Tombstones() (memdb.ResultIterator, error) {

// KVS is used when restoring from a snapshot. Use KVSSet for general inserts.
func (s *Restore) KVS(entry *structs.DirEntry) error {
if err := insertKVTxn(s.tx, entry, true); err != nil {
if err := insertKVTxn(s.tx, entry, true, true); err != nil {
return fmt.Errorf("failed inserting kvs entry: %s", err)
}

Expand Down Expand Up @@ -153,7 +153,7 @@ func kvsSetTxn(tx WriteTxn, idx uint64, entry *structs.DirEntry, updateSession b
entry.ModifyIndex = idx

// Store the kv pair in the state store and update the index.
if err := insertKVTxn(tx, entry, false); err != nil {
if err := insertKVTxn(tx, entry, false, false); err != nil {
return fmt.Errorf("failed inserting kvs entry: %s", err)
}

Expand Down
2 changes: 1 addition & 1 deletion agent/consul/state/kvs_oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func kvsIndexer() *memdb.StringFieldIndex {
}
}

func insertKVTxn(tx WriteTxn, entry *structs.DirEntry, updateMax bool) error {
func insertKVTxn(tx WriteTxn, entry *structs.DirEntry, updateMax bool, _ bool) error {
if err := tx.Insert("kvs", entry); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions agent/consul/state/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (s *Snapshot) Sessions() (memdb.ResultIterator, error) {
// Session is used when restoring from a snapshot. For general inserts, use
// SessionCreate.
func (s *Restore) Session(sess *structs.Session) error {
if err := insertSessionTxn(s.tx, sess, sess.ModifyIndex, true); err != nil {
if err := insertSessionTxn(s.tx, sess, sess.ModifyIndex, true, true); err != nil {
return fmt.Errorf("failed inserting session: %s", err)
}

Expand Down Expand Up @@ -213,7 +213,7 @@ func sessionCreateTxn(tx *txn, idx uint64, sess *structs.Session) error {
}

// Insert the session
if err := insertSessionTxn(tx, sess, idx, false); err != nil {
if err := insertSessionTxn(tx, sess, idx, false, false); err != nil {
return fmt.Errorf("failed inserting session: %s", err)
}

Expand Down
2 changes: 1 addition & 1 deletion agent/consul/state/session_oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func sessionDeleteWithSession(tx WriteTxn, session *structs.Session, idx uint64)
return nil
}

func insertSessionTxn(tx *txn, session *structs.Session, idx uint64, updateMax bool) error {
func insertSessionTxn(tx *txn, session *structs.Session, idx uint64, updateMax bool, _ bool) error {
if err := tx.Insert("sessions", session); err != nil {
return err
}
Expand Down

0 comments on commit 71da020

Please sign in to comment.