cluster.go 4.0 KB

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