standby.go 3.1 KB

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