Method and device for implementing distributed lock

Distributed locks are implemented through unique indexes and shared lock tables, which solves the problems of complexity and high maintenance costs in the existing technology, simplifies the operation of distributed locks, and improves user experience.

CN113760841BActive Publication Date: 2024-10-18BEIJING WODONG TIANJUN INFORMATION TECH CO LTD +1
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202010603579.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-06-29
Publication Date
2024-10-18
Estimated Expiration
2040-06-29

AI Technical Summary

Technical Problem

In the existing technology, the implementation method of distributed locks is complex and the development and maintenance costs are high. In particular, methods based on database locks and Zookeeper require understanding of complex technologies, which increases system complexity and development difficulty.

Method used

By using unique indexes and shared lock tables, distributed locks are implemented, allowing only one thread to insert successfully and other threads to fail, thereby implementing a lock mechanism and avoiding complex deployment and high development costs.

Benefits of technology

It simplifies the implementation of distributed locks, reduces the difficulty of operation, improves user experience, and avoids the complexity and high maintenance costs of existing technologies.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113760841B_ABST
    Figure CN113760841B_ABST
Patent Text Reader

Abstract

The present invention discloses a method and device for implementing distributed locks, and relates to the field of computer technology. A specific implementation of the method includes: receiving an access request for a data resource, obtaining at least one thread corresponding to the access request and a unique index corresponding to the data resource; based on the unique index, controlling at least one thread to insert specific data into a shared lock table corresponding to the data resource; obtaining the insertion result corresponding to at least one thread, and determining the thread accessing the data resource according to the insertion result. This implementation can implement distributed locks through a unique index and a shared lock table, avoiding the technical problems of the existing technology that the deployment is relatively complex and the development and maintenance costs are high, with low operational difficulty and good user experience.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a method and device for implementing a distributed lock. Background Art

[0002] Distributed locks are a way to control the synchronous access to shared resources between distributed systems. If different systems or different devices in the same system share one or a group of resources, then when accessing these resources, mutual exclusion is required to prevent interference with each other to ensure consistency. In this case, distributed locks are needed. The role of distributed locks is to allow only one thread to operate shared resources at the same time in a concurrent situation. This thread is the thread that obtains the lock, and other threads also recognize this lock.

[0003] There are currently several methods for implementing distributed locks: 1. Implementing distributed locks based on database locks; 2. Implementing distributed locks based on cache; 3. Implementing distributed locks based on Zookeeper.

[0004] In the process of implementing the present invention, the inventors found that there are at least the following problems in the prior art: First, the distributed lock implemented by database lock will make the whole solution complicated in the process of solving the problem; Second, the method of implementing distributed lock by cache or Zookeeper requires understanding of cache technology or Zookeeper technology, which has high requirements for developers, resulting in a more complex system and increased development and maintenance costs. Summary of the invention

[0005] In view of this, an embodiment of the present invention provides a method and apparatus for implementing a distributed lock, which can implement a distributed lock through a unique index and a shared lock table, with low operation difficulty and good user experience.

[0006] To achieve the above objective, according to a first aspect of an embodiment of the present invention, a method for implementing a distributed lock is provided.

[0007] A method for implementing a distributed lock according to an embodiment of the present invention includes: receiving an access request for a data resource, obtaining at least one thread corresponding to the access request and a unique index corresponding to the data resource; based on the unique index, controlling the at least one thread to insert specific data into a shared lock table corresponding to the data resource; obtaining an insertion result corresponding to the at least one thread, and determining a thread accessing the data resource based on the insertion result to implement a distributed lock.

[0008] Optionally, obtaining the insertion result corresponding to the at least one thread and determining the thread accessing the data resource based on the insertion result includes: obtaining the insertion result corresponding to each thread in the at least one thread, the insertion result being a successful insertion and a failed insertion; determining a thread with a successful insertion and a thread with a failed insertion based on the insertion result corresponding to each thread; and determining the thread with a successful insertion as the thread accessing the data resource.

[0009] Optionally, after determining that the successfully inserted thread is the thread accessing the data resource, the method further includes: after the successfully inserted thread accesses the data resource, deleting specific data corresponding to the successfully inserted thread from the shared lock table.

[0010] Optionally, the method further includes: determining whether specific data corresponding to the thread that has been successfully inserted is successfully deleted from the shared lock table; if not, deleting the specific data corresponding to the thread that has been successfully inserted using an asynchronous thread.

[0011] Optionally, after determining the thread in which the insertion was successful and the thread in which the insertion failed, the method further includes: returning the result of the failed access request to the thread in which the insertion failed, and receiving a re-request returned by the thread in which the insertion failed; wherein the re-request is a request for accessing the data resource or a request for testing the data resource sent by the thread in which the insertion failed according to the result and corresponding requirements.

