[go: up one dir, main page]

CN117311913A - Distributed transaction processing method, device, equipment and storage medium - Google Patents

Distributed transaction processing method, device, equipment and storage medium Download PDF

Info

Publication number
CN117311913A
CN117311913A CN202311268432.6A CN202311268432A CN117311913A CN 117311913 A CN117311913 A CN 117311913A CN 202311268432 A CN202311268432 A CN 202311268432A CN 117311913 A CN117311913 A CN 117311913A
Authority
CN
China
Prior art keywords
transaction
sub
node
delay
transactions
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202311268432.6A
Other languages
Chinese (zh)
Inventor
卢卫
杜小勇
李彤
庄琪钰
陈育兴
潘安群
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Renmin University of China
Shenzhen Tencent Computer Systems Co Ltd
Original Assignee
Renmin University of China
Shenzhen Tencent Computer Systems Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Renmin University of China, Shenzhen Tencent Computer Systems Co Ltd filed Critical Renmin University of China
Priority to CN202311268432.6A priority Critical patent/CN117311913A/en
Publication of CN117311913A publication Critical patent/CN117311913A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/465Distributed object oriented systems
    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/466Transaction processing

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The embodiment of the application provides a distributed transaction processing method, a device, equipment and a storage medium, which can be applied to various scenes such as cloud technology, artificial intelligence, intelligent traffic, auxiliary driving, vehicle-mounted and the like, and the method comprises the following steps: the coordinator node divides the distributed transaction into sub-transactions that execute on different participant nodes. For each sub-transaction, determining the target sending time delay of the coordination node when sending the sub-transaction by taking the negative correlation of the target sending time delay corresponding to the sub-transaction and the network time delay corresponding to the sub-transaction as a reference. For the sub-transaction with shorter network delay, the target sending delay of the coordination node when sending the sub-transaction is longer, so that the execution time of the sub-transaction is later, and the associated data items do not need to be locked before the sub-transaction is executed, so that the data items can be operated by other distributed transactions, thereby reducing the lock contention time of the sub-transaction with shorter network delay to the data items and improving the throughput performance and concurrency of the distributed system.

Description

一种分布式事务处理方法、装置、设备及存储介质A distributed transaction processing method, device, equipment and storage medium

技术领域Technical Field

本申请实施例涉及数据库技术领域,尤其涉及一种分布式事务处理方法、装置、设备及存储介质。The embodiments of the present application relate to the field of database technology, and in particular to a distributed transaction processing method, apparatus, device and storage medium.

背景技术Background Art

在数据计算和数据高可用的驱动下,数据中心的数量越来越多。在传统的业务模式中,事务聚焦单个数据中心的数据访问。随着业务模式的变化,跨数据中心进行数据项操作的分布式事务越来越普遍。Driven by data computing and data high availability, the number of data centers is increasing. In the traditional business model, transactions focus on data access in a single data center. As business models change, distributed transactions that operate data items across data centers are becoming more and more common.

在分布式事务处理过程中,协调节点将分布式事务划分为多个子事务,然后将多个子事务分别发送至相应的参与节点。每个参与节点在接收到子事务时,对子事务涉及的数据项进行加锁并处理,然后返回执行结果至协调节点。协调节点在接收到所有子事务的执行结果后,返回分布式事务的最终状态至各个参与节点。每个参与节点在接收到处理状态后,对已加锁的数据项解锁。In the distributed transaction processing process, the coordinating node divides the distributed transaction into multiple sub-transactions, and then sends the multiple sub-transactions to the corresponding participating nodes. When each participating node receives a sub-transaction, it locks and processes the data items involved in the sub-transaction, and then returns the execution result to the coordinating node. After receiving the execution results of all sub-transactions, the coordinating node returns the final status of the distributed transaction to each participating node. After receiving the processing status, each participating node unlocks the locked data items.

在上述处理过程中,协调节点需要接收到所有子事务对应的执行结果,才能触发各个参与节点分别对已加锁的数据项解锁。然而,不同子事务的执行结果到达协调节点的时间是不相同的,这样使得最快子事务(执行结果最早到达协调节点的子事务)对应的参考节点需要等待最慢子事务(执行结果最晚到达协调节点的子事务)对应的参考节点返回执行结果时,才能对已加锁的数据项解锁。而数据项在加锁状态下无法被其他分布式事务操作,导致其他分布式事务需要等待。在分布式事务的数量较多时,容易造成事务处理阻塞,进而影响分布式数据库的吞吐性能。In the above processing, the coordination node needs to receive the execution results corresponding to all subtransactions before it can trigger each participating node to unlock the locked data items. However, the time when the execution results of different subtransactions arrive at the coordination node is different, so the reference node corresponding to the fastest subtransaction (the subtransaction whose execution result arrives at the coordination node earliest) needs to wait for the reference node corresponding to the slowest subtransaction (the subtransaction whose execution result arrives at the coordination node latest) to return the execution result before it can unlock the locked data items. In the locked state, the data items cannot be operated by other distributed transactions, causing other distributed transactions to wait. When the number of distributed transactions is large, it is easy to cause transaction processing blockage, which in turn affects the throughput performance of the distributed database.

发明内容Summary of the invention

本申请实施例提供了一种分布式事务处理方法、装置、设备及存储介质,用于提高分布式数据库的吞吐性能。Embodiments of the present application provide a distributed transaction processing method, apparatus, device, and storage medium for improving the throughput performance of a distributed database.

一方面,本申请实施例提供了一种分布式事务处理方法,包括:On the one hand, an embodiment of the present application provides a distributed transaction processing method, including:

将分布式事务划分为多个子事务,每个子事务对应一个参与节点;Divide the distributed transaction into multiple sub-transactions, each of which corresponds to a participating node;

针对所述多个子事务,分别执行以下操作:基于一个子事务对应的参与节点与协调节点之间的网络时延,获得所述一个子事务对应的目标发送时延,所述目标发送时延与所述网络时延呈负相关;For the multiple sub-transactions, the following operations are performed respectively: based on the network delay between the participating node and the coordinating node corresponding to the sub-transaction, a target sending delay corresponding to the sub-transaction is obtained, and the target sending delay is negatively correlated with the network delay;

按照获得的多个目标发送时延,分别将所述多个子事务发送至相应的参与节点进行事务处理;According to the obtained multiple target sending delays, the multiple sub-transactions are respectively sent to corresponding participating nodes for transaction processing;

基于所述多个子事务各自对应的参与节点返回的子事务处理消息,获得所述分布式事务的处理状态。Based on the sub-transaction processing messages returned by the participating nodes corresponding to each of the multiple sub-transactions, the processing status of the distributed transaction is obtained.

一方面,本申请实施例提供了一种分布式事务处理装置,包括:On the one hand, an embodiment of the present application provides a distributed transaction processing device, including:

划分模块,用于将分布式事务划分为多个子事务,每个子事务对应一个参与节点;A partitioning module is used to divide a distributed transaction into multiple sub-transactions, each of which corresponds to a participating node;

处理模块,用于针对所述多个子事务,分别执行以下操作:基于一个子事务对应的参与节点与所述协调节点之间的网络时延,获得所述一个子事务对应的目标发送时延,所述目标发送时延与所述网络时延呈负相关;The processing module is used to perform the following operations respectively for the multiple sub-transactions: based on the network delay between the participating node corresponding to the sub-transaction and the coordinating node, obtain the target sending delay corresponding to the sub-transaction, wherein the target sending delay is negatively correlated with the network delay;

发送模块,用于按照获得的多个目标发送时延,分别将所述多个子事务发送至相应的参与节点进行事务处理;A sending module, configured to send the plurality of sub-transactions to corresponding participating nodes for transaction processing respectively according to the obtained plurality of target sending delays;

所述处理模块,还用于基于所述多个子事务各自对应的参与节点返回的子事务处理消息,获得所述分布式事务的处理状态。The processing module is further used to obtain the processing status of the distributed transaction based on the sub-transaction processing messages returned by the participating nodes corresponding to each of the multiple sub-transactions.

可选地,所述划分模块具体用于:Optionally, the division module is specifically used for:

基于所述分布式事务关联的多个数据项各自对应的参与节点,将所述分布式事务划分为多个子事务,其中,每个子事务对应至少一个数据项,且每个子事务对应一个参与节点。Based on participating nodes corresponding to each of the multiple data items associated with the distributed transaction, the distributed transaction is divided into multiple sub-transactions, wherein each sub-transaction corresponds to at least one data item and each sub-transaction corresponds to a participating node.

可选地,所述处理模块具体用于:Optionally, the processing module is specifically used for:

获取所述多个子事务各自对应的参与节点,分别与所述协调节点之间的网络时延;Obtaining network delays between participating nodes corresponding to each of the multiple sub-transactions and the coordinating node;

从所述多个子事务各自对应的网络时延中获得最大网络时延;Obtaining a maximum network delay from the network delays corresponding to the multiple sub-transactions;

基于所述最大网络时延和所述一个子事务对应的网络时延,获得所述一个子事务对应的目标发送时延,其中,所述最大网络时延与所述一个子事务对应的网络时延之间的时延差值,与所述目标发送时延呈正相关。Based on the maximum network delay and the network delay corresponding to the subtransaction, a target sending delay corresponding to the subtransaction is obtained, wherein a delay difference between the maximum network delay and the network delay corresponding to the subtransaction is positively correlated with the target sending delay.

可选地,所述处理模块具体用于:Optionally, the processing module is specifically used for:

基于所述一个子事务对应的网络时延,获得所述一个子事务对应的总执行时延;Based on the network delay corresponding to the subtransaction, obtaining a total execution delay corresponding to the subtransaction;

基于所述最大网络时延,获得所述分布式事务的总执行时延;Based on the maximum network delay, obtaining a total execution delay of the distributed transaction;

将所述分布式事务的总执行时延,与所述一个子事务对应的总执行时延之间的时延差值,作为所述一个子事务对应的目标发送时延。The delay difference between the total execution delay of the distributed transaction and the total execution delay corresponding to the sub-transaction is used as the target sending delay corresponding to the sub-transaction.

可选地,所述处理模块具体用于:Optionally, the processing module is specifically used for:

基于所述一个子事务对应的网络时延,以及所述一个子事务对应的参与节点本地执行所述一个子事务的执行时延,获得所述一个子事务对应的目标发送时延,其中,所述执行时延与所述目标发送时延呈负相关。Based on the network delay corresponding to the sub-transaction and the execution delay of the participating node corresponding to the sub-transaction locally executing the sub-transaction, a target sending delay corresponding to the sub-transaction is obtained, wherein the execution delay is negatively correlated with the target sending delay.

可选地,所述处理模块具体用于:Optionally, the processing module is specifically used for:

接收所述多个子事务各自对应的参与节点返回的子事务处理消息;Receiving sub-transaction processing messages returned by participating nodes corresponding to each of the multiple sub-transactions;

针对所述多个子事务,分别执行以下操作:从一个子事务对应的子事务处理消息中,获取所述一个子事务关联的数据项的锁争用状态;For the multiple subtransactions, the following operations are performed respectively: from a subtransaction processing message corresponding to a subtransaction, a lock contention state of a data item associated with the subtransaction is obtained;

若所述多个子事务各自对应的锁争用状态均为:已对关联的数据项加锁,则确定所述分布式事务的处理状态为:提交状态。If the lock contention states corresponding to the multiple subtransactions are all: the associated data items have been locked, then the processing state of the distributed transaction is determined to be: committed state.

可选地,所述处理模块具体用于:Optionally, the processing module is specifically used for:

分别向所述多个子事务各自对应的参与节点发送收集请求,所述收集请求用于指示参与节点返回相应子事务的执行结果;Sending a collection request to each participating node corresponding to each of the multiple subtransactions, wherein the collection request is used to instruct the participating node to return the execution result of the corresponding subtransaction;

接收所述多个子事务各自对应的参与节点返回的子事务处理消息,所述子事务处理消息中包括:子事务的执行结果;Receiving sub-transaction processing messages returned by participating nodes corresponding to the multiple sub-transactions, wherein the sub-transaction processing messages include: execution results of the sub-transactions;

当所述多个子事务各自对应的执行结果均为:执行成功时,确定所述分布式事务的处理状态为:提交状态。When the execution results corresponding to the multiple sub-transactions are all: successful execution, it is determined that the processing status of the distributed transaction is: committed status.

所述发送模块还用于:The sending module is also used for:

基于所述多个子事务各自对应的参与节点返回的子事务处理消息,获得所述分布式事务的处理状态之后,将所述分布式事务的处理状态,分别发送至所述多个子事务各自对应的参与节点,以使每个参与节点对相应的数据项进行解锁。After obtaining the processing status of the distributed transaction based on the sub-transaction processing messages returned by the participating nodes corresponding to each of the multiple sub-transactions, the processing status of the distributed transaction is sent to the participating nodes corresponding to each of the multiple sub-transactions, so that each participating node unlocks the corresponding data item.

一方面,本申请实施例提供了一种计算机设备,包括存储器、处理器及存储在存储器上并可在处理器上运行的计算机程序,所述处理器执行所述程序时实现上述分布式事务处理方法的步骤。On the one hand, an embodiment of the present application provides a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the above-mentioned distributed transaction processing method when executing the program.

一方面,本申请实施例提供了一种计算机可读存储介质,其存储有可由计算机设备执行的计算机程序,当所述程序在计算机设备上运行时,使得所述计算机设备执行上述分布式事务处理方法的步骤。On the one hand, an embodiment of the present application provides a computer-readable storage medium, which stores a computer program executable by a computer device. When the program runs on the computer device, the computer device executes the steps of the above-mentioned distributed transaction processing method.

一方面,本申请实施例提供了一种计算机程序产品,所述计算机程序产品包括存储在计算机可读存储介质上的计算机程序,所述计算机程序包括程序指令,当所述程序指令被计算机设备执行时,使所述计算机设备执行上述分布式事务处理的步骤。On the one hand, an embodiment of the present application provides a computer program product, which includes a computer program stored on a computer-readable storage medium, and the computer program includes program instructions. When the program instructions are executed by a computer device, the computer device performs the above-mentioned distributed transaction processing steps.

本申请实施例中,协调节点在接收到分布式事务之后,先将分布式事务划分为在不同参与节点上执行的子事务。针对每个子事务,基于该子事务对应的参与节点与协调节点之间的网络时延,确定协调节点在发送该子事务时的目标发送时延。由于子事务对应的目标发送时延与子事务对应的网络时延呈负相关,因此,对应网络时延越长的子事务,其对应的目标发送时延越短,即协调节点会越早发送该子事务至相应的参与节点进行事务处理;对于网络时延越短的子事务,其对应的目标发送时延越长,即协调节点会越晚发送该子事务至相应的参与节点进行事务处理。In an embodiment of the present application, after receiving a distributed transaction, the coordinating node first divides the distributed transaction into sub-transactions executed on different participating nodes. For each sub-transaction, based on the network delay between the participating node and the coordinating node corresponding to the sub-transaction, the target sending delay of the coordinating node when sending the sub-transaction is determined. Since the target sending delay corresponding to the sub-transaction is negatively correlated with the network delay corresponding to the sub-transaction, the longer the corresponding network delay of the sub-transaction, the shorter the corresponding target sending delay, that is, the earlier the coordinating node will send the sub-transaction to the corresponding participating node for transaction processing; for the shorter the network delay of the sub-transaction, the longer the corresponding target sending delay, that is, the later the coordinating node will send the sub-transaction to the corresponding participating node for transaction processing.

对于网络时延较短的子事务来说,由于协调节点在发送该子事务时的目标发送时延较长,因此,该子事务的执行时刻较晚,那么在该子事务执行之前,不需要锁定该子事务对应的数据项,故这些数据项可以被其他分布式事务操作,从而减少了网络时延较短的子事务对数据项的锁争用时长。For subtransactions with shorter network delays, since the target sending delay of the coordinating node when sending the subtransaction is longer, the execution time of the subtransaction is later. Therefore, before the subtransaction is executed, there is no need to lock the data items corresponding to the subtransaction, so these data items can be operated by other distributed transactions, thereby reducing the lock contention time of the subtransactions with shorter network delays for the data items.

同时,由于该子事务的网络时延较短,故相应的执行结果到达协调节点的时刻,与网络时延较长的子事务的执行结果到达协调节点的时刻相差不大,从而实现在不影响分布式事务执行的情况下,减少了热点数据的锁争用时长,从而提高了分布式系统的吞吐性能以及并发度。At the same time, since the network delay of this sub-transaction is relatively short, the time when the corresponding execution result arrives at the coordination node is not much different from the time when the execution result of the sub-transaction with a longer network delay arrives at the coordination node. This reduces the lock contention time of hot data without affecting the execution of distributed transactions, thereby improving the throughput performance and concurrency of the distributed system.

附图说明BRIEF DESCRIPTION OF THE DRAWINGS

