Disclosure of Invention
In order to solve the technical problem or at least partially solve the technical problem, the present application provides a data processing method and apparatus.
In a first aspect, an embodiment of the present application provides a data processing method, including:
acquiring a message to be sent and service data sent by a sender system; the message to be sent is generated according to the service data;
generating task information according to the message to be sent; wherein the task information comprises the message to be sent;
establishing an execution policy that associates a first operation with a second operation; the first operation comprises writing the business data into a database, the second operation comprises inserting the task information into a task table, and the execution strategy is used for enabling the first operation and the second operation to succeed or fail at the same time;
and respectively executing the first operation and the second operation.
Further, as in the foregoing data processing method, the executing the first operation and the second operation includes:
acquiring service processing result data in the service data;
judging whether the service data meet the writing requirements written into the database or not according to the service processing result data;
and when the service data meet the writing requirement, executing the first operation.
Further, as the foregoing data processing method, when the service data does not meet the write requirement, the method further includes:
rolling back an execution procedure when the second operation is executed.
In a second aspect, an embodiment of the present application provides a data processing method, including:
acquiring a message to be sent from a task table;
sending the message to be sent to a message consumer;
obtaining a sending result of sending the message to be sent to the message consumer;
and executing corresponding processing on the message to be sent according to the sending result.
Further, as in the aforementioned data processing method,
the acquiring the message to be sent from the task table includes:
scanning the task list according to a preset scanning frequency;
the sending the message to be sent to a message consumer includes:
and sending the message to be sent to the message consumer according to a preset sending frequency.
Further, as in the foregoing data processing method, the executing a corresponding processing policy on the message to be sent according to the sending result includes:
when the sending result represents that the message to be sent is sent successfully, stopping sending the message to be sent again;
when the sending result represents that the sending of the message to be sent fails and the retransmission times are within a preset task retry interval, retransmitting the message to be sent;
and when the sending result represents that the sending of the message to be sent fails and the retransmission times are not within a preset task retry interval, generating alarm information.
Further, as in the foregoing data processing method, the obtaining a sending result of sending the message to be sent to the message consumer includes:
acquiring message callback information sent by the message consumer; the message callback information is generated by the message consumer according to a callback field in the message to be sent, and is used for representing whether the message to be sent is successfully consumed by the message consumer;
determining a database routing field in the message callback information, and determining a first task table corresponding to the message callback information in a database according to the database routing field;
and changing the task state of the task information in the first task table according to the message callback information.
In a third aspect, an embodiment of the present application provides a data processing apparatus, including:
the acquisition module is used for acquiring a message to be sent and service data sent by a sender system; the message to be sent is generated according to the service data;
the generating module is used for generating task information according to the message to be sent; wherein the task information comprises the message to be sent;
a transaction module to establish an execution policy that associates a first operation with a second operation; the first operation comprises writing the business data into a database, the second operation comprises inserting the task information into a task table, and the execution strategy is used for enabling the first operation and the second operation to succeed or fail at the same time;
and the execution module is used for respectively executing the first operation and the second operation.
In a fourth aspect, an embodiment of the present application provides a data processing apparatus, including:
the first acquisition module is used for acquiring a message to be sent from the task table;
the sending module is used for sending the message to be sent to a message consumer;
the second acquisition module is used for acquiring a sending result of the message to be sent to the message consumer;
and the execution module is used for executing corresponding processing on the message to be sent according to the sending result.
In a fifth aspect, an embodiment of the present application provides a data processing system, including the data processing apparatus as described in the third aspect and the data processing apparatus as described in the fourth aspect.
In a sixth aspect, an embodiment of the present application provides an electronic device, including: the system comprises a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory are communicated with each other through the communication bus;
the memory is used for storing a computer program;
the processor is configured to implement the processing method according to any one of the first aspect and the second aspect when executing the computer program.
In a seventh aspect, this application embodiment provides a non-transitory computer-readable storage medium storing computer instructions that cause the computer to perform the processing method of any one of the first and second aspects.
The embodiment of the application provides a data processing method and a data processing device, wherein the data processing method comprises the following steps: acquiring a message to be sent and service data; the message to be sent is generated according to the service data; generating task information; wherein the task information comprises the message to be sent; establishing an execution policy that associates a first operation with a second operation; the first operation comprises writing the business data into a database, the second operation comprises inserting the task information into a task table, and the execution strategy is used for enabling the first operation and the second operation to succeed or fail at the same time; and respectively executing the first operation and the second operation. The method can separate the message sending system from the service systems such as the sender system and the like, and can avoid the problem that the database transaction is not submitted for a long time due to overtime message sending, so that the corresponding database connection can be occupied and can not be released; when the access amount of the service system is large, the occupied database can not be released, and finally the service request of the user can not have the database connection, so that the system availability is reduced on the system level, and the user level has experience problems of operation failure, retry request and the like; and then the database of the service system can be released in time, and the availability of the system is guaranteed.
Detailed Description
In order to make the objects, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application, and it is obvious that the described embodiments are some embodiments of the present application, but not all embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
Fig. 1 is a data processing method according to an embodiment of the present application, including the following steps S11 to S14:
s11, obtaining a message to be sent and service data; the message to be sent is generated according to the service data;
specifically, the device for executing the method may be a database in a payment scenario, and a sender system of the message to be sent and the service data is generally a service system, for example: after the user performs online payment operation, the sender system needs to generate corresponding service data (including but not limited to payment time, payment amount, and payment merchant) and a message to be sent for notifying the user whether payment is successful (for example, a certain order is paid successfully) according to the payment operation; after the message to be sent and the service data are obtained, the service system needs to write the message to be sent and the service data into the database so as to realize the storage of the service data and the subsequent message sending;
s12, generating task information according to the message to be sent; the task information comprises a message to be sent;
specifically, the task information may be data for writing in a task table (schedule), and generally, the task table is established in a database, so that the task information is inserted into the task table, that is, the purpose of writing a message to be sent into the database is also achieved, and the task information is used for recording whether the message to be sent is successfully sent, and in addition, the task information in the task table can be actively changed according to a specific sending result according to a platform for sending the message to be sent; the task table is as follows: a tool for managing tasks, which can add tasks to a task list when in use; tasks needing to be processed immediately can be added into the task table, and the task state in the task table can be modified according to the actual execution result, for example, the tasks can be executed when being processed, and completed when the tasks are completed; optionally, the data stored in the task table includes: service type, task state, service data, database routing information, UUID duplication prevention, retry times and whether to call back fields.
The message to be sent can be a message to be sent to a user or a message required by other systems in the background of the service party (for example, the message to be sent needs to be acquired for storage or data processing, etc.), and the message to be sent is generally sent through a message sending platform; the task information comprises information such as a sending result of the corresponding message to be sent, so the task information comprises the message to be sent;
s13, establishing an execution strategy for associating the first operation with the second operation; the first operation comprises writing business data into a database, the second operation comprises inserting task information into a task table, and an execution strategy is used for enabling the first operation and the second operation to be successful or failed at the same time; optionally, the first operation and the second operation are set in the same transaction to implement the function corresponding to the execution policy in this step; furthermore, the first operation is also decoupled from the third operation; the third operation is to send a message to be sent; because the task table is established in the database, the task information is inserted into the task table, namely, the purpose of writing the message to be sent into the database is also realized;
specifically, in the prior art, the following problems may occur when the service data is written into the database and the message is sent in the same transaction:
1. if the message is sent overtime, the database transaction is not submitted for a long time, and the corresponding database connection is occupied and cannot be released; when the access amount of a service system is large, the service request of a user is not available in database connection, the system availability is reduced on a system level, and the user level has experience problems such as operation failure, retry request and the like;
2. when the message sending fails, if the message is captured abnormally, the service data cannot be synchronized to a downstream system, and the upstream and downstream data are inconsistent, if data compensation is performed through other modes, for example, abnormal information is recorded for data compensation, time and labor are consumed, and the final consistency of the upstream and downstream data cannot be completely guaranteed; if the exception is not captured, the service data is rolled back, and the return operation at the system level fails, so that although the consistency of upstream and downstream data can be ensured, the system availability and the user experience are influenced;
therefore, it is necessary to decouple the writing of the service data into the database (i.e. the first operation) and the sending of the message (i.e. the third operation) in order to solve the above technical problem;
however, it is also necessary to ensure that the service data and the message to be sent are simultaneously stored in the database to ensure the consistency of the data, and in order to achieve this purpose, the first operation and the second operation may be set in the same "Transaction". And the transaction: is a series of rigorous logical operations in a program, and all operations must be completed successfully, otherwise all changes made in each operation are undone; and the transaction has four characteristics as follows: 1. atomicity (Atomicity): when all instructions related to the same transaction are operated, all the instructions are executed successfully or not; if only one instruction fails to be executed, even if partial execution succeeds, rolling back the data which is successfully executed, judging that all instructions fail to be executed, and returning to the data state before the instructions are executed; 2. consistency (Consistency): the execution of a transaction causes the data to transition from one state to another, but the integrity of the entire data remains stable; 3. isolation (Isolation): the isolation is that when a plurality of users access the database concurrently, for example, when the same table is operated, the database is a transaction opened by each user and cannot be interfered by the operation of other transactions, and a plurality of concurrent transactions are isolated from each other; 4. durability (durabilty): when a transaction completes correctly, it is permanent to changes to the data. In the embodiment, the atomicity of the transaction is applied, so that the service data and the message to be sent are ensured to be written into the database at the same time.
S14, respectively executing a first operation and a second operation;
specifically, taking the example that the first operation and the second operation are set in the same "transaction", after the first operation and the second operation are executed respectively, according to the atomicity of the transaction, when one of the operations fails to be executed successfully, both the operations fail to be executed, and the data in the database is rolled back to the data state executed by each operation; wherein, whether the first operation is successfully executed is caused by a service execution result in the service data;
therefore, by the method in the embodiment, it is possible to realize: when the message is sent overtime, because the database affair is not submitted for a long time, the corresponding database connection is occupied and can not be released; when the access amount of the service system is large, the occupied database can not be released, and finally the service request of the user can not have the database connection, so that the system availability is reduced on the system level, and the user level has experience problems of operation failure, retry request and the like; and then the database of the service system can be released in time, and the availability of the system is guaranteed.
As shown in fig. 2, in some embodiments, as in the aforementioned data processing method, the first operation performed in step S14 includes the following steps S141 to S143:
s141, acquiring service processing result data in the service data;
specifically, the service processing result data is as follows: whether the service is successfully handled or executed, for example, when the service operation performed by the user is payment, the service processing result data is: whether the user successfully pays the deduction; when a user starts a payment process and fails to pay successfully due to network reasons, unwilling payment and other conditions in the final confirmation payment link, the corresponding execution service processing result is payment failure;
s142, judging whether the service data meet the writing requirements written into the database or not according to the service processing result data;
specifically, not all the service data need to be written into the database, whether the service data need to be written into the database can be matched through a preset writing requirement (rule), the writing requirement can be set according to the preset requirement, and the writing requirements corresponding to different service data types are generally different; the service data is only in accordance with the writing requirement (namely when the rule is matched) under the condition that the service processing result meets the requirement; for example, in the payment system, the database is only used for storing the payment success service data, and one optional determination method is as follows: judging through field information corresponding to the payment result, and writing in only when the field information corresponding to the payment result is success; therefore, in the generation of the service data a according to a payment behavior, if the field information corresponding to the payment result is success, the service data a meets the write-in requirement; in the generation of the service data B according to the other payment behavior, if the field information corresponding to the payment result is fail, the service data B does not meet the writing requirement;
s143, when the service data meet the writing requirement, executing a first operation;
that is, when the service data meets the predetermined writing requirement, the first operation is executed, so that writing of useless service data into the database can be avoided.
In some embodiments, as the foregoing data processing method, when the service data does not meet the writing requirement, the first operation is not executed, and the method further includes the following steps:
rolling back the execution process when the second operation is executed;
that is, when the service data does not meet the writing requirement, the judgment result is obtained, and the first operation is judged not to be executed according to the judgment result;
because the first operation and the second operation do not have a sequence when executed, there may be a case that the service data is determined not to meet the writing requirement after the second operation is written; in order to ensure the atomicity of the transaction, the process that the second operation has been executed needs to be rolled back, so that the data in the database is returned to the state that the second operation is not executed.
As shown in fig. 3, according to another embodiment of the present application, there is also provided a data processing method including the following steps S21 to S24:
s21, obtaining a message to be sent from a task table;
specifically, on the basis of the foregoing embodiment, the message to be sent may be actively obtained from the task table by the message sending platform that implements the method of this embodiment, or may be actively sent to the message sending platform by the task table;
s22, sending the message to be sent to a message consumer;
specifically, the message consumer is a receiver for receiving a message to be sent or a requester for actively requesting the message to be sent; for example: the message consumer can be a user side which receives the payment result after online payment, and can also be other systems which are positioned in the background of the same service side as the platform for sending the message to be sent; after receiving the message to be sent, the message consumer may perform a downstream RPC call (a way of processing data to a downstream system, such as short message content data) or update its own data according to the message to be sent, where the update of its own data may be, for example: when receiving a data notification from an external system, the service system can be temporarily stored in a task table of the service system, the task system scans an external data notification record in the task table and sends a message, the service system consumes the message and performs corresponding service processing and data updating, and thus the method has the advantages of facilitating service expansion of the service system, greatly promoting pressure reduction of the service system and the like;
s23, obtaining a sending result of a message to be sent by a targeted message consumer;
specifically, the sending result, that is, whether the message to be sent is successfully consumed by the message consumer, may be used to determine whether the message needs to be sent to the message consumer again, and the sending result may include: successful or failed transmission; if the transmission is successful, repeated transmission is not needed, and if the transmission is failed, the transmission is needed again until the message to be transmitted is successfully received by the message consumer; furthermore, different messages to be sent are provided with different duplication prevention fields, so that a message consumer can perform idempotent consumption on the messages to be sent according to the duplication prevention fields, namely, the situation of repeated consumption is prevented, and the problem of repeated writing of data or repeated sending of information is avoided, thereby affecting the system performance and the user experience;
s24, executing corresponding processing on the message to be sent according to the sending result;
specifically, the processing corresponding to different sending results is generally different, and the processing strategy can be set according to the preset requirement, as long as the message to be sent can be guaranteed to be successfully consumed.
In some embodiments, a data processing method, as previously described,
step S21 is to obtain a message to be sent from the task table, including:
scanning the task list according to a preset scanning frequency;
specifically, the message to be sent is obtained by scanning the task table actively; and the preset scanning frequency can be a frequency set by people, such as: once per second, 10 scans per second, etc.; if the message to be sent is obtained through scanning, the fact that the scanning party actively obtains the message to be sent means; thus, the tasks of messaging may all be performed by a server, such as a messaging platform;
sending a message to be sent to a message consumer, comprising:
sending the message to be sent to a message consumer according to a preset sending frequency;
that is to say, the frequency when sending the message is also controllable, and the message is sent at a fixed sending frequency, so that the problem of interface congestion can be effectively alleviated under the condition of high concurrent message sending requirements, and the processing efficiency of the system can be improved.
As shown in fig. 4, in some embodiments, as in the foregoing data processing method, the step S24 executes a corresponding processing policy on the message to be sent according to the sending result, including the following steps S241 to S243:
s241, when the sending result represents that the message to be sent is sent successfully, stopping sending the message to be sent again;
s242, when the sending result represents that the sending of the message to be sent fails and the retransmission times are within a preset task retry interval, retransmitting the message to be sent;
s243, when the sending result represents that the sending of the message to be sent fails and the retransmission times are not within a preset task retry interval, generating alarm information;
specifically, in the application, the message can be retransmitted through the message middleware, wherein the message middleware is a message queue. The message retry mechanism is a message compensation mechanism implemented by the message queue, and when a problem occurs in the message consumption process of the consumer (for example, calling a downstream interface is overtime or fails), the message queue can push the message to the message consumer again by using the compensation mechanism of the message queue, that is, the retry mechanism, and the consumer can receive the message again to perform corresponding service processing until the consumer successfully consumes the message, so that the message can be guaranteed to be correctly consumed;
the embodiment of the application further includes step S243, when the message queue is used for sending for multiple times (exceeding the number of task retries), the sending is not performed any more, and an alarm is performed instead, so that the sending of other messages in the message queue can be prevented from being influenced, and the problem that the message cannot be sent is judged and checked by changing the alarm to manual operation.
As shown in fig. 5, in some embodiments, as in the foregoing data processing method, the obtaining of the sending result of the message to be sent to the message consumer in step S23 includes the following steps S231 to S233:
s231, message callback information sent by a message consumer is acquired; the message callback information is generated by a message consumer according to a callback field in a message to be sent, and is used for representing whether the message to be sent is successfully consumed by the message consumer;
specifically, one method for acquiring the message callback information may be: the message body of the message to be sent comprises a field of 'whether to call back information', namely isCallback, a message consumer judges the isCallback after successful consumption, if the isCallback is 1, the isCallback needs to be called back, the message consumer updates the state of the corresponding message record in the task table of the service library to be successful consumption through a task system such as the message sending platform, and the like, and does not update when the consumption is failed. The consumer can also realize the failure retry mechanism of self consumption, and can retry by relying on the task system strategy (for example, after the message record in the task table is successfully sent, if the message record is not changed into the consumption success in a long time, the message is tried to be sent again for retrying)
S232, determining a database routing field in the message callback information, and determining a first task table corresponding to the message callback information in a database according to the database routing field;
generally, many service systems need to be accessed to the task system, each service system has its own task table, a database routing field (i.e. a database routing key) is the task table for distinguishing different service systems, and the corresponding message to be sent can be found and stored in which task table of which service system library is stored through the database routing field; in addition, the database routing field can exist in the message to be sent, so that when the message callback information is generated, the database routing field can be taken out and written into the message callback information, and then the first task table corresponding to the message callback information can be obtained according to the field positioning of the database route;
s233, changing the task state of the task information in the first task table according to the message callback information;
specifically, the updating of the task table data in the database is to update the corresponding message record state in the task table according to the processing result corresponding to the message callback information after the task system finishes processing the data, for example: after a certain message is successfully sent to a corresponding message consumer, the task system updates the state of the corresponding message in the task table to be sent, and when the message is not successfully sent, the task system updates the state of the corresponding message in the task table to be failed in sending, and the other states are changed in the same way.
As shown in fig. 6, according to another embodiment of the present application, there is also provided a data processing apparatus including:
an obtaining module 1, configured to obtain a message to be sent and service data; the message to be sent is generated according to the service data;
the generating module 2 is used for generating task information according to the message to be sent; the task information comprises a message to be sent;
a transaction module 3 for establishing an execution policy associating a first operation with a second operation; the first operation comprises writing business data into a database, the second operation comprises inserting task information into a task table, and an execution strategy is used for enabling the first operation and the second operation to be successful or failed at the same time;
and the execution module 4 is used for executing the first operation and the second operation.
Specifically, the specific process of each module in the apparatus according to the embodiment of the present invention to implement its function may refer to the related description in the corresponding method embodiment, and is not described herein again.
As shown in fig. 7, according to another embodiment of the present application, there is also provided a data processing apparatus including:
a first obtaining module 5, configured to obtain a message to be sent from a task table;
the sending module 6 is used for sending the message to be sent to the message consumer;
a second obtaining module 7, configured to obtain a sending result of sending a message to be sent to a message consumer;
and the execution module 8 is configured to execute corresponding processing on the message to be sent according to the sending result.
Specifically, the specific process of each module in the apparatus according to the embodiment of the present invention to implement its function may refer to the related description in the corresponding method embodiment, and is not described herein again.
According to another embodiment of the present application, there is also provided a data processing system, which includes the data processing apparatus in the foregoing embodiment shown in fig. 6 and the data processing apparatus in the foregoing embodiment shown in fig. 7.
The system can solve the problem that the service data and the information to be sent are simultaneously put in storage, ensure the success of other operations such as message sending and the like, realize the real decoupling of the service operation and the remote RPC and message sending, shorten the response time of the service operation and ensure the final consistency of the data.
Specifically, the specific process of implementing the functions of each module in the apparatus according to the embodiment of the present invention may refer to the related description in the method embodiment, and is not described herein again.
According to another embodiment of the present application, there is also provided an electronic apparatus including: as shown in fig. 8, the electronic device may include: the system comprises a processor 1501, a communication interface 1502, a memory 1503 and a communication bus 1504, wherein the processor 1501, the communication interface 1502 and the memory 1503 complete communication with each other through the communication bus 1504.
A memory 1503 for storing a computer program;
the processor 1501 is configured to implement the steps of the above-described method embodiments when executing the program stored in the memory 1503.
The bus mentioned in the electronic device may be a Peripheral Component Interconnect (PCI) bus, an Extended Industry Standard Architecture (EISA) 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 thick line is shown, but this does not mean that there is only one bus or one type of bus.
The communication interface is used for communication between the electronic equipment and other equipment.
The Memory may include a Random Access Memory (RAM) or a Non-Volatile Memory (NVM), such as at least one disk Memory. Optionally, the memory may also be at least one memory device located remotely from the processor.
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 Gate or transistor logic devices, discrete hardware components.
Embodiments of the present application also provide a non-transitory computer-readable storage medium storing computer instructions for causing a computer to perform the steps of the above-described method embodiments.
It is noted that, in this document, relational terms such as "first" and "second," and the like, may be used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Also, 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 a process, method, article, or apparatus that comprises the element.
The foregoing are merely exemplary embodiments of the present invention, which enable those skilled in the art to understand or practice the present invention. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the generic principles defined herein may be applied to other embodiments without departing from the spirit or scope of the invention. Thus, the present invention is not intended to be limited to the embodiments shown herein but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.