Skip to content

Commit ba15067

Browse files
committed
update: a little
1 parent 4ef2e18 commit ba15067

File tree

4 files changed

+24
-25
lines changed

4 files changed

+24
-25
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea/
22
vendor/
3-
example/kv/log
3+
example/kv/log
4+
example/kv/snapshot

example/kv/main.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,24 @@ type Command struct {
2828
}
2929

3030
type Server struct {
31-
raft.StateMachine
32-
peer raft.Raft
33-
r *gin.Engine
34-
state map[string]string
35-
locker sync.RWMutex
36-
pool *sync.Pool
37-
ch chan *Command
31+
peer raft.Raft
32+
r *gin.Engine
33+
state map[string]string
34+
locker sync.RWMutex
35+
cmdPool sync.Pool
3836
}
3937

4038
func New() *Server {
41-
return &Server{
39+
s := &Server{
4240
state: map[string]string{},
4341
locker: sync.RWMutex{},
44-
pool: &sync.Pool{
42+
cmdPool: sync.Pool{
4543
New: func() interface{} {
4644
return new(Command)
4745
},
4846
},
49-
ch: make(chan *Command, 1024),
5047
}
48+
return s
5149
}
5250

5351
// Save 读取状态机快照
@@ -66,27 +64,24 @@ func (s *Server) Recovery(state []byte) error {
6664

6765
// Apply 状态机执行命令的回调函数
6866
func (s *Server) Apply(commandName string, command []byte) {
69-
//
70-
}
71-
72-
func (s *Server) handle(commandName string, command []byte) {
73-
cmd := s.pool.Get().(*Command)
74-
_ = json.Unmarshal(command, cmd)
67+
cmd := s.cmdPool.Get().(*Command)
68+
_ = json.Unmarshal(command, &cmd)
7569

76-
// log.Debugf("commandName:%s key:%s value:%s", commandName, cmd.Key, cmd.Value)
7770
switch commandName {
7871
case SET:
7972
s.state[cmd.Key] = cmd.Value
8073
case DEL:
8174
delete(s.state, cmd.Key)
8275
}
83-
s.pool.Put(cmd)
76+
77+
*cmd = Command{}
78+
s.cmdPool.Put(cmd)
8479
}
8580

8681
func (s *Server) Serve(addr string) {
8782
r := gin.Default()
8883
r.POST("/test", func(c *gin.Context) {
89-
cmd := s.pool.Get().(*Command)
84+
cmd := &Command{}
9085
cmd.Key = "hello"
9186
cmd.Value = "world"
9287
data, _ := json.Marshal(cmd)
@@ -124,6 +119,8 @@ func (s *Server) Serve(addr string) {
124119
used := time.Now().Sub(since).Milliseconds()
125120
done <- struct{}{}
126121

122+
s.peer.TakeSnapshot()
123+
127124
c.JSON(http.StatusOK, gin.H{
128125
"code": 0,
129126
"detail": fmt.Sprintf("count:%d success:%d used:%dms average:%dms",
@@ -136,7 +133,7 @@ func (s *Server) Serve(addr string) {
136133
return
137134
}
138135

139-
cmd := s.pool.Get().(*Command)
136+
cmd := &Command{}
140137
cmd.Key = c.Query("key")
141138
cmd.Value = c.Query("value")
142139
data, _ := json.Marshal(cmd)
@@ -155,7 +152,7 @@ func (s *Server) Serve(addr string) {
155152
return
156153
}
157154

158-
cmd := s.pool.Get().(*Command)
155+
cmd := &Command{}
159156
cmd.Key = c.Query("key")
160157
data, _ := json.Marshal(cmd)
161158

log.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ func (l *Log) encodeLogEntry(w io.Writer, e *pb.LogEntry) (int, error) {
513513
return -1, err
514514
}
515515

516-
if _, err = fmt.Fprintf(w, "%8x\n", len(b)); err != nil {
516+
if _, err = fmt.Fprintf(w, "%08x", len(b)); err != nil {
517517
return -1, err
518518
}
519519

@@ -523,7 +523,7 @@ func (l *Log) encodeLogEntry(w io.Writer, e *pb.LogEntry) (int, error) {
523523
func (l *Log) decodeLogEntry(r io.Reader, e *pb.LogEntry) (int, error) {
524524
var length int = 0
525525

526-
_, err := fmt.Fscanf(r, "%8x\n", &length)
526+
_, err := fmt.Fscanf(r, "%08x", &length)
527527
if err != nil {
528528
return -1, err
529529
}

server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net"
1313
"os"
1414
"path"
15+
"path/filepath"
1516
"sort"
1617
"sync"
1718
"sync/atomic"
@@ -944,7 +945,7 @@ func (s *server) CurrentTerm() uint64 {
944945

945946
// snapshotPath 快照路径
946947
func (s *server) snapshotPath(lastIndex, lastTerm uint64) string {
947-
return path.Join(s.config.SnapshotPath, "snapshot", fmt.Sprintf("%d_%d.ss", lastTerm, lastIndex))
948+
return filepath.Join(s.config.SnapshotPath, "snapshot", fmt.Sprintf("%d_%d.ss", lastTerm, lastIndex))
948949
}
949950

950951
// TakeSnapshot 创建快照

0 commit comments

Comments
 (0)