Talent Plan KV学习营
关于Talent Plan KV训练营我想我有必要介绍一下,这是PingCAP公司推出的一套开源分布式KV存储实战课程,这课程一共包含了4子项目:
Project 1需要参与者独立完成一个单机的KV ServerProject 2需要基于Raft算法实现分布式键值数据库服务端Project 3需要在Project 2的基础上支持多个Raft集群Project 4需要Project 3的基础上支持分布式事务
难度都是阶梯式的,如果能实现tinykv 所有子项目,产出一个分布式kv数据库,相信这也是一个很大的收获,据我所知目前也就麻省理工学院有一套MIT 6.824课程,Ping Cap推出这套tinykv课程也是相当于弥补了国内这块课程的空白了。
我当前参加的本次项目要求的是Go语言去实现这些Lab,或许读者你看到这篇文章的时候可能换到其他语言上了比如说Rust,当然至于用什么编程语言去实现,都是不重要的问题,重要的是在论文阅读部分,这就好比你看懂了一栋大楼的设计图纸,然后语言就是一个堆砖头🧱的工具。
由于举办方要求参赛者不得以任何方式直接贴每个项目的答案代码,当然我认为这么做是对的,但是举办方鼓励参赛者写一些关于实现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_storage
import (
"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 server
import (
"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!。