Skip to content

Commit 99cd750

Browse files
author
Junlong Gao
committed
Adding client example
Added a basic raw kv interface demo
1 parent 1314dd5 commit 99cd750

File tree

12 files changed

+167
-54
lines changed

12 files changed

+167
-54
lines changed

Dockerfile

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,11 @@ COPY raft /src/raft
1313
COPY go.mod /src/go.mod
1414
COPY go.sum /src/go.sum
1515
COPY Makefile /src/Makefile
16-
COPY test.sh /src/test.sh
1716
COPY scheduler /src/scheduler
1817

1918
WORKDIR /src
20-
RUN ls && make proto && go mod download
21-
22-
RUN make kv
19+
RUN ls && make proto && go mod download && go test -c ./kv/test_raftstore && go test -c ./raft
2320

2421
FROM ubuntu:latest
2522
WORKDIR /root/
26-
COPY --from=builder /src/bin/tinykv-server .
27-
ENTRYPOINT ["/root/tinykv-server"]
23+
COPY --from=builder /src/ .

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ PACKAGES := $$($(PACKAGE_LIST))
1919
# Targets
2020
.PHONY: clean test proto kv dev
2121

22-
default: kv
22+
default: kv example
2323

2424
dev: default test
2525

26+
example:
27+
$(GOBUILD) -o bin/tinykv-client client/client.go
28+
2629
test:
2730
@echo "Running tests in native mode."
2831
@export TZ='Asia/Shanghai'; \

README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
| Project 3a| Done |
99
| Project 3b| Done |
1010
| Project 3c| De-scoped, using official placement driver v3.0.13 |
11+
| Project 4a| In progress |
1112

1213
# The TinyKV Course
1314

@@ -76,31 +77,29 @@ After you finished the whole implementation, it's runnable now. You can try Tiny
7677
### Build
7778

7879
```
79-
make
80+
make kv
8081
```
8182

82-
It builds the binary of `tinykv-server` and `tinyscheduler-server` to `bin` dir.
83+
It builds the binary of `tinykv-server` to `bin` dir.
8384

8485
### Run
85-
86-
Put the binary of `tinyscheduler-server`, `tinykv-server` and `tinysql-server` into a single dir.
87-
88-
Under the binary dir, run the following commands:
89-
90-
```
91-
mkdir -p data
92-
```
93-
86+
On your laptop with local docker:
9487
```
95-
./tinyscheduler-server
88+
$ ./start-pd.sh
9689
```
90+
To start the scheduler.
9791

92+
Start 3 store servers (since by default replication factor is 3):
9893
```
99-
./tinykv-server -path=data
94+
$ ./start-server.sh 20601
95+
$ ./start-server.sh 20602
96+
$ ./start-server.sh 20603
10097
```
10198

99+
After the placement driver does the magic, hack the client dir:
102100
```
103-
./tinysql-server --store=tikv --path="127.0.0.1:2379"
101+
$ make client
102+
$ ./bin/tinykv-client
104103
```
105104

106105
### Play

client/client.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/pingcap-incubator/tinykv/log"
7+
"github.com/pingcap-incubator/tinykv/proto/pkg/kvrpcpb"
8+
"github.com/pingcap-incubator/tinykv/proto/pkg/metapb"
9+
10+
"google.golang.org/grpc"
11+
12+
sched "github.com/pingcap-incubator/tinykv/proto/pkg/pdpb"
13+
kv "github.com/pingcap-incubator/tinykv/proto/pkg/tinykvpb"
14+
)
15+
16+
type ctx struct {
17+
conn kv.TinyKvClient
18+
h *kvrpcpb.Context
19+
}
20+
21+
var pdconn *grpc.ClientConn
22+
var tikvconn *grpc.ClientConn
23+
24+
func locate(key []byte) (*grpc.ClientConn, ctx) {
25+
if pdconn == nil {
26+
pd, err := grpc.Dial("127.0.0.1:32379", grpc.WithInsecure(), grpc.WithBlock())
27+
if err != nil {
28+
panic(err)
29+
}
30+
pdconn = pd
31+
}
32+
33+
client := sched.NewPDClient(pdconn)
34+
var clusterID uint64
35+
{
36+
resp, err := client.GetMembers(context.TODO(), &sched.GetMembersRequest{})
37+
38+
if err != nil {
39+
panic(err)
40+
}
41+
42+
clusterID = resp.Header.ClusterId
43+
}
44+
45+
var region *metapb.Region
46+
var leader *metapb.Peer
47+
{
48+
resp, err := client.GetRegion(context.TODO(), &sched.GetRegionRequest{
49+
Header: &sched.RequestHeader{ClusterId: clusterID},
50+
RegionKey: key,
51+
})
52+
53+
if err != nil {
54+
panic(err)
55+
}
56+
57+
region = resp.Region
58+
leader = resp.Leader
59+
}
60+
61+
var store *metapb.Store
62+
{
63+
resp, err := client.GetStore(context.TODO(), &sched.GetStoreRequest{
64+
Header: &sched.RequestHeader{ClusterId: clusterID},
65+
StoreId: leader.StoreId,
66+
})
67+
if err != nil {
68+
panic(err)
69+
}
70+
store = resp.Store
71+
}
72+
73+
// XXX cache region and close connection in case leader changes
74+
if tikvconn == nil {
75+
conn, err := grpc.Dial(store.Address, grpc.WithInsecure(), grpc.WithBlock())
76+
if err != nil {
77+
panic(err)
78+
}
79+
tikvconn = conn
80+
}
81+
82+
return tikvconn, ctx{
83+
conn: kv.NewTinyKvClient(tikvconn),
84+
h: &kvrpcpb.Context{
85+
RegionId: region.Id,
86+
RegionEpoch: region.RegionEpoch,
87+
Peer: leader,
88+
},
89+
}
90+
}
91+
92+
func Put(key, val []byte) {
93+
_, ctx := locate(key)
94+
_, err := ctx.conn.RawPut(context.TODO(), &kvrpcpb.RawPutRequest{
95+
Context: ctx.h,
96+
Key: key,
97+
Value: val,
98+
Cf: "default",
99+
})
100+
101+
if err != nil {
102+
panic(err)
103+
}
104+
105+
if err != nil {
106+
panic(err)
107+
}
108+
}
109+
110+
func Get(key []byte) []byte {
111+
_, ctx := locate(key)
112+
resp, err := ctx.conn.RawGet(context.TODO(), &kvrpcpb.RawGetRequest{
113+
Context: ctx.h,
114+
Key: key,
115+
Cf: "default",
116+
})
117+
118+
if err != nil {
119+
panic(err)
120+
}
121+
122+
if err != nil {
123+
panic(err)
124+
}
125+
return resp.Value
126+
}
127+
128+
func main() {
129+
for i := 0; i < 128; i++ {
130+
key := []byte("hello " + fmt.Sprintf("%4d", i))
131+
Put(key, []byte("world "+fmt.Sprintf("%4d", i)))
132+
133+
log.Infof("Get val %v", Get(key))
134+
}
135+
}

