cluster.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. "fmt"
  16. "math/rand"
  17. "net/url"
  18. "sort"
  19. "strings"
  20. "github.com/coreos/etcd/pkg/flags"
  21. "github.com/coreos/etcd/pkg/types"
  22. )
  23. // Cluster is a list of Members that belong to the same raft cluster
  24. type Cluster map[uint64]*Member
  25. func (c Cluster) FindID(id uint64) *Member {
  26. return c[id]
  27. }
  28. func (c Cluster) FindName(name string) *Member {
  29. for _, m := range c {
  30. if m.Name == name {
  31. return m
  32. }
  33. }
  34. return nil
  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[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 = Cluster{}
  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) String() string {
  85. sl := []string{}
  86. for _, m := range c {
  87. for _, u := range m.PeerURLs {
  88. sl = append(sl, fmt.Sprintf("%s=%s", m.Name, u))
  89. }
  90. }
  91. sort.Strings(sl)
  92. return strings.Join(sl, ",")
  93. }
  94. func (c Cluster) IDs() []uint64 {
  95. var ids []uint64
  96. for _, m := range c {
  97. ids = append(ids, m.ID)
  98. }
  99. sort.Sort(types.Uint64Slice(ids))
  100. return ids
  101. }
  102. // PeerURLs returns a list of all peer addresses. Each address is prefixed
  103. // with the scheme (currently "http://"). The returned list is sorted in
  104. // ascending lexicographical order.
  105. func (c Cluster) PeerURLs() []string {
  106. endpoints := make([]string, 0)
  107. for _, p := range c {
  108. for _, addr := range p.PeerURLs {
  109. endpoints = append(endpoints, addr)
  110. }
  111. }
  112. sort.Strings(endpoints)
  113. return endpoints
  114. }
  115. // ClientURLs returns a list of all client addresses. Each address is prefixed
  116. // with the scheme (currently "http://"). The returned list is sorted in
  117. // ascending lexicographical order.
  118. func (c Cluster) ClientURLs() []string {
  119. urls := make([]string, 0)
  120. for _, p := range c {
  121. for _, url := range p.ClientURLs {
  122. urls = append(urls, url)
  123. }
  124. }
  125. sort.Strings(urls)
  126. return urls
  127. }