Browse Source

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

pull/1291/head
Ryan Uber 9 years ago committed by James Phillips
parent
commit
6ebed234bb
  1. 15
      consul/state/state_store.go
  2. 23
      consul/state/state_store_test.go
  3. 2
      consul/structs/structs.go

15
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

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

2
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

Loading…
Cancel
Save