Distributed lock processing method and device, electronic device, and storage medium

By setting key-value pairs through the application programming interface of object storage, the problems of complex operation and weak anti-concurrency of existing distributed lock solutions are solved, achieving efficient distributed lock management and avoiding data inconsistency caused by master node failure.

CN113010326BActive Publication Date: 2024-11-01PING AN TECH (SHENZHEN) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202110301625.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-03-22
Publication Date
2024-11-01
Estimated Expiration
2041-03-22

AI Technical Summary

Technical Problem

Existing distributed lock implementations are complex to operate, have weak concurrency resistance, and suffer from data inconsistency due to the failure of the master node.

Method used

The application programming interface of object storage is used to set key-value pairs, where the key is the lock name of the distributed lock and the key is the unique hostname of the target application server. It is then used to determine whether the target application server has successfully acquired the lock resource. If successful, the target business operation is performed to implement the distributed lock.

Benefits of technology

It simplifies the operation process, improves concurrency resistance, avoids data asynchrony caused by master node failure, and improves the performance of distributed locks.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113010326B_ABST
    Figure CN113010326B_ABST
Patent Text Reader

Abstract

The present invention relates to big data, and provides a distributed lock processing method and device, electronic device, and storage medium, including: setting a key-value pair in an upload object function through an application program interface of an object storage, wherein the key-value pair includes a key name and a key value, the key name is set to the lock name of the distributed lock, and the key value is set to contain a unique host name of a target application server; judging whether the target application server successfully seizes the lock resource according to the lock name and the unique host name of the target application server; if the target application server successfully seizes the lock resource, operating the target business. The present invention also relates to blockchain technology, and the shared data is stored in a blockchain. Through the present invention, technical problems such as complex operation, weak anti-concurrency, and data asynchrony caused by the failure of the master node in the existing distributed lock implementation methods in the related art are solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to big data processing, and in particular to a distributed lock processing method and device, electronic equipment, and storage medium. Background Art

[0002] In a single Java Virtual Machine (JVM), the Java Development Kit (JDK) provides a ready-made application programming interface (API). However, in distributed applications (multiple JVMs), the lock API provided by the JDK can no longer meet the needs. In order to enable the distributed storage system to run correctly and effectively, it is necessary to control the concurrent access of multiple clients to the same shared resources in order to prevent certain uncertain behaviors from uncontrolled, concurrent or simultaneous access to shared resources, resulting in the destruction of data and metadata consistency. Locks are a common means of implementing concurrent access control.

[0003] At present, there are roughly three solutions for implementing distributed locks on the market: one is to implement the distributed lock function based on the database, and the other two are based on redis or zookeeper. These three distributed lock implementation solutions can be said to have their own advantages and disadvantages.

[0004] First, distributed locks are implemented based on database solutions. Although the implementation is simple, the performance is worrying. It is generally implemented by creating a unique index for the primary key or field in the table. Multiple indexes need to be created, which not only takes up disk space, but also easily leads to database deadlock in high-concurrency scenarios. There is also a single point of failure when using a database. If a database cluster is used, there is also the problem of master-slave data synchronization. Secondly, distributed locks are implemented based on redis. It is mainly based on redis single-threaded request reception and redis setnx command, but it is difficult to control when to delete the key. Using redis to implement distributed locks seems to be the most feasible, but it has deadlocks. Although the framework redisson is used in the market for secondary encapsulation and simple operation, it still cannot solve the situation in sentinel mode or master-slave mode, when the master node hangs up and multiple clients successfully obtain locks at the same time. Finally, for distributed locks based on zookeeper, it is mainly based on its temporary sequential node characteristics and watch monitoring. Although the performance is good, in order to avoid single point of failure, a cluster must be built. The zookeeper environment is complex to build and consumes huge resource costs. In summary, existing distributed lock implementations have technical problems such as complex operations, weak anti-concurrency, and data asynchrony caused by the failure of the master node.

[0005] The inventors have discovered that there is currently no effective solution to the above-mentioned problems in the related art. Summary of the invention

[0006] The present invention provides a distributed lock processing method and device, electronic device, and storage medium, so as to at least solve the technical problems of the existing distributed lock implementation methods in the related technology, such as complex operation, weak anti-concurrency, and data asynchrony caused by the failure of the master node.

[0007] According to one aspect of the present invention, a distributed lock processing method is provided, comprising: setting a key-value pair in an upload object function through an application program interface of an object storage, wherein the key-value pair comprises a key name and a key value, the key name is set to a lock name of the distributed lock, the key value is set to a unique host name containing a target application server, the lock name is determined by a business name of a target business to be operated when multiple application servers concurrently access shared data, and the target application server is any application server among the multiple application servers; judging whether the target application server successfully seizes lock resources based on the lock name and the unique host name of the target application server; and operating the target business if the target application server successfully seizes the lock resources.

