cluster.go 4.3 KB

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