cluster.go 3.8 KB

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