kv/config/config.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ func (c *Config) Validate() error {
5252
return fmt.Errorf("election tick must be greater than heartbeat tick.")
5353
}
5454

55+
if c.SchedulerHeartbeatTickInterval <= c.RaftBaseTickInterval {
56+
return fmt.Errorf("schduler heartbeat tick must be larger than raft base tick")
57+
}
58+
//... and others too
59+
5560
return nil
5661
}
5762

@@ -73,7 +78,7 @@ func NewDefaultConfig() *Config {
7378
// Assume the average size of entries is 1k.
7479
RaftLogGcCountLimit: 128000,
7580
SplitRegionCheckTickInterval: 10 * time.Second,
76-
SchedulerHeartbeatTickInterval: 100 * time.Millisecond,
81+
SchedulerHeartbeatTickInterval: 2 * time.Second,
7782
SchedulerStoreHeartbeatTickInterval: 10 * time.Second,
7883
RegionMaxSize: 144 * MB,
7984
RegionSplitSize: 96 * MB,

kv/raftstore/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
"github.com/pingcap-incubator/tinykv/kv/util/engine_util"
1414
"github.com/pingcap-incubator/tinykv/log"
1515
"github.com/pingcap-incubator/tinykv/proto/pkg/metapb"
16-
"github.com/pingcap-incubator/tinykv/proto/pkg/raft_serverpb"
1716
schedulerpb "github.com/pingcap-incubator/tinykv/proto/pkg/pdpb"
17+
"github.com/pingcap-incubator/tinykv/proto/pkg/raft_serverpb"
1818
"github.com/pingcap/errors"
1919
)
2020

kv/raftstore/runner/scheduler_task.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"github.com/pingcap-incubator/tinykv/kv/util/worker"
1010
"github.com/pingcap-incubator/tinykv/log"
1111
"github.com/pingcap-incubator/tinykv/proto/pkg/metapb"
12-
"github.com/pingcap-incubator/tinykv/proto/pkg/raft_cmdpb"
1312
schedulerpb "github.com/pingcap-incubator/tinykv/proto/pkg/pdpb"
13+
"github.com/pingcap-incubator/tinykv/proto/pkg/raft_cmdpb"
1414
"github.com/shirou/gopsutil/disk"
1515
)
1616

kv/raftstore/scheduler_client/client.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ func (c *client) updateLeader() (*schedulerpb.GetMembersResponse, error) {
162162
for _, u := range c.urls {
163163
ctx, cancel := context.WithTimeout(c.ctx, schedulerTimeout)
164164
members, err := c.getMembers(ctx, u)
165-
log.Infof("Got %v %v", members, err)
166165
cancel()
167166
if err != nil || members.GetLeader() == nil || len(members.GetLeader().GetClientUrls()) == 0 {
168167
select {

kv/raftstore/store_worker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
"github.com/pingcap-incubator/tinykv/kv/util/engine_util"
1414
"github.com/pingcap-incubator/tinykv/log"
1515
"github.com/pingcap-incubator/tinykv/proto/pkg/metapb"
16-
rspb "github.com/pingcap-incubator/tinykv/proto/pkg/raft_serverpb"
1716
schedulerpb "github.com/pingcap-incubator/tinykv/proto/pkg/pdpb"
17+
rspb "github.com/pingcap-incubator/tinykv/proto/pkg/raft_serverpb"
1818
"github.com/pingcap/errors"
1919
)
2020

start-pd.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
set -o pipefail
44

5-
IP=127.0.0.1
6-
docker run --network=host pingcap/pd:v3.0.13 \
5+
IP=0.0.0.0
6+
docker run -p 32379:32379 -p 32380:32380 pingcap/pd:v3.0.13 \
77
-L debug \
88
-initial-cluster pd=http://$IP:32380 \
99
-peer-urls http://$IP:32380 \

0 commit comments

Comments
 (0)