2016-08-16 18:40:02 -07:00
|
|
|
/**
|
|
|
|
* 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"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2017-04-06 23:15:44 -07:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/samuel/go-zookeeper/zk"
|
2016-08-16 18:40:02 -07:00
|
|
|
)
|
|
|
|
|
2019-06-12 11:22:59 -07:00
|
|
|
type endpoint struct {
|
2016-08-16 18:40:02 -07:00
|
|
|
Host string `json:"host"`
|
|
|
|
Port int `json:"port"`
|
|
|
|
}
|
|
|
|
|
2019-06-12 11:22:59 -07:00
|
|
|
type serviceInstance struct {
|
|
|
|
Service endpoint `json:"serviceEndpoint"`
|
|
|
|
AdditionalEndpoints map[string]endpoint `json:"additionalEndpoints"`
|
2016-08-16 18:40:02 -07:00
|
|
|
Status string `json:"status"`
|
|
|
|
}
|
|
|
|
|
2018-03-03 13:58:36 -08:00
|
|
|
type zkConfig struct {
|
|
|
|
endpoints []string
|
|
|
|
path string
|
|
|
|
backoff Backoff
|
|
|
|
timeout time.Duration
|
2019-06-12 11:22:59 -07:00
|
|
|
logger logger
|
2018-03-03 13:58:36 -08:00
|
|
|
}
|
|
|
|
|
2019-06-12 11:22:59 -07:00
|
|
|
// ZKOpt - Configuration option for the Zookeeper client used.
|
2018-03-03 13:58:36 -08:00
|
|
|
type ZKOpt func(z *zkConfig)
|
|
|
|
|
2019-06-12 11:22:59 -07:00
|
|
|
// ZKEndpoints - Endpoints on which a Zookeeper instance is running to be used by the client.
|
2018-03-03 13:58:36 -08:00
|
|
|
func ZKEndpoints(endpoints ...string) ZKOpt {
|
|
|
|
return func(z *zkConfig) {
|
|
|
|
z.endpoints = endpoints
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 11:22:59 -07:00
|
|
|
// ZKPath - Path to look for information in when connected to Zookeeper.
|
2018-03-03 13:58:36 -08:00
|
|
|
func ZKPath(path string) ZKOpt {
|
|
|
|
return func(z *zkConfig) {
|
|
|
|
z.path = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 11:22:59 -07:00
|
|
|
// ZKBackoff - Configuration for Retry mechanism used when connecting to Zookeeper.
|
|
|
|
// TODO(rdelvalle): Determine if this is really necessary as the ZK library already has a retry built in.
|
2018-03-03 13:58:36 -08:00
|
|
|
func ZKBackoff(b Backoff) ZKOpt {
|
|
|
|
return func(z *zkConfig) {
|
|
|
|
z.backoff = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 11:22:59 -07:00
|
|
|
// ZKTimeout - How long to wait on a response from the Zookeeper instance before considering it dead.
|
2018-03-03 13:58:36 -08:00
|
|
|
func ZKTimeout(d time.Duration) ZKOpt {
|
|
|
|
return func(z *zkConfig) {
|
|
|
|
z.timeout = d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 11:22:59 -07:00
|
|
|
// ZKLogger - Attach a logger to the Zookeeper client in order to debug issues.
|
|
|
|
func ZKLogger(l logger) ZKOpt {
|
2018-03-03 13:58:36 -08:00
|
|
|
return func(z *zkConfig) {
|
|
|
|
z.logger = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 11:22:59 -07:00
|
|
|
// LeaderFromZK - Retrieves current Aurora leader from ZK.
|
2016-08-16 18:40:02 -07:00
|
|
|
func LeaderFromZK(cluster Cluster) (string, error) {
|
2018-03-03 13:58:36 -08:00
|
|
|
return LeaderFromZKOpts(ZKEndpoints(strings.Split(cluster.ZK, ",")...), ZKPath(cluster.SchedZKPath))
|
|
|
|
}
|
|
|
|
|
2019-06-12 11:22:59 -07:00
|
|
|
// LeaderFromZKOpts - Retrieves current Aurora leader from ZK with a custom configuration.
|
2018-03-03 13:58:36 -08:00
|
|
|
func LeaderFromZKOpts(options ...ZKOpt) (string, error) {
|
|
|
|
var leaderURL string
|
|
|
|
|
|
|
|
// Load the default configuration for Zookeeper followed by overriding values with those provided by the caller.
|
|
|
|
config := &zkConfig{backoff: defaultBackoff, timeout: time.Second * 10, logger: NoopLogger{}}
|
|
|
|
for _, opt := range options {
|
|
|
|
opt(config)
|
|
|
|
}
|
2016-08-16 18:40:02 -07:00
|
|
|
|
2018-03-03 13:58:36 -08:00
|
|
|
if len(config.endpoints) == 0 {
|
|
|
|
return "", errors.New("no Zookeeper endpoints supplied")
|
|
|
|
}
|
2017-04-06 23:15:44 -07:00
|
|
|
|
2018-03-03 13:58:36 -08:00
|
|
|
if config.path == "" {
|
|
|
|
return "", errors.New("no Zookeeper path supplied")
|
|
|
|
}
|
2017-08-02 16:22:46 -07:00
|
|
|
|
2018-03-03 13:58:36 -08:00
|
|
|
// Create a closure that allows us to use the ExponentialBackoff function.
|
|
|
|
retryErr := ExponentialBackoff(config.backoff, config.logger, func() (bool, error) {
|
2017-08-02 16:22:46 -07:00
|
|
|
|
2018-03-03 13:58:36 -08:00
|
|
|
c, _, err := zk.Connect(config.endpoints, config.timeout, func(c *zk.Conn) { c.SetLogger(config.logger) })
|
2017-11-30 12:02:50 -08:00
|
|
|
if err != nil {
|
2019-05-05 11:46:22 -07:00
|
|
|
return false, NewTemporaryError(errors.Wrap(err, "failed to connect to Zookeeper"))
|
2017-04-06 23:15:44 -07:00
|
|
|
}
|
|
|
|
|
2017-11-30 12:02:50 -08:00
|
|
|
defer c.Close()
|
2016-08-16 18:40:02 -07:00
|
|
|
|
2017-11-30 12:02:50 -08:00
|
|
|
// Open up descriptor for the ZK path given
|
2018-03-03 13:58:36 -08:00
|
|
|
children, _, _, err := c.ChildrenW(config.path)
|
2017-11-30 12:02:50 -08:00
|
|
|
if err != nil {
|
2018-03-03 13:58:36 -08:00
|
|
|
|
|
|
|
// Sentinel error check as there is no other way to check.
|
|
|
|
if err == zk.ErrInvalidPath {
|
|
|
|
return false, errors.Wrapf(err, "path %s is an invalid Zookeeper path", config.path)
|
|
|
|
}
|
|
|
|
|
2019-05-05 11:46:22 -07:00
|
|
|
return false, NewTemporaryError(errors.Wrapf(err, "path %s doesn't exist on Zookeeper ", config.path))
|
2017-11-30 12:02:50 -08:00
|
|
|
}
|
2016-08-16 18:40:02 -07:00
|
|
|
|
2017-11-30 12:02:50 -08:00
|
|
|
// Search for the leader through all the children in the given path
|
|
|
|
for _, child := range children {
|
|
|
|
|
|
|
|
// Only the leader will start with member_
|
|
|
|
if strings.HasPrefix(child, "member_") {
|
|
|
|
|
2018-03-03 13:58:36 -08:00
|
|
|
childPath := config.path + "/" + child
|
|
|
|
data, _, err := c.Get(childPath)
|
2017-11-30 12:02:50 -08:00
|
|
|
if err != nil {
|
2018-03-03 13:58:36 -08:00
|
|
|
if err == zk.ErrInvalidPath {
|
|
|
|
return false, errors.Wrapf(err, "path %s is an invalid Zookeeper path", childPath)
|
|
|
|
}
|
|
|
|
|
2019-05-05 11:46:22 -07:00
|
|
|
return false, NewTemporaryError(errors.Wrap(err, "unable to fetch contents of leader"))
|
2017-11-30 12:02:50 -08:00
|
|
|
}
|
|
|
|
|
2019-06-12 11:22:59 -07:00
|
|
|
var serviceInst serviceInstance
|
|
|
|
err = json.Unmarshal([]byte(data), &serviceInst)
|
2017-11-30 12:02:50 -08:00
|
|
|
if err != nil {
|
2019-05-05 11:46:22 -07:00
|
|
|
return false, NewTemporaryError(errors.Wrap(err, "unable to unmarshal contents of leader"))
|
2017-11-30 12:02:50 -08:00
|
|
|
}
|
|
|
|
|
2018-03-03 13:58:36 -08:00
|
|
|
// Should only be one endpoint.
|
|
|
|
// This should never be encountered as it would indicate Aurora
|
|
|
|
// writing bad info into Zookeeper but is kept here as a safety net.
|
2017-11-30 12:02:50 -08:00
|
|
|
if len(serviceInst.AdditionalEndpoints) > 1 {
|
2019-06-11 11:47:14 -07:00
|
|
|
return false,
|
|
|
|
NewTemporaryError(errors.New("ambiguous endpoints in json blob, Aurora wrote bad info to ZK"))
|
2017-11-30 12:02:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
var scheme, host, port string
|
|
|
|
for k, v := range serviceInst.AdditionalEndpoints {
|
|
|
|
scheme = k
|
|
|
|
host = v.Host
|
|
|
|
port = strconv.Itoa(v.Port)
|
|
|
|
}
|
|
|
|
|
2018-03-03 13:58:36 -08:00
|
|
|
leaderURL = scheme + "://" + host + ":" + port
|
2017-11-30 12:02:50 -08:00
|
|
|
return true, nil
|
2016-08-16 18:40:02 -07:00
|
|
|
}
|
2017-11-30 12:02:50 -08:00
|
|
|
}
|
2016-08-16 18:40:02 -07:00
|
|
|
|
2018-02-15 15:16:39 -08:00
|
|
|
// Leader data might not be available yet, try to fetch again.
|
2019-05-05 11:46:22 -07:00
|
|
|
return false, NewTemporaryError(errors.New("no leader found"))
|
2017-11-30 12:02:50 -08:00
|
|
|
})
|
2016-08-16 18:40:02 -07:00
|
|
|
|
2017-11-30 12:02:50 -08:00
|
|
|
if retryErr != nil {
|
2019-05-05 11:46:22 -07:00
|
|
|
config.logger.Printf("failed to determine leader after %v attempts", config.backoff.Steps)
|
2018-03-03 13:58:36 -08:00
|
|
|
return "", retryErr
|
2016-08-16 18:40:02 -07:00
|
|
|
}
|
|
|
|
|
2018-03-03 13:58:36 -08:00
|
|
|
return leaderURL, nil
|
2016-08-16 18:40:02 -07:00
|
|
|
}
|