standby.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 stopped\n")
  57. return stopMode
  58. }
  59. if update, err := s.syncCluster(nodes); err != nil {
  60. log.Println("standby sync:", 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. return participantMode
  70. }
  71. }
  72. func (s *standby) stop() {
  73. close(s.stopc)
  74. }
  75. func (s *standby) leaderInfo() (int64, string) {
  76. s.mu.RLock()
  77. defer s.mu.RUnlock()
  78. return s.leader, s.leaderAddr
  79. }
  80. func (s *standby) setLeaderInfo(leader int64, leaderAddr string) {
  81. s.mu.Lock()
  82. defer s.mu.Unlock()
  83. s.leader, s.leaderAddr = leader, leaderAddr
  84. }
  85. func (s *standby) serveRedirect(w http.ResponseWriter, r *http.Request) error {
  86. leader, leaderAddr := s.leaderInfo()
  87. if leader == noneId {
  88. return fmt.Errorf("no leader in the cluster")
  89. }
  90. redirectAddr, err := buildRedirectURL(leaderAddr, r.URL)
  91. if err != nil {
  92. return err
  93. }
  94. http.Redirect(w, r, redirectAddr, http.StatusTemporaryRedirect)
  95. return nil
  96. }
  97. func (s *standby) syncCluster(nodes map[string]bool) (map[string]bool, error) {
  98. for node := range nodes {
  99. machines, err := s.client.GetMachines(node)
  100. if err != nil {
  101. continue
  102. }
  103. config, err := s.client.GetClusterConfig(node)
  104. if err != nil {
  105. continue
  106. }
  107. nn := make(map[string]bool)
  108. for _, machine := range machines {
  109. nn[machine.PeerURL] = true
  110. if machine.State == stateLeader {
  111. id, err := strconv.ParseInt(machine.Name, 0, 64)
  112. if err != nil {
  113. return nil, err
  114. }
  115. s.setLeaderInfo(id, machine.PeerURL)
  116. }
  117. }
  118. s.clusterConf = config
  119. return nn, nil
  120. }
  121. return nil, fmt.Errorf("unreachable cluster")
  122. }