CN114679464B - 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
CN114679464B
CN114679464B CN202210295397.6A CN202210295397A CN114679464B CN 114679464 B CN114679464 B CN 114679464B CN 202210295397 A CN202210295397 A CN 202210295397A CN 114679464 B CN114679464 B CN 114679464B
Authority
CN
China
Prior art keywords
client
locking
distributed lock
data
locking information
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.)
Active
Application number
CN202210295397.6A
Other languages
Chinese (zh)
Other versions
CN114679464A (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
Shenzhen Jiuyou Database 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 Shenzhen Jiuyou Database Co ltd filed Critical Shenzhen Jiuyou Database 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

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, a device, terminal equipment and a storage medium based on a distributed lock. 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 into a designated 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 has the latest 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 the 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, device, terminal equipment and storage medium based on a distributed lock.
Background
Distributed locks are one way to control the synchronous access of clients to a shared resource in a distributed system. When Redis is used as a distributed lock, if a server cluster adopts a master-slave mode, the master node fails, and when the cluster node executes master-slave switching, the problem of loss of the distributed lock is likely to occur.
Aiming at the problem, the prior art provides a solution of red lock, namely a server cluster is provided with a plurality of master nodes, a client side simultaneously sends a locking request to the plurality of master nodes, and the client side is successfully locked as long as more than half of master nodes pass the request.
However, the following problems still remain with red locks: after a certain client a acquires a lock, if a thread stall (such as a network congestion or fluctuation for a long time) 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, after the thread of the client a is restored, the subsequent data operation is still performed, and thus, there is a possibility that a problem such as dirty data may occur.
Disclosure of Invention
In view of this, the present application proposes a data rollback method, apparatus, terminal device and storage medium based on distributed lock, which aims to solve the dirty data problem that may occur 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 a plurality of clients that are provided 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 into a designated cache database, wherein the first locking information comprises an identifier of the distributed lock, locking time and an identifier of the first client; each client of the plurality of clients writes corresponding locking information into the cache database after receiving the distributed locks distributed by the server cluster;
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 locking request is firstly sent to a server cluster by a certain first client; after the first client obtains the corresponding distributed lock based on the locking request, writing first locking information of the distributed lock into a designated cache database, 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; if the client identifier contained in the second locking information is not the identifier of the first client, the first client rolls back the data related to the task thread. By means of the arrangement, if the first client side has a thread pause in a task execution thread to cause the distributed lock to be out of date, and the other second client side has obtained the distributed lock, the first client side can find that the client side identification contained in the second locking information searched in the cache database is the identification of the second client side after the thread is executed (the second client side can write the locking information of the distributed lock into the cache database as the first client side after the distributed lock is obtained, wherein the locking information contains the locking time with later time and the identification of the second client side, and obviously the second locking information searched by the first client side is the locking information written by the second client side), and at the moment, the first client side rolls back data related to the task thread, so that the problems of dirty data and the like are avoided.
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;
if other locking information corresponding to the distributed lock exists in the cache database, searching the most night locking time contained in the other locking information, and determining the sum of the most night locking time and the appointed duration as the locking time contained in the first locking information;
and if other locking information corresponding to the distributed lock does not exist in the cache database, 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 rollback of the data related to the task thread may include:
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 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 is smaller than that of the second client, rolling back the data related to the task thread.
Further, after obtaining the priority level between the first client and the second client, the method may further include:
if the priority of the first client is greater than that of the second client, no rollback operation is executed on the data related to the task thread, and a data rollback instruction is sent to the second client, wherein the data rollback instruction is used for indicating the second client to rollback the data which is executed during the process of acquiring the distributed lock.
In an implementation manner of the embodiment of the present application, after searching, from all locking information stored in the cache database, second locking information corresponding to the distributed lock and having a latest locking time, 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 or not;
if the data related to the task thread is not the predefined core data, not executing the rollback operation on the data related to the task thread;
If the data related to the task thread is the predefined core data, a first lock release request is sent to the server cluster in the stage of releasing the distributed lock, wherein the first lock release request is used for indicating the server cluster to detect whether the distributed lock is still on the first client, if the distributed lock is still on the first client, a lock release process is executed, and if the distributed lock is not on the first client, first exception information is returned to the first client;
and after receiving the first abnormal information, rolling back data related to the task thread.
In an implementation manner of the embodiment of the present application, after searching, from all locking information stored in the cache database, second locking information corresponding to the distributed lock and having a latest locking time, the method may further include:
if the client identifier contained 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, not executing the operation of rollback the data related to the task thread;
If the performance data meets the preset data rollback performance requirement, a second lock release request is sent to the server cluster in the stage of releasing the distributed lock, wherein the second lock release request is used for indicating the server cluster to detect whether the distributed lock is still on the first client, if the distributed lock is still on the first client, a lock release process is executed, and if the distributed lock is not on the first client, second abnormal information is returned to the first client;
and after receiving the second abnormal information, rolling back 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, from all locking information stored in the cache database, second locking information corresponding to the distributed lock and having a latest locking time, 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 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 a plurality of 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;
the locking information writing module is used for writing first locking information of the distributed lock into a designated cache database after receiving the distributed lock returned by the server cluster 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; each client of the plurality of clients writes corresponding locking information into the cache database after receiving the distributed locks distributed by the server cluster;
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 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 capable of running on the processor, where the processor implements a data rollback method based on a distributed lock as set forth in the first aspect of the embodiment of the present application when the processor executes the computer program.
In a fourth aspect, embodiments of the present application provide a computer readable storage medium storing a computer program, which when executed by a processor implements a distributed lock-based data rollback method as set forth in the first aspect of embodiments of the present application.
In a fifth aspect, embodiments of the present application provide a computer program product, which when run on a terminal device, causes the terminal device to perform a distributed lock based data rollback method as set forth in the first aspect of embodiments of the present application.
The advantages achieved by the second to fifth aspects described above may be referred to in the description of the first aspect described above.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings that are required for the embodiments or the description of the prior art will be briefly described below, it being obvious that the drawings in the following description are only some embodiments of the present application, and that other drawings may be obtained according to these drawings without inventive effort for a person skilled in the art.
FIG. 1 is a flow chart of one embodiment of a distributed lock based data rollback method provided by embodiments of the present application;
FIG. 2 is a schematic diagram of locking information according to an embodiment of the present application;
FIG. 3 is a block diagram of one embodiment of a distributed lock based data rollback apparatus provided in an embodiment of the present application;
fig. 4 is a schematic diagram of a terminal device provided in 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 configurations, 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. In addition, in the description of the present application and the appended claims, the terms "first," "second," "third," and the like are used merely to distinguish between descriptions and are not to be construed as indicating or implying relative importance.
At present, many large websites and applications are distributed, data consistency in a distributed scene is always a problem that a technician needs to consider with great importance, and the adoption of distributed locks is an effective way for solving the problem. The data rollback method provided by the embodiment of the application is mainly used for solving the problem that dirty data can be generated in a red lock (Redlock) scheme disclosed by the existing distributed lock technology. For specific description and technical implementation details of this data rollback method, reference may be made to the method embodiments described below.
The embodiment of the application can acquire and process the related data based on the artificial intelligence technology. Among these, artificial intelligence (Artificial Intelligence, AI) is the theory, method, technique and application system that uses a digital computer or a digital computer-controlled machine to simulate, extend and extend human intelligence, sense the environment, acquire knowledge and use knowledge to obtain optimal results.
Artificial intelligence infrastructure technologies generally include 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 other directions.
It should be understood that the execution body of the data rollback method based on the distributed lock provided in the embodiments of the present application may be a mobile phone, a tablet computer, a wearable device, a vehicle-mounted device, an augmented reality (augmented reality, AR)/Virtual Reality (VR) device, a notebook computer, an ultra-mobile personal computer (UMPC), a netbook, a personal digital assistant (personal digital assistant, PDA), a large-screen television, or other various types of terminal devices or servers, and the embodiments of the present application do not limit the specific types of the terminal devices and the servers. The server may be an independent server, or may be a cloud server that provides cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, content delivery networks (ContentDelivery Network, CDN), and basic cloud computing services such as big data and artificial intelligence platforms.
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. the method comprises the steps that 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 locking request to a server cluster of the distributed system to obtain a distributed lock required by a task thread to be executed. Typically, the locking request will contain the expiration time of 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 into a designated 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 the locking request sent by the first client, if more than half of nodes make a decision 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 (represented by first locking information) of the distributed lock into a designated cache database, wherein the locking information comprises 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, assuming that the first client is a client a and the obtained distributed lock is X, the client a will obtain that the current time is the locking time, then construct locking information of "ID of the distributed lock x+the current time+id of the client a" (schematic diagram is shown in fig. 2), and write the locking information into the cache database.
It should be understood that, after each client of the distributed system acquires a distributed lock allocated by the server cluster, the same operation steps as those of the first client are performed, and corresponding locking information is written 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;
(2) If other locking information corresponding to the distributed lock exists in the cache database, searching the most night locking time contained in the other locking information, and determining the sum of the most night locking time and the appointed duration as the locking time contained in the first locking information;
(3) And if other locking information corresponding to the distributed lock does not exist in the cache database, determining the current clock time of the first client as the locking time contained in the first locking information.
Since clock time of different clients may be different, when the first client builds locking information, if it is found that other locking information of the distributed lock is already in the cache database (for example, locking information closest to the current time is written when the client C obtains the distributed lock by a certain time), the locking time of the locking information built by the first client adds a certain designated time t to the locking time (i.e. the latest locking time) of the locking information of the client C, so as to ensure that the locking time in the locking information currently written by the first client is the latest in all locking information of the distributed lock existing in the cache database, thereby ensuring validity of the locking information of the first client. In addition, if no other locking information of the distributed lock exists 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 during this process, a first client may stall the thread resulting in a distributed lock overrun, while other clients may acquire the distributed lock.
After the first client executes the corresponding task thread, the latest locking information (represented by the second locking information) corresponding to the distributed lock is searched from the cache database. Since the other clients may already obtain the distributed lock and write locking information with a later locking time into the cache database during the task thread execution of the first client, the second locking information that the first client finds 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 lock information in the cache database that is earlier in lock time (this part of the lock information has not been used) to save space in the cache.
In actual operation, the locking information can be stored in a key-value pair mode, namely a key is formed by the identification of the distributed lock and the locking time; the value consists of the identification of the client and the expiration time of the distributed lock. Assuming that the distributed lock obtained by the first client is X, when searching the second locking information, searching the latest locking information of all locking information of key=id of the distributed lock X 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, the first client rolls back 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, it indicates that the distributed lock has been acquired by another client, and at this time, the first client rolls back the data related to the task thread (data rollback refers to restoring the data modified during execution of the task thread to a state before execution of the task thread), so as to avoid the occurrence of problems such as dirty data. 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 another client (there are two possible cases, 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 and no other client acquires the distributed lock), and in either case, there is no problem of dirty data generation, so the first client may not perform the data rollback operation.
In an implementation manner of the embodiment of the present application, the rollback of 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 is smaller than that of the second client, 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 importance levels of each client, and in general, the importance levels of clients executing the core service are higher, and may be assigned higher priorities. 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, through the server cluster, priority information of the client B, and compare it with its own priority. If the priority of the client a is lower than that of the client B, it indicates that the importance of executing the task 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 dirty data, client a needs to perform data rollback.
Further, after obtaining the priority level between the first client and the second client, the method may further include:
if the priority of the first client is greater than that of the second client, no rollback operation is executed on the data related to the task thread, and a data rollback instruction is sent to the second client, wherein the data rollback instruction is used for indicating the second client to rollback the data which is executed during the process of acquiring the distributed lock.
Conversely, if the priority of the first client a is greater than the priority of the second client B, the task thread of the client a should be prioritized for normal processing, and the client a may not perform the data rollback operation. To avoid dirty data, client a may also send a data rollback instruction to client B that instructs client B to rollback data related to the task thread that it executed during the acquisition of the distributed lock.
In an implementation manner of the embodiment of the present application, after searching, from all locking information stored in the cache database, second locking information corresponding to the distributed lock and having a latest locking time, 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 or not;
(2) If the data related to the task thread is not the predefined core data, not executing the rollback operation on the data related to the task thread;
(3) If the data related to the task thread is the predefined core data, a first lock release request is sent to the server cluster in the stage of releasing the distributed lock, wherein the first lock release request is used for indicating the server cluster to detect whether the distributed lock is still on the first client, if the distributed lock is still on the first client, a lock release process is executed, and if the distributed lock is not on the first client, first exception information is returned to the first client;
(4) And after receiving the first abnormal information, rolling back data related to the task thread.
The first client may pre-construct a core data set, and define a portion of the 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. The core data has high requirements for data accuracy. Therefore, for the core data, verification can be performed once more in the stage of releasing the distributed lock, whether the distributed lock is still on the first client side is checked, and if not, the core data is rolled back, so that the data security is further improved. Conversely, no additional verification operations have to be performed for uncore data.
In an implementation manner of the embodiment of the present application, after searching, from all locking information stored in the cache database, second locking information corresponding to the distributed lock and having a latest locking time, the method may further include:
(1) If the client identifier contained 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, not executing the operation of rollback the data related to the task thread;
(3) If the performance data meets the preset data rollback performance requirement, a second lock release request is sent to the server cluster in the stage of releasing the distributed lock, wherein the second lock release request is used for indicating the server cluster to detect whether the distributed lock is still on the first client, if the distributed lock is still on the first client, a lock release process is executed, and if the distributed lock is not on the first client, second abnormal information is returned to the first client;
(4) And after receiving the second abnormal information, rolling back data related to the task thread.
The performance data of the first client may be data such as CPU performance and memory size 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 verification in the lock releasing stage is performed again, problems such as jamming and the like may be caused, so that verification can be omitted. If the performance data meets the data rollback performance requirement, the performance of the first client is better, and there is no need to worry about the system performance problems such as jamming, so that the verification (whether the distributed lock is still on the first client or not is checked), if yes, the operation of releasing the lock is normally executed, otherwise, the data rollback is performed) can be performed again in the stage of releasing the lock, so as to further improve the data security.
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, from all locking information stored in the cache database, second locking information corresponding to the distributed lock and having a latest locking time, 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 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 means that although no other client obtains the distributed lock, the other client may apply for obtaining the distributed lock because the distributed lock has expired, i.e., the distributed lock on the first client does not already exist. Considering that there may be a possibility that another client fails to write locking information after acquiring the distributed lock (which may be mistaken that the distributed lock is not acquired by another client), the first client still rolls back 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 no data rollback operation has to be performed.
In the embodiment of the application, a locking request is firstly sent to a server cluster by a certain first client; after the first client obtains the corresponding distributed lock based on the locking request, writing first locking information of the distributed lock into a designated cache database, 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-side executes the corresponding task thread, searching second locking information with latest locking time corresponding to the distributed lock from the cache database; if the client identifier contained in the second locking information is not the identifier of the first client, the first client rolls back the data related to the task thread. By means of the arrangement, if the first client side has a thread pause in a task execution thread to cause the distributed lock to be out of date, and the other second client side has obtained the distributed lock, the first client side can find that the client side identification contained in the second locking information searched in the cache database is the identification of the second client side after the thread is executed (the second client side can write the locking information of the distributed lock into the cache database as the first client side after the distributed lock is obtained, wherein the locking information contains the locking time with later time and the identification of the second client side, and obviously the second locking information searched by the first client side is the locking information written by the second client side), and at the moment, the first client side rolls back data related to the task thread, so that the problems of dirty data and the like are avoided.
It should be understood that the sequence number of each step in the foregoing embodiment does not mean that the execution sequence of each process should be determined by the function and the internal logic of each process, and should not limit the implementation process of the embodiment of the present application in any way.
Corresponding to the data rollback method based on the distributed lock described in the above embodiments, fig. 3 shows a block diagram of a data rollback device based on the distributed lock provided in the embodiment of the present application, and for convenience of explanation, only the portion relevant to the embodiment of the present application is 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, after receiving a distributed lock returned by the server cluster based on the locking request, write first locking information of the distributed lock to a specified cache database, where the first locking information includes an identifier of the distributed lock, a locking time, and an identifier of the first client; each client of the plurality of clients writes corresponding locking information into the cache database after receiving the distributed locks distributed by the server cluster;
The locking information searching module 303 is configured to search, after executing the task thread corresponding to the distributed lock, second locking information corresponding to the distributed lock and having the latest locking time from all locking information stored in the cache database;
and the data rollback module 304 is configured to rollback 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 inquiry module is used for inquiring whether other locking information corresponding to the distributed lock exists in the cache database or not;
the night locking time searching module is used for searching the night locking time contained in other locking information if other locking information corresponding to the distributed lock exists in the cache database, and determining the sum of the night locking time and the appointed duration as the locking time contained in the first locking information;
and the locking time determining module is used for determining the current clock time of the first client as the locking time contained in the first locking information if other locking information corresponding to the distributed lock does not exist in the cache database.
In one implementation manner of the embodiment of the present application, the data rollback module may include:
the priority information receiving unit is used for 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 plurality of clients;
the priority comparison unit is used for 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 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 the data rollback instruction sending unit is used for not executing the operation of rollback on the data related to the task thread if the priority of the first client is greater than that of the second client, and sending a data rollback instruction to the second client, wherein the data rollback instruction is used for indicating the second client to rollback the data which is executed during the process of acquiring the distributed lock.
In an implementation manner of the embodiment of the present application, the data rollback apparatus may further include:
the core data judging module is used for judging whether the data related to the task thread is predefined core data or not if the client identifier contained in the second locking information is the identifier of the first client;
the first execution module is used for not executing the rollback operation of 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 in a stage of releasing the distributed lock if the 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 process of releasing the lock 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 receiving the first abnormal information.
In an implementation manner of the embodiment of the present application, the data rollback apparatus may further include:
the performance data acquisition module is used for acquiring the performance data of the first client if the client identifier contained in the second locking information is the 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 meet the preset data rollback performance requirement;
the second verification module is configured to send a second lock release request to the server cluster in 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 instruct the server cluster to detect whether the distributed lock is still on the first client, execute a process of releasing the lock 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:
the expiration judging module is used for judging whether the current time exceeds the expiration time if the client identifier contained in the second locking information is the 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 also provide a computer readable storage medium storing computer readable instructions that, when executed by a processor, implement any of the distributed lock-based data rollback methods as represented in fig. 1.
Embodiments of the present application also provide a computer program product that, when run on a server, causes the server to perform implementing any of the distributed lock-based data rollback methods as represented 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 of the various distributed lock-based data rollback method embodiments described above, such as steps 101 through 104 shown in fig. 1. Alternatively, the processor 40, when executing the computer readable instructions 42, performs the functions of the modules/units of the apparatus embodiments described above, such as the functions of modules 301 through 304 shown in fig. 3.
Illustratively, the computer readable instructions 42 may be partitioned into one or more modules/units that are stored in the memory 41 and executed by the processor 40 to complete the present application. The one or more modules/units may be a series of computer readable instruction segments capable of performing a specific function describing the execution 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, a cloud terminal device, and the like. The terminal device 4 may include, but is not limited to, a processor 40, a memory 41. It will be appreciated by those skilled in the art that fig. 4 is merely an example of the terminal device 4 and does not constitute a limitation of the terminal device 4, and may include more or less components than illustrated, or may combine certain components, or different components, e.g., 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 (Central Processing Unit, CPU), but may also be other general purpose processors, digital signal processors (Digital Signal Processor, DSP), application specific integrated circuits (AppLication Specific Integrated Circuit, ASIC), off-the-shelf programmable gate arrays (fierld-Programmable Gate Array, FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, or the like. 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 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) or 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 for temporarily storing data that has been output or is to be output.
It should be noted that, because the content of information interaction and execution process between the above devices/units is based on the same concept as the method embodiment of the present application, specific functions and technical effects thereof may be referred to in the method embodiment section, and will not be described herein again.
It will be apparent to those skilled in the art that, for convenience and brevity of description, only the above-described division of the functional units and modules is illustrated, and in practical application, the above-described functional distribution may be performed by different functional units and modules according to needs, i.e. the internal structure of the apparatus is divided into different functional units or modules to perform all or part of the above-described functions. The functional units and modules in the embodiment may be integrated in one processing unit, or each unit may exist alone physically, or two or more units may be integrated in one unit, where the integrated units may be implemented in a form of hardware or a form of a 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. The specific working process of the units and modules in the above system may refer to the corresponding process in the foregoing method embodiment, which is not described herein again.
The integrated units, if implemented in the form of software functional units and sold or used as stand-alone products, may be stored in a computer readable storage medium. Based on such understanding, the present application implements all or part of the flow of the method of the above embodiments, and may be implemented by a computer program to instruct related hardware, where the computer program may be stored in a computer readable storage medium, where the computer program, when executed by a processor, may implement the steps of each of the method embodiments described above. Wherein the computer program comprises computer program code which may be in source code form, object code form, 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 device/terminal apparatus, recording medium, computer Memory, read-Only Memory (ROM), random access Memory (RAM, random Access Memory), electrical carrier signals, telecommunications signals, and software distribution media. Such as a U-disk, removable hard disk, magnetic or optical disk, etc.
In the foregoing embodiments, the descriptions of the embodiments are emphasized, and in part, not described or illustrated in any particular embodiment, reference is made to the related descriptions of other embodiments.
The above embodiments are only for illustrating the technical solution of the present application, and are not limiting; 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 scheme described in the foregoing embodiments can be modified or some technical features thereof can be replaced by equivalents; such modifications and substitutions do not depart from the spirit and scope of the technical solutions of the embodiments of the present application, and are intended to be included in the scope of the present application.