[0012] Optionally, before controlling the at least one thread to insert specific data into the shared lock table corresponding to the data resource, the method further includes: constructing a shared lock table corresponding to the data resource.

[0013] Optionally, the unique index is a data feature of the data resource or a combination of multiple data features of the data resource; and the specific data includes: a unique index and attribute data corresponding to the data resource, the attribute data including at least one of the following options: a unique identifier of the specific data, a creation time of the specific data, and an update time of the data resource.

[0014] To achieve the above objective, according to a second aspect of an embodiment of the present invention, a device for implementing a distributed lock is provided.

[0015] An apparatus for implementing a distributed lock according to an embodiment of the present invention comprises: an acquisition module for receiving an access request for a data resource, and acquiring at least one thread corresponding to the access request and a unique index corresponding to the data resource; an insertion module for controlling the at least one thread to insert specific data into a shared lock table corresponding to the data resource based on the unique index; and a determination module for acquiring an insertion result corresponding to the at least one thread, and determining a thread accessing the data resource based on the insertion result, so as to implement a distributed lock.

[0016] Optionally, the determination module is also used to: obtain an insertion result corresponding to each thread in the at least one thread, the insertion result being a successful insertion and a failed insertion; determine a thread in which the insertion is successful and a thread in which the insertion is failed based on the insertion result corresponding to each thread; and determine the thread in which the insertion is successful as the thread accessing the data resource.

[0017] Optionally, the determination module is further used to: after the successfully inserted thread accesses the data resource, delete the specific data corresponding to the successfully inserted thread from the shared lock table.

[0018] Optionally, the device further includes: a deletion module, configured to determine whether the specific data corresponding to the successfully inserted thread is successfully deleted from the shared lock table; if not, deleting the specific data corresponding to the successfully inserted thread using an asynchronous thread.

[0019] Optionally, the determination module is also used to: return the result of the failed access request to the thread where the insertion failed, and receive a re-request returned by the thread where the insertion failed; wherein the re-request is a request for accessing the data resource or a request for testing the data resource sent by the thread where the insertion failed based on the result and corresponding requirements.

[0020] Optionally, the insertion module is further used to: construct a shared lock table corresponding to the data resource.

[0021] Optionally, the unique index is a data feature of the data resource or a combination of multiple data features of the data resource; and the specific data includes: a unique index and attribute data corresponding to the data resource, the attribute data including at least one of the following options: a unique identifier of the specific data, a creation time of the specific data, and an update time of the data resource.

[0022] To achieve the above objective, according to a third aspect of an embodiment of the present invention, an electronic device is provided.

[0023] An electronic device according to an embodiment of the present invention includes: one or more processors; a storage device for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors implement the method for implementing a distributed lock according to an embodiment of the present invention.

[0024] To achieve the above objective, according to a fourth aspect of an embodiment of the present invention, a computer-readable medium is provided.

[0025] A computer-readable medium according to an embodiment of the present invention stores a computer program, and when the program is executed by a processor, the method for implementing a distributed lock according to an embodiment of the present invention is implemented.

[0026] An embodiment of the above invention has the following advantages or beneficial effects: after receiving an access request for a data resource, at least one thread corresponding to the access request and a unique index corresponding to the data resource are obtained, at least one thread can insert data into a shared lock table based on the unique index, and only one thread can insert data successfully, indicating that the data resource can be accessed, and other threads all fail to insert data, thereby realizing distributed locks, avoiding the technical problems of complex deployment and high development and maintenance costs of the prior art, and realizing distributed locks through unique indexes and shared lock tables, with low operational difficulty and good user experience.

[0027] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with the specific implementation manner. BRIEF DESCRIPTION OF THE DRAWINGS

[0028] The accompanying drawings are used to better understand the present invention and do not constitute an improper limitation of the present invention.

[0029] Figure 1 is a schematic diagram of main steps of a method for implementing a distributed lock according to an embodiment of the present invention;

[0030] Figure 2 is a schematic diagram of inserting specific data into a shared lock table by a thread according to an embodiment of the present invention;

[0031] Figure 3 is a schematic diagram of implementing a distributed lock according to an embodiment of the present invention;

[0032] Figure 4 is a schematic diagram of the main process of a method for implementing a distributed lock according to an embodiment of the present invention;

[0033] Figure 5 is a schematic diagram of main modules of a device for implementing a distributed lock according to an embodiment of the present invention;

[0034] Figure 6 is an exemplary system architecture diagram to which embodiments of the present invention may be applied;

[0035] Figure 7 It is a schematic diagram of the structure of a computer system of a terminal device or a server suitable for implementing an embodiment of the present invention. DETAILED DESCRIPTION

