Browse Source

consul/state: basic health check retrieval works

pull/1291/head
Ryan Uber 9 years ago committed by James Phillips
parent
commit
77f14af977
  1. 14
      consul/state/schema.go
  2. 25
      consul/state/state_store.go
  3. 12
      consul/state/state_store_test.go

14
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,
},
},
},

25
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
}

12
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])
}
}

Loading…
Cancel
Save