diff --git a/api/apihttp/apihttp.go b/api/apihttp/apihttp.go index 1bf6c5657..c72e0d8d1 100644 --- a/api/apihttp/apihttp.go +++ b/api/apihttp/apihttp.go @@ -323,7 +323,7 @@ func NewApiHttp(httpEndpoint string, balloon raftwal.RaftBalloonApi) *http.Serve api.HandleFunc("/proofs/membership", AuthHandlerMiddleware(Membership(balloon))) api.HandleFunc("/proofs/digest-membership", AuthHandlerMiddleware(DigestMembership(balloon))) api.HandleFunc("/proofs/incremental", AuthHandlerMiddleware(Incremental(balloon))) - api.HandleFunc("/info", AuthHandlerMiddleware(InfoHandle(httpEndpoint, balloon))) + api.HandleFunc("/info/shards", AuthHandlerMiddleware(InfoShardsHandler(httpEndpoint, balloon))) return api } @@ -363,7 +363,7 @@ func LogHandler(handle http.Handler) http.HandlerFunc { } } -func InfoHandle(httpEndpoint string, balloon raftwal.RaftBalloonApi) http.HandlerFunc { +func InfoShardsHandler(httpEndpoint string, balloon raftwal.RaftBalloonApi) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { w.Header().Set("Allow", "GET") @@ -371,8 +371,16 @@ func InfoHandle(httpEndpoint string, balloon raftwal.RaftBalloonApi) http.Handle return } + var scheme string + if r.TLS != nil { + scheme = "https://" + } else { + scheme = "http://" + } + info := balloon.Info() - info["httpEndpoint"] = "http://" + httpEndpoint + info["httpEndpoint"] = scheme + httpEndpoint + out, err := json.Marshal(info) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) diff --git a/client/client.go b/client/client.go index b0b3f3774..789845347 100644 --- a/client/client.go +++ b/client/client.go @@ -94,20 +94,22 @@ func (c *HTTPClient) exponentialBackoff(req *http.Request) (*http.Response, erro } func (c *HTTPClient) updateClusterLeader() { - info, _ := c.GetClusterInfo() - if info["isLeader"].(bool) { - c.conf.Cluster.Leader = info["httpEndpoint"].(string) - } else { - time.Sleep(100 * time.Millisecond) - c.conf.Cluster.Leader = c.conf.Cluster.Endpoints[rand.Int()%len(c.conf.Cluster.Endpoints)] - c.updateClusterLeader() + info, _ := c.getClusterInfo() + if isLeader, ok := info["isLeader"]; ok { + if isLeader.(bool) { + c.conf.Cluster.Leader = info["httpEndpoint"].(string) + } else { + time.Sleep(100 * time.Millisecond) + c.conf.Cluster.Leader = c.conf.Cluster.Endpoints[rand.Int()%len(c.conf.Cluster.Endpoints)] + c.updateClusterLeader() + } } } -func (c HTTPClient) GetClusterInfo() (map[string]interface{}, error) { +func (c HTTPClient) getClusterInfo() (map[string]interface{}, error) { info := make(map[string]interface{}) - req, err := http.NewRequest("GET", c.conf.Cluster.Leader+"/info", bytes.NewBuffer([]byte{})) + req, err := http.NewRequest("GET", c.conf.Cluster.Leader+"/info/shards", bytes.NewBuffer([]byte{})) if err != nil { return info, err } diff --git a/raftwal/raft.go b/raftwal/raft.go index 00608369c..22ca29157 100644 --- a/raftwal/raft.go +++ b/raftwal/raft.go @@ -422,5 +422,11 @@ func (b *RaftBalloon) Join(nodeID, addr string) error { func (b *RaftBalloon) Info() map[string]interface{} { m := make(map[string]interface{}) m["isLeader"] = b.IsLeader() + var nodes []string + raftNodes, _ := b.Nodes() + for _, node := range raftNodes { + nodes = append(nodes, string(node.Address)) + } + m["nodes"] = nodes return m }