Only set precedence on write path

pull/4275/head
Paul Banks 2018-06-12 12:26:12 +01:00 committed by Jack Pearkes
parent 4a54f8f7e3
commit 382ce8f98a
2 changed files with 6 additions and 11 deletions

View File

@ -106,11 +106,6 @@ func (s *Snapshot) Intentions() (structs.Intentions, error) {
var ret structs.Intentions var ret structs.Intentions
for wrapped := ixns.Next(); wrapped != nil; wrapped = ixns.Next() { for wrapped := ixns.Next(); wrapped != nil; wrapped = ixns.Next() {
// Update precedence values for consistency (i.e. the object returned should
// be usable/comparable to the ones returned from Intentions() and Match()
// etc.)
ixn := wrapped.(*structs.Intention)
ixn.UpdatePrecedence()
ret = append(ret, wrapped.(*structs.Intention)) ret = append(ret, wrapped.(*structs.Intention))
} }
@ -147,9 +142,7 @@ func (s *Store) Intentions(ws memdb.WatchSet) (uint64, structs.Intentions, error
var results structs.Intentions var results structs.Intentions
for ixn := iter.Next(); ixn != nil; ixn = iter.Next() { for ixn := iter.Next(); ixn != nil; ixn = iter.Next() {
ixnReal := ixn.(*structs.Intention) results = append(results, ixn.(*structs.Intention))
ixnReal.UpdatePrecedence()
results = append(results, ixnReal)
} }
// Sort by precedence just because that's nicer and probably what most clients // Sort by precedence just because that's nicer and probably what most clients
@ -180,6 +173,9 @@ func (s *Store) intentionSetTxn(tx *memdb.Txn, idx uint64, ixn *structs.Intentio
return ErrMissingIntentionID return ErrMissingIntentionID
} }
// Ensure Precedence is populated correctly on "write"
ixn.UpdatePrecedence()
// Check for an existing intention // Check for an existing intention
existing, err := tx.First(intentionsTableName, "id", ixn.ID) existing, err := tx.First(intentionsTableName, "id", ixn.ID)
if err != nil { if err != nil {
@ -324,9 +320,7 @@ func (s *Store) IntentionMatch(ws memdb.WatchSet, args *structs.IntentionQueryMa
ws.Add(iter.WatchCh()) ws.Add(iter.WatchCh())
for ixn := iter.Next(); ixn != nil; ixn = iter.Next() { for ixn := iter.Next(); ixn != nil; ixn = iter.Next() {
ixnReal := ixn.(*structs.Intention) ixns = append(ixns, ixn.(*structs.Intention))
ixnReal.UpdatePrecedence()
ixns = append(ixns, ixnReal)
} }
} }

View File

@ -61,6 +61,7 @@ func TestStore_IntentionSetGet_basic(t *testing.T) {
ModifyIndex: 1, ModifyIndex: 1,
}, },
} }
expected.UpdatePrecedence()
ws = memdb.NewWatchSet() ws = memdb.NewWatchSet()
idx, actual, err := s.IntentionGet(ws, ixn.ID) idx, actual, err := s.IntentionGet(ws, ixn.ID)