[0008] Optionally, before determining whether the target application server successfully seizes the lock resources based on the lock name and the unique host name of the target application server, the method further includes: calling the upload object function through the application interface; calling the obtain object function in the object storage through the application interface to obtain the key-value pair.

[0009] Optionally, the shared data is stored in a blockchain, and judging whether the target application server has successfully seized the lock resources based on the lock name and the unique host name of the target application server includes: judging whether the business name corresponding to the lock name is located in the object storage; if the business name is located in the object storage, judging whether the key value contains the unique host name of the target application server; if the key value contains the unique host name of the target application server, the target application server has successfully seized the lock resources.

[0010] Optionally, after determining whether the business name corresponding to the lock name is located in the object storage, the step further includes: if the key name is not located in the object storage, determining that no application server has seized the lock resources, and resetting the key-value pairs in the upload object function through the application program interface; and executing the step of determining whether the business name corresponding to the lock name is located in the object storage.

[0011] Optionally, after determining whether the key value contains the unique host name of the target application server, the step further includes: step a, if the key value does not contain the unique host name of the target application server, the target application server fails to seize the lock resource; step b, updating the key value in the upload object function to the unique host name of the first application server, wherein the first application server is any application server among the multiple application servers except the target application server; step c, executing the step of calling the get object function in the object storage through the application program interface to obtain the updated key value in the get object function; step d, determining whether the update key value contains the unique host name of the first application server; step e, if the update key value contains the host name of the first application server, the first application server successfully seizes the lock resource; otherwise, returning to execute steps b, c, and d.

[0012] Optionally, after operating the target business, the method further includes: deleting the lock name in the object storage to release lock resources.

[0013] Optionally, if the key value also includes an initial time at which the key value is set to contain the unique host name of the target application server, before successfully operating the target business, the method further includes: updating the initial time to the current time; monitoring the timestamp of the target application server accessing shared data based on the initial time and the current time; comparing the timestamp with a preset time; if the timestamp is greater than or equal to the preset time, deleting the lock name in the object storage to release lock resources.

[0014] According to another aspect of the present invention, a distributed lock processing device is provided, comprising: a setting module, used to set a key-value pair in an upload object function through an application program interface of an object storage, wherein the key-value pair comprises a key name and a key value, the key name is set to a lock name of the distributed lock, the key value is set to a unique host name containing a target application server, the lock name is determined by a business name of a target business to be operated when multiple application servers concurrently access shared data, and the target application server is any application server among the multiple application servers; a judgment module, used to judge whether the target application server has successfully seized lock resources based on the lock name and the unique host name of the target application server; and an operation module, used to operate the target business if the target application server has successfully seized lock resources.

[0015] Optionally, the device also includes: a first calling module, used to call the upload object function through the application interface before determining whether the target application server successfully seizes the lock resources based on the lock name and the unique host name of the target application server; and a second calling module, used to call the obtain object function in the object storage through the application interface to obtain the key-value pair.

[0016] Optionally, the shared data is stored in a blockchain, and the judgment module includes: a first judgment unit, used to judge whether the business name corresponding to the lock name is located in the object storage; a second judgment unit, used to judge whether the key value contains the unique host name of the target application server if the business name is located in the object storage; and a first determination unit, used to determine whether the target application server successfully preempts the lock resources if the key value contains the unique host name of the target application server.

[0017] Optionally, the judgment module also includes: a second determination unit, used to determine whether the business name corresponding to the lock name is located in the object storage after determining whether it is located in the object storage, if the key name is not located in the object storage, then determine that no application server has preempted the lock resources, and reset the key-value pairs in the upload object function through the application program interface; a first execution unit, used to execute the step of determining whether the business name corresponding to the lock name is located in the object storage.

[0018] Optionally, the judgment module also includes: a third determination unit, used to execute step a, after judging whether the key value contains the unique host name of the target application server, if the key value does not contain the unique host name of the target application server, the target application server fails to seize the lock resource; an update unit, used to execute step b, updating the key value in the upload object function to the unique host name of the first application server, wherein the first application server is any application server among the multiple application servers except the target application server; a second execution unit, used to execute step c, executing the step of calling the obtain object function in the object storage through the application program interface to obtain the updated key value in the obtain object function; a judgment unit, used to execute step d, judging whether the update key value contains the unique host name of the first application server; a processing unit, used to execute step e, if the update key value contains the host name of the first application server, the first application server successfully seizes the lock resource; otherwise, return to execute step b, step c, step d.

[0019] Optionally, the device further includes: a first deletion module, configured to delete the lock name in the object storage after operating the target business to release lock resources.

