Skip to content

Commit

Permalink
add: debug post
Browse files Browse the repository at this point in the history
  • Loading branch information
summer-boythink committed Mar 18, 2024
1 parent a4b335b commit 640f334
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
1 change: 1 addition & 0 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ type RequestVoteArgs struct {
type RequestVoteReply struct {
Term int `json:"term"`
VoteGranted bool `json:"vote_granted"`
Online bool `json:"online"`
}
10 changes: 5 additions & 5 deletions example/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ func httpServe(raft *raftgo.Raft, stateMachine *raftgo.MemStateMachine, port int
r.POST("/append", func(c *gin.Context) {
checkLeader(c, raft)

var body any
var body CommandBody
c.BindJSON(&body)
commandBase64 := body.(CommandBody).command
commandBase64 := body.command
receiveHandleAppend := raft.HandleAppend(commandBase64)
select {
case <-receiveHandleAppend:
Expand Down Expand Up @@ -134,9 +134,9 @@ func main() {
rootCmd.MarkFlagRequired("local")
rootCmd.Flags().StringSliceVarP(&peer, "peer", "p", []string{}, "the raft server peer")
rootCmd.MarkFlagRequired("peer")
rootCmd.Flags().IntVarP(&rpcTimeout, "rpcTimeout", "r", 1000, "the raft server local url")
rootCmd.Flags().IntVarP(&heartbeatTimeout, "heartbeatTimeout", "", 3000, "")
rootCmd.Flags().IntVarP(&heartbeatInterval, "heartbeatInterval", "", 1000, "")
rootCmd.Flags().IntVarP(&rpcTimeout, "rpcTimeout", "r", 100, "the raft server local url")
rootCmd.Flags().IntVarP(&heartbeatTimeout, "heartbeatTimeout", "", 300, "")
rootCmd.Flags().IntVarP(&heartbeatInterval, "heartbeatInterval", "", 100, "")

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
Expand Down
28 changes: 13 additions & 15 deletions raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (r *Raft) HandleAppendEntries(aea AppendEntriesArgs) AppendEntriesReply {

func (r *Raft) HandleRequestVote(rv RequestVoteArgs) RequestVoteReply {
if rv.Term < r.CurrentTerm {
return RequestVoteReply{Term: r.CurrentTerm, VoteGranted: false}
return RequestVoteReply{Term: r.CurrentTerm, VoteGranted: false, Online: true}
}

if rv.Term > r.CurrentTerm {
Expand All @@ -99,10 +99,10 @@ func (r *Raft) HandleRequestVote(rv RequestVoteArgs) RequestVoteReply {
if r.Logs.IsUpToDate(rv.LastLogIndex, rv.LastLogTerm) {
r.Status = Follower
r.VotedFor = &rv.CandidateID
return RequestVoteReply{Term: r.CurrentTerm, VoteGranted: true}
return RequestVoteReply{Term: r.CurrentTerm, VoteGranted: true, Online: true}
}
}
return RequestVoteReply{Term: r.CurrentTerm, VoteGranted: false}
return RequestVoteReply{Term: r.CurrentTerm, VoteGranted: false, Online: true}
}

func (r *Raft) HandleAppend(command string) chan bool {
Expand Down Expand Up @@ -256,16 +256,14 @@ func (r *Raft) leaderSendHeartbeat(nextIndex map[int]int) []Reply {

replyCh := make(chan Reply)
for peerID, args := range appendEntriesArgs {
go func(peerID int, args AppendEntriesArgs) {
reply, err := r.Peers[peerID].AppendEntries(args, time.Duration(r.Config.RPCTimeout)*time.Millisecond)
if err == nil {
replyCh <- Reply{
AReply: reply,
PeerID: peerID,
AppendLen: len(args.Entries),
}
reply, err := r.Peers[peerID].AppendEntries(args, time.Duration(r.Config.RPCTimeout)*time.Millisecond)
if err == nil {
replyCh <- Reply{
AReply: reply,
PeerID: peerID,
AppendLen: len(args.Entries),
}
}(peerID, args)
}
}

var replies []Reply
Expand All @@ -286,18 +284,18 @@ func (r *Raft) electSelf() <-chan []RequestVoteReply {
last := r.Logs.Last()
var votes []RequestVoteReply
var res = make(chan []RequestVoteReply, 1)
// go func() {
for _, peer := range r.Peers {
vote, _ := peer.RequestVote(RequestVoteArgs{
Term: r.CurrentTerm,
CandidateID: r.ID,
LastLogIndex: last.LogIndex,
LastLogTerm: last.LogTerm,
}, time.Duration(r.Config.RPCTimeout)*time.Millisecond)
votes = append(votes, vote)
if vote.Online {
votes = append(votes, vote)
}
}
res <- votes
// }()
return res
}

Expand Down
2 changes: 1 addition & 1 deletion rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (p *HttpPeer) post(method string, data interface{}, timeout time.Duration)

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

log.Println(p.Addr + "/" + method)
req, err := http.NewRequestWithContext(ctx, "POST", p.Addr+"/"+method, bytes.NewBuffer(datas))
if err != nil {
return nil, err
Expand Down

0 comments on commit 640f334

Please sign in to comment.