Skip to content

Commit

Permalink
release: v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
summer-boythink committed Mar 19, 2024
1 parent 643fc1f commit e127846
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
all:build

build:fmt
go build -o ./build/server ./example/server/
go build -o ./build/client ./example/client/
go build -o ./build/server ./cmd/server/
go build -o ./build/client ./cmd/client/

run-server:build
./build/server --local http://127.0.0.1:8080 --peer http://127.0.0.1:8081 --peer http://127.0.0.1:8082
Expand Down
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## raft_go

This is a simple KV database based on `Raft` algorithm,**this implementation is very easy to understand**

## Usage

### build
```
make
```

### start-server
```sh
# node1 [leader]
./build/server --local http://127.0.0.1:8080 --peer http://127.0.0.1:8081 --peer http://127.0.0.1:8082

# node2
./build/server --local http://127.0.0.1:8081 --peer http://127.0.0.1:8082 --peer http://127.0.0.1:8080

# node3
./build/server --local http://127.0.0.1:8082 --peer http://127.0.0.1:8081 --peer http://127.0.0.1:8080
```

### start-client
```sh
# set key
./build/client set key1 val1 -a http://127.0.0.1:8080

# get key
./build/client get key1 -a http://127.0.0.1:8081

# rm key
./build/client rm key1 -a http://127.0.0.1:8080

```

## Future plan
1. I will change the `MemStorage` method to store the log on disk
2. I'm going to use `LSM trees` and other, more advanced data structures
3. Add tests
19 changes: 9 additions & 10 deletions example/client/main.go → cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,22 @@ func send(method string, addr string, path string, body interface{}) {
return
}
defer resp.Body.Close()
data, _ := io.ReadAll(resp.Body)
var result Response
json.Unmarshal(data, &result)
fmt.Println("Response:", result)
fmt.Println("Latency:", time.Since(start))
GetResponse(start, resp)
} else {
resp, err := http.Post(addr+path, "application/json", bytes.NewBuffer(jsonBody))
if err != nil {
fmt.Println("Error:", err)
return
}
defer resp.Body.Close()
data, _ := io.ReadAll(resp.Body)
var result Response
json.Unmarshal(data, &result)
fmt.Println("Response:", result)
fmt.Println("Latency:", time.Since(start))
GetResponse(start, resp)
}
}

func GetResponse(start time.Time, resp *http.Response) {
data, _ := io.ReadAll(resp.Body)
var result Response
json.Unmarshal(data, &result)
fmt.Println("Response:", result)
fmt.Println("Latency:", time.Since(start))
}
1 change: 0 additions & 1 deletion example/server/main.go → cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ func httpServe(raft *raftgo.Raft, stateMachine *raftgo.MemStateMachine, port int
})

r.GET("/get", func(c *gin.Context) {
// checkLeader(c, raft)

key := c.Query("key")
c.JSON(200, map[string]interface{}{"success": true, "value": stateMachine.Get(key)})
Expand Down
3 changes: 0 additions & 3 deletions raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ func (r *Raft) leaderSendHeartbeat(nextIndex map[int]int) []Reply {
})
}
}
// log.Println(replies)

return replies
}
Expand Down Expand Up @@ -333,12 +332,10 @@ func (rt *ResettableTimeout) Stop() {

func (rt *ResettableTimeout) Start() {
rt.Timer = time.AfterFunc(rt.Delay+time.Duration(rand.Intn(int(rt.Delay))), func() {
// log.Println("send heart")
rt.Callback <- true
})
}

func (rt *ResettableTimeout) Reset() {
log.Println("reset time")
rt.Timer.Reset(rt.Delay + time.Duration(rand.Intn(int(rt.Delay))))
}
1 change: 0 additions & 1 deletion rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ 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
1 change: 0 additions & 1 deletion state_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func (m *MemStateMachine) Get(key string) string {
func (m *MemStateMachine) Apply(command []byte) {
var cmd Command
err := json.Unmarshal(command, &cmd)
log.Println(string(command))
if err != nil {
log.Fatalf("Error unmarshalling command: %v", err)
}
Expand Down

0 comments on commit e127846

Please sign in to comment.