diff --git a/consul/state/state_store.go b/consul/state/state_store.go index 8a52a87352..fb894104ab 100644 --- a/consul/state/state_store.go +++ b/consul/state/state_store.go @@ -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 diff --git a/consul/state/state_store_test.go b/consul/state/state_store_test.go index 2545fe99c0..230aeb9367 100644 --- a/consul/state/state_store_test.go +++ b/consul/state/state_store_test.go @@ -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]) + } } diff --git a/consul/structs/structs.go b/consul/structs/structs.go index 9b7cab7452..12a82410bb 100644 --- a/consul/structs/structs.go +++ b/consul/structs/structs.go @@ -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