WO2017181931A1 - 一种处理分布式事务的方法与设备 - Google Patents

一种处理分布式事务的方法与设备 Download PDF

Info

Publication number
WO2017181931A1
WO2017181931A1 PCT/CN2017/080846 CN2017080846W WO2017181931A1 WO 2017181931 A1 WO2017181931 A1 WO 2017181931A1 CN 2017080846 W CN2017080846 W CN 2017080846W WO 2017181931 A1 WO2017181931 A1 WO 2017181931A1
Authority
WO
WIPO (PCT)
Prior art keywords
target table
write lock
request
lock
transaction
Prior art date
Application number
PCT/CN2017/080846
Other languages
English (en)
French (fr)
Inventor
徐飞
刘汪根
代克
康毅
Original Assignee
星环信息科技(上海)有限公司
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 星环信息科技(上海)有限公司 filed Critical 星环信息科技(上海)有限公司
Publication of WO2017181931A1 publication Critical patent/WO2017181931A1/zh

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/23Updating
    • G06F16/2308Concurrency control
    • G06F16/2336Pessimistic concurrency control approaches, e.g. locking or multiple versions without time stamps
    • G06F16/2343Locking methods, e.g. distributed locking or locking implementation details
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR 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
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/07Responding to the occurrence of a fault, e.g. fault tolerance
    • G06F11/14Error detection or correction of the data by redundancy in operation
    • G06F11/1402Saving, restoring, recovering or retrying
    • G06F11/1415Saving, restoring, recovering or retrying at system level
    • G06F11/1438Restarting or rejuvenating
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/07Responding to the occurrence of a fault, e.g. fault tolerance
    • G06F11/16Error detection or correction of the data by redundancy in hardware
    • G06F11/20Error detection or correction of the data by redundancy in hardware using active fault-masking, e.g. by switching out faulty elements or by switching in spare elements
    • G06F11/202Error detection or correction of the data by redundancy in hardware using active fault-masking, e.g. by switching out faulty elements or by switching in spare elements where processing functionality is redundant
    • G06F11/2023Failover techniques
    • G06F11/203Failover techniques using migration
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/23Updating
    • G06F16/2379Updates performed during online database operations; commit processing
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/24Querying
    • G06F16/245Query processing
    • G06F16/2455Query execution
    • G06F16/24553Query execution of query operations
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR 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; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F2201/00Indexing scheme relating to error detection, to error correction, and to monitoring
    • G06F2201/80Database-specific techniques
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F2201/00Indexing scheme relating to error detection, to error correction, and to monitoring
    • G06F2201/805Real-time
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F2201/00Indexing scheme relating to error detection, to error correction, and to monitoring
    • G06F2201/82Solving problems relating to consistency
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F2201/00Indexing scheme relating to error detection, to error correction, and to monitoring
    • G06F2201/825Indexing scheme relating to error detection, to error correction, and to monitoring the problem or solution involving locking

