member.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package etcdserver
  14. import (
  15. "crypto/sha1"
  16. "encoding/binary"
  17. "fmt"
  18. "log"
  19. "math/rand"
  20. "path"
  21. "sort"
  22. "time"
  23. "github.com/coreos/etcd/pkg/strutil"
  24. "github.com/coreos/etcd/pkg/types"
  25. )
  26. // RaftAttributes represents the raft related attributes of an etcd member.
  27. type RaftAttributes struct {
  28. // TODO(philips): ensure these are URLs
  29. PeerURLs []string `json:"peerURLs"`
  30. }
  31. // Attributes represents all the non-raft related attributes of an etcd member.
  32. type Attributes struct {
  33. Name string `json:"name,omitempty"`
  34. ClientURLs []string `json:"clientURLs,omitempty"`
  35. }
  36. type Member struct {
  37. ID uint64 `json:"id"`
  38. RaftAttributes
  39. Attributes
  40. }
  41. // newMember creates a Member without an ID and generates one based on the
  42. // name, peer URLs. This is used for bootstrapping/adding new member.
  43. func NewMember(name string, peerURLs types.URLs, clusterName string, now *time.Time) *Member {
  44. m := &Member{
  45. RaftAttributes: RaftAttributes{PeerURLs: peerURLs.StringSlice()},
  46. Attributes: Attributes{Name: name},
  47. }
  48. var b []byte
  49. sort.Strings(m.PeerURLs)
  50. for _, p := range m.PeerURLs {
  51. b = append(b, []byte(p)...)
  52. }
  53. b = append(b, []byte(clusterName)...)
  54. if now != nil {
  55. b = append(b, []byte(fmt.Sprintf("%d", now.Unix()))...)
  56. }
  57. hash := sha1.Sum(b)
  58. m.ID = binary.BigEndian.Uint64(hash[:8])
  59. return m
  60. }
  61. // PickPeerURL chooses a random address from a given Member's PeerURLs.
  62. // It will panic if there is no PeerURLs available in Member.
  63. func (m *Member) PickPeerURL() string {
  64. if len(m.PeerURLs) == 0 {
  65. panic("member should always have some peer url")
  66. }
  67. return m.PeerURLs[rand.Intn(len(m.PeerURLs))]
  68. }
  69. func memberStoreKey(id uint64) string {
  70. return path.Join(storeMembersPrefix, strutil.IDAsHex(id))
  71. }
  72. func mustParseMemberIDFromKey(key string) uint64 {
  73. id, err := strutil.IDFromHex(path.Base(key))
  74. if err != nil {
  75. log.Panicf("unexpected parse member id error: %v", err)
  76. }
  77. return id
  78. }
  79. func removedMemberStoreKey(id uint64) string {
  80. return path.Join(storeRemovedMembersPrefix, strutil.IDAsHex(id))
  81. }
  82. type SortableMemberSliceByPeerURLs []*Member
  83. func (p SortableMemberSliceByPeerURLs) Len() int { return len(p) }
  84. func (p SortableMemberSliceByPeerURLs) Less(i, j int) bool {
  85. return p[i].PeerURLs[0] < p[j].PeerURLs[0]
  86. }
  87. func (p SortableMemberSliceByPeerURLs) Swap(i, j int) { p[i], p[j] = p[j], p[i] }