为了更清楚地说明本发明实施例中的技术方案,下面将对实施例描述中所需要使用的附图作简要介绍,显而易见地,下面描述中的附图仅仅是本发明的一些实施例,对于本领域的普通技术人员来讲,在不付出创造性劳动性的前提下,还可以根据这些附图获得其他的附图。In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.

图1A为本申请实施例提供的一种分布式事务处理方法的流程示意图;FIG1A is a schematic diagram of a flow chart of a distributed transaction processing method provided in an embodiment of the present application;

图1B为本申请实施例提供的一种系统架构的结构示意图;FIG1B is a schematic diagram of a system architecture provided in an embodiment of the present application;

图2为本申请实施例提供的一种服务端的结构示意图;FIG2 is a schematic diagram of the structure of a server provided in an embodiment of the present application;

图3为本申请实施例提供的一种企业级分布式数据库的系统架构图;FIG3 is a system architecture diagram of an enterprise-level distributed database provided in an embodiment of the present application;

图4为本申请实施例提供的一种分布式事务处理方法的流程示意图;FIG4 is a schematic diagram of a flow chart of a distributed transaction processing method provided in an embodiment of the present application;

图5为本申请实施例提供的一种分布式事务的加锁流程示意图;FIG5 is a schematic diagram of a locking process of a distributed transaction provided in an embodiment of the present application;

图6为本申请实施例提供的一种分布式事务的解锁流程示意图;FIG6 is a schematic diagram of an unlocking process of a distributed transaction provided in an embodiment of the present application;

图7为本申请实施例提供的一种分布式事务处理方法的流程示意图;FIG7 is a schematic diagram of a flow chart of a distributed transaction processing method provided in an embodiment of the present application;

图8为本申请实施例提供的一种分布式事务处理装置的结构示意图;FIG8 is a schematic diagram of the structure of a distributed transaction processing device provided in an embodiment of the present application;

图9为本申请实施例提供的一种计算机设备的结构示意图。FIG. 9 is a schematic diagram of the structure of a computer device provided in an embodiment of the present application.

具体实施方式DETAILED DESCRIPTION

为了使本发明的目的、技术方案及有益效果更加清楚明白,以下结合附图及实施例,对本发明进行进一步详细说明。应当理解,此处所描述的具体实施例仅仅用以解释本发明,并不用于限定本发明。In order to make the purpose, technical solution and beneficial effects of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention.

为了方便理解,下面对本发明实施例中涉及的名词进行解释。For ease of understanding, the terms involved in the embodiments of the present invention are explained below.

云技术(Cloud technology)是指在广域网或局域网内将硬件、软件、网络等系列资源统一起来,实现数据的计算、储存、处理和共享的一种托管技术。Cloud technology refers to a hosting technology that unifies hardware, software, network and other resources within a wide area network or local area network to achieve data computing, storage, processing and sharing.

云技术(Cloud technology)基于云计算商业模式应用的网络技术、信息技术、整合技术、管理平台技术、应用技术等的总称,可以组成资源池,按需所用,灵活便利。云计算技术将变成重要支撑。技术网络系统的后台服务需要大量的计算、存储资源,如视频网站、图片类网站和更多的门户网站。伴随着互联网行业的高度发展和应用,将来每个物品都有可能存在自己的识别标志,都需要传输到后台系统进行逻辑处理,不同程度级别的数据将会分开处理,各类行业数据皆需要强大的系统后盾支撑,只能通过云计算来实现。本申请实施例可以云技术对分布式事务进行处理。Cloud technology is a general term for network technology, information technology, integration technology, management platform technology, application technology, etc. based on the cloud computing business model. It can form a resource pool, which can be used on demand and is flexible and convenient. Cloud computing technology will become an important support. The background services of the technical network system require a large amount of computing and storage resources, such as video websites, picture websites and more portal websites. With the rapid development and application of the Internet industry, each item may have its own identification mark in the future, and all of them need to be transmitted to the background system for logical processing. Data of different levels will be processed separately, and all kinds of industry data require strong system backing support, which can only be achieved through cloud computing. The embodiment of the present application can process distributed transactions with cloud technology.

数据库(Database),简而言之可视为电子化的文件柜,即存储电子文件的处所,用户可以对文件中的数据进行新增、查询、更新、删除等操作。所谓“数据库”是以一定方式储存在一起、能与多个用户共享、具有尽可能小的冗余度、与应用程序彼此独立的数据集合。In short, a database can be seen as an electronic filing cabinet, that is, a place where electronic files are stored, and users can add, query, update, delete, and other operations on the data in the files. The so-called "database" is a collection of data that is stored together in a certain way, can be shared with multiple users, has as little redundancy as possible, and is independent of the application.

数据库管理系统(Database Management System,简称DBMS):为管理数据库而设计的电脑软件系统,一般具有存储、截取、安全保障、备份等基础功能。数据库管理系统可以依据它所支持的数据库模型来做分类,例如关系式、XML(Extensible Markup Language,即可扩展标记语言);或依据所支持的计算机类型来做分类,例如服务器群集、移动电话;或依据所用查询语言做作分类;例如,结构化查询语言(Structured Query Language,简称SQL)、XQuery;或依据性能冲量重点来做分类。例如,最大规模、最高运行速度;亦或其他的分类方式。不论使用哪种分类方式,一些DBMS能够跨类别,例如,同时支持多种查询语言。Database Management System (DBMS): A computer software system designed to manage databases, generally with basic functions such as storage, retrieval, security, and backup. Database management systems can be classified according to the database model they support, such as relational, XML (Extensible Markup Language); or according to the type of computer they support, such as server clusters, mobile phones; or according to the query language used, such as Structured Query Language (SQL), XQuery; or according to performance focus, such as maximum scale, maximum operating speed; or other classification methods. Regardless of the classification method used, some DBMS can cross categories, for example, supporting multiple query languages at the same time.

分布式事务:指事务的参与者、支持事务的服务器、资源服务器以及事务管理器分别位于不同的分布式系统的不同节点之上。在本申请实施中,分布式事务中包含:对不同节点上的数据项进行操作的事务。Distributed transaction: refers to the transaction participants, servers supporting the transaction, resource servers and transaction managers located on different nodes of different distributed systems. In the implementation of this application, the distributed transaction includes: transactions that operate on data items on different nodes.

锁争用时长:事务从执行阶段获取数据项上的锁到释放锁之间的时长,即事务对数据项解锁的时刻与事务对数据项加锁的时刻的差值。Lock contention duration: the duration from when a transaction acquires a lock on a data item to when it releases the lock during the execution phase, that is, the difference between the time when the transaction unlocks the data item and the time when the transaction locks the data item.

数据库模式定义语言:(Data Definition Language,简称DDL),用于描述数据库中要存储的现实世界实体的语言。Database Schema Definition Language: (Data Definition Language, referred to as DDL), is a language used to describe real-world entities to be stored in a database.

数据库操作语句:(Data Manipulation Language,简称DML),用于对数据库中表的数据记录进行更新,基本操作关键字包括:插入、删除、更新。Database operation statements: (Data Manipulation Language, referred to as DML), are used to update data records in the database table. The basic operation keywords include: insert, delete, and update.

2PL:两阶段锁协议,包括共享锁(S-Lock)和排它锁(X-Lock),读操作对数据项加共享锁,写操作对数据项加排它锁。2PL: Two-phase locking protocol, including shared lock (S-Lock) and exclusive lock (X-Lock). Read operations add shared locks to data items, and write operations add exclusive locks to data items.

下面对本申请实施例的设计思想进行介绍。The design concept of the embodiments of the present application is introduced below.

在传统的业务模式中,事务聚焦单个数据中心的数据访问。随着业务模式的变化,跨数据中心进行数据项操作的分布式事务越来越普遍。In the traditional business model, transactions focus on data access in a single data center. As business models change, distributed transactions that operate on data items across data centers are becoming more and more common.

参见图1A,分布式事务处理过程包括:执行阶段、准备阶段和提交阶段。Referring to FIG. 1A , the distributed transaction processing process includes: an execution phase, a preparation phase, and a submission phase.

在执行阶段,数据库节点N1(协调节点)将分布式事务T1划分为多个子事务(子事务T11、子事务T12、子事务T13),数据库节点N1在本地执行子事务T11,并将子事务T12发给数据库节点N2(参与节点),将子事务T13发给数据库节点N3(参与节点)。参与节点根据并发控制协议执行子事务;如果采用2PL作为并发控制协议时,参与节点将在执行阶段对子事务对应的数据项进行加锁。In the execution phase, database node N1 (coordinating node) divides distributed transaction T1 into multiple subtransactions (subtransaction T11 , subtransaction T12 , subtransaction T13 ). Database node N1 executes subtransaction T11 locally, sends subtransaction T12 to database node N2 (participating node), and sends subtransaction T13 to database node N3 (participating node). Participating nodes execute subtransactions according to the concurrency control protocol; if 2PL is used as the concurrency control protocol, participating nodes will lock the data items corresponding to the subtransactions in the execution phase.

在执行阶段结束后,采用两阶段提交协议(Two-Phase Commit,简称2PC)来保证数据库的一致性,其中,两阶段提交协议将事务提交分为了准备阶段和提交阶段这两个阶段。After the execution phase, a two-phase commit protocol (2PC) is used to ensure database consistency. The two-phase commit protocol divides transaction commit into two phases: preparation phase and commit phase.

在准备阶段,数据库节点N1向数据库节点N2和数据库节点N3发送收集请求。数据库节点N1接收数据库节点N2和数据库节点N3各自返回的子事务的执行结果。In the preparation phase, database node N1 sends a collection request to database node N2 and database node N3. Database node N1 receives the execution results of the sub-transactions returned by database node N2 and database node N3 respectively.

在提交阶段,若数据库节点N2和数据库节点N3中至少一个数据库节点返回的子事务的执行结果为:执行失败,则将分布式事务的最终状态设置为:回滚状态,并通知数据库节点N2和数据库节点N3执行对子事务的回滚操作。In the commit phase, if the execution result of the sub-transaction returned by at least one of database node N2 and database node N3 is: execution failure, the final state of the distributed transaction is set to: rollback state, and database node N2 and database node N3 are notified to perform a rollback operation on the sub-transaction.

若数据库节点N1收到数据库节点N2和数据库节点N3返回的执行结果均为:执行成功,则将分布式事务的最终状态设置为:提交状态。If the execution results returned by database node N2 and database node N3 received by database node N1 are both: execution successful, the final state of the distributed transaction is set to: committed state.

数据库节点N1将分布式事务的最终状态发送至数据库节点N2和数据库节点N3,数据库节点N2和数据库节点N3分别对子事务对应的数据项进行解锁。Database node N1 sends the final status of the distributed transaction to database node N2 and database node N3, and database node N2 and database node N3 unlock the data items corresponding to the sub-transactions respectively.

由以上分布式事务处理过程可知,在执行阶段,子事务T12和子事务T13分别需要经过一个往返时延(Round Trip Time,简称RTT)才能执行完毕。在准备阶段,子事务T12和子事务T13也分别经过一个往返时延。在提交阶段,数据库节点N1(协调节点)根据各子事务的执行状态,确定分布式事务的最终状态(提交状态或回滚状态)。协调节点发送分布式事务的最终状态至各个参与节点,各个参与节点在收到最终状态后才对相应的数据项进行解锁(即释放锁)。From the above distributed transaction processing process, it can be seen that in the execution phase, subtransaction T 12 and subtransaction T 13 need to go through a round trip time (RTT) to complete execution. In the preparation phase, subtransaction T 12 and subtransaction T 13 also go through a round trip time. In the submission phase, database node N1 (coordinating node) determines the final state (committed state or rollback state) of the distributed transaction according to the execution state of each subtransaction. The coordinating node sends the final state of the distributed transaction to each participating node, and each participating node unlocks the corresponding data item (i.e. releases the lock) only after receiving the final state.

也就是说,在不改变分布式事务执行逻辑的前提下,影响分布式事务处理性能的主要原因包括:多轮的TCP/IP网络通信的网络延时以及高争用场景下的子事务本地执行时延。在跨空间域的场景下,分布式事务可能会涉及不同的参与节点,不同参与节点与协调节点之间的网络时延不可忽略且差异较大(比如,数据库节点N1与数据库节点N2之间的往返时延,远小于数据库节点N1与数据库节点N3之间的往返时延)。That is to say, without changing the distributed transaction execution logic, the main reasons affecting the performance of distributed transaction processing include: network delays in multiple rounds of TCP/IP network communications and local execution delays of sub-transactions in high-contention scenarios. In cross-space domain scenarios, distributed transactions may involve different participating nodes, and the network delays between different participating nodes and the coordination node cannot be ignored and vary greatly (for example, the round-trip delay between database node N1 and database node N2 is much smaller than the round-trip delay between database node N1 and database node N3).

而采用两阶段提交协议进行事务提交时,最快子事务(比如图1A中的子事务T11)对应的参考节点,需要等待最慢子事务(比如图1A中的子事务T13)对应的参考节点返回执行结果时,才能对已加锁的数据项解锁。这样给最快子事务引入了因等待最慢子事务带来的不必要的锁争用时长;并且,在最快子事务对数据项锁定的时段内,其他分布式事务操作无法对该数据项进行操作,导致其他分布式事务需要等待。在分布式事务的数量较多时(即高争用场景下),被阻塞或者回滚的分布式事务也会随之增多,进而影响分布式数据库的吞吐性能。When a transaction is committed using the two-phase commit protocol, the reference node corresponding to the fastest subtransaction (such as subtransaction T 11 in FIG. 1A ) needs to wait for the reference node corresponding to the slowest subtransaction (such as subtransaction T 13 in FIG. 1A ) to return the execution result before unlocking the locked data item. This introduces unnecessary lock contention time to the fastest subtransaction due to waiting for the slowest subtransaction; and during the period when the fastest subtransaction locks the data item, other distributed transaction operations cannot operate on the data item, causing other distributed transactions to wait. When the number of distributed transactions is large (i.e., in a high contention scenario), the number of blocked or rolled back distributed transactions will also increase, thereby affecting the throughput performance of the distributed database.

通过对分布式事务处理过程进行分析可知,过早的执行网络时延较短的子事务,反而增加了这些子事务对数据项的锁争用时长。因此,若在不额外增加分布式事务的总执行时长的情况下,适当延迟网络时延较短的子事务的执行时刻,则可以有效地减少这些延迟执行的子事务对数据项的锁争用时长,进而减小被阻塞或者回滚的分布式事务的数量,提高分布式数据库的吞吐性能。By analyzing the distributed transaction processing process, it can be seen that premature execution of subtransactions with shorter network delays increases the lock contention time of these subtransactions on data items. Therefore, if the execution time of subtransactions with shorter network delays is appropriately delayed without increasing the total execution time of distributed transactions, the lock contention time of these delayed subtransactions on data items can be effectively reduced, thereby reducing the number of blocked or rolled back distributed transactions and improving the throughput performance of distributed databases.

鉴于此,本申请实施例提供了一种分布式事务处理方法,该方法由协调节点执行,包括以下步骤:将分布式事务划分为多个子事务,每个子事务对应一个参与节点;针对多个子事务,分别执行以下操作:基于一个子事务对应的参与节点与协调节点之间的网络时延,获得该子事务对应的目标发送时延,目标发送时延与网络时延呈负相关。然后按照获得的多个目标发送时延,分别将多个子事务发送至相应的参与节点进行事务处理。最后,基于多个子事务各自对应的参与节点返回的子事务处理消息,获得分布式事务的处理状态。In view of this, an embodiment of the present application provides a distributed transaction processing method, which is executed by a coordination node and includes the following steps: dividing a distributed transaction into multiple sub-transactions, each sub-transaction corresponds to a participating node; for multiple sub-transactions, performing the following operations respectively: based on the network delay between a participating node corresponding to a sub-transaction and the coordination node, obtaining the target sending delay corresponding to the sub-transaction, and the target sending delay is negatively correlated with the network delay. Then, according to the obtained multiple target sending delays, the multiple sub-transactions are respectively sent to the corresponding participating nodes for transaction processing. Finally, based on the sub-transaction processing messages returned by the participating nodes corresponding to each of the multiple sub-transactions, the processing status of the distributed transaction is obtained.

本申请实施例中,协调节点在接收到分布式事务之后,先将分布式事务划分为在不同参与节点上执行的子事务。针对每个子事务,基于该子事务对应的参与节点与协调节点之间的网络时延,确定协调节点在发送该子事务时的目标发送时延。由于子事务对应的目标发送时延与子事务对应的网络时延呈负相关,因此,对应网络时延越长的子事务,其对应的目标发送时延越短,即协调节点会越早发送该子事务至相应的参与节点进行事务处理;对于网络时延越短的子事务,其对应的目标发送时延越长,即协调节点会越晚发送该子事务至相应的参与节点进行事务处理。In an embodiment of the present application, after receiving a distributed transaction, the coordinating node first divides the distributed transaction into sub-transactions executed on different participating nodes. For each sub-transaction, based on the network delay between the participating node and the coordinating node corresponding to the sub-transaction, the target sending delay of the coordinating node when sending the sub-transaction is determined. Since the target sending delay corresponding to the sub-transaction is negatively correlated with the network delay corresponding to the sub-transaction, the longer the corresponding network delay of the sub-transaction, the shorter the corresponding target sending delay, that is, the earlier the coordinating node will send the sub-transaction to the corresponding participating node for transaction processing; for the shorter the network delay of the sub-transaction, the longer the corresponding target sending delay, that is, the later the coordinating node will send the sub-transaction to the corresponding participating node for transaction processing.

对于网络时延较短的子事务来说,由于协调节点在发送该子事务时的目标发送时延较长,因此,该子事务的执行时刻较晚,那么在该子事务执行之前,不需要锁定该子事务对应的数据项,故这些数据项可以被其他分布式事务操作,从而减少了网络时延较短的子事务对数据项的锁争用时长。For subtransactions with shorter network delays, since the target sending delay of the coordinating node when sending the subtransaction is longer, the execution time of the subtransaction is later. Therefore, before the subtransaction is executed, there is no need to lock the data items corresponding to the subtransaction, so these data items can be operated by other distributed transactions, thereby reducing the lock contention time of the subtransactions with shorter network delays for the data items.

同时,由于该子事务的网络时延较短,故相应的执行结果到达协调节点的时刻,与网络时延较长的子事务的执行结果到达协调节点的时刻相差不大,从而实现在不影响分布式事务执行的情况下,减少了热点数据的锁争用时长,从而提高了分布式系统的吞吐性能以及并发度。At the same time, since the network delay of this sub-transaction is relatively short, the time when the corresponding execution result arrives at the coordination node is not much different from the time when the execution result of the sub-transaction with a longer network delay arrives at the coordination node. This reduces the lock contention time of hot data without affecting the execution of distributed transactions, thereby improving the throughput performance and concurrency of the distributed system.

参考图1B,其为本申请实施例适用的一种系统架构图,该系统架构至少包括客户端101和服务端102,客户端101的数量可以是一个或多个,服务端102的数量也可以是一个或多个,本申请对客户端101和服务端102的数量不做具体限定。Refer to Figure 1B, which is a system architecture diagram applicable to an embodiment of the present application. The system architecture at least includes a client 101 and a server 102. The number of clients 101 can be one or more, and the number of servers 102 can also be one or more. The present application does not specifically limit the number of clients 101 and servers 102.

客户端101中预先安装应用,其中,应用是客户端应用、网页版应用、小程序应用等。客户端101包括但不限于手机、电脑、智能语音交互设备、智能家电、车载终端、飞行器等。Applications are pre-installed in the client 101, where the applications are client applications, web applications, mini-program applications, etc. The client 101 includes but is not limited to mobile phones, computers, intelligent voice interaction devices, smart home appliances, vehicle terminals, aircraft, etc.

服务端102可以是独立的物理服务器,也可以是多个物理服务器构成的服务器集群或者分布式系统、分布式数据库,还可以是提供云服务、云数据库、云计算、云函数、云存储、网络服务、云通信、中间件服务、域名服务、安全服务、内容分发网络(Content DeliveryNetwork,CDN)、以及大数据和人工智能平台等基础云计算服务的云服务器。客户端101和服务端102可以通过有线或无线通信方式进行直接或间接地连接,本申请在此不做限制。The server 102 may be an independent physical server, or a server cluster or distributed system or distributed database composed of multiple physical servers, or a cloud server providing basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, content delivery networks (CDN), and big data and artificial intelligence platforms. The client 101 and the server 102 may be directly or indirectly connected via wired or wireless communications, which is not limited in this application.

本申请实施例中的分布式事务处理方法可以是客户端101执行,也可以是服务端102执行,还也可以由客户端101与服务端102交互执行,对此,本申请不做具体限定。本发明实施例可应用于各种场景,包括但不限于云技术、人工智能、智慧交通、辅助驾驶等。The distributed transaction processing method in the embodiment of the present application can be executed by the client 101, or by the server 102, or by the client 101 and the server 102 interacting with each other, and this application does not make specific limitations on this. The embodiment of the present invention can be applied to various scenarios, including but not limited to cloud technology, artificial intelligence, smart transportation, assisted driving, etc.

在一些实施例中,服务端102包括多个数据库节点,多个数据库节点中包括协调节点201和多个参与节点202,需要说明的是,协调节点201同时也可以是参与节点。In some embodiments, the server 102 includes multiple database nodes, and the multiple database nodes include a coordination node 201 and multiple participating nodes 202. It should be noted that the coordination node 201 can also be a participating node.

另外,协调节点可以是从多个数据库节点中通过选举获得的,也可以是预先设定的。任意两个数据库节点之间可以通过有线或无线方式进行直接或间接地连接,本申请在此不做限制。In addition, the coordination node may be obtained through election from multiple database nodes, or may be pre-set. Any two database nodes may be directly or indirectly connected via wired or wireless means, and this application does not limit this.

协调节点201接收到客户端101发送的分布式事务,然后将分布式事务划分多个子事务,然后将多个子事务分发给各个参与节点202进行事务处理。协调节点201接收各个参与节点202返回的执行结果,并基于返回的执行结果确定分布式事务的执行结果,并将分布式事务的执行结果返回给客户端101。The coordination node 201 receives the distributed transaction sent by the client 101, divides the distributed transaction into multiple sub-transactions, and then distributes the multiple sub-transactions to each participating node 202 for transaction processing. The coordination node 201 receives the execution results returned by each participating node 202, determines the execution result of the distributed transaction based on the returned execution results, and returns the execution result of the distributed transaction to the client 101.

在实际应用中,本申请实施例中的分布式事务处理方法适用于任意系统架构下的跨空间域的事务处理。In practical applications, the distributed transaction processing method in the embodiment of the present application is applicable to cross-space domain transaction processing under any system architecture.

举例来说,参见图3,为本申请实施例提供的一种企业级分布式数据库的系统架构图,该系统架构包括:For example, referring to FIG. 3 , a system architecture diagram of an enterprise-level distributed database provided in an embodiment of the present application is shown. The system architecture includes:

前端应用接口301、网关系统302、调度服务303、决策服务304、多个数据库节点305,每个数据库节点305包括一个主机和至少一个备机。数据库节点305中的主机或备机可以指用于存储业务数据的MySQL实例。Front-end application interface 301, gateway system 302, scheduling service 303, decision service 304, multiple database nodes 305, each database node 305 includes a host and at least one backup. The host or backup in the database node 305 can refer to a MySQL instance for storing business data.

该企业级分布式数据库是通过分库分表架构实现的分布式数据库,将一个大表通过主键把数据打散到每个数据库节点305中存放,每个数据库节点305水平切分了逻辑大表,这样每个数据库节点305上的数据独立且与其他数据库节点305没有关系。如果存在跨数据库节点事务,则通过XA/2PC技术进行支持,保证了写操作时数据的原子性和一致性。The enterprise-level distributed database is a distributed database implemented through a sub-library and sub-table architecture. A large table is divided into each database node 305 for storage through the primary key. Each database node 305 horizontally divides the logical large table, so that the data on each database node 305 is independent and has no relationship with other database nodes 305. If there is a cross-database node transaction, it is supported by XA/2PC technology to ensure the atomicity and consistency of data during write operations.

在企业级分布式数据库中,决策服务304是管理调度中心,主要包括以下功能:调度DDL操作、控制数据库节点主备切换、控制数据库节点容量伸缩、控制数据迁移、自身的故障解决。In an enterprise-level distributed database, the decision service 304 is a management and scheduling center, which mainly includes the following functions: scheduling DDL operations, controlling database node master-slave switching, controlling database node capacity expansion and contraction, controlling data migration, and solving its own faults.

调度DDL操作的功能具体为:网关系统302通过前端应用接口301接收客户端发送的操作请求。网关系统302从操作请求中识别出DDL操作时,将DDL操作以DDL任务形式保存在调度服务303中。决策服务304从调度服务303中拉取DDL任务,并将DDL任务发送至数据库节点305的MySQL实例(主机或备机)执行。数据库节点305中的MySQL实例执行完毕后,将相应的应答消息发送至网关系统302。网关系统302收集MySQL实例反馈的应答消息,并对收集的应答消息进行组合处理之后,通过前端应用接口301返回给客户端。The function of scheduling DDL operations is specifically as follows: the gateway system 302 receives the operation request sent by the client through the front-end application interface 301. When the gateway system 302 identifies the DDL operation from the operation request, it saves the DDL operation in the scheduling service 303 in the form of a DDL task. The decision service 304 pulls the DDL task from the scheduling service 303 and sends the DDL task to the MySQL instance (host or backup) of the database node 305 for execution. After the MySQL instance in the database node 305 is executed, the corresponding response message is sent to the gateway system 302. The gateway system 302 collects the response messages fed back by the MySQL instance, and after combining and processing the collected response messages, returns them to the client through the front-end application interface 301.

控制数据库节点主备切换的功能具体为:数据库节点305上报主机和备机的状态信息至调度服务303。决策服务304从调度服务303拉取主机和备机的状态信息。决策服务304基于获取的状态信息,确定数据库节点305中的主机故障时,选取备机作为新主机,并通知新主机和其他备机。The function of controlling the active/standby switching of the database node is specifically as follows: the database node 305 reports the status information of the master and standby machines to the scheduling service 303. The decision service 304 pulls the status information of the master and standby machines from the scheduling service 303. Based on the acquired status information, the decision service 304 determines that when the master in the database node 305 fails, selects the standby machine as the new master, and notifies the new master and other standby machines.

控制数据库节点容量伸缩的功能具体为:决策服务304检测到数据库节点305中的MySQL实例故障时,对数据库节点305自动扩容。另外,决策服务304检测数据库节点305中各个MySQL实例的资源利用率,并基于资源利用率,判断是否对MySQL实例进行扩容或缩容。The function of controlling the capacity expansion and contraction of the database node is specifically as follows: when the decision service 304 detects a MySQL instance failure in the database node 305, the database node 305 is automatically expanded. In addition, the decision service 304 detects the resource utilization of each MySQL instance in the database node 305, and determines whether to expand or reduce the capacity of the MySQL instance based on the resource utilization.

控制数据迁移功能具体为:决策服务304通过调度服务303下发数据迁移任务至数据库节点305。数据库节点305中的原实例将备份镜像复制到目标实例,并返回应答消息至决策服务304。决策服务304通知网关系统302修改路由至目标实例,以及下发删除旧数据的指令至数据库节点305,数据库节点305中的原实例删除旧数据。The control data migration function is specifically as follows: the decision service 304 sends the data migration task to the database node 305 through the scheduling service 303. The original instance in the database node 305 copies the backup image to the target instance and returns a response message to the decision service 304. The decision service 304 notifies the gateway system 302 to modify the route to the target instance, and sends an instruction to delete the old data to the database node 305, and the original instance in the database node 305 deletes the old data.

自身的故障解决功能具体为:决策服务304故障时,协调服务303选举新的决策服务304作为管理调度中心,以实现容灾。The fault resolution function thereof is specifically as follows: when the decision service 304 fails, the coordination service 303 elects a new decision service 304 as the management and scheduling center to achieve disaster recovery.

接着介绍调度服务303、网关系统302和数据库节点305的功能:Next, the functions of the scheduling service 303, the gateway system 302 and the database node 305 are introduced:

调度服务303为企业级分布式数据库的资源管理中心,用于保存网关系统302上报的DDL任务、数据库节点305上报的主机和备机的状态信息、数据迁移任务等数据。在实际应用中,调度服务303是单个分布式应用程序协调服务(ZooKeeper),或者ZooKeeper集群。The scheduling service 303 is a resource management center of an enterprise-level distributed database, and is used to store data such as DDL tasks reported by the gateway system 302, status information of the master and backup machines reported by the database node 305, and data migration tasks. In actual applications, the scheduling service 303 is a single distributed application coordination service (ZooKeeper), or a ZooKeeper cluster.

网关系统302为数据库节点访问的中间件,具体包括:路由功能、用户鉴权功能、检测功能等。The gateway system 302 is the middleware for database node access, which specifically includes: routing function, user authentication function, detection function, etc.

路由功能除了包含前文描述的将DDL操作以DDL任务形式保存在调度服务303中之外,还包括对DML操作的路由功能。具体地,网关系统302从操作请求中识别出DML操作时,对DML操作进行sql转换,获得转换操作。网关系统303拉取实时路由信息,并基于实时路由信息,将转换操作发送至对应的数据库节点305中的MySQL实例执行。数据库节点305中的MySQL实例执行完毕后,将相应的应答消息发送至网关系统302。网关系统302收集数据库节点305中的MySQL实例反馈的应答消息,并对收集的应答消息进行组合处理之后,通过前端应用接口301返回给客户端。In addition to the aforementioned storage of DDL operations in the form of DDL tasks in the scheduling service 303, the routing function also includes a routing function for DML operations. Specifically, when the gateway system 302 identifies a DML operation from an operation request, it performs a sql conversion on the DML operation to obtain a conversion operation. The gateway system 303 pulls real-time routing information, and based on the real-time routing information, sends the conversion operation to the corresponding MySQL instance in the database node 305 for execution. After the MySQL instance in the database node 305 is executed, the corresponding response message is sent to the gateway system 302. The gateway system 302 collects the response messages fed back by the MySQL instance in the database node 305, and after combining and processing the collected response messages, returns them to the client through the front-end application interface 301.

用户鉴权功能具体为:预先获得相关权限信息,网关系统302在与客户端建立连接时,基于权限信息,对建立连接的用户名和密码进行校验。The user authentication function is specifically as follows: relevant authority information is obtained in advance, and when the gateway system 302 establishes a connection with the client, the user name and password for establishing the connection are verified based on the authority information.

检测功能:实时检测调度服务303的状态,并在检测到检测调度服务303异常时,触发告警。Detection function: detect the status of the dispatch service 303 in real time, and trigger an alarm when an abnormality is detected in the dispatch service 303.

数据库节点305中的每个MySQL实例包括以下功能:检测自身的状态信息并上报状态信息至调度服务303、参与前文描述的数据迁移流程和主备切换流程等。数据库节点305的相关功能在前文介绍网关系统302、调度服务303、决策服务304的功能时已有详细介绍,此处不再赘述。Each MySQL instance in the database node 305 includes the following functions: detecting its own status information and reporting the status information to the scheduling service 303, participating in the data migration process and the master-slave switching process described above, etc. The relevant functions of the database node 305 have been introduced in detail in the above description of the functions of the gateway system 302, the scheduling service 303, and the decision service 304, and will not be repeated here.

在上述企业级分布式数据库中,存在大量的跨数据库节点的分布式事务,采用本申请实施例中的分布式事务处理方法进行调度和处理时,可以对企业级分布式数据库等分布式数据库带来分布式事务性能的提升,使得企业级分布式数据库等分布式数据库可以满足当前互联网应用对数据库的性能要求,进而提升产品竞争力。In the above-mentioned enterprise-level distributed database, there are a large number of distributed transactions across database nodes. When the distributed transaction processing method in the embodiment of the present application is used for scheduling and processing, the distributed transaction performance of distributed databases such as enterprise-level distributed databases can be improved, so that distributed databases such as enterprise-level distributed databases can meet the performance requirements of current Internet applications for databases, thereby improving product competitiveness.

下面详细介绍本申请实施例提供了一种分布式事务处理方法的流程,如图4所示,该方法的流程由计算机设备执行,该计算机设备可以是图2所示的协调节点201,也可以是图3所示的数据库节点305,包括以下步骤:The following is a detailed description of a process of a distributed transaction processing method provided by an embodiment of the present application. As shown in FIG4 , the process of the method is executed by a computer device, which may be the coordination node 201 shown in FIG2 or the database node 305 shown in FIG3 , and includes the following steps:

步骤S401,将分布式事务划分为多个子事务,每个子事务对应一个参与节点。Step S401, divide the distributed transaction into multiple sub-transactions, each sub-transaction corresponds to a participating node.

具体地,分布式事务中包括多个事务操作,该多个事务操作是针对不同数据库节点上的数据项进行操作,因此,服务端在接收到客户端发送的分布式事务后,由服务端中的协调节点将分布式事务划分为多个子事务,并将多个子事务分别分发至相应的参与节点。Specifically, a distributed transaction includes multiple transaction operations, which are performed on data items on different database nodes. Therefore, after the server receives the distributed transaction sent by the client, the coordination node in the server divides the distributed transaction into multiple sub-transactions and distributes the multiple sub-transactions to the corresponding participating nodes.

在一些实施例中,协调节点基于分布式事务关联的多个数据项各自对应的参与节点,将分布式事务划分为多个子事务,其中,每个子事务对应至少一个数据项,且每个子事务对应一个参与节点。In some embodiments, the coordinating node divides the distributed transaction into multiple sub-transactions based on the participating nodes corresponding to each of the multiple data items associated with the distributed transaction, wherein each sub-transaction corresponds to at least one data item and each sub-transaction corresponds to a participating node.

