raft_handler.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. "encoding/json"
  16. "fmt"
  17. "log"
  18. "net/http"
  19. "strconv"
  20. "sync"
  21. "github.com/coreos/etcd/raft"
  22. "github.com/coreos/etcd/store"
  23. )
  24. const (
  25. raftPrefix = "/raft"
  26. )
  27. type raftHandler struct {
  28. mu sync.RWMutex
  29. serving bool
  30. peerGetter peerGetter
  31. storeVersion int
  32. recv chan *raft.Message
  33. *http.ServeMux
  34. }
  35. func newRaftHandler(p peerGetter, version int) *raftHandler {
  36. h := &raftHandler{
  37. recv: make(chan *raft.Message, 512),
  38. peerGetter: p,
  39. storeVersion: version,
  40. }
  41. h.ServeMux = http.NewServeMux()
  42. h.ServeMux.HandleFunc(raftPrefix+"/cfg/", h.serveCfg)
  43. h.ServeMux.HandleFunc(raftPrefix, h.serveRaft)
  44. h.ServeMux.HandleFunc(raftPrefix+"/version", h.serveVersion)
  45. h.ServeMux.HandleFunc(raftPrefix+"/version/", h.serveVersionCheck)
  46. return h
  47. }
  48. func (h *raftHandler) start() {
  49. h.mu.Lock()
  50. h.serving = true
  51. h.mu.Unlock()
  52. }
  53. func (h *raftHandler) stop() {
  54. h.mu.Lock()
  55. h.serving = false
  56. h.mu.Unlock()
  57. }
  58. func (h *raftHandler) serveRaft(w http.ResponseWriter, r *http.Request) {
  59. h.mu.RLock()
  60. serving := h.serving
  61. h.mu.RUnlock()
  62. if !serving {
  63. http.Error(w, "404 page not found", http.StatusNotFound)
  64. return
  65. }
  66. msg := new(raft.Message)
  67. if err := json.NewDecoder(r.Body).Decode(msg); err != nil {
  68. log.Printf("raftHandler.serve decodeErr=\"%v\"\n", err)
  69. return
  70. }
  71. select {
  72. case h.recv <- msg:
  73. default:
  74. log.Printf("raftHandler.serve pushErr=\"recv channel is full\"\n")
  75. // drop the incoming package at network layer if the upper layer
  76. // cannot consume them in time.
  77. // TODO(xiangli): not return 200.
  78. }
  79. return
  80. }
  81. func (h *raftHandler) serveCfg(w http.ResponseWriter, r *http.Request) {
  82. h.mu.RLock()
  83. serving := h.serving
  84. h.mu.RUnlock()
  85. if !serving {
  86. http.Error(w, "404 page not found", http.StatusNotFound)
  87. return
  88. }
  89. id, err := strconv.ParseInt(r.URL.Path[len("/raft/cfg/"):], 10, 64)
  90. if err != nil {
  91. http.Error(w, err.Error(), http.StatusBadRequest)
  92. return
  93. }
  94. p, err := h.peerGetter.peer(id)
  95. if err == nil {
  96. w.Write([]byte(p.url))
  97. return
  98. }
  99. http.Error(w, err.Error(), http.StatusNotFound)
  100. }
  101. func (h *raftHandler) serveVersion(w http.ResponseWriter, req *http.Request) {
  102. w.Write([]byte(strconv.Itoa(h.storeVersion)))
  103. }
  104. func (h *raftHandler) serveVersionCheck(w http.ResponseWriter, req *http.Request) {
  105. var version int
  106. n, err := fmt.Sscanf(req.URL.Path, raftPrefix+"/version/%d/check", &version)
  107. if err != nil || n != 1 {
  108. http.Error(w, "error version check format: "+req.URL.Path, http.StatusBadRequest)
  109. return
  110. }
  111. if version < store.MinVersion() || version > store.MaxVersion() {
  112. w.WriteHeader(http.StatusForbidden)
  113. return
  114. }
  115. }