Skip to content

Commit

Permalink
Change /info endpoint to /info/shards
Browse files Browse the repository at this point in the history
Improve /info/shards endpoint info
  • Loading branch information
Jose Luis Lucas authored and iknite committed Feb 21, 2019
1 parent 77ac9a0 commit a56e376
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
14 changes: 11 additions & 3 deletions api/apihttp/apihttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -363,16 +363,24 @@ 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")
w.WriteHeader(http.StatusMethodNotAllowed)
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)
Expand Down
20 changes: 11 additions & 9 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 6 additions & 0 deletions raftwal/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit a56e376

Please sign in to comment.