[0036] The following is a description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, the description of well-known functions and structures is omitted in the following description.

[0037] The existing technology mainly implements distributed locks based on database locks, caches or Zookeeper. Among them, the methods for implementing distributed locks based on database locks mainly include pessimistic locks and optimistic locks. The pessimistic lock method is that every time you get data, you think that others will modify it, so every time you get data, you will lock it, so that others who want to get this data will be blocked until they get the lock. Many such lock mechanisms are used in traditional relational databases, such as row locks, table locks, read locks, write locks, etc., all of which are locked before performing operations. Pessimistic locks may cause deadlock problems. In high concurrency situations, all threads need to wait at the same time, and the user experience is poor. Optimistic locks are that every time you get data, you think that others will not modify it, so you will not lock it, but when updating, you will judge whether others have updated this data during this period, and you can use mechanisms such as version numbers. Optimistic locks are suitable for multi-read application types, which can improve throughput. Optimistic locks require an additional version number field, and optimistic locks are not necessarily reliable, especially when the lock table is not large. In addition, if it is a multi-table operation, then optimistic locks in the form of version numbers cannot meet the requirements. In general, pessimistic locking and optimistic locking methods will make the entire solution more and more complicated in the process of solving the problem.

[0038] In the method of implementing distributed locks based on cache (such as Redis), users need to have a certain understanding of cache technology and deploy cache services, which obviously increases the difficulty of development. In addition, in this method, the expiration time set for distributed locks is highly business-oriented and less practical. In the method of implementing distributed locks based on Zookeeper, developers need to have a certain grasp of Zookeeper technology and deploy Zookeeper services, which places high demands on developers, resulting in a more complex system and increased development and maintenance costs. Zookeeper is a distributed, open source distributed application coordination service.

[0039] Therefore, the current methods for implementing distributed locks are difficult to operate, require additional technologies, are complex to deploy, and have high development and maintenance costs. In order to solve these problems, an embodiment of the present invention provides a method for implementing distributed locks, which can implement distributed locks using unique indexes. Figure 1 FIG. 1 is a schematic diagram of the main steps of a method for implementing a distributed lock according to an embodiment of the present invention. Figure 1 As shown, the main steps of the method for implementing a distributed lock may include steps S101 to S103.

[0040] Step S101: receiving an access request for a data resource, and obtaining at least one thread corresponding to the access request and a unique index corresponding to the data resource.

[0041] A request for accessing a data resource refers to a request for accessing a certain data resource. Based on the request for accessing the data resource, at least one thread requesting access to the data resource at the same time can be obtained, and a unique index corresponding to the data resource can also be obtained. Among them, a unique index refers to a type of index, and rows with the same index value are not allowed, thereby prohibiting duplicate indexes or key values. The system checks whether there are duplicate key values ​​when creating the index, and checks each time an insert or update statement is used to add data. In an embodiment of the present invention, the unique index feature of the database can be directly utilized, such as a Mysql database. When the data resources stored in the Mysql database have a unique index feature, it can be seen that the method for implementing a distributed lock in an embodiment of the present invention is applied to a database with a unique index feature.

[0042] In addition, the unique index can be a data feature of a data resource or a combination of multiple data features of a data resource. For example, when an e-commerce platform conducts a product promotion, multiple users may rush to buy a certain product at the same time. Since the inventory of the product is limited, users can only successfully buy it if the inventory of the product is sufficient. Assuming that there are 10 users rushing to buy the product, they need to be processed in sequence. After the previous user purchases the product, the next user can buy it if the product is still in stock. This is equivalent to these 10 users concurrently requesting to buy the product. In this case, the unique index can be the unique identifier of the product, such as a serial code, or a combination of multiple features of the product, such as a combination of features of the product brand, product category, and product model.

[0043] Step S102: Based on the unique index, control at least one thread to insert specific data into the shared lock table corresponding to the data resource.

[0044] A shared lock table refers to a table created for a data resource requested for access. In an embodiment of the present invention, different shared lock tables can be created for different data resources. For example, in a ticket booking system, there are ticket issuance and ticket refund services. A shared lock table can be created for the ticket booking system, or corresponding shared lock tables can be created for the ticket issuance and ticket refund services, respectively. In addition, different shared lock tables can be set for tickets for different trains. For example, a shared lock table can be created for the Beijing to Shanghai train, and a shared lock table can also be created for the Beijing to Guangzhou train. Of course, the shared lock table is analyzed according to the specific business situation. Therefore, in a reference embodiment of the present invention, before controlling at least one thread to insert specific data into the shared lock table corresponding to the data resource, the method for implementing a distributed lock can also include: constructing a shared lock table corresponding to the data resource.

