Skip to content

Commit

Permalink
Add protocol for /info/shards endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
aalda committed Apr 9, 2019
1 parent 8dee396 commit da04138
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 25 deletions.
23 changes: 19 additions & 4 deletions api/apihttp/apihttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package apihttp

import (
"encoding/json"
"fmt"
"net/http"
"time"

Expand Down Expand Up @@ -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
Expand Down
30 changes: 9 additions & 21 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
}
Expand Down
File renamed without changes.
36 changes: 36 additions & 0 deletions protocol/info.go
Original file line number Diff line number Diff line change
@@ -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"`
}

0 comments on commit da04138

Please sign in to comment.