当前位置:网站首页>Etcd database source code analysis -- ServerV2 interface implementation
Etcd database source code analysis -- ServerV2 interface implementation
2022-07-20 07:52:00 【Tertium FATUM】
We know EtcdServer Realized Server Interface 、ServerV2 Interface and ServerV3 Interface , Let's first introduce ServerV2 Interface (ServerV2 Interfaces are used to operate v2 store Of API). As shown in the figure above ServerV2 As shown in the code , It provides Leader()、Do() and ClientCertAuthEnabled() Function definition .EtcdServer The structure needs to realize ServerV2 Interface , To provide operation v2 store The ability of .
Do function
Here is Do Implementation of function , use first reqIDGen Generate a request id, Set to pb.Request Of ID Among members ; Then initialize reqV2HandlerEtcdServer Structure ( The structure in the right part of the above figure ); And then call Request(RequestV2 Is defined as Request) Provided Handle function ; Finally, the current tenure of the node and commit raft log pos Set in response .
func (s *EtcdServer) Do(ctx context.Context, r pb.Request) (Response, error) {
r.ID = s.reqIDGen.Next()
h := &reqV2HandlerEtcdServer{
reqV2HandlerStore: reqV2HandlerStore{
store: s.v2store, applier: s.applyV2, }, s: s,
}
rp := &r
resp, err := ((*RequestV2)(rp)).Handle(ctx, h)
resp.Term, resp.Index = s.Term(), s.CommittedIndex()
return resp, err
}
func (r *RequestV2) Handle(ctx context.Context, v2api RequestV2Handler) (Response, error) {
if r.Method == "GET" && r.Quorum {
r.Method = "QGET" }
switch r.Method {
case "POST": return v2api.Post(ctx, r)
case "PUT": return v2api.Put(ctx, r)
case "DELETE": return v2api.Delete(ctx, r)
case "QGET": return v2api.QGet(ctx, r)
case "GET": return v2api.Get(ctx, r)
case "HEAD": return v2api.Head(ctx, r)
}
return Response{
}, errors.ErrUnknownMethod
}
Handle function Yes Request To analyze , according to Request.Method And other fields to perform corresponding operations . If Request.Method yes “POST”、“PUT”、“DELETE” or Quorum by true Of “QGET”, be Request Will be sent by consensus before performing its corresponding operation ,Do Function will block , Until the operation is performed or an error occurs .
reqV2HandlerEtcdServer
reqV2HandlerEtcdServer The structure contains reqV2HandlerStore and EtcdServer Two members ,reqV2HandlerStore Contains v2store.Store and ApplierV2 Two parts , From the above Do The function can see , These two members are EtcdServer Of v2store and applyv2, That is, follow-up reqV2HandlerStore Interface function implemented by structure , That's right EtcdServer Of v2store and applyv2 The operation of these two members .
type reqV2HandlerEtcdServer struct {
reqV2HandlerStore
s *EtcdServer
}
reqV2HandlerEtcdServer The structure realizes RequestV2Handler All interface functions of the interface . As can be seen below these Post、Put、Delete and QGet Methods are called processRaftRequest Realized , stay processRaftRequest Method will give the request to etcd-raft Module processing , And block waiting for the request processing to end .
type RequestV2Handler interface {
Post(ctx context.Context, r *RequestV2) (Response, error)
Put(ctx context.Context, r *RequestV2) (Response, error)
Delete(ctx context.Context, r *RequestV2) (Response, error)
QGet(ctx context.Context, r *RequestV2) (Response, error)
Get(ctx context.Context, r *RequestV2) (Response, error)
Head(ctx context.Context, r *RequestV2) (Response, error)
}
func (a *reqV2HandlerEtcdServer) Post(ctx context.Context, r *RequestV2) (Response, error) {
return a.processRaftRequest(ctx, r) }
func (a *reqV2HandlerEtcdServer) Put(ctx context.Context, r *RequestV2) (Response, error) {
return a.processRaftRequest(ctx, r) }
func (a *reqV2HandlerEtcdServer) Delete(ctx context.Context, r *RequestV2) (Response, error) {
return a.processRaftRequest(ctx, r) }
func (a *reqV2HandlerEtcdServer) QGet(ctx context.Context, r *RequestV2) (Response, error) {
return a.processRaftRequest(ctx, r) }
func (a *reqV2HandlerEtcdServer) processRaftRequest(ctx context.Context, r *RequestV2) (Response, error) {
data, err := ((*pb.Request)(r)).Marshal()
if err != nil {
return Response{
}, err }
ch := a.s.w.Register(r.ID)
start := time.Now()
a.s.r.Propose(ctx, data)
select {
case x := <-ch:
resp := x.(Response)
return resp, resp.Err
case <-ctx.Done():
proposalsFailed.Inc()
a.s.w.Trigger(r.ID, nil) // GC wait
return Response{
}, a.s.parseProposeCtxErr(ctx.Err(), start)
case <-a.s.stopping:
}
return Response{
}, errors.ErrStopped
}
reqV2HandlerStore
type reqV2HandlerStore struct {
store v2store.Store
applier ApplierV2
}
reqV2HandlerStore The structure realizes RequestV2Handler All interface functions of the interface . from reqV2HandlerStore The implementation of these functions can be seen Post、Put、Delete、QGet In fact, it's all called ApplierV2 The corresponding function of ;Get and Head It's called v2store.Store Corresponding interface function ,Do Function for Request.Method by Get and Head The operation of is to call the following Get and Head function , perform v2store.Store The corresponding interface function obtains data .
func (a *reqV2HandlerStore) Post(ctx context.Context, r *RequestV2) (Response, error) {
return a.applier.Post(r), nil }
func (a *reqV2HandlerStore) Put(ctx context.Context, r *RequestV2) (Response, error) {
return a.applier.Put(r, membership.ApplyBoth), nil }
func (a *reqV2HandlerStore) Delete(ctx context.Context, r *RequestV2) (Response, error) {
return a.applier.Delete(r), nil }
func (a *reqV2HandlerStore) QGet(ctx context.Context, r *RequestV2) (Response, error) {
return a.applier.QGet(r), nil }
func (a *reqV2HandlerStore) Get(ctx context.Context, r *RequestV2) (Response, error) {
if r.Wait {
wc, err := a.store.Watch(r.Path, r.Recursive, r.Stream, r.Since)
return Response{
Watcher: wc}, err
}
ev, err := a.store.Get(r.Path, r.Recursive, r.Sorted)
return Response{
Event: ev}, err
}
func (a *reqV2HandlerStore) Head(ctx context.Context, r *RequestV2) (Response, error) {
ev, err := a.store.Get(r.Path, r.Recursive, r.Sorted)
return Response{
Event: ev}, err
}
Get rid of v2sotre, It needs to be deleted ServerV2 Of Do Function implementation and updateClusterVersionV2 function , Delete server/etcdserver/apply.v2.go and server/etcdserver/v2.server.go file .
边栏推荐
- Flask框架——模型关系(多对多)
- 基于51单片机驱动HC-SR04超声波模块(LCD1602显示)
- Proxy mode (proxy)
- TensorFlow 2 详解(tf生态系统、安装、内务管理、基本操作)
- Rock paper will develop ar game for San Diego priest baseball team
- 不知道 MySQL 咋学?刷完牛客这 50 道题就够了!(第二篇)
- Unityvr -- robot scene 2- robot
- 【MySQL】索引& 事务
- ES6中 Symbol 的基础学习,迭代器和生成器的基本用法
- 200元一把的电吹风,只用了一个星期,就做到200万?
猜你喜欢
YoloV7——配置YOLOV7(一)
AR美妆平台YouCam支持男性胡须实时预览
Network security module B of national vocational college skills competition Wireshark data analysis and forensics information pacapng
What should the embedded R & D industry do about source code confidentiality
Openstack Stein deployment
Brain magnetic signal processing matlab
社交电商模式前期应该怎么选?泰山众筹
岩土工程渗流问题之有限单元法:理论、模块化编程实现、开源程序手把手实操应用
Flask框架——模型关系(多对多)
Custom types - Enumeration and union
随机推荐
【gclient sync】webrtc dial tcp 173.252.88.133:443: connectex: A connection attempt failed
基于hough的图像分割-matlab
How to distinguish solid state disk and mechanical disk
Applet container technology is added to the mixed app development queue, and the cross end development efficiency is improved by 100%
Meta CTO:Cambria首发配手柄,新品保持12-24个月迭代
Programmer social death moment 3.0
2022g2 boiler stoker judgment questions and answers
TensorFlow 2 详解(tf生态系统、安装、内务管理、基本操作)
ES6中 Symbol 的基础学习,迭代器和生成器的基本用法
YoloV7——配置YOLOV7(一)
学UI设计,可以向哪些方向发展?该怎么学?
UnityVR-机械臂场景3-手爪
Namespace and module of TS
【电脑重置后安装软件】可搜索电脑所有文件的软件,世界上最好的截图软件,免费音乐播放器,jdk的安装,MySQL安装,安装破解 IDEA,配置IDEA,让IDEA更好用
代理模式(Proxy )
[transfer] pyhton__ pycache__ Generation and function of folders
10页PPT,看懂 SaaS 客户生命周期
ETCD数据库源码分析——ServerV2接口实现
Meta CTO: Cambria will be equipped with handle for the first time, and the new product will last for 12-24 months
岩土工程渗流问题之有限单元法:理论、模块化编程实现、开源程序手把手实操应用