[0020] Optionally, if the key value also includes the initial time at which the key value is set to contain the unique host name of the target application server, the device also includes: an update module, used to update the initial time to the current time before successfully operating the target business; a monitoring module, used to monitor the timestamp of the target application server accessing shared data based on the initial time and the current time; a comparison module, used to compare the timestamp with a preset time; and a second deletion module, used to delete the lock name in the object storage to release lock resources if the timestamp is greater than or equal to the preset time.

[0021] According to another aspect of the present invention, there is provided an electronic device, comprising a memory and a processor, wherein the memory stores a computer program, and the processor is configured to run the computer program to execute the steps in any one of the above method embodiments.

[0022] According to yet another aspect of the present invention, a storage medium is provided, in which a computer program is stored, wherein the computer program is configured to execute the steps of any of the above-mentioned device embodiments when running.

[0023] Through the present invention, the application program interface of the object storage is used to set the key name in the upload object function to the lock name of the distributed lock (determined by the business name of the business to be operated), and the key value corresponding to the key name is set to contain the unique host name of the target application server; then, according to the lock name and the unique host name of the target application server, it is judged whether the target application server successfully seizes the lock resource; if the seizure is successful, the target business has the authority to operate (that is, the authority to access shared data), and the use of object storage to implement the distributed lock is realized, without the need to use third-party components as tools for the distributed lock, and the operation is simple; decentralization improves the anti-concurrency of the distributed lock implementation, avoids the situation where data is out of sync due to the main node hanging up, solves the technical problems of the existing distributed lock implementation method in the related technology, such as complex operation, weak anti-concurrency, and data out of sync due to the main node hanging up, and improves the performance of implementing the distributed lock. BRIEF DESCRIPTION OF THE DRAWINGS

[0024] The drawings described herein are used to provide a further understanding of the present invention and constitute a part of this application. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the drawings:

[0025] Figure 1 It is a hardware structure block diagram of a distributed lock processing method according to an embodiment of the present invention applied to a computer terminal;

[0026] Figure 2is a flow chart of a distributed lock processing method according to an embodiment of the present invention;

[0027] Figure 3 is a flow chart of implementing a distributed lock according to a specific embodiment of the present invention;

[0028] Figure 4 is a structural block diagram of a distributed lock processing device according to an embodiment of the present invention;

[0029] Figure 5 is a schematic structural diagram of an electronic device according to an embodiment of the present invention. DETAILED DESCRIPTION

[0030] The present invention will be described in detail below with reference to the accompanying drawings and in combination with embodiments. It should be noted that the embodiments and features in the embodiments of the present application can be combined with each other without conflict.

[0031] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence.

[0032] The method embodiment provided in the first embodiment of the present application can be executed in a mobile terminal, a server, a computer terminal or a similar computing device. Taking running on a computer terminal as an example, Figure 1 1 is a hardware structure diagram of a distributed lock processing method according to an embodiment of the present invention applied to a computer terminal. Figure 1 As shown, the computer terminal may include one or more ( Figure 1 Only one is shown in the figure) a processor 102 (the processor 102 may include but is not limited to a processing device such as a microprocessor MCU or a programmable logic device FPGA) and a memory 104 for storing data. Optionally, the computer terminal may also include a transmission device 106 for communication functions and an input and output device 108. It can be understood by those skilled in the art that Figure 1 The structure shown is only for illustration and does not limit the structure of the above-mentioned computer terminal. Figure 1 More or fewer components as shown, or with Figure 1 Different configurations are shown.

[0033] The memory 104 can be used to store computer programs, for example, software programs and modules of application software, such as the computer program corresponding to the processing method of the distributed lock in the embodiment of the present invention. The processor 102 executes various functional applications and data processing by running the computer program stored in the memory 104, that is, to implement the above method. The memory 104 may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory, and may also include a volatile memory. In some examples, the memory 104 may further include a memory remotely arranged relative to the processor 102, and these remote memories may be connected to the computer terminal via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and a combination thereof.

[0034] The transmission device 106 is used to receive or send data via a network. The specific example of the above network may include a wireless network provided by a communication provider of a computer terminal. In one example, the transmission device 106 includes a network adapter (Network Interface Controller, referred to as NIC), which can be connected to other network devices through a base station so as to communicate with the Internet. In one example, the transmission device 106 can be a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.

[0035] In order to solve the above-mentioned problems existing in the related technologies, this solution provides a method for processing distributed locks using object storage. If object storage is used in the project and there are distributed lock usage scenarios, object storage can be directly used to implement distributed locks without building third-party components; the distributed lock implementation method provided by this solution can be applied to business scenarios such as ordering and flash sales.