Claims (10)

1. A data rollback method based on a distributed lock, applied to a first client, where the first client is any one of a plurality of clients of a distributed system, and the method is characterized in that 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 into a designated cache database, wherein the first locking information comprises an identifier of the distributed lock, locking time and an identifier of the first client; each client of the plurality of clients writes corresponding locking information into the cache database after receiving the distributed locks distributed by the server cluster;
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 locking information comprises a locking time determined by:
inquiring whether other locking information corresponding to the distributed lock exists in the cache database;
if other locking information corresponding to the distributed lock exists in the cache database, searching the most night locking time contained in the other locking information, and determining the sum of the most night locking time and the appointed duration as the locking time contained in the first locking information;
and if other locking information corresponding to the distributed lock does not exist 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 method of data rollback of claim 1, wherein said rolling back data associated with the task thread comprises:
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 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 is smaller than that of the second client, rolling back the data related to the task thread.
4. The data rollback method of claim 3, further comprising, after obtaining a priority size between the first client and the second client:
if the priority of the first client is greater than that of the second client, no rollback operation is executed on the data related to the task thread, and a data rollback instruction is sent to the second client, wherein the data rollback instruction is used for indicating the second client to rollback the data which is executed during the process of acquiring the distributed lock.
5. The data rollback method of claim 1, further comprising, after searching for second locking information corresponding to the distributed lock and having a latest locking time from all locking information stored in the cache database:
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 or not;
if the data related to the task thread is not the predefined core data, not executing the rollback operation on the data related to the task thread;
if the data related to the task thread is the predefined core data, a first lock release request is sent to the server cluster in the stage of releasing the distributed lock, wherein the first lock release request is used for indicating the server cluster to detect whether the distributed lock is still on the first client, if the distributed lock is still on the first client, a lock release process is executed, and if the distributed lock is not on the first client, first exception information is returned to the first client;
and after receiving the first abnormal information, rolling back data related to the task thread.
6. The data rollback method of claim 1, further comprising, after searching for second locking information corresponding to the distributed lock and having a latest locking time from all locking information stored in the cache database:
if the client identifier contained 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, not executing the operation of rollback the data related to the task thread;
if the performance data meets the preset data rollback performance requirement, a second lock release request is sent to the server cluster in the stage of releasing the distributed lock, wherein the second lock release request is used for indicating the server cluster to detect whether the distributed lock is still on the first client, if the distributed lock is still on the first client, a lock release process is executed, and if the distributed lock is not on the first client, second abnormal information is returned to the first client;
and after receiving the second abnormal information, rolling back data related to the task thread.
7. The data rollback method of any of claims 1 to 6 wherein the first locking information further comprises an expiration time of the distributed lock, and after searching for 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 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 that a distributed system has, the apparatus comprising:
a locking request sending module, configured to send a locking request to a server cluster of the distributed system;
the locking information writing module is used for writing first locking information of the distributed lock into a designated cache database after receiving the distributed lock returned by the server cluster 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; each client of the plurality of clients writes corresponding locking information into the cache database after receiving the distributed locks distributed by the server cluster;
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 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 of the claims 1 to 7 when executing the computer program.
10. A computer readable storage medium storing a computer program, wherein the computer program when executed by a processor implements the distributed lock based data rollback method of any 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 CN114679464A (en) 2022-06-28
CN114679464B true 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)

