Skip to content

Commit

Permalink
Merge pull request #64 from coreos/master
Browse files Browse the repository at this point in the history
add encoding speed test and gofmt
  • Loading branch information
benbjohnson committed Jul 10, 2013
2 parents 7cfb6f5 + 62ddd37 commit da98ea0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 0 deletions append_entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type AppendEntriesRequest struct {
// The response returned from a server appending entries to the log.
type AppendEntriesResponse struct {
Term uint64 `json:"term"`
Index uint64 `json:"index"`
Success bool `json:"success"`
CommitIndex uint64 `json:"commitIndex"`
}
Expand Down
40 changes: 40 additions & 0 deletions encodingAndDecoding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package raft

import (
"bytes"
"encoding/json"
"fmt"
"testing"
"time"
)

func TestEncodingAndDecodingSpeed(t *testing.T) {
entries := make([]*LogEntry, 2000)

e := newLogEntry(nil, 1, 2, &joinCommand{Name: "localhost:1000"})

for i := 0; i < 2000; i++ {
entries[i] = e
}

req := newAppendEntriesRequest(1, "leader", 1, 1, entries, 1)

var b bytes.Buffer

startTime := time.Now()

json.NewEncoder(&b).Encode(req)

fmt.Println("Encoding ", b.Len()/1000, " kb took ", time.Now().Sub(startTime))

startTime = time.Now()

resp := &AppendEntriesResponse{}

length := b.Len()

json.NewDecoder(&b).Decode(&resp)

fmt.Println("Decoding ", length/1000, " kb took ", time.Now().Sub(startTime))

}
4 changes: 2 additions & 2 deletions peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func (p *Peer) startHeartbeat() {
// Stops the peer heartbeat.
func (p *Peer) stopHeartbeat() {
// here is a problem
// the previous stop is no buffer leader may get blocked
// when heartbeat returns at line 132
// the previous stop is no buffer leader may get blocked
// when heartbeat returns at line 132
// I make the channel with 1 buffer
// and try to panic here
select {
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ func (s *Server) processAppendEntriesResponse(resp *AppendEntriesResponse) {
s.debugln("commit index ", commitIndex)
for i := committedIndex; i < commitIndex; i++ {
if entry := s.log.getEntry(i + 1); entry != nil {
// if the leader is a new one and the entry came from the
// if the leader is a new one and the entry came from the
// old leader, the commit channel will be nil and no go routine
// is waiting from this channel
// if we try to send to it, the new leader will get stuck
Expand Down

0 comments on commit da98ea0

Please sign in to comment.