cluster.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. "math/rand"
  19. "net/url"
  20. "sort"
  21. "strings"
  22. "github.com/coreos/etcd/pkg/flags"
  23. "github.com/coreos/etcd/pkg/types"
  24. )
  25. // Cluster is a list of Members that belong to the same raft cluster
  26. type Cluster struct {
  27. id uint64
  28. name string
  29. members map[uint64]*Member
  30. }
  31. func NewCluster(clusterName string) *Cluster {
  32. return &Cluster{name: clusterName, members: make(map[uint64]*Member)}
  33. }
  34. func (c Cluster) FindName(name string) *Member {
  35. for _, m := range c.members {
  36. if m.Name == name {
  37. return m
  38. }
  39. }
  40. return nil
  41. }
  42. func (c Cluster) FindID(id uint64) *Member {
  43. return c.members[id]
  44. }
  45. func (c Cluster) Add(m Member) error {
  46. if c.FindID(m.ID) != nil {
  47. return fmt.Errorf("Member exists with identical ID %v", m)
  48. }
  49. c.members[m.ID] = &m
  50. return nil
  51. }
  52. func (c *Cluster) AddSlice(mems []Member) error {
  53. for _, m := range mems {
  54. err := c.Add(m)
  55. if err != nil {
  56. return err
  57. }
  58. }
  59. return nil
  60. }
  61. // Pick chooses a random address from a given Member's addresses, and returns it as
  62. // an addressible URI. If the given member does not exist, an empty string is returned.
  63. func (c Cluster) Pick(id uint64) string {
  64. if m := c.FindID(id); m != nil {
  65. urls := m.PeerURLs
  66. if len(urls) == 0 {
  67. return ""
  68. }
  69. return urls[rand.Intn(len(urls))]
  70. }
  71. return ""
  72. }
  73. // SetMembersFromString parses a sets of names to IPs either from the command line or discovery formatted like:
  74. // mach0=http://1.1.1.1,mach0=http://2.2.2.2,mach0=http://1.1.1.1,mach1=http://2.2.2.2,mach1=http://3.3.3.3
  75. func (c *Cluster) SetMembersFromString(s string) error {
  76. c.members = make(map[uint64]*Member)
  77. v, err := url.ParseQuery(strings.Replace(s, ",", "&", -1))
  78. if err != nil {
  79. return err
  80. }
  81. for name, urls := range v {
  82. if len(urls) == 0 || urls[0] == "" {
  83. return fmt.Errorf("Empty URL given for %q", name)
  84. }
  85. m := newMember(name, types.URLs(*flags.NewURLsValue(strings.Join(urls, ","))), c.name, nil)
  86. err := c.Add(*m)
  87. if err != nil {
  88. return err
  89. }
  90. }
  91. return nil
  92. }
  93. func (c *Cluster) AddMemberFromURLs(name string, urls types.URLs) (*Member, error) {
  94. m := newMember(name, urls, c.name, nil)
  95. err := c.Add(*m)
  96. if err != nil {
  97. return nil, err
  98. }
  99. return m, nil
  100. }
  101. func (c *Cluster) GenID(salt []byte) {
  102. mIDs := c.MemberIDs()
  103. b := make([]byte, 8*len(mIDs))
  104. for i, id := range mIDs {
  105. binary.BigEndian.PutUint64(b[8*i:], id)
  106. }
  107. b = append(b, salt...)
  108. hash := sha1.Sum(b)
  109. c.id = binary.BigEndian.Uint64(hash[:8])
  110. }
  111. func (c Cluster) String() string {
  112. sl := []string{}
  113. for _, m := range c.members {
  114. for _, u := range m.PeerURLs {
  115. sl = append(sl, fmt.Sprintf("%s=%s", m.Name, u))
  116. }
  117. }
  118. sort.Strings(sl)
  119. return strings.Join(sl, ",")
  120. }
  121. func (c Cluster) ID() uint64 { return c.id }
  122. func (c Cluster) Members() map[uint64]*Member { return c.members }
  123. func (c Cluster) MemberIDs() []uint64 {
  124. var ids []uint64
  125. for _, m := range c.members {
  126. ids = append(ids, m.ID)
  127. }
  128. sort.Sort(types.Uint64Slice(ids))
  129. return ids
  130. }
  131. // PeerURLs returns a list of all peer addresses. Each address is prefixed
  132. // with the scheme (currently "http://"). The returned list is sorted in
  133. // ascending lexicographical order.
  134. func (c Cluster) PeerURLs() []string {
  135. endpoints := make([]string, 0)
  136. for _, p := range c.members {
  137. for _, addr := range p.PeerURLs {
  138. endpoints = append(endpoints, addr)
  139. }
  140. }
  141. sort.Strings(endpoints)
  142. return endpoints
  143. }
  144. // ClientURLs returns a list of all client addresses. Each address is prefixed
  145. // with the scheme (currently "http://"). The returned list is sorted in
  146. // ascending lexicographical order.
  147. func (c Cluster) ClientURLs() []string {
  148. urls := make([]string, 0)
  149. for _, p := range c.members {
  150. for _, url := range p.ClientURLs {
  151. urls = append(urls, url)
  152. }
  153. }
  154. sort.Strings(urls)
  155. return urls
  156. }