From da0413818f40a886c976b9579e9c143585d6372f Mon Sep 17 00:00:00 2001 From: Alvaro Alda Date: Tue, 9 Apr 2019 22:23:33 +0200 Subject: [PATCH] Add protocol for /info/shards endpoint --- api/apihttp/apihttp.go | 23 ++++++++++++++---- client/client.go | 30 ++++++++---------------- protocol/{protocol.go => events.go} | 0 protocol/info.go | 36 +++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 25 deletions(-) rename protocol/{protocol.go => events.go} (100%) create mode 100644 protocol/info.go diff --git a/api/apihttp/apihttp.go b/api/apihttp/apihttp.go index 1947399bc..b76a01b79 100644 --- a/api/apihttp/apihttp.go +++ b/api/apihttp/apihttp.go @@ -19,6 +19,7 @@ package apihttp import ( "encoding/json" + "fmt" "net/http" "time" @@ -351,15 +352,29 @@ func InfoShardsHandler(balloon raftwal.RaftBalloonApi) http.HandlerFunc { var scheme string if r.TLS != nil { - scheme = "https://" + scheme = "https" } else { - scheme = "http://" + scheme = "http" } info := balloon.Info() - info["URIScheme"] = scheme + details := make(map[string]protocol.ShardDetail) + for k, v := range info["meta"].(map[string]map[string]string) { + fmt.Println(k, v) + details[k] = protocol.ShardDetail{ + NodeId: k, + HTTPAddr: v["HTTPAddr"], + } + } + + shards := &protocol.Shards{ + NodeId: info["nodeID"].(string), + LeaderId: info["leaderID"].(string), + URIScheme: protocol.Scheme(scheme), + Shards: details, + } - out, err := json.Marshal(info) + out, err := json.Marshal(shards) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/client/client.go b/client/client.go index acd934bfd..be95e1e15 100644 --- a/client/client.go +++ b/client/client.go @@ -374,10 +374,6 @@ func (c *HTTPClient) healthCheck(timeout time.Duration) { // by the preceding discovery process (if discovery is enabled). func (c *HTTPClient) discover() error { - if !c.discoveryEnabled { - return nil - } - for { e, err := c.topology.NextReadEndpoint(Any) @@ -387,30 +383,22 @@ func (c *HTTPClient) discover() error { body, err := c.doReq("GET", e, "/info/shards", nil) if err == nil { - info := make(map[string]interface{}) - err = json.Unmarshal(body, &info) + var shards protocol.Shards + err = json.Unmarshal(body, &shards) if err != nil { return err } - clusterMeta := info["meta"].(map[string]interface{}) - primaryID := info["leaderID"].(string) - scheme := info["URIScheme"].(string) - - var prim string + var primary string secondaries := make([]string, 0) - for id, nodeMeta := range clusterMeta { - for k, address := range nodeMeta.(map[string]interface{}) { - if k == "HTTPAddr" { - if id == primaryID { - prim = scheme + address.(string) - } else { - secondaries = append(secondaries, scheme+address.(string)) - } - } + for id, shard := range shards.Shards { + if id == shards.LeaderId { + primary = fmt.Sprintf("%s://%s", shards.URIScheme, shard.HTTPAddr) + } else { + secondaries = append(secondaries, fmt.Sprintf("%s://%s", shards.URIScheme, shard.HTTPAddr)) } } - c.topology.Update(prim, secondaries...) + c.topology.Update(primary, secondaries...) break } } diff --git a/protocol/protocol.go b/protocol/events.go similarity index 100% rename from protocol/protocol.go rename to protocol/events.go diff --git a/protocol/info.go b/protocol/info.go new file mode 100644 index 000000000..fa98f24b5 --- /dev/null +++ b/protocol/info.go @@ -0,0 +1,36 @@ +/* + Copyright 2018-2019 Banco Bilbao Vizcaya Argentaria, S.A. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package protocol + +type Scheme string + +const ( + Http Scheme = "http" + Https Scheme = "https" +) + +type ShardDetail struct { + NodeId string `json:"nodeId"` + HTTPAddr string `json:"httpAddr"` +} + +type Shards struct { + NodeId string `json:"nodeId"` + LeaderId string `json:"leaderId"` + URIScheme Scheme `json:"uriScheme"` + Shards map[string]ShardDetail `json:"shards"` +}