consul/state: basic health check retrieval works

This commit is contained in:
Ryan Uber 2015-08-24 21:23:24 -07:00 committed by James Phillips
parent cf569f4848
commit 77f14af977
3 changed files with 39 additions and 12 deletions

View File

@ -172,17 +172,9 @@ func checksTableSchema() *memdb.TableSchema {
Name: "node", Name: "node",
AllowMissing: true, AllowMissing: true,
Unique: false, Unique: false,
Indexer: &memdb.CompoundIndex{ Indexer: &memdb.StringFieldIndex{
Indexes: []memdb.Indexer{ Field: "Node",
&memdb.StringFieldIndex{ Lowercase: true,
Field: "Node",
Lowercase: true,
},
&memdb.StringFieldIndex{
Field: "ServiceID",
Lowercase: true,
},
},
}, },
}, },
}, },

View File

@ -384,7 +384,7 @@ func (s *StateStore) ensureCheckTxn(idx uint64, hc *structs.HealthCheck, tx *mem
// TODO: invalidate sessions if status == critical // TODO: invalidate sessions if status == critical
// Persist the check registration in the db // 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) return fmt.Errorf("failed inserting service: %s", err)
} }
if err := tx.Insert("index", &IndexEntry{"checks", idx}); err != nil { 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 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
}

View File

@ -313,4 +313,16 @@ func TestStateStore_EnsureCheck(t *testing.T) {
if err := s.EnsureCheck(3, check); err != nil { if err := s.EnsureCheck(3, check); err != nil {
t.Fatalf("err: %s", err) 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])
}
} }