具体地,分布式事务中多个事务操作对应的数据项,位于不同的参与节点。为了便于进行事务操作,可以将位于同一参与节点的数据项对应的事务操作,合并为一个子事务,以实现对分布式事务进行划分。Specifically, the data items corresponding to multiple transaction operations in the distributed transaction are located at different participating nodes. In order to facilitate transaction operations, the transaction operations corresponding to the data items located at the same participating node can be merged into a sub-transaction to achieve the division of the distributed transaction.

举例来说,设定服务端包括三个数据库节点,分别为数据库节点N1、数据库节点N2和数据库节点N3,其中,数据库节点N1为协调节点,数据库节点N1同时也是参与节点,数据库节点N2和数据库节点N3均为参与节点。For example, it is assumed that the server includes three database nodes, namely database node N1, database node N2 and database node N3, wherein database node N1 is a coordination node and also a participating node, and database node N2 and database node N3 are both participating nodes.

数据项x和数据项y位于数据库节点N1,数据项z位于数据库节点N2,数据项v和数据项u位于数据库节点N3。Data item x and data item y are located at database node N1, data item z is located at database node N2, and data item v and data item u are located at database node N3.

分布式事务T1中的事务操作序列为:R1(x)W1(y)W1(z)R1(v)W1(u),其中,R1(x)表示读取数据项x,W1(y)表示修改数据项y,W1(z)表示修改数据项z,R1(v)表示读取数据项v,W1(u)表示修改数据项u。The transaction operation sequence in distributed transaction T1 is: R 1 (x) W 1 (y) W 1 (z) R 1 (v) W 1 (u), where R 1 (x) means reading data item x, W 1 (y) means modifying data item y, W 1 (z) means modifying data item z, R 1 (v) means reading data item v, and W 1 (u) means modifying data item u.

由于数据项x和数据项y位于数据库节点N1,因此,将针对数据项x和数据项y的事务操作作为一个子事务T11,子事务T11中的事务操作序列为:R1(x)W1(y)。Since data item x and data item y are located in database node N1, the transaction operation for data item x and data item y is regarded as a sub-transaction T11 , and the transaction operation sequence in sub-transaction T11 is: R1 (x) W1 (y).

由于数据项z位于数据库节点N2,因此,将针对数据项z的事务操作作为一个子事务T12,子事务T12中的事务操作包括:W1(z)。Since the data item z is located in the database node N2, the transaction operation for the data item z is taken as a sub-transaction T 12. The transaction operation in the sub-transaction T 12 includes: W 1 (z).

由于数据项v和数据项u位于数据库节点N3,因此,将针对数据项v和数据项u的事务操作作为一个子事务T13,子事务T13中的事务操作序列为:R1(v)W1(u)。Since data item v and data item u are located in database node N3, the transaction operation for data item v and data item u is regarded as a sub-transaction T 13 , and the transaction operation sequence in sub-transaction T 13 is: R 1 (v) W 1 (u).

本申请实施例中,将分布式事务划分为多个子事务,并将多个子事务分别发送至不同的参与节点进行事务处理,实现多个子事务并行处理,从而有效提高分布式事务的处理效率。In an embodiment of the present application, a distributed transaction is divided into multiple sub-transactions, and the multiple sub-transactions are respectively sent to different participating nodes for transaction processing, so that multiple sub-transactions can be processed in parallel, thereby effectively improving the processing efficiency of distributed transactions.

步骤S402,针对多个子事务,分别执行以下操作:基于一个子事务对应的参与节点与协调节点之间的网络时延,获得该子事务对应的目标发送时延。Step S402, for multiple sub-transactions, respectively perform the following operations: based on the network delay between the participating node and the coordinating node corresponding to a sub-transaction, obtain the target sending delay corresponding to the sub-transaction.

具体地,由于过早的执行网络时延较短的子事务,反而增加了这些子事务对数据项的锁争用时长。因此,在参与节点本地执行子事务的执行时延,远小于跨空间域的数据库节点之间的网络时延的前提下,通过使网络时延较短的子事务延迟执行,可以有效地减少这些网络时延较短的子事务对数据项的锁争用时长。鉴于此,本申请实施例中以目标发送时延与网络时延呈负相关为基准,基于子事务对应的参与节点与协调节点之间的网络时延,获得该子事务对应的目标发送时延。Specifically, due to the premature execution of subtransactions with shorter network delays, the lock contention duration of these subtransactions on data items is increased. Therefore, under the premise that the execution delay of subtransactions executed locally by participating nodes is much less than the network delay between database nodes across spatial domains, by delaying the execution of subtransactions with shorter network delays, the lock contention duration of these subtransactions with shorter network delays on data items can be effectively reduced. In view of this, in the embodiment of the present application, the target sending delay is negatively correlated with the network delay, and the target sending delay corresponding to the subtransaction is obtained based on the network delay between the participating nodes and the coordinating node corresponding to the subtransaction.

在一些实施例中,在分布式事务对应的多个子事务中,最慢子事务对应的网络时延大于其他子事务的网络延时,且最慢子事务对应的网络时延实质上也是分布式事务中的最大网络时延。不同分布式事务中的最大网络时延是不相同的。In some embodiments, among the multiple subtransactions corresponding to the distributed transaction, the network delay corresponding to the slowest subtransaction is greater than the network delays of other subtransactions, and the network delay corresponding to the slowest subtransaction is substantially the maximum network delay in the distributed transaction. The maximum network delays in different distributed transactions are different.

针对同一起始时刻,分布式事务中的最大网络时延越大,对应的最慢子事务的执行结果到达协调节点的时刻越晚,其他子事务的执行结果相应也可以越晚到达协调节点;也就是说,其他子事务的目标发送时延也可以设置的越大。相反,分布式事务中的最大网络时延越小,对应的最慢子事务的执行结果到达协调节点的时刻越早,其他子事务的执行结果相应需要越早到达协调节点;也就是说,其他子事务的目标发送时延需要设置的越小。For the same starting time, the larger the maximum network delay in a distributed transaction, the later the execution result of the corresponding slowest subtransaction arrives at the coordination node, and the execution results of other subtransactions can also arrive at the coordination node later; that is, the target sending delay of other subtransactions can also be set larger. On the contrary, the smaller the maximum network delay in a distributed transaction, the earlier the execution result of the corresponding slowest subtransaction arrives at the coordination node, and the execution results of other subtransactions need to arrive at the coordination node earlier; that is, the target sending delay of other subtransactions needs to be set smaller.

鉴于此,本申请实施例中,获取多个子事务各自对应的参与节点,分别与协调节点之间的网络时延,然后从多个子事务各自对应的网络时延中获得最大网络时延。基于最大网络时延和一个子事务对应的网络时延,获得该子事务对应的目标发送时延,其中,最大网络时延与一个子事务对应的网络时延之间的时延差值,与该子事务的目标发送时延呈正相关。In view of this, in an embodiment of the present application, the network delays between the participating nodes corresponding to each of the multiple sub-transactions and the coordinating node are obtained, and then the maximum network delay is obtained from the network delays corresponding to each of the multiple sub-transactions. Based on the maximum network delay and the network delay corresponding to a sub-transaction, the target sending delay corresponding to the sub-transaction is obtained, wherein the delay difference between the maximum network delay and the network delay corresponding to a sub-transaction is positively correlated with the target sending delay of the sub-transaction.

本申请实施例中,获得分布式事务的最大网络时延与子事务对应的网络时延之间的时延差值,并以获得的时延差值,与子事务的目标发送时延呈正相关为基准,确定分布式事务中各个子事务对应的目标发送时延。那么,对于最大网络时延更长的分布式事务来说,其中的除最慢子事务的其他子事务的发送时延更长,相应对关联的数据项的锁争用时长也就越短,进而减小被阻塞或者回滚的分布式事务的数量,提高分布式数据库的吞吐性能。In the embodiment of the present application, the delay difference between the maximum network delay of the distributed transaction and the network delay corresponding to the subtransaction is obtained, and the target sending delay corresponding to each subtransaction in the distributed transaction is determined based on the obtained delay difference being positively correlated with the target sending delay of the subtransaction. Then, for a distributed transaction with a longer maximum network delay, the sending delay of other subtransactions except the slowest subtransaction is longer, and the corresponding lock contention time for the associated data item is shorter, thereby reducing the number of distributed transactions that are blocked or rolled back and improving the throughput performance of the distributed database.

在一些实施例中,在对网络时延较短的子事务延迟执行时,也需要设置相关约束条件进行约束。例如,延迟执行的子事务的执行结果到达协调节点的时间,不能晚于最慢子事务的执行结果到达协调节点的时间。In some embodiments, when delaying the execution of a sub-transaction with a shorter network delay, relevant constraints also need to be set. For example, the time when the execution result of the delayed sub-transaction arrives at the coordination node cannot be later than the time when the execution result of the slowest sub-transaction arrives at the coordination node.

为了最大程度地减少分布式事务的锁争用时长,同时满足相关约束条件,本申请实施例采用以下方式确定子事务对应的目标发送时延:In order to minimize the lock contention duration of distributed transactions and satisfy relevant constraints, the embodiment of the present application adopts the following method to determine the target sending delay corresponding to the sub-transaction:

首先,将分布式事务T的锁争用时长采用以下公式(1)表示:First, the lock contention duration of distributed transaction T is expressed by the following formula (1):

其中,W表示分布式事务T的锁争用时长,ki表示分布式事务T需要操作的数据项,T.ws表示分布式事务T执行写操作(修改操作)的数据项集合,T.rs表示分布式事务T执行读操作的数据项集合,UnLock(ki)表示分布式事务T对数据项ki解锁的时刻,Lock(ki)表示分布式事务T对数据项ki加锁的时刻。Wherein, W represents the lock contention duration of distributed transaction T, ki represents the data item that distributed transaction T needs to operate, T.ws represents the set of data items on which distributed transaction T performs write operations (modification operations), T.rs represents the set of data items on which distributed transaction T performs read operations, UnLock( ki ) represents the moment when distributed transaction T unlocks data item ki , and Lock( ki ) represents the moment when distributed transaction T locks data item ki .

由于协调节点需要接收到所有子事务对应的执行结果,才能触发各个参与节点分别对已加锁的数据项解锁,并且,参与节点本地执行子事务的执行时延,远小于跨空间域的数据库节点之间的网络时延(即,参与节点本地执行子事务的执行时延忽略不计),因此将分布式事务T的总执行时延表示为以下公式(2):Since the coordinating node needs to receive the execution results corresponding to all sub-transactions before triggering each participating node to unlock the locked data items respectively, and the execution delay of the participating node executing the sub-transaction locally is much smaller than the network delay between database nodes across the spatial domain (that is, the execution delay of the participating node executing the sub-transaction locally is negligible), the total execution delay of the distributed transaction T is expressed as the following formula (2):

D(T)=2·max{τc,j}………(2)D(T)=2·max{τ c,j }......(2)

其中,D(T)表示分布式事务T的总执行时延,τc,j表示协调节点Nc和参与节点Nj之间的网络时延。Where D(T) represents the total execution delay of distributed transaction T, and τc ,j represents the network delay between the coordinating node Nc and the participating nodes Nj .

Lock(ki)可以进一步表示为以下公式(3):Lock(k i ) can be further expressed as the following formula (3):

其中,DelayTime(ki)表示数据项ki所在的子事务对应的发送时延,node(ki)表示执行数据项ki所在的子事务的参与节点。Wherein, DelayTime(k i ) represents the sending delay corresponding to the subtransaction where the data item k i is located, and node(k i ) represents the participating node that executes the subtransaction where the data item k i is located.

UnLock(ki)可以进一步表示为以下公式(4):UnLock(k i ) can be further expressed as the following formula (4):

其中,node(ki)表示执行数据项ki所在的子事务的参与节点。Wherein, node(k i ) represents the participating node that executes the subtransaction where data item k i is located.

基于上述公式(3)和公式(4),将上述公式(1)改写为以下公式(5):Based on the above formula (3) and formula (4), the above formula (1) is rewritten as the following formula (5):

以最小化分布式事务T的锁争用时长W为优化目标,以各子事务的执行结果到达协调节点的时刻,位于分布式事务T的总执行时延D(T)之前为约束条件,构建以下公式(6):Taking minimizing the lock contention time W of distributed transaction T as the optimization goal, and taking the time when the execution result of each sub-transaction arrives at the coordination node before the total execution delay D(T) of distributed transaction T as the constraint condition, the following formula (6) is constructed:

由于2·max{τc,j}为一个常量,因此可以将公式(6)进一步改写为以下公式(7):Since 2·max{τ c,j } is a constant, formula (6) can be further rewritten as the following formula (7):

将公式(7)中优化目标与约束条件合并,获得以下公式(8):Combining the optimization objective and constraint conditions in formula (7) yields the following formula (8):

从上述公式(8)中可以看出,当公式(8)取等号时,优化目标取得最大值,设定数据项ki属于子事务ti,则可以获得子事务ti的目标发送时延的计算公式如以下公式(9)所示:It can be seen from the above formula (8) that when the formula (8) takes the equal sign, the optimization target obtains the maximum value. Assuming that the data item k i belongs to the subtransaction ti , the calculation formula for the target sending delay of the subtransaction ti can be obtained as shown in the following formula (9):

其中,DelayTime(ti)表示子事务ti的目标发送时延。Wherein, DelayTime(t i ) represents the target sending delay of subtransaction t i .

从上述公式(9)可以看出,在分布式事务处理过程中,以参与节点本地执行子事务的执行时延忽略不计为前提,协调节点基于一个子事务对应的参与节点与协调节点之间的网络时延,获得该子事务对应的总执行时延。基于分布式事务中的最大网络时延,获得分布式事务的总执行时延。再将分布式事务的总执行时延,与该子事务对应的总执行时延之间的时延差值,作为该子事务对应的目标发送时延。It can be seen from the above formula (9) that in the distributed transaction processing process, the execution delay of the local execution of the sub-transaction by the participating node is negligible. The coordinating node obtains the total execution delay corresponding to a sub-transaction based on the network delay between the participating node and the coordinating node corresponding to the sub-transaction. The total execution delay of the distributed transaction is obtained based on the maximum network delay in the distributed transaction. The delay difference between the total execution delay of the distributed transaction and the total execution delay corresponding to the sub-transaction is then used as the target sending delay corresponding to the sub-transaction.

具体地,在参与节点本地执行子事务的执行时延忽略不计的情况下,子事务对应的总执行时延指:协调节点与处理该子事务的参与节点之间的往返时延,也就是协调节点与参与节点之间的网络时延的两倍;分布式事务的总执行时延指:协调节点与处理最慢子事务的参与节点之间的往返时延,也就是分布式事务中的最大网络时延的两倍。将分布式事务的总执行时与该子事务对应的总执行时延之间的时延差值,作为该子事务对应的目标发送时延。Specifically, when the execution delay of the subtransaction executed locally by the participating nodes is ignored, the total execution delay corresponding to the subtransaction refers to: the round-trip delay between the coordinating node and the participating nodes processing the subtransaction, that is, twice the network delay between the coordinating node and the participating nodes; the total execution delay of the distributed transaction refers to: the round-trip delay between the coordinating node and the participating node processing the slowest subtransaction, that is, twice the maximum network delay in the distributed transaction. The delay difference between the total execution time of the distributed transaction and the total execution delay corresponding to the subtransaction is used as the target sending delay corresponding to the subtransaction.

举例来说,设定服务端包括三个数据库节点,分别为数据库节点N1、数据库节点N2和数据库节点N3,其中,数据库节点N1为协调节点,同时也是参与节点,数据库节点N2和数据库节点N3为参与节点。数据库节点N1与数据库节点N2之间的网络时延为10ms,数据库节点N1与数据库节点N3之间的网络时延为30ms。For example, assume that the server includes three database nodes, namely database node N1, database node N2 and database node N3, where database node N1 is a coordination node and also a participating node, and database node N2 and database node N3 are participating nodes. The network delay between database node N1 and database node N2 is 10ms, and the network delay between database node N1 and database node N3 is 30ms.

数据库节点N1接收客户端发送的分布式事务T1,分布式事务T1中的事务操作包括:R1(x)W1(y)W1(z)R1(v)W1(u),其中,R1(x)表示读取数据项x,W1(y)表示修改数据项y,W1(z)表示修改数据项z,R1(v)表示读取数据项v,W1(u)表示修改数据项u。Database node N1 receives the distributed transaction T1 sent by the client. The transaction operations in the distributed transaction T1 include: R 1 (x) W 1 (y) W 1 (z) R 1 (v) W 1 (u), where R 1 (x) represents reading data item x, W 1 (y) represents modifying data item y, W 1 (z) represents modifying data item z, R 1 (v) represents reading data item v, and W 1 (u) represents modifying data item u.

