当前位置:网站首页>Concurrency, Timing and Relativity
Concurrency, Timing and Relativity
2022-07-31 16:39:00 【HUAWEI CLOUD】
Two operations are said to be concurrent if they happen at the same time, but in fact it doesn't matter whether the operations overlap in time.Due to the complex clock synchronization problem of distributed systems, it is difficult to strictly judge whether two events occur at the same time in reality.
To better define concurrency, it does not depend on the exact time of occurrence, i.e. two operations are said to be concurrent if they do not need to be aware of each other, regardless of the physical time at which they occur.
In a computer system, two operations may be concurrent, even if the speed of light is fast enough to allow one operation to affect the other.If the network is congested or interrupted, it may happen that two operations become concurrent because one operation cannot perceive the other due to network problems.
Identify context
Look at the algorithm for determining whether two operations are concurrent.Start with a database with only one replica.
Figure-13 shows two clients adding items to the shopping cart at the same time.Initially, the cart is empty.The two clients then issue five writes to the DB:
- Client 1 first adds milk to the shopping cart.This is the first time the value of this K is written, the server successfully stores and assigns it version number 1, and finally returns the value to the client along with the version number 1
- Client 2 adds eggs to the shopping cart, at this point it does not know that client 1 has added milk (client 2 thinks its eggs are the only items in the shopping cart).The server assigns version number 2 for this write, and stores eggs and milk as two separate values.Finally, both values are returned to client 2 and the version number 2 is attached
- Similarly, client 1 does not know the writing of client 2 and wants to add flour to the shopping cart, so it thinks that the current shopping cart should be [milk, flour].Send this value to the server along with the version number 1 previously provided by the server to client 1.The server knows from the version number that the new value of [milk, flour] is written to replace the previous value of [milk], but concurrently with the [egg] value.So the server assigns version 3 to [milk, flour] and overwrites version 1's [milk], but keeps the value of version 2 [egg], returning both values to client 1
- Meanwhile, Client 2 wants to add ham, not knowing that Client 1 just added flour.Client 2 received two values [milk] and [egg] from the server in the last response, so client 2 now merges these values and adds ham to form a new value, [egg, milk, ham].It sends this value to the server with the previous version number 2 .The server detects that the new value overwrites version 2[egg], but the new value is also concurrent with version 3[milk,flour], so the remaining two are v3[milk,flour], andv4: [eggs, milk, ham]
- Finally, Client 1 wants to add bacon.It used to receive [milk, flour] and [eggs] from the server in v3, so it merged these, added bacon, and sent the final value [milk, flour, eggs, bacon] to the server along with the version number v3.This overwrites v3[milk,flour] (note that [egg] is already overridden in the last step), but is concurrent with v4[egg,milk,ham], so the server keeps both concurrent values
Figure-13 The data flow between operations is shown in Figure-14.The arrows indicate which operations occur before other operations, i.e. later operations know or depend on earlier operations.In this case, the client will never fully grasp the data on the server because there is always another operation going on at the same time.However, the new version value will eventually overwrite the old value, and no loss of the written value will occur.
The basis for the server to judge whether the operation is concurrent or not mainly relies on comparing the version number, without explaining the old and new values themselves (the value can be any data structure).Algorithm Workflow:
- The server keeps a version number for each K, increments the version number each time a new value of K is written, and saves the new version number with the written value
- When the client reads K, the server will return all (uncovered values) current values and the latest version number.And before requesting to write, the client must first send a read request
- When the client writes K, the write request must contain the version number read before and the merged set of all values read before.The response to a write request can be like a read request, returning all current values, so that multiple writes can be concatenated like in the shopping cart example.)
- When the server receives a write pending for a specific version number, overwrites all values of the version number or lower (because they know they have been merged into the new incoming set of values), but must save higherAll values of the version number (since these values are concurrent with the current write)
When the write request contains the version number of the previous read, it means that the modification is based on the previous state.If a write request does not contain a version number, it will be concurrent with all other write operations, will not overwrite any existing values, and the incoming value will be included in the return value list of subsequent read requests.
边栏推荐
- Baidu cloud web speed playback (is there any website available)
- AcWing 1282. 搜索关键词 题解((AC自动机)Trie+KMP)+bfs)
- 对话庄表伟:开源第一课
- 【7.29】代码源 - 【排列】【石子游戏 II】【Cow and Snacks】【最小生成数】【数列】
- 使用互相关进行音频对齐
- LeetCode_733_图像渲染
- Replication Latency Case (1) - Eventual Consistency
- 智能垃圾桶(八)——红外对管传感器(树莓派pico)
- 牛客网刷题(三)
- 【pytorch】pytorch 自动求导、 Tensor 与 Autograd
猜你喜欢
C语言-函数
The new BMW 3 Series is on the market, with safety and comfort
Qt practical cases (54) - using transparency QPixmap design pictures
最新神作!阿里巴巴刚出炉的面试参考指南(泰山版),我直接狂刷29天
复杂高维医学数据挖掘与疾病风险分类研究
Implementing DDD based on ABP
利用PHP开发具有注册、登陆、文件上传、发布动态功能的网站
[TypeScript] In-depth study of TypeScript type operations
动态规划之线性dp(上)
.NET 20th Anniversary Interview - Zhang Shanyou: How .NET technology empowers and changes the world
随机推荐
Qt实战案例(54)——利用QPixmap设计图片透明度
Implementing distributed locks based on Redis (SETNX), case: Solving oversold orders under high concurrency
IP协议从0到1
多主复制下处理写冲突(4)-多主复制拓扑
selenium的常见方法及使用
牛客 HJ16 购物单
无主复制系统(1)-节点故障时写DB
阿里三面:MQ 消息丢失、重复、积压问题,如何解决?
LevelSequence源码分析
Applicable scenario of multi-master replication (2) - client and collaborative editing that require offline operation
牛客 HJ19 简单错误记录
.NET 20th Anniversary Interview - Zhang Shanyou: How .NET technology empowers and changes the world
GP 6 overall architecture study notes
【7.29】Code Source - 【Arrangement】【Stone Game II】【Cow and Snacks】【Minimum Number of Spawns】【Sequence】
2.索引及调优篇【mysql高级】
牛客 HJ17 坐标移动
【Yugong Series】July 2022 Go Teaching Course 022-Dictionary of Go Containers
ansible学习笔记02
LeetCode_733_Image rendering
【网络通信三】研华网关Modbus服务设置