[0036] Among them, object storage is object / file-oriented, massive Internet storage, which can also be directly called "cloud storage". Object storage can be divided into: file storage (file system), block storage (hard disk) and object storage (key-value database) according to data storage type. Among them, object storage deals with PB-level data, which is neatly arranged in containers (buckets) and filled with various object data. Goods (data) sent by hundreds of thousands of customers can be processed and remembered clearly according to key values ​​(key, value).

[0037] Although an object is a file, it is an encapsulated file (objects in programming have the characteristic of encapsulation). That is to say, in an object storage system, you cannot directly open / modify a file, but you can upload and download files like FTP. In addition, object storage does not have a multi-level file structure like a file system, but only has a concept of "bucket" (that is, storage space). The "bucket" is full of objects, which is a very flat storage method. Its biggest feature is that its object name is a domain name address. Once the object is set to "public", all netizens can access it; its owner can also access the objects in it through the REST API. Therefore, the most mainstream use scenario of object storage is to store static content (videos, pictures, files, software installation packages, etc.) of Internet / mobile Internet applications such as websites and mobile apps.

[0038] Figure 2 is a flow chart of a distributed lock processing method according to an embodiment of the present invention. Figure 2 As shown, the process includes the following steps:

[0039] Step S202, setting a key-value pair in the upload object function through the application program interface of the object storage, wherein the key-value pair includes a key name and a key value, the key name is set to the lock name of the distributed lock, the key value is set to the unique host name containing the target application server, the lock name is determined by the business name of the target business to be operated when multiple application servers concurrently access shared data, and the target application server is any application server among the multiple application servers;

[0040] Distributed locks are a way to control the synchronous access to shared resources (i.e. the shared data mentioned above) between distributed systems. The purpose is to ensure that when multiple threads are concurrent, only one thread (each thread corresponds to an application server) operates the business, method or variable (i.e. the target business mentioned above) at the same time, thereby accessing shared resources.

[0041] In this embodiment, the key to the implementation of distributed locks is to build a storage server (i.e., the above-mentioned object storage) outside the distributed application server to store lock information (i.e., lock resources); then the distributed lock is implemented by calling the object storage application program interface API (full name: Application Programming Interface). The object storage interface complies with the restful interface specification, and the call is simple. There is no need to use third-party components as distributed lock tools, which reduces costs; it is decentralized, unlike frameworks such as zookeeper, redis, and database, which have master nodes, thereby solving the problem of data asynchrony caused by the failure of the master node.

[0042] In this embodiment, the object storage may be OBS object storage (full name: Object-based Storage) or OSS object storage service (full name: Object Storage Service), but is not limited thereto.

[0043] This embodiment is implemented by using the overwriteable property of putObject(key, value).

[0044] Preferably, Java provides an API, which can obtain the unique host name and current system time of the application server by directly calling the object storage API; the key of putObject is designed to be the lock name of the distributed lock (key is the unique identifier of the lock, named according to business decisions), and the value is designed to be the host name of the application server (that is, the unique host name mentioned above).

[0045] Step S204, judging whether the target application server successfully seizes the lock resource according to the lock name and the unique host name of the target application server;

[0046] Optionally, a bucket is used to store lock resources. According to the business name and the unique host name, it is determined whether the current application server (i.e., the target application server mentioned above) has the authority to execute the target business (whether the lock resource is successfully seized), i.e., the authority to access the shared data.

[0047] Step S206: If the target application server successfully seizes the lock resource, the target service is operated.

[0048] It should be emphasized that in order to further ensure the privacy and security of the above-mentioned shared data, the above-mentioned shared data can also be stored in a node of a blockchain.

[0049] The distributed lock processing method provided by the embodiment of the present invention adopts the application program interface of the object storage to set the key name in the upload object function as the lock name of the distributed lock, and sets the key value corresponding to the key name to contain the unique host name of the target application server; then, according to the lock name and the unique host name of the target application server, it is judged whether the target application server has successfully seized the lock resource. If the seizure is successful, it has the authority to operate the target business, and the use of object storage to implement the distributed lock is realized. There is no need to adopt third-party components as tools for distributed locks, and the operation is simple; decentralization improves the anti-concurrency of the distributed lock implementation, avoids the situation where data is out of sync due to the main node hanging up, solves the technical problems of the existing distributed lock implementation method in the related technology, such as complex operation, weak anti-concurrency and data out of sync due to the main node hanging up, and improves the performance of implementing the distributed lock.

[0050] In an optional embodiment of the present case, before determining whether the target application server has successfully seized the lock resources based on the lock name and the unique host name of the target application server, it also includes: calling an upload object function through an application program interface; calling an obtain object function in the object storage through an application program interface to obtain a key-value pair.

[0051] In an optional embodiment of the present case, the shared data is stored in the blockchain, and judging whether the target application server has successfully seized the lock resource based on the lock name and the unique host name of the target application server includes: judging whether the business name corresponding to the lock name is located in the object storage; if the business name is located in the object storage, judging whether the key value contains the unique host name of the target application server; if the key value contains the unique host name of the target application server, the target application server has successfully seized the lock resource.

