关于golang:参加Tinykv的一些总结

9次阅读

共计 3232 个字符,预计需要花费 9 分钟才能阅读完成。

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_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
}
  1. 第二个你要实现的
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!

正文完
 0