standby.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. "log"
  17. "net/http"
  18. "strconv"
  19. "sync"
  20. "time"
  21. "github.com/coreos/etcd/conf"
  22. )
  23. var (
  24. noneId int64 = -1
  25. )
  26. type standby struct {
  27. client *v2client
  28. peerHub *peerHub
  29. leader int64
  30. leaderAddr string
  31. mu sync.RWMutex
  32. clusterConf *conf.ClusterConfig
  33. *http.ServeMux
  34. }
  35. func newStandby(client *v2client, peerHub *peerHub) *standby {
  36. s := &standby{
  37. client: client,
  38. peerHub: peerHub,
  39. leader: noneId,
  40. leaderAddr: "",
  41. clusterConf: conf.NewClusterConfig(),
  42. ServeMux: http.NewServeMux(),
  43. }
  44. s.Handle("/", handlerErr(s.serveRedirect))
  45. return s
  46. }
  47. func (s *standby) run(stop chan struct{}) {
  48. syncDuration := time.Millisecond * 100
  49. nodes := s.peerHub.getSeeds()
  50. for {
  51. select {
  52. case <-time.After(syncDuration):
  53. case <-stop:
  54. log.Printf("standby.stop\n")
  55. return
  56. }
  57. if update, err := s.syncCluster(nodes); err != nil {
  58. log.Printf("standby.run syncErr=\"%v\"", err)
  59. continue
  60. } else {
  61. nodes = update
  62. }
  63. syncDuration = time.Duration(s.clusterConf.SyncInterval * float64(time.Second))
  64. if s.clusterConf.ActiveSize <= len(nodes) {
  65. continue
  66. }
  67. log.Printf("standby.end\n")
  68. return
  69. }
  70. }
  71. func (s *standby) leaderInfo() (int64, string) {
  72. s.mu.RLock()
  73. defer s.mu.RUnlock()
  74. return s.leader, s.leaderAddr
  75. }
  76. func (s *standby) setLeaderInfo(leader int64, leaderAddr string) {
  77. s.mu.Lock()
  78. defer s.mu.Unlock()
  79. s.leader, s.leaderAddr = leader, leaderAddr
  80. }
  81. func (s *standby) serveRedirect(w http.ResponseWriter, r *http.Request) error {
  82. leader, leaderAddr := s.leaderInfo()
  83. if leader == noneId {
  84. return fmt.Errorf("no leader in the cluster")
  85. }
  86. redirectAddr, err := buildRedirectURL(leaderAddr, r.URL)
  87. if err != nil {
  88. return err
  89. }
  90. http.Redirect(w, r, redirectAddr, http.StatusTemporaryRedirect)
  91. return nil
  92. }
  93. func (s *standby) syncCluster(nodes map[string]bool) (map[string]bool, error) {
  94. for node := range nodes {
  95. machines, err := s.client.GetMachines(node)
  96. if err != nil {
  97. continue
  98. }
  99. cfg, err := s.client.GetClusterConfig(node)
  100. if err != nil {
  101. continue
  102. }
  103. nn := make(map[string]bool)
  104. for _, machine := range machines {
  105. nn[machine.PeerURL] = true
  106. if machine.State == stateLeader {
  107. id, err := strconv.ParseInt(machine.Name, 0, 64)
  108. if err != nil {
  109. return nil, err
  110. }
  111. s.setLeaderInfo(id, machine.PeerURL)
  112. }
  113. }
  114. s.clusterConf = cfg
  115. return nn, nil
  116. }
  117. return nil, fmt.Errorf("unreachable cluster")
  118. }