Talent Plan KV学习营

对于Talent Plan KV训练营我想我有必要介绍一下,这是PingCAP公司推出的一套开源分布式KV存储实战课程,这课程一共蕴含了4子项目:

  1. Project 1须要参与者独立实现一个单机的KV Server
  2. Project 2须要基于Raft算法实现分布式键值数据库服务端
  3. Project 3须要在Project 2的根底上反对多个Raft集群
  4. 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实现状况。

BadgerDBdgraph.io开发的一款基于 Log Structured Merge (LSM) Treekey-value 本地数据库, 应用Go 开发。

开发这个我的项目你得须要有Go mod根底,没有本人去补吧。

执行了make project1能够看到抛出了一大堆异样,这些异样起因就是官网工程师给你写的单元测试没有跑通过,你要做的只须要把/tinykv/kv/server/server_test.go下的所有的单元测试用例调用的api外面的性能实现即可。

  1. 第一个你要实现的
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}
  1. 第二个你要实现的
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 fieldsfunc (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 responsefunc (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 responsefunc (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 resultfunc (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!