From f49b2f9b99df6cc8ad7cbbe87d98e89fbab744c3 Mon Sep 17 00:00:00 2001 From: Travis Turner Date: Mon, 30 Oct 2017 09:02:46 -0500 Subject: [PATCH] Adjust /status and /schema endpoints. Tried/failed to deprecate /slices/max (client is using it for backups). --- client.go | 10 ++++----- handler.go | 46 +++++++++++++++++------------------------- handler_test.go | 6 +++--- internal/private.proto | 4 ---- 4 files changed, 26 insertions(+), 40 deletions(-) diff --git a/client.go b/client.go index a579e527f..3b63895ec 100644 --- a/client.go +++ b/client.go @@ -100,9 +100,6 @@ func (c *Client) MaxInverseSliceByIndex(ctx context.Context) (map[string]uint64, func (c *Client) maxSliceByIndex(ctx context.Context, inverse bool) (map[string]uint64, error) { // Execute request against the host. u := uriPathToURL(c.host, "/slices/max") - u.RawQuery = (&url.Values{ - "inverse": {strconv.FormatBool(inverse)}, - }).Encode() // Build request. req, err := http.NewRequest("GET", u.String(), nil) @@ -119,14 +116,17 @@ func (c *Client) maxSliceByIndex(ctx context.Context, inverse bool) (map[string] } defer resp.Body.Close() - var rsp sliceMaxResponse + var rsp getSlicesMaxResponse if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("http: status=%d", resp.StatusCode) } else if err := json.NewDecoder(resp.Body).Decode(&rsp); err != nil { return nil, fmt.Errorf("json decode: %s", err) } - return rsp.MaxSlices, nil + if inverse { + return rsp.Inverse, nil + } + return rsp.Standard, nil } // Schema returns all index and frame schema information. diff --git a/handler.go b/handler.go index ffe189e35..5c7aee509 100644 --- a/handler.go +++ b/handler.go @@ -132,7 +132,7 @@ func NewRouter(handler *Handler) *mux.Router { router.HandleFunc("/index/{index}/time-quantum", handler.handlePatchIndexTimeQuantum).Methods("PATCH") router.HandleFunc("/hosts", handler.handleGetHosts).Methods("GET") router.HandleFunc("/schema", handler.handleGetSchema).Methods("GET") - //router.HandleFunc("/slices/max", handler.handleGetSliceMax).Methods("GET") // TODO: this is being used by the client (for backups) + router.HandleFunc("/slices/max", handler.handleGetSlicesMax).Methods("GET") // TODO: deprecate, but it's being used by the client (for backups) router.HandleFunc("/status", handler.handleGetStatus).Methods("GET") router.HandleFunc("/version", handler.handleGetVersion).Methods("GET") router.HandleFunc("/recalculate-caches", handler.handleRecalculateCaches).Methods("POST") @@ -216,13 +216,16 @@ func (h *Handler) handleGetSchema(w http.ResponseWriter, r *http.Request) { // handleGetStatus handles GET /status requests. func (h *Handler) handleGetStatus(w http.ResponseWriter, r *http.Request) { - status, err := h.StatusHandler.ClusterStatus() + pb, err := h.StatusHandler.ClusterStatus() if err != nil { h.logger().Printf("cluster status error: %s", err) return } + + cs := pb.(*internal.ClusterStatus) if err := json.NewEncoder(w).Encode(getStatusResponse{ - Status: status, + State: cs.State, + URISet: decodeURIs(cs.URISet), }); err != nil { h.logger().Printf("write status response error: %s", err) } @@ -233,7 +236,8 @@ type getSchemaResponse struct { } type getStatusResponse struct { - Status proto.Message `json:"status"` + State string `json:"state"` + URISet []URI `json:"uri-set"` } // handlePostQuery handles /query requests. @@ -305,33 +309,19 @@ func (h *Handler) handlePostQuery(w http.ResponseWriter, r *http.Request) { } } -/* -func (h *Handler) handleGetSliceMax(w http.ResponseWriter, r *http.Request) { - var ms map[string]uint64 - if inverse, _ := strconv.ParseBool(r.URL.Query().Get("inverse")); inverse { - ms = h.Holder.MaxInverseSlices() - } else { - ms = h.Holder.MaxSlices() +// handleGetSlicesMax handles GET /schema requests. +func (h *Handler) handleGetSlicesMax(w http.ResponseWriter, r *http.Request) { + if err := json.NewEncoder(w).Encode(getSlicesMaxResponse{ + Standard: h.Holder.MaxSlices(), + Inverse: h.Holder.MaxInverseSlices(), + }); err != nil { + h.logger().Printf("write slices-max response error: %s", err) } - if strings.Contains(r.Header.Get("Accept"), "application/x-protobuf") { - pb := &internal.MaxSlicesResponse{ - MaxSlices: ms, - } - if buf, err := proto.Marshal(pb); err != nil { - h.logger().Printf("protobuf marshal error: %s", err) - } else if _, err := w.Write(buf); err != nil { - h.logger().Printf("stream write error: %s", err) - } - return - } - json.NewEncoder(w).Encode(sliceMaxResponse{ - MaxSlices: ms, - }) } -*/ -type sliceMaxResponse struct { - MaxSlices map[string]uint64 `json:"maxSlices"` +type getSlicesMaxResponse struct { + Standard map[string]uint64 `json:"standard"` + Inverse map[string]uint64 `json:"inverse"` } // handleGetIndexes handles GET /index request. diff --git a/handler_test.go b/handler_test.go index 0a540d059..05f93c6ab 100644 --- a/handler_test.go +++ b/handler_test.go @@ -147,7 +147,7 @@ func TestHandler_Status(t *testing.T) { h.ServeHTTP(w, test.MustNewHTTPRequest("GET", "/status", nil)) if w.Code != http.StatusOK { t.Fatalf("unexpected status code: %d", w.Code) - } else if body := w.Body.String(); body != `{"status":{"State":"NORMAL","URISet":[{"Scheme":"http","Host":"localhost","Port":10101}]}}`+"\n" { + } else if body := w.Body.String(); body != `{"state":"NORMAL","uri-set":[{"scheme":"http","host":"localhost","port":10101}]}`+"\n" { t.Fatalf("unexpected body: %s", body) } } @@ -172,7 +172,7 @@ func TestHandler_MaxSlices(t *testing.T) { h.ServeHTTP(w, test.MustNewHTTPRequest("GET", "/slices/max", nil)) if w.Code != http.StatusOK { t.Fatalf("unexpected status code: %d", w.Code) - } else if body := w.Body.String(); body != `{"maxSlices":{"i0":3,"i1":0}}`+"\n" { + } else if body := w.Body.String(); body != `{"standard":{"i0":3,"i1":0},"inverse":{"i0":0,"i1":0}}`+"\n" { t.Fatalf("unexpected body: %s", body) } } @@ -213,7 +213,7 @@ func TestHandler_MaxSlices_Inverse(t *testing.T) { h.ServeHTTP(w, test.MustNewHTTPRequest("GET", "/slices/max?inverse=true", nil)) if w.Code != http.StatusOK { t.Fatalf("unexpected status code: %d", w.Code) - } else if body := w.Body.String(); body != `{"maxSlices":{"i0":3,"i1":0}}`+"\n" { + } else if body := w.Body.String(); body != `{"standard":{"i0":0,"i1":0},"inverse":{"i0":3,"i1":0}}`+"\n" { t.Fatalf("unexpected body: %s", body) } } diff --git a/internal/private.proto b/internal/private.proto index 5101a6c35..4658f21be 100644 --- a/internal/private.proto +++ b/internal/private.proto @@ -38,10 +38,6 @@ message Cache { repeated uint64 IDs = 1; } -//message MaxSlicesResponse { -// map MaxSlices = 1; -//} - message MaxSlices { map Standard = 1; map Inverse = 2;