Browse Source

Merge pull request #8272 from gyuho/health

/health reports unhealthy when alarm is raised
Gyu-Ho Lee 8 years ago
parent
commit
608df0fc90
3 changed files with 13 additions and 0 deletions
  1. 5 0
      e2e/ctl_v3_alarm_test.go
  2. 4 0
      etcdserver/api/etcdhttp/base.go
  3. 4 0
      etcdserver/server.go

+ 5 - 0
e2e/ctl_v3_alarm_test.go

@@ -52,6 +52,11 @@ func alarmTest(cx ctlCtx) {
 		cx.t.Fatal(err)
 	}
 
+	// '/health' handler should return 'false'
+	if err := cURLGet(cx.epc, cURLReq{endpoint: "/health", expected: `{"health": "false"}`}); err != nil {
+		cx.t.Fatalf("failed get with curl (%v)", err)
+	}
+
 	// check that Put is rejected when alarm is on
 	if err := ctlV3Put(cx, "3rd_test", smallbuf, ""); err != nil {
 		if !strings.Contains(err.Error(), "etcdserver: mvcc: database space exceeded") {

+ 4 - 0
etcdserver/api/etcdhttp/base.go

@@ -67,6 +67,10 @@ func healthHandler(server *etcdserver.EtcdServer) http.HandlerFunc {
 			http.Error(w, `{"health": "false"}`, http.StatusServiceUnavailable)
 			return
 		}
+		if len(server.Alarms()) > 0 {
+			w.Write([]byte(`{"health": "false"}`))
+			return
+		}
 		ctx, cancel := context.WithTimeout(context.Background(), time.Second)
 		defer cancel()
 		if _, err := server.Do(ctx, etcdserverpb.Request{Method: "QGET"}); err != nil {

+ 4 - 0
etcdserver/server.go

@@ -1667,3 +1667,7 @@ func (s *EtcdServer) goAttach(f func()) {
 		f()
 	}()
 }
+
+func (s *EtcdServer) Alarms() []*pb.AlarmMember {
+	return s.alarmStore.Get(pb.AlarmType_NONE)
+}