Skip to content

Commit

Permalink
add docker deploy scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
LLiuJJ committed Sep 21, 2024
1 parent 193a4b3 commit f75dd43
Show file tree
Hide file tree
Showing 16 changed files with 393 additions and 364 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ default: meta_cli shard_server shard_cli meta_server
image:
docker build -f Dockerfile --network=host -t $(BUILDER_IMAGE) .

build_dev:
build-dev:
chmod +x scripts/build_dev.sh
docker run -it --rm -v $(realpath .):/eraft eraft/eraftbook:$(IMAGE_VERSION) /eraft/scripts/build_dev.sh

run_test:
run-test:
chmod +x scripts/run_tests.sh
docker run --name test-cli-node --network mytestnetwork --ip 172.18.0.5 -it --rm -v $(realpath .):/eraft eraft/eraftbook:$(IMAGE_VERSION) /eraft/scripts/run_tests.sh

Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,33 @@ eraft 中使用了 hash 分片的方法,我们将数据通过哈希算法映

它主要负责集群数据存储,一般有三台机器组成一个 raft 组,对外提供高可用的服务。

### 在容器里面运行

构建镜像
```
make image
```

编译代码
```
make build-dev
```

运行 demo 集群
```
make run-demo
```

运行读写测试
```
make run-test
```

停止集群
```
make stop-demo
```

### 项目构建

构建依赖
Expand Down
20 changes: 10 additions & 10 deletions cmd/metacli/metacli.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ func main() {
sigs := make(chan os.Signal, 1)

addrs := strings.Split(os.Args[1], ",")
meta_cli := metaserver.MakeMetaSvrClient(common.UN_UNSED_TID, addrs)
metaCli := metaserver.MakeMetaSvrClient(common.UN_UNSED_TID, addrs)

sig_chan := make(chan os.Signal, 1)
signal.Notify(sig_chan)
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan)