[0045] At least one thread inserts specific data into the shared lock table corresponding to the data resource, and the specific data needs to define the unique index corresponding to the data resource. If threads A1 to A10 request to access data resource D, and the unique index corresponding to data resource D is S, then threads A1 to A10 simultaneously insert the specific data corresponding to the unique index S into the shared lock table corresponding to data resource D. Considering the unique index feature, there can only be at most one specific data corresponding to the unique index S in the shared lock table, that is, only one thread can successfully insert the specific data into the shared lock table.

[0046] Step S103: Obtain an insertion result corresponding to at least one thread, and determine a thread that accesses the data resource according to the insertion result to implement a distributed lock.

[0047] The insertion result may include: insertion success and insertion failure. When at least one thread inserts specific data into the shared lock table corresponding to the data resource, only one thread can insert the data successfully, and the insertion result of each thread is obtained, and the thread whose insertion result is successful is determined to be the thread that can access the data resource.

[0048] Considering that the role of a distributed lock is to allow only one thread to operate a shared resource at the same time in a concurrent situation, this thread is the thread that has acquired the lock, and other threads also recognize this lock. Therefore, the method for implementing a distributed lock in an embodiment of the present invention, after receiving an access request for a data resource, obtains at least one thread corresponding to the access request and a unique index corresponding to the data resource, at least one thread can insert data into the shared lock table based on the unique index, and only one thread can insert successfully, indicating that the data resource can be accessed, and the other threads all fail to insert, thereby implementing a distributed lock, which can avoid the technical problems of the prior art that the deployment is relatively complex and the development and maintenance costs are high, and the distributed lock can be implemented through a unique index and a shared lock table, with low operational difficulty and good user experience.

[0049] In a reference embodiment of the present invention, obtaining the insertion result corresponding to at least one thread, and determining the thread accessing the data resource based on the insertion result, may include: obtaining the insertion result corresponding to each thread in the at least one thread, the insertion result being a successful insertion and a failed insertion; determining the thread with a successful insertion and the thread with a failed insertion based on the insertion result corresponding to each thread; and determining the thread with a successful insertion as the thread accessing the data resource. As mentioned in step S103, the insertion result may include a successful insertion and a failed insertion. If the insertion result of a thread is a successful insertion, the thread may be regarded as a thread with a successful insertion. Correspondingly, if the insertion result of a thread is a failed insertion, the thread may be regarded as a thread with a failed insertion. Therefore, in an embodiment of the present invention, at least one thread may be divided into a thread with a successful insertion and a thread with a failed insertion based on the insertion result.

[0050] Specifically, the return value of the insertion result can be used to represent whether the thread insertion is successful. For example, if thread A1 inserts specific data into the shared lock table D and the return value is 1, it means that thread A1 successfully inserts the specific data D into the shared lock table. If thread A2 inserts specific data into the shared lock table D, but returns DuplicateKeyException, it indicates an exception, indicating that thread A2 fails to insert the specific data D into the shared lock table. In addition, in an embodiment of the present invention, at least one thread inserts specific data into the shared lock table, and the return value obtained can also represent the order in which the thread inserts data. The return value corresponding to the thread that inserts the data first is 1, and the return value corresponding to the thread that inserts the data later is DuplicateKeyException.

[0051] Figure 2 Threads 1 to 4 request access to the same data resource, and the primary key of the data resource is set as a unique index. Figure 2 It can be seen that the unique indexes corresponding to threads 1 to 4 are the same, and data is inserted into the shared lock table using the unique index. Figure 2 In the example, thread 3 successfully inserts data into the shared lock table, and the corresponding return value is 1, while threads 1, 2, and 4 fail to insert data, and the corresponding return value is DuplicateKeyException. Then threads 1, 2, and 4 are the threads that failed to insert data, and thread 3 is the thread that successfully inserted data.

[0052] In an embodiment of the present invention, at least one thread sends a request to access a data resource, and the data resource can only be accessed by one thread at the same time. Therefore, at least one thread can insert specific data into the shared lock table corresponding to the data resource based on the unique index feature, and determine that the thread that successfully inserts the data is the thread that can access the data resource. In a reference embodiment of the present invention, the specific data may include: a unique index and attribute data corresponding to the data resource. Among them, the unique index corresponding to the data resource has been explained, and it can be a data feature of the data resource, or a combination of multiple data features of the data resource; attribute data refers to the data of the data resource, which can include at least one of the options of the unique identifier of the specific data, the creation time of the specific data, and the update time of the specific data. The unique identifier of the specific data can be the primary key of the data resource, or it can be an increasing sequence, representing the number of times the data resource is accessed. Therefore, in the case where the unique identifier is an increasing sequence, the concurrent request volume of the data resource can be obtained through the unique identifier. In the case where the concurrent request volume is too high, a reminder message can be sent to the thread in advance. In this case, if a thread does not have to access the data resource at this time, it can request to access the data resource at other times. The creation time of specific data may refer to the time when the thread accesses the data resource, and the update time of the data resource may refer to the time when the thread updates the data resource.