将分布式事务T1划分为:子事务T11、子事务T12、子事务T13,其中,子事务T11中的事务操作包括:R1(x)W1(y),子事务T12中的事务操作包括:W1(z),子事务T13中的事务操作包括:R1(v)W1(u)。子事务T11由数据库节点N1本地执行,子事务T12由数据库节点N2执行,子事务T13由数据库节点N3执行。Distributed transaction T1 is divided into subtransaction T11 , subtransaction T12 , and subtransaction T13 , wherein the transaction operation in subtransaction T11 includes: R1 (x) W1 (y), the transaction operation in subtransaction T12 includes: W1 (z), and the transaction operation in subtransaction T13 includes: R1 (v) W1 (u). Subtransaction T11 is executed locally by database node N1, subtransaction T12 is executed by database node N2, and subtransaction T13 is executed by database node N3.

在数据库节点本地执行子事务的执行时延忽略不计的情况下,分布式事务T1的总执行时延:2·max{τc,j}=60ms;那么,基于上述公式(9)可知,子事务T11的目标发送时延DelayTime(T11)=60-0=60ms;子事务T12的目标发送时延DelayTime(T12)=60-20=40ms;子事务T13的目标发送时延DelayTime(T13)=60-60=0ms。When the execution delay of the subtransaction executed locally on the database node is ignored, the total execution delay of the distributed transaction T1 is: 2·max{τ c,j }=60ms. Then, based on the above formula (9), it can be known that the target sending delay of subtransaction T11 is DelayTime( T11 )=60-0=60ms; the target sending delay of subtransaction T12 is DelayTime( T12 )=60-20=40ms; the target sending delay of subtransaction T13 is DelayTime( T13 )=60-60=0ms.

也就是说,数据库节点N1在0ms时刻将子事务T13发送至数据库节点N1进行事务处理。数据库节点N1在40ms时刻将子事务T12发送至数据库节点N2进行事务处理。数据库节点N1在60ms时刻本地执行子事务T11That is, database node N1 sends subtransaction T13 to database node N1 for transaction processing at 0 ms. Database node N1 sends subtransaction T12 to database node N2 for transaction processing at 40 ms. Database node N1 executes subtransaction T11 locally at 60 ms.

本申请实施例中,将分布式事务的总执行时与一个子事务对应的总执行时延之间的时延差值,作为该子事务对应的目标发送时延,实现了在各子事务的执行结果到达协调节点的时刻,位于分布式事务的总执行时延之前的情况下,使分布式事务的锁争用时长达到最小值,从而有效减少了相关数据项的锁争用时长,提高了分布式系统的吞吐性能以及并发度。In an embodiment of the present application, the delay difference between the total execution time of a distributed transaction and the total execution delay corresponding to a subtransaction is used as the target sending delay corresponding to the subtransaction, so that the lock contention duration of the distributed transaction is minimized when the execution result of each subtransaction arrives at the coordination node before the total execution delay of the distributed transaction, thereby effectively reducing the lock contention duration of related data items and improving the throughput performance and concurrency of the distributed system.

在一些实施例中,在冲突率较高的场景中,参与节点本地执行子事务的执行时延会相应变长,因此,在该场景下,参与节点本地执行子事务的执行时延不可忽略。为了提高确定子事务的目标发送时延的准确度,本申请实施例中,基于一个子事务对应的网络时延,以及该子事务对应的参与节点本地执行该子事务的执行时延,获得该子事务对应的目标发送时延。In some embodiments, in a scenario with a high conflict rate, the execution delay of the participating node executing the subtransaction locally will be correspondingly longer. Therefore, in this scenario, the execution delay of the participating node executing the subtransaction locally cannot be ignored. In order to improve the accuracy of determining the target sending delay of the subtransaction, in an embodiment of the present application, based on the network delay corresponding to a subtransaction and the execution delay of the participating node executing the subtransaction locally corresponding to the subtransaction, the target sending delay corresponding to the subtransaction is obtained.

具体地,将子事务对应的网络时延与子事务的本地执行时延求和,获得子事务对应的总执行时延,然后以子事务对应的总执行时延与子事务对应的目标发送时延呈负相关为基准,确定该子事务对应的目标发送时延。Specifically, the network delay corresponding to the subtransaction is summed with the local execution delay of the subtransaction to obtain the total execution delay corresponding to the subtransaction. Then, based on the negative correlation between the total execution delay corresponding to the subtransaction and the target sending delay corresponding to the subtransaction, the target sending delay corresponding to the subtransaction is determined.

本申请实施例中,通过推迟总执行时长较短的子事务,提前执行总执行时长较长的子事务,这样在不影响分布式事务的总执行时长的情况下,减少了总执行时长较短的子事务,对数据项的锁争用时长,从而减少了其他子事务被阻塞的次数,提高了分布式系统的吞吐性能以及并发度。In the embodiment of the present application, by postponing the sub-transactions with shorter total execution time and executing the sub-transactions with longer total execution time in advance, the sub-transactions with shorter total execution time are reduced without affecting the total execution time of the distributed transaction, and the lock contention time for the data item is reduced, thereby reducing the number of times other sub-transactions are blocked, and improving the throughput performance and concurrency of the distributed system.

步骤S403,按照获得的多个目标发送时延,分别将多个子事务发送至相应的参与节点进行事务处理。Step S403: according to the obtained multiple target sending delays, the multiple sub-transactions are respectively sent to the corresponding participating nodes for transaction processing.

具体地,针对每个参与节点,参与节点接收协调节点发送的子事务,其中,子事务中包括:针对参与节点中的数据项的事务操作,事务操作的类型包括但不限于:读操作(Read)、修改或写操作(Write)。Specifically, for each participating node, the participating node receives a sub-transaction sent by the coordinating node, wherein the sub-transaction includes: transaction operations on data items in the participating node, and the types of transaction operations include but are not limited to: read operations (Read), modification or write operations (Write).

当事务操作为读操作时,参与节点访问本地锁表,并对读操作对应的数据项加共享锁SH。当事务操作为修改操作时,参与节点访问本地锁表,并对修改操作对应的数据项加排它锁EX。When the transaction operation is a read operation, the participating node accesses the local lock table and adds a shared lock SH to the data item corresponding to the read operation. When the transaction operation is a modification operation, the participating node accesses the local lock table and adds an exclusive lock EX to the data item corresponding to the modification operation.

对数据项加排它锁EX之前,若该数据项已经被其他事务加锁,则将分布式事务添加至该数据项的等待队列。当其他事务释放锁后,再对该数据项加排它锁EX。Before adding an exclusive lock EX to a data item, if the data item has been locked by other transactions, the distributed transaction is added to the waiting queue of the data item. When other transactions release the lock, an exclusive lock EX is added to the data item.

参与节点对数据项执行完子事务中的事务操作后,返回子事务执行结果和加锁成功消息至协调节点。After the participating nodes complete the transaction operations in the subtransaction on the data items, they return the subtransaction execution results and the lock success message to the coordinating node.

举例来说,参见图5,设定服务端包括三个数据库节点,分别为数据库节点N1、数据库节点N2和数据库节点N3,其中,数据库节点N1为协调节点,同时也是参与节点,数据库节点N2和数据库节点N3为参与节点。For example, referring to FIG5 , it is assumed that the server includes three database nodes, namely database node N1, database node N2 and database node N3, wherein database node N1 is a coordination node and also a participating node, and database node N2 and database node N3 are participating nodes.

数据库节点N1接收客户端发送的分布式事务T1,分布式事务T1中的事务操作包括:Read(X)和Write(Y),其中,Read(X)表示读取数据项X,Write(Y)表示修改数据项Y。数据库节点N1将分布式事务T1划分为:子事务T11和子事务T12Database node N1 receives distributed transaction T1 sent by the client. The transaction operations in distributed transaction T1 include: Read(X) and Write(Y), where Read(X) represents reading data item X and Write(Y) represents modifying data item Y. Database node N1 divides distributed transaction T1 into: subtransaction T11 and subtransaction T12 .

分布式事务T1的加锁流程如图5所示,包括以下步骤:The locking process of distributed transaction T1 is shown in Figure 5, which includes the following steps:

①、数据库节点N1将子事务T11发送至数据库节点N2。①. Database node N1 sends subtransaction T11 to database node N2.

②、数据库节点N1将子事务T12发送至数据库节点N3。②. Database node N1 sends subtransaction T12 to database node N3.

③、数据库节点N2接收到子事务T11后,访问本地锁表并对数据项X加锁(此时数据库节点N2中数据项X的版本为X0),将本地锁表中数据项X的锁类型修改为共享锁SH,将本地锁表中锁拥有者改为分布式事务T1。③ After receiving subtransaction T11 , database node N2 accesses the local lock table and locks data item X (the version of data item X in database node N2 is X0 at this time), changes the lock type of data item X in the local lock table to shared lock SH, and changes the lock owner in the local lock table to distributed transaction T1.

④、数据库节点N3接收到子事务T12后,由于数据项Y当前被分布式事务T0加了共享锁SH(此时数据库节点N3中数据项Y的版本为Y0),因此,将分布式事务T1作为等待者加入到数据项Y的等待队列中。④. After database node N3 receives subtransaction T12 , since data item Y is currently locked with shared lock SH by distributed transaction T0 (the version of data item Y in database node N3 is Y0 at this time), distributed transaction T1 is added to the waiting queue of data item Y as a waiter.

⑤、在分布式事务T0释放数据项Y的锁后,数据库节点N3针对本地锁表执行以下操作:将数据项Y的锁类型从共享锁SH修改为排它锁EX,将锁拥有者从分布式事务T0修改为分布式事务T1,将等待者从分布式事务T1修改为0。⑤. After distributed transaction T0 releases the lock of data item Y, database node N3 performs the following operations on the local lock table: changes the lock type of data item Y from shared lock SH to exclusive lock EX, changes the lock owner from distributed transaction T0 to distributed transaction T1, and changes the waiter from distributed transaction T1 to 0.

⑥、数据库节点N2执行完子事务T11后,将数据项X和加锁成功消息返回协调节点。⑥ After executing subtransaction T11 , database node N2 returns data item X and a lock success message to the coordinating node.

⑦、数据库节点N3执行完子事务T12后,将修改成功消息和加锁成功消息返回协调节点。⑦. After executing subtransaction T12 , database node N3 returns the modification success message and the locking success message to the coordination node.

步骤S404,基于多个子事务各自对应的参与节点返回的子事务处理消息,获得分布式事务的处理状态。Step S404: obtaining the processing status of the distributed transaction based on the sub-transaction processing messages returned by the participating nodes corresponding to the multiple sub-transactions.

具体地,分布式事务的处理状态包括:提交状态和回滚状态。Specifically, the processing status of a distributed transaction includes: a commit status and a rollback status.

协调节点结合多个子事务的执行结果,确定分布式事务的执行结果,并将分布式事务的执行结果返回给客户端。The coordinating node combines the execution results of multiple sub-transactions to determine the execution result of the distributed transaction and returns the execution result of the distributed transaction to the client.

以图3所示的系统架构举例来说,网关系统302通过前端应用接口301接收客户端发送的操作请求。网关系统302从操作请求中识别出DML操作时,对DML操作进行sql转换,获得转换操作。网关系统303拉取实时路由信息,并基于实时路由信息,将转换操作发送至对应的数据库节点305(即协调节点)。由于转换操作中包括针对不同数据库节点上的数据项的事务操作,因此,接收到转换操作的数据库节点305(即协调节点)将转换操作划分为多个子事务,然后采用前文描述的任意一个实施例确定每个子事务的目标发送时延。按照获得的多个目标发送时延,分别将多个子事务发送至相应的数据库节点305(即参与节点)进行事务处理。数据库节点305(即协调节点)收集多个子事务的执行结果,并基于多个子事务的执行结果确定转换操作的执行结果,然后将转换操作的执行结果发送给网关系统302。网关系统302通过前端应用接口301将执行结果返回给客户端。Taking the system architecture shown in FIG3 as an example, the gateway system 302 receives an operation request sent by the client through the front-end application interface 301. When the gateway system 302 identifies a DML operation from the operation request, it performs sql conversion on the DML operation to obtain a conversion operation. The gateway system 303 pulls real-time routing information and sends the conversion operation to the corresponding database node 305 (i.e., the coordination node) based on the real-time routing information. Since the conversion operation includes transaction operations for data items on different database nodes, the database node 305 (i.e., the coordination node) that receives the conversion operation divides the conversion operation into multiple sub-transactions, and then uses any one of the embodiments described above to determine the target sending delay of each sub-transaction. According to the obtained multiple target sending delays, the multiple sub-transactions are sent to the corresponding database node 305 (i.e., the participating node) for transaction processing. The database node 305 (i.e., the coordination node) collects the execution results of the multiple sub-transactions, determines the execution results of the conversion operation based on the execution results of the multiple sub-transactions, and then sends the execution results of the conversion operation to the gateway system 302. The gateway system 302 returns the execution result to the client through the front-end application interface 301 .

本申请实施例中,协调节点在接收到分布式事务之后,先将分布式事务划分为在不同参与节点上执行的子事务。针对每个子事务,基于该子事务对应的参与节点与协调节点之间的网络时延,确定协调节点在发送该子事务时的目标发送时延。由于子事务对应的目标发送时延与子事务对应的网络时延呈负相关,因此,对应网络时延越长的子事务,其对应的目标发送时延越短,即协调节点会越早发送该子事务至相应的参与节点进行事务处理;对于网络时延越短的子事务,其对应的目标发送时延越长,即协调节点会越晚发送该子事务至相应的参与节点进行事务处理。In an embodiment of the present application, after receiving a distributed transaction, the coordinating node first divides the distributed transaction into sub-transactions executed on different participating nodes. For each sub-transaction, based on the network delay between the participating node and the coordinating node corresponding to the sub-transaction, the target sending delay of the coordinating node when sending the sub-transaction is determined. Since the target sending delay corresponding to the sub-transaction is negatively correlated with the network delay corresponding to the sub-transaction, the longer the corresponding network delay of the sub-transaction, the shorter the corresponding target sending delay, that is, the earlier the coordinating node will send the sub-transaction to the corresponding participating node for transaction processing; for the shorter the network delay of the sub-transaction, the longer the corresponding target sending delay, that is, the later the coordinating node will send the sub-transaction to the corresponding participating node for transaction processing.

对于网络时延较短的子事务来说,由于协调节点在发送该子事务时的目标发送时延较长,因此,该子事务的执行时刻较晚,那么在该子事务执行之前,不需要锁定该子事务对应的数据项,故这些数据项可以被其他分布式事务操作,从而减少了网络时延较短的子事务对数据项的锁争用时长。For subtransactions with shorter network delays, since the target sending delay of the coordinating node when sending the subtransaction is longer, the execution time of the subtransaction is later. Therefore, before the subtransaction is executed, there is no need to lock the data items corresponding to the subtransaction, so these data items can be operated by other distributed transactions, thereby reducing the lock contention time of the subtransactions with shorter network delays for the data items.

同时,由于该子事务的网络时延较短,故相应的执行结果到达协调节点的时刻,与网络时延较长的子事务的执行结果到达协调节点的时刻相差不大,从而实现在不影响分布式事务执行的情况下,减少了热点数据的锁争用时长,从而提高了分布式系统的吞吐性能以及并发度。At the same time, since the network delay of this sub-transaction is relatively short, the time when the corresponding execution result arrives at the coordination node is not much different from the time when the execution result of the sub-transaction with a longer network delay arrives at the coordination node. This reduces the lock contention time of hot data without affecting the execution of distributed transactions, thereby improving the throughput performance and concurrency of the distributed system.

在一些实施例中,协调节点按照获得的多个目标发送时延,分别将多个子事务发送至相应的参与节点进行事务处理之后,分别向多个子事务各自对应的参与节点发送收集请求,收集请求用于指示参与节点返回相应子事务的执行结果。In some embodiments, the coordinating node sends multiple sub-transactions to corresponding participating nodes for transaction processing according to the obtained multiple target sending delays, and then sends collection requests to the participating nodes corresponding to each of the multiple sub-transactions. The collection requests are used to instruct the participating nodes to return the execution results of the corresponding sub-transactions.

协调节点接收多个子事务各自对应的参与节点返回的子事务处理消息,子事务处理消息中包括:子事务的执行结果。当多个子事务各自对应的执行结果均为:执行成功时,确定分布式事务的处理状态为:提交状态。The coordinating node receives the sub-transaction processing messages returned by the participating nodes corresponding to the multiple sub-transactions, and the sub-transaction processing messages include: the execution results of the sub-transactions. When the execution results corresponding to the multiple sub-transactions are all: execution success, the processing state of the distributed transaction is determined to be: commit state.

具体地,服务端接收到客户端发送的提交请求时,进入准备阶段。在准备阶段,协调节点分别向多个子事务各自对应的参与节点发送收集请求。协调节点接收到各个参与节点返回的子事务的执行结果。Specifically, when the server receives the commit request sent by the client, it enters the preparation phase. In the preparation phase, the coordinating node sends a collection request to the participating nodes corresponding to each of the multiple sub-transactions. The coordinating node receives the execution results of the sub-transactions returned by each participating node.