[0052] The blockchain referred to in this invention is a new application mode of computer technologies such as distributed data storage, peer-to-peer transmission, consensus mechanism, encryption algorithm, etc. Blockchain is essentially a decentralized database, a string of data blocks generated by cryptographic methods. Each data block contains a batch of network transaction information, which is used to verify the validity of its information (anti-counterfeiting) and generate the next block. Blockchain can include the underlying blockchain platform, platform product service layer, and application service layer.

[0053] Optionally, after operating the target business, the method further includes: deleting the lock name in the object storage to release the lock resources.

[0054] In an alternative example, Figure 3 is a processing flow chart of a distributed lock provided according to a specific embodiment of the present invention, see Figure 3 As shown, the following steps are included:

[0055] Step S302, after setting the uploaded object putObject(key, value) to the lock name of the distributed lock and the unique host name of the current application server (i.e., the above-mentioned target application server), execute the putObject operation (i.e., call the upload object function); then call the getObject function (i.e., the above-mentioned get object function) to obtain the key-value (and the above-mentioned key-value pair) in the getObject interface request through the API.

[0056] Step S304, determine whether the key in the getObject interface request is in the object storage, if so (getObject(key) is identified as correct), then further execute step S306, obtain getObject(value), and determine whether the host name in the value is the same as the host name of the server where the application is located (that is, the unique host name of the above-mentioned target application server);

[0057] Step S308: If they are the same, the current application server (i.e., the target application server) acquires the lock successfully, i.e., has the authority to execute the shared data, and then operates the target business;

[0058] Step S310: After the target business is successfully operated, the key (lock name) in the object storage OBS is deleted to release the lock resources.

[0059] Optionally, after determining whether the business name corresponding to the lock name is located in the object storage, it also includes: if the key name is not located in the object storage, determining that no application server has preempted the lock resources, and resetting the key-value pairs in the upload object function through the application program interface; and executing the step of determining whether the business name corresponding to the lock name is located in the object storage.

[0060] like Figure 3 As shown, when the getObject(key) method is called, if the key does not exist in the OBS, an error will be reported, indicating that the lock grabbing failed, that is, there is currently no process (application server) grabbing the lock resources; then execute step S302, call the putObject function (that is, the above-mentioned upload object function), redesign the key-value pair, and after success, execute step S304.

[0061] In another embodiment of the present case, after determining whether the key value contains the unique host name of the target application server, the method further includes: step a, if the key value does not contain the unique host name of the target application server, the target application server fails to seize the lock resource; step b, updating the key value in the upload object function to the unique host name of the first application server, wherein the first application server is any application server among multiple application servers except the target application server; step c, executing the step of calling the get object function in the object storage through the application program interface to obtain the updated key value in the get object function; step d, determining whether the update key value contains the unique host name of the first application server; step e, if the update key value contains the host name of the first application server, the first application server successfully seizes the lock resource; otherwise, returning to execute steps b, c, and d.

[0062] like Figure 3As shown, if the key exists in the OBS when the getObject(key) method is called, the value is obtained, and it is determined whether the host name before the colon of the value (i.e., the unique host name of the application server) is the same as the host name of the server where the application is located; if they are the same, the current process (the current process corresponds to the current application server, i.e., the target application server) successfully grabs the lock and can execute the operation of the target business (i.e., access shared data). After the operation is completed, the key in the storage object is deleted (the lock is released); if they are not the same, the lock acquisition fails, and the server where the application is located does not have the authority to execute the target business, and then the key value in the upload object function is updated to any application server among multiple application servers except the current application server (i.e., the first application server for uploading data), and the process returns to step S304, executes the call to the getObject method, and step S306, and continues to determine whether the value (i.e., the above-mentioned updated key value) is the host name of the application server corresponding to other processes (i.e., the unique host name of the above-mentioned first application server), wherein the application server and the process are in a one-to-one mapping relationship.

[0063] According to an example of the above embodiment, taking the following order as an example, when a customer places an order, the inventory center is generally called to reduce the inventory, specifically: update store set num=$num where id=$id / / Update store inventory information; and by setting the modification method of the inventory, it is very easy to lose the update of the database when the concurrency is high. Assuming that there are two current transactions A and B, the queried inventory is 5, A has placed 3 orders, and the inventory needs to be modified to 2, and B has placed 1 order, and the inventory needs to be modified to 4. If A and B access the inventory concurrently, the execution result of A transaction will cover the execution result of B transaction, or the execution result of B transaction will cover the execution result of A transaction, which is very easy to be confused and the result is unpredictable.

