Detailed Description
In order to make the objects, technical solutions and advantages of the present disclosure more clear, the technical solutions of the present disclosure will be clearly and completely described below with reference to the specific embodiments of the present disclosure and the accompanying drawings. It is to be understood that the embodiments described are only a few embodiments of the present disclosure, and not all embodiments. All other embodiments obtained by a person of ordinary skill in the art based on the embodiments in the present specification without any creative effort belong to the protection scope of the present specification.
Embodiments of the present specification provide a distributed transaction processing method and terminal device, so as to solve the problems in the prior art that there are many execution links of distributed transaction processing and the total time consumption is long. The embodiments of the present specification provide a distributed transaction processing method, and an execution subject of the method may be, but is not limited to, a transaction manager of a distributed transaction or an apparatus or system capable of being configured to execute the method provided by the embodiments of the present specification.
For convenience of description, the following description will be made of an embodiment of the method, taking an execution subject of the method as a transaction manager capable of executing the method. It is to be understood that the execution of the method by the transaction manager is merely an exemplary illustration and should not be construed as a limitation of the method.
Fig. 3 is a flowchart of a distributed transaction processing method provided in an embodiment of the present specification, where the method of fig. 3 may be executed by a transaction manager, and as shown in fig. 3, the method may include:
step 310, responding to the request operation of the distributed transaction, respectively sending preparation operation requests of sub-transactions of the distributed transaction to a plurality of resource managers.
The prepare operation request requests validation of the execution results of the resources of the corresponding sub-transaction.
And 320, executing the transaction operation corresponding to the response result on the distributed transaction in an asynchronous mode based on the response result of the preparation operation request of the plurality of resource managers on the sub-transaction of the distributed transaction.
It should be understood that after the transaction manager obtains the response result of the preparation operation request of the multiple resource managers to the sub-transaction of the distributed transaction, the transaction manager executes the transaction operation corresponding to the response result to the distributed transaction in an asynchronous manner.
In other words, the transaction manager's processing of the distributed transaction is completed after the transaction manager obtains the results of the responses of the multiple resource managers to the prepare operation requests of the sub-transactions of the distributed transaction.
The response result may be a preparation success or a preparation failure; alternatively, the response result may be an execution success or an execution failure.
Executing the transaction operation corresponding to the response result on the distributed transaction in an asynchronous manner, which may specifically be:
the transaction manager reports response results of the preparation operation requests of the resource managers for the sub-transactions of the distributed transactions to other equipment, such as a server, and the other equipment executes the transaction operation corresponding to the response result according to the response result; or,
and the transaction manager distributes response results of the preparation operation requests of the resource managers to the sub-transactions of the distributed transactions to a plurality of synchronous threads in the thread pool, and the plurality of synchronous threads execute the transaction operation corresponding to the response result according to the response result.
In the embodiment of the description, based on the response result of the preparation operation request of the multiple resource managers for the sub-transaction of the distributed transaction, the transaction operation corresponding to the response result is executed on the distributed transaction in an asynchronous manner, so that the transaction operation corresponding to the response result is executed in an asynchronous manner, the execution link of distributed transaction processing is reduced, and the execution loss of the distributed transaction is shortened.
Optionally, as an embodiment, the distributed transaction processing method provided in this specification embodiment may further include:
receiving a transaction starting request sent by a client, and allocating a plurality of sub-transactions divided by a distributed transaction corresponding to the transaction starting request to a plurality of resource managers for processing a preparation operation.
For example, assuming "account a transfers to account B" as a distributed transaction, then a write operation needs to be performed on three resource managers (i.e., the distributed transaction can be divided into three sub-transactions), respectively:
operation 1, deducting a preset amount from the account A on the resource manager 1;
operation 2, adding corresponding amount to the account B on the resource manager 2;
and operation 3, performing transfer aggregation on the resource manager 3.
If the response results of the three resource managers after the preparation operation processing are all successful, determining to submit the distributed transaction; and if at least one response result in response results after the three resource managers perform the preparation operation processing is that the preparation is failed, determining to perform rollback operation on the distributed transaction.
In practical applications, the implementation of the method of the present specification is described in detail, for example, along with the above examples:
a user initiates an operation of transferring accounts from an account A to an account B at a client side, and registers a distributed transaction to a transaction manager according to the operation performed by the user; the transaction manager will assign a transaction identification ID to the distributed transaction. The "account a transfers to account B" requires a write operation on three resource managers, that is, the distributed transaction may be divided into three sub-transactions:
operation 1, deducting a preset amount from the account A on the resource manager 1;
operation 2, adding corresponding amount to the account B on the resource manager 2;
and operation 3, performing transfer aggregation on the resource manager 3.
If the response results of the three resource managers after the preparation operation processing are all successful, determining to submit the distributed transaction; and if at least one response result in response results after the three resource managers perform the preparation operation processing is that the preparation is failed, determining to perform rollback operation on the distributed transaction.
And after the sub-transaction processing is completed, the transaction manager executes the transaction operation corresponding to the response result on the distributed transaction in an asynchronous mode based on the response results of all the sub-transactions.
If so, after the transaction manager reports the response results of all the sub-transactions to the standby database, the server side serves as a coordinator to query the response results of all the sub-transactions in the standby database, and determines to initiate submission or rollback of the distributed transaction according to the queried response results of all the sub-transactions; or,
and the transaction manager distributes the sub-transactions respectively corresponding to the response results to a plurality of synchronous threads in the thread pool according to a preset distribution rule, and the synchronous threads determine to initiate the submission or rollback of the distributed transaction based on the response results.
Optionally, as an embodiment, the step 120 may be specifically implemented as:
distributing sub-transactions respectively corresponding to the plurality of response results to a plurality of synchronous threads in a thread pool according to a preset distribution rule, wherein the thread pool is pre-configured with the plurality of synchronous threads;
and the synchronous threads in the thread pool are used for executing the transaction operation corresponding to the response result on the distributed transaction in an asynchronous mode.
In this application, a thread pool needs to be established in advance, and the synchronous threads in the thread pool in this embodiment may be: a thread that calls run method execution is required.
For example, 10 synchronous threads are configured in advance in the thread pool, the 10 thread synchronizations need to be executed by a run method, and the 10 synchronous threads executed by the run method can be executed in the main thread during execution, so that the ending time of the synchronous thread executing the task can be perceived by the main thread.
The thread pool can directly or indirectly extend from the threadpoolsexecutor provided by JDK (Java Development Kit, software Development Kit in Java language). The thread pool (doimmediateltylthreadthreadpottaskeexecutor) in this embodiment is different from the thread pool in the prior art, and the synchronous threads of the thread pool in this embodiment all call run methods to be executed, so that the synchronous threads can be synchronously executed in the called main thread.
The sub-transactions respectively corresponding to the multiple response results are allocated to multiple synchronous threads in a thread pool according to a preset allocation rule, and the multiple synchronous threads are preconfigured in the thread pool, and the specific implementation may include:
step A1: and establishing a task queue for the thread pool.
Firstly, a task queue is established for the created thread pool, and the task queue can store the tasks distributed to the synchronous threads in the thread pool according to the sequence.
Step A2: and adding the plurality of sub-transactions into the task queue according to the users triggering the plurality of sub-transactions respectively.
In this embodiment, the plurality of sub-transactions may be added to the task queue according to different users who trigger the sub-transactions.
For example, for the buffering and account-repairing service in practical application, there are four users that trigger 7 tasks of buffering and account-repairing.
Specifically, as shown in table 1:
TABLE 1
User tasks |
id |
A |
1 |
A |
2 |
A |
3 |
B |
4 |
B |
5 |
C |
6 |
D |
7 |
Then, the task queue may add the three sub-transactions of user a, then add the two sub-transactions of user B, and then add the tasks of user C and user D in sequence.
Step A3: and respectively distributing the sub-transactions in the task queue to a plurality of synchronous threads in a thread pool, wherein each user corresponds to one synchronous thread.
And then, the sub-transactions in the task queue are respectively allocated to a plurality of synchronous threads in the thread pool, for example, the sub-transaction of each user is correspondingly allocated to one synchronous thread. Then, referring to Table 1, three sub-transactions for user A may be assigned to the first synchronous thread in the thread pool, two sub-transactions for user B may be assigned to the second synchronous thread in the thread pool, and so on.
Certainly, in practical application, the method is not limited to allocating the threads to the tasks by taking the user as the dimension, and may also allocate one synchronization thread to each sub-transaction by taking the sub-transactions as the dimension, and of course, other allocation manners may also be adopted.
As shown in fig. 4, in the embodiment of the present specification, in the two-phase transaction manager, the sub-transactions corresponding to the multiple response results returned by all the resource managers in the one phase are respectively allocated to the multiple synchronous threads in the thread pool according to the preset allocation rule, and the multiple synchronous threads in the thread pool execute the transaction operation corresponding to the response result on the distributed transaction in an asynchronous manner, so that the transaction manager reduces the total execution link of the distributed transaction, and the total consumption time is changed to the sum of a + b + c + g, thereby effectively reducing the execution loss of the distributed transaction.
Optionally, as an embodiment, the step 120 may be specifically implemented as:
and storing the response result and the corresponding relation between the response result and the transaction operation request corresponding to the response result in a standby database, so that other equipment asynchronously executes the transaction operation corresponding to the response result based on the response result inquired in the standby database.
The backup database may be a database of other devices, or may be an independent database with respect to other devices, and the embodiments of the present specification are not particularly limited.
For example, after the transaction manager reports the response results of all the sub-transactions to the standby database, the server side, as a coordinator, queries the response result of each sub-transaction in the standby database, and determines to initiate submission or rollback of the distributed transaction according to the queried response result of each sub-transaction, specifically, if the response results of all the sub-transactions of the distributed transaction are successfully prepared, it is determined to submit the distributed transaction, and if at least one response result of all the sub-transactions is a sub-transaction that fails to be prepared, it is determined to rollback the distributed transaction. Specifically, after determining to commit or rollback the distributed transaction, the server sends a commit command or a rollback command to each of the sub-transactions to initiate a commit or rollback of the global transaction. Therefore, the committing of the distributed transaction can be executed according to the received command which is sent by the server and used for committing the distributed transaction; or according to a received command of rolling back the distributed transaction sent by the server, the roll back of the distributed transaction is executed. That is, if a command to commit the distributed transaction is received, which is sent by the server, the commit of the distributed transaction is performed; and if the received command sent by the server for rolling back the distributed transaction is received, the roll back of the distributed transaction is executed.
In the embodiment of the present specification, the response result and the corresponding relationship between the response result and the transaction operation request corresponding to the response result are stored in the standby database, and the other device executes the transaction operation corresponding to the response result based on the response result queried in the standby database, so that the purpose that the transaction operation corresponding to the response result is executed in an asynchronous manner is achieved, the execution link of the distributed transaction processing is reduced, and the execution loss of the distributed transaction is reduced.
The distributed transaction processing method according to the embodiment of the present disclosure is described in detail above with reference to fig. 3 to 4, and the transaction manager according to the embodiment of the present disclosure is described in detail below with reference to fig. 5.
Fig. 5 is a schematic structural diagram of a transaction manager provided in an embodiment of this specification, and as shown in fig. 5, the transaction manager 500 may include:
a sending module 501, configured to send, in response to a request operation of a distributed transaction, a preparation operation request of a sub-transaction of the distributed transaction to a plurality of resource managers, respectively, where the preparation operation request is used to request to confirm an execution result of a resource of the corresponding sub-transaction;
and the executing module 503 is configured to execute, based on response results of the preparation operation requests of the multiple resource managers for the sub-transactions of the distributed transaction, the transaction operation corresponding to the response result on the distributed transaction in an asynchronous manner.
In one embodiment, the execution module 503 may include:
the allocation unit is used for allocating the sub-transactions respectively corresponding to the plurality of response results to a plurality of synchronous threads in a thread pool according to a preset allocation rule, and the thread pool is pre-configured with the plurality of synchronous threads;
and the execution unit is used for executing the transaction operation corresponding to the response result on the distributed transaction in an asynchronous mode by a plurality of synchronous threads in the thread pool.
In one embodiment, the allocation unit may include:
the establishing subunit is used for establishing a task queue for the thread pool;
the adding subunit is used for respectively adding the plurality of sub-transactions into the task queue according to each user triggering the plurality of sub-transactions;
and the distribution subunit is used for respectively distributing the sub-transactions in the task queue to a plurality of synchronous threads in a thread pool, wherein each user corresponds to one synchronous thread.
In one embodiment, the execution module 503 may include:
and the storage unit is used for storing the response result and the corresponding relation between the response result and the transaction operation request corresponding to the response result in a standby database, so that other equipment asynchronously executes the transaction operation corresponding to the response result based on the response result inquired in the standby database.
In one embodiment, the transaction manager may further include:
the receiving and allocating module 505 is configured to receive a transaction start request sent by a client, and allocate a plurality of sub-transactions divided by a distributed transaction corresponding to the transaction start request to the plurality of resource managers to perform preparation operation.
In the embodiment of the description, based on the response result of the preparation operation request of the multiple resource managers for the sub-transaction of the distributed transaction, the transaction operation corresponding to the response result is executed on the distributed transaction in an asynchronous manner, so that the transaction operation corresponding to the response result is executed in an asynchronous manner, the execution link of distributed transaction processing is reduced, and the execution loss of the distributed transaction is shortened.
The distributed transaction processing method according to the embodiment of the present disclosure is described in detail above with reference to fig. 3 to 4, and the distributed transaction processing system according to the embodiment of the present disclosure is described in detail below with reference to fig. 6.
Fig. 6 is a schematic structural diagram of a distributed transaction processing system provided in an embodiment of the present specification, and as shown in fig. 6, the distributed transaction processing system 600 may include:
the transaction manager 601 responds to the request operation of the distributed transaction, and sends preparation operation requests of sub-transactions of the distributed transaction to a plurality of resource managers respectively, wherein the preparation operation requests are used for requesting to confirm execution results of resources of the corresponding sub-transactions;
the transaction manager 601 distributes the sub-transactions corresponding to the response results to a plurality of synchronous threads 603 in a thread pool according to a preset distribution rule, wherein the thread pool is configured with a plurality of synchronous threads in advance;
and executing the transaction operation corresponding to the response result on the distributed transaction by a plurality of synchronous threads 603 in the thread pool in an asynchronous mode.
In the embodiment of the description, based on the response result of the preparation operation request of the multiple resource managers for the sub-transaction of the distributed transaction, the transaction operation corresponding to the response result is executed on the distributed transaction in an asynchronous manner, so that the transaction operation corresponding to the response result is executed in an asynchronous manner, the execution link of distributed transaction processing is reduced, and the execution loss of the distributed transaction is shortened.
The distributed transaction processing method according to the embodiment of the present disclosure is described in detail above with reference to fig. 3 and 4, and the distributed transaction processing system according to the embodiment of the present disclosure is described in detail below with reference to fig. 7.
Fig. 7 is a schematic structural diagram of a distributed transaction processing system provided in an embodiment of the present specification, and as shown in fig. 7, the distributed transaction processing system 700 may include:
the transaction manager 701 responds to a request operation of a distributed transaction, and respectively sends preparation operation requests of sub-transactions of the distributed transaction to a plurality of resource managers, wherein the preparation operation requests are used for requesting to confirm execution results of resources of the corresponding sub-transactions;
the transaction manager 701 stores the response result and the corresponding relationship between the response result and the transaction operation request corresponding to the response result in the standby database 703;
the other device 705 asynchronously executes the transaction operation corresponding to the response result based on the response result queried in the standby database.
In the embodiment of the description, based on the response result of the preparation operation request of the multiple resource managers for the sub-transaction of the distributed transaction, the transaction operation corresponding to the response result is executed on the distributed transaction in an asynchronous manner, so that the transaction operation corresponding to the response result is executed in an asynchronous manner, the execution link of distributed transaction processing is reduced, and the execution loss of the distributed transaction is shortened.
Fig. 8 is a schematic structural diagram of a terminal device provided in an embodiment of the present specification. Referring to fig. 8, in the hardware level, the terminal device includes a processor, and optionally further includes an internal bus, a network interface, and a memory. The memory may include a memory, such as a Random-access memory (RAM), and may further include a non-volatile memory, such as at least 1 disk memory. Of course, the terminal device may also include hardware required for other services.
The processor, the network interface, and the memory may be connected to each other via an internal bus, which may be an ISA (Industry Standard Architecture) bus, a PCI (peripheral component Interconnect) bus, an EISA (Extended Industry Standard Architecture) bus, or the like. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of illustration, only one double-headed arrow is shown in FIG. 8, but that does not indicate only one bus or one type of bus.
And the memory is used for storing programs. In particular, the program may include program code comprising computer operating instructions. The memory may include both memory and non-volatile storage and provides instructions and data to the processor.
The processor reads the corresponding computer program from the nonvolatile memory to the memory and then runs the computer program to form the association device of the resource value-added object and the resource object on the logic level. The processor is used for executing the program stored in the memory and is specifically used for executing the following operations:
responding to a request operation of a distributed transaction, respectively sending preparation operation requests of sub-transactions of the distributed transaction to a plurality of resource managers, wherein the preparation operation requests are used for requesting to confirm execution results of resources of the corresponding sub-transactions;
and executing the transaction operation corresponding to the response result on the distributed transaction in an asynchronous mode based on the response result of the plurality of resource managers to the preparation operation request of the sub-transaction of the distributed transaction.
In the embodiment of the description, based on the response result of the preparation operation request of the multiple resource managers for the sub-transaction of the distributed transaction, the transaction operation corresponding to the response result is executed on the distributed transaction in an asynchronous manner, so that the transaction operation corresponding to the response result is executed in an asynchronous manner, the execution link of distributed transaction processing is reduced, and the execution loss of the distributed transaction is shortened.
The distributed transaction processing method disclosed in the embodiment of fig. 3 in this specification can be applied to or implemented by a processor. The processor may be an integrated circuit chip having signal processing capabilities. In implementation, the steps of the above method may be performed by integrated logic circuits of hardware in a processor or instructions in the form of software. The Processor may be a general-purpose Processor, including a Central Processing Unit (CPU), a Network Processor (NP), and the like; but also Digital Signal Processors (DSPs), Application Specific Integrated Circuits (ASICs), Field Programmable Gate Arrays (FPGAs) or other Programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The various methods, steps and logic blocks disclosed in one or more embodiments of the present specification may be implemented or performed. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like. The steps of a method disclosed in connection with one or more embodiments of the present disclosure may be embodied directly in hardware, in a software module executed by a hardware decoding processor, or in a combination of the hardware and software modules executed by a hardware decoding processor. The software module may be located in ram, flash memory, rom, prom, or eprom, registers, etc. storage media as is well known in the art. The storage medium is located in a memory, and a processor reads information in the memory and completes the steps of the method in combination with hardware of the processor.
The terminal device may further execute the distributed transaction processing method of fig. 1 executed by the distributed transaction processing apparatus of fig. 3, which is not described herein again.
Of course, the terminal device in this specification does not exclude other implementations, such as a logic device or a combination of software and hardware, and the like, besides the software implementation, that is, the execution subject of the following processing flow is not limited to each logic unit, and may also be hardware or a logic device.
Embodiments of the present disclosure further provide a computer-readable storage medium, where a computer program is stored on the computer-readable storage medium, and when the computer program is executed by a processor, the computer program implements each process of the above method embodiments, and can achieve the same technical effect, and in order to avoid repetition, details are not repeated here. The computer-readable storage medium may be a Read-Only Memory (ROM), a Random Access Memory (RAM), a magnetic disk or an optical disk.
As will be appreciated by one skilled in the art, embodiments of the present description may be provided as a method, system, or computer program product. Accordingly, the description may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the description may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
The description has been presented with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the description. It will be understood that each flow and/or block of the flow diagrams and/or block diagrams, and combinations of flows and/or blocks in the flow diagrams and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create a system for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including an instruction system which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
In a typical configuration, a computing device includes one or more processors (CPUs), input/output interfaces, network interfaces, and memory.
The memory may include forms of volatile memory in a computer readable medium, Random Access Memory (RAM) and/or non-volatile memory, such as Read Only Memory (ROM) or flash memory (flash RAM). Memory is an example of a computer-readable medium.
Computer-readable media, including both non-transitory and non-transitory, removable and non-removable media, may implement information storage by any method or technology. The information may be computer readable instructions, data structures, modules of a program, 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 disc read only memory (CD-ROM), Digital Versatile Discs (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices, or any other non-transmission medium that can be used to store information that can be accessed by a computing device. As defined herein, a computer readable medium does not include a transitory computer readable medium such as a modulated data signal and a carrier wave.
The foregoing description describes certain embodiments of the specification. Other embodiments are within the scope of the following claims. In some cases, the actions or steps recited in the claims may be performed in a different order than in the embodiments and still achieve desirable results. In addition, the processes depicted in the accompanying figures do not necessarily require the particular order shown, or sequential order, to achieve desirable results. In some embodiments, multitasking and parallel processing may also be possible or may be advantageous.
It should also be noted that the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other identical elements in the process, method, article, or apparatus that comprises the element.
The above description is only an example of the present specification, and is not intended to limit the present specification. Various modifications and alterations to this description will become apparent to those skilled in the art. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present specification should be included in the scope of the claims of the present specification.