peers.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package etcdhttp
  2. import (
  3. "bytes"
  4. "fmt"
  5. "log"
  6. "math/rand"
  7. "net/http"
  8. "net/url"
  9. "sort"
  10. "strconv"
  11. "github.com/coreos/etcd/elog"
  12. "github.com/coreos/etcd/raft/raftpb"
  13. )
  14. // Peers contains a mapping of unique IDs to a list of hostnames/IP addresses
  15. type Peers map[int64][]string
  16. // addScheme adds the protocol prefix to a string; currently only HTTP
  17. // TODO: improve this when implementing TLS
  18. func addScheme(addr string) string {
  19. return fmt.Sprintf("http://%s", addr)
  20. }
  21. // Pick chooses a random address from a given Peer's addresses, and returns it as
  22. // an addressible URI. If the given peer does not exist, an empty string is returned.
  23. func (ps Peers) Pick(id int64) string {
  24. addrs := ps[id]
  25. if len(addrs) == 0 {
  26. return ""
  27. }
  28. return addScheme(addrs[rand.Intn(len(addrs))])
  29. }
  30. // Set parses command line sets of names to IPs formatted like:
  31. // a=1.1.1.1&a=1.1.1.2&b=2.2.2.2
  32. func (ps *Peers) Set(s string) error {
  33. m := make(map[int64][]string)
  34. v, err := url.ParseQuery(s)
  35. if err != nil {
  36. return err
  37. }
  38. for k, v := range v {
  39. id, err := strconv.ParseInt(k, 0, 64)
  40. if err != nil {
  41. return err
  42. }
  43. m[id] = v
  44. }
  45. *ps = m
  46. return nil
  47. }
  48. func (ps *Peers) String() string {
  49. v := url.Values{}
  50. for k, vv := range *ps {
  51. for i := range vv {
  52. v.Add(strconv.FormatInt(k, 16), vv[i])
  53. }
  54. }
  55. return v.Encode()
  56. }
  57. func (ps Peers) IDs() []int64 {
  58. var ids []int64
  59. for id := range ps {
  60. ids = append(ids, id)
  61. }
  62. return ids
  63. }
  64. // Endpoints returns a list of all peer addresses. Each address is prefixed
  65. // with the scheme (currently "http://"). The returned list is sorted in
  66. // ascending lexicographical order.
  67. func (ps Peers) Endpoints() []string {
  68. endpoints := make([]string, 0)
  69. for _, addrs := range ps {
  70. for _, addr := range addrs {
  71. endpoints = append(endpoints, addScheme(addr))
  72. }
  73. }
  74. sort.Strings(endpoints)
  75. return endpoints
  76. }
  77. func Sender(p Peers) func(msgs []raftpb.Message) {
  78. return func(msgs []raftpb.Message) {
  79. for _, m := range msgs {
  80. // TODO: reuse go routines
  81. // limit the number of outgoing connections for the same receiver
  82. go send(p, m)
  83. }
  84. }
  85. }
  86. func send(p Peers, m raftpb.Message) {
  87. // TODO (xiangli): reasonable retry logic
  88. for i := 0; i < 3; i++ {
  89. url := p.Pick(m.To)
  90. if url == "" {
  91. // TODO: unknown peer id.. what do we do? I
  92. // don't think his should ever happen, need to
  93. // look into this further.
  94. log.Printf("etcdhttp: no addr for %d", m.To)
  95. return
  96. }
  97. url += raftPrefix
  98. // TODO: don't block. we should be able to have 1000s
  99. // of messages out at a time.
  100. data, err := m.Marshal()
  101. if err != nil {
  102. log.Println("etcdhttp: dropping message:", err)
  103. return // drop bad message
  104. }
  105. if httpPost(url, data) {
  106. return // success
  107. }
  108. // TODO: backoff
  109. }
  110. }
  111. func httpPost(url string, data []byte) bool {
  112. // TODO: set timeouts
  113. resp, err := http.Post(url, "application/protobuf", bytes.NewBuffer(data))
  114. if err != nil {
  115. elog.TODO()
  116. return false
  117. }
  118. resp.Body.Close()
  119. if resp.StatusCode != http.StatusNoContent {
  120. elog.TODO()
  121. return false
  122. }
  123. return true
  124. }