Definitions

  • the present application relates to the field of computers, and in particular, to a technique for processing distributed transactions.
  • a transaction in a database is a logical unit of work in a database run.
  • a series of operations performed as a single logical unit of work are either completely executed or not executed at all.
  • Transaction processing ensures that data-oriented resources are not permanently updated unless all operations within the transactional unit complete successfully.
  • a logical unit of work must satisfy the so-called ACID (atomicity, consistency, isolation, and persistence) attributes; when multiple transactions are concurrent, how to avoid conflicts is related to system stability and user experience. Important question.
  • ACID atomicity, consistency, isolation, and persistence
  • a method for processing a distributed transaction at a first device side includes:
  • the first device includes a total node and a plurality of child nodes; wherein the step b includes: distributing, by the total node, the write lock request to a plurality of the child nodes, to the target The table performs write lock processing.
  • the method further includes: if the total node acquires a response greater than or equal to a predetermined number of the child nodes with respect to the write lock request within a predetermined time, the write lock process is successful.
  • processing of the write lock lock further includes: writing new lock status information to the log In the file; or the total node stores new lock status information.
  • the method further includes: restarting the child node that does not respond to the write lock request, the child node reads the latest lock state information from the log file; or restarts the unresponsive The child node that adds a write lock request, the total node sends the latest lock state information to the child node.
  • the restarting the child node that does not respond to the write lock request further comprises: if the child node that does not respond to the write lock request cannot be restarted, then the The child node's service is migrated to other physical machines and the services of the child nodes are started on the physical machine.
  • the step b includes: performing a write lock process on a part of the target table; wherein the method further comprises: sending a request for a partial write lock of the target table to the second device.
  • the performing a write lock process on the part of the target table includes: if the full table of the target table has been written with a lock, ignoring the write lock process on the part of the target table; otherwise, A part of the target table is subjected to a write lock process.
  • the method further includes: combining the plurality of versions of the target table when the number of versions of the target table is equal to or greater than a predetermined number of thresholds.
  • the method further includes: when acquiring a transaction commit statement, setting a state of the target version of the new version to be valid, unlocking the target table; or setting a new version of the target table when acquiring a transaction rollback statement The status is invalid and the target table is unlocked.
  • the step a further includes: acquiring a read lock request sent by the SQL compiler about the source table of the transaction to be processed; wherein the step b further comprises: performing, according to the read lock request, the The source table performs a read lock process.
  • the step c further includes: if the read lock process is successful, sending the to-be-processed version information of the source table to the SQL compiler; wherein the method further includes : Unlock the source table when getting a transaction commit statement or a transaction rollback statement.
  • the step B includes: querying, according to the partial write-lock request of the target table, whether the full table of the target table has been written-locked to the first device; The entire table is not padlocked, and the part of the target table is subjected to a write lock process.
  • the second device includes a total node and a plurality of child nodes; wherein the step B includes: distributing, by the total node, the request for a partial write lock on the target table to the plurality of the children The node performs a write lock process on a part of the target table.
  • a first device for processing a distributed transaction comprising:
  • a first device configured to acquire a write lock request sent by the SQL compiler about a target table of a transaction to be processed, where the transaction includes a plurality of SQL statements;
  • a second device configured to perform a write lock process on the target table based on the write lock request
  • a third device configured to send, to the SQL compiler, the to-be-processed version information of the target table, if the write-and-write lock processing is successful.
  • the device includes a total node and a plurality of child nodes, wherein the second device is configured to: distribute, by the total node, the write lock request to a plurality of the child nodes, to the target The table performs write lock processing.
  • the device further includes: a fourth device, configured to: if the total node acquires a response greater than or equal to a predetermined number of the child nodes with respect to the write lock request within a predetermined time, the adding The lock was processed successfully.
  • the successfully performing the write lock processing further comprises: writing new lock status information into the log file; or the total node stores new lock status information.
  • the device further includes: a fifth device, configured to restart the child node that does not respond to the write lock request, the child node reads the latest lock state information from the log file Or a sixth device, configured to restart the child node that does not respond to the write lock request, and the total node sends the latest lock state information to the child node.
  • the restarting the child node that does not respond to the write lock request further comprises: if the child node that does not respond to the write lock request cannot be restarted, then the The child node's service is migrated to other physical machines and the services of the child nodes are started on the physical machine.
  • the second device is configured to: perform a write lock process on a part of the target table; wherein the device further includes: a seventh device, configured to send, to the second device, a part about the target table Add a lock request.
  • the performing a write lock process on the part of the target table includes: if the full table of the target table has been written with a lock, ignoring the write lock process on the part of the target table; otherwise, A part of the target table is subjected to a write lock process.
  • the device further includes: an eighth device, configured to perform a combining process on the plurality of versions of the target table when the number of versions of the target table is equal to or greater than a predetermined version number threshold.
  • the device further includes: a ninth device, configured to: when acquiring a transaction commit statement, setting a state of the target table of the new version to be valid, unlocking the target table; or tenth device, for acquiring a transaction The rollback statement sets the state of the target table of the new version to invalid, and unlocks the target table.
  • the first device is further configured to: acquire a read lock request sent by the SQL compiler about a source table of the transaction to be processed; wherein the second device is further configured to: based on the read lock request And the third device is further configured to: send the to-be-processed version information of the source table to the SQL compiler if the read lock processing is successful;
  • the device further includes: an eleventh device, configured to unlock the source table when acquiring a transaction commit statement or a transaction rollback statement.
  • a second device for processing a distributed transaction wherein the device comprises:
  • a twelfth device configured to acquire a request for a partial write lock of the target table sent by the first device
  • a thirteenth device configured to perform a write lock process on a portion of the target table according to the request for a partial write lock on the target table.
  • the thirteenth device is configured to: according to the request for a partial write lock on the target table, query the first device whether the full table of the target table has been written and locked; The entire table of the target table is not padlocked, and the part of the target table is subjected to write lock processing.
  • the device includes a total node and a plurality of child nodes, wherein the thirteenth device is configured to: distribute, by the total node, the request for a partial write lock on the target table to the plurality of The child node performs a write lock process on a part of the target table.
  • a system for processing a distributed transaction comprising a first device as described above and a second device as described above.
  • the first device of the present application is a group consisting of a plurality of nodes, and the first device acquires a target table sent by the SQL compiler about a transaction to be processed.
  • Write a lock request and perform a write lock process on the target table based on the write lock request, thereby avoiding conflicts when multiple transactions are concurrent, ensuring isolation of the transaction, and if the write lock is successfully processed,
  • the pending version information of the target table is sent to the SQL compiler to ensure that the subsequent SQL compiler operates on the correct version of the target table.
  • the present application restarts the child node that does not respond to the write lock request, or migrates the service of the child node to another physical machine, and then updates the lock state information of the child node, thereby performing Effective error recovery and guarantee intra-group consistency. Further, in a scenario where the number is small and the degree of concurrency is high, the application performs a write lock process on the target table of the transaction by using a system consisting of the first device and the second device, by the second The device performs a write-and-lock process on the part of the target table, thereby improving the efficiency of the system.
  • FIG. 1 is a structural diagram of a first device according to an embodiment of the present application.
  • FIG. 2 shows a flow chart of a method of processing a distributed transaction in accordance with an aspect of the present application
  • FIG. 3 illustrates a flow chart of a method for processing a distributed transaction according to an embodiment of the present application
  • FIG. 4 shows a flow chart of a method for processing distributed transactions in accordance with another embodiment of the present application.
  • FIG. 5 shows a schematic diagram of a first device for processing a distributed transaction according to another aspect of the present application
  • FIG. 6 shows a schematic diagram of a first device for processing a distributed transaction, in accordance with an embodiment of the present application
  • FIG. 7 shows a schematic diagram of a first device and a second device for processing distributed transactions, according to another embodiment of the present application.
  • the terminal, the device of the service network, and the trusted party each include one or more processors (CPUs), input/output interfaces, network interfaces, and memory.
  • processors CPUs
  • input/output interfaces network interfaces
  • memory volatile and non-volatile memory
  • the memory may include non-persistent memory, random access memory (RAM), and/or non-volatile memory in a computer readable medium, such as read only memory (ROM) or flash memory.
  • RAM random access memory
  • ROM read only memory
  • Memory is an example of a computer readable medium.
  • Computer readable media includes both permanent and non-persistent, removable and non-removable media.
  • Information storage can be implemented by any method or technology.
  • the information can be computer readable instructions, data structures, modules of programs, or other data.
  • Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read only memory. (ROM), electrically erasable programmable read only memory (EEPROM), flash memory or other memory technology, compact disk read only memory (CD-ROM), digital versatile disk (DVD) or other optical storage,
  • computer readable media does not include non-transitory computer readable media, such as modulated data signals and carrier waves.
  • FIG. 2 shows a flow diagram of a method for processing distributed transactions in accordance with an aspect of the present application.
  • the method includes step S101, step S102, and step S103.
  • step S101 the first device 1 acquires a write lock request sent by the SQL compiler about a target table of a transaction to be processed, wherein the transaction includes a plurality of SQL statements; in step S102, The first device 1 performs a write lock process on the target table based on the write lock request; in step S103, the first device 1 compiles to the SQL if the write lock process is successful.
  • the device sends the pending version information of the target table.
  • the first device 1 includes but is not limited to a user equipment, a network device, or a user equipment.
  • a device that is integrated with a network device through a network includes, but is not limited to, any mobile electronic product that can interact with a user through a touchpad, such as a smart phone, a tablet computer, etc., and the mobile electronic product can adopt any operating system, such as an android operating system. , iOS operating system, etc.
  • the network device includes an electronic device capable of automatically performing numerical calculation and information processing according to an instruction set or stored in advance, and the hardware includes but is not limited to a microprocessor, an application specific integrated circuit (ASIC), and a programmable gate. Arrays (FPGAs), digital processors (DSPs), embedded devices, and more.
  • the network device includes, but is not limited to, a computer, a network host, a single network server, a plurality of network server sets, or a plurality of servers; wherein the cloud is composed of a large number of computers or network servers based on Cloud Computing Among them, cloud computing is a kind of distributed computing, a virtual supercomputer composed of a group of loosely coupled computers.
  • the network includes, but is not limited to, the Internet, a wide area network, a metropolitan area network, a local area network, a VPN network, a wireless ad hoc network (Ad Hoc network), and the like.
  • the first device 1 may also be a script program running on the user equipment, the network device, or the user equipment and the network device, the network device, the touch terminal, or the network device and the touch terminal integrated through the network. .
  • the foregoing first device 1 is only an example, and other existing devices that may be present or may appear in the future, as applicable to the present application, are also included in the scope of protection of the present application, and This is hereby incorporated by reference.
  • step S101 the first device 1 acquires a write lock request sent by the SQL compiler regarding a target table of a transaction to be processed, wherein the transaction includes a plurality of SQL statements.
  • the components of the SQL language include the DCL language (Database Control Language).
  • the transaction begins with the DCL statement begin transaction and ends with the DCL statement commit (transaction commit) or rollback (transaction rollback).
  • DCL statement commit commit
  • rollback transaction rollback
  • the target table refers to a table in which the SQL statement is to be written.
  • the write lock requests corresponding to each SQL statement in the transaction are independently performed.
  • the acquired SQL statement is first compiled by the SQL compiler to obtain a table and an execution plan involved in the SQL statement, and then the SQL compiler sends the first device 1 about the
  • the write lock request of the target table of the SQL statement in the transaction avoids the conflict of multiple transactions when the lock is concurrent, and ensures the isolation of the transaction (Isolation).
  • the first device 1 further acquires a read lock request sent by the SQL compiler about the source table of the transaction to be processed.
  • the source table refers to a table in which the SQL statement is to be read.
  • the SQL statement has both a target table and an active table, and the SQL compiler also needs to send a read lock request to the source table of the SQL statement.
  • the SQL statement only needs no source table for the target table, and the SQL compiler does not need to send a read lock request to the source table of the SQL statement.
  • step S102 the first device 1 performs a write lock process on the target table based on the write lock request.
  • the target table of the SQL statement of the transaction is subjected to write lock processing.
  • the target table to which the lock has been written cannot be written by other transactions until the write lock is released.
  • the first device 1 further performs read lock processing on the source table based on the read lock request.
  • the SQL statement has both a target table and an active table, it is also necessary to perform read lock processing on the source table based on the read lock request.
  • the first device 1 includes a total node and a plurality of child nodes; wherein, in step S102, the first device 1 distributes the write lock request to the plurality of the child through the total node The node performs a write lock process on the target table.
  • a physical machine such as a server or the like
  • the first device 1 may be a group composed of programs running on a plurality of physical machines; In (group), one of the physical machines is selected as the total node, and the remaining physical machines are selected as child nodes.
  • the total node is responsible for broadcasting the write lock request to all the child nodes in the group to perform write lock processing on the target table.
  • the write lock request is processed in batches in a scenario where the amount of data is large and the degree of concurrency is not high.
  • step S103 the first device 1 sends the pending version information of the target table to the SQL compiler if the write lock processing is successful.
  • the result of each CRUD operation is a new version of the target table for that CRUD operation. Since CRUD operations, there will usually be multiple versions of a table.
  • the pending version information of the target table is returned to the SQL compiler to ensure that the subsequent SQL compiler operates on the correct version of the target table.
  • the first device 1 sends the to-be-processed version information of the source table to the SQL compiler if the read lock processing is successful.
  • the pending version information of the source table needs to be returned to the SQL compiler to ensure that the subsequent SQL compiler operates on the correct version of the source table.
  • the method further includes: the first device 1 performs a merge process on the plurality of versions of the target table when the number of versions of the target table is equal to or greater than a predetermined version number threshold.
  • the number of versions of the target table is continuously increasing over time.
  • the number of versions of the target table is equal to or greater than a predetermined number of thresholds, several versions of the target table are merged to obtain a new version, and the previous version is cleared.
  • the method further comprises: when the first device 1 acquires a transaction commit statement, sets a state of the target version of the new version to be valid, unlocks the target table; and when the transaction rollback statement is obtained, sets a new version The state of the target table is invalid, and the target table is unlocked.
  • the write operation of the transaction to the target table is marked as valid, and all locks possessed by the transaction are released, that is, the target table is unlocked; when the DCL is acquired
  • the statement rollback (the transaction rollback) marks all writes of the transaction to the target table as invalid, and releases all locks possessed by the transaction, that is, unlocks the target table. This ensures the atomicity and consistency of the transaction.
  • the method further comprises: the first device 1 unlocking the source table when acquiring a transaction commit statement or a transaction rollback statement.
  • FIG. 3 illustrates a flow diagram of a method for processing distributed transactions in accordance with one embodiment of the present application.
  • the method includes a step S101', a step S102', a step S104', and a step S103'.
  • the steps S101' and S103' are the same as or substantially the same as the steps S101 and S103 in FIG. 1.
  • details are not described herein again.
  • step S102' the first device 1 distributes the write lock request to a plurality of the child nodes by the total node based on the write lock request, and performs the target table on the target table. Adding a lock processing; in step S104', the first device 1 if the total node acquires a response greater than or equal to a predetermined number of the child nodes with respect to the write lock request within a predetermined time, Add write lock processing is successful.
  • the total node is responsible for broadcasting the write lock request to all of the child nodes in the group and waiting for the child node to respond to the write lock request only when received greater than or equal to the predetermined number
  • the write-lock processing is successful if the child node responds to the write-lock request. If the total node fails to receive enough response of the child node to the write lock request within a predetermined time, the lock fails; at this time, the total node may have two choices: Waiting for a period of time to continue to send the write lock request until the threshold of the number of lock attempts is reached. If the lock is not successful at this time, the lock failure information is returned; the other is to directly return the lock failure information. After receiving the lock failure information, the SQL compiler needs to retry, that is, resend the write lock request.
  • the successfully writing the lock processing further comprises: writing new lock status information into the log file; or the total node stores new lock status information.
  • the write lock process is successful and the new lock state information is saved.
  • the new lock state information there are two ways to save the new lock state information, one is to write new lock state information into the log file, and the other is that the total node stores new lock state information.
  • the method further comprises: the first device 1 restarting the child node that does not respond to the write lock request, the child node reading the latest lock state information from the log file Or restarting the child node that does not respond to the write lock request, and the total node sends the latest lock status information to the child node.
  • the child node if the child node does not respond (possibly due to a physical machine crash, etc.) the write lock request, the child node that does not respond to the write lock request needs to be restarted, and the child After the node restarts, it needs to update its own state information so that it can interact with other members in the group. (including the total node and the child nodes) are consistent.
  • the child node also has two ways of updating the state information, one is that the child node reads the latest lock state information from the log file, and the other is that the total node is up to date.
  • the locked state information is sent to the child node.
  • the new total node is reselected from the remaining of the child nodes in the group.
  • the restarting the child node that does not respond to the write lock request further comprises: if the child node that does not respond to the write lock request cannot be restarted, then the The service of the child node is migrated to other physical machines, and the services of the child nodes are started on the physical machine.
  • the child node that does not respond to the write lock request cannot be restarted, and at this time, the service of the child node may be migrated to other physical machines by the total node. And start the service of the child node on the physical machine. Then, the child node on the physical machine can update its own state information in the above two ways to be consistent with other members in the group, including the total node and the child node.
  • the present application updates the locked state of the child node by restarting the child node that does not respond to the write lock request, or migrating the service of the child node to another physical machine. Information for effective error recovery and consistency within the group.
  • FIG. 4 shows a flow chart of a method for processing distributed transactions in accordance with another embodiment of the present application.
  • the method includes a step S101", a step S102", a step S107” and a step S103", a step S212" and a step S213".
  • the steps S101" and S103" are the same as or substantially the same as the steps S101 and S103 in FIG. 1.
  • details are not described herein again.
  • step S102 the first device 1 performs a write lock process on a portion of the target table based on the write lock request; in step S107", the first device 1 The second device 2 sends a request for a partial write lock on the target table.
  • the SQL compiler may further refine the granularity of the lock, and send a write lock request to the first device 1 for a certain part of the target table.
  • the first device 1 responds to the write lock request in units of a table, and forwards the write lock request in units of a certain portion of the table. Specifically, sending the relevant information to the second device 2 A request to write a lock on a portion of the target table.
  • the performing a write lock process on the part of the target table comprises: if the full table of the target table has been written with a lock, ignoring the write lock process on the part of the target table; otherwise, A part of the target table is subjected to a write lock process.
  • the current transaction has acquired the write lock of the full table of the target table, and it is not necessary to acquire the part of the target table again.
  • the lock is written, so there is no need to perform a write lock process on the part of the target table at this time.
  • step S212 the second device 2 acquires a request for a partial write lock of the target table sent by the first device 1; in step S213", the second device 2 is based on the portion related to the target table A write lock request is applied to the part of the target table for write lock processing.
  • the second device 2 performs a write lock process on a portion of the target table according to a request for a partial write lock of the target table sent by the first device 1.
  • the second device 2 includes a total node and a plurality of child nodes; wherein, in step S213", the second device 2 requests the portion of the target table to be locked by the total node It is distributed to a plurality of the child nodes, and a part of the target table is subjected to a write lock process.
  • the second device 2 may also be a group consisting of programs running on a plurality of physical machines; one of the physical machines is selected as a total node in the group, and the remaining physical machines are Child node.
  • the total node is responsible for broadcasting the request for the partial write lock of the target table to all the child nodes in the group. To perform a write lock process on a portion of the target table.
  • the second device 2 queries the first device whether the full table of the target table has been written and locked according to the request for a partial write lock on the target table; If the full table of the target table is not locked, the part of the target table is subjected to a write lock process.
  • the first device 1 may be queried whether the full table of the target table has been locked or not; The full table of the table has been written and locked by the first device 1, and there is no need to perform a write lock process on the portion of the target table.
  • FIG. 5 illustrates a first device 1 for processing a distributed transaction, wherein the first device 1 includes a first device 101, a second device 102, and a third device 103, in accordance with another aspect of the present application.
  • the first device 101 acquires a write lock request sent by the SQL compiler about a target table of a transaction to be processed, wherein the transaction includes a plurality of SQL statements; the second device 102 is based on the adding Writing a lock request, performing a write lock process on the target table; the third device 103, if the write lock process is successful, sends the pending version information of the target table to the SQL compiler.
  • the first device 1 includes, but is not limited to, a user equipment, a network device, or a device formed by integrating a user equipment and a network device through a network.
  • the user equipment includes, but is not limited to, any mobile electronic product that can interact with a user through a touchpad, such as a smart phone, a tablet computer, etc., and the mobile electronic product can adopt any operating system, such as an android operating system. , iOS operating system, etc.
  • the network device includes an electronic device capable of automatically performing numerical calculation and information processing according to an instruction set or stored in advance, and the hardware includes but is not limited to a microprocessor, an application specific integrated circuit (ASIC), and a programmable gate.
  • ASIC application specific integrated circuit
  • the network device includes, but is not limited to, a computer, a network host, a single network server, a plurality of network server sets, or a plurality of servers; wherein the cloud is composed of a large number of computers or network servers based on Cloud Computing Among them, cloud computing is a kind of distributed computing, a virtual supercomputer composed of a group of loosely coupled computers.
  • the network includes, but is not limited to, the Internet, a wide area network, a metropolitan area network, a local area network, a VPN network, a wireless ad hoc network (Ad Hoc network), and the like.
  • the first device 1 may also be a script program running on the user equipment, the network device, or the user equipment and the network device, the network device, the touch terminal, or the network device and the touch terminal integrated through the network. .
  • the foregoing first device 1 is only an example, and other existing devices that may be present or may appear in the future, as applicable to the present application, are also included in the scope of protection of the present application, and This is hereby incorporated by reference.
  • the first device 101 acquires a write lock request sent by the SQL compiler about a target table of a transaction to be processed, wherein the transaction includes a plurality of SQL statements.
  • the components of the SQL language include the DCL language (Database Control Language).
  • the transaction begins with the DCL statement begin transaction and ends with the DCL statement commit (transaction commit) or rollback (transaction rollback).
  • the target table refers to a table in which the SQL statement is to be written.
  • the write lock requests corresponding to each SQL statement in the transaction are independently performed.
  • the acquired SQL statement is first compiled by the SQL compiler to obtain a table and an execution plan involved in the SQL statement, and then the SQL compiler sends a target for the SQL statement in the transaction to the first device 1.
  • the write-and-write request of the table avoids conflicts in the case of multiple transactions concurrently by locking, ensuring the isolation of the transaction (Isolation).
  • the first device 101 also acquires a read lock request sent by the SQL compiler about the source table of the transaction to be processed.
  • the source table refers to a table in which the SQL statement is to be read.
  • the SQL statement has both a target table and an active table, and the SQL compiler also needs to send a read lock request to the source table of the SQL statement.
  • the SQL statement only needs no source table for the target table, and the SQL compiler does not need to send a read lock request to the source table of the SQL statement.
  • the second device 102 performs a write lock process on the target table based on the write lock request.
  • the target table of the SQL statement of the transaction is subjected to write lock processing.
  • the target table to which the lock has been written cannot be written by other transactions until the write lock is released.
  • the second device 102 further performs read lock processing on the source table based on the read lock request.
  • the SQL statement has both a target table and an active table, it is also necessary to perform read lock processing on the source table based on the read lock request.
  • the first device 1 includes a total node and a plurality of child nodes; wherein the second device 102 distributes the write lock request to the plurality of child nodes through the total node,
  • the target table is subjected to write lock processing.
  • a physical machine such as a server or the like
  • the first device 1 may be a group composed of programs running on a plurality of physical machines; In (group), one of the physical machines is selected as the total node, and the remaining physical machines are selected as child nodes.
  • the write lock request sent by the SQL compiler is obtained, the total node is responsible for the write lock
  • the request is broadcast to all of the child nodes in the group to perform a write lock process on the target table.
  • the write lock request is processed in batches in a scenario where the amount of data is large and the degree of concurrency is not high.
  • the third device 103 sends the pending version information of the target table to the SQL compiler if the write lock processing is successful.
  • the result of each CRUD operation is a new version of the target table for that CRUD operation. Since CRUD operations, there will usually be multiple versions of a table.
  • the pending version information of the target table is returned to the SQL compiler to ensure that the subsequent SQL compiler operates on the correct version of the target table.
  • the third device 103 sends the to-be-processed version information of the source table to the SQL compiler if the read lock processing is successful.
  • the pending version information of the source table needs to be returned to the SQL compiler to ensure that the subsequent SQL compiler operates on the correct version of the source table.
  • the first device 1 further includes an eighth device (not shown); the eighth device has a number of versions of the target table equal to or greater than a predetermined version number threshold, and a number of the target table The versions are merged.
  • the number of versions of the target table is continuously increasing over time.
  • the number of versions of the target table is equal to or greater than a predetermined number of thresholds, several versions of the target table are merged to obtain a new version, and the previous version is cleared.
  • the first device 1 further includes a ninth device (not shown) or a tenth device (not shown); the ninth device acquires a transaction commit statement, sets a new version of the The state of the target table is valid, and the target table is unlocked; the tenth device acquires a transaction rollback statement, sets a state of the target table of the new version to be invalid, and unlocks the target table.
  • the ninth device acquires a transaction commit statement, sets a new version of the The state of the target table is valid, and the target table is unlocked
  • the tenth device acquires a transaction rollback statement, sets a state of the target table of the new version to be invalid, and unlocks the target table.
  • the first device 1 further includes an eleventh device (not shown); the eleventh device unlocks the source table when acquiring a transaction commit statement or a transaction rollback statement.
  • FIG. 6 illustrates a first device 1 for processing a distributed transaction, wherein the first device 1 includes a first device 101', a second device 102', a fourth device 104', and Third device 103'.
  • first device 101' and the third device 103' are the same as or substantially the same as the first device 101 and the third device 103 in FIG. 4, and are not described again for the sake of brevity.
  • the second device 102' distributes the write lock request to the plurality of child nodes by the total node based on the write lock request, and performs a write lock process on the target table;
  • the fourth device 104' if the total node acquires a response greater than or equal to a predetermined number of the child nodes with respect to the write lock request within a predetermined time, the write lock processing is successful.
  • the total node is responsible for broadcasting the write lock request to all of the child nodes in the group and waiting for the child node to respond to the write lock request only when received greater than or equal to the predetermined number
  • the write-lock processing is successful if the child node responds to the write-lock request. If the total node fails to receive enough response of the child node to the write lock request within a predetermined time, the lock fails; at this time, the total node may have two choices: Waiting for a period of time to continue to send the write lock request until the threshold of the number of lock attempts is reached. If the lock is not successful at this time, the lock failure information is returned; the other is to directly return the lock failure information. After receiving the lock failure information, the SQL compiler needs to retry, that is, resend the write lock request.
  • the successfully writing the lock processing further comprises: writing new lock status information into the log file; or the total node stores new lock status information.
  • the write lock process is successful and the new lock state information is saved.
  • the new lock state information there are two ways to save the new lock state information, one is to write new lock state information into the log file, and the other is that the total node stores new lock state information.
  • the first device 1 further includes a fifth device (not shown) and a sixth device (not shown in the figure); the fifth device restarts the child node that does not respond to the write lock request, the child node reads the latest lock state information from the log file; or The sixth device restarts the child node that does not respond to the write lock request, and the total node sends the latest lock state information to the child node.
  • the fifth device restarts the child node that does not respond to the write lock request, the child node reads the latest lock state information from the log file; or
  • the sixth device restarts the child node that does not respond to the write lock request, and the total node sends the latest lock state information to the child node.
  • the child node if the child node does not respond (possibly due to a physical machine crash, etc.) the write lock request, the child node that does not respond to the write lock request needs to be restarted, and the child After the node restarts, it needs to update its own state information to be consistent with other members in the group, including the total node and the child nodes.
  • the child node also has two ways of updating the state information, one is that the child node reads the latest lock state information from the log file, and the other is that the total node is up to date.
  • the locked state information is sent to the child node.
  • the new total node is reselected from the remaining of the child nodes in the group.
  • the restarting the child node that does not respond to the write lock request further comprises: if the child node that does not respond to the write lock request cannot be restarted, then the The service of the child node is migrated to other physical machines, and the services of the child nodes are started on the physical machine.
  • the child node that does not respond to the write lock request cannot be restarted, and at this time, the service of the child node may be migrated to other physical machines by the total node. And start the service of the child node on the physical machine. Then, the child node on the physical machine can update its own state information in the above two ways to be consistent with other members in the group, including the total node and the child node.
  • the present application updates the locked state of the child node by restarting the child node that does not respond to the write lock request, or migrating the service of the child node to another physical machine. Information for effective error recovery and consistency within the group.
  • FIG. 7 illustrates a first device 1 and a second device 2 for processing distributed transactions according to another embodiment of the present application, wherein the first device 1 includes a first device 101", a second device 102", The seventh device 107" and the third device 103", the second device 2 includes a twelfth device 212" and a thirteenth device 213".
  • the first device 101 the third device 103" and the first device 101 in FIG. 4,
  • the content of the third device 103 is the same or substantially the same, and will not be described again for the sake of brevity.
  • the second device 102" performs a write lock process on a portion of the target table based on the write lock request; the seventh device 107" sends a portion about the target table to a second device Add a lock request.
  • the SQL compiler may further refine the granularity of the lock, and send a write lock request to the first device 1 for a certain part of the target table.
  • the first device 1 responds to the write lock request in units of a table, and forwards the write lock request in units of a certain portion of the table.
  • a request for a partial write lock of the target table is sent to the second device 2.
  • the performing a write lock process on the part of the target table comprises: if the full table of the target table has been written with a lock, ignoring the write lock process on the part of the target table; otherwise, A part of the target table is subjected to a write lock process.
  • the current transaction has acquired the write lock of the full table of the target table, and it is not necessary to acquire the part of the target table again.
  • the lock is written, so there is no need to perform a write lock process on the part of the target table at this time.
  • the twelfth device 212" acquires a request for a partial write lock of the target table sent by the first device 1; the thirteenth device 213" according to the request for the partial write lock of the target table, The part of the target table is subjected to write lock processing.
  • the second device 2 performs a write lock process on a portion of the target table according to a request for a partial write lock of the target table sent by the first device 1.
  • the second device 2 includes a total node and a plurality of child nodes; wherein the thirteenth device 213" distributes the request for a partial write lock on the target table to the plurality of nodes through the total node The child node performs a write lock process on a portion of the target table.
  • the second device 2 may also be a group consisting of programs running on a plurality of physical machines; one of the physical machines is selected as a total node in the group, and the remaining physical machines are Child node.
  • the total node is responsible for broadcasting the request for the partial write lock of the target table to all the child nodes in the group. To perform a write lock process on a portion of the target table.
  • the thirteenth device 213" is based on the request for a partial write lock on the target table, Querying, by the first device, whether the entire table of the target table has been locked; if the full table of the target table is not locked, the part of the target table is subjected to a write lock process.
  • the first device 1 may be queried whether the full table of the target table has been locked or not; The full table of the table has been written and locked by the first device 1, and there is no need to perform a write lock process on the portion of the target table.
  • the present application also provides a system for processing transactions, wherein the system comprises a first device 1 as described above and a second device 2 as described above.
  • a system consisting of the first device 1 and the second device 2 performs a write lock process on a target table of the transaction; wherein, the first The device 1 responds to the write lock request in units of a table, and forwards a write lock request to a certain unit in the table to the second device 2, and the second device 2 performs a part of the target table Add write lock processing.
  • the first device of the present application is a group consisting of a plurality of nodes, and the first device acquires a target table sent by the SQL compiler about a transaction to be processed.
  • Write a lock request and perform a write lock process on the target table based on the write lock request, thereby avoiding conflicts when multiple transactions are concurrent, ensuring isolation of the transaction, and if the write lock is successfully processed,
  • the pending version information of the target table is sent to the SQL compiler to ensure that the subsequent SQL compiler operates on the correct version of the target table.
  • the present application restarts the child node that does not respond to the write lock request, or migrates the service of the child node to another physical machine, and then updates the lock state information of the child node, thereby performing Effective error recovery and guarantee intra-group consistency. Further, in a scenario where the number is small and the degree of concurrency is high, the application performs a write lock process on the target table of the transaction by using a system consisting of the first device and the second device, by the second The device performs a write-and-lock process on the part of the target table, thereby improving the efficiency of the system.
  • the present application can be implemented in software and/or a combination of software and hardware, for example, using an application specific integrated circuit (ASIC), a general purpose computer, or any other similar hardware device.
  • the software program of the present application can be executed by a processor to implement the steps or functions described above.
  • the software program of the present application (including related numbers) According to the structure, it can be stored in a computer readable recording medium such as a RAM memory, a magnetic or optical drive or a floppy disk, and the like.
  • some of the steps or functions of the present application may be implemented in hardware, for example, as a circuit that cooperates with a processor to perform various steps or functions.
  • a portion of the present application can be applied as a computer program product, such as computer program instructions, which, when executed by a computer, can invoke or provide a method and/or technical solution in accordance with the present application.
  • the program instructions for invoking the method of the present application may be stored in a fixed or removable recording medium, and/or transmitted by a data stream in a broadcast or other signal bearing medium, and/or stored in a The working memory of the computer device in which the program instructions are run.
  • an embodiment in accordance with the present application includes a device including a memory for storing computer program instructions and a processor for executing program instructions, wherein when the computer program instructions are executed by the processor, triggering
  • the apparatus operates based on the aforementioned methods and/or technical solutions in accordance with various embodiments of the present application.

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Databases & Information Systems (AREA)
  • Software Systems (AREA)
  • Data Mining & Analysis (AREA)
  • Quality & Reliability (AREA)
  • Computational Linguistics (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
  • Stored Programmes (AREA)

Abstract

一种处理分布式事务的方法与设备,该方法中包括:获取SQL编译器发送的关于待处理的事务的目标表的加写锁请求,其中,所述事务包括若干条SQL语句(S101);并基于所述加写锁请求,对所述目标表进行加写锁处理(S102);若所述加写锁处理成功,向所述SQL编译器发送所述目标表的待处理版本信息(S103),以确保后续SQL编译器对目标表的正确版本进行操作。进一步地,通过重启未响应所述加写锁请求的所述子节点,或将所述子节点的服务迁移到其他物理机器上,而后更新所述子节点的加锁状态信息,从而进行有效的出错恢复并保证组内一致性。

Description

一种处理分布式事务的方法与设备 技术领域
本申请涉及计算机领域,尤其涉及一种处理分布式事务的技术。
背景技术
数据库中的事务是数据库运行中的一个逻辑工作单位,作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全地不执行。事务处理可以确保除非事务性单元内的所有操作都成功完成,否则不会永久更新面向数据的资源。而一个逻辑工作单元要成为事务,必须满足所谓的ACID(原子性、一致性、隔离性和持久性)属性;在多个事务并发时,如何避免冲突问题,都是关乎系统稳定性及用户体验的重要问题。
发明内容
本申请的一个目的是提供一种处理分布式事务的方法与设备。
根据本申请的一个方面,提供了一种在第一设备端处理分布式事务的方法,其中,该方法包括:
a获取SQL编译器发送的关于待处理的事务的目标表的加写锁请求,其中,所述事务包括若干条SQL语句;
b基于所述加写锁请求,对所述目标表进行加写锁处理;
c若所述加写锁处理成功,向所述SQL编译器发送所述目标表的待处理版本信息。
进一步地,所述第一设备包括一个总节点及若干个子节点;其中,所述步骤b包括:通过所述总节点将所述加写锁请求分发至若干个所述子节点,对所述目标表进行加写锁处理。
进一步地,所述方法还包括:若所述总节点在预定时间内获取大于或等于预定数量的所述子节点关于所述加写锁请求的响应,则所述加写锁处理成功。
进一步地,所述加写锁处理成功还包括:将新的加锁状态信息写入日志 文件中;或所述总节点存储新的加锁状态信息。
进一步地,所述方法还包括:重启未响应所述加写锁请求的所述子节点,所述子节点从所述日志文件中读取最新的所述加锁状态信息;或重启未响应所述加写锁请求的所述子节点,所述总节点将最新的所述加锁状态信息发送至所述子节点。
进一步地,所述重启未响应所述加写锁请求的所述子节点还包括:若所述未响应所述加写锁请求的所述子节点无法重启,则通过所述总节点将所述子节点的服务迁移到其他物理机器上,并在该物理机器上启动所述子节点的服务。
进一步地,所述步骤b包括:对所述目标表的部分进行加写锁处理;其中,所述方法还包括:向第二设备发送关于所述目标表的部分加写锁的请求。
进一步地,所述对所述目标表的部分进行加写锁处理包括:若所述目标表的全表已被加写锁,则忽略对所述目标表的部分进行加写锁处理;否则,对所述目标表的部分进行加写锁处理。
进一步地,所述方法还包括:当所述目标表的版本数量等于或大于预定版本数量阈值,对所述目标表的若干个版本进行合并处理。
进一步地,所述方法还包括:当获取事务提交语句,设置新版本的所述目标表的状态为有效,解锁所述目标表;或当获取事务回滚语句,设置新版本的所述目标表的状态为无效,解锁所述目标表。
进一步地,所述步骤a还包括:获取SQL编译器发送的关于待处理的事务的源表的加读锁请求;其中,所述步骤b还包括:基于所述加读锁请求,对所述源表进行加读锁处理;其中,所述步骤c还包括:若所述加读锁处理成功,向所述SQL编译器发送所述源表的待处理版本信息;其中,所述方法还包括:当获取事务提交语句或事务回滚语句,解锁所述源表。根据本申请的另一个方面,还提供了一种在第二设备端处理分布式事务的方法,其中,该方法包括:
A获取第一设备发送的关于目标表的部分加写锁的请求;
B根据所述关于目标表的部分加写锁的请求,对所述目标表的部分进行加写锁处理。
进一步地,所述步骤B包括:根据所述关于目标表的部分加写锁的请求,向所述第一设备查询所述目标表的全表是否已被加写锁;若所述目标表的全表未被加写锁,对所述目标表的部分进行加写锁处理。
进一步地,所述第二设备包括一个总节点及若干个子节点;其中,所述步骤B包括:通过所述总节点将所述关于目标表的部分加写锁的请求分发至若干个所述子节点,对所述目标表的部分进行加写锁处理。
根据本申请的又一个方面,还提供了一种用于处理分布式事务的第一设备,其中,该设备包括:
第一装置,用于获取SQL编译器发送的关于待处理的事务的目标表的加写锁请求,其中,所述事务包括若干条SQL语句;
第二装置,用于基于所述加写锁请求,对所述目标表进行加写锁处理;
第三装置,用于若所述加写锁处理成功,向所述SQL编译器发送所述目标表的待处理版本信息。
进一步地,所述设备包括一个总节点及若干个子节点;其中,所述第二装置用于:通过所述总节点将所述加写锁请求分发至若干个所述子节点,对所述目标表进行加写锁处理。
进一步地,所述设备还包括:第四装置,用于若所述总节点在预定时间内获取大于或等于预定数量的所述子节点关于所述加写锁请求的响应,则所述加写锁处理成功。
进一步地,所述加写锁处理成功还包括:将新的加锁状态信息写入日志文件中;或所述总节点存储新的加锁状态信息。
进一步地,所述设备还包括:第五装置,用于重启未响应所述加写锁请求的所述子节点,所述子节点从所述日志文件中读取最新的所述加锁状态信息;或第六装置,用于重启未响应所述加写锁请求的所述子节点,所述总节点将最新的所述加锁状态信息发送至所述子节点。
进一步地,所述重启未响应所述加写锁请求的所述子节点还包括:若所述未响应所述加写锁请求的所述子节点无法重启,则通过所述总节点将所述子节点的服务迁移到其他物理机器上,并在该物理机器上启动所述子节点的服务。
进一步地,所述第二装置用于:对所述目标表的部分进行加写锁处理;其中,所述设备还包括:第七装置,用于向第二设备发送关于所述目标表的部分加写锁的请求。
进一步地,所述对所述目标表的部分进行加写锁处理包括:若所述目标表的全表已被加写锁,则忽略对所述目标表的部分进行加写锁处理;否则,对所述目标表的部分进行加写锁处理。
进一步地,所述设备还包括:第八装置,用于当所述目标表的版本数量等于或大于预定版本数量阈值,对所述目标表的若干个版本进行合并处理。
进一步地,所述设备还包括:第九装置,用于当获取事务提交语句,设置新版本的所述目标表的状态为有效,解锁所述目标表;或第十装置,用于当获取事务回滚语句,设置新版本的所述目标表的状态为无效,解锁所述目标表。
进一步地,所述第一装置还用于:获取SQL编译器发送的关于待处理的事务的源表的加读锁请求;其中,所述第二装置还用于:基于所述加读锁请求,对所述源表进行加读锁处理;其中,所述第三装置还用于:若所述加读锁处理成功,向所述SQL编译器发送所述源表的待处理版本信息;其中,所述设备还包括:第十一装置,用于当获取事务提交语句或事务回滚语句,解锁所述源表。
根据本申请的再一个方面,还提供了一种用于处理分布式事务的第二设备,其中,该设备包括:
第十二装置,用于获取第一设备发送的关于目标表的部分加写锁的请求;
第十三装置,用于根据所述关于目标表的部分加写锁的请求,对所述目标表的部分进行加写锁处理。
进一步地,所述第十三装置用于:根据所述关于目标表的部分加写锁的请求,向所述第一设备查询所述目标表的全表是否已被加写锁;若所述目标表的全表未被加写锁,对所述目标表的部分进行加写锁处理。
进一步地,所述设备包括一个总节点及若干个子节点;其中,所述第十三装置用于:通过所述总节点将所述关于目标表的部分加写锁的请求分发至若干个所述子节点,对所述目标表的部分进行加写锁处理。
根据本申请的另一个方面,还提供了一种处理分布式事务的系统,其中,该系统包括如上所述的第一设备及如上所述的第二设备。
与现有技术相比,在分布式系统中,本申请的第一设备是由多个节点组成的一个组,所述第一设备获取SQL编译器发送的关于待处理的事务的目标表的加写锁请求,并基于所述加写锁请求,对所述目标表进行加写锁处理,从而避免多个事务并发时的冲突问题,确保事务的隔离性,若所述加写锁处理成功,向所述SQL编译器发送所述目标表的待处理版本信息,以确保后续SQL编译器对目标表的正确版本进行操作。进一步地,本申请通过重启未响应所述加写锁请求的所述子节点,或将所述子节点的服务迁移到其他物理机器上,而后更新所述子节点的加锁状态信息,从而进行有效的出错恢复并保证组内一致性。进一步地,在数量小、并发度较高的场景下,本申请通过所述第一设备及所述第二设备组成的系统对所述事务的目标表进行加写锁处理,由所述第二设备对所述目标表的部分进行加写锁处理,从而提高系统的效率。
附图说明
通过阅读参照以下附图所作的对非限制性实施例所作的详细描述,本申请的其它特征、目的和优点将会变得更明显:
图1示出根据本申请一个实施例的一种第一设备的结构图;
图2示出根据本申请一个方面的一种处理分布式事务的方法流程图;
图3示出根据本申请一个实施例的一种处理分布式事务的方法流程图;
图4示出根据本申请另一个实施例的一种处理分布式事务的方法流程图;
图5示出根据本申请另一个方面的一种用于处理分布式事务的第一设备示意图;
图6示出根据本申请一个实施例的一种用于处理分布式事务的第一设备示意图;
图7示出根据本申请另一个实施例的一种用于处理分布式事务的第一设备与第二设备的示意图。
附图中相同或相似的附图标记代表相同或相似的部件。
具体实施方式
下面结合附图对本申请作进一步详细描述。
在本申请一个典型的配置中,终端、服务网络的设备和可信方均包括一个或多个处理器(CPU)、输入/输出接口、网络接口和内存。
内存可能包括计算机可读介质中的非永久性存储器,随机存取存储器(RAM)和/或非易失性内存等形式,如只读存储器(ROM)或闪存(flash RAM)。内存是计算机可读介质的示例。
计算机可读介质包括永久性和非永久性、可移动和非可移动媒体可以由任何方法或技术来实现信息存储。信息可以是计算机可读指令、数据结构、程序的模块或其他数据。计算机的存储介质的例子包括,但不限于相变内存(PRAM)、静态随机存取存储器(SRAM)、动态随机存取存储器(DRAM)、其他类型的随机存取存储器(RAM)、只读存储器(ROM)、电可擦除可编程只读存储器(EEPROM)、快闪记忆体或其他内存技术、只读光盘只读存储器(CD-ROM)、数字多功能光盘(DVD)或其他光学存储、磁盒式磁带,磁带磁盘存储或其他磁性存储设备或任何其他非传输介质,可用于存储可以被计算设备访问的信息。按照本文中的界定,计算机可读介质不包括非暂存电脑可读媒体(transitory media),如调制的数据信号和载波。
图2示出根据本申请一个方面的一种用于处理分布式事务的方法流程图。
该方法包括步骤S101、步骤S102和步骤S103。具体地,在步骤S101中,所述第一设备1获取SQL编译器发送的关于待处理的事务的目标表的加写锁请求,其中,所述事务包括若干条SQL语句;在步骤S102中,所述第一设备1基于所述加写锁请求,对所述目标表进行加写锁处理;在步骤S103中,所述第一设备1若所述加写锁处理成功,向所述SQL编译器发送所述目标表的待处理版本信息。
在此,所述第一设备1包括但不限于用户设备、网络设备、或用户设备 与网络设备通过网络相集成所构成的设备。所述用户设备其包括但不限于任何一种可与用户通过触摸板进行人机交互的移动电子产品,例如智能手机、平板电脑等,所述移动电子产品可以采用任意操作系统,如android操作系统、iOS操作系统等。其中,所述网络设备包括一种能够按照事先设定或存储的指令,自动进行数值计算和信息处理的电子设备,其硬件包括但不限于微处理器、专用集成电路(ASIC)、可编程门阵列(FPGA)、数字处理器(DSP)、嵌入式设备等。所述网络设备其包括但不限于计算机、网络主机、单个网络服务器、多个网络服务器集或多个服务器构成的云;在此,云由基于云计算(Cloud Computing)的大量计算机或网络服务器构成,其中,云计算是分布式计算的一种,由一群松散耦合的计算机集组成的一个虚拟超级计算机。所述网络包括但不限于互联网、广域网、城域网、局域网、VPN网络、无线自组织网络(Ad Hoc网络)等。优选地,第一设备1还可以是运行于所述用户设备、网络设备、或用户设备与网络设备、网络设备、触摸终端或网络设备与触摸终端通过网络相集成所构成的设备上的脚本程序。当然,本领域技术人员应能理解上述第一设备1仅为举例,其他现有的或今后可能出现的第一设备1如可适用于本申请,也应包含在本申请保护范围以内,并在此以引用方式包含于此。
在步骤S101中,所述第一设备1获取SQL编译器发送的关于待处理的事务的目标表的加写锁请求,其中,所述事务包括若干条SQL语句。
在此,SQL语言(结构化查询语言)的组成部分包括了DCL语言(数据库控制语言)。所述事务由DCL语句begin transaction(开始事务)开始,并由DCL语句commit(事务提交)或rollback(事务回滚)结束。在所述事务起始与终点处的DCL语句之间,包括若干条CRUD(Create、Retrieve、Update、Delete几个单词的首字母简写,主要被用在描述软件系统中数据库或者持久层的基本操作功能)SQL语句。所述目标表指的是所述SQL语句将要对其进行写操作的表。而且,在加锁过程中,所述事务中每条SQL语句对应的加写锁请求分别独立进行。
例如,首先由SQL编译器对所获取的SQL语句进行编译,得到该SQL语句涉及的表与执行计划,然后SQL编译器向所述第一设备1发送关于所述 事务中所述SQL语句的目标表的加写锁请求,通过加锁的方式避免多个事务并发时产生冲突的问题,确保事务的隔离性(Isolation)。
优选地,在步骤S101中,所述第一设备1还获取SQL编译器发送的关于待处理的事务的源表的加读锁请求。
在此,所述源表指的是所述SQL语句将要对其进行读操作的表。在大多数应用场景中,所述SQL语句既有目标表也有源表,SQL编译器还需发送对所述SQL语句的源表的加读锁请求。而在部分应用场景中,所述SQL语句只有目标表没有源表,则SQL编译器无需发送对所述SQL语句的源表的加读锁请求。
在步骤S102中,所述第一设备1基于所述加写锁请求,对所述目标表进行加写锁处理。
在此,对所述事务的SQL语句的目标表进行加写锁处理。已被加写锁的所述目标表,在写锁被释放之前,其他事务无法对其进行写操作。
优选地,在步骤S102中,所述第一设备1还基于所述加读锁请求,对所述源表进行加读锁处理。
例如,若所述SQL语句既有目标表也有源表,还需基于所述加读锁请求,对所述源表进行加读锁处理。
优选地,所述第一设备1包括一个总节点及若干个子节点;其中,在步骤S102中,所述第一设备1通过所述总节点将所述加写锁请求分发至若干个所述子节点,对所述目标表进行加写锁处理。
例如,一个物理机器(例如服务器等)即为一个节点(node),参照图1,所述第一设备1可以是由运行在多个物理机器上的程序组成的一个组(group);在组(group)内将其中一个物理机器选为总节点,其余物理机器则为子节点。当获取SQL编译器发送的所述加写锁请求时,所述总节点负责将所述加写锁请求广播给组内的所有所述子节点,以对所述目标表进行加写锁处理。在优选的实施例中,在数据量大、并发度不高的场景下,批量处理所述加写锁请求。
在步骤S103中,所述第一设备1若所述加写锁处理成功,向所述SQL编译器发送所述目标表的待处理版本信息。
例如,每条CRUD操作的结果都是该条CRUD操作的目标表的一个新的版本,由于经过CRUD操作后,通常一个表会有多个版本。在此,当所述加写锁处理成功后,将所述目标表的待处理版本信息返回给SQL编译器,以确保后续SQL编译器对目标表的正确版本进行操作。
优选地,在步骤S103中,所述第一设备1若所述加读锁处理成功,向所述SQL编译器发送所述源表的待处理版本信息。
例如,若所述SQL语句既有目标表也有源表,还需将所述源表的待处理版本信息返回给SQL编译器,以确保后续SQL编译器对源表的正确版本进行操作。
优选地,所述方法还包括:所述第一设备1当所述目标表的版本数量等于或大于预定版本数量阈值,对所述目标表的若干个版本进行合并处理。
例如,对所述目标表进行若干次CRUD操作后,也会相应地产生若干个版本,随着时间的推移,所述目标表的版本数量在不断地增加。在优选的实施例中,当所述目标表的版本数量等于或大于预定版本数量阈值,对所述目标表的若干个版本进行合并处理,以得到一个新的版本,并清除之前的版本。
优选地,所述方法还包括:所述第一设备1当获取事务提交语句,设置新版本的所述目标表的状态为有效,解锁所述目标表;当获取事务回滚语句,设置新版本的所述目标表的状态为无效,解锁所述目标表。
例如,当获取DCL语句commit(事务提交),将所述事务对所述目标表的写操作全部标记为有效,同时释放所述事务占有的所有锁,即解锁了所述目标表;当获取DCL语句rollback(事务回滚),将所述事务对所述目标表的写操作全部标记为无效,同时释放所述事务占有的所有锁,即解锁了所述目标表。从而确保事务的原子性(Atomicity)与一致性(Consistency)。
优选地,所述方法还包括:所述第一设备1当获取事务提交语句或事务回滚语句,解锁所述源表。
例如,当获取commit语句或rollback语句,会释放所述事务占有的所有锁,既释放了所述目标表的写锁,也释放了所述源表的读锁。
图3示出根据本申请一个实施例的一种用于处理分布式事务的方法流程图。
该方法包括步骤S101’、步骤S102’、步骤S104’和步骤S103’。在此,所述步骤S101’、步骤S103’与图1中步骤S101、步骤S103内容相同或基本相同,为简明起见,不再赘述。
具体地,在步骤S102’中,所述第一设备1基于所述加写锁请求,通过所述总节点将所述加写锁请求分发至若干个所述子节点,对所述目标表进行加写锁处理;在步骤S104’中,所述第一设备1若所述总节点在预定时间内获取大于或等于预定数量的所述子节点关于所述加写锁请求的响应,则所述加写锁处理成功。
例如,所述总节点负责将所述加写锁请求广播给组内的所有所述子节点并等待所述子节点对所述加写锁请求的响应,只有当收到大于或等于预定数量的所述子节点关于所述加写锁请求的响应,所述加写锁处理才算成功。如果所述总节点在预定时间内没能收到足够多的所述子节点关于所述加写锁请求的响应,则加锁失败;此时,所述总节点可以有两种选择:一种是随机等待一段时间后继续发送所述加写锁请求,直到到达加锁尝试次数的阈值,如果此时仍未加锁成功则返回加锁失败信息;另一种是直接返回加锁失败信息。SQL编译器在收到加锁失败信息后,需要重试,即重新发送所述加写锁请求。
优选地,所述加写锁处理成功还包括:将新的加锁状态信息写入日志文件中;或所述总节点存储新的加锁状态信息。
例如,若得到大于或等于预定数量的所述子节点关于所述加写锁请求的响应,则所述加写锁处理成功,并保存新的加锁状态信息。在此,有两种保存新的加锁状态信息的方式,一种是将新的加锁状态信息写入日志文件中,另一种是所述总节点存储新的加锁状态信息。
优选地,所述方法还包括:所述第一设备1重启未响应所述加写锁请求的所述子节点,所述子节点从所述日志文件中读取最新的所述加锁状态信息;或重启未响应所述加写锁请求的所述子节点,所述总节点将最新的所述加锁状态信息发送至所述子节点。
例如,接上例,若所述子节点未响应(可能由于物理机器死机等原因所致)所述加写锁请求,需重启未响应所述加写锁请求的所述子节点,而且该子节点在重启后需要更新自己的状态信息,以便与组内的其他成员 (包括所述总节点及所述子节点)保持一致。在此,所述子节点也有两种更新状态信息的方式,一种是所述子节点从所述日志文件中读取最新的所述加锁状态信息,另一种是所述总节点将最新的所述加锁状态信息发送至所述子节点。在另外的实施例中,若所述总节点无法响应,则从组内的其余所述子节点中重新选出新的总节点。
更优选地,所述重启未响应所述加写锁请求的所述子节点还包括:若所述未响应所述加写锁请求的所述子节点无法重启,则通过所述总节点将所述子节点的服务迁移到其他物理机器上,并在该物理机器上启动所述子节点的服务。
例如,接上例,还可能存在所述未响应所述加写锁请求的所述子节点无法重启的情况,此时,可通过所述总节点将所述子节点的服务迁移到其他物理机器上,并在该物理机器上启动所述子节点的服务。而后,该物理机器上的所述子节点可通过上述两种方式更新自己的状态信息,以便与组内的其他成员(包括所述总节点及所述子节点)保持一致。与现有技术相比,本申请通过重启未响应所述加写锁请求的所述子节点,或将所述子节点的服务迁移到其他物理机器上,而后更新所述子节点的加锁状态信息,从而进行有效的出错恢复并保证组内一致性。
图4示出根据本申请另一个实施例的一种用于处理分布式事务的方法流程图。
该方法包括步骤S101”、步骤S102”、步骤S107”和步骤S103”,步骤S212”和步骤S213”。在此,所述步骤S101”、步骤S103”与图1中步骤S101、步骤S103内容相同或基本相同,为简明起见,不再赘述。
具体地,在步骤S102”中,所述第一设备1基于所述加写锁请求,对所述目标表的部分进行加写锁处理;在步骤S107”中,所述第一设备1向第二设备2发送关于所述目标表的部分加写锁的请求。
例如,在数量小、并发度较高的场景下,SQL编译器可进一步细化锁的粒度,针对所述目标表中的某一部分向所述第一设备1发送加写锁请求。在此,所述第一设备1响应以表为单位的所述加写锁请求,并转发以表中某部分为单位的所述加写锁请求。具体地,向所述第二设备2发送关于所 述目标表的部分加写锁的请求。
优选地,所述对所述目标表的部分进行加写锁处理包括:若所述目标表的全表已被加写锁,则忽略对所述目标表的部分进行加写锁处理;否则,对所述目标表的部分进行加写锁处理。
在此,若所述目标表的全表已被所述第一设备1加写锁,当前事务已经获取了所述目标表的全表的写锁,没必要再获取所述目标表的部分的写锁,所以此时无需对所述目标表的部分进行加写锁处理。
在步骤S212”中,所述第二设备2获取第一设备1发送的关于目标表的部分加写锁的请求;在步骤S213”中,所述第二设备2根据所述关于目标表的部分加写锁的请求,对所述目标表的部分进行加写锁处理。
在此,所述第二设备2根据所述第一设备1发送的关于目标表的部分加写锁的请求,对所述目标表的部分进行加写锁处理。
优选地,所述第二设备2包括一个总节点及若干个子节点;其中,在步骤S213”中,所述第二设备2通过所述总节点将所述关于目标表的部分加写锁的请求分发至若干个所述子节点,对所述目标表的部分进行加写锁处理。
例如,所述第二设备2也可以是由运行在多个物理机器上的程序组成的一个组(group);在组(group)内将其中一个物理机器选为总节点,其余物理机器则为子节点。当获取所述第一设备1发送的关于目标表的部分加写锁的请求时,所述总节点负责将所述关于目标表的部分加写锁的请求广播给组内的所有所述子节点,以对所述目标表的部分进行加写锁处理。
优选地,在步骤S213”中,所述第二设备2根据所述关于目标表的部分加写锁的请求,向所述第一设备查询所述目标表的全表是否已被加写锁;若所述目标表的全表未被加写锁,对所述目标表的部分进行加写锁处理。
例如,所述第二设备2获取所述关于目标表的部分加写锁的请求后,可向所述第一设备1查询所述目标表的全表是否已被加写锁;若所述目标表的全表已被所述第一设备1加写锁,则无需再对所述目标表的部分进行加写锁处理。
图5示出根据本申请另一个方面的一种用于处理分布式事务的第一设备1,其中,第一设备1包括第一装置101、第二装置102和第三装置103。
具体地,所述第一装置101获取SQL编译器发送的关于待处理的事务的目标表的加写锁请求,其中,所述事务包括若干条SQL语句;所述第二装置102基于所述加写锁请求,对所述目标表进行加写锁处理;所述第三装置103若所述加写锁处理成功,向所述SQL编译器发送所述目标表的待处理版本信息。
在此,所述第一设备1包括但不限于用户设备、网络设备、或用户设备与网络设备通过网络相集成所构成的设备。所述用户设备其包括但不限于任何一种可与用户通过触摸板进行人机交互的移动电子产品,例如智能手机、平板电脑等,所述移动电子产品可以采用任意操作系统,如android操作系统、iOS操作系统等。其中,所述网络设备包括一种能够按照事先设定或存储的指令,自动进行数值计算和信息处理的电子设备,其硬件包括但不限于微处理器、专用集成电路(ASIC)、可编程门阵列(FPGA)、数字处理器(DSP)、嵌入式设备等。所述网络设备其包括但不限于计算机、网络主机、单个网络服务器、多个网络服务器集或多个服务器构成的云;在此,云由基于云计算(Cloud Computing)的大量计算机或网络服务器构成,其中,云计算是分布式计算的一种,由一群松散耦合的计算机集组成的一个虚拟超级计算机。所述网络包括但不限于互联网、广域网、城域网、局域网、VPN网络、无线自组织网络(Ad Hoc网络)等。优选地,第一设备1还可以是运行于所述用户设备、网络设备、或用户设备与网络设备、网络设备、触摸终端或网络设备与触摸终端通过网络相集成所构成的设备上的脚本程序。当然,本领域技术人员应能理解上述第一设备1仅为举例,其他现有的或今后可能出现的第一设备1如可适用于本申请,也应包含在本申请保护范围以内,并在此以引用方式包含于此。
所述第一装置101获取SQL编译器发送的关于待处理的事务的目标表的加写锁请求,其中,所述事务包括若干条SQL语句。
在此,SQL语言(结构化查询语言)的组成部分包括了DCL语言(数据库控制语言)。所述事务由DCL语句begin transaction(开始事务)开始,并由DCL语句commit(事务提交)或rollback(事务回滚)结束。在所述事务起始与终点处的DCL语句之间,包括若干条CRUD(Create、Retrieve、Update、 Delete几个单词的首字母简写,主要被用在描述软件系统中数据库或者持久层的基本操作功能)SQL语句。所述目标表指的是所述SQL语句将要对其进行写操作的表。而且,在加锁过程中,所述事务中每条SQL语句对应的加写锁请求分别独立进行。
例如,首先由SQL编译器对所获取的SQL语句进行编译,得到该SQL语句涉及的表与执行计划,然后SQL编译器向所述第一设备1发送关于所述事务中所述SQL语句的目标表的加写锁请求,通过加锁的方式避免多个事务并发时产生冲突的问题,确保事务的隔离性(Isolation)。
优选地,所述第一装置101还获取SQL编译器发送的关于待处理的事务的源表的加读锁请求。
在此,所述源表指的是所述SQL语句将要对其进行读操作的表。在大多数应用场景中,所述SQL语句既有目标表也有源表,SQL编译器还需发送对所述SQL语句的源表的加读锁请求。而在部分应用场景中,所述SQL语句只有目标表没有源表,则SQL编译器无需发送对所述SQL语句的源表的加读锁请求。
所述第二装置102基于所述加写锁请求,对所述目标表进行加写锁处理。
在此,对所述事务的SQL语句的目标表进行加写锁处理。已被加写锁的所述目标表,在写锁被释放之前,其他事务无法对其进行写操作。
优选地,所述第二装置102还基于所述加读锁请求,对所述源表进行加读锁处理。
例如,若所述SQL语句既有目标表也有源表,还需基于所述加读锁请求,对所述源表进行加读锁处理。
优选地,所述第一设备1包括一个总节点及若干个子节点;其中,所述第二装置102通过所述总节点将所述加写锁请求分发至若干个所述子节点,对所述目标表进行加写锁处理。
例如,一个物理机器(例如服务器等)即为一个节点(node),参照图1,所述第一设备1可以是由运行在多个物理机器上的程序组成的一个组(group);在组(group)内将其中一个物理机器选为总节点,其余物理机器则为子节点。当获取SQL编译器发送的所述加写锁请求时,所述总节点负责将所述加写锁 请求广播给组内的所有所述子节点,以对所述目标表进行加写锁处理。在优选的实施例中,在数据量大、并发度不高的场景下,批量处理所述加写锁请求。
所述第三装置103若所述加写锁处理成功,向所述SQL编译器发送所述目标表的待处理版本信息。
例如,每条CRUD操作的结果都是该条CRUD操作的目标表的一个新的版本,由于经过CRUD操作后,通常一个表会有多个版本。在此,当所述加写锁处理成功后,将所述目标表的待处理版本信息返回给SQL编译器,以确保后续SQL编译器对目标表的正确版本进行操作。
优选地,所述第三装置103若所述加读锁处理成功,向所述SQL编译器发送所述源表的待处理版本信息。
例如,若所述SQL语句既有目标表也有源表,还需将所述源表的待处理版本信息返回给SQL编译器,以确保后续SQL编译器对源表的正确版本进行操作。
优选地,所述第一设备1还包括第八装置(图中未示出);所述第八装置当所述目标表的版本数量等于或大于预定版本数量阈值,对所述目标表的若干个版本进行合并处理。
例如,对所述目标表进行若干次CRUD操作后,也会相应地产生若干个版本,随着时间的推移,所述目标表的版本数量在不断地增加。在优选的实施例中,当所述目标表的版本数量等于或大于预定版本数量阈值,对所述目标表的若干个版本进行合并处理,以得到一个新的版本,并清除之前的版本。
优选地,所述第一设备1还包括第九装置(图中未示出)或第十装置(图中未示出);所述第九装置当获取事务提交语句,设置新版本的所述目标表的状态为有效,解锁所述目标表;所述第十装置当获取事务回滚语句,设置新版本的所述目标表的状态为无效,解锁所述目标表。
例如,当获取DCL语句commit(事务提交),将所述事务对所述目标表的写操作全部标记为有效,同时释放所述事务占有的所有锁,即解锁了所述目标表;当获取DCL语句rollback(事务回滚),将所述事务对所述目标表的写操作全部标记为无效,同时释放所述事务占有的所有锁,即解锁了所述目 标表。从而确保事务的原子性(Atomicity)与一致性(Consistency)。
优选地,所述第一设备1还包括第十一装置(图中未示出);所述第十一装置当获取事务提交语句或事务回滚语句,解锁所述源表。
例如,当获取commit语句或rollback语句,会释放所述事务占有的所有锁,既释放了所述目标表的写锁,也释放了所述源表的读锁。
图6示出根据本申请一个实施例的一种用于处理分布式事务的第一设备1,其中,第一设备1包括第一装置101’、第二装置102’、第四装置104’和第三装置103’。
在此,所述第一装置101’、第三装置103’与图4中第一装置101、第三装置103内容相同或基本相同,为简明起见,不再赘述。
具体地,所述第二装置102’基于所述加写锁请求,通过所述总节点将所述加写锁请求分发至若干个所述子节点,对所述目标表进行加写锁处理;所述第四装置104’若所述总节点在预定时间内获取大于或等于预定数量的所述子节点关于所述加写锁请求的响应,则所述加写锁处理成功。
例如,所述总节点负责将所述加写锁请求广播给组内的所有所述子节点并等待所述子节点对所述加写锁请求的响应,只有当收到大于或等于预定数量的所述子节点关于所述加写锁请求的响应,所述加写锁处理才算成功。如果所述总节点在预定时间内没能收到足够多的所述子节点关于所述加写锁请求的响应,则加锁失败;此时,所述总节点可以有两种选择:一种是随机等待一段时间后继续发送所述加写锁请求,直到到达加锁尝试次数的阈值,如果此时仍未加锁成功则返回加锁失败信息;另一种是直接返回加锁失败信息。SQL编译器在收到加锁失败信息后,需要重试,即重新发送所述加写锁请求。
优选地,所述加写锁处理成功还包括:将新的加锁状态信息写入日志文件中;或所述总节点存储新的加锁状态信息。
例如,若得到大于或等于预定数量的所述子节点关于所述加写锁请求的响应,则所述加写锁处理成功,并保存新的加锁状态信息。在此,有两种保存新的加锁状态信息的方式,一种是将新的加锁状态信息写入日志文件中,另一种是所述总节点存储新的加锁状态信息。
优选地,所述第一设备1还包括第五装置(图中未示出)和第六装置 (图中未示出);所述第五装置重启未响应所述加写锁请求的所述子节点,所述子节点从所述日志文件中读取最新的所述加锁状态信息;或所述第六装置重启未响应所述加写锁请求的所述子节点,所述总节点将最新的所述加锁状态信息发送至所述子节点。
例如,接上例,若所述子节点未响应(可能由于物理机器死机等原因所致)所述加写锁请求,需重启未响应所述加写锁请求的所述子节点,而且该子节点在重启后需要更新自己的状态信息,以便与组内的其他成员(包括所述总节点及所述子节点)保持一致。在此,所述子节点也有两种更新状态信息的方式,一种是所述子节点从所述日志文件中读取最新的所述加锁状态信息,另一种是所述总节点将最新的所述加锁状态信息发送至所述子节点。在另外的实施例中,若所述总节点无法响应,则从组内的其余所述子节点中重新选出新的总节点。
更优选地,所述重启未响应所述加写锁请求的所述子节点还包括:若所述未响应所述加写锁请求的所述子节点无法重启,则通过所述总节点将所述子节点的服务迁移到其他物理机器上,并在该物理机器上启动所述子节点的服务。
例如,接上例,还可能存在所述未响应所述加写锁请求的所述子节点无法重启的情况,此时,可通过所述总节点将所述子节点的服务迁移到其他物理机器上,并在该物理机器上启动所述子节点的服务。而后,该物理机器上的所述子节点可通过上述两种方式更新自己的状态信息,以便与组内的其他成员(包括所述总节点及所述子节点)保持一致。与现有技术相比,本申请通过重启未响应所述加写锁请求的所述子节点,或将所述子节点的服务迁移到其他物理机器上,而后更新所述子节点的加锁状态信息,从而进行有效的出错恢复并保证组内一致性。
图7示出根据本申请另一个实施例的一种用于处理分布式事务的第一设备1与第二设备2,其中,第一设备1包括第一装置101”、第二装置102”、第七装置107”和第三装置103”,第二设备2包括第十二装置212”和第十三装置213”。
在此,所述第一装置101”、第三装置103”与图4中第一装置101、 第三装置103内容相同或基本相同,为简明起见,不再赘述。
具体地,所述第二装置102”基于所述加写锁请求,对所述目标表的部分进行加写锁处理;所述第七装置107”向第二设备发送关于所述目标表的部分加写锁的请求。
例如,在数量小、并发度较高的场景下,SQL编译器可进一步细化锁的粒度,针对所述目标表中的某一部分向所述第一设备1发送加写锁请求。在此,所述第一设备1响应以表为单位的所述加写锁请求,并转发以表中某部分为单位的所述加写锁请求。具体地,向所述第二设备2发送关于所述目标表的部分加写锁的请求。
优选地,所述对所述目标表的部分进行加写锁处理包括:若所述目标表的全表已被加写锁,则忽略对所述目标表的部分进行加写锁处理;否则,对所述目标表的部分进行加写锁处理。
在此,若所述目标表的全表已被所述第一设备1加写锁,当前事务已经获取了所述目标表的全表的写锁,没必要再获取所述目标表的部分的写锁,所以此时无需对所述目标表的部分进行加写锁处理。
所述第十二装置212”获取第一设备1发送的关于目标表的部分加写锁的请求;所述第十三装置213”根据所述关于目标表的部分加写锁的请求,对所述目标表的部分进行加写锁处理。
在此,所述第二设备2根据所述第一设备1发送的关于目标表的部分加写锁的请求,对所述目标表的部分进行加写锁处理。
优选地,所述第二设备2包括一个总节点及若干个子节点;其中,所述第十三装置213”通过所述总节点将所述关于目标表的部分加写锁的请求分发至若干个所述子节点,对所述目标表的部分进行加写锁处理。
例如,所述第二设备2也可以是由运行在多个物理机器上的程序组成的一个组(group);在组(group)内将其中一个物理机器选为总节点,其余物理机器则为子节点。当获取所述第一设备1发送的关于目标表的部分加写锁的请求时,所述总节点负责将所述关于目标表的部分加写锁的请求广播给组内的所有所述子节点,以对所述目标表的部分进行加写锁处理。
优选地,所述第十三装置213”根据所述关于目标表的部分加写锁的请求, 向所述第一设备查询所述目标表的全表是否已被加写锁;若所述目标表的全表未被加写锁,对所述目标表的部分进行加写锁处理。
例如,所述第二设备2获取所述关于目标表的部分加写锁的请求后,可向所述第一设备1查询所述目标表的全表是否已被加写锁;若所述目标表的全表已被所述第一设备1加写锁,则无需再对所述目标表的部分进行加写锁处理。
优选地,本申请还提供了一种处理事务的系统,其中,该系统包括如上所述的第一设备1及如上所述的第二设备2。
例如,在数量小、并发度较高的场景下,通过所述第一设备1及所述第二设备2组成的系统对所述事务的目标表进行加写锁处理;其中,所述第一设备1响应以表为单位的加写锁请求,并向所述第二设备2转发以表中某部分为单位的加写锁请求,由所述第二设备2对所述目标表的部分进行加写锁处理。
与现有技术相比,在分布式系统中,本申请的第一设备是由多个节点组成的一个组,所述第一设备获取SQL编译器发送的关于待处理的事务的目标表的加写锁请求,并基于所述加写锁请求,对所述目标表进行加写锁处理,从而避免多个事务并发时的冲突问题,确保事务的隔离性,若所述加写锁处理成功,向所述SQL编译器发送所述目标表的待处理版本信息,以确保后续SQL编译器对目标表的正确版本进行操作。进一步地,本申请通过重启未响应所述加写锁请求的所述子节点,或将所述子节点的服务迁移到其他物理机器上,而后更新所述子节点的加锁状态信息,从而进行有效的出错恢复并保证组内一致性。进一步地,在数量小、并发度较高的场景下,本申请通过所述第一设备及所述第二设备组成的系统对所述事务的目标表进行加写锁处理,由所述第二设备对所述目标表的部分进行加写锁处理,从而提高系统的效率。
需要注意的是,本申请可在软件和/或软件与硬件的组合体中被实施,例如,可采用专用集成电路(ASIC)、通用目的计算机或任何其他类似硬件设备来实现。在一个实施例中,本申请的软件程序可以通过处理器执行以实现上文所述步骤或功能。同样地,本申请的软件程序(包括相关的数 据结构)可以被存储到计算机可读记录介质中,例如,RAM存储器,磁或光驱动器或软磁盘及类似设备。另外,本申请的一些步骤或功能可采用硬件来实现,例如,作为与处理器配合从而执行各个步骤或功能的电路。
另外,本申请的一部分可被应用为计算机程序产品,例如计算机程序指令,当其被计算机执行时,通过该计算机的操作,可以调用或提供根据本申请的方法和/或技术方案。而调用本申请的方法的程序指令,可能被存储在固定的或可移动的记录介质中,和/或通过广播或其他信号承载媒体中的数据流而被传输,和/或被存储在根据所述程序指令运行的计算机设备的工作存储器中。在此,根据本申请的一个实施例包括一个装置,该装置包括用于存储计算机程序指令的存储器和用于执行程序指令的处理器,其中,当该计算机程序指令被该处理器执行时,触发该装置运行基于前述根据本申请的多个实施例的方法和/或技术方案。
对于本领域技术人员而言,显然本申请不限于上述示范性实施例的细节,而且在不背离本申请的精神或基本特征的情况下,能够以其他的具体形式实现本申请。因此,无论从哪一点来看,均应将实施例看作是示范性的,而且是非限制性的,本申请的范围由所附权利要求而不是上述说明限定,因此旨在将落在权利要求的等同要件的含义和范围内的所有变化涵括在本申请内。不应将权利要求中的任何附图标记视为限制所涉及的权利要求。此外,显然“包括”一词不排除其他单元或步骤,单数不排除复数。装置权利要求中陈述的多个单元或装置也可以由一个单元或装置通过软件或者硬件来实现。第一,第二等词语用来表示名称,而并不表示任何特定的顺序。

Claims (29)

  1. 一种在第一设备端处理分布式事务的方法,其中,该方法包括:
    a获取SQL编译器发送的关于待处理的事务的目标表的加写锁请求,其中,所述事务包括若干条SQL语句;
    b基于所述加写锁请求,对所述目标表进行加写锁处理;
    c若所述加写锁处理成功,向所述SQL编译器发送所述目标表的待处理版本信息。
  2. 根据权利要求1所述的方法,其中,所述第一设备包括一个总节点及若干个子节点;
    其中,所述步骤b包括:
    通过所述总节点将所述加写锁请求分发至若干个所述子节点,对所述目标表进行加写锁处理。
  3. 根据权利要求2所述的方法,其中,所述方法还包括:
    若所述总节点在预定时间内获取大于或等于预定数量的所述子节点关于所述加写锁请求的响应,则所述加写锁处理成功。
  4. 根据权利要求3所述的方法,其中,所述加写锁处理成功还包括:
    将新的加锁状态信息写入日志文件中;或
    所述总节点存储新的加锁状态信息。
  5. 根据权利要求4所述的方法,其中,所述方法还包括:
    重启未响应所述加写锁请求的所述子节点,所述子节点从所述日志文件中读取最新的所述加锁状态信息;或
    重启未响应所述加写锁请求的所述子节点,所述总节点将最新的所述加锁状态信息发送至所述子节点。
  6. 根据权利要求5所述的方法,其中,所述重启未响应所述加写锁请求的所述子节点还包括:
    若所述未响应所述加写锁请求的所述子节点无法重启,则通过所述总节点将所述子节点的服务迁移到其他物理机器上,并在该物理机器上启动所述子节点的服务。
  7. 根据权利要求1所述的方法,其中,所述步骤b包括:
    对所述目标表的部分进行加写锁处理;
    其中,所述方法还包括:
    向第二设备发送关于所述目标表的部分加写锁的请求。
  8. 根据权利要求7所述的方法,其中,所述对所述目标表的部分进行加写锁处理包括:
    若所述目标表的全表已被加写锁,则忽略对所述目标表的部分进行加写锁处理;否则,对所述目标表的部分进行加写锁处理。
  9. 根据权利要求1所述的方法,其中,所述方法还包括:
    当所述目标表的版本数量等于或大于预定版本数量阈值,对所述目标表的若干个版本进行合并处理。
  10. 根据权利要求1所述的方法,其中,所述方法还包括:
    当获取事务提交语句,设置新版本的所述目标表的状态为有效,解锁所述目标表;或
    当获取事务回滚语句,设置新版本的所述目标表的状态为无效,解锁所述目标表。
  11. 根据权利要求1所述的方法,其中,所述步骤a还包括:
    获取SQL编译器发送的关于待处理的事务的源表的加读锁请求;
    其中,所述步骤b还包括:
    基于所述加读锁请求,对所述源表进行加读锁处理;
    其中,所述步骤c还包括:
    若所述加读锁处理成功,向所述SQL编译器发送所述源表的待处理版本信息;
    其中,所述方法还包括:
    当获取事务提交语句或事务回滚语句,解锁所述源表。
  12. 一种在第二设备端处理分布式事务的方法,其中,该方法包括:
    A获取第一设备发送的关于目标表的部分加写锁的请求;
    B根据所述关于目标表的部分加写锁的请求,对所述目标表的部分进行加写锁处理。
  13. 根据权利要求12所述的方法,其中,所述步骤B包括:
    根据所述关于目标表的部分加写锁的请求,向所述第一设备查询所述目 标表的全表是否已被加写锁;
    若所述目标表的全表未被加写锁,对所述目标表的部分进行加写锁处理。
  14. 根据权利要求12所述的方法,其中,所述第二设备包括一个总节点及若干个子节点;
    其中,所述步骤B包括:
    通过所述总节点将所述关于目标表的部分加写锁的请求分发至若干个所述子节点,对所述目标表的部分进行加写锁处理。
  15. 一种用于处理分布式事务的第一设备,其中,该设备包括:
    第一装置,用于获取SQL编译器发送的关于待处理的事务的目标表的加写锁请求,其中,所述事务包括若干条SQL语句;
    第二装置,用于基于所述加写锁请求,对所述目标表进行加写锁处理;
    第三装置,用于若所述加写锁处理成功,向所述SQL编译器发送所述目标表的待处理版本信息。
  16. 根据权利要求15所述的设备,其中,所述设备包括一个总节点及若干个子节点;
    其中,所述第二装置用于:
    通过所述总节点将所述加写锁请求分发至若干个所述子节点,对所述目标表进行加写锁处理。
  17. 根据权利要求16所述的设备,其中,所述设备还包括:
    第四装置,用于若所述总节点在预定时间内获取大于或等于预定数量的所述子节点关于所述加写锁请求的响应,则所述加写锁处理成功。
  18. 根据权利要求17所述的设备,其中,所述加写锁处理成功还包括:
    将新的加锁状态信息写入日志文件中;或
    所述总节点存储新的加锁状态信息。
  19. 根据权利要求18所述的设备,其中,所述设备还包括:
    第五装置,用于重启未响应所述加写锁请求的所述子节点,所述子节点从所述日志文件中读取最新的所述加锁状态信息;或
    第六装置,用于重启未响应所述加写锁请求的所述子节点,所述总节点将最新的所述加锁状态信息发送至所述子节点。
  20. 根据权利要求19所述的设备,其中,所述重启未响应所述加写锁请求的所述子节点还包括:
    若所述未响应所述加写锁请求的所述子节点无法重启,则通过所述总节点将所述子节点的服务迁移到其他物理机器上,并在该物理机器上启动所述子节点的服务。
  21. 根据权利要求15所述的设备,其中,所述第二装置用于:
    对所述目标表的部分进行加写锁处理;
    其中,所述设备还包括:
    第七装置,用于向第二设备发送关于所述目标表的部分加写锁的请求。
  22. 根据权利要求21所述的设备,其中,所述对所述目标表的部分进行加写锁处理包括:
    若所述目标表的全表已被加写锁,则忽略对所述目标表的部分进行加写锁处理;否则,对所述目标表的部分进行加写锁处理。
  23. 根据权利要求15所述的设备,其中,所述设备还包括:
    第八装置,用于当所述目标表的版本数量等于或大于预定版本数量阈值,对所述目标表的若干个版本进行合并处理。
  24. 根据权利要求15所述的设备,其中,所述设备还包括:
    第九装置,用于当获取事务提交语句,设置新版本的所述目标表的状态为有效,解锁所述目标表;或
    第十装置,用于当获取事务回滚语句,设置新版本的所述目标表的状态为无效,解锁所述目标表。
  25. 根据权利要求15所述的设备,其中,所述第一装置还用于:
    获取SQL编译器发送的关于待处理的事务的源表的加读锁请求;
    其中,所述第二装置还用于:
    基于所述加读锁请求,对所述源表进行加读锁处理;
    其中,所述第三装置还用于:
    若所述加读锁处理成功,向所述SQL编译器发送所述源表的待处理版本信息;
    其中,所述设备还包括:
    第十一装置,用于当获取事务提交语句或事务回滚语句,解锁所述源表。
  26. 一种用于处理分布式事务的第二设备,其中,该设备包括:
    第十二装置,用于获取第一设备发送的关于目标表的部分加写锁的请求;
    第十三装置,用于根据所述关于目标表的部分加写锁的请求,对所述目标表的部分进行加写锁处理。
  27. 根据权利要求26所述的设备,其中,所述第十三装置用于:
    根据所述关于目标表的部分加写锁的请求,向所述第一设备查询所述目标表的全表是否已被加写锁;
    若所述目标表的全表未被加写锁,对所述目标表的部分进行加写锁处理。
  28. 根据权利要求26所述的设备,其中,所述设备包括一个总节点及若干个子节点;
    其中,所述第十三装置用于:
    通过所述总节点将所述关于目标表的部分加写锁的请求分发至若干个所述子节点,对所述目标表的部分进行加写锁处理。
  29. 一种处理分布式事务的系统,其中,该系统包括如权利要求21或22所述的第一设备及如权利要求26至28中任一项所述的第二设备。
PCT/CN2017/080846 2016-04-22 2017-04-18 一种处理分布式事务的方法与设备 WO2017181931A1 (zh)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN201610254762.3A CN105955804B (zh) 2016-04-22 2016-04-22 一种处理分布式事务的方法与设备
CN201610254762.3 2016-04-22

Publications (1)

Publication Number Publication Date
WO2017181931A1 true WO2017181931A1 (zh) 2017-10-26

Family

ID=56915131

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2017/080846 WO2017181931A1 (zh) 2016-04-22 2017-04-18 一种处理分布式事务的方法与设备

Country Status (3)

Country Link
US (1) US11023446B2 (zh)
CN (1) CN105955804B (zh)
WO (1) WO2017181931A1 (zh)

Families Citing this family (10)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN105955804B (zh) 2016-04-22 2018-06-05 星环信息科技(上海)有限公司 一种处理分布式事务的方法与设备
CN107977376B (zh) * 2016-10-24 2020-07-07 腾讯科技(深圳)有限公司 分布式数据库系统及事务处理方法
CN109800062B (zh) * 2018-12-25 2020-02-07 星环信息科技(上海)有限公司 一种分布式数据库事务处理系统
CN111444199B (zh) * 2019-01-17 2023-11-14 阿里巴巴集团控股有限公司 数据处理方法及装置、存储介质和处理器
CN109828847B (zh) * 2019-01-25 2023-09-01 平安科技(深圳)有限公司 基于区块链的锁处理方法、装置、计算机设备及存储介质
GB2582590A (en) * 2019-03-26 2020-09-30 Ge Aviat Systems Ltd Method and system for fusing disparate industrial asset event information
CN111694851B (zh) * 2020-05-28 2023-06-16 平安科技(深圳)有限公司 分布式事务的交易处理方法及相关设备
CN112039970B (zh) * 2020-08-25 2023-04-18 北京思特奇信息技术股份有限公司 一种分布式业务锁服务方法、服务端、系统及存储介质
CN113268483A (zh) * 2021-05-24 2021-08-17 北京金山云网络技术有限公司 请求处理方法和装置、电子设备和存储介质
CN113836548B (zh) * 2021-09-01 2023-07-25 广州大学 智能合约的数据一致性保障方法、系统、设备及存储介质

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN102375955A (zh) * 2010-08-17 2012-03-14 伊姆西公司 网络文件系统联合命名空间内文件加锁的系统与方法
CN103942269A (zh) * 2014-03-26 2014-07-23 北京京东尚科信息技术有限公司 对文件系统进行操作的方法和装置
CN104572568A (zh) * 2013-10-15 2015-04-29 阿里巴巴集团控股有限公司 读锁操作方法、写锁操作方法及系统
CN105162878A (zh) * 2015-09-24 2015-12-16 网宿科技股份有限公司 基于分布式存储的文件分发系统及方法
CN105955804A (zh) * 2016-04-22 2016-09-21 星环信息科技(上海)有限公司 一种处理分布式事务的方法与设备

Family Cites Families (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6339772B1 (en) * 1999-07-06 2002-01-15 Compaq Computer Corporation System and method for performing database operations on a continuous stream of tuples
CN102122288A (zh) * 2010-12-21 2011-07-13 北京高森明晨信息科技有限公司 一种并发控制的方法及系统
CN103502940B (zh) * 2011-12-21 2017-12-12 华为技术有限公司 一种分布式系统中处理事务的方法、相关设备及系统
CN103365852A (zh) * 2012-03-28 2013-10-23 天津书生软件技术有限公司 一种文档库系统中的并发控制方法及系统
CN102831156B (zh) * 2012-06-29 2014-12-31 浙江大学 一种云计算平台上的分布式事务处理方法
CN103677771B (zh) * 2012-09-06 2017-01-18 阿里巴巴集团控股有限公司 一种并发事务的处理方法和装置
CN104793988B (zh) * 2014-01-20 2019-01-22 阿里巴巴集团控股有限公司 跨数据库分布式事务的实现方法和装置
US10108653B2 (en) * 2015-03-27 2018-10-23 International Business Machines Corporation Concurrent reads and inserts into a data structure without latching or waiting by readers

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN102375955A (zh) * 2010-08-17 2012-03-14 伊姆西公司 网络文件系统联合命名空间内文件加锁的系统与方法
CN104572568A (zh) * 2013-10-15 2015-04-29 阿里巴巴集团控股有限公司 读锁操作方法、写锁操作方法及系统
CN103942269A (zh) * 2014-03-26 2014-07-23 北京京东尚科信息技术有限公司 对文件系统进行操作的方法和装置
CN105162878A (zh) * 2015-09-24 2015-12-16 网宿科技股份有限公司 基于分布式存储的文件分发系统及方法
CN105955804A (zh) * 2016-04-22 2016-09-21 星环信息科技(上海)有限公司 一种处理分布式事务的方法与设备

Also Published As

Publication number Publication date
CN105955804B (zh) 2018-06-05
CN105955804A (zh) 2016-09-21
US20170308566A1 (en) 2017-10-26
US11023446B2 (en) 2021-06-01

Similar Documents

Publication Publication Date Title
WO2017181931A1 (zh) 一种处理分布式事务的方法与设备
US11314716B2 (en) Atomic processing of compound database transactions that modify a metadata entity
US11681684B2 (en) Client-driven commit of distributed write transactions in a database environment
US8170997B2 (en) Unbundled storage transaction services
US8655859B2 (en) Concurrency control for extraction, transform, load processes
US9336262B2 (en) Accelerated transactions with precommit-time early lock release
US9779128B2 (en) System and method for massively parallel processing database
EP3111325B1 (en) Automatically retrying transactions with split procedure execution
US8868514B2 (en) Transaction support for distributed data
US8898109B2 (en) Automatic transaction retry after session failure
US8126843B2 (en) Cluster-wide read-copy update system and method
JP4833590B2 (ja) 同時トランザクション(concurrenttransactions)とページ同期(pagesynchronization)
WO2018001135A1 (zh) 处理数据库事务的方法、客户端和服务器
US7996360B2 (en) Coordinating updates to replicated data
JP2022013854A (ja) 2フェーズコミット分散トランザクションを使用することによってデータベースを更新するための方法および装置
CN107710203B (zh) 分布式键/值存储库之上的事务数据库层
RU2746155C2 (ru) Уничтожение объекта на основе последовательности выполняемых действий
US11874816B2 (en) Lock free distributed transaction coordinator for in-memory database participants
US11113251B2 (en) Transaction manager
Padhye et al. Scalable transaction management with snapshot isolation for NoSQL data storage systems
CN112579307A (zh) 一种物理锁资源的分配检测方法、装置及电子设备
CN109800062B (zh) 一种分布式数据库事务处理系统
US9418097B1 (en) Listener event consistency points
CN108475211B (zh) 无状态系统和用于获得资源的系统
WO2021147375A1 (zh) 数据管理方法、装置、设备、计算机可读存储介质及系统

Legal Events

Date Code Title Description
NENP Non-entry into the national phase

Ref country code: DE

121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 17785415

Country of ref document: EP

Kind code of ref document: A1

122 Ep: pct application non-entry in european phase

Ref document number: 17785415

Country of ref document: EP

Kind code of ref document: A1