Families Citing this family (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115277712B (en) * 2022-07-08 2023-07-25 北京城市网邻信息技术有限公司 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
分布式锁的设计与实现;周梅;;计算机工程(第16期);全文 *

Also Published As

Publication number Publication date
CN114679464A (en) 2022-06-28

Similar Documents

Publication Publication Date Title
JPH10116259A (en) Quorum mechanism in two-node decentralized type computer system
CN106844055B (en) Task execution method and device
CN107423942B (en) Service transfer method and device
CN114679464B (en) Data rollback method, device, equipment and storage medium based on distributed lock
CN109257396B (en) Distributed lock scheduling method and device
CN112463318A (en) Timed task processing method, device and system
CN111552701A (en) Method for determining data consistency in distributed cluster and distributed data system
CN105302489A (en) Heterogeneous multi-core remote embedded memory system and method
CN112436962B (en) Block chain consensus network dynamic expansion method, electronic device, system and medium
US8341368B2 (en) Automatic reallocation of structured external storage structures
CN112631756A (en) Distributed regulation and control method and device applied to space flight measurement and control software
Du et al. Closing the performance gap between causal consistency and eventual consistency
CN111092956A (en) Resource synchronization method, device, storage medium and equipment
CN111367921A (en) Data object refreshing method and device
CN114564286A (en) Rule engine warning method and rule engine warning system
CN110363515B (en) Rights and interests card account information inquiry method, system, server and readable storage medium
US7337252B2 (en) System and method for resolving conflicts of re-locking resources
CN114036195A (en) Data request processing method, device, server and storage medium
CN113760876A (en) Data filtering method and device
CN116743589B (en) Cloud host migration method and device and electronic equipment
CN116723191B (en) Method and system for performing data stream acceleration calculations using acceleration devices
CN111158590B (en) Method and equipment for solving hash collision
CN114168593A (en) Method, device, equipment, storage medium and program product for generating time sequence main key
CN116521471A (en) Scene handling method, electronic device and storage medium
CN115168003A (en) Timed task processing method and device for cluster server

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

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.

TA01 Transfer of patent application right
GR01 Patent grant
GR01 Patent grant