go func() {
sig := <-sigs
fmt.Println(sig)
for _, cli := range meta_cli.GetRpcClis() {
for _, cli := range metaCli.GetRpcClis() {
cli.CloseAllConn()
}
os.Exit(-1)
Expand All @@ -69,18 +69,18 @@ func main() {
gid, _ := strconv.Atoi(os.Args[3])
addr_map := make(map[int64]string)
addr_map[int64(gid)] = os.Args[4]
meta_cli.Join(addr_map)
metaCli.Join(addr_map)
}
case "leave":
{
gid, _ := strconv.Atoi(os.Args[3])
meta_cli.Leave([]int64{int64(gid)})
metaCli.Leave([]int64{int64(gid)})
}
case "query":
{
last_conf := meta_cli.Query(-1)
out_bytes, _ := json.Marshal(last_conf)
raftcore.PrintDebugLog("latest configuration: " + string(out_bytes))
lastConf := metaCli.Query(-1)
outBytes, _ := json.Marshal(lastConf)
raftcore.PrintDebugLog("latest configuration: " + string(outBytes))
}
case "move":
{
Expand All @@ -94,7 +94,7 @@ func main() {
gid, _ := strconv.Atoi(os.Args[4])

for i := start; i <= end; i++ {
meta_cli.Move(i, gid)
metaCli.Move(i, gid)
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions cmd/metasvr/metasvr.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,26 @@ func main() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

node_id_str := os.Args[1]
node_id, err := strconv.Atoi(node_id_str)
nodeIdStr := os.Args[1]
nodeID, err := strconv.Atoi(nodeIdStr)
if err != nil {
panic(err)
}
meta_svr_addrs := strings.Split(os.Args[2], ",")
cf_peer_map := make(map[int]string)
for i, addr := range meta_svr_addrs {
cf_peer_map[i] = addr
metaSvrAddrs := strings.Split(os.Args[2], ",")
cfPeerMap := make(map[int]string)
for i, addr := range metaSvrAddrs {
cfPeerMap[i] = addr
}

meta_svr := metaserver.MakeMetaServer(cf_peer_map, node_id)
lis, err := net.Listen("tcp", cf_peer_map[node_id])
metaSvr := metaserver.MakeMetaServer(cfPeerMap, nodeID)
lis, err := net.Listen("tcp", cfPeerMap[nodeID])
if err != nil {
fmt.Printf("failed to listen: %v", err)
return
}
s := grpc.NewServer()

pb.RegisterRaftServiceServer(s, meta_svr)
pb.RegisterRaftServiceServer(s, metaSvr)

sigChan := make(chan os.Signal, 1)

Expand All @@ -78,8 +78,8 @@ func main() {
go func() {
sig := <-sigs
fmt.Println(sig)
meta_svr.Rf.CloseEndsConn()
meta_svr.StopApply()
metaSvr.Rf.CloseEndsConn()
metaSvr.StopApply()
os.Exit(-1)
}()

Expand Down
32 changes: 16 additions & 16 deletions cmd/shardcli/shardcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ func main() {
}
sigs := make(chan os.Signal, 1)

shard_kvcli := shardkvserver.MakeKvClient(os.Args[1])
shardKvCli := shardkvserver.MakeKvClient(os.Args[1])

sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan)

switch os.Args[2] {
case "put":
if err := shard_kvcli.Put(os.Args[3], os.Args[4]); err != nil {
if err := shardKvCli.Put(os.Args[3], os.Args[4]); err != nil {
fmt.Println("err: " + err.Error())
return
}
case "get":
v, err := shard_kvcli.Get(os.Args[3])
v, err := shardKvCli.Get(os.Args[3])
if err != nil {
fmt.Println("err: " + err.Error())
return
Expand All @@ -69,40 +69,40 @@ func main() {
gid, _ := strconv.Atoi(os.Args[3])
bidsStr := os.Args[4]
bids := []int64{}
bids_strarr := strings.Split(bidsStr, ",")
for _, bidStr := range bids_strarr {
bidsStrArr := strings.Split(bidsStr, ",")
for _, bidStr := range bidsStrArr {
bid, _ := strconv.Atoi(bidStr)
bids = append(bids, int64(bid))
}
datas := shard_kvcli.GetBucketDatas(gid, bids)
datas := shardKvCli.GetBucketDatas(gid, bids)
fmt.Println("get buckets datas: " + datas)
case "delbuckets":
gid, _ := strconv.Atoi(os.Args[3])
bidsStr := os.Args[4]
bids := []int64{}
bids_strarr := strings.Split(bidsStr, ",")
for _, bidStr := range bids_strarr {
bidsStrArr := strings.Split(bidsStr, ",")
for _, bidStr := range bidsStrArr {
bid, _ := strconv.Atoi(bidStr)
bids = append(bids, int64(bid))
}
shard_kvcli.DeleteBucketDatas(gid, bids)
shardKvCli.DeleteBucketDatas(gid, bids)
case "insertbucketkv":
gid, _ := strconv.Atoi(os.Args[3])
bid, _ := strconv.Atoi(os.Args[4])
bucket_datas := &shardkvserver.BucketDatasVo{}
bucket_datas.Datas = make(map[int]map[string]string)
bucketDatas := &shardkvserver.BucketDatasVo{}
bucketDatas.Datas = make(map[int]map[string]string)
kv := map[string]string{os.Args[5]: os.Args[6]}
bucket_datas.Datas[bid] = kv
datas, _ := json.Marshal(bucket_datas)
shard_kvcli.InsertBucketDatas(gid, []int64{int64(bid)}, datas)
bucketDatas.Datas[bid] = kv
datas, _ := json.Marshal(bucketDatas)
shardKvCli.InsertBucketDatas(gid, []int64{int64(bid)}, datas)
}
go func() {
sig := <-sigs
fmt.Println(sig)
for _, cli := range shard_kvcli.GetCsClient().GetRpcClis() {
for _, cli := range shardKvCli.GetCsClient().GetRpcClis() {
cli.CloseAllConn()
}
shard_kvcli.GetRpcClient().CloseAllConn()
shardKvCli.GetRpcClient().CloseAllConn()
os.Exit(-1)
}()
}
30 changes: 14 additions & 16 deletions cmd/shardsvr/shardsvr.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (

"github.com/eraft-io/eraft/shardkvserver"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)

func main() {
Expand All @@ -49,33 +48,33 @@ func main() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

node_id_str := os.Args[1]
node_id, err := strconv.Atoi(node_id_str)
nodeIdStr := os.Args[1]
nodeID, err := strconv.Atoi(nodeIdStr)
if err != nil {
panic(err)
}

gid_str := os.Args[2]
gid, err := strconv.Atoi(gid_str)
gidStr := os.Args[2]
gid, err := strconv.Atoi(gidStr)
if err != nil {
panic(err)
}

svr_addrs := strings.Split(os.Args[4], ",")
svr_peer_map := make(map[int]string)
for i, addr := range svr_addrs {
svr_peer_map[i] = addr
svrAddrs := strings.Split(os.Args[4], ",")
svrPeerMap := make(map[int]string)
for i, addr := range svrAddrs {
svrPeerMap[i] = addr
}

shard_svr := shardkvserver.MakeShardKVServer(svr_peer_map, int64(node_id), gid, os.Args[3])
lis, err := net.Listen("tcp", svr_peer_map[node_id])
shardSvr := shardkvserver.MakeShardKVServer(svrPeerMap, int64(nodeID), gid, os.Args[3])
lis, err := net.Listen("tcp", svrPeerMap[nodeID])
if err != nil {
fmt.Printf("failed to listen: %v", err)
return
}
fmt.Printf("server listen on: %s \n", svr_peer_map[node_id])
fmt.Printf("server listen on: %s \n", svrPeerMap[nodeID])
s := grpc.NewServer()
pb.RegisterRaftServiceServer(s, shard_svr)
pb.RegisterRaftServiceServer(s, shardSvr)

sigChan := make(chan os.Signal, 1)

Expand All @@ -84,12 +83,11 @@ func main() {
go func() {
sig := <-sigs
fmt.Println(sig)
shard_svr.GetRf().CloseEndsConn()
shard_svr.CloseApply()
shardSvr.GetRf().CloseEndsConn()
shardSvr.CloseApply()
os.Exit(-1)
}()

reflection.Register(s)
err = s.Serve(lis)
if err != nil {
fmt.Printf("failed to serve: %v", err)
Expand Down
6 changes: 3 additions & 3 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ func Key2BucketID(key string) int {
}

func CRC32KeyHash(k string, base int) int {
bucket_id := 0
bucketID := 0
crc32q := crc32.MakeTable(0xD5828281)
sum := crc32.Checksum([]byte(k), crc32q)
bucket_id = int(sum) % NBuckets
return bucket_id
bucketID = int(sum) % NBuckets
return bucketID
}

func Int64ArrToIntArr(in []int64) []int {
Expand Down
Loading

0 comments on commit f75dd43

Please sign in to comment.