member.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package etcdserver
  2. import (
  3. "crypto/sha1"
  4. "encoding/binary"
  5. "fmt"
  6. "log"
  7. "path"
  8. "strconv"
  9. "time"
  10. "github.com/coreos/etcd/pkg/types"
  11. )
  12. const machineKVPrefix = "/_etcd/machines/"
  13. // RaftAttributes represents the raft related attributes of an etcd member.
  14. type RaftAttributes struct {
  15. // TODO(philips): ensure these are URLs
  16. PeerURLs []string
  17. }
  18. // Attributes represents all the non-raft related attributes of an etcd member.
  19. type Attributes struct {
  20. Name string
  21. ClientURLs []string
  22. }
  23. type Member struct {
  24. ID uint64
  25. RaftAttributes
  26. Attributes
  27. }
  28. // newMember creates a Member without an ID and generates one based on the
  29. // name, peer URLs. This is used for bootstrapping.
  30. func newMember(name string, peerURLs types.URLs, now *time.Time) *Member {
  31. m := &Member{
  32. RaftAttributes: RaftAttributes{PeerURLs: peerURLs.StringSlice()},
  33. Attributes: Attributes{Name: name},
  34. }
  35. b := []byte(m.Name)
  36. for _, p := range m.PeerURLs {
  37. b = append(b, []byte(p)...)
  38. }
  39. if now != nil {
  40. b = append(b, []byte(fmt.Sprintf("%d", now.Unix()))...)
  41. }
  42. hash := sha1.Sum(b)
  43. m.ID = binary.BigEndian.Uint64(hash[:8])
  44. return m
  45. }
  46. func (m Member) storeKey() string {
  47. return path.Join(machineKVPrefix, strconv.FormatUint(m.ID, 16))
  48. }
  49. func parseMemberID(key string) uint64 {
  50. id, err := strconv.ParseUint(path.Base(key), 16, 64)
  51. if err != nil {
  52. log.Panicf("unexpected parse member id error: %v", err)
  53. }
  54. return id
  55. }