From e2463569e7756e4db4fb2c047d39048d336d5f30 Mon Sep 17 00:00:00 2001 From: Anthony Romano Date: Tue, 27 Dec 2016 16:34:11 -0800 Subject: [PATCH] v2http: submit QGET in health endpoint if no progress Removing the periodic SYNC calls broke the health endpoint since the raft index stops updating. Instead, don't bother monitoring the raft index; issue a QGET directly to get a consensus response. Fixes #6985 --- etcdserver/api/v2http/client.go | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/etcdserver/api/v2http/client.go b/etcdserver/api/v2http/client.go index 39631dc5c..038f5417e 100644 --- a/etcdserver/api/v2http/client.go +++ b/etcdserver/api/v2http/client.go @@ -346,32 +346,23 @@ func serveVars(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "\n}\n") } -// TODO: change etcdserver to raft interface when we have it. -// add test for healthHandler when we have the interface ready. func healthHandler(server *etcdserver.EtcdServer) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if !allowMethod(w, r.Method, "GET") { return } - if uint64(server.Leader()) == raft.None { http.Error(w, `{"health": "false"}`, http.StatusServiceUnavailable) return } - - // wait for raft's progress - index := server.Index() - for i := 0; i < 3; i++ { - time.Sleep(250 * time.Millisecond) - if server.Index() > index { - w.WriteHeader(http.StatusOK) - w.Write([]byte(`{"health": "true"}`)) - return - } + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + if _, err := server.Do(ctx, etcdserverpb.Request{Method: "QGET"}); err != nil { + http.Error(w, `{"health": "false"}`, http.StatusServiceUnavailable) + return } - - http.Error(w, `{"health": "false"}`, http.StatusServiceUnavailable) - return + w.WriteHeader(http.StatusOK) + w.Write([]byte(`{"health": "true"}`)) } }