[0064] Through this embodiment, multiple transactions (such as the above-mentioned A and B transactions) will access the inventory (shared data) and perform inventory reduction (i.e., the above-mentioned target business), and the inventory reduction operation will be performed only after a single transaction grabs the lock resource. For example, after A grabs the lock resource, it will reduce the inventory, and B can only grab the lock resource after A releases the lock resource, and then perform the inventory reduction, thereby preventing multiple transactions from modifying the inventory concurrently, so as to avoid inaccurate data.

[0065] Optionally, if the key value also includes an initial time in which the key value is set to contain a unique host name of the target application server, before the target business is successfully operated, it also includes: updating the initial time to the current time; monitoring the timestamp of the target application server accessing the shared data based on the initial time and the current time; comparing the timestamp with a preset time; if the timestamp is greater than or equal to the preset time, deleting the lock name in the object storage to release the lock resources.

[0066] In a preferred embodiment of the present case, the value is designed into the format of the application server host name (i.e., the above unique host name) + colon + current system time by string concatenation, i.e., application server host name: current operating system time. Each client (client, corresponding to the application server one by one) needs to enable a scheduled job (task) (ratio 30 seconds, i.e., the above preset time), i.e., the expiration time, to monitor the bucket storing the lock resource, cyclically detect the value corresponding to each key under the bucket, i.e., monitor the time value in the value, and compare it with the current time. If the difference (i.e., the above timestamp) is greater than the preset time (e.g., 30 seconds), the key in the object storage is set to an expiration time to avoid deadlock.

[0067] Since the value corresponding to each key in the bucket is the time to seize the lock, the process that usually seizes the lock will release the lock resource after executing the shared data operation. If the current process hangs up, it is very easy to cause the current process to always hold the lock resource, thereby affecting other processes from seizing the lock. Through this embodiment, the time in the value corresponding to each key under the bucket is monitored. If it exceeds the preset time for seizing the lock resource (for example, 30 seconds), the key in the object storage is deleted, so that other processes can seize the lock resource.

[0068] Through the embodiments of the present invention, the use of object storage OBS to implement distributed locks has the following beneficial effects compared with other existing distributed lock implementation solutions (such as zookeeper, redis, database, etc.): 1) The operation is simple, and it can be implemented by simply calling the API in the object storage; 2) It is decentralized, unlike using third-party components to implement distributed locks, once the main node hangs up, it will cause data to be out of sync; 3) The OBS gateway can also be dynamically expanded to improve the concurrency of accessing shared data; 4) The object storage OBS interface complies with the restful (where the full name of rest is representational state transfer, that is, representational state transfer, and the Chinese equivalent of restful is rest-style) interface specification, and the call is simple; 5) The underlying data of OBS adopts a distributed storage mechanism. Even in extreme cases where a network failure or a bad disk occurs, the client application can interact with the OBS for data, and the client will not be unavailable.

[0069] Based on the distributed lock processing methods provided in the above embodiments, based on the same inventive concept, a distributed lock processing device is also provided in this embodiment, which is used to implement the above embodiments and preferred implementations, and will not be repeated for what has been described. As used below, the term "module" can implement a combination of software and / or hardware for a predetermined function. Although the devices described in the following embodiments are preferably implemented in software, the implementation of hardware, or a combination of software and hardware, is also possible and conceived.

[0070] Figure 4 is a structural block diagram of a distributed lock processing device according to an embodiment of the present invention, such as Figure 4 As shown, the device includes: a setting module 40, which is used to set a key-value pair in an upload object function through an application program interface of an object storage, wherein the key-value pair includes a key name and a key value, the key name is set to a lock name of a distributed lock, the key value is set to a unique host name containing a target application server, the lock name is determined by a business name of a target business to be operated when multiple application servers concurrently access shared data, and the target application server is any application server among multiple application servers; a judgment module 42, which is connected to the above-mentioned setting module 40, and is used to judge whether the target application server has successfully seized the lock resource based on the lock name and the unique host name of the target application server; an operation module 44, which is connected to the above-mentioned judgment module 42, and is used to operate the target business if the target application server has successfully seized the lock resource.

[0071] It should be emphasized that in order to further ensure the privacy and security of the above-mentioned shared data, the above-mentioned shared data can also be stored in a node of a blockchain.

[0072] Optionally, the above-mentioned device also includes: a first calling module, which is used to call the upload object function through the application program interface before determining whether the target application server successfully seizes the lock resources based on the lock name and the unique host name of the target application server; and a second calling module, which is used to call the obtain object function in the object storage through the application program interface to obtain a key-value pair.

[0073] Optionally, the shared data is stored in the blockchain, and the judgment module 42 includes: a first judgment unit, used to judge whether the business name corresponding to the lock name is located in the object storage; a second judgment unit, used to judge whether the key value contains the unique host name of the target application server if the business name is located in the object storage; a first determination unit, used to determine whether the target application server successfully preempts the lock resources if the key value contains the unique host name of the target application server.

