raft_handler.go 3.3 KB

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