Initial support for getting the leader from Zookeeper.
Some tests included for making sure the JSON blob pulled from Zookeeper is correct. Updated client to be able to take Zookeeper json cluster config that is currently being used in aurora (clusters.json usually located at /etc/aurora/cluster.json). Changed error messages to no longer have a period at the end as that was throwing off printing of the error. Modified samuel's ZK library slightly to stop verbose logging using a NoOpLogger from stackoverflow.
This commit is contained in:
parent
c62e5ab750
commit
0e26c33129
35 changed files with 4437 additions and 51 deletions
88
zk.go
Normal file
88
zk.go
Normal file
|
@ -0,0 +1,88 @@
|
|||
/**
|
||||
* 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 realis
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/samuel/go-zookeeper/zk"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Endpoint struct {
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
}
|
||||
|
||||
type ServiceInstance struct {
|
||||
Service Endpoint `json:"serviceEndpoint"`
|
||||
AdditionalEndpoints map[string]Endpoint `json:"additionalEndpoints"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
// Loads leader from ZK endpoint.
|
||||
func LeaderFromZK(cluster Cluster) (string, error) {
|
||||
|
||||
endpoints := strings.Split(cluster.ZK, ",")
|
||||
//TODO (rdelvalle): When enabling debugging, change logger here
|
||||
c, _, err := zk.Connect(endpoints, time.Second*10, zk.WithoutLogger())
|
||||
defer c.Close()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "Failed to connect to Zookeeper at "+cluster.ZK)
|
||||
}
|
||||
|
||||
children, _, _, err := c.ChildrenW(cluster.SchedZKPath)
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(err, "Path %s doesn't exist on Zookeeper ", cluster.SchedZKPath)
|
||||
}
|
||||
|
||||
serviceInst := new(ServiceInstance)
|
||||
|
||||
for _, child := range children {
|
||||
|
||||
// Only the leader will start with member_
|
||||
if strings.HasPrefix(child, "member_") {
|
||||
|
||||
data, _, err := c.Get(cluster.SchedZKPath + "/" + child)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "Error fetching contents of leader")
|
||||
}
|
||||
|
||||
err = json.Unmarshal([]byte(data), serviceInst)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "Unable to unmarshall contents of leader")
|
||||
}
|
||||
|
||||
// Should only be one endpoint
|
||||
if len(serviceInst.AdditionalEndpoints) > 1 {
|
||||
fmt.Errorf("Ambiguous end points schemes")
|
||||
}
|
||||
|
||||
var scheme, host, port string
|
||||
for k, v := range serviceInst.AdditionalEndpoints {
|
||||
scheme = k
|
||||
host = v.Host
|
||||
port = strconv.Itoa(v.Port)
|
||||
}
|
||||
|
||||
return scheme + "://" + host + ":" + port, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", errors.New("No leader found")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue