CN114679464A - Data rollback method, device, equipment and storage medium based on distributed lock - Google Patents

Data rollback method, device, equipment and storage medium based on distributed lock Download PDF

Info

Publication number
CN114679464A
CN114679464A CN202210295397.6A CN202210295397A CN114679464A CN 114679464 A CN114679464 A CN 114679464A CN 202210295397 A CN202210295397 A CN 202210295397A CN 114679464 A CN114679464 A CN 114679464A
Authority
CN
China
Prior art keywords
client
locking
distributed lock
data
lock
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Granted
Application number
CN202210295397.6A
Other languages
Chinese (zh)
Other versions
CN114679464B (en
Inventor
王国涛
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Shenzhen Jiuyou Database Co ltd
Original Assignee
Weikun Shanghai Technology Service Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Weikun Shanghai Technology Service Co Ltd filed Critical Weikun Shanghai Technology Service Co Ltd
Priority to CN202210295397.6A priority Critical patent/CN114679464B/en
Publication of CN114679464A publication Critical patent/CN114679464A/en
Application granted granted Critical
Publication of CN114679464B publication Critical patent/CN114679464B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L67/00Network arrangements or protocols for supporting network services or applications
    • H04L67/01Protocols
    • H04L67/10Protocols in which an application is distributed across nodes in the network
    • H04L67/104Peer-to-peer [P2P] networks
    • H04L67/1044Group management mechanisms 
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/10File systems; File servers
    • G06F16/17Details of further file system functions
    • G06F16/172Caching, prefetching or hoarding of files
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L67/00Network arrangements or protocols for supporting network services or applications
    • H04L67/01Protocols
    • H04L67/10Protocols in which an application is distributed across nodes in the network
    • H04L67/104Peer-to-peer [P2P] networks
    • H04L67/1044Group management mechanisms 
    • H04L67/1051Group master selection mechanisms

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • Computing Systems (AREA)
  • Mathematical Physics (AREA)
  • Computer Networks & Wireless Communication (AREA)
  • Signal Processing (AREA)
  • Data Mining & Analysis (AREA)
  • Databases & Information Systems (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The application is applicable to the technical field of information security, and provides a data rollback method and device based on a distributed lock, terminal equipment and a storage medium. The method comprises the following steps: sending a locking request to a server cluster of the distributed system; after receiving a distributed lock returned by the server cluster based on the locking request, writing first locking information of the distributed lock to a specified cache database, wherein the first locking information comprises an identifier of the distributed lock, locking time and an identifier of a first client; after the task thread corresponding to the distributed lock is executed, second locking information which corresponds to the distributed lock and is latest in locking time is searched from the cache database; and if the client identifier contained in the second locking information is not the identifier of the first client, rolling back the data related to the task thread. By adopting the method, the problem of dirty data possibly generated in a red lock scheme of the existing distributed lock technology can be solved.

Description

Data rollback method, device, equipment and storage medium based on distributed lock
Technical Field
The application relates to the technical field of information security, and provides a data rollback method and device based on a distributed lock, a terminal device and a storage medium.
Background
A distributed lock is a way to control the simultaneous access of clients to shared resources in a distributed system. When Redis is used as a distributed lock, if a server cluster adopts a master-slave mode, when a master node fails and cluster nodes execute master-slave switching, the problem of loss of the distributed lock may occur.
To solve this problem, the prior art proposes a solution of red lock, that is, a server cluster is provided with a plurality of master nodes, a client sends a locking request to the plurality of master nodes at the same time, and as long as more than half of the master nodes pass the request, the client is successfully locked.
However, the following problems still exist with the red lock: after a certain client a acquires a lock, if a thread stall (for example, a long-time network congestion or fluctuation) occurs and the stall exceeds the expiration time of the lock, another client B can acquire the lock, thereby completing the corresponding data operation; after that, when the thread of the client a is resumed, the subsequent data operation is still executed, which may cause a problem such as dirty data.
Disclosure of Invention
In view of this, the present application provides a data rollback method and apparatus based on a distributed lock, a terminal device, and a storage medium, and aims to solve the problem of dirty data that may be generated in the red lock scheme in the prior art.
In a first aspect, an embodiment of the present application provides a data rollback method based on a distributed lock, which is applied to a first client, where the first client is any one of multiple clients in a distributed system, and the method includes:
sending a locking request to a server cluster of the distributed system;
after receiving a distributed lock returned by the server cluster based on the locking request, writing first locking information of the distributed lock to a specified cache database, wherein the first locking information comprises an identifier of the distributed lock, locking time and an identifier of the first client; after each client in the plurality of clients receives the distributed locks distributed by the server cluster, corresponding locking information is written into the cache database;
after the task thread corresponding to the distributed lock is executed, searching second locking information which corresponds to the distributed lock and has the latest locking time from all locking information stored in the cache database;
and if the client identifier contained in the second locking information is not the identifier of the first client, rolling back the data related to the task thread.
In the embodiment of the application, a certain first client sends a locking request to a server cluster; writing first locking information of the distributed lock into a specified cache database after the first client acquires the corresponding distributed lock based on the locking request, wherein the first locking information comprises an identifier of the distributed lock, locking time and an identifier of the first client; after the first client executes the corresponding task thread, searching second locking information which corresponds to the distributed lock and has the latest locking time from the cache database; and if the client identifier contained in the second locking information is not the identifier of the first client, rolling back the data related to the task thread by the first client. Through the setting, it is assumed that a thread pause occurs in the task thread execution of the first client, which results in the distributed lock being out of date, and another second client already obtains the distributed lock, after the first client finishes executing the thread, the first client finds that the client identifier included in the second locking information searched from the cache database is the identifier of the second client (after the second client obtains the distributed lock, as with the first client, the second client writes the locking information of the distributed lock into the cache database, wherein the locking information includes the locking time of later time and the identifier of the second client, and it is obvious that the second locking information searched by the first client is the locking information written by the second client), and at this time, the first client rolls back the data related to the task thread, thereby avoiding the problems of dirty data and the like.
In an implementation manner of the embodiment of the present application, the locking time included in the first locking information may be determined by:
inquiring whether other locking information corresponding to the distributed lock exists in the cache database or not;
if other locking information corresponding to the distributed lock exists in the cache database, searching the latest locking time contained in the other locking information, and determining the sum of the latest locking time and the specified time length as the locking time contained in the first locking information;
if the cache database does not contain other locking information corresponding to the distributed lock, determining the current clock time of the first client as the locking time contained in the first locking information.
In an implementation manner of the embodiment of the present application, the rolling back the data related to the task thread may include:
receiving priority information of a second client sent by the server cluster, where the second client is a client corresponding to a client identifier included in the second locking information among the multiple clients;
comparing the priority information of the second client with the priority information of the first client to obtain the priority between the first client and the second client;
and if the priority of the first client side is smaller than that of the second client side, rolling back the data related to the task thread.
Further, after obtaining the priority size between the first client and the second client, the method may further include:
and if the priority of the first client is greater than that of the second client, not executing the operation of rolling back the data related to the task thread, and sending a data rolling back instruction to the second client, wherein the data rolling back instruction is used for instructing the second client to roll back the data which is executed during the period when the second client acquires the distributed lock.
In an implementation manner of the embodiment of the present application, after searching for second locking information that corresponds to the distributed lock and has a latest locking time from all locking information stored in the cache database, the method may further include:
if the client identifier contained in the second locking information is the identifier of the first client, judging whether the data related to the task thread is predefined core data;
if the data related to the task thread is not predefined core data, not executing rollback operation on the data related to the task thread;
if the data related to the task thread is predefined core data, sending a first lock releasing request to the server cluster at the stage of releasing the distributed lock, wherein the first lock releasing request is used for indicating the server cluster to detect whether the distributed lock is still on the first client, executing a lock releasing process if the distributed lock is still on the first client, and returning first abnormal information to the first client if the distributed lock is not on the first client;
and after the first exception information is received, rolling back data related to the task thread.
In an implementation manner of the embodiment of the present application, after searching for second locking information that corresponds to the distributed lock and has a latest locking time from all locking information stored in the cache database, the method may further include:
if the client identifier included in the second locking information is the identifier of the first client, acquiring performance data of the first client;
if the performance data does not meet the preset data rollback performance requirement, the operation of performing rollback on the data related to the task thread is not executed;
if the performance data meets the preset data rollback performance requirement, sending a second lock releasing request to the server cluster at the stage of releasing the distributed lock, wherein the second lock releasing request is used for indicating the server cluster to detect whether the distributed lock is still on the first client, executing a lock releasing process if the distributed lock is still on the first client, and returning second abnormal information to the first client if the distributed lock is not on the first client;
and after receiving the second exception information, rolling back the data related to the task thread.
In an implementation manner of the embodiment of the present application, the first locking information further includes an expiration time of the distributed lock, and after searching for second locking information that corresponds to the distributed lock and has a latest locking time from all locking information stored in the cache database, the method may further include:
if the client identifier included in the second locking information is the identifier of the first client, judging whether the current time exceeds the expiration time;
and if the current time exceeds the expiration time, rolling back the data related to the task thread.
In a second aspect, an embodiment of the present application provides a data rollback apparatus based on a distributed lock, which is applied to a first client, where the first client is any one of multiple clients that a distributed system has, and the apparatus includes:
a locking request sending module, configured to send a locking request to a server cluster of the distributed system;
a locking information writing module, configured to write first locking information of the distributed lock to a specified cache database after receiving the distributed lock returned by the server cluster based on the locking request, where the first locking information includes an identifier of the distributed lock, a locking time, and an identifier of the first client; after each client in the plurality of clients receives the distributed locks distributed by the server cluster, corresponding locking information is written into the cache database;
the locking information searching module is used for searching second locking information which corresponds to the distributed lock and has the latest locking time from all the locking information stored in the cache database after the task thread corresponding to the distributed lock is executed;
and the data rollback module is used for rolling back the data related to the task thread if the client identifier contained in the second locking information is not the identifier of the first client.
In a third aspect, an embodiment of the present application provides a terminal device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, where the processor, when executing the computer program, implements the method for data rollback based on distributed locks as set forth in the first aspect of the embodiment of the present application.
In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, where a computer program is stored, and when executed by a processor, the computer program implements the distributed lock-based data rollback method as set forth in the first aspect of the embodiment of the present application.
In a fifth aspect, the present application provides a computer program product, which when running on a terminal device, causes the terminal device to execute the distributed lock-based data rollback method as set forth in the first aspect of the present application.
The advantageous effects achieved by the second aspect to the fifth aspect described above can be referred to the description of the first aspect described above.
Drawings
In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed to be used in the embodiments or the prior art descriptions will be briefly described below, and it is obvious that the drawings in the following description are only some embodiments of the present application, and it is obvious for those skilled in the art to obtain other drawings based on these drawings without inventive exercise.
FIG. 1 is a flowchart of an embodiment of a method for data rollback based on a distributed lock according to an embodiment of the present application;
fig. 2 is a schematic diagram of locking information according to an embodiment of the present disclosure;
FIG. 3 is a block diagram of one embodiment of a distributed lock based data rollback apparatus provided by an embodiment of the present application;
fig. 4 is a schematic diagram of a terminal device according to an embodiment of the present application.
Detailed Description
In the following description, for purposes of explanation and not limitation, specific details are set forth, such as particular system structures, techniques, etc. in order to provide a thorough understanding of the embodiments of the present application. It will be apparent, however, to one skilled in the art that the present application may be practiced in other embodiments that depart from these specific details. In other instances, detailed descriptions of well-known systems, devices, circuits, and methods are omitted so as not to obscure the description of the present application with unnecessary detail. Furthermore, in the description of the present application and the appended claims, the terms "first," "second," "third," and the like are used for distinguishing between descriptions and not necessarily for describing a relative importance or importance.
At present, many large websites and applications are distributed, data consistency in a distributed scene is always a problem that technicians need to consider in a key way, and the adoption of a distributed lock is an effective way to solve the problem. The data rollback method provided by the embodiment of the application mainly aims to solve the problem that dirty data may be generated in a red lock (Redlock) scheme disclosed by the existing distributed lock technology. For a detailed description of the data rollback method and details of the technical implementation, reference may be made to the method embodiments described below.
The embodiment of the application can acquire and process related data based on an artificial intelligence technology. Among them, Artificial Intelligence (AI) is a theory, method, technique and application system that simulates, extends and expands human Intelligence using a digital computer or a machine controlled by a digital computer, senses the environment, acquires knowledge and uses the knowledge to obtain the best result.
The artificial intelligence infrastructure generally includes technologies such as sensors, dedicated artificial intelligence chips, cloud computing, distributed storage, big data processing technologies, operation/interaction systems, mechatronics, and the like. The artificial intelligence software technology mainly comprises a computer vision technology, a robot technology, a biological recognition technology, a voice processing technology, a natural language processing technology, machine learning/deep learning and the like.
It should be understood that an execution subject of the data rollback method based on the distributed lock provided in the embodiment of the present application may be various types of terminal devices or servers, such as a mobile phone, a tablet computer, a wearable device, an in-vehicle device, an Augmented Reality (AR)/Virtual Reality (VR) device, a notebook computer, an ultra-mobile personal computer (UMPC), a netbook, a Personal Digital Assistant (PDA), a large screen television, and the like. The server may be an independent server, or may be a cloud server that provides basic cloud computing services such as a cloud service, a cloud database, cloud computing, a cloud function, cloud storage, a web service, cloud communication, a middleware service, a domain name service, a security service, a Content Delivery Network (CDN), a big data and artificial intelligence platform, and the like.
Referring to fig. 1, an embodiment of a data rollback method based on a distributed lock in an embodiment of the present application includes:
101. a first client sends a locking request to a server cluster of a distributed system;
the first client is any one of a plurality of clients (terminal devices) of the distributed system, and first sends a lock request to a server cluster of the distributed system to obtain a distributed lock required by a task thread to be executed. Typically, the lock request will include an expiration time for a distributed lock.
102. After receiving a distributed lock returned by the server cluster based on the locking request, a first client writes first locking information of the distributed lock to a specified cache database, wherein the first locking information comprises an identifier of the distributed lock, locking time and an identifier of the first client;
after each node in the server cluster receives a locking request sent by a first client, if more than half of the nodes decide to pass the locking request of the first client, the first client is successfully locked, and a corresponding distributed lock is obtained. After receiving the distributed lock returned by the server cluster, the first client writes locking information (indicated by first locking information) of the distributed lock to a specified cache database, wherein the locking information includes an identifier of the distributed lock, locking time and an identifier of the first client. The cache database may be any type of database set in the server cluster, and assuming that the first client is client a and the obtained distributed lock is X, client a may obtain the current time as the locking time, then construct locking information of "ID of distributed lock X + current time + ID of client a" (a schematic diagram is shown in fig. 2), and write the locking information into the cache database.
It should be understood that each client in the distributed system, after acquiring a distributed lock assigned by the server cluster, performs the same operation steps as the first client, and writes corresponding lock information into the cache database.
In an implementation manner of the embodiment of the present application, the locking time included in the first locking information may be determined by:
(1) inquiring whether other locking information corresponding to the distributed lock exists in the cache database or not;
(2) if other locking information corresponding to the distributed lock exists in the cache database, searching the latest locking time contained in the other locking information, and determining the sum of the latest locking time and the specified time length as the locking time contained in the first locking information;
(3) if no other locking information corresponding to the distributed lock exists in the cache database, determining the current clock time of the first client as the locking time contained in the first locking information.
Since the clock times of different clients may be different, when the first client constructs the locking information, if it is found that other locking information of the distributed lock already exists in the cache database (for example, the locking information closest to the current time is written by the client C when the client C obtains the distributed lock before a certain time), the locking time of the locking information constructed by the first client is added to the locking time of the locking information of the client C (i.e., the latest locking time) by a certain specified time length t, so as to ensure that the locking time of the locking information currently written by the first client is the latest of all the locking information of the distributed lock already existing in the cache database, thereby ensuring the validity of the locking information of the first client. In addition, if other locking information of the distributed lock does not exist in the cache database, the validity of the locking information of the first client can be ensured only by determining the current clock time of the first client as the corresponding locking time.
103. After the first client executes the task thread corresponding to the distributed lock, searching second locking information which corresponds to the distributed lock and has the latest locking time from all locking information stored in the cache database;
next, the first client may execute a corresponding task thread based on the distributed lock. It should be noted that in this process, the first client may have thread stalls that cause the distributed lock to expire, while other clients may obtain the distributed lock.
After the first client executes the corresponding task thread, the first client searches for locking information (represented by second locking information) corresponding to the distributed lock with the latest locking time from the cache database. Since during the process of executing the task thread by the first client, another client may already obtain the distributed lock and write the locking information with the later locking time into the cache database, the second locking information searched by the first client at this time is not necessarily the locking information (i.e., the first locking information) written by the first client in step 102.
In addition, the server cluster may periodically clean up the locking information with an earlier locking time in the cache database (the part of the locking information is not used), so as to save the space of the cache region.
In actual operation, the locking information may be stored in a key-value pair manner, i.e., key-value, where the key is composed of the identification of the distributed lock and the locking time; the value consists of the identity of the client and the expiration time of the distributed lock. If the distributed lock obtained by the first client is X, when searching for the second locking information, one locking information with the latest locking time among all locking information with key being the ID of the distributed lock X is searched from the cache database as the second locking information.
104. And if the client identifier contained in the second locking information is not the identifier of the first client, rolling back the data related to the task thread by the first client.
If the client identifier included in the second locking information is not the identifier of the first client, it indicates that the distributed lock has been acquired by another client, and at this time, the first client performs rollback on the data related to the task thread (data rollback, which refers to restoring the modified data during the execution of the task thread to the state before the execution of the task thread), so as to avoid the problems of dirty data and the like. On the contrary, if the client identifier included in the second locking information is the identifier of the first client, it indicates that the distributed lock is not acquired by other clients (there are two possible situations, one is that the distributed lock is not expired and is still on the first client, and the other is that the distributed lock is expired but no other client acquires the distributed lock), and in any case, there is no problem of generating dirty data, so the first client may not execute the operation of data rollback.
In an implementation manner of the embodiment of the present application, the rolling back the data related to the task thread may include:
(1) receiving priority information of a second client sent by the server cluster, wherein the second client is a client corresponding to a client identifier contained in the second locking information in the distributed system;
(2) comparing the priority information of the second client with the priority information of the first client to obtain the priority between the first client and the second client;
(3) and if the priority of the first client side is smaller than that of the second client side, rolling back the data related to the task thread.
The server cluster may store priority information of each client in advance, which is used to indicate the importance degree of each client, and generally speaking, the importance degree of the client executing the core service is higher, and a higher priority may be assigned. For example, after detecting that the client identifier in the second locking information is the identifier of the second client B, the first client a may obtain the priority information of the client B through the server cluster, and compare the priority information with its own priority. And if the priority of the client A is lower than that of the client B, the importance degree of the task executed by the client B is higher than that of the client A. Therefore, in order to ensure that the task thread of client B processes normally and avoid generating dirty data, client a needs to perform data rollback.
Further, after obtaining the priority size between the first client and the second client, the method may further include:
and if the priority of the first client is greater than that of the second client, not executing the operation of rolling back the data related to the task thread, and sending a data rolling back instruction to the second client, wherein the data rolling back instruction is used for instructing the second client to roll back the data which is executed during the period when the second client acquires the distributed lock.
On the contrary, if the priority of the first client a is higher than the priority of the second client B, the normal processing of the task thread of the client a should be considered preferentially, and the client a may not perform the data rollback operation. In order to avoid generating dirty data, the client a may further send a data rollback instruction to the client B, where the data rollback instruction is used to instruct the client B to rollback data related to a task thread executed during the time when the client B acquires the distributed lock.
In an implementation manner of the embodiment of the present application, after searching for second locking information that corresponds to the distributed lock and has a latest locking time from all locking information stored in the cache database, the method may further include:
(1) if the client identifier contained in the second locking information is the identifier of the first client, judging whether the data related to the task thread is predefined core data;
(2) if the data related to the task thread is not predefined core data, not executing rollback operation on the data related to the task thread;
(3) if the data related to the task thread is predefined core data, sending a first lock releasing request to the server cluster at the stage of releasing the distributed lock, wherein the first lock releasing request is used for indicating the server cluster to detect whether the distributed lock is still on the first client, executing a lock releasing process if the distributed lock is still on the first client, and returning first abnormal information to the first client if the distributed lock is not on the first client;
(4) and after receiving the first exception information, rolling back data related to the task thread.
The first client may pre-construct a core data set, and define a part of important data as core data. Then, whether the data related to the task thread belongs to the data in the core data set is judged by detecting whether the data belongs to the core data set. For core data, the requirement on data accuracy is high. Therefore, for the core data, verification can be performed once more at the stage of releasing the distributed lock, whether the distributed lock is still on the first client is checked, and if not, the core data is rolled back to further improve the data security. Otherwise, no additional verify operations need to be performed for uncore data.
In an implementation manner of the embodiment of the present application, after searching for second locking information that corresponds to the distributed lock and has a latest locking time from all locking information stored in the cache database, the method may further include:
(1) if the client identifier included in the second locking information is the identifier of the first client, acquiring performance data of the first client;
(2) if the performance data does not meet the preset data rollback performance requirement, the operation of performing rollback on the data related to the task thread is not executed;
(3) if the performance data meets the preset data rollback performance requirement, sending a second lock releasing request to the server cluster in a phase of releasing the distributed lock, wherein the second lock releasing request is used for indicating the server cluster to detect whether the distributed lock is still on the first client, executing a lock releasing process if the distributed lock is still on the first client, and returning second abnormal information to the first client if the distributed lock is not on the first client;
(4) and after receiving the second exception information, rolling back the data related to the task thread.
The performance data of the first client can be data of the CPU performance, the memory size and the like of the client; if the performance data does not meet the set data rollback performance requirement, the performance of the first client is poor, and if the verification in the lock releasing stage is executed again, the problems of jamming and the like can be caused, so that the verification can not be performed any more. If the performance data meets the data rollback performance requirement, the performance of the first client side is better, and the system performance problems such as blocking and the like do not need to be worried about, so that verification can be performed again at the lock releasing stage (whether the distributed lock is still on the first client side is checked, if yes, the lock releasing operation is normally executed, and if not, data rollback is performed), so that the data security is further improved.
In an implementation manner of the embodiment of the present application, the first locking information further includes an expiration time of the distributed lock, and after searching for second locking information that corresponds to the distributed lock and has a latest locking time from all locking information stored in the cache database, the method may further include:
(1) if the client identifier included in the second locking information is the identifier of the first client, judging whether the current time exceeds the expiration time;
(2) and if the current time exceeds the expiration time, rolling back the data related to the task thread.
If the current time exceeds the expiration time of the distributed lock, it indicates that no other client has obtained the distributed lock, but because the distributed lock has expired, that is, the distributed lock on the first client does not exist, another client may apply for obtaining the distributed lock. Considering that there may be a possibility that another client fails to write the locking information after acquiring the distributed lock (which may misunderstand that the distributed lock is not acquired by another client), the first client still rolls back the data related to the task thread, thereby further improving data security. If the current time does not exceed the expiration time of the distributed lock, it indicates that the distributed lock is still on the first client, so the rollback operation does not need to be performed.
In the embodiment of the application, a certain first client sends a locking request to a server cluster; writing first locking information of the distributed lock into a specified cache database after the first client acquires the corresponding distributed lock based on the locking request, wherein the first locking information comprises an identifier of the distributed lock, locking time and an identifier of the first client; after the first client executes the corresponding task thread, searching second locking information with the latest locking time corresponding to the distributed lock from the cache database; and if the client identifier contained in the second locking information is not the identifier of the first client, rolling back the data related to the task thread by the first client. Through the setting, if a first client ends thread pause in executing a task thread to cause the distributed lock to expire and another second client ends obtain the distributed lock, the first client ends after executing the thread will find that the client end identification included in the second locking information searched from the cache database is the identification of the second client ends (after the second client ends obtain the distributed lock, the second client ends will write the locking information of the distributed lock into the cache database as the first client ends, wherein the locking time of the distributed lock and the identification of the second client ends are included, it is obvious that the second locking information searched by the first client ends is the locking information written by the second client ends), and at this time, the first client ends will roll back data related to the task thread, thereby avoiding the problems of generating dirty data and the like.
It should be understood that, the sequence numbers of the steps in the foregoing embodiments do not imply an execution sequence, and the execution sequence of each process should be determined by its function and inherent logic, and should not constitute any limitation to the implementation process of the embodiments of the present application.
Corresponding to the data rollback method based on the distributed lock described in the foregoing embodiment, fig. 3 shows a structural block diagram of a data rollback apparatus based on the distributed lock provided in the embodiment of the present application, and for convenience of explanation, only the parts related to the embodiment of the present application are shown.
The apparatus is applied to a first client, where the first client is any one of a plurality of clients of a distributed system, please refer to fig. 3, and the apparatus includes:
a locking request sending module 301, configured to send a locking request to a server cluster of the distributed system;
a locking information writing module 302, configured to write, to a specified cache database, first locking information of a distributed lock after receiving the distributed lock returned by the server cluster based on the locking request, where the first locking information includes an identifier of the distributed lock, a locking time, and an identifier of the first client; after each client in the plurality of clients receives the distributed locks distributed by the server cluster, corresponding locking information is written into the cache database;
a locking information searching module 303, configured to search, after executing a task thread corresponding to the distributed lock, second locking information that corresponds to the distributed lock and has the latest locking time from all locking information stored in the cache database;
a data rollback module 304, configured to rollback the data related to the task thread if the client identifier included in the second locking information is not the identifier of the first client.
In an implementation manner of the embodiment of the present application, the data rollback apparatus may further include:
the locking information query module is used for querying whether other locking information corresponding to the distributed lock exists in the cache database or not;
a latest locking time search module, configured to search, if there is another locking information corresponding to the distributed lock in the cache database, a latest locking time included in the another locking information, and determine a sum of the latest locking time and a specified duration as a locking time included in the first locking information;
a locking time determination module, configured to determine, if there is no other locking information corresponding to the distributed lock in the cache database, a current clock time of the first client as a locking time included in the first locking information.
In an implementation manner of the embodiment of the present application, the data rollback module may include:
a priority information receiving unit, configured to receive priority information of a second client sent by the server cluster, where the second client is a client corresponding to a client identifier included in the second locking information, among the multiple clients;
a priority comparing unit, configured to compare the priority information of the second client with the priority information of the first client to obtain a priority size between the first client and the second client;
and the data rollback unit is used for rolling back the data related to the task thread if the priority of the first client is smaller than that of the second client.
Further, the data rollback module may further include:
and a data rollback instruction sending unit, configured to not execute an operation of performing rollback on data related to the task thread if the priority of the first client is greater than that of the second client, and send a data rollback instruction to the second client, where the data rollback instruction is used to instruct the second client to rollback data on which an operation is performed during the period in which the second client acquires the distributed lock.
In an implementation manner of the embodiment of the present application, the data rollback apparatus may further include:
a core data determination module, configured to determine whether data related to the task thread is predefined core data if a client identifier included in the second locking information is an identifier of the first client;
the first execution module is used for not executing the operation of rolling back the data related to the task thread if the data related to the task thread is not the predefined core data;
a first verification module, configured to send a first lock release request to the server cluster at a stage of releasing the distributed lock if data related to the task thread is predefined core data, where the first lock release request is used to instruct the server cluster to detect whether the distributed lock is still on the first client, execute a lock release process if the distributed lock is still on the first client, and return first exception information to the first client if the distributed lock is not on the first client;
and the first rollback module is used for rolling back the data related to the task thread after the first abnormal information is received.
In an implementation manner of the embodiment of the present application, the data rollback apparatus may further include:
a performance data obtaining module, configured to obtain performance data of the first client if a client identifier included in the second locking information is an identifier of the first client;
the second execution module is used for not executing the operation of rolling back the data related to the task thread if the performance data does not accord with the preset data rolling back performance requirement;
a second verification module, configured to send a second lock release request to the server cluster at a stage of releasing the distributed lock if the performance data meets a preset data rollback performance requirement, where the second lock release request is used to indicate the server cluster to detect whether the distributed lock is still on the first client, execute a lock release process if the distributed lock is still on the first client, and return second abnormal information to the first client if the distributed lock is not on the first client;
and the second rollback module is used for rolling back the data related to the task thread after receiving the second abnormal information.
In an implementation manner of the embodiment of the present application, the data rollback apparatus may further include:
an expiration judging module, configured to judge whether the current time exceeds the expiration time if a client identifier included in the second locking information is an identifier of the first client;
and the third rollback module is used for rolling back the data related to the task thread if the current time exceeds the expiration time.
Embodiments of the present application further provide a computer-readable storage medium, which stores computer-readable instructions, and when executed by a processor, the computer-readable instructions implement any one of the distributed lock-based data rollback methods shown in fig. 1.
Embodiments of the present application further provide a computer program product, when the computer program product runs on a server, causing the server to execute a method for implementing any one of the distributed lock-based data rollback methods shown in fig. 1.
Fig. 4 is a schematic diagram of a terminal device according to an embodiment of the present application. As shown in fig. 4, the terminal device 4 of this embodiment includes: a processor 40, a memory 41, and computer readable instructions 42 stored in the memory 41 and executable on the processor 40. The processor 40, when executing the computer readable instructions 42, implements the steps in the various distributed lock-based data rollback method embodiments described above, such as steps 101-104 shown in fig. 1. Alternatively, the processor 40, when executing the computer readable instructions 42, implements the functions of the modules/units in the above device embodiments, such as the functions of the modules 301 to 304 shown in fig. 3.
Illustratively, the computer-readable instructions 42 may be partitioned into one or more modules/units, which are stored in the memory 41 and executed by the processor 40 to accomplish the present application. The one or more modules/units may be a series of computer-readable instruction segments capable of performing specific functions, which are used for describing the execution process of the computer-readable instructions 42 in the terminal device 4.
The terminal device 4 may be a computing device such as a smart phone, a notebook, a palm computer, and a cloud terminal device. The terminal device 4 may include, but is not limited to, a processor 40 and a memory 41. It will be understood by those skilled in the art that fig. 4 is only an example of the terminal device 4, and does not constitute a limitation to the terminal device 4, and may include more or less components than those shown, or combine some components, or different components, for example, the terminal device 4 may further include an input-output device, a network access device, a bus, etc.
The Processor 40 may be a Central Processing Unit (CPU), other general purpose Processor, a Digital Signal Processor (DSP), an AppLication Specific Integrated Circuit (ASIC), an off-the-shelf Programmable Gate Array (FPGA) or other Programmable logic device, discrete Gate or transistor logic device, discrete hardware component, etc. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like.
The memory 41 may be an internal storage unit of the terminal device 4, such as a hard disk or a memory of the terminal device 4. The memory 41 may also be an external storage device of the terminal device 4, such as a plug-in hard disk, a Smart Media Card (SMC), a Secure Digital (SD) Card, a Flash memory Card (Flash Card), and the like, which are provided on the terminal device 4. Further, the memory 41 may also include both an internal storage unit and an external storage device of the terminal device 4. The memory 41 is used for storing the computer readable instructions and other programs and data required by the terminal device. The memory 41 may also be used to temporarily store data that has been output or is to be output.
It should be noted that, for the information interaction, execution process, and other contents between the above-mentioned devices/units, the specific functions and technical effects thereof are based on the same concept as those of the embodiment of the method of the present application, and specific reference may be made to the part of the embodiment of the method, which is not described herein again.
It will be apparent to those skilled in the art that, for convenience and brevity of description, only the above-mentioned division of the functional units and modules is illustrated, and in practical applications, the above-mentioned function distribution may be performed by different functional units and modules according to needs, that is, the internal structure of the apparatus is divided into different functional units or modules to perform all or part of the above-mentioned functions. Each functional unit and module in the embodiments may be integrated in one processing unit, or each unit may exist alone physically, or two or more units are integrated in one unit, and the integrated unit may be implemented in a form of hardware, or in a form of software functional unit. In addition, specific names of the functional units and modules are only for convenience of distinguishing from each other, and are not used for limiting the protection scope of the present application. For the specific working processes of the units and modules in the system, reference may be made to the corresponding processes in the foregoing method embodiments, which are not described herein again.
The integrated unit, if implemented in the form of a software functional unit and sold or used as a stand-alone product, may be stored in a computer readable storage medium. Based on such understanding, all or part of the processes in the methods of the embodiments described above can be implemented by a computer program, which can be stored in a computer-readable storage medium and can implement the steps of the embodiments of the methods described above when the computer program is executed by a processor. Wherein the computer program comprises computer program code, which may be in the form of source code, object code, an executable file or some intermediate form, etc. The computer-readable medium may include at least: any entity or device capable of carrying computer program code to a photographing apparatus/terminal apparatus, a recording medium, computer Memory, Read-Only Memory (ROM), Random Access Memory (RAM), an electrical carrier signal, a telecommunications signal, and a software distribution medium. Such as a usb-disk, a removable hard disk, a magnetic or optical disk, etc.
In the above embodiments, the descriptions of the respective embodiments have respective emphasis, and reference may be made to the related descriptions of other embodiments for parts that are not described or illustrated in a certain embodiment.
The above-mentioned embodiments are only used for illustrating the technical solutions of the present application, and not for limiting the same; although the present application has been described in detail with reference to the foregoing embodiments, it should be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; such modifications and substitutions do not substantially depart from the spirit and scope of the embodiments of the present application and are intended to be included within the scope of the present application.

Claims (10)

1. A data rollback method based on a distributed lock is applied to a first client, wherein the first client is any one of a plurality of clients in a distributed system, and the method comprises the following steps:
sending a locking request to a server cluster of the distributed system;
after receiving a distributed lock returned by the server cluster based on the locking request, writing first locking information of the distributed lock to a specified cache database, wherein the first locking information comprises an identifier of the distributed lock, locking time and an identifier of the first client; after each client in the plurality of clients receives the distributed locks distributed by the server cluster, corresponding locking information is written into the cache database;
after the task thread corresponding to the distributed lock is executed, searching second locking information which corresponds to the distributed lock and has the latest locking time from all locking information stored in the cache database;
and if the client identifier contained in the second locking information is not the identifier of the first client, rolling back the data related to the task thread.
2. The data rollback method of claim 1, wherein the first lock information includes a lock time determined by:
inquiring whether other locking information corresponding to the distributed lock exists in the cache database or not;
if other locking information corresponding to the distributed lock exists in the cache database, searching the latest locking time contained in the other locking information, and determining the sum of the latest locking time and the specified time length as the locking time contained in the first locking information;
if no other locking information corresponding to the distributed lock exists in the cache database, determining the current clock time of the first client as the locking time contained in the first locking information.
3. The data rollback method of claim 1, wherein the rolling back data related to the task thread comprises:
receiving priority information of a second client sent by the server cluster, where the second client is a client corresponding to a client identifier included in the second locking information among the plurality of clients;
comparing the priority information of the second client with the priority information of the first client to obtain the priority between the first client and the second client;
and if the priority of the first client side is smaller than that of the second client side, rolling back the data related to the task thread.
4. The data rollback method of claim 3, after obtaining the priority size between the first client and the second client, further comprising:
and if the priority of the first client is greater than that of the second client, not executing the operation of rolling back the data related to the task thread, and sending a data rolling back instruction to the second client, wherein the data rolling back instruction is used for instructing the second client to roll back the data which is executed during the period when the second client acquires the distributed lock.
5. The data rollback method of claim 1, wherein after searching for a second locking information corresponding to the distributed lock and having a latest locking time from all locking information stored in the cache database, further comprising:
if the client identifier contained in the second locking information is the identifier of the first client, judging whether the data related to the task thread is predefined core data;
if the data related to the task thread is not predefined core data, not executing rollback operation on the data related to the task thread;
if the data related to the task thread is predefined core data, sending a first lock releasing request to the server cluster at the stage of releasing the distributed lock, wherein the first lock releasing request is used for indicating the server cluster to detect whether the distributed lock is still on the first client, executing a lock releasing process if the distributed lock is still on the first client, and returning first abnormal information to the first client if the distributed lock is not on the first client;
and after receiving the first exception information, rolling back data related to the task thread.
6. The data rollback method of claim 1, wherein after searching for a second locking information corresponding to the distributed lock and having a latest locking time from all locking information stored in the cache database, further comprising:
if the client identifier included in the second locking information is the identifier of the first client, acquiring performance data of the first client;
if the performance data does not meet the preset data rollback performance requirement, the operation of performing rollback on the data related to the task thread is not executed;
if the performance data meets the preset data rollback performance requirement, sending a second lock releasing request to the server cluster at the stage of releasing the distributed lock, wherein the second lock releasing request is used for indicating the server cluster to detect whether the distributed lock is still on the first client, executing a lock releasing process if the distributed lock is still on the first client, and returning second abnormal information to the first client if the distributed lock is not on the first client;
and after receiving the second exception information, rolling back data related to the task thread.
7. The data rollback method according to any one of claims 1 to 6, wherein the first locking information further includes an expiration time of the distributed lock, and after searching all locking information stored in the cache database for second locking information corresponding to the distributed lock and having a latest locking time, the method further comprises:
if the client identifier included in the second locking information is the identifier of the first client, judging whether the current time exceeds the expiration time;
and if the current time exceeds the expiration time, rolling back the data related to the task thread.
8. A data rollback apparatus based on a distributed lock, applied to a first client, where the first client is any one of a plurality of clients in a distributed system, the apparatus comprising:
a locking request sending module, configured to send a locking request to a server cluster of the distributed system;
a locking information writing module, configured to write first locking information of the distributed lock to a specified cache database after receiving the distributed lock returned by the server cluster based on the locking request, where the first locking information includes an identifier of the distributed lock, a locking time, and an identifier of the first client; after each client in the plurality of clients receives the distributed locks distributed by the server cluster, corresponding locking information is written into the cache database;
the locking information searching module is used for searching second locking information which corresponds to the distributed lock and has the latest locking time from all the locking information stored in the cache database after the task thread corresponding to the distributed lock is executed;
and the data rollback module is used for rolling back the data related to the task thread if the client identifier contained in the second locking information is not the identifier of the first client.
9. A terminal device comprising a memory, a processor and a computer program stored in the memory and executable on the processor, characterized in that the processor implements the distributed lock based data rollback method according to any one of claims 1 to 7 when executing the computer program.
10. A computer-readable storage medium, in which a computer program is stored, which, when being executed by a processor, implements a distributed lock-based data rollback method according to any one of claims 1 to 7.
CN202210295397.6A 2022-03-24 2022-03-24 Data rollback method, device, equipment and storage medium based on distributed lock Active CN114679464B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202210295397.6A CN114679464B (en) 2022-03-24 2022-03-24 Data rollback method, device, equipment and storage medium based on distributed lock

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202210295397.6A CN114679464B (en) 2022-03-24 2022-03-24 Data rollback method, device, equipment and storage medium based on distributed lock

Publications (2)

Publication Number Publication Date
CN114679464A true CN114679464A (en) 2022-06-28
CN114679464B CN114679464B (en) 2024-02-13

Family

ID=82074268

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202210295397.6A Active CN114679464B (en) 2022-03-24 2022-03-24 Data rollback method, device, equipment and storage medium based on distributed lock

Country Status (1)

Country Link
CN (1) CN114679464B (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115277712A (en) * 2022-07-08 2022-11-01 北京城市网邻信息技术有限公司 Distributed lock service providing method, device and system and electronic equipment

Citations (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN104954411A (en) * 2014-03-31 2015-09-30 腾讯科技(深圳)有限公司 Method for sharing network resource by distributed system, terminal thereof and system thereof
US9171019B1 (en) * 2013-02-19 2015-10-27 Amazon Technologies, Inc. Distributed lock service with external lock information database
CN106354565A (en) * 2016-09-21 2017-01-25 努比亚技术有限公司 Distributed lock client and control method
CN107145396A (en) * 2016-03-01 2017-09-08 阿里巴巴集团控股有限公司 Distributed lock implementation method and equipment
CN108874552A (en) * 2018-06-28 2018-11-23 杭州云英网络科技有限公司 Distributed lock executes method, apparatus and system, application server and storage medium
CN111259030A (en) * 2020-01-16 2020-06-09 平安医疗健康管理股份有限公司 Thread execution method and device based on distributed lock and storage medium
CN113138992A (en) * 2020-01-19 2021-07-20 上海静客网络科技有限公司 Method for constructing distributed lock
CN113419792A (en) * 2021-06-30 2021-09-21 未鲲(上海)科技服务有限公司 Event processing method and device, terminal equipment and storage medium

Patent Citations (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US9171019B1 (en) * 2013-02-19 2015-10-27 Amazon Technologies, Inc. Distributed lock service with external lock information database
CN104954411A (en) * 2014-03-31 2015-09-30 腾讯科技(深圳)有限公司 Method for sharing network resource by distributed system, terminal thereof and system thereof
CN107145396A (en) * 2016-03-01 2017-09-08 阿里巴巴集团控股有限公司 Distributed lock implementation method and equipment
CN106354565A (en) * 2016-09-21 2017-01-25 努比亚技术有限公司 Distributed lock client and control method
CN108874552A (en) * 2018-06-28 2018-11-23 杭州云英网络科技有限公司 Distributed lock executes method, apparatus and system, application server and storage medium
CN111259030A (en) * 2020-01-16 2020-06-09 平安医疗健康管理股份有限公司 Thread execution method and device based on distributed lock and storage medium
CN113138992A (en) * 2020-01-19 2021-07-20 上海静客网络科技有限公司 Method for constructing distributed lock
CN113419792A (en) * 2021-06-30 2021-09-21 未鲲(上海)科技服务有限公司 Event processing method and device, terminal equipment and storage medium

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
周梅;: "分布式锁的设计与实现", 计算机工程, no. 16 *

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115277712A (en) * 2022-07-08 2022-11-01 北京城市网邻信息技术有限公司 Distributed lock service providing method, device and system and electronic equipment

Also Published As

Publication number Publication date
CN114679464B (en) 2024-02-13

Similar Documents

Publication Publication Date Title
CN109684285B (en) User mode network file system file locking method, device and equipment
CN107423942B (en) Service transfer method and device
CN108989390B (en) User request forwarding method and device, reverse proxy server and computer readable storage medium
CN114679464B (en) Data rollback method, device, equipment and storage medium based on distributed lock
CN112463318A (en) Timed task processing method, device and system
CN109257396B (en) Distributed lock scheduling method and device
CN111880909A (en) Distributed data publishing method and device
CN108647102B (en) Service request processing method and device of heterogeneous system and electronic equipment
CN112631756A (en) Distributed regulation and control method and device applied to space flight measurement and control software
CN111681011B (en) Data processing method, blockchain system, computer system and medium
CN111400051A (en) Resource scheduling method, device and system
CN114844771A (en) Monitoring method, device, storage medium and program product for micro-service system
CN111367921A (en) Data object refreshing method and device
CN114564286A (en) Rule engine warning method and rule engine warning system
US20060136636A1 (en) System and method for resolving conflicts of re-locking resources
CN113448976A (en) Data processing method, data processing device, computer equipment and storage medium
CN113448758A (en) Task processing method and device and terminal equipment
CN108805741B (en) Fusion method, device and system of power quality data
CN113760876A (en) Data filtering method and device
CN111159236A (en) Data processing method and device, electronic equipment and storage medium
US20210224272A1 (en) Method, device, computer apparatus, and storage medium for storing data
CN107967181B (en) Method and device for controlling critical section
CN114979153B (en) Load balancing method, computer equipment and storage device
CN110209666B (en) data storage method and terminal equipment
CN111400324B (en) Method, device and server for locking object in cloud storage

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
TA01 Transfer of patent application right
TA01 Transfer of patent application right

Effective date of registration: 20240117

Address after: 518000 Nanshan Software Park, Liancheng Community, Nantou Street, Nanshan District, Shenzhen City, Guangdong Province A1101, A1102, A1103, A1104

Applicant after: Shenzhen Jiuyou Database Co.,Ltd.

Address before: Floor 15, no.1333, Lujiazui Ring Road, pilot Free Trade Zone, Pudong New Area, Shanghai

Applicant before: Weikun (Shanghai) Technology Service Co.,Ltd.

GR01 Patent grant
GR01 Patent grant