当前位置:网站首页>Etcd教程 — 第七章 Etcd之事务API
Etcd教程 — 第七章 Etcd之事务API
2022-06-29 03:10:00 【西木Qi】
Etcd教程 — 第七章 Etcd之事务API
一、Etcd 的事务 Transaction
事务可以使得 etcd 服务端在单个事物中自动处理多个外部请求。对于键值存储库的修改,这意味着该存储库的修订版仅对事务增加一次,并且该事务生成的所有事件都将具有相同的修订版。需要注意的是,禁止在单个事务中多次修改同一 key。
事务中的每个比较都会检查存储中的单个 key,类似于 If 操作,检查是否存在值,与给定值进行比较或检查键的修订或版本。两种不同的比较可能适用于相同或不同的 key。
所有比较都是原子操作。如果所有比较都为真,则表示事务成功,而 etcd 则应用事务的 then/success 请求块,否则,则认为失败并应用 else/failure 请求块。
二、Txn 的定义
Txn 方法在单个事务中处理多个请求。txn 请求增加键值存储的修订版本并为每个完成的请求生成带有相同修订版本的事件。Etcd 不容许在一个 txn 中多次修改同一个 key。
rpc Txn(TxnRequest) returns (TxnResponse) {
}
请求的消息体 TxnRequest 定义如下:
message TxnRequest {
// compare 是断言列表,体现为条件的联合
repeated Compare compare = 1;
// 成功请求列表,当比较评估为 true 时将被应用。
repeated RequestOp success = 2;
// 失败请求列表,当比较评估为 false 时将被应用。
repeated RequestOp failure = 3;
}
compare 如果比较成功,那么成功请求将被按顺序处理,而应答将按顺序包含他们对应的应答;
如果比较失败,那么失败请求将被按顺序处理,而应答将按顺序包含他们对应的应答。
应答的消息体 TxnResponse 定义如下:
message TxnResponse {
ResponseHeader header = 1;
// 如果比较评估为true则succeeded被设置为true,否则是false
bool succeeded = 2;
repeated ResponseOp responses = 3;
}
header 代表通用的响应头。responses 为应答列表,如果 succeeded 为 true 则对应成功请求,如果 succeeded 是 false 则对应失败请求。
Compare 消息体:
message Compare {
enum CompareResult {
EQUAL = 0;
GREATER = 1;
LESS = 2;
NOT_EQUAL = 3;
}
enum CompareTarget {
VERSION = 0;
CREATE = 1;
MOD = 2;
VALUE= 3;
}
CompareResult result = 1;
CompareTarget target = 2;
bytes key = 3;
oneof target_union {
// version 是给定 key 的版本
int64 version = 4;
// create_revision 是给定 key 的创建修订版本
int64 create_revision = 5;
// mod_revision 是给定 key 的最后修改修订版本
int64 mod_revision = 6;
// value 是给定 key 的值,以 bytes 的形式
bytes value = 7;
}
}
- result 是这个比较的逻辑比较操作。
- target 是比较要检查的键值字段。
- key 是用于比较操作的主题 key。在 .proto 中定义一个oneof 关键字后跟着oneof 名称,设置 oneof 字段将自动清除oneof字段的所有其他成员。在生成的代码中,oneof 字段具有与常规字段相同的 setter 和 getter 方法。
RequestOp 消息体定义如下:
message RequestOp {
oneof request {
RangeRequest request_range = 1;
PutRequest request_put = 2;
DeleteRangeRequest request_delete_range = 3;
}
}
request 是可以被事务接受的请求类型的联合。ResponseOp 消息体:
message ResponseOp {
oneof response {
RangeResponse response_range = 1;
PutResponse response_put = 2;
DeleteRangeResponse response_delete_range = 3;
}
}
response 是事务返回的应答类型的集合。
三、总结
本篇主要介绍了 Etcd API 中涉及的 事务 Txn 定义,Txn 方法在单个事务中处理多个请求,从而保证了业务执行的一致性。
边栏推荐
- Synchronous movement state of Jerry's watch [chapter]
- Pytoch Learning Series: Introduction
- [leetcode daily question] number of schemes to reconstruct a tree
- Nvisual helps integrators transform
- Delphi time to timestamp
- FPGA (VIII) RTL code IV (basic circuit design 1)
- Merge sort
- What is a thread pool?
- 18. ` BS object. Nom du noeud. Suivant Sibling ` get Brother Node
- Only in the past four years, Microsoft finally gave it up!
猜你喜欢

18. `bs对象.节点名.next_sibling` 获取兄弟节点

Leetcode counts the number of ways to place houses

If you dare to write MQ message queue middleware on your resume, you must win these questions!

Connect error: no route to host (errno:113)
![Movement state change of monitoring device of Jerry's watch [chapter]](/img/ff/cbc9e50a7d64e943f9f9924eadb184.jpg)
Movement state change of monitoring device of Jerry's watch [chapter]
![[linear algebra] 1.1 second and third order determinants](/img/ea/70b59c64d3287a887e371a9181fe45.png)
[linear algebra] 1.1 second and third order determinants

2022-2028 global pneumatic test probe industry survey and trend analysis report

How does kubernetes store business data persistently? (10)

Basic MySQL database operations

逆序对对数计算,顺序对对数计算——归并排序
随机推荐
兰宝传感科技冲刺科创板:年营收3.5亿 许永童家族色彩浓厚
双击事件与单击事件的那些事
线程池是什么老鸡?
FarrowTech的无线传感器采用橙群微电子的NanoBeacon蓝牙信标技术
sql连续登录问题
【云原生】这么火,你不来了解下?
Allegro's method of setting network flying line and network color
Redu.us took the initiative to transform, and the operation leader of China's charging pile emerged
[线性代数] 1.1 二阶与三阶行列式
【线程通信】
18. ` BS object. Nom du noeud. Suivant Sibling ` get Brother Node
FortiGate firewall configuration log uploading regularly
Leetcode daily question - 324 Swing sort II
PWN attack and defense world level2
Logarithmic calculation in reverse order, logarithmic calculation in sequence -- merge sort
Concise words tell about technical people who must master basic IT knowledge and skills. Part 1
Provide ideas in old texts
LinkedList学习
Synchronous real-time data of Jerry's watch [chapter]
The method of exporting packages of all components from existing PCBs in Altium Designer