Skip to content

Commit 57997ca

Browse files
author
Junlong Gao
committed
Working on a solution
1 parent 0a3cb4d commit 57997ca

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2539
-38535
lines changed

.dockerignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
proto/_tools
2+
bin/*
3+
doc/*
4+
out

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
node/node
44
unikv/unikv
55
bin/*
6-
_tools
6+
_tools
7+
proto/pkg

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM golang:1.13.8
2+
3+
COPY . /src
4+
ENTRYPOINT ["/src/entrypoint.sh"]

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ endif
99

1010
GO := GO111MODULE=on go
1111
GOBUILD := $(GO) build $(BUILD_FLAG) -tags codes
12-
GOTEST := $(GO) test -v --count=1 --parallel=1 -p=1
12+
GOTEST := $(GO) test -v --count=1 --parallel=1 -p=1 -failfast -timeout 600m
1313

1414
TEST_LDFLAGS := ""
1515

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
# Progress
2+
|Module|Status|
3+
|-|-|
4+
| Project 1 | Done |
5+
| Project 2a| Done |
6+
| Project 2b| Done |
7+
| Project 2c| Done |
8+
| Project 3a| Done |
9+
| Project 3b| Done |
10+
| Project 3c| In Progress |
11+
112
# The TinyKV Course
213

314
This is a series of projects on a key-value storage system built with the Raft consensus algorithm. These projects are inspired by the famous [MIT 6.824](http://nil.csail.mit.edu/6.824/2018/index.html) course, but aim to be closer to industry implementations. The whole course is pruned from [TiKV](https://github.com/tikv/tikv) and re-written in Go. After completing this course, you will have the knowledge to implement a horizontal scalable, high available, key-value storage service with distributed transaction support and a better understanding of TiKV implementation.

doc/project2-RaftKV.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ The format is as below and some helper functions are provided in `kv/raftstore/m
122122

123123
> You may wonder why TinyKV needs two badger instances. Actually, it can use only one badger to store both raft log and state machine data. Separating into two instances is just to be consistent with TiKV design.
124124
125-
These metadatas should be created and updated in `PeerStorage`. When creating PeerStorage, see `kv/raftstore/peer_storager.go`. It initializes RaftLocalState, RaftApplyState of this Peer or gets the previous value from the underlying engine in the case of restart. Note that the value of both RAFT_INIT_LOG_TERM and RAFT_INIT_LOG_INDEX is 5 (as long as it's larger than 1) but not 0. The reason why not set it to 0 is to distinguish with the case that peer created passively after conf change. You may not quite understand it now, so just keep it in mind and the detail will be described in project3b when you are implementing conf change.
125+
The metadata should be created and updated in `PeerStorage`. When creating PeerStorage, see `kv/raftstore/peer_storage.go`. It initializes RaftLocalState, RaftApplyState of this Peer or gets the previous value from the underlying engine in the case of restart. Note that the value of both RAFT_INIT_LOG_TERM and RAFT_INIT_LOG_INDEX is 5 (as long as it's larger than 1) but not 0. The reason why not set it to 0 is to distinguish with the case that peer created passively after conf change. You may not quite understand it now, so just keep it in mind and the detail will be described in project3b when you are implementing conf change.
126126

127127
The code you need to implement in this part is only one function: `PeerStorage.SaveReadyState`, what this function does is to save the data in `raft.Ready` to badger, including append log entries and save the Raft hard state.
128128

entrypoint.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash -ex
2+
set -o pipefail
3+
4+
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
5+
ulimit -n 65536
6+
elif [[ "$OSTYPE" == "darwin"* ]]; then
7+
sudo launchctl limit maxfiles 65536 200000
8+
fi
9+
10+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
11+
cd $DIR
12+
iter=0
13+
rm -rf *-LOCK
14+
touch $iter-LOCK
15+
export GO111MODULE=on
16+
go mod tidy
17+
make proto
18+
19+
function run_test_raftstore() {
20+
go test ./kv/test_raftstore -count=1 --parallel=1 -p=1 -v -failfast -run "$1"
21+
}
22+
23+
while true; do
24+
rm -rf /tmp/test-raftstore* /tmp/go-build*
25+
mv $iter-LOCK $((iter+1))-LOCK
26+
iter=$((iter+1))
27+
echo "iteration $iter"
28+
29+
make project3b
30+
make project3a
31+
make project2
32+
make project1
33+
done

kv/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func NewDefaultConfig() *Config {
8484
func NewTestConfig() *Config {
8585
return &Config{
8686
Raft: true,
87+
LogLevel: "info",
8788
RaftBaseTickInterval: 50 * time.Millisecond,
8889
RaftHeartbeatTicks: 2,
8990
RaftElectionTimeoutTicks: 10,

kv/raftstore/peer.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ type peer struct {
8383

8484
// Record the callback of the proposals
8585
// (Used in 2B)
86-
proposals []*proposal
86+
proposals map[uint64]proposal
8787

8888
// Index of last scheduled compacted raft log.
8989
// (Used in 2C)
@@ -133,6 +133,10 @@ func NewPeer(storeId uint64, cfg *config.Config, engines *engine_util.Engines, r
133133
Storage: ps,
134134
}
135135

136+
for _, p := range region.GetPeers() {
137+
raftCfg.Peers = append(raftCfg.Peers, p.Id)
138+
}
139+
136140
raftGroup, err := raft.NewRawNode(raftCfg)
137141
if err != nil {
138142
return nil, err
@@ -146,6 +150,7 @@ func NewPeer(storeId uint64, cfg *config.Config, engines *engine_util.Engines, r
146150
PeersStartPendingTime: make(map[uint64]time.Time),
147151
Tag: tag,
148152
ticker: newTicker(region.GetId(), cfg),
153+
proposals: map[uint64]proposal{},
149154
}
150155

151156
// If this region has only one peer and I am the one, campaign directly.
@@ -353,6 +358,7 @@ func (p *peer) HeartbeatScheduler(ch chan<- worker.Task) {
353358
Peer: p.Meta,
354359
PendingPeers: p.CollectPendingPeers(),
355360
ApproximateSize: p.ApproximateSize,
361+
Term: p.RaftGroup.Raft.Term,
356362
}
357363
}
358364

0 commit comments

Comments
 (0)