2014-09-25 06:51:27 +04:00
|
|
|
package etcdserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/binary"
|
2014-10-01 01:57:04 +04:00
|
|
|
"fmt"
|
2014-10-03 02:48:58 +04:00
|
|
|
"log"
|
2014-09-25 06:51:27 +04:00
|
|
|
"path"
|
|
|
|
"strconv"
|
2014-10-01 01:57:04 +04:00
|
|
|
"time"
|
2014-10-02 00:12:24 +04:00
|
|
|
|
|
|
|
"github.com/coreos/etcd/pkg/types"
|
2014-09-25 06:51:27 +04:00
|
|
|
)
|
|
|
|
|
2014-10-12 05:33:52 +04:00
|
|
|
const membersKVPrefix = "/_etcd/members/"
|
2014-09-25 06:51:27 +04:00
|
|
|
|
2014-10-03 02:48:58 +04:00
|
|
|
// RaftAttributes represents the raft related attributes of an etcd member.
|
|
|
|
type RaftAttributes struct {
|
2014-09-25 06:51:27 +04:00
|
|
|
// TODO(philips): ensure these are URLs
|
2014-10-03 02:48:58 +04:00
|
|
|
PeerURLs []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attributes represents all the non-raft related attributes of an etcd member.
|
|
|
|
type Attributes struct {
|
|
|
|
Name string
|
2014-09-25 06:51:27 +04:00
|
|
|
ClientURLs []string
|
|
|
|
}
|
|
|
|
|
2014-10-03 02:48:58 +04:00
|
|
|
type Member struct {
|
|
|
|
ID uint64
|
|
|
|
RaftAttributes
|
|
|
|
Attributes
|
|
|
|
}
|
|
|
|
|
2014-09-25 06:51:27 +04:00
|
|
|
// newMember creates a Member without an ID and generates one based on the
|
|
|
|
// name, peer URLs. This is used for bootstrapping.
|
2014-10-02 00:12:24 +04:00
|
|
|
func newMember(name string, peerURLs types.URLs, now *time.Time) *Member {
|
2014-10-03 02:48:58 +04:00
|
|
|
m := &Member{
|
|
|
|
RaftAttributes: RaftAttributes{PeerURLs: peerURLs.StringSlice()},
|
|
|
|
Attributes: Attributes{Name: name},
|
|
|
|
}
|
2014-09-25 06:51:27 +04:00
|
|
|
|
|
|
|
b := []byte(m.Name)
|
|
|
|
for _, p := range m.PeerURLs {
|
|
|
|
b = append(b, []byte(p)...)
|
|
|
|
}
|
|
|
|
|
2014-10-01 01:57:04 +04:00
|
|
|
if now != nil {
|
|
|
|
b = append(b, []byte(fmt.Sprintf("%d", now.Unix()))...)
|
|
|
|
}
|
|
|
|
|
2014-09-25 06:51:27 +04:00
|
|
|
hash := sha1.Sum(b)
|
2014-10-08 15:58:53 +04:00
|
|
|
m.ID = binary.BigEndian.Uint64(hash[:8])
|
2014-09-25 06:51:27 +04:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Member) storeKey() string {
|
2014-10-17 05:39:55 +04:00
|
|
|
return path.Join(membersKVPrefix, idAsHex(m.ID))
|
2014-09-25 06:51:27 +04:00
|
|
|
}
|
2014-10-03 02:48:58 +04:00
|
|
|
|
|
|
|
func parseMemberID(key string) uint64 {
|
|
|
|
id, err := strconv.ParseUint(path.Base(key), 16, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("unexpected parse member id error: %v", err)
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|