gRPC communication interface for etcd

Get into the habit of writing together! This is the 9th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

gRPC communication interface for etcd

Client Definition

type Client struct{
	Cluster  // 向集群里增加etcd服务端节点之类,属于管理员操作
	KV // 我们主要使用的功能,即操作K-V
	Lease // 租约相关操作,比如申请一个TTL=10s的租约
	Auth // 管理ETCD的用户和权限 属于管理员操作
	Watcher // 观察订阅,从而监听最新的数据变化
	Maintenance // 维护etcd,比如主动迁移etcd的leader节点,管理员操作
	UserName string // 认证的用户名
	Password string // 认证的密码
}
复制代码

etcd's core API

  • KV Service: create, update get and delete key-value pairs
  • Watch Service : Watch key changes
  • Lease Service : Implement key-value pair expired client to renew lease to keep heartbeat
  • Lock Service: etcd provides distributed shared lock support
  • Election Service: Expose client election mechanism

Overview of the read and write process

read request

The client selects an etcd node through load balancing and sends a read request. The API interface layer provides the Range RPC method. The etcd server intercepts the gRPC read request and calls the corresponding processor to process the request.

  1. etcdctl will create a clientv3 library object and select a suitable etcd node
  2. Call the Range RPC method of the KV Server module to send the request
  3. Interceptor interception, mainly to do some verification and monitoring
  4. Call the Range interface of the KV Server module to obtain data

Linear read: Linear read is a concept relative to serial read. There will be multiple etcd nodes in cluster mode, and different nodes may have consistency problems. Serial read directly returns status data without interacting with other nodes in the cluster. This method is fast and has low overhead, but there will be data inconsistencies. Linear read requires consensus among cluster members, which has overhead and relatively full response speed, but ensures data consistency. The default read mode of etcd is linear read.

The query request in etcd, querying a single key or a group of keys and the number of queries, will actually call the Range keys method at the bottom layer.insert image description here

  1. Quickly query the corresponding index item keyIndex of the key according to BTree in treeIndex, and the index item contains Revision
  2. According to the queried version number information Revision, use binary search in Backend's cache Buffer, if it hits, it will return directly
  3. If the cache does not meet the conditions, search in BlotDB (based on the index of BlotDB), and return the key-value pair information after the query

write request

The client selects an etcd node to initiate a write request through load balancing. The etcd server intercepts the gRPC write request . After the checksum monitoring is involved, the KV Server sends a proposal to the raft module. The content is the write data command, which is forwarded through the network. After reaching an agreement, after persisting the data, the state becomes the content of the MVCC module execution proposal.

  1. The client selects an etcd node through the load balancing algorithm and initiates a gRPC call
  2. etcd server receives client request
  3. After gRPC interception and Quota verification, the Quota module is used to verify whether the etcd db file size exceeds the quota.
  4. The KV Server module sends the request to the raft in this module, which is responsible for communicating with the etcd raft module to initiate a proposal. The command is put, foo, bar, that is, the put method is used to update foo to bar
  5. After the proposal was forwarded, half of the nodes successfully persisted
  6. MVCC module update state machine

insert image description here

Guess you like

Origin juejin.im/post/7085155080701214734