[0053] Since the thread inserts specific data into the shared lock table, and the specific data may include the unique index and attribute data corresponding to the data resource, the created shared lock table may include the unique index, unique identifier, creation time, and update time fields corresponding to the data resource. It should be noted that the unique index is a required field, and the unique identifier, creation time, and update time are optional fields. When a thread inserts specific data into the shared lock table, the unique index can be used to limit the number of threads that successfully insert data. In concurrent operations, the first insert statement will be executed successfully, and the return value is 1. The thread corresponding to the first insert statement is the thread that successfully inserts data. Other insert operations will fail to execute, and a DuplicateKeyException exception may be thrown.

[0054] Different threads have different insertion results after inserting specific data, that is, the return values ​​are different. According to the return value, you can judge whether the current thread can access the data resource, or whether the current thread has obtained the distributed operation lock. If the return value is 1, it means that the current thread can access the data resource. It can be considered that the current thread has obtained the distributed operation lock and can operate on the data resource. After the operation is completed, it is necessary to delete the specific data inserted in the shared lock table so that the lock can be released. Other threads can obtain the distributed operation lock, that is, the thread can access the data resource. If the return value is DuplicateKeyException, you need to catch the exception and then call the insert operation.

[0055] Figure 3 Schematic diagram of implementing a distributed lock according to an embodiment of the present invention. Figure 3 It can be seen that in order to solve the problem of database downtime, a master database and a slave database can be set up. When the master database goes down, the slave database can be used to perform operations. Figure 3 In the example, threads 1 to 4 insert data into the shared lock table corresponding to the data resource in the main database through the unique index. The return result of threads 1, 2, and 4 is a DuplicateKeyException, and the return result of thread 3 is 1. Therefore, it can be considered that thread 3 has obtained the distributed operation lock and can access the data resource, while threads 1, 2, and 4 need to wait and can call the insert data operation again.

[0056] In a reference embodiment of the present invention, after determining that the successfully inserted thread is the thread accessing the data resource, the method for implementing the distributed lock may further include: after the successfully inserted thread accesses the data resource, deleting specific data corresponding to the successfully inserted thread from the shared lock table. For example, Figure 3 After thread 3 successfully inputs data, it can access the data resource and use the data resource to perform business operations. After thread 3 completes the operation, it is necessary to delete the specific data inserted in the shared lock table so that other threads can successfully insert specific data into the shared lock table using the unique index corresponding to the data resource. In other words, for the same unique index, there is at most one specific data corresponding to the unique index in the shared lock table, which solves the problem that the data resource is accessed by only one thread at the same time and implements a distributed lock. In the embodiment of this solution, specific data can be deleted from the shared lock table based on the unique index, or it can be deleted from the shared lock table based on the unique identifier of the specific data or the creation time of the specific data.

[0057] In a reference embodiment of the present invention, the method for implementing a distributed lock may further include: determining whether specific data corresponding to the successfully inserted thread is successfully deleted from the shared lock table; if not, deleting the specific data corresponding to the successfully inserted thread using an asynchronous thread.

[0058] In order to ensure that the lock resources can be released correctly, that is, to ensure that the data resources can be accessed normally and to avoid deadlock, the lock release must be successful. Therefore, it is necessary to determine whether the specific data corresponding to the thread that successfully inserted the data is successfully deleted from the shared lock table. If the deletion fails, it means that the main process has been completed, but the lock release fails. At this time, the main process can end normally. It is necessary to start a separate asynchronous thread to delete the specific data corresponding to the thread that successfully inserted the data. The asynchronous thread is continuously polled for deletion until the deletion is completed, and the lock is released.

[0059] The thread that successfully inserts can obtain the distributed operation lock and access the requested data resource. For the thread that fails to insert, it is necessary to capture the exception and then call the insert operation. Therefore, in the reference embodiment of the present invention, after determining the thread that successfully inserts and the thread that fails to insert, the method for implementing the distributed lock may also include: returning the result of the failed access request to the thread that fails to insert, and receiving a re-request returned by the thread that fails to insert. The re-request is a request for accessing the data resource or a request for testing the data resource sent by the thread that fails to insert according to the result and the corresponding requirements.

[0060] Specifically, if thread A1 requests to access data resource D, and thread A1 fails to insert specific data into the shared lock table corresponding to data resource D, then thread A1 can send a request to access data resource D again, or send a request to test data resource D based on the returned insertion failure result and the specific needs of thread A1.

