consul/state: set index if we have an existing health check

This commit is contained in:
Ryan Uber 2015-08-24 22:32:18 -07:00 committed by James Phillips
parent d6380e31cc
commit ee8a1dc5d2
3 changed files with 39 additions and 1 deletions

View File

@ -352,6 +352,21 @@ func (s *StateStore) EnsureCheck(idx uint64, hc *structs.HealthCheck) error {
// a health check into the state store. It ensures safety against inserting
// checks with no matching node or service.
func (s *StateStore) ensureCheckTxn(idx uint64, hc *structs.HealthCheck, tx *memdb.Txn) error {
// Check if we have an existing health check
existing, err := tx.First("checks", "id", hc.Node, hc.CheckID)
if err != nil {
return fmt.Errorf("failed health check lookup: %s", err)
}
// Set the indexes
if existing != nil {
hc.CreateIndex = existing.(*structs.HealthCheck).CreateIndex
hc.ModifyIndex = idx
} else {
hc.CreateIndex = idx
hc.ModifyIndex = idx
}
// Use the default check status if none was provided
if hc.Status == "" {
hc.Status = structs.HealthCritical

View File

@ -332,9 +332,30 @@ func TestStateStore_EnsureCheck(t *testing.T) {
t.Fatalf("err: %s", err)
}
if len(checks) != 1 {
t.Fatalf("bad number of checks: %d", len(checks))
t.Fatalf("wrong number of checks: %d", len(checks))
}
if !reflect.DeepEqual(checks[0], check) {
t.Fatalf("bad: %#v", checks[0])
}
// Modify the health check
check.Output = "bbb"
if err := s.EnsureCheck(4, check); err != nil {
t.Fatalf("err: %s", err)
}
// Check that we successfully updated
checks, err = s.NodeChecks("node1")
if err != nil {
t.Fatalf("err: %s", err)
}
if len(checks) != 1 {
t.Fatalf("wrong number of checks: %d", len(checks))
}
if checks[0].Output != "bbb" {
t.Fatalf("wrong check output: %#v", checks[0])
}
if checks[0].CreateIndex != 3 || checks[0].ModifyIndex != 4 {
t.Fatalf("bad index: %#v", checks[0])
}
}

View File

@ -283,6 +283,8 @@ type HealthCheck struct {
Output string // Holds output of script runs
ServiceID string // optional associated service
ServiceName string // optional service name
RaftIndex
}
type HealthChecks []*HealthCheck