在提交阶段,当各个子事务的执行结果均为执行成功时,将分布式事务的处理状态设置为:提交状态。当存在至少一个参与节点返回的子事务的执行结果为:执行失败时,将分布式事务的处理状态设置为:回滚状态。In the commit phase, when the execution results of each sub-transaction are all successful, the processing state of the distributed transaction is set to the commit state. When the execution result of the sub-transaction returned by at least one participating node is execution failure, the processing state of the distributed transaction is set to the rollback state.

在一些实施例中,在分布式数据库中,不同的数据项将被映射到不同的分区上,分区间通过多副本来保证高可用性。如果数据库节点使用两阶段提交协议进行并发控制,两阶段提交协议包括:准备阶段和提交阶段这两个阶段。然而,在执行阶段,参与节点会将子事务获取锁的情况发送给协调节点,而子事务在成功获得相应数据项的锁时,参与节点便可以对数据项执行子事务中的数据操作,获得执行结果。因此,协调节点实际上可以根据各子事务获取锁的情况确定事务的处理状态,也就是将执行阶段和准备阶段合并为一个阶段。In some embodiments, in a distributed database, different data items will be mapped to different partitions, and high availability is ensured between partitions through multiple copies. If the database node uses a two-phase commit protocol for concurrency control, the two-phase commit protocol includes two stages: a preparation stage and a commit stage. However, in the execution stage, the participating nodes will send the situation of the sub-transaction acquiring the lock to the coordination node, and when the sub-transaction successfully obtains the lock of the corresponding data item, the participating node can perform the data operation in the sub-transaction on the data item to obtain the execution result. Therefore, the coordination node can actually determine the processing status of the transaction based on the situation of each sub-transaction acquiring the lock, that is, merge the execution stage and the preparation stage into one stage.

鉴于此,协调节点接收多个子事务各自对应的参与节点返回的子事务处理消息;针对多个子事务,分别执行以下操作:从一个子事务对应的子事务处理消息中,获取该子事务关联的数据项的锁争用状态。若多个子事务各自对应的锁争用状态均为:已对关联的数据项加锁,则确定分布式事务的处理状态为:提交状态。In view of this, the coordinating node receives the sub-transaction processing messages returned by the participating nodes corresponding to each of the multiple sub-transactions; for the multiple sub-transactions, the following operations are performed respectively: from the sub-transaction processing message corresponding to a sub-transaction, the lock contention status of the data item associated with the sub-transaction is obtained. If the lock contention status corresponding to each of the multiple sub-transactions is: the associated data item has been locked, then the processing status of the distributed transaction is determined to be: committed status.

若多个子事务各自对应的锁争用状态中存在至少一个子事务的锁争用状态为:等待状态,则确定分布式事务的处理状态为:回滚状态。If the lock contention state of at least one sub-transaction among the lock contention states corresponding to the plurality of sub-transactions is: waiting state, then the processing state of the distributed transaction is determined to be: rollback state.

本申请实施例中,在执行阶段结束后,协调节点基于所有参与节点上报的子事务关联的数据项的锁争用状态,直接确定分布式事务的处理状态,相较于两阶段提交协议来说,协调节点与各个参与节点之间均减少了一个往返延时,从而提高了分布式事务处理效率,同时降低了分布式事务的锁争用时长,提高了分布式数据库的性能。In an embodiment of the present application, after the execution phase ends, the coordinating node directly determines the processing status of the distributed transaction based on the lock contention status of the data items associated with the sub-transactions reported by all participating nodes. Compared with the two-phase commit protocol, a round-trip delay is reduced between the coordinating node and each participating node, thereby improving the distributed transaction processing efficiency, while reducing the lock contention duration of the distributed transaction and improving the performance of the distributed database.

在一些实施例中,采用上述任意一种实施方式,确定分布式事务的处理状态之后,将分布式事务的处理状态,分别发送至多个子事务各自对应的参与节点,以使每个参与节点对相应的数据项进行解锁。In some embodiments, using any of the above implementations, after determining the processing status of the distributed transaction, the processing status of the distributed transaction is sent to the participating nodes corresponding to each of the multiple sub-transactions, so that each participating node unlocks the corresponding data item.

具体地,协调节点向各个参与节点发送解锁请求,参与节点在接收到解锁请求后,对相应的数据项进行解锁,并将解锁消息返回给协调节点。Specifically, the coordinating node sends an unlocking request to each participating node. After receiving the unlocking request, the participating node unlocks the corresponding data item and returns the unlocking message to the coordinating node.

举例来说,分布式事务T1的解锁流程如图6所示,包括以下步骤:For example, the unlocking process of distributed transaction T1 is shown in Figure 6, which includes the following steps:

①、数据库节点N1将解锁请求发送至数据库节点N2。①. Database node N1 sends an unlock request to database node N2.

②、数据库节点N1将解锁请求发送至数据库节点N3。②. Database node N1 sends the unlock request to database node N3.

③、数据库节点N2访问本地锁表,并对数据项X解锁。③. Database node N2 accesses the local lock table and unlocks data item X.

具体地,针对本地锁表执行以下操作:将数据项X的锁类型从共享锁SH修改为0,将锁拥有者从分布式事务T1修改为0。Specifically, the following operations are performed on the local lock table: the lock type of the data item X is changed from the shared lock SH to 0, and the lock owner is changed from the distributed transaction T1 to 0.

④、数据库节点N3访问本地锁表并对数据项Y解锁。④. Database node N3 accesses the local lock table and unlocks data item Y.

具体地,针对本地锁表执行以下操作:将数据项Y的锁类型从排它锁EX修改为共享锁SH,将锁拥有者从分布式事务T1修改为分布式事务Tx,将等待者从分布式事务Tx修改为0。Specifically, the following operations are performed on the local lock table: the lock type of the data item Y is changed from the exclusive lock EX to the shared lock SH, the lock owner is changed from the distributed transaction T1 to the distributed transaction Tx, and the waiter is changed from the distributed transaction Tx to 0.

⑤、数据库节点N2将解锁成功消息返回给协调节点。⑤. Database node N2 returns the unlock success message to the coordination node.

⑥、数据库节点N3将解锁成功消息返回给协调节点。⑥. Database node N3 returns the unlock success message to the coordination node.

本申请实施例中,在不额外增加分布式事务的总执行时长的情况下,适当延迟网络时延较短的子事务(快子事务)的执行时刻,这样快子事务和网络时延较长的子事务(慢子事务)可以在基本相同的时刻返回执行结果至协调节点,进而触发快子事务和慢子事务对应的数据项解锁,这样有效减少了快子事务因等待慢子事务产生的不必要的锁争用时长,从而提高了分布式事务的处理性能。In an embodiment of the present application, without additionally increasing the total execution time of the distributed transaction, the execution time of the sub-transaction with shorter network delay (fast sub-transaction) is appropriately delayed, so that the fast sub-transaction and the sub-transaction with longer network delay (slow sub-transaction) can return the execution results to the coordination node at basically the same time, thereby triggering the unlocking of the data items corresponding to the fast sub-transaction and the slow sub-transaction, which effectively reduces the unnecessary lock contention time caused by the fast sub-transaction waiting for the slow sub-transaction, thereby improving the processing performance of the distributed transaction.

为了更好地解释本申请实施例,下面结合具体实施场景介绍本申请实施例提供的一种分布式事务处理方法,该方法的流程可以由图1B所示的客户端101与服务端102交互执行。In order to better explain the embodiments of the present application, a distributed transaction processing method provided by the embodiments of the present application is introduced below in combination with a specific implementation scenario. The process of the method can be interactively executed by the client 101 and the server 102 shown in Figure 1B.

服务端包括三个数据库节点,分别为数据库节点N1、数据库节点N2和数据库节点N3,其中,数据库节点N1为协调节点,同时也是参与节点;数据库节点N2和数据库节点N3为参与节点。数据库节点N1与数据库节点N2之间的网络时延为10ms,数据库节点N1与数据库节点N3之间的网络时延为30ms。The server includes three database nodes, namely database node N1, database node N2 and database node N3, among which database node N1 is a coordination node and also a participating node; database node N2 and database node N3 are participating nodes. The network delay between database node N1 and database node N2 is 10ms, and the network delay between database node N1 and database node N3 is 30ms.

数据库节点N1接收客户端发送的4个事务请求,分别为分布式事务T1、分布式事务T2、分布式事务T3和单机事务T4。Database node N1 receives four transaction requests sent by the client, namely distributed transaction T1, distributed transaction T2, distributed transaction T3 and single-machine transaction T4.

分布式事务T1中的事务操作包括:R1(x)W1(y)W1(z)R1(v)W1(u),其中,R1(x)表示读取数据项x,W1(y)表示修改数据项y,W1(z)表示修改数据项z,R1(v)表示读取数据项v,W1(u)表示修改数据项u。The transaction operations in the distributed transaction T1 include: R 1 (x) W 1 (y) W 1 (z) R 1 (v) W 1 (u), where R 1 (x) represents reading data item x, W 1 (y) represents modifying data item y, W 1 (z) represents modifying data item z, R 1 (v) represents reading data item v, and W 1 (u) represents modifying data item u.

分布式事务T2中的事务操作包括:R2(x)W2(y)W2(z),其中,R2(x)表示读取数据项x,W2(y)表示修改数据项y,W2(z)表示修改数据项z。The transaction operations in the distributed transaction T2 include: R 2 (x) W 2 (y) W 2 (z), where R 2 (x) represents reading data item x, W 2 (y) represents modifying data item y, and W 2 (z) represents modifying data item z.

分布式事务T3中的事务操作包括:R3(x)R3(z),其中,R3(x)表示读取数据项x,R3(z)表示读取数据项z。The transaction operations in the distributed transaction T3 include: R 3 (x)R 3 (z), where R 3 (x) represents reading data item x, and R 3 (z) represents reading data item z.

单机事务T4中的事务操作包括:W4(x),W4(x)表示修改数据项x。The transaction operations in the stand-alone transaction T4 include: W 4 (x), where W 4 (x) represents modifying the data item x.

针对分布式事务T1,将分布式事务T1划分为:子事务T11、子事务T12、子事务T13,其中,子事务T11中的事务操作包括:R1(x)W1(y),子事务T12中的事务操作包括:W1(z),子事务T13中的事务操作包括:R1(v)W1(u)。For distributed transaction T1, distributed transaction T1 is divided into subtransaction T11 , subtransaction T12 , and subtransaction T13 , wherein the transaction operation in subtransaction T11 includes: R1 (x) W1 (y), the transaction operation in subtransaction T12 includes: W1 (z), and the transaction operation in subtransaction T13 includes: R1 (v) W1 (u).

子事务T11由数据库节点N1本地执行,子事务T12由数据库节点N2执行,子事务T13由数据库节点N3执行。Subtransaction T11 is executed locally by database node N1, subtransaction T12 is executed by database node N2, and subtransaction T13 is executed by database node N3.

在数据库节点本地执行子事务的执行时延忽略不计的情况下,分布式事务T1的总执行时延为:2·max{τc,j}=60ms;那么,基于上述公式(9)可知,子事务T11的目标发送时延DelayTime(T11)=60-0=60ms;子事务T12的目标发送时延DelayTime(T12)=60-20=40ms;子事务T13的目标发送时延DelayTime(T13)=60-60=0ms。When the execution delay of the subtransaction executed locally on the database node is ignored, the total execution delay of the distributed transaction T1 is: 2·max{τ c,j }=60ms. Then, based on the above formula (9), the target sending delay of subtransaction T11 is DelayTime( T11 )=60-0=60ms; the target sending delay of subtransaction T12 is DelayTime( T12 )=60-20=40ms; the target sending delay of subtransaction T13 is DelayTime( T13 )=60-60=0ms.

针对分布式事务T2,将分布式事务T2划分为:子事务T21、子事务T22,其中,子事务T21中的事务操作包括:R2(x)W2(y),子事务T22中的事务操作包括:W2(z)。For the distributed transaction T2, the distributed transaction T2 is divided into: sub-transaction T 21 and sub-transaction T 22 , wherein the transaction operation in the sub-transaction T 21 includes: R 2 (x) W 2 (y), and the transaction operation in the sub-transaction T 22 includes: W 2 (z).

子事务T21由数据库节点N1本地执行,子事务T22由数据库节点N2执行。Subtransaction T21 is executed locally by database node N1, and subtransaction T22 is executed by database node N2.

在数据库节点本地执行子事务的执行时延忽略不计的情况下,分布式事务T2的总执行时延:2·max{τc,j}=20ms;那么,基于上述公式(9)可知,子事务T21的目标发送时延DelayTime(T21)=20-0=20ms;子事务T22的目标发送时延DelayTime(T22)=20-20=0ms。When the execution delay of the subtransaction executed locally on the database node is ignored, the total execution delay of the distributed transaction T2 is: 2·max{τ c,j }=20ms. Then, based on the above formula (9), the target sending delay of subtransaction T 21 is DelayTime(T 21 )=20-0=20ms; the target sending delay of subtransaction T 22 is DelayTime(T 22 )=20-20=0ms.

针对分布式事务T3,将分布式事务T2划分为:子事务T31、子事务T32,其中,子事务T31中的事务操作包括:R3(x),子事务T32中的事务操作包括:R3(z)。With respect to the distributed transaction T3, the distributed transaction T2 is divided into: sub-transaction T31 and sub-transaction T32 , wherein the transaction operation in the sub-transaction T31 includes: R3 (x), and the transaction operation in the sub-transaction T32 includes: R3 (z).

子事务T31由数据库节点N1本地执行,子事务T32由数据库节点N2执行。Subtransaction T31 is executed locally by database node N1, and subtransaction T32 is executed by database node N2.

在数据库节点本地执行子事务的执行时延忽略不计的情况下,分布式事务T3的总执行时延:2·max{τc,j}=20ms;那么,基于上述公式(9)可知,子事务T31的目标发送时延DelayTime(T31)=20-0=20ms;子事务T32的目标发送时延DelayTime(T32)=20-20=0ms。When the execution delay of the subtransaction executed locally on the database node is ignored, the total execution delay of the distributed transaction T3 is: 2·max{τ c,j }=20ms. Then, based on the above formula (9), the target sending delay of subtransaction T 31 is DelayTime(T 31 )=20-0=20ms; the target sending delay of subtransaction T 32 is DelayTime(T 32 )=20-20=0ms.

针对单机事务T4,单机事务T4由数据库节点N1本地执行,目标发送时延DelayTime(T4)=0ms。For the stand-alone transaction T4, the stand-alone transaction T4 is executed locally by the database node N1, and the target sending delay DelayTime( T4 )=0ms.

在获得上述各个事务的目标发送时延后,采用以下执行流程执行各个事务,参见图7,包括以下步骤:After obtaining the target sending delay of each of the above transactions, the following execution process is used to execute each transaction, see FIG7 , including the following steps:

①、数据库节点N1在0ms时刻将子事务T13发送至数据库节点N3。①. Database node N1 sends subtransaction T13 to database node N3 at time 0ms.

数据库节点N3将本地锁表中数据项u和数据项v的锁拥有者改为分布式事务T1,然后对数据项u和数据项v执行子事务T13中的事务操作,获得子事务T13的执行结果。再将子事务T13的执行结果返回给数据库节点N1。Database node N3 changes the lock owner of data item u and data item v in the local lock table to distributed transaction T1, and then executes the transaction operation in subtransaction T13 on data item u and data item v to obtain the execution result of subtransaction T13 . Then, the execution result of subtransaction T13 is returned to database node N1.

另外,数据库节点N1在0ms时刻执行单机事务T4。In addition, database node N1 executes stand-alone transaction T4 at time 0 ms.

此时数据项x并没有加锁,因此将本地锁表中数据项x的锁拥有者改为单机事务T4,然后对数据项x执行单机事务T4中的事务操作,获得单机事务T4的执行结果。由于单机事务T4不需要等待其他数据库节点返回执行结果,因此,数据库节点N1可以立刻提交单机事务T4的执行结果,并释放数据项x的锁。At this time, data item x is not locked, so the lock owner of data item x in the local lock table is changed to stand-alone transaction T4, and then the transaction operation in stand-alone transaction T4 is executed on data item x to obtain the execution result of stand-alone transaction T4. Since stand-alone transaction T4 does not need to wait for other database nodes to return the execution result, database node N1 can immediately submit the execution result of stand-alone transaction T4 and release the lock of data item x.

②、数据库节点N1在0ms时刻将子事务T22和子事务T32发送至数据库节点N2。②. Database node N1 sends subtransaction T 22 and subtransaction T 32 to database node N2 at time 0 ms.

数据库节点N2在接收到子事务T22和子事务T32,选其中一个进行事务处理。假设先对子事务T22进行处理,子事务T32放入等待队列中。After receiving subtransaction T 22 and subtransaction T 32 , database node N2 selects one of them for transaction processing. Assume that subtransaction T 22 is processed first, and subtransaction T 32 is put into the waiting queue.