[0074] Optionally, the judgment module 42 also includes: a second determination unit, which is used to determine whether the business name corresponding to the lock name is located in the object storage, and if the key name is not located in the object storage, determine that no application server has preempted the lock resources, and reset the key-value pairs in the upload object function through the application program interface; a first execution unit, which is used to execute the step of determining whether the business name corresponding to the lock name is located in the object storage.

[0075] Optionally, the judgment module 42 also includes: a third determination unit, used to execute step a, after determining whether the key value contains the unique host name of the target application server, if the key value does not contain the unique host name of the target application server, the target application server fails to seize the lock resource; an update unit, used to execute step b, updating the key value in the upload object function to the unique host name of the first application server, wherein the first application server is any application server among multiple application servers except the target application server; a second execution unit, used to execute step c, executing the step of calling the get object function in the object storage through the application program interface to obtain the updated key value in the get object function; a judgment unit, used to execute step d, determining whether the update key value contains the unique host name of the first application server; a processing unit, used to execute step e, if the update key value contains the host name of the first application server, the first application server successfully seizes the lock resource; otherwise, return to execute step b, step c, and step d.

[0076] Optionally, the above device also includes: a first deletion module, used to delete the lock name in the object storage after operating the target business to release the lock resources.

[0077] Optionally, if the key value also includes an initial time in which the key value is set to contain a unique host name of the target application server, the above-mentioned device also includes: an update module, used to update the initial time to the current time before the target business is successfully operated; a monitoring module, used to monitor the timestamp of the target application server accessing the shared data based on the initial time and the current time; a comparison module, used to compare the timestamp with a preset time; and a second deletion module, used to delete the lock name in the object storage if the timestamp is greater than or equal to the preset time to release the lock resources.

[0078] It should be noted that the above modules can be implemented by software or hardware. For the latter, it can be implemented in the following ways, but not limited to: the above modules are all located in the same processor; or the above modules are located in different processors in any combination.

[0079] An embodiment of the present invention further provides a storage medium, in which a computer program is stored, wherein the computer program is configured to execute the steps of any of the above method embodiments when running.

[0080] Optionally, in this embodiment, the storage medium may be configured to store a computer program for performing the following steps:

[0081] S1, setting a key-value pair in an upload object function through an application program interface of an object storage, wherein the key-value pair includes a key name and a key value, the key name is set to a lock name of a distributed lock, the key value is set to a unique host name containing a target application server, the lock name is determined by a business name of a target business to be operated when multiple application servers concurrently access shared data, and the target application server is any application server among the multiple application servers;

[0082] S2, judging whether the target application server successfully seizes the lock resource according to the lock name and the unique host name of the target application server;

[0083] S3: If the target application server successfully seizes the lock resource, the target service is operated.

[0084] Optionally, in this embodiment, the above-mentioned storage medium may include but is not limited to: a USB flash drive, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk or an optical disk, and other media that can store computer programs.

[0085] It should be emphasized that in order to further ensure the privacy and security of the above-mentioned shared data, the above-mentioned shared data can also be stored in a node of a blockchain.

[0086] Based on the above Figure 2 The method shown and Figure 4 In order to achieve the above-mentioned purpose, the present application also provides an electronic device, such as Figure 5 As shown, the system includes a memory 52 and a processor 51, wherein the memory 52 and the processor 51 are both arranged on a bus 53. The memory 52 stores a computer program, and the processor 51 executes the computer program to realize Figure 2 The distributed lock processing method shown in FIG.

[0087] Based on this understanding, the technical solution of the present application can be embodied in the form of a software product, which can be stored in a memory (which can be a CD-ROM, a USB flash drive, a mobile hard disk, etc.), and includes a number of instructions for enabling an electronic device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each implementation scenario of the present application.

[0088] Optionally, the device may also be connected to a user interface, a network interface, a camera, a radio frequency (RF) circuit, a sensor, an audio circuit, a WI-FI module, etc. The user interface may include a display, an input unit such as a keyboard, etc., and the optional user interface may also include a USB interface, a card reader interface, etc. The network interface may optionally include a standard wired interface, a wireless interface (such as a Bluetooth interface, a WI-FI interface), etc.

[0089] Those skilled in the art will appreciate that the structure of an electronic device provided in this embodiment does not limit the physical device, and may include more or fewer components, or a combination of certain components, or different component arrangements.

[0090] Optionally, the specific examples in this embodiment may refer to the examples described in the above embodiments and optional implementation modes, and this embodiment will not be described in detail here.

