Skip to content
This repository has been archived by the owner on Feb 6, 2020. It is now read-only.

Add log compaction #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion raftd.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,19 @@ func main() {
server.Initialize()
} else {
join(server)
fmt.Println("success join")
}
}

// open snapshot
//go server.Snapshot()

// Create HTTP interface.
r := mux.NewRouter()
r.HandleFunc("/join", JoinHttpHandler).Methods("POST")
r.HandleFunc("/vote", VoteHttpHandler).Methods("POST")
r.HandleFunc("/log", GetLogHttpHandler).Methods("GET")
r.HandleFunc("/log/append", AppendEntriesHttpHandler).Methods("POST")
r.HandleFunc("/snapshot", SnapshotHttpHandler).Methods("POST")
r.HandleFunc("/files/{filename}", ReadFileHttpHandler).Methods("GET")
r.HandleFunc("/files/{filename}", WriteFileHttpHandler).Methods("POST")
http.Handle("/", r)
Expand Down Expand Up @@ -208,10 +212,12 @@ func (t transHandler) SendAppendEntriesRequest(server *raft.Server, peer *raft.P
debug("[send] POST http://%s/log/append [%d]", peer.Name(), len(req.Entries))
resp, err := http.Post(fmt.Sprintf("http://%s/log/append", peer.Name()), "application/json", &b)
if resp != nil {
defer resp.Body.Close()
aersp = &raft.AppendEntriesResponse{}
if err = json.NewDecoder(resp.Body).Decode(&aersp); err == nil || err == io.EOF {
return aersp, nil
}

}
return aersp, fmt.Errorf("raftd: Unable to append entries: %v", err)
}
Expand All @@ -224,14 +230,35 @@ func (t transHandler) SendVoteRequest(server *raft.Server, peer *raft.Peer, req
debug("[send] POST http://%s/vote", peer.Name())
resp, err := http.Post(fmt.Sprintf("http://%s/vote", peer.Name()), "application/json", &b)
if resp != nil {
defer resp.Body.Close()
rvrsp := &raft.RequestVoteResponse{}
if err = json.NewDecoder(resp.Body).Decode(&rvrsp); err == nil || err == io.EOF {
return rvrsp, nil
}

}
return rvrsp, fmt.Errorf("raftd: Unable to request vote: %v", err)
}

// Sends SnapshotRequest RPCs to a peer when the server is the candidate.
func (t transHandler) SendSnapshotRequest(server *raft.Server, peer *raft.Peer, req *raft.SnapshotRequest) (*raft.SnapshotResponse, error) {
var aersp *raft.SnapshotResponse
var b bytes.Buffer
json.NewEncoder(&b).Encode(req)
debug("[send] POST http://%s/snapshot [%d %d]", peer.Name(), req.LastTerm, req.LastIndex)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be better to use a URL object here instead of sprintf'ing the URL.

resp, err := http.Post(fmt.Sprintf("http://%s/snapshot", peer.Name()), "application/json", &b)
if resp != nil {
defer resp.Body.Close()
aersp = &raft.SnapshotResponse{}
if err = json.NewDecoder(resp.Body).Decode(&aersp); err == nil || err == io.EOF {

return aersp, nil
}
}
fmt.Println("error send snapshot")
return aersp, fmt.Errorf("raftd: Unable to send snapshot: %v", err)
}

//--------------------------------------
// HTTP Handlers
//--------------------------------------
Expand Down Expand Up @@ -281,13 +308,31 @@ func AppendEntriesHttpHandler(w http.ResponseWriter, req *http.Request) {
if resp, _ := server.AppendEntries(aereq); resp != nil {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
if !resp.Success {
fmt.Println("append error")
}
return
}
}
warn("[append] ERROR: %v", err)
w.WriteHeader(http.StatusInternalServerError)
}

func SnapshotHttpHandler(w http.ResponseWriter, req *http.Request) {
aereq := &raft.SnapshotRequest{}
err := decodeJsonRequest(req, aereq)
if err == nil {
debug("[recv] POST http://%s/snapshot/ ", server.Name())
if resp, _ := server.SnapshotRecovery(aereq.LastIndex, aereq.LastTerm, aereq.MachineState); resp != nil {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
return
}
}
warn("[snapshot] ERROR: %v", err)
w.WriteHeader(http.StatusInternalServerError)
}

func WriteFileHttpHandler(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
debug("[recv] POST http://%v/files/%s", server.Name(), vars["filename"])
Expand Down