数据库节点N2将本地锁表中数据项z的锁拥有者改为分布式事务T2,然后对数据项z执行子事务T22中的事务操作,获得子事务T22的执行结果。Database node N2 changes the lock owner of data item z in the local lock table to distributed transaction T2, and then executes the transaction operation in subtransaction T22 on data item z to obtain the execution result of subtransaction T22 .

③、数据库节点N2将子事务T22的执行结果返回给数据库节点N1。③. Database node N2 returns the execution result of subtransaction T 22 to database node N1.

数据库节点N1在20ms时刻接收到数据库节点N2返回的子事务T22的执行结果。The database node N1 receives the execution result of the subtransaction T22 returned by the database node N2 at 20 ms.

数据库节点N1在20ms时刻对子事务T21进行事务处理,具体地,数据库节点N1在20ms时刻对数据项x和数据项y加锁(也就是将本地锁表中数据项x和数据项y的锁拥有者改为分布式事务T2),然后对数据项x和数据项y执行子事务T21中的事务操作,获得子事务T21的执行结果。结合子事务T22的执行结果和子事务T21的执行结果,确定分布式事务T2的处理状态。Database node N1 performs transaction processing on subtransaction T 21 at 20ms. Specifically, database node N1 locks data item x and data item y at 20ms (that is, changes the lock owner of data item x and data item y in the local lock table to distributed transaction T2), and then executes the transaction operation in subtransaction T 21 on data item x and data item y to obtain the execution result of subtransaction T 21. Combined with the execution result of subtransaction T 22 and the execution result of subtransaction T 21 , the processing status of distributed transaction T2 is determined.

数据库节点N1将本地锁表中数据项x的锁拥有者从分布式事务T2改为分布式事务T3,然后对数据项x执行子事务T31中的事务操作,获得子事务T31的执行结果。Database node N1 changes the lock owner of data item x in the local lock table from distributed transaction T2 to distributed transaction T3, and then executes the transaction operation in subtransaction T31 on data item x to obtain the execution result of subtransaction T31 .

④、数据库节点N1在20ms时刻发送解锁请求至数据库节点N2。④. Database node N1 sends an unlock request to database node N2 at 20ms.

数据库节点N2接收到解锁请求后,释放数据项z的锁。将本地锁表中数据项z的锁拥有者从分布式事务T2改为分布式事务T3,然后对数据项z执行子事务T32中的事务操作,获得子事务T32的执行结果。After receiving the unlock request, database node N2 releases the lock of data item z. The lock owner of data item z in the local lock table is changed from distributed transaction T2 to distributed transaction T3, and then the transaction operation in subtransaction T32 is executed on data item z to obtain the execution result of subtransaction T32 .

⑤、数据库节点N2将子事务T32的执行结果返回给数据库节点N1。⑤. Database node N2 returns the execution result of subtransaction T32 to database node N1.

数据库节点N1在40ms时刻接收到返回的子事务T32的执行结果。结合子事务T32的执行结果和子事务T31的执行结果,确定分布式事务T3的处理状态。At 40 ms, database node N1 receives the returned execution result of subtransaction T32 . Based on the execution result of subtransaction T32 and the execution result of subtransaction T31 , the processing status of distributed transaction T3 is determined.

⑥、数据库节点N1在40ms时刻发送解锁请求和子事务T12至数据库节点N2。⑥. Database node N1 sends an unlock request and subtransaction T12 to database node N2 at 40ms.

数据库节点N2释放数据项z的锁,将本地锁表中数据项z的锁拥有者从分布式事务T3改为分布式事务T1,然后对数据项z执行子事务T12中的事务操作,获得子事务T12的执行结果。Database node N2 releases the lock of data item z, changes the lock owner of data item z in the local lock table from distributed transaction T3 to distributed transaction T1, and then executes the transaction operation in subtransaction T12 on data item z to obtain the execution result of subtransaction T12 .

⑦、数据库节点N2将子事务T12的执行结果返回给数据库节点N1。⑦. Database node N2 returns the execution result of subtransaction T12 to database node N1.

数据库节点N1在60ms时刻接收到数据库节点N2返回的子事务T12的执行结果。Database node N1 receives the execution result of subtransaction T12 returned by database node N2 at 60 ms.

数据库节点N1在60ms时刻将本地锁表中数据项x和数据项y的锁拥有者改为分布式事务T1,然后对数据项x和数据项y执行子事务T11中的事务操作,获得子事务T11的执行结果。At 60 ms, database node N1 changes the lock owner of data item x and data item y in the local lock table to distributed transaction T1, and then executes the transaction operation in subtransaction T11 on data item x and data item y to obtain the execution result of subtransaction T11 .

⑧、数据库节点N1在60ms时刻接收到数据库节点N3返回的子事务T13的执行结果。⑧. Database node N1 receives the execution result of subtransaction T13 returned by database node N3 at 60ms.

结合子事务T11、子事务T12和子事务T13各自的执行结果,确定分布式事务T1的处理状态。The processing status of distributed transaction T1 is determined based on the execution results of subtransaction T11 , subtransaction T12 , and subtransaction T13 .

数据库节点N1发送解锁请求至数据库节点N2和数据库节点N3,数据库节点N2释放数据项z的锁,数据库节点N3释放数据项u和数据项v的锁,数据库节点N1本地释放数据项x和数据项y的锁。Database node N1 sends an unlock request to database node N2 and database node N3, database node N2 releases the lock of data item z, database node N3 releases the locks of data item u and data item v, and database node N1 locally releases the locks of data item x and data item y.

为了进一步验证本申请中的技术方案的性能,下面对本申请中的技术方案(优化后)与现有技术方案(优化前)各自对应的执行性能进行比较,具体参见以下表1:In order to further verify the performance of the technical solution in this application, the following is a comparison of the execution performance of the technical solution in this application (after optimization) and the existing technical solution (before optimization), as shown in Table 1 below:

表1Table 1

由上述表1可以看出,在现有技术方案(优化前)中,分布式事务T2~T4需要被阻塞,直到分布式事务T1执行完毕。而本申请中的技术方案(优化后)中,将分布式事务T1的子事务T11和子事务T12延迟执行,这样分布式事务T2~T4可以提前执行完,在不影响分布式事务T1的总执行时延的情况下,减少了分布式事务T1在数据项x、数据项y和数据项z上的锁争用时长,使得分布式事务T1~T4都可以在较小的时延内执行完成。It can be seen from Table 1 above that in the prior art solution (before optimization), distributed transactions T 2 to T 4 need to be blocked until distributed transaction T 1 is executed. In the technical solution (after optimization) in the present application, subtransactions T 11 and T 12 of distributed transaction T 1 are delayed for execution, so that distributed transactions T 2 to T 4 can be executed in advance, and the lock contention duration of distributed transaction T 1 on data item x, data item y and data item z is reduced without affecting the total execution delay of distributed transaction T 1 , so that distributed transactions T 1 to T 4 can be executed within a shorter delay.

从可用性方面来说,在跨空间域场景下,数据库节点间的网络时延和差异均比较大,本申请根据数据项的分布情况以及网络时延差异,在不影响分布式事务的总执行时延的基础上延迟执行部分子事务,以此来减少热点数据的锁争用时长,提高系统的并发度。值得注意的是,本申请并没有确定性执行的假设,因为不需要对事务进行批处理。其次,对于不存在逻辑谓词的存储过程、单语句的交互式事务以及多语句的交互式事务的第一条语句,均可以利用本申请提出的技术进行优化。From the perspective of availability, in cross-space domain scenarios, the network latency and differences between database nodes are relatively large. This application delays the execution of some sub-transactions based on the distribution of data items and network latency differences without affecting the total execution latency of distributed transactions, thereby reducing the lock contention duration of hot data and improving the concurrency of the system. It is worth noting that this application does not assume deterministic execution because transactions do not need to be batched. Secondly, for stored procedures that do not have logical predicates, single-statement interactive transactions, and the first statement of multi-statement interactive transactions, the technology proposed in this application can be used for optimization.

此外,分布式事务模型(Distributed Transaction Processing ReferenceModel,简称DTP)也是当前数据库应用中处理跨域事务的方法。在DTP模型中,客户端应用通过中间件连接各数据库并通过事务管理器(Transaction Manager,TM)完成XA/2PC以保证事务的原子性和隔离性。在DTP模型中,由中间件负责协调各个数据库中的事务,中间件在感知各目标节点的网络时延差异的基础上,利用本申请判断子事务的延迟时间并延迟发送子事务,同样可以达到减少锁争用时长、提高系统并发度的效果。因此,本申请在DTP模型中仍然是有效的。In addition, the Distributed Transaction Processing Reference Model (DTP) is also a method for processing cross-domain transactions in current database applications. In the DTP model, the client application connects to each database through the middleware and completes XA/2PC through the transaction manager (TM) to ensure the atomicity and isolation of the transaction. In the DTP model, the middleware is responsible for coordinating the transactions in each database. Based on the perception of the network delay differences of each target node, the middleware uses this application to determine the delay time of the sub-transaction and delay the sending of the sub-transaction, which can also achieve the effect of reducing the lock contention duration and improving the system concurrency. Therefore, this application is still valid in the DTP model.

基于相同的技术构思,本申请实施例提供了一种分布式事务处理装置的结构示意图,如图8所示,该装置800包括:Based on the same technical concept, an embodiment of the present application provides a structural diagram of a distributed transaction processing device, as shown in FIG8 , the device 800 includes:

划分模块801,用于将分布式事务划分为多个子事务,每个子事务对应一个参与节点;A division module 801 is used to divide a distributed transaction into multiple sub-transactions, each sub-transaction corresponds to a participating node;

处理模块802,用于针对所述多个子事务,分别执行以下操作:基于一个子事务对应的参与节点与所述协调节点之间的网络时延,获得所述一个子事务对应的目标发送时延,所述目标发送时延与所述网络时延呈负相关;The processing module 802 is used to perform the following operations respectively for the multiple sub-transactions: based on the network delay between the participating node corresponding to the sub-transaction and the coordinating node, obtain the target sending delay corresponding to the sub-transaction, wherein the target sending delay is negatively correlated with the network delay;

发送模块803,用于按照获得的多个目标发送时延,分别将所述多个子事务发送至相应的参与节点进行事务处理;A sending module 803, configured to send the plurality of sub-transactions to corresponding participating nodes for transaction processing respectively according to the obtained plurality of target sending delays;

所述处理模块802,还用于基于所述多个子事务各自对应的参与节点返回的子事务处理消息,获得所述分布式事务的处理状态。The processing module 802 is further configured to obtain the processing status of the distributed transaction based on the sub-transaction processing messages returned by the participating nodes corresponding to each of the multiple sub-transactions.

可选地,所述划分模块801具体用于:Optionally, the division module 801 is specifically used for:

基于所述分布式事务关联的多个数据项各自对应的参与节点,将所述分布式事务划分为多个子事务,其中,每个子事务对应至少一个数据项,且每个子事务对应一个参与节点。Based on participating nodes corresponding to each of the multiple data items associated with the distributed transaction, the distributed transaction is divided into multiple sub-transactions, wherein each sub-transaction corresponds to at least one data item and each sub-transaction corresponds to a participating node.

可选地,所述处理模块802具体用于:Optionally, the processing module 802 is specifically configured to:

获取所述多个子事务各自对应的参与节点,分别与所述协调节点之间的网络时延;Obtaining network delays between participating nodes corresponding to each of the multiple sub-transactions and the coordinating node;

从所述多个子事务各自对应的网络时延中获得最大网络时延;Obtaining a maximum network delay from the network delays corresponding to the multiple sub-transactions;

基于所述最大网络时延和所述一个子事务对应的网络时延,获得所述一个子事务对应的目标发送时延,其中,所述最大网络时延与所述一个子事务对应的网络时延之间的时延差值,与所述目标发送时延呈正相关。Based on the maximum network delay and the network delay corresponding to the subtransaction, a target sending delay corresponding to the subtransaction is obtained, wherein a delay difference between the maximum network delay and the network delay corresponding to the subtransaction is positively correlated with the target sending delay.

可选地,所述处理模块802具体用于:Optionally, the processing module 802 is specifically configured to:

基于所述一个子事务对应的网络时延,获得所述一个子事务对应的总执行时延;Based on the network delay corresponding to the subtransaction, obtaining a total execution delay corresponding to the subtransaction;

基于所述最大网络时延,获得所述分布式事务的总执行时延;Based on the maximum network delay, obtaining a total execution delay of the distributed transaction;

将所述分布式事务的总执行时延,与所述一个子事务对应的总执行时延之间的时延差值,作为所述一个子事务对应的目标发送时延。The delay difference between the total execution delay of the distributed transaction and the total execution delay corresponding to the sub-transaction is used as the target sending delay corresponding to the sub-transaction.

可选地,所述处理模块802具体用于:Optionally, the processing module 802 is specifically configured to:

基于所述一个子事务对应的网络时延,以及所述一个子事务对应的参与节点本地执行所述一个子事务的执行时延,获得所述一个子事务对应的目标发送时延,其中,所述执行时延与所述目标发送时延呈负相关。Based on the network delay corresponding to the sub-transaction and the execution delay of the participating node corresponding to the sub-transaction locally executing the sub-transaction, a target sending delay corresponding to the sub-transaction is obtained, wherein the execution delay is negatively correlated with the target sending delay.

可选地,所述处理模块802具体用于:Optionally, the processing module 802 is specifically configured to:

接收所述多个子事务各自对应的参与节点返回的子事务处理消息;Receiving sub-transaction processing messages returned by participating nodes corresponding to each of the multiple sub-transactions;

针对所述多个子事务,分别执行以下操作:从一个子事务对应的子事务处理消息中,获取所述一个子事务关联的数据项的锁争用状态;For the multiple subtransactions, the following operations are performed respectively: from a subtransaction processing message corresponding to a subtransaction, a lock contention state of a data item associated with the subtransaction is obtained;

若所述多个子事务各自对应的锁争用状态均为:已对关联的数据项加锁,则确定所述分布式事务的处理状态为:提交状态。If the lock contention states corresponding to the multiple subtransactions are all: the associated data items have been locked, then the processing state of the distributed transaction is determined to be: committed state.

可选地,所述处理模块802具体用于:Optionally, the processing module 802 is specifically configured to:

分别向所述多个子事务各自对应的参与节点发送收集请求,所述收集请求用于指示参与节点返回相应子事务的执行结果;Sending a collection request to each participating node corresponding to each of the multiple sub-transactions, wherein the collection request is used to instruct the participating node to return the execution result of the corresponding sub-transaction;

接收所述多个子事务各自对应的参与节点返回的子事务处理消息,所述子事务处理消息中包括:子事务的执行结果;Receiving sub-transaction processing messages returned by participating nodes corresponding to the multiple sub-transactions, wherein the sub-transaction processing messages include: execution results of the sub-transactions;

当所述多个子事务各自对应的执行结果均为:执行成功时,确定所述分布式事务的处理状态为:提交状态。When the execution results corresponding to the multiple sub-transactions are all: successful execution, it is determined that the processing status of the distributed transaction is: committed status.

所述发送模块803还用于:The sending module 803 is also used for:

基于所述多个子事务各自对应的参与节点返回的子事务处理消息,获得所述分布式事务的处理状态之后,将所述分布式事务的处理状态,分别发送至所述多个子事务各自对应的参与节点,以使每个参与节点对相应的数据项进行解锁。After obtaining the processing status of the distributed transaction based on the sub-transaction processing messages returned by the participating nodes corresponding to each of the multiple sub-transactions, the processing status of the distributed transaction is sent to the participating nodes corresponding to each of the multiple sub-transactions, so that each participating node unlocks the corresponding data item.

本申请实施例中,延迟执行网络时延较短的子事务,减少了网络时延较短的子事务对数据项的锁争用时长,同时,由于该子事务的网络时延较短,故相应的执行结果到达协调节点的时刻,与网络时延较长的子事务的执行结果到达协调节点的时刻相差不大,从而实现在不影响分布式事务执行的情况下,减少了热点数据的锁争用时长,从而提高了分布式系统的吞吐性能以及并发度。In an embodiment of the present application, the sub-transaction with a shorter network delay is executed delayed, thereby reducing the lock contention duration of the sub-transaction with a shorter network delay on the data item. At the same time, since the network delay of the sub-transaction is shorter, the time when the corresponding execution result arrives at the coordination node is not much different from the time when the execution result of the sub-transaction with a longer network delay arrives at the coordination node. This reduces the lock contention duration of hot data without affecting the execution of distributed transactions, thereby improving the throughput performance and concurrency of the distributed system.

基于相同的技术构思,本申请实施例提供了一种计算机设备,该计算机设备可以是图1B所示的服务端和/或客户端,如图9所示,包括至少一个处理器901,以及与至少一个处理器连接的存储器902,本申请实施例中不限定处理器901与存储器902之间的具体连接介质,图9中处理器901和存储器902之间通过总线连接为例。总线可以分为地址总线、数据总线、控制总线等。Based on the same technical concept, the embodiment of the present application provides a computer device, which may be the server and/or client shown in FIG1B , as shown in FIG9 , including at least one processor 901, and a memory 902 connected to at least one processor. The embodiment of the present application does not limit the specific connection medium between the processor 901 and the memory 902. FIG9 takes the example of the processor 901 and the memory 902 being connected via a bus. The bus can be divided into an address bus, a data bus, a control bus, etc.

