From 77f14af9777f2fff117657b32d384bf66e929d5d Mon Sep 17 00:00:00 2001 From: Ryan Uber Date: Mon, 24 Aug 2015 21:23:24 -0700 Subject: [PATCH] consul/state: basic health check retrieval works --- consul/state/schema.go | 14 +++----------- consul/state/state_store.go | 25 ++++++++++++++++++++++++- consul/state/state_store_test.go | 12 ++++++++++++ 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/consul/state/schema.go b/consul/state/schema.go index 1f84f76e92..006853a7f2 100644 --- a/consul/state/schema.go +++ b/consul/state/schema.go @@ -172,17 +172,9 @@ func checksTableSchema() *memdb.TableSchema { Name: "node", AllowMissing: true, Unique: false, - Indexer: &memdb.CompoundIndex{ - Indexes: []memdb.Indexer{ - &memdb.StringFieldIndex{ - Field: "Node", - Lowercase: true, - }, - &memdb.StringFieldIndex{ - Field: "ServiceID", - Lowercase: true, - }, - }, + Indexer: &memdb.StringFieldIndex{ + Field: "Node", + Lowercase: true, }, }, }, diff --git a/consul/state/state_store.go b/consul/state/state_store.go index 085a1c8f0e..8a52a87352 100644 --- a/consul/state/state_store.go +++ b/consul/state/state_store.go @@ -384,7 +384,7 @@ func (s *StateStore) ensureCheckTxn(idx uint64, hc *structs.HealthCheck, tx *mem // TODO: invalidate sessions if status == critical // Persist the check registration in the db - if err := tx.Insert("services", hc); err != nil { + if err := tx.Insert("checks", hc); err != nil { return fmt.Errorf("failed inserting service: %s", err) } if err := tx.Insert("index", &IndexEntry{"checks", idx}); err != nil { @@ -395,3 +395,26 @@ func (s *StateStore) ensureCheckTxn(idx uint64, hc *structs.HealthCheck, tx *mem return nil } + +// NodeChecks is used to retrieve checks associated with the +// given node from the state store. +func (s *StateStore) NodeChecks(nodeID string) (structs.HealthChecks, error) { + tx := s.db.Txn(false) + defer tx.Abort() + return s.parseChecks(tx.Get("checks", "node", nodeID)) +} + +// parseChecks is a helper function used to deduplicate some +// repetitive code for returning health checks. +func (s *StateStore) parseChecks(iter memdb.ResultIterator, err error) (structs.HealthChecks, error) { + if err != nil { + return nil, fmt.Errorf("failed health check lookup: %s", err) + } + + // Gather the health checks and return them properly type casted + var results structs.HealthChecks + for hc := iter.Next(); hc != nil; hc = iter.Next() { + results = append(results, hc.(*structs.HealthCheck)) + } + return results, nil +} diff --git a/consul/state/state_store_test.go b/consul/state/state_store_test.go index 9137bd6ce8..facaeae40c 100644 --- a/consul/state/state_store_test.go +++ b/consul/state/state_store_test.go @@ -313,4 +313,16 @@ func TestStateStore_EnsureCheck(t *testing.T) { if err := s.EnsureCheck(3, check); err != nil { t.Fatalf("err: %s", err) } + + // Retrieve the check and make sure it matches + checks, err := s.NodeChecks("node1") + if err != nil { + t.Fatalf("err: %s", err) + } + if len(checks) != 1 { + t.Fatalf("bad number of checks: %d", len(checks)) + } + if !reflect.DeepEqual(checks[0], check) { + t.Fatalf("bad: %#v", checks[0]) + } }