当前位置:网站首页>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 方法在单个事务中处理多个请求,从而保证了业务执行的一致性。
边栏推荐
- [Algèbre linéaire] 1.1 déterminant du deuxième et du troisième ordre
- Matlab exercises - image drawing exercises
- PHP SimpleXML
- [yunyuanyuan] it's so hot. Why don't you come and understand it?
- How to add the live video function to the website built by your own live video software (build a live video website)
- Double click events and click events
- 蓝桥杯2022初赛——扫雷
- FortiGate firewall configuration log uploading regularly
- 1110: 最近共同祖先(函数专题)
- PAT甲级 A1057 Stack
猜你喜欢

Counter analysis of the parameter anti content in the backstage of the # yyds dry goods inventory # knife fight shop

SQL training 01
![The continued movement of Jerry's watch [chapter]](/img/3e/f8b98997320580431a8e7117f4a506.jpg)
The continued movement of Jerry's watch [chapter]

2022-2028 global CAE engineering service industry research and trend analysis report

How to optimize databases and tables
![[Shangshui Shuo series] the simplest subtitle configuration](/img/22/7e0bcb489d0f2d35c7fe3248ad3419.png)
[Shangshui Shuo series] the simplest subtitle configuration

嵌入式开发如何进行源代码保密工作

2022-2028 global secondary butyl lithium industry research and trend analysis report

2022-2028 global long wave infrared camera and camera core industry research and trend analysis report

Yyds dry inventory everything a primary developer should know about activity
随机推荐
priority_ Understanding of queue
Equal wealth
Synchronous real-time data of Jerry's watch [chapter]
matlab习题 —— 图像绘制练习
Jerry's watch pause [chapter]
Lanbao sensor technology rushes to the scientific innovation board: annual revenue of 350million yuan xuyongtong family has a strong color
[North Asia data recovery] data recovery case of ibm-ds3512 storage server RAID5 damaged data loss
LinkedList learning
问题——adb shellerror: insufficient permissions for device: verify udev rules.
2022-2028 global sound insulation coating industry research and trend analysis report
Codeforces Round #771 (Div. 2) ABC
Allegro's method of setting network flying line and network color
Matlab exercises - image drawing exercises
线程池是什么老鸡?
PMP商业分析概述
99乘法表
Tu ne peux pas comprendre le feu?
逆序对对数计算,顺序对对数计算——归并排序
2022-2028 global CAE engineering service industry research and trend analysis report
解决allegro中测量距离时,点击一个点后光标闪烁的问题