在本申请实施例中,存储器902存储有可被至少一个处理器901执行的指令,至少一个处理器901通过执行存储器902存储的指令,可以执行上述分布式事务处理方法的步骤。In the embodiment of the present application, the memory 902 stores instructions that can be executed by at least one processor 901, and the at least one processor 901 can execute the steps of the above-mentioned distributed transaction processing method by executing the instructions stored in the memory 902.

其中,处理器901是计算机设备的控制中心,可以利用各种接口和线路连接计算机设备的各个部分,通过运行或执行存储在存储器902内的指令以及调用存储在存储器902内的数据,从而实现分布式事务处理。可选的,处理器901可包括一个或多个处理单元,处理器901可集成应用处理器和调制解调处理器,其中,应用处理器主要处理操作系统、用户界面和应用程序等,调制解调处理器主要处理无线通信。可以理解的是,上述调制解调处理器也可以不集成到处理器901中。在一些实施例中,处理器901和存储器902可以在同一芯片上实现,在一些实施例中,它们也可以在独立的芯片上分别实现。Among them, the processor 901 is the control center of the computer device, and can use various interfaces and lines to connect various parts of the computer device, and realize distributed transaction processing by running or executing instructions stored in the memory 902 and calling data stored in the memory 902. Optionally, the processor 901 may include one or more processing units, and the processor 901 may integrate an application processor and a modem processor, wherein the application processor mainly processes the operating system, user interface, and application programs, and the modem processor mainly processes wireless communications. It is understandable that the above-mentioned modem processor may not be integrated into the processor 901. In some embodiments, the processor 901 and the memory 902 may be implemented on the same chip, and in some embodiments, they may also be implemented separately on independent chips.

处理器901可以是通用处理器,例如中央处理器(CPU)、数字信号处理器、专用集成电路(Application Specific Integrated Circuit,ASIC)、现场可编程门阵列或者其他可编程逻辑器件、分立门或者晶体管逻辑器件、分立硬件组件,可以实现或者执行本申请实施例中公开的各方法、步骤及逻辑框图。通用处理器可以是微处理器或者任何常规的处理器等。结合本申请实施例所公开的方法的步骤可以直接体现为硬件处理器执行完成,或者用处理器中的硬件及软件模块组合执行完成。Processor 901 can be a general-purpose processor, such as a central processing unit (CPU), a digital signal processor, an application-specific integrated circuit (ASIC), a field programmable gate array or other programmable logic device, a discrete gate or transistor logic device, a discrete hardware component, and can implement or execute the methods, steps and logic block diagrams disclosed in the embodiments of the present application. A general-purpose processor can be a microprocessor or any conventional processor, etc. The steps of the method disclosed in the embodiments of the present application can be directly embodied as a hardware processor for execution, or can be executed by a combination of hardware and software modules in the processor.

存储器902作为一种非易失性计算机可读存储介质,可用于存储非易失性软件程序、非易失性计算机可执行程序以及模块。存储器902可以包括至少一种类型的存储介质,例如可以包括闪存、硬盘、多媒体卡、卡型存储器、随机访问存储器(Random AccessMemory,RAM)、静态随机访问存储器(Static Random Access Memory,SRAM)、可编程只读存储器(Programmable Read Only Memory,PROM)、只读存储器(Read Only Memory,ROM)、带电可擦除可编程只读存储器(Electrically Erasable Programmable Read-Only Memory,EEPROM)、磁性存储器、磁盘、光盘等等。存储器902是能够用于携带或存储具有指令或数据结构形式的期望的程序代码并能够由计算机设备存取的任何其他介质,但不限于此。本申请实施例中的存储器902还可以是电路或者其它任意能够实现存储功能的装置,用于存储程序指令和/或数据。The memory 902 is a non-volatile computer-readable storage medium that can be used to store non-volatile software programs, non-volatile computer executable programs and modules. The memory 902 may include at least one type of storage medium, such as a flash memory, a hard disk, a multimedia card, a card-type memory, a random access memory (Random Access Memory, RAM), a static random access memory (Static Random Access Memory, SRAM), a programmable read-only memory (Programmable Read Only Memory, PROM), a read-only memory (Read Only Memory, ROM), an electrically erasable programmable read-only memory (Electrically Erasable Programmable Read-Only Memory, EEPROM), a magnetic memory, a disk, an optical disk, etc. The memory 902 is any other medium that can be used to carry or store a desired program code in the form of an instruction or data structure and can be accessed by a computer device, but is not limited thereto. The memory 902 in the embodiment of the present application can also be a circuit or any other device that can realize a storage function, for storing program instructions and/or data.

基于同一发明构思,本申请实施例提供了一种计算机可读存储介质,其存储有可由计算机设备执行的计算机程序,当程序在计算机设备上运行时,使得计算机设备执行上述分布式事务处理方法的步骤。Based on the same inventive concept, an embodiment of the present application provides a computer-readable storage medium, which stores a computer program that can be executed by a computer device. When the program runs on the computer device, the computer device executes the steps of the above-mentioned distributed transaction processing method.

基于同一发明构思,本申请实施例提供了一种计算机程序产品,所述计算机程序产品包括存储在计算机可读存储介质上的计算机程序,所述计算机程序包括程序指令,当所述程序指令被计算机设备执行时,使所述计算机设备执行上述分布式事务处理方法的步骤。Based on the same inventive concept, an embodiment of the present application provides a computer program product, which includes a computer program stored on a computer-readable storage medium, and the computer program includes program instructions. When the program instructions are executed by a computer device, the computer device executes the steps of the above-mentioned distributed transaction processing method.

本领域内的技术人员应明白,本发明的实施例可提供为方法、或计算机程序产品。因此,本发明可采用完全硬件实施例、完全软件实施例、或结合软件和硬件方面的实施例的形式。而且,本发明可采用在一个或多个其中包含有计算机可用程序代码的计算机可用存储介质(包括但不限于磁盘存储器、CD-ROM、光学存储器等)上实施的计算机程序产品的形式。Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Furthermore, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

本发明是参照根据本发明实施例的方法、设备(系统)、和计算机程序产品的流程图和/或方框图来描述的。应理解可由计算机程序指令实现流程图和/或方框图中的每一流程和/或方框、以及流程图和/或方框图中的流程和/或方框的结合。可提供这些计算机程序指令到通用计算机、专用计算机、嵌入式处理机或其他可编程数据处理设备的处理器以产生一个机器,使得通过计算机设备或其他可编程数据处理设备的处理器执行的指令产生用于实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能的装置。The present invention is described with reference to the flowchart and/or block diagram of the method, device (system), and computer program product according to the embodiment of the present invention. It should be understood that each process and/or box in the flowchart and/or block diagram, as well as the combination of the process and/or box in the flowchart and/or block diagram can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer device or other programmable data processing device produce a device for implementing the functions specified in one or more processes in the flowchart and/or one or more boxes in the block diagram.

这些计算机程序指令也可存储在能引导计算机设备或其他可编程数据处理设备以特定方式工作的计算机可读存储器中,使得存储在该计算机可读存储器中的指令产生包括指令装置的制造品,该指令装置实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能。These computer program instructions may also be stored in a computer-readable memory that can direct a computer device or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce a manufactured product including an instruction device that implements the functions specified in one or more processes in the flowchart and/or one or more boxes in the block diagram.

这些计算机程序指令也可装载到计算机设备或其他可编程数据处理设备上,使得在计算机设备或其他可编程设备上执行一系列操作步骤以产生计算机设备实现的处理,从而在计算机设备或其他可编程设备上执行的指令提供用于实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能的步骤。These computer program instructions may also be loaded onto a computer device or other programmable data processing device so that a series of operating steps are executed on the computer device or other programmable device to produce a process implemented by the computer device, thereby the instructions executed on the computer device or other programmable device provide steps for implementing the functions specified in one or more processes in the flowchart and/or one or more boxes in the block diagram.

尽管已描述了本发明的优选实施例,但本领域内的技术人员一旦得知了基本创造性概念,则可对这些实施例作出另外的变更和修改。所以,所附权利要求意欲解释为包括优选实施例以及落入本发明范围的所有变更和修改。Although the preferred embodiments of the present invention have been described, those skilled in the art may make other changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the present invention.

显然,本领域的技术人员可以对本发明进行各种改动和变型而不脱离本发明的精神和范围。这样,倘若本发明的这些修改和变型属于本发明权利要求及其等同技术的范围之内,则本发明也意图包含这些改动和变型在内。Obviously, those skilled in the art can make various changes and modifications to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include these modifications and variations.

Claims (12)

1. A method of distributed transaction processing, comprising:
Dividing the distributed transaction into a plurality of sub-transactions, wherein each sub-transaction corresponds to one participating node;
for the plurality of sub-transactions, performing the following operations respectively: based on network time delay between a participating node corresponding to one sub-transaction and a coordinating node, obtaining target sending time delay corresponding to the one sub-transaction, wherein the target sending time delay and the network time delay are in negative correlation;
according to the obtained multiple target sending delays, the multiple sub-transactions are respectively sent to corresponding participating nodes for transaction processing;
and obtaining the processing state of the distributed transaction based on the sub-transaction processing information returned by the participating nodes corresponding to the sub-transactions.
2. The method of claim 1, wherein the dividing the distributed transaction into a plurality of sub-transactions comprises:
the distributed transaction is divided into a plurality of sub-transactions based on the participating nodes corresponding to the data items associated with the distributed transaction, wherein each sub-transaction corresponds to at least one data item and each sub-transaction corresponds to one participating node.
3. The method of claim 1, wherein the obtaining the target transmission delay corresponding to one sub-transaction based on the network delay between the participating node corresponding to the one sub-transaction and the coordinating node comprises:
Acquiring network time delay between each participating node corresponding to each sub-transaction and the coordinating node;
obtaining the maximum network delay from the network delays corresponding to the sub-transactions respectively;
and obtaining a target sending delay corresponding to the sub-transaction based on the maximum network delay and the network delay corresponding to the sub-transaction, wherein a delay difference value between the maximum network delay and the network delay corresponding to the sub-transaction is positively correlated with the target sending delay.
4. The method of claim 3, wherein the obtaining the target transmission delay for the one sub-transaction based on the maximum network delay and the network delay for the one sub-transaction comprises:
based on the network time delay corresponding to the sub-transaction, obtaining the total execution time delay corresponding to the sub-transaction;
based on the maximum network delay, obtaining the total execution delay of the distributed transaction;
and taking the time delay difference between the total execution time delay of the distributed transaction and the total execution time delay corresponding to one sub-transaction as the target sending time delay corresponding to the one sub-transaction.
5. The method of claim 1, wherein the obtaining the target transmission delay corresponding to one sub-transaction based on the network delay between the participating node corresponding to the one sub-transaction and the coordinating node comprises:
and obtaining a target sending delay corresponding to the sub-transaction based on the network delay corresponding to the sub-transaction and the execution delay of the sub-transaction locally executed by the participating node corresponding to the sub-transaction, wherein the execution delay is inversely related to the target sending delay.
6. The method according to any one of claims 1-5, wherein the obtaining the processing state of the distributed transaction based on the sub-transaction messages returned by the participating nodes to which the plurality of sub-transactions correspond respectively, comprises:
receiving sub-transaction processing information returned by the participating nodes corresponding to the sub-transactions respectively;
for the plurality of sub-transactions, performing the following operations respectively: acquiring the lock contention state of a data item associated with one sub-transaction from a sub-transaction processing message corresponding to the sub-transaction;
if the lock contention states corresponding to the plurality of sub-transactions are: having locked the associated data item, determining the processing state of the distributed transaction is: a commit status.
7. The method according to any one of claims 1-5, wherein the obtaining the processing state of the distributed transaction based on the sub-transaction messages returned by the participating nodes to which the plurality of sub-transactions correspond respectively, comprises:
respectively sending a collection request to each corresponding participating node of the plurality of sub-transactions, wherein the collection request is used for indicating the participating node to return the execution result of the corresponding sub-transaction;
receiving a sub-transaction processing message returned by a participating node corresponding to each of the plurality of sub-transactions, wherein the sub-transaction processing message comprises: the execution result of the sub-transaction;
when the execution results corresponding to the plurality of sub-transactions are all: and when the execution is successful, determining the processing state of the distributed transaction as follows: a commit status.
8. The method according to any one of claims 1-5, wherein after obtaining the processing state of the distributed transaction based on the sub-transaction messages returned by the participating nodes corresponding to the plurality of sub-transactions, further comprises:
and respectively sending the processing states of the distributed transactions to the participating nodes corresponding to the plurality of sub-transactions so as to unlock the corresponding data items by each participating node.
9. A distributed transaction processing apparatus, comprising:
the division module is used for dividing the distributed transaction into a plurality of sub-transactions, and each sub-transaction corresponds to one participation node;
the processing module is used for respectively executing the following operations for the plurality of sub-transactions: based on network time delay between a participating node corresponding to one sub-transaction and the coordination node, obtaining target sending time delay corresponding to the one sub-transaction, wherein the target sending time delay is in negative correlation with the network time delay;
the sending module is used for respectively sending the plurality of sub-transactions to corresponding participating nodes for transaction processing according to the obtained target sending delays;
the processing module is further configured to obtain a processing state of the distributed transaction based on sub-transaction processing messages returned by the participating nodes corresponding to the plurality of sub-transactions.
10. A computer device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, characterized in that the processor implements the steps of the method of any of claims 1-8 when the program is executed.
11. A computer readable storage medium, characterized in that it stores a computer program executable by a computer device, which program, when run on the computer device, causes the computer device to perform the steps of the method according to any one of claims 1-8.
12. A computer program product comprising a computer program stored on a computer readable storage medium, the computer program comprising program instructions which, when executed by a computer device, cause the computer device to carry out the steps of the method according to any one of claims 1 to 8.
CN202311268432.6A 2023-09-26 2023-09-26 Distributed transaction processing method, device, equipment and storage medium Pending CN117311913A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202311268432.6A CN117311913A (en) 2023-09-26 2023-09-26 Distributed transaction processing method, device, equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202311268432.6A CN117311913A (en) 2023-09-26 2023-09-26 Distributed transaction processing method, device, equipment and storage medium

Publications (1)

Publication Number Publication Date
CN117311913A true CN117311913A (en) 2023-12-29

Family

ID=89287919

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202311268432.6A Pending CN117311913A (en) 2023-09-26 2023-09-26 Distributed transaction processing method, device, equipment and storage medium

Country Status (1)

Country Link
CN (1) CN117311913A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN119621847A (en) * 2024-11-25 2025-03-14 国义招标股份有限公司 A big data processing method and system based on distributed microservice architecture

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN119621847A (en) * 2024-11-25 2025-03-14 国义招标股份有限公司 A big data processing method and system based on distributed microservice architecture

Similar Documents

Publication Publication Date Title
Mehdi et al. I {Can’t} Believe {It’s} Not Causal! Scalable Causal Consistency with No Slowdown Cascades
JP7549137B2 (en) Transaction processing method, system, device, equipment, and program
CN115098229B (en) Transaction processing method, device, node device and storage medium
CN111597015B (en) Transaction processing method and device, computer equipment and storage medium
US11822540B2 (en) Data read method and apparatus, computer device, and storage medium
EP2932370B1 (en) System and method for performing a transaction in a massively parallel processing database
US7801997B2 (en) Asynchronous interconnect protocol for a clustered DBMS
US20180173745A1 (en) Systems and methods to achieve sequential consistency in replicated states without compromising performance in geo-distributed, replicated services
CN111526186A (en) Distributed server cluster configuration method based on Raft
CN102724304A (en) Information warehouse federation in subscription/release system and data synchronization method
CN112527759B (en) Log execution method and device, computer equipment and storage medium
US20240354314A1 (en) Data processing method and system
US11061719B2 (en) High availability cluster management of computing nodes
WO2021022396A1 (en) Transaction processing for database distributed across regions
CN103064898B (en) Affairs locking, unlocking method and device
CN117311913A (en) Distributed transaction processing method, device, equipment and storage medium
Ciciani et al. Analysis of concurrency-coherency control protocols for distributed transaction processing systems with regional locality
Zhuang et al. GeoTP: Latency-Aware Geo-Distributed Transaction Processing in Database Middlewares
Erciyes Distributed mutual exclusion algorithms on a ring of clusters
Pankowski Consistency and availability of Data in replicated NoSQL databases
Bi et al. Parameterized algorithm for replicated objects with local reads
Islam Database consistency in cloud databases
Grov et al. Scalable and fully consistent transactions in the cloud through hierarchical validation
CN118656178A (en) A resource transfer method, device, equipment and system
Cheng et al. Concurrent Control and Performance Optimization of Distributed Transaction in Heterogeneous Network Environment

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination