mirror of https://github.com/status-im/consul.git
consul/state: basic health check retrieval works
This commit is contained in:
parent
cf569f4848
commit
77f14af977
|
@ -172,18 +172,10 @@ func checksTableSchema() *memdb.TableSchema {
|
|||
Name: "node",
|
||||
AllowMissing: true,
|
||||
Unique: false,
|
||||
Indexer: &memdb.CompoundIndex{
|
||||
Indexes: []memdb.Indexer{
|
||||
&memdb.StringFieldIndex{
|
||||
Indexer: &memdb.StringFieldIndex{
|
||||
Field: "Node",
|
||||
Lowercase: true,
|
||||
},
|
||||
&memdb.StringFieldIndex{
|
||||
Field: "ServiceID",
|
||||
Lowercase: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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…
Reference in New Issue