member.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package membership
  15. import (
  16. "crypto/sha1"
  17. "encoding/binary"
  18. "fmt"
  19. "math/rand"
  20. "sort"
  21. "time"
  22. "github.com/coreos/etcd/pkg/types"
  23. "github.com/coreos/pkg/capnslog"
  24. )
  25. var (
  26. plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "etcdserver/membership")
  27. )
  28. // RaftAttributes represents the raft related attributes of an etcd member.
  29. type RaftAttributes struct {
  30. // PeerURLs is the list of peers in the raft cluster.
  31. // TODO(philips): ensure these are URLs
  32. PeerURLs []string `json:"peerURLs"`
  33. }
  34. // Attributes represents all the non-raft related attributes of an etcd member.
  35. type Attributes struct {
  36. Name string `json:"name,omitempty"`
  37. ClientURLs []string `json:"clientURLs,omitempty"`
  38. }
  39. type Member struct {
  40. ID types.ID `json:"id"`
  41. RaftAttributes
  42. Attributes
  43. }
  44. // NewMember creates a Member without an ID and generates one based on the
  45. // cluster name, peer URLs, and time. This is used for bootstrapping/adding new member.
  46. func NewMember(name string, peerURLs types.URLs, clusterName string, now *time.Time) *Member {
  47. m := &Member{
  48. RaftAttributes: RaftAttributes{PeerURLs: peerURLs.StringSlice()},
  49. Attributes: Attributes{Name: name},
  50. }
  51. var b []byte
  52. sort.Strings(m.PeerURLs)
  53. for _, p := range m.PeerURLs {
  54. b = append(b, []byte(p)...)
  55. }
  56. b = append(b, []byte(clusterName)...)
  57. if now != nil {
  58. b = append(b, []byte(fmt.Sprintf("%d", now.Unix()))...)
  59. }
  60. hash := sha1.Sum(b)
  61. m.ID = types.ID(binary.BigEndian.Uint64(hash[:8]))
  62. return m
  63. }
  64. // PickPeerURL chooses a random address from a given Member's PeerURLs.
  65. // It will panic if there is no PeerURLs available in Member.
  66. func (m *Member) PickPeerURL() string {
  67. if len(m.PeerURLs) == 0 {
  68. plog.Panicf("member should always have some peer url")
  69. }
  70. return m.PeerURLs[rand.Intn(len(m.PeerURLs))]
  71. }
  72. func (m *Member) Clone() *Member {
  73. if m == nil {
  74. return nil
  75. }
  76. mm := &Member{
  77. ID: m.ID,
  78. Attributes: Attributes{
  79. Name: m.Name,
  80. },
  81. }
  82. if m.PeerURLs != nil {
  83. mm.PeerURLs = make([]string, len(m.PeerURLs))
  84. copy(mm.PeerURLs, m.PeerURLs)
  85. }
  86. if m.ClientURLs != nil {
  87. mm.ClientURLs = make([]string, len(m.ClientURLs))
  88. copy(mm.ClientURLs, m.ClientURLs)
  89. }
  90. return mm
  91. }
  92. func (m *Member) IsStarted() bool {
  93. return len(m.Name) != 0
  94. }
  95. // MembersByID implements sort by ID interface
  96. type MembersByID []*Member
  97. func (ms MembersByID) Len() int { return len(ms) }
  98. func (ms MembersByID) Less(i, j int) bool { return ms[i].ID < ms[j].ID }
  99. func (ms MembersByID) Swap(i, j int) { ms[i], ms[j] = ms[j], ms[i] }
  100. // MembersByPeerURLs implements sort by peer urls interface
  101. type MembersByPeerURLs []*Member
  102. func (ms MembersByPeerURLs) Len() int { return len(ms) }
  103. func (ms MembersByPeerURLs) Less(i, j int) bool {
  104. return ms[i].PeerURLs[0] < ms[j].PeerURLs[0]
  105. }
  106. func (ms MembersByPeerURLs) Swap(i, j int) { ms[i], ms[j] = ms[j], ms[i] }