参加Tinykv的一些总结
Talent Plan KV学习营
关于Talent Plan KV训练营
我想我有必要介绍一下,这是PingCAP
公司推出的一套开源分布式KV
存储实战课程,这课程一共包含了4
子项目:
Project 1
需要参与者独立完成一个单机的KV Server
Project 2
需要基于Raft
算法实现分布式键值数据库服务端Project 3
需要在Project 2
的基础上支持多个Raft
集群Project 4
需要Project 3
的基础上支持分布式事务
tinykv
所有子项目,产出一个分布式kv
数据库,相信这也是一个很大的收获,据我所知目前也就麻省理工学院
有一套MIT 6.824
课程,Ping Cap
推出这套tinykv
课程也是相当于弥补了国内这块课程的空白了。我当前参加的本次项目要求的是
Go
语言去实现这些Lab
,或许读者你看到这篇文章的时候可能换到其他语言上了比如说Rust
,当然至于用什么编程语言
去实现,都是不重要的问题,重要的是在论文
阅读部分,这就好比你看懂了一栋大楼的设计图纸,然后语言就是一个堆砖头的工具。- 论文地址:https://github.com/kvbase/raft-thesis-zh_cn
lab
的解题思路分享,晚上抽时间把第一个lab
完成了,本文这篇将写写Project 1
的怎么入坑的。Standalone KV Server 第一个
lab
是一个典型的TDD
开发的例子,什么是TDD
如果读者不了解自己去Google
吧。Project 1
是要基于BadgerDB
作为存储引擎的去实现出题者预留的API
接口的,然后官方在项目根目录建了一个makefile
文件,参与者只需要make project1
即可查看Project 1
完成情况。BadgerDB
是dgraph.io
开发的一款基于Log Structured Merge (LSM) Tree
的key-value
本地数据库, 使用Go
开发。

文章图片
开发这个项目你得需要有
Go mod
基础,没有自己去补吧。
文章图片
执行了
make project1
可以看到抛出了一大堆异常,这些异常原因就是官方工程师给你写的单元测试没有跑通过,你要做的只需要把/tinykv/kv/server/server_test.go
下的所有的单元测试用例调用的api
里面的功能实现即可。- 第一个你要实现的
package standalone_storageimport (
"github.com/pingcap-incubator/tinykv/kv/config"
"github.com/pingcap-incubator/tinykv/kv/storage"
"github.com/pingcap-incubator/tinykv/proto/pkg/kvrpcpb"
)// StandAloneStorage is an implementation of `Storage` for a single-node TinyKV instance. It does not
// communicate with other nodes and all data is stored locally.
type StandAloneStorage struct {
// Your Data Here (1).
}func NewStandAloneStorage(conf *config.Config) *StandAloneStorage {
// Your Code Here (1).
return nil
}func (s *StandAloneStorage) Start() error {
// Your Code Here (1).
return nil
}func (s *StandAloneStorage) Stop() error {
// Your Code Here (1).
return nil
}func (s *StandAloneStorage) Reader(ctx *kvrpcpb.Context) (storage.StorageReader, error) {
// Your Code Here (1).
return nil, nil
}func (s *StandAloneStorage) Write(ctx *kvrpcpb.Context, batch []storage.Modify) error {
// Your Code Here (1).
return nil
}
- 第二个你要实现的
package serverimport (
"context"
"github.com/pingcap-incubator/tinykv/proto/pkg/kvrpcpb"
)// The functions below are Server's Raw API. (implements TinyKvServer).
// Some helper methods can be found in sever.go in the current directory// RawGet return the corresponding Get response based on RawGetRequest's CF and Key fields
func (server *Server) RawGet(_ context.Context, req *kvrpcpb.RawGetRequest) (*kvrpcpb.RawGetResponse, error) {
// Your Code Here (1).
return nil, nil
}// RawPut puts the target data into storage and returns the corresponding response
func (server *Server) RawPut(_ context.Context, req *kvrpcpb.RawPutRequest) (*kvrpcpb.RawPutResponse, error) {
// Your Code Here (1).
// Hint: Consider using Storage.Modify to store data to be modified
return nil, nil
}// RawDelete delete the target data from storage and returns the corresponding response
func (server *Server) RawDelete(_ context.Context, req *kvrpcpb.RawDeleteRequest) (*kvrpcpb.RawDeleteResponse, error) {
// Your Code Here (1).
// Hint: Consider using Storage.Modify to store data to be deleted
return nil, nil
}// RawScan scan the data starting from the start key up to limit. and return the corresponding result
func (server *Server) RawScan(_ context.Context, req *kvrpcpb.RawScanRequest) (*kvrpcpb.RawScanResponse, error) {
// Your Code Here (1).
// Hint: Consider using reader.IterCF
return nil, nil
}
对应的单元测试文件是
tinykv/kv/server/server_test.go
,测试用例官方工程师已经写好了,你只需要把底层代码实现,跑通单元测试即可完成Project 1
,我这里提示一下Project 1
可以看成一个适配器模式,只是进行了一层封装,写实现的或者测试单元测试的时候可以一个一个测试来测试,先把单元测试全部注释掉,然后实现一个解注释一个,跑一个,good luck!
。
文章图片
推荐阅读
- 热闹中的孤独
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 放屁有这三个特征的,请注意啦!这说明你的身体毒素太多
- 一个人的旅行,三亚
- 布丽吉特,人生绝对的赢家
- 慢慢的美丽
- 尽力
- 一个小故事,我的思考。
- 家乡的那条小河
- 《真与假的困惑》???|《真与假的困惑》??? ——致良知是一种伟大的力量