diff --git a/consul/state/state_store.go b/consul/state/state_store.go index 44c1f8472..636b9a21d 100644 --- a/consul/state/state_store.go +++ b/consul/state/state_store.go @@ -40,6 +40,26 @@ func NewStateStore(logOutput io.Writer) (*StateStore, error) { return s, nil } +// maxIndex is a helper used to retrieve the highest known index +// amongst a set of tables in the db. +func (s *StateStore) maxIndex(tables ...string) uint64 { + tx := s.db.Txn(false) + defer tx.Abort() + + var lindex uint64 + for _, table := range tables { + ti, err := tx.First("index", "id", table) + if err != nil { + panic(fmt.Sprintf("unknown index: %s", table)) + } + idx := ti.(*IndexEntry).Value + if idx > lindex { + lindex = idx + } + } + return lindex +} + // EnsureNode is used to upsert node registration or modification. func (s *StateStore) EnsureNode(idx uint64, node *structs.Node) error { tx := s.db.Txn(true) diff --git a/consul/state/state_store_test.go b/consul/state/state_store_test.go index 678f2dc34..db46d42e1 100644 --- a/consul/state/state_store_test.go +++ b/consul/state/state_store_test.go @@ -143,10 +143,13 @@ func TestStateStore_DeleteNode(t *testing.T) { t.Fatalf("err: %s", err) } - // The node is now gone + // The node is now gone and the index was updated if n, err := s.GetNode("node1"); err != nil || n != nil { t.Fatalf("bad: %#v (err: %#v)", node, err) } + if idx := s.maxIndex("nodes"); idx != 2 { + t.Fatalf("bad index: %d", idx) + } } func TestStateStore_EnsureService_NodeServices(t *testing.T) { @@ -262,9 +265,12 @@ func TestStateStore_DeleteNodeService(t *testing.T) { t.Fatalf("err: %s", err) } - // The service doesn't exist. + // The service doesn't exist and the index was updated ns, err = s.NodeServices("node1") if err != nil || ns == nil || len(ns.Services) != 0 { t.Fatalf("bad: %#v (err: %#v)", ns, err) } + if idx := s.maxIndex("services"); idx != 3 { + t.Fatalf("bad index: %d", idx) + } }