Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:JameyWoo/tinykv_raft
- Loading branch information
Showing
5 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8= | ||
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I= | ||
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= | ||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= | ||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= | ||
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= | ||
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= | ||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package gorpc | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/rpc" | ||
"os" | ||
"strconv" | ||
"testing" | ||
) | ||
|
||
// 模拟一个raft******************************************************** | ||
type raft struct { | ||
Name string // 自己的名字,以端口号区分 | ||
peers []*rpc.Client // 这里直接使用 rpc 的 client | ||
} | ||
|
||
// 模拟raft 的方法, 原来的 labrpc 是创建所有的方法 | ||
func (test *raft) handler1(args string, reply *int) { | ||
args += " deal with handler1" | ||
*reply, _ = strconv.Atoi(args) | ||
} | ||
|
||
func (test *raft) handler2(args int, reply *string) { | ||
*reply = strconv.Itoa(args) + "deal with handler2" | ||
} | ||
|
||
|
||
|
||
func TestStartRaft(t *testing.T) { | ||
|
||
// 解析配置文件 | ||
// 模拟 读取自己 的 host | ||
me := "127.0.0.1:8001" | ||
var peers = []string{"127.0.0.1:8001", "127.0.0.1:8002"} | ||
|
||
testRaft := new(raft) | ||
|
||
gorpc := new(gorpc) | ||
gorpc.Addr = me //设置自己的host | ||
gorpc.Peers = peers //设置peers 的服务 | ||
gorpc.init(testRaft) | ||
|
||
t.Log("success") | ||
} | ||
|
||
// 封装 go 的rpc | ||
type gorpc struct { | ||
Addr string // 那个端口提供的服务, 在同一台机子上跑的话用端口作为区分 | ||
Peers []string // 暂存 | ||
} | ||
|
||
func (gorpc *gorpc) init(raft *raft) { | ||
_ = rpc.Register(raft) //代理 raft 的所有服务 | ||
tcpAddr, err := net.ResolveTCPAddr("tcp", gorpc.Addr) | ||
checkError(err) | ||
listener, err := net.ListenTCP("tcp", tcpAddr) | ||
checkError(err) | ||
go func() { | ||
for { | ||
conn, err := listener.Accept() | ||
if err != nil { | ||
continue | ||
} | ||
go func() { // 每来一个 rpc , 在这里记录日志 | ||
fmt.Printf("receive from %s\n", conn.RemoteAddr()) | ||
rpc.ServeConn(conn) | ||
}() | ||
} | ||
}() | ||
|
||
for _, p := range gorpc.Peers { | ||
client, err := rpc.Dial("tcp", p) // 这里会不会阻塞呢???? | ||
checkError(err) | ||
raft.peers = append(raft.peers, client) | ||
} | ||
} | ||
|
||
func checkError(err error) { | ||
if err != nil { | ||
fmt.Println("Fatal error ", err.Error()) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters