etcd/discovery/discovery.go

322 lines
8.5 KiB
Go
Raw Normal View History

/*
Copyright 2014 CoreOS, Inc.
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.
*/
2014-09-21 22:26:59 +04:00
package discovery
import (
"errors"
"fmt"
"log"
2014-10-04 16:20:45 +04:00
"net/http"
"net/url"
2014-09-21 22:26:59 +04:00
"path"
"sort"
"strconv"
"strings"
2014-10-04 16:20:45 +04:00
"time"
2014-09-21 22:26:59 +04:00
2014-10-16 23:06:47 +04:00
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/jonboulle/clockwork"
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
2014-10-17 07:23:07 +04:00
"github.com/coreos/etcd/client"
"github.com/coreos/etcd/pkg/types"
2014-09-21 22:26:59 +04:00
)
var (
ErrInvalidURL = errors.New("discovery: invalid URL")
ErrBadSizeKey = errors.New("discovery: size key is bad")
ErrSizeNotFound = errors.New("discovery: size key not found")
ErrTokenNotFound = errors.New("discovery: token not found")
ErrDuplicateID = errors.New("discovery: found duplicate id")
ErrFullCluster = errors.New("discovery: cluster is full")
ErrTooManyRetries = errors.New("discovery: too many retries")
2014-10-08 05:20:45 +04:00
)
2014-10-08 05:20:45 +04:00
const (
// Number of retries discovery will attempt before giving up and erroring out.
nRetries = uint(3)
2014-09-21 22:26:59 +04:00
)
// JoinCluster will connect to the discovery service at the given url, and
// register the server represented by the given id and config to the cluster
func JoinCluster(durl, dproxyurl string, id types.ID, config string) (string, error) {
d, err := newDiscovery(durl, dproxyurl, id)
if err != nil {
return "", err
}
return d.joinCluster(config)
}
// GetCluster will connect to the discovery service at the given url and
// retrieve a string describing the cluster
func GetCluster(durl, dproxyurl string) (string, error) {
d, err := newDiscovery(durl, dproxyurl, 0)
if err != nil {
return "", err
}
return d.getCluster()
2014-10-04 16:20:45 +04:00
}
2014-09-21 22:26:59 +04:00
type discovery struct {
cluster string
id types.ID
c client.KeysAPI
retries uint
url *url.URL
2014-10-09 03:41:59 +04:00
2014-10-15 22:53:05 +04:00
clock clockwork.Clock
2014-09-21 22:26:59 +04:00
}
// newProxyFunc builds a proxy function from the given string, which should
// represent a URL that can be used as a proxy. It performs basic
// sanitization of the URL and returns any error encountered.
func newProxyFunc(proxy string) (func(*http.Request) (*url.URL, error), error) {
if proxy == "" {
return nil, nil
}
// Do a small amount of URL sanitization to help the user
// Derived from net/http.ProxyFromEnvironment
proxyURL, err := url.Parse(proxy)
if err != nil || !strings.HasPrefix(proxyURL.Scheme, "http") {
// proxy was bogus. Try prepending "http://" to it and
// see if that parses correctly. If not, we ignore the
// error and complain about the original one
var err2 error
proxyURL, err2 = url.Parse("http://" + proxy)
if err2 == nil {
err = nil
}
}
if err != nil {
return nil, fmt.Errorf("invalid proxy address %q: %v", proxy, err)
}
log.Printf("discovery: using proxy %q", proxyURL.String())
return http.ProxyURL(proxyURL), nil
}
func newDiscovery(durl, dproxyurl string, id types.ID) (*discovery, error) {
2014-10-04 16:20:45 +04:00
u, err := url.Parse(durl)
if err != nil {
return nil, err
}
token := u.Path
u.Path = ""
pf, err := newProxyFunc(dproxyurl)
if err != nil {
return nil, err
}
c, err := client.NewHTTPClient(&http.Transport{Proxy: pf}, []string{u.String()})
2014-10-04 16:20:45 +04:00
if err != nil {
return nil, err
}
dc := client.NewDiscoveryKeysAPI(c)
2014-10-04 16:20:45 +04:00
return &discovery{
2014-10-15 22:53:05 +04:00
cluster: token,
c: dc,
id: id,
2014-10-15 22:53:05 +04:00
url: u,
clock: clockwork.NewRealClock(),
2014-10-04 16:20:45 +04:00
}, nil
}
func (d *discovery) joinCluster(config string) (string, error) {
// fast path: if the cluster is full, return the error
// do not need to register to the cluster in this case.
2014-09-22 09:34:39 +04:00
if _, _, err := d.checkCluster(); err != nil {
2014-10-04 16:20:45 +04:00
return "", err
2014-09-22 09:34:39 +04:00
}
if err := d.createSelf(config); err != nil {
2014-10-08 05:20:45 +04:00
// Fails, even on a timeout, if createSelf times out.
// TODO(barakmich): Retrying the same node might want to succeed here
// (ie, createSelf should be idempotent for discovery).
2014-10-04 16:20:45 +04:00
return "", err
2014-09-21 22:26:59 +04:00
}
nodes, size, err := d.checkCluster()
if err != nil {
2014-10-04 16:20:45 +04:00
return "", err
2014-09-21 22:26:59 +04:00
}
all, err := d.waitNodes(nodes, size)
if err != nil {
2014-10-04 16:20:45 +04:00
return "", err
2014-09-21 22:26:59 +04:00
}
2014-10-04 16:20:45 +04:00
return nodesToCluster(all), nil
2014-09-21 22:26:59 +04:00
}
func (d *discovery) getCluster() (string, error) {
nodes, size, err := d.checkCluster()
2014-11-05 00:09:24 +03:00
if err != nil {
if err == ErrFullCluster {
return nodesToCluster(nodes), nil
}
return "", err
}
all, err := d.waitNodes(nodes, size)
2014-11-05 00:09:24 +03:00
if err != nil {
return "", err
}
return nodesToCluster(all), nil
}
func (d *discovery) createSelf(contents string) error {
ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
resp, err := d.c.Create(ctx, d.selfKey(), contents, -1)
cancel()
2014-09-21 22:26:59 +04:00
if err != nil {
return err
}
// ensure self appears on the server we connected to
2014-09-22 22:13:49 +04:00
w := d.c.Watch(d.selfKey(), resp.Node.CreatedIndex)
_, err = w.Next(context.Background())
2014-09-23 02:38:19 +04:00
return err
2014-09-21 22:26:59 +04:00
}
func (d *discovery) checkCluster() (client.Nodes, int, error) {
2014-09-23 01:25:35 +04:00
configKey := path.Join("/", d.cluster, "_config")
ctx, cancel := context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
// find cluster size
resp, err := d.c.Get(ctx, path.Join(configKey, "size"))
cancel()
2014-09-21 22:26:59 +04:00
if err != nil {
if err == client.ErrKeyNoExist {
return nil, 0, ErrSizeNotFound
}
if err == client.ErrTimeout {
return d.checkClusterRetry()
}
2014-09-21 22:26:59 +04:00
return nil, 0, err
}
size, err := strconv.Atoi(resp.Node.Value)
if err != nil {
return nil, 0, ErrBadSizeKey
2014-09-21 22:26:59 +04:00
}
ctx, cancel = context.WithTimeout(context.Background(), client.DefaultRequestTimeout)
resp, err = d.c.Get(ctx, d.cluster)
cancel()
2014-09-21 22:26:59 +04:00
if err != nil {
if err == client.ErrTimeout {
return d.checkClusterRetry()
}
return nil, 0, err
}
nodes := make(client.Nodes, 0)
// append non-config keys to nodes
for _, n := range resp.Node.Nodes {
if !(path.Base(n.Key) == path.Base(configKey)) {
nodes = append(nodes, n)
}
2014-09-21 22:26:59 +04:00
}
2014-09-23 02:38:19 +04:00
snodes := sortableNodes{nodes}
sort.Sort(snodes)
2014-09-21 22:26:59 +04:00
// find self position
for i := range nodes {
if path.Base(nodes[i].Key) == path.Base(d.selfKey()) {
2014-09-21 22:26:59 +04:00
break
}
if i >= size-1 {
2014-11-05 00:09:24 +03:00
return nodes[:size], size, ErrFullCluster
2014-09-21 22:26:59 +04:00
}
}
return nodes, size, nil
}
func (d *discovery) logAndBackoffForRetry(step string) {
d.retries++
2014-10-15 22:53:05 +04:00
retryTime := time.Second * (0x1 << d.retries)
log.Println("discovery: during", step, "connection to", d.url, "timed out, retrying in", retryTime)
2014-10-15 22:53:05 +04:00
d.clock.Sleep(retryTime)
}
func (d *discovery) checkClusterRetry() (client.Nodes, int, error) {
if d.retries < nRetries {
d.logAndBackoffForRetry("cluster status check")
return d.checkCluster()
}
return nil, 0, ErrTooManyRetries
}
2014-10-08 05:20:45 +04:00
func (d *discovery) waitNodesRetry() (client.Nodes, error) {
if d.retries < nRetries {
d.logAndBackoffForRetry("waiting for other nodes")
nodes, n, err := d.checkCluster()
if err != nil {
return nil, err
}
return d.waitNodes(nodes, n)
}
return nil, ErrTooManyRetries
}
2014-09-21 22:26:59 +04:00
func (d *discovery) waitNodes(nodes client.Nodes, size int) (client.Nodes, error) {
if len(nodes) > size {
nodes = nodes[:size]
}
2014-10-04 16:20:45 +04:00
w := d.c.RecursiveWatch(d.cluster, nodes[len(nodes)-1].ModifiedIndex+1)
2014-09-21 22:26:59 +04:00
all := make(client.Nodes, len(nodes))
copy(all, nodes)
for _, n := range all {
if path.Base(n.Key) == path.Base(d.selfKey()) {
log.Printf("discovery: found self %s in the cluster", path.Base(d.selfKey()))
} else {
log.Printf("discovery: found peer %s in the cluster", path.Base(n.Key))
}
}
2014-09-21 22:26:59 +04:00
// wait for others
for len(all) < size {
log.Printf("discovery: found %d peer(s), waiting for %d more", len(all), size-len(all))
resp, err := w.Next(context.Background())
2014-09-21 22:26:59 +04:00
if err != nil {
2014-10-08 05:20:45 +04:00
if err == client.ErrTimeout {
return d.waitNodesRetry()
}
2014-09-21 22:26:59 +04:00
return nil, err
}
log.Printf("discovery: found peer %s in the cluster", path.Base(resp.Node.Key))
2014-09-21 22:26:59 +04:00
all = append(all, resp.Node)
}
log.Printf("discovery: found %d needed peer(s)", len(all))
2014-09-21 22:26:59 +04:00
return all, nil
}
2014-09-22 22:13:49 +04:00
func (d *discovery) selfKey() string {
return path.Join("/", d.cluster, d.id.String())
2014-09-22 22:13:49 +04:00
}
2014-10-04 16:20:45 +04:00
func nodesToCluster(ns client.Nodes) string {
2014-09-21 22:26:59 +04:00
s := make([]string, len(ns))
for i, n := range ns {
s[i] = n.Value
}
2014-10-04 16:20:45 +04:00
return strings.Join(s, ",")
2014-09-21 22:26:59 +04:00
}
2014-09-23 02:38:19 +04:00
type sortableNodes struct{ client.Nodes }
2014-09-21 22:26:59 +04:00
2014-09-23 02:38:19 +04:00
func (ns sortableNodes) Len() int { return len(ns.Nodes) }
func (ns sortableNodes) Less(i, j int) bool {
2014-09-21 22:26:59 +04:00
return ns.Nodes[i].CreatedIndex < ns.Nodes[j].CreatedIndex
}
2014-09-23 02:38:19 +04:00
func (ns sortableNodes) Swap(i, j int) { ns.Nodes[i], ns.Nodes[j] = ns.Nodes[j], ns.Nodes[i] }