当前位置:网站首页>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.
边栏推荐
- [Meetup Preview] OpenMLDB+OneFlow: Link feature engineering to model training to accelerate machine learning model development
- 百度网盘网页版加速播放(有可用的网站吗)
- 【网络通信三】研华网关Modbus服务设置
- Foreign media right, apple on May be true in inventory
- 网站漏洞修复服务商关于越权漏洞分析
- Handling write conflicts under multi-master replication (4) - multi-master replication topology
- ML.NET related resources
- 华为顶级工程师历时9年总结的“趣谈网络协议”PDF文档,太强了
- [TypeScript] In-depth study of TypeScript type operations
- 小程序:matlab解微分方程「建议收藏」
猜你喜欢

Implementing distributed locks based on Redis (SETNX), case: Solving oversold orders under high concurrency

Huawei's top engineers lasted nine years "anecdotal stories network protocol" PDF document summary, is too strong

SringMVC中个常见的几个问题

The 2nd China PWA Developer Day

adb shell 报错error: device unauthorized

C language - function

【pytorch】pytorch 自动求导、 Tensor 与 Autograd

adb shell error error: device unauthorized

利用PHP开发具有注册、登陆、文件上传、发布动态功能的网站

智能垃圾桶(九)——震动传感器(树莓派pico实现)
随机推荐
After the form is submitted, the page does not jump [easy to understand]
adb shell error error: device unauthorized
LeetCode_733_图像渲染
多主复制的适用场景(1)-多IDC
【Yugong Series】July 2022 Go Teaching Course 022-Dictionary of Go Containers
Website vulnerability repair service provider's analysis of unauthorized vulnerability
Handling write conflicts under multi-master replication (4) - multi-master replication topology
tensorflow2.0 cnn(layerwise)
【pytorch】1.7 pytorch与numpy,tensor与array的转换
无主复制系统(3)-Quorum一致性的局限性
多主复制的适用场景(2)-需离线操作的客户端和协作编辑
【C语言】LeetCode27.移除元素
网站漏洞修复服务商关于越权漏洞分析
基于C语言的编译器设计与实现
Qt practical cases (54) - using transparency QPixmap design pictures
2020 WeChat applet decompilation tutorial (can applet decompile source code be used)
MySQL multi-table union query
Dialogue with Zhuang Biaowei: The first lesson of open source
ansible学习笔记02
jeecg主从数据库读写分离配置「建议收藏」