[0091] Obviously, those skilled in the art should understand that the above modules or steps of the present invention can be implemented by a general computing device, they can be concentrated on a single computing device, or distributed on a network composed of multiple computing devices, and optionally, they can be implemented by a program code executable by a computing device, so that they can be stored in a storage device and executed by the computing device, and in some cases, the steps shown or described can be executed in a different order than here, or they can be made into individual integrated circuit modules, or multiple modules or steps therein can be made into a single integrated circuit module for implementation. Thus, the present invention is not limited to any specific combination of hardware and software.

[0092] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the principle of the present invention shall be included in the protection scope of the present invention.

Claims

1. A distributed lock processing method, characterized in that: include: Setting a key-value pair in the upload object function through an application program interface of the object storage, wherein the key-value pair includes a key name and a key value, the key name is set to a lock name of a distributed lock, the key value is set to a unique host name containing a target application server, the lock name is determined by a business name of a target business to be operated when multiple application servers concurrently access shared data, and the target application server is any application server among the multiple application servers; Calling the upload object function through the application program interface; Calling an object acquisition function in the object storage through the application program interface to acquire the key-value pair; Determine whether the business name corresponding to the lock name is located in the object storage; If the business name is located in the object storage, determining whether the key value contains a unique host name of the target application server; If the key value contains the unique host name of the target application server, the target application server successfully seizes the lock resource and operates the target service; If the key value does not contain the unique host name of the target application server, perform the following steps: Step a, determining that the target application server fails to seize lock resources; Step b, updating the key value in the upload object function to the unique host name of the first application server, wherein the first application server is any application server among the multiple application servers except the target application server; Step c, executing the step of calling the object acquisition function in the object storage through the application program interface to acquire the update key value in the object acquisition function; Step d, determining whether the update key value contains the unique host name of the first application server; Step e: if the update key value contains the host name of the first application server, the first application server successfully seizes the lock resource; otherwise, return to execute step b, step c, and step d.

2. The method according to claim 1, characterized in that The shared data is stored in the blockchain.

3. The method according to claim 2, characterized in that After determining whether the business name corresponding to the lock name is located in the object storage, the method further includes: If the key name is not located in the object storage, it is determined that no application server has seized the lock resources, and the key-value pair in the upload object function is reset through the application program interface; and the step of determining whether the business name corresponding to the lock name is located in the object storage is executed.

4. The method according to claim 1, characterized in that After operating the target service, the method further includes: Delete the lock name in the object storage to release the lock resources.

5. The method according to claim 1, characterized in that: If the key value further includes the initial time when the key value is set to contain the unique host name of the target application server, before the target service is successfully operated, the method further includes: Update the initial time to the current time; Monitoring a timestamp of access of the target application server to the shared data based on the initial time and the current time; comparing the timestamp with a preset time; If the timestamp is greater than or equal to the preset time, the lock name in the object storage is deleted to release the lock resources.

6. A distributed lock processing device, characterized in that: include: A setting module, used to set a key-value pair in an upload object function through an application program interface of an object storage, wherein the key-value pair includes a key name and a key value, the key name is set to a lock name of a distributed lock, the key value is set to a unique host name containing a target application server, the lock name is determined by a business name of a target business to be operated when multiple application servers concurrently access shared data, and the target application server is any application server among the multiple application servers; The first calling module is used to call the upload object function through the application program interface before determining whether the target application server successfully seizes the lock resource according to the lock name and the unique host name of the target application server; A second calling module is used to call an object acquisition function in the object storage through an application program interface to obtain a key-value pair; A judgment module, used for judging whether the target application server successfully seizes the lock resource according to the lock name and the unique host name of the target application server; An operation module, used for operating the target service if the target application server successfully seizes the lock resource; The judgment module includes: a first judgment unit, used to judge whether the business name corresponding to the lock name is located in the object storage; a second judgment unit, used to judge whether the key value contains the unique host name of the target application server if the business name is located in the object storage; a first determination unit, used to determine that the target application server successfully seizes the lock resource if the key value contains the unique host name of the target application server; The judgment module also includes: a third determination unit, used to execute step a, after judging whether the key value contains the unique host name of the target application server, if the key value does not contain the unique host name of the target application server, the target application server fails to seize the lock resource; an update unit, used to execute step b, updating the key value in the upload object function to the unique host name of the first application server, wherein the first application server is any application server among the multiple application servers except the target application server; a second execution unit, used to execute step c, executing the step of calling the obtain object function in the object storage through the application program interface to obtain the updated key value in the obtain object function; a judgment unit, used to execute step d, judging whether the update key value contains the unique host name of the first application server; a processing unit, used to execute step e, if the update key value contains the host name of the first application server, the first application server successfully seizes the lock resource; otherwise, return to execute step b, step c, step d.

7. An electronic device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 5 are implemented.

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