[0061] Assume that data resource D is an urgent need for thread A1, that is, thread A1 can perform other operations only after accessing data resource D, and needs to access data resource D as soon as possible. Then, after receiving the insertion failure result, thread A1 will send a request to access data resource D again, that is, insert specific data into the shared lock table again.

[0062] For another example, data resource D is not an urgent need for thread A1, that is, thread A1 does not need to access data resource D at the current moment. Then, after receiving the insertion failure result, thread A1 can send a request to access data resource D again, that is, insert specific data into the shared lock table again, or send a request to access data resource D again after a period of time, that is, insert specific data into the shared lock table again.

[0063] Alternatively, according to the specific situation, thread A1 is set to be able to access data resource D within a preset time, that is, thread A1 can successfully insert specific data into the shared lock table within the preset time, and thread A1 can fail to insert specific data into the shared lock table within the preset time. In this case, thread A1 can send a request to test data resource D to test whether there is a problem with data resource D, such as database downtime, network connection, etc.

[0064] It can be seen that in the method for implementing a distributed lock in an embodiment of the present invention, for a thread that fails to be inserted, a request for accessing data resources or a request for testing data resources sent by the thread can be received in combination with the actual needs of the thread, thereby improving the practicality of the solution and bringing a good experience to users.

[0065] Figure 4 FIG. 1 is a schematic diagram of the main process of implementing a distributed lock method according to an embodiment of the present invention. Figure 4 As shown, the main process of implementing the distributed lock method may include:

[0066] Step S401, receiving an access request for a data resource, obtaining at least one thread corresponding to the access request and a unique index corresponding to the data resource, wherein the unique index is a data feature of the data resource or a combination of multiple data features of the data resource;

[0067] Step S402, constructing a shared lock table corresponding to the data resource;

[0068] Step S403: Based on the unique index, control at least one thread to insert specific data into the shared lock table corresponding to the data resource, wherein the specific data may include: a unique index and attribute data corresponding to the data resource, and the attribute data may include at least one of the following options: a unique identifier of the specific data, a creation time of the specific data, and an update time of the data resource;

[0069] Step S404, obtaining an insertion result corresponding to each thread in at least one thread, wherein the insertion result is insertion success or insertion failure;

[0070] Step S405, determining the thread that successfully inserted and the thread that failed to insert according to the insertion result corresponding to each thread, and determining the thread that successfully inserted is the thread that accesses the data resource;

[0071] Step S406, after the successfully inserted thread accesses the data resource, the specific data corresponding to the successfully inserted thread is deleted from the shared lock table;

[0072] Step S407, determining whether the specific data corresponding to the thread successfully inserted is deleted from the shared lock table successfully, if not, executing step S408;

[0073] Step S408, using an asynchronous thread to delete specific data corresponding to the thread that has been successfully inserted;

[0074] Step S409: Return the result of access request failure to the thread that failed to insert, and receive a second request returned by the thread that failed to insert.

[0075] The technical solution for implementing a distributed lock in an embodiment of the present invention obtains at least one thread corresponding to the access request and a unique index corresponding to the data resource after receiving an access request for a data resource. At least one thread can insert data into a shared lock table based on the unique index, and only one thread can insert data successfully, indicating that the data resource can be accessed. The insertion of other threads all fails, thereby implementing a distributed lock. The technical problems of the prior art that the deployment is relatively complex and the development and maintenance costs are relatively high can be avoided. Distributed locks can be implemented through unique indexes and shared lock tables, with low operational difficulty and good user experience.

[0076] Figure 5 Schematic diagram of the main modules of the apparatus for implementing a distributed lock according to an embodiment of the present invention. Figure 5 As shown, the main modules of the apparatus 500 for implementing a distributed lock may include: an acquisition module 501 , an insertion module 502 , and a determination module 503 .

[0077] Among them, the acquisition module 501 can be used to receive an access request for a data resource, and obtain at least one thread corresponding to the access request and a unique index corresponding to the data resource; the insertion module 502 can be used to control at least one thread to insert specific data into a shared lock table corresponding to the data resource based on the unique index; the determination module 503 can be used to obtain an insertion result corresponding to at least one thread, and determine the thread accessing the data resource based on the insertion result to implement a distributed lock.

[0078] In an embodiment of the present invention, the unique index is a data feature of a data resource or a combination of multiple data features of a data resource; and the specific data may include: a unique index and attribute data corresponding to the data resource, and the attribute data may include at least one of the following options: a unique identifier of the specific data, a creation time of the specific data, and an update time of the data resource.

[0079] In an embodiment of the present invention, the determination module 503 can also be used to: obtain the insertion result corresponding to each thread in at least one thread, where the insertion result is successful insertion and failed insertion; determine the thread with successful insertion and the thread with failed insertion according to the insertion result corresponding to each thread; and determine the thread with successful insertion as the thread accessing the data resource.

[0080] In the embodiment of the present invention, the determination module 503 may also be used to: after the successfully inserted thread accesses the data resource, delete the specific data corresponding to the successfully inserted thread from the shared lock table.

[0081] In an embodiment of the present invention, the apparatus for implementing a distributed lock may further include: a deletion module (not shown in the figure). The deletion module may be used to: determine whether the specific data corresponding to the thread that has been successfully inserted is successfully deleted from the shared lock table, and if not, use an asynchronous thread to delete the specific data corresponding to the thread that has been successfully inserted.

[0082] In the embodiment of the present invention, the determination module 503 may also be used to: return the result of the access request failure to the thread that failed to insert, and receive a re-request returned by the thread that failed to insert. The re-request is a request for accessing the data resource or a request for testing the data resource sent by the thread that failed to insert according to the result and the corresponding requirements.

[0083] In the embodiment of the present invention, the insert module 502 may also be used to: construct a shared lock table corresponding to the data resource.

[0084] From the above description, it can be seen that the device for implementing a distributed lock in an embodiment of the present invention, after receiving an access request for a data resource, obtains at least one thread corresponding to the access request and a unique index corresponding to the data resource. At least one thread can insert data into the shared lock table based on the unique index, and only one thread can insert data successfully, indicating that the data resource can be accessed, and the other threads all fail to insert data, thereby implementing a distributed lock, which can avoid the technical problems of complex deployment and high development and maintenance costs of the prior art. Distributed locks can be implemented through unique indexes and shared lock tables, with low operating difficulty and good user experience.

[0085] Figure 6 An exemplary system architecture 600 is shown to which a method for implementing a distributed lock or an apparatus for implementing a distributed lock according to an embodiment of the present invention can be applied.

[0086] like Figure 6 As shown, system architecture 600 may include terminal devices 601, 602, 603, network 604 and server 605. Network 604 is used to provide a medium for communication links between terminal devices 601, 602, 603 and server 605. Network 604 may include various connection types, such as wired, wireless communication links or optical fiber cables, etc.

[0087] Users can use terminal devices 601, 602, and 603 to interact with server 605 through network 604 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 601, 602, and 603, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social platform software, etc. (only examples).

[0088] The terminal devices 601 , 602 , and 603 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, and desktop computers, etc.

[0089] The server 605 may be a server that provides various services, such as a backend management server (only an example) that provides support for shopping websites browsed by users using the terminal devices 601, 602, and 603. The backend management server may analyze and process the received data such as product information query requests, and feed back the processing results (such as target push information, product information - only an example) to the terminal device.

[0090] It should be noted that the method for implementing a distributed lock provided in the embodiment of the present invention is generally executed by the server 605 , and accordingly, the device for implementing a distributed lock is generally disposed in the server 605 .

[0091] It should be understood that Figure 6 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.

[0092] Reference below Figure 7 , which shows a schematic diagram of the structure of a computer system 700 of a terminal device suitable for implementing an embodiment of the present invention. Figure 7 The terminal device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.

[0093] like Figure 7 As shown, the computer system 700 includes a central processing unit (CPU) 701, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 702 or a program loaded from a storage part 708 into a random access memory (RAM) 703. In the RAM 703, various programs and data required for the operation of the system 700 are also stored. The CPU 701, the ROM 702, and the RAM 703 are connected to each other via a bus 704. An input / output (I / O) interface 705 is also connected to the bus 704.

[0094] The following components are connected to the I / O interface 705: an input section 706 including a keyboard, a mouse, etc.; an output section 707 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 708 including a hard disk, etc.; and a communication section 709 including a network interface card such as a LAN card, a modem, etc. The communication section 709 performs communication processing via a network such as the Internet. A drive 710 is also connected to the I / O interface 705 as needed. A removable medium 711, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 710 as needed, so that a computer program read therefrom is installed into the storage section 708 as needed.

[0095] In particular, according to the embodiments disclosed in the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present invention include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 709, and / or installed from the removable medium 711. When the computer program is executed by the central processing unit (CPU) 701, the above-mentioned functions defined in the system of the present invention are executed.

[0096] It should be noted that the computer-readable medium shown in the present invention may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present invention, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. The computer-readable signal medium may also be any computer-readable medium other than a computer-readable storage medium, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.

[0097] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present invention. In this regard, each box in the flow chart or block diagram can represent a module, a program segment, or a part of a code, and the above-mentioned module, program segment, or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0098] The modules involved in the embodiments of the present invention may be implemented in software or hardware. The modules described may also be set in a processor, for example, they may be described as: a processor includes an acquisition module, an insertion module, and a determination module. The names of these modules do not constitute a limitation on the modules themselves in some cases, for example, the acquisition module may also be described as "a module that receives an access request to a data resource, obtains at least one thread corresponding to the access request and a unique index corresponding to the data resource".

[0099] As another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiment; or may exist independently without being assembled into the device. The above computer-readable medium carries one or more programs, and when the above one or more programs are executed by a device, the device includes: receiving an access request for a data resource, obtaining at least one thread corresponding to the access request and a unique index corresponding to the data resource; based on the unique index, controlling at least one thread to insert specific data into a shared lock table corresponding to the data resource; obtaining an insertion result corresponding to at least one thread, and determining the thread accessing the data resource according to the insertion result, so as to implement a distributed lock.

[0100] According to the technical solution of the embodiment of the present invention, after receiving an access request for a data resource, at least one thread corresponding to the access request and a unique index corresponding to the data resource are obtained, and at least one thread can insert data into the shared lock table based on the unique index, and only one thread can insert data successfully, indicating that the data resource can be accessed, and the insertion of other threads all fails, thereby realizing distributed locks, which can avoid the technical problems of complex deployment and high development and maintenance costs of the prior art, and distributed locks can be realized through unique indexes and shared lock tables, with low operational difficulty and good user experience.

[0101] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.

Claims

1. A method for implementing a distributed lock, characterized in that: include: Receive an access request for a data resource, and obtain at least one thread corresponding to the access request and a unique index corresponding to the data resource; wherein the unique index is a data feature of the data resource or a combination of multiple data features of the data resource; Based on the unique index, control the at least one thread to insert specific data into the shared lock table corresponding to the data resource; wherein the specific data includes: the unique index and attribute data corresponding to the data resource, and the attribute data includes at least one of the following options: a unique identifier of the specific data, a creation time of the specific data, and an update time of the data resource; Obtain an insertion result corresponding to the at least one thread, and determine the thread accessing the data resource based on the insertion result to implement a distributed lock, including: obtaining an insertion result corresponding to each thread in the at least one thread, the insertion result being a successful insertion and a failed insertion; determining a thread that successfully inserted and a thread that failed to insert based on the insertion result corresponding to each thread; and determining the thread that successfully inserted is the thread accessing the data resource.

2. The method according to claim 1, characterized in that After determining that the thread that successfully inserted is the thread that accesses the data resource, the method further includes: After the successfully inserted thread accesses the data resource, the specific data corresponding to the successfully inserted thread is deleted from the shared lock table.

3. The method according to claim 2, characterized in that The method further comprises: It is determined whether the specific data corresponding to the thread that has been successfully inserted is deleted from the shared lock table successfully; if not, the specific data corresponding to the thread that has been successfully inserted is deleted using an asynchronous thread.

4. The method according to claim 1, characterized in that: After determining the thread into which the insertion succeeds and the thread into which the insertion fails, the method further includes: Returning the result of the access request failure to the thread that failed to insert, and receiving a second request returned by the thread that failed to insert; wherein, The re-request is a request for accessing the data resource or a request for testing the data resource sent by the thread that failed to insert according to the result and corresponding requirements.

5. The method according to claim 1, characterized in that Before controlling the at least one thread to insert specific data into the shared lock table corresponding to the data resource, the method further includes: Construct a shared lock table corresponding to the data resource.

6. A device for implementing a distributed lock, characterized in that: include: An acquisition module, configured to receive an access request for a data resource, and acquire at least one thread corresponding to the access request and a unique index corresponding to the data resource; wherein the unique index is a data feature of the data resource or a combination of multiple data features of the data resource; An insertion module, configured to control the at least one thread to insert specific data into the shared lock table corresponding to the data resource based on the unique index; wherein the specific data includes: a unique index and attribute data corresponding to the data resource, and the attribute data includes at least one of the following options: a unique identifier of the specific data, a creation time of the specific data, and an update time of the data resource; A determination module is used to obtain an insertion result corresponding to the at least one thread, and determine the thread accessing the data resource based on the insertion result to implement a distributed lock, including: obtaining an insertion result corresponding to each thread in the at least one thread, the insertion result being a successful insertion and a failed insertion; determining a thread that successfully inserted and a thread that failed to insert based on the insertion result corresponding to each thread; and determining the thread that successfully inserted is the thread accessing the data resource.

7. An electronic device, characterized in that: include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 5.

8. A computer readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 5 is implemented.

Citation Information

Patent Citations

  • Hybrid locking using network and on-disk based schemes

    CN101800763A

  • Distributed lock management method and device

    CN110430258A