github.com/kafkaliu/etcd@v0.1.2-0.20131007164923-44c16dd30d69/raft_handlers.go (about) 1 /* 2 Copyright 2013 CoreOS Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "encoding/json" 21 "net/http" 22 23 "github.com/coreos/go-raft" 24 ) 25 26 //------------------------------------------------------------- 27 // Handlers to handle raft related request via raft server port 28 //------------------------------------------------------------- 29 30 // Get all the current logs 31 func GetLogHttpHandler(w http.ResponseWriter, req *http.Request) { 32 debugf("[recv] GET %s/log", r.url) 33 w.Header().Set("Content-Type", "application/json") 34 w.WriteHeader(http.StatusOK) 35 json.NewEncoder(w).Encode(r.LogEntries()) 36 } 37 38 // Response to vote request 39 func VoteHttpHandler(w http.ResponseWriter, req *http.Request) { 40 rvreq := &raft.RequestVoteRequest{} 41 err := decodeJsonRequest(req, rvreq) 42 if err == nil { 43 debugf("[recv] POST %s/vote [%s]", r.url, rvreq.CandidateName) 44 if resp := r.RequestVote(rvreq); resp != nil { 45 w.WriteHeader(http.StatusOK) 46 json.NewEncoder(w).Encode(resp) 47 return 48 } 49 } 50 warnf("[vote] ERROR: %v", err) 51 w.WriteHeader(http.StatusInternalServerError) 52 } 53 54 // Response to append entries request 55 func AppendEntriesHttpHandler(w http.ResponseWriter, req *http.Request) { 56 aereq := &raft.AppendEntriesRequest{} 57 err := decodeJsonRequest(req, aereq) 58 59 if err == nil { 60 debugf("[recv] POST %s/log/append [%d]", r.url, len(aereq.Entries)) 61 62 r.serverStats.RecvAppendReq(aereq.LeaderName, int(req.ContentLength)) 63 64 if resp := r.AppendEntries(aereq); resp != nil { 65 w.WriteHeader(http.StatusOK) 66 json.NewEncoder(w).Encode(resp) 67 if !resp.Success { 68 debugf("[Append Entry] Step back") 69 } 70 return 71 } 72 } 73 warnf("[Append Entry] ERROR: %v", err) 74 w.WriteHeader(http.StatusInternalServerError) 75 } 76 77 // Response to recover from snapshot request 78 func SnapshotHttpHandler(w http.ResponseWriter, req *http.Request) { 79 aereq := &raft.SnapshotRequest{} 80 err := decodeJsonRequest(req, aereq) 81 if err == nil { 82 debugf("[recv] POST %s/snapshot/ ", r.url) 83 if resp := r.RequestSnapshot(aereq); resp != nil { 84 w.WriteHeader(http.StatusOK) 85 json.NewEncoder(w).Encode(resp) 86 return 87 } 88 } 89 warnf("[Snapshot] ERROR: %v", err) 90 w.WriteHeader(http.StatusInternalServerError) 91 } 92 93 // Response to recover from snapshot request 94 func SnapshotRecoveryHttpHandler(w http.ResponseWriter, req *http.Request) { 95 aereq := &raft.SnapshotRecoveryRequest{} 96 err := decodeJsonRequest(req, aereq) 97 if err == nil { 98 debugf("[recv] POST %s/snapshotRecovery/ ", r.url) 99 if resp := r.SnapshotRecoveryRequest(aereq); resp != nil { 100 w.WriteHeader(http.StatusOK) 101 json.NewEncoder(w).Encode(resp) 102 return 103 } 104 } 105 warnf("[Snapshot] ERROR: %v", err) 106 w.WriteHeader(http.StatusInternalServerError) 107 } 108 109 // Get the port that listening for etcd connecting of the server 110 func EtcdURLHttpHandler(w http.ResponseWriter, req *http.Request) { 111 debugf("[recv] Get %s/etcdURL/ ", r.url) 112 w.WriteHeader(http.StatusOK) 113 w.Write([]byte(argInfo.EtcdURL)) 114 } 115 116 // Response to the join request 117 func JoinHttpHandler(w http.ResponseWriter, req *http.Request) error { 118 119 command := &JoinCommand{} 120 121 if err := decodeJsonRequest(req, command); err == nil { 122 debugf("Receive Join Request from %s", command.Name) 123 return dispatch(command, w, req, false) 124 } else { 125 w.WriteHeader(http.StatusInternalServerError) 126 return nil 127 } 128 } 129 130 // Response to remove request 131 func RemoveHttpHandler(w http.ResponseWriter, req *http.Request) { 132 if req.Method != "DELETE" { 133 w.WriteHeader(http.StatusMethodNotAllowed) 134 return 135 } 136 137 nodeName := req.URL.Path[len("/remove/"):] 138 command := &RemoveCommand{ 139 Name: nodeName, 140 } 141 142 debugf("[recv] Remove Request [%s]", command.Name) 143 144 dispatch(command, w, req, false) 145 146 } 147 148 // Response to the name request 149 func NameHttpHandler(w http.ResponseWriter, req *http.Request) { 150 debugf("[recv] Get %s/name/ ", r.url) 151 w.WriteHeader(http.StatusOK) 152 w.Write([]byte(r.name)) 153 } 154 155 // Response to the name request 156 func RaftVersionHttpHandler(w http.ResponseWriter, req *http.Request) { 157 debugf("[recv] Get %s/version/ ", r.url) 158 w.WriteHeader(http.StatusOK) 159 w.Write([]byte(r.version)) 160 }