CN113010326A - Distributed lock processing method and device, electronic equipment and storage medium - Google Patents

Distributed lock processing method and device, electronic equipment and storage medium Download PDF

Info

Publication number
CN113010326A
CN113010326A CN202110301625.1A CN202110301625A CN113010326A CN 113010326 A CN113010326 A CN 113010326A CN 202110301625 A CN202110301625 A CN 202110301625A CN 113010326 A CN113010326 A CN 113010326A
Authority
CN
China
Prior art keywords
lock
name
application server
key value
target application
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202110301625.1A
Other languages
Chinese (zh)
Inventor
郑钦锋
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Ping An Technology Shenzhen Co Ltd
Original Assignee
Ping An Technology Shenzhen Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Ping An Technology Shenzhen Co Ltd filed Critical Ping An Technology Shenzhen Co Ltd
Priority to CN202110301625.1A priority Critical patent/CN113010326A/en
Publication of CN113010326A publication Critical patent/CN113010326A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/52Program synchronisation; Mutual exclusion, e.g. by means of semaphores
    • G06F9/526Mutual exclusion algorithms
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/27Replication, distribution or synchronisation of data between databases or within a distributed database system; Distributed database system architectures therefor

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Databases & Information Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Software Systems (AREA)
  • Computing Systems (AREA)
  • Data Mining & Analysis (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The invention relates to big data, and provides a processing method and a device of a distributed lock, electronic equipment and a storage medium, wherein the processing method comprises the following steps: setting a key value pair in an uploading object function through an application program interface of object storage, wherein the key value pair comprises a key name and a key value, the key name is set as a lock name of a distributed lock, and the key value is set as a unique host name containing a target application server; judging whether the target application server successfully seizes lock resources or not according to the lock name and the unique host name of the target application server; and if the target application server successfully seizes the lock resource, operating the target service. The invention also relates to a block chain technology, and the shared data is stored in the block chain. The invention solves the technical problems that the existing distributed lock implementation mode in the prior art has complex operation, weak concurrency resistance, asynchronous data caused by hanging up of the master node and the like.

Description

Distributed lock processing method and device, electronic equipment and storage medium
Technical Field
The invention relates to big data processing, in particular to a distributed lock processing method and device, electronic equipment and a storage medium.
Background
In a single Java Virtual Machine (JVM), a Development Kit (jdk) provides an Application Program Interface (API) ready, while in a distributed application (multi JVM), a lock API provided by jdk cannot meet the requirement, and in order to enable the distributed storage system to operate correctly and effectively, concurrent access to the same shared resource by multiple clients must be controlled, so as to prevent certain uncertain behaviors from being uncontrolled, concurrent or simultaneously accessed to the shared resource, which leads to the destruction of the consistency of data and metadata, and the lock is a common means for achieving concurrent access control.
Currently, there are three general solutions for implementing distributed locks on the market: one is to realize the distributed lock function based on the database, and the other two are realized based on redis or zookeeper, and the three distributed lock realization schemes have respective advantages and disadvantages.
Firstly, the distributed lock is realized based on a database scheme, although the realization is simple, the performance is worried, the distributed lock is realized by establishing a unique index through a primary key or a field in a table, more indexes are required to be established, not only is a space of a disk occupied, but also the database deadlock is easy to occur in a high-concurrency scene; the use of the database also has a single point of failure, and if the database cluster is used, the problem of master-slave data synchronization exists. Secondly, a distributed lock is realized based on the redis, and the distributed lock is mainly based on the setnx command of the redis and the request received by the single thread of the redis, but the key is not easy to control when to delete; implementing a distributed lock using redis seems to be the most implementable, but it has a deadlock situation; although the frame redisson is adopted in the market for secondary packaging, the operation is simple, but the problem that a master node is hung off and a plurality of clients can acquire locks successfully at the same time under the condition that a sentinel mode or a master-slave mode cannot be solved. Finally, for realizing the distributed lock based on the zookeeper, the performance is good mainly based on the temporary sequence node characteristic and watch monitoring, but in order to avoid single-point failure, a cluster is required to be built, and the zookeeper environment is complex to build and consumes huge resource cost. In summary, the existing distributed lock implementation scheme has the technical problems of complex operation, weak concurrency resistance, asynchronous data caused by hanging up of a master node and the like.
The inventors have found that no effective solution has been found so far against the above-mentioned problems in the related art.
Disclosure of Invention
The invention provides a processing method and device of a distributed lock, electronic equipment and a storage medium, which at least solve the technical problems that the existing distributed lock implementation mode in the prior art is complex in operation, weak in concurrency resistance, asynchronous in data and the like caused by hanging up of a master node.
According to an aspect of the present invention, there is provided a method for processing a distributed lock, including: setting a key value pair in an uploading object function through an application program interface stored by an object, wherein the key value pair comprises a key name and a key value, the key name is set as a lock name of a distributed lock, the key value is set as a unique host name containing a target application server, the lock name is determined by a service name of a target service to be operated when a plurality of application servers access shared data concurrently, and the target application server is any one of the plurality of application servers; judging whether the target application server successfully seizes lock resources or not according to the lock name and the unique host name of the target application server; and if the target application server successfully seizes the lock resource, operating the target service.
Optionally, before determining whether the target application server successfully preempts the lock resource according to the lock name and the unique host name of the target application server, the method further includes: calling the uploading object function through the application program interface; and calling an acquisition object function in the object storage through the application program interface to acquire the key value pair.
Optionally, the storing the shared data in a block chain, and the determining, according to the lock name and the unique host name of the target application server, whether the target application server successfully preempts the lock resource includes: judging whether the service name corresponding to the lock name is located in the object storage; if the service name is located in the object storage, judging whether the key value contains the unique host name of the target application server; and if the key value contains the unique host name of the target application server, the target application server successfully seizes the lock resource.
Optionally, after the determining whether the service 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, determining that no application server occupies a lock resource, and resetting the key value pair in the uploading object function through the application program interface; and executing the step of judging whether the service name corresponding to the lock name is located in the object storage.
Optionally, after the 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 have the unique host name of the target application server, the target application server fails to seize lock resources; step b, updating a key value in the uploading object function to be a unique host name of a first application server, wherein the first application server is any one of the application servers except the target application server; step c, executing the step of calling the object obtaining function in the object storage through the application program interface to obtain the update key value in the object obtaining function; step d, judging whether the update key value contains the unique host name of the first application server; step e, if the host name of the first application server is contained in the updating key value, the first application server successfully seizes the lock resource; otherwise, returning to execute the step b, the step c and the step d.
Optionally, after operating the target service, the method further includes: and deleting the lock name in the object storage to release the lock resource.
Optionally, if the key value further includes an 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: updating the initial time to be the current time; monitoring a timestamp of the target application server for accessing shared data based on the initial time and the current time; comparing the timestamp with a preset time; and if the timestamp is greater than or equal to the preset time, deleting the lock name in the object storage to release the lock resource.
According to another aspect of the present invention, there is provided a processing apparatus for a distributed lock, including: the device comprises a setting module, a processing module and a processing module, wherein the setting module is used for setting a key value pair in an uploading object function through an application program interface stored in an object, the key value pair comprises a key name and a key value, the key name is set as a lock name of a distributed lock, the key value is set as a unique host name containing a target application server, the lock name is determined by a service name of a target service to be operated when a plurality of application servers access shared data concurrently, and the target application server is any one of the plurality of application servers; the judging module is used for judging whether the target application server successfully seizes lock resources according to the lock name and the unique host name of the target application server; and the operation module is used for operating the target service if the target application server successfully seizes the lock resource.
Optionally, the apparatus further comprises: the first calling module is used for calling the uploading object function through the application program interface before 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; and the second calling module is used for calling the object obtaining function in the object storage through the application program interface so as to obtain the key value pair.
Optionally, the shared data is stored in a block chain, and the determining module includes: the first judgment unit is used for judging whether the service name corresponding to the lock name is positioned in the object storage; a second determining unit, configured to determine whether the key value includes the unique host name of the target application server if the service name is located in the object store; and the first determining unit is used for determining that the target application server successfully preempts the lock resource if the key value contains the unique host name of the target application server.
Optionally, the determining module further includes: a second determining unit, configured to determine that no application server occupies a lock resource if the key name is not located in the object storage after the determining whether the service name corresponding to the lock name is located in the object storage, and reset a key value pair in the upload object function through the application program interface; and the first execution unit is used for executing the step of judging whether the service name corresponding to the lock name is positioned in the object storage.
Optionally, the determining module further includes: a third determining unit, configured to execute step a, after determining whether the key value includes the unique host name of the target application server, if the key value does not include the unique host name of the target application server, then the target application server fails to seize the lock resource; an updating unit, configured to execute step b, update a key value in the upload object function as a unique host name of a first application server, where the first application server is any one of the application servers except the target application server; a second execution unit, configured to execute step c, where the step of calling the get object function in the object storage through the application program interface is executed to obtain an update key value in the get object function; a judging unit, configured to execute step d, and judge whether the update key value contains the unique host name of the first application server; a processing unit, configured to execute step e, where if the update key value contains the hostname of the first application server, the first application server successfully seizes the lock resource; otherwise, returning to execute the step b, the step c and the step d.
Optionally, the apparatus further comprises: and the first deleting module is used for deleting the lock name in the object storage after the target service is operated so as to release the lock resource.
Optionally, if the key value further includes an initial time when the key value is set to contain the unique host name of the target application server, the apparatus further includes: the updating module is used for updating the initial time to be the current time before the target service is operated successfully; a monitoring module for monitoring a timestamp of the target application server accessing shared data based on the initial time and the current time; the comparison module is used for comparing the timestamp with preset time; and the second deleting module is used for deleting the lock name in the object storage to release the lock resource if the timestamp is greater than or equal to the preset time.
According to yet another aspect of the present invention, there is also provided an electronic device comprising a memory having a computer program stored therein and a processor arranged to run the computer program to perform the steps of any of the above method embodiments.
According to a further aspect of the present invention, there is also provided a storage medium having a computer program stored therein, wherein the computer program is arranged to perform the steps in any of the apparatus embodiments described above when executed.
According to the method, an application program interface of object storage is adopted to set the key name in the uploaded object function as the lock name of the distributed lock (determined by the service name of the service to be operated), and the key value corresponding to the key name is set as the unique host name containing the target application server; then, judging whether the target application server successfully seizes lock resources according to the lock name and the unique host name of the target application server; if the preemption is successful, the target service is authorized to be operated (namely, the shared data is authorized to be accessed), the distributed lock is realized by using the object storage, a component of a third party is not required to be used as a tool of the distributed lock, and the operation is simple; the decentralized implementation method has the advantages that the concurrency resistance of the distributed lock implementation is improved, the condition that data are not synchronous due to the fact that the main node is hung down is avoided, the technical problems that an existing distributed lock implementation mode in the related art is complex in operation, weak in concurrency resistance, and data are not synchronous due to the fact that the main node is hung down are solved, and the performance of the distributed lock implementation is improved.
Drawings
The accompanying drawings, which are included to provide a further understanding of the invention and are incorporated in and constitute a part of this application, illustrate embodiment(s) of the invention and together with the description serve to explain the invention without limiting the invention. In the drawings:
fig. 1 is a block diagram of a hardware structure in which a distributed lock processing method according to an embodiment of the present invention is applied to a computer terminal;
FIG. 2 is a flow diagram of a method of processing a distributed lock according to an embodiment of the invention;
FIG. 3 is a flow diagram of an implementation of a distributed lock provided in accordance with an embodiment of the present invention;
FIG. 4 is a block diagram of a distributed lock processing apparatus according to an embodiment of the present invention;
fig. 5 is a schematic structural diagram of an electronic device according to an embodiment of the present invention.
Detailed Description
The invention will be described in detail hereinafter with reference to the accompanying drawings in conjunction with embodiments. It should be noted that the embodiments and features of the embodiments in the present application may be combined with each other without conflict.
It should be noted that the terms "first," "second," and the like in the description and claims of the present invention and in the drawings described above are used for distinguishing between similar elements and not necessarily for describing a particular sequential or chronological order.
The method provided by the first embodiment of the present application may be executed in a mobile terminal, a server, a computer terminal, or a similar computing device. Taking the example of running on a computer terminal, fig. 1 is a hardware structure block diagram of a distributed lock processing method applied to a computer terminal according to an embodiment of the present invention. As shown in fig. 1, the computer terminal may include one or more (only one shown in fig. 1) processors 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, and optionally, a transmission device 106 for communication functions and an input-output device 108. It will be understood by those skilled in the art that the structure shown in fig. 1 is only an illustration and is not intended to limit the structure of the computer terminal. For example, the computer terminal may also include more or fewer components than shown in FIG. 1, or have a different configuration than shown in FIG. 1.
The memory 104 may be used to store computer programs, for example, software programs and modules of application software, such as computer programs corresponding to the distributed lock processing method in the embodiment of the present invention, and the processor 102 executes various functional applications and data processing by running the computer programs stored in the memory 104, so as to implement the above-mentioned method. The memory 104 may include high speed random access memory, and may also include non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory, and may also include volatile memory. In some examples, the memory 104 may further include memory located remotely from the processor 102, which may be connected to a computer terminal over a network. Examples of such networks include, but are not limited to, the internet, intranets, local area networks, mobile communication networks, and combinations thereof.
The transmission device 106 is used for receiving or transmitting data via a network. Specific examples of the network described above may include a wireless network provided by a communication provider of the computer terminal. In one example, the transmission device 106 includes a Network adapter (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 may be a Radio Frequency (RF) module, which is used for communicating with the internet in a wireless manner.
In order to solve the problems in the related art, the scheme provides a method for processing the distributed lock by using the object storage, if the object storage exists in the project and the distributed lock usage scene exists, the object storage can be directly used for realizing the distributed lock without building a third-party component; the distributed lock implementation method provided by the scheme can be applied to business scenes of ordering, killing second and the like.
Among these, object storage is object/file oriented, massive internet storage, which may also be referred to directly as "cloud storage". The object storage can be divided into the following types according to the data storage types: the system comprises a file storage (file system), a block storage (hard disk) and an object storage (key value database), wherein the object storage corresponds to PB-level data, the PB-level data is orderly stored by taking a container (bucket) as a unit code, various object data are filled in the PB-level data, and goods (data) sent by hundreds of thousands of clients can be processed and clearly remembered according to key values.
Although an object is a file, it is an encapsulated file (objects in programming have the characteristic of encapsulation), that is, in an object storage system, you cannot directly open/modify the file, but can upload the file, download the file, and the like ftp. In addition, the object storage does not have a file structure with a plurality of levels like a file system, but only has the concept of a 'bucket' (namely storage space), all objects in the 'bucket' are objects, and the storage mode is very flat. The method has the biggest characteristic that the name of an object is a domain name address, and once the object is set to be public, all netizens can access the object; its owner may also access objects therein by way of the REST API. Therefore, the object stores the most mainstream usage scenario, that is, stores static content (videos, pictures, files, software installation packages, etc.) of internet/mobile internet applications such as websites, mobile apps, etc.
Fig. 2 is a flowchart of a processing method of a distributed lock according to an embodiment of the present invention, and as shown in fig. 2, the flowchart includes the following steps:
step S202, setting a key value pair in an uploading object function through an application program interface stored in an object, wherein the key value pair comprises a key name and a key value, the key name is set as a lock name of a distributed lock, the key value is set as a unique host name containing a target application server, the lock name is determined by a service name of a target service to be operated when a plurality of application servers access shared data concurrently, and the target application server is any one of the application servers;
a distributed lock is a way to control synchronous access to a shared resource (i.e. the above-mentioned shared data) between distributed systems, and aims to ensure that when multiple threads are concurrent, only one thread (each thread corresponds to one application server) operates this service, method or variable (i.e. the above-mentioned target service) at the same time, thereby accessing the shared resource.
In this embodiment, the key to implement the distributed lock is to set up a storage server (i.e., the object storage) outside the distributed application server to store lock information (i.e., lock resources); then, the distributed lock is realized by calling an Application Programming Interface (API) of the object storage, the Interface of the object storage conforms to the restful Interface specification, the calling is simple, a component of a third party is not required to be used as a tool of the distributed lock, and the cost is reduced; decentralization does not exist in a framework such as zookeeper, redis, database and the like, so that the problem of data asynchronization caused by hanging of the main node is solved.
In this embodiment, the Object store may be an OBS Object store (all referred to as Object-based store) or an OSS Object store Service (all referred to as Object store Service), but is not limited thereto.
This embodiment is implemented by using a property that putObject (key, value) content can cover.
Preferably, Java provides an API, and the unique hostname and the current system time of the application server can be obtained by directly calling the API of the object store; the key of the putObject is designed as the lock name of the distributed lock (the key is the unique identification of the lock and is named according to business decision), and the value is designed as the host name of the application server (namely the unique host name).
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;
optionally, a bucket is used to store the lock resource. And determining whether the current application server (namely the target application server) has the authority of executing the target service (whether the lock of the resource is successfully seized) or not, namely the authority of accessing the shared data according to the service name and the unique host name.
Step S206, if the target application server successfully seizes the lock resource, the target service is operated.
It is emphasized that, to further ensure the privacy and security of the shared data, the shared data may also be stored in a node of a block chain.
The processing method of the distributed lock provided by the embodiment of the invention adopts an application program interface stored by an object to set the key name in an uploaded object function as the lock name of the distributed lock, and sets the key value corresponding to the key name as the unique host name containing a target application server; then, whether the target application server successfully seizes lock resources is judged according to the lock name and the unique host name of the target application server, if the lock resources are successfully seized, the target service is authorized to be operated, the distributed lock is realized by using object storage, a component of a third party is not needed to be used as a tool of the distributed lock, and the operation is simple; the decentralized implementation method has the advantages that the concurrency resistance of the distributed lock implementation is improved, the condition that data are not synchronous due to the fact that the main node is hung down is avoided, the technical problems that an existing distributed lock implementation mode in the related art is complex in operation, weak in concurrency resistance, and data are not synchronous due to the fact that the main node is hung down are solved, and the performance of the distributed lock implementation is improved.
In an optional embodiment of the present disclosure, before determining whether the target application server successfully preempts the lock resource according to the lock name and the unique hostname of the target application server, the method further includes: calling an uploading object function through an application program interface; and calling an acquisition object function in the object storage through the application program interface to acquire the key value pair.
In an optional embodiment of the present disclosure, the storing the shared data in the blockchain, and determining whether the target application server successfully preempts the lock resource according to the lock name and the unique hostname of the target application server includes: judging whether the service name corresponding to the lock name is in the object storage; if the service name is located in the object storage, judging whether the key value contains the unique host name of the target application server; and if the key value contains the unique host name of the target application server, the target application server successfully seizes the lock resource.
The block chain is a novel application mode of computer technologies such as distributed data storage, point-to-point transmission, a consensus mechanism, an encryption algorithm and the like. A block chain (Blockchain), which is essentially a decentralized database, is a series of data blocks associated by using a cryptographic method, and each data block contains information of a batch of network transactions, so as to verify the validity (anti-counterfeiting) of the information and generate a next block. The blockchain may include a blockchain underlying platform, a platform product service layer, an application service layer, and the like.
Optionally, after the target service is operated, the method further includes: the lock name in the object store is deleted to release the lock resource.
In an alternative example, fig. 3 is a flowchart of a process of providing a distributed lock according to a specific embodiment of the present invention, which is shown in fig. 3 and includes the following steps:
step S302, after setting the upload object putObject (key, value) as the lock name of the distributed lock and the unique host name of the current application server (i.e. the above target application server), executing the putObject operation (i.e. calling the upload object function); then, the get object getObject function (i.e. the get object function) is called to get the key-value (and the key-value pair) in the getObject interface request through the API.
Step S304, determining whether the key in the getObject interface request is in the object storage, if so, (identifying that the getObject (key) is correct), further executing step S306, obtaining getObject (value), and determining whether the host name in the value is the same as the host name of the server where the application is located (i.e. the unique host name of the target application server);
step S308, if the data are the same, the current application server (namely the target application server) successfully acquires the lock, namely has the authority of executing the shared data, and then the target service is operated;
step S310, after the operation target service is successful, deleting the key (lock name) in the object storage OBS to release the lock resource.
Optionally, after determining whether the service 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, determining that no application server occupies the lock resource, and resetting the key value pair in the uploaded object function through the application program interface; and executing the step of judging whether the service name corresponding to the lock name is positioned in the object storage.
As shown in fig. 3, when the getobject (key) method is called, if the key does not exist in the OBS, an error is reported, which indicates that the lock snatching fails, that is, no process (application server) preempts the lock resource currently; then, step S302 is executed to call the putObject function (i.e. the above-mentioned upload object function), redesign the key-value pair, and after success, step S304 is executed.
In another embodiment of the present disclosure, after determining whether the key value includes the unique host name of the target application server, the method further includes: step a, if the key value does not have the unique host name of the target application server, the target application server fails to seize the lock resource; step b, updating a key value in the uploading object function to be the unique host name of a first application server, wherein the first application server is any one of the application servers except the target application server; step c, executing the step of calling the acquisition object function in the object storage through the application program interface to acquire the update key value in the acquisition object function; step d, judging whether the update key value contains the unique host name of the first application server; step e, if the host name of the first application server is contained in the updating key value, the first application server successfully seizes the lock resource; otherwise, returning to execute the step b, the step c and the step d.
As shown in fig. 3, if the getobject (key) method is called and the key exists in the OBS, a value is obtained, and whether a host name before the value colon (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 is determined; if the current process is the same as the current application server, the lock of the current process (the current process corresponds to the current application server, namely, the target application server) is successfully robbed, the operation of the target service (namely, the shared data is accessed) can be executed, and after the operation is finished, the key (the lock is released) in the storage object is deleted; if not, the lock acquisition is failed, the server where the application is located does not have the right to execute the target service, then the key value in the uploading object function is updated to any application server except the current application server (namely, the first application server for uploading data) in the plurality of application servers, the step S304 is returned to, the getObject method is executed, and the step S306 is continued to judge whether the value (namely, the updating key value) is the host name of the application server corresponding to other processes (namely, the unique host name of the first application server), wherein the application servers and the processes are in a one-to-one mapping relationship.
According to an example of the above embodiment, taking the following order as an example, when a customer orders, the inventory center is generally called to reduce inventory, specifically: updating store inventory information; and by setting the modification mode of the inventory, the database is very easy to lose and update when the concurrency is high. Assuming A, B that the current two transactions are, the queried inventory is 5, a has 3 sheets, the inventory needs to be modified to 2, B has 1 sheet, the inventory needs to be modified to 4, if a and B access the inventory concurrently, the execution result of the a transaction covers the execution result of the B transaction, or the execution result of the B transaction covers the execution result of the a transaction, which is very confusing and unpredictable.
With this embodiment, multiple transactions (such as the A, B transaction) access the inventory (shared data) to perform inventory reduction (i.e., the operation target service), and the inventory reduction operation is performed after a single transaction seizes a lock on the resource. For example, A performs inventory reduction after the lock resource is preempted, while B performs inventory reduction only after A releases the lock resource, so as to prevent multiple transactions from modifying the inventory concurrently, thereby avoiding data inaccuracy.
Optionally, if the key value further includes an initial time that 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: updating the initial time to be the current time; monitoring a 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; and if the time stamp is greater than or equal to the preset time, deleting the lock name in the object storage to release the lock resource.
In a preferred embodiment of the present disclosure, a string concatenation manner is adopted to design the value into a format of a host name of the application server (i.e., the unique host name) + colon + current system time, i.e., the host name of the application server: the current operating system time. Each client (client, corresponding to the application server one by one) needs to start a timing job (the ratio is 30 seconds, namely the preset time), namely the expiration time, to monitor the bucket of the storage lock resource, circularly detect the value corresponding to each key below the bucket, namely the time value in the value, and compare the value with the current time, if the difference value (namely the timestamp) is greater than the preset time (for example 30 seconds), the key in the object storage is prevented from being deadlocked by setting the expiration time.
Because the value corresponding to each key in the bucket is the time for preempting the lock, the lock resource can be released after the process normally preempting the lock executes the shared data operation, and if the current process is hung, the current process is easy to hold the lock resource all the time, so that the lock preemption of other processes is influenced; by the embodiment, the time in the value corresponding to each key below the bucket is monitored, and if the preset time (for example, 30 seconds) for preempting the lock resource is exceeded, the key in the object storage is deleted, so that the lock resource can be preempted by other processes.
Through the embodiment of the invention, compared with other existing distributed lock implementation schemes (such as zookeeper, redis, database and the like), the implementation of the distributed lock by using the object storage OBS has the following beneficial effects: 1) the operation is simple, and the API in the object storage can be simply called to realize the operation; 2) decentralization is not achieved, and data asynchronism can be caused once a main node is hung up unlike the situation that a third-party component is used for achieving distributed locks; 3) the OBS gateway can also be dynamically expanded, so that the concurrency of accessing shared data can be improved; 4) the object storage OBS interface complies with restful (wherein, the English of rest is all called representational state transfer, namely, representational state transition, and Chinese corresponding to the restful is of rest formula), and is simple to call; 5) the OBS bottom layer data adopts a distributed storage mechanism, even if network failure or disk damage occurs under extreme conditions, the client application can also carry out data interaction with the OBS, and the condition that the client is unavailable can not occur.
Based on the processing method of the distributed lock provided in the foregoing embodiments, and based on the same inventive concept, the present embodiment further provides a processing apparatus of the distributed lock, where the apparatus is used to implement the foregoing embodiments and preferred embodiments, and the description of the apparatus has been already made, and is not repeated. As used below, the term "module" may be a combination of software and/or hardware that implements a predetermined function. Although the means described in the embodiments below are preferably implemented in software, an implementation in hardware, or a combination of software and hardware is also possible and contemplated.
Fig. 4 is a block diagram of a processing apparatus of a distributed lock according to an embodiment of the present invention, and as shown in fig. 4, the apparatus includes: the setting module 40 is configured to set a key value pair in an upload object function through an application program interface stored in an object, where the key value pair includes a key name and a key value, the key name is set as a lock name of a distributed lock, the key value is set as a unique host name including a target application server, the lock name is determined by a service name of a target service to be operated when multiple application servers concurrently access shared data, and the target application server is any one of the multiple application servers; a judging module 42, connected to the setting module 40, 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; and an operation module 44, connected to the determining module 42, configured to operate the target service if the target application server successfully seizes the lock resource.
It is emphasized that, to further ensure the privacy and security of the shared data, the shared data may also be stored in a node of a block chain.
Optionally, the apparatus further comprises: the first calling module is used for calling the uploading object function through the application program interface before 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; and the second calling module is used for calling the object obtaining function in the object storage through the application program interface so as to obtain the key value pair.
Optionally, the shared data is stored in a block chain, and the determining module 42 includes: the first judgment unit is used for judging whether the service name corresponding to the lock name is positioned in the object storage; the second judgment unit is used for judging whether the key value contains the unique host name of the target application server or not if the business name is positioned in the object storage; and the first determining unit is used for successfully preempting the lock resource by the target application server if the key value contains the unique host name of the target application server.
Optionally, the determining module 42 further includes: a second determining unit, configured to determine that no application server occupies the lock resource and reset the key value pair in the upload object function through the application program interface after determining whether the service name corresponding to the lock name is located in the object storage and if the key name is not located in the object storage; and the first execution unit is used for executing the step of judging whether the service name corresponding to the lock name is positioned in the object storage.
Optionally, the determining module 42 further includes: a third determining unit, configured 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; the updating unit is used for executing the step b, and updating the key value in the uploading object function to be the unique host name of the first application server, wherein the first application server is any one of the plurality of application servers except the target application server; a second execution unit, configured to execute step c, where the step c is executed to call an object obtaining function in the object storage through an application program interface, so as to obtain an update key value in the object obtaining function; a judging unit, configured to execute step d, and judge whether the update key value contains the unique host name of the first application server; the processing unit is used for executing the step e, and if the host name of the first application server is contained in the updating key value, the first application server successfully seizes the lock resource; otherwise, returning to execute the step b, the step c and the step d.
Optionally, the apparatus further comprises: and the first deleting module is used for deleting the lock name in the object storage after the target service is operated so as to release the lock resource.
Optionally, if the key value further includes an initial time when the key value is set to contain the unique host name of the target application server, the apparatus further includes: the updating module is used for updating the initial time to be the current time before the operation target service is successful; the monitoring module is used for monitoring a timestamp of the target application server for accessing the shared data based on the initial time and the current time; the comparison module is used for comparing the timestamp with preset time; and the second deleting module is used for deleting the lock name in the object storage to release the lock resource if the timestamp is greater than or equal to the preset time.
It should be noted that, the above modules may be implemented by software or hardware, and for the latter, the following may be implemented, but not limited to: the modules are all positioned in the same processor; alternatively, the modules are respectively located in different processors in any combination.
Embodiments of the present invention also provide a storage medium having a computer program stored therein, wherein the computer program is arranged to perform the steps of any of the above method embodiments when executed.
Alternatively, in the present embodiment, the storage medium may be configured to store a computer program for executing the steps of:
s1, setting a key value pair in an uploading object function through an application program interface stored by an object, wherein the key value pair comprises a key name and a key value, the key name is set as a lock name of a distributed lock, the key value is set as a unique host name containing a target application server, the lock name is determined by a service name of a target service to be operated when a plurality of application servers access shared data concurrently, and the target application server is any one of the application servers;
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;
and S3, if the target application server successfully seizes the lock resource, operating the target service.
Optionally, in this embodiment, the storage medium may include, but is not limited to: various media capable of storing computer programs, such as a usb disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a removable hard disk, a magnetic disk, or an optical disk.
It is emphasized that, to further ensure the privacy and security of the shared data, the shared data may also be stored in a node of a block chain.
Based on the above embodiments of the method shown in fig. 2 and the apparatus shown in fig. 4, in order to achieve the above object, an electronic device is further provided in the embodiments of the present application, as shown in fig. 5, including a memory 52 and a processor 51, where the memory 52 and the processor 51 are both disposed on a bus 53, and the memory 52 stores a computer program, and the processor 51 implements the processing method of the distributed lock shown in fig. 2 when executing the computer program.
Based on such understanding, the technical solution of the present application may be embodied in the form of a software product, which may be stored in a memory (which may be a CD-ROM, a usb disk, a removable hard disk, etc.), and includes several instructions to enable an electronic device (which may be a personal computer, a server, or a network device, etc.) to execute the method described in the implementation scenarios of the present application.
Optionally, the device may also be connected to a user interface, a network interface, a camera, Radio Frequency (RF) circuitry, sensors, audio circuitry, a WI-FI module, and so forth. The user interface may include a Display screen (Display), an input unit such as a keypad (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 (e.g., a bluetooth interface, WI-FI interface), etc.
It will be understood by those skilled in the art that the structure of an electronic device provided in the present embodiment does not constitute a limitation of the physical device, and may include more or less components, or some components in combination, or a different arrangement of components.
Optionally, the specific examples in this embodiment may refer to the examples described in the above embodiments and optional implementation manners, and this embodiment is not described herein again.
It will be apparent to those skilled in the art that the modules or steps of the present invention described above may be implemented by a general purpose computing device, they may be centralized on a single computing device or distributed across a network of multiple computing devices, and alternatively, they may be implemented by program code executable by a computing device, such that they may be stored in a storage device and executed by a computing device, and in some cases, the steps shown or described may be performed in an order different than that described herein, or they may be separately fabricated into individual integrated circuit modules, or multiple ones of them may be fabricated into a single integrated circuit module. Thus, the present invention is not limited to any specific combination of hardware and software.
The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention, and various modifications and changes may be made by those skilled in the art. Any modification, equivalent replacement, or improvement made within the principle of the present invention should be included in the protection scope of the present invention.

Claims (10)

1. A method for processing a distributed lock, comprising:
setting a key value pair in an uploading object function through an application program interface stored by an object, wherein the key value pair comprises a key name and a key value, the key name is set as a lock name of a distributed lock, the key value is set as a unique host name containing a target application server, the lock name is determined by a service name of a target service to be operated when a plurality of application servers access shared data concurrently, and the target application server is any one of the plurality of application servers;
judging whether the target application server successfully seizes lock resources or not according to the lock name and the unique host name of the target application server;
and if the target application server successfully seizes the lock resource, operating the target service.
2. The method of claim 1, wherein prior to determining whether the target application server successfully preempts the lock resource based on the lock name and the unique hostname of the target application server, the method further comprises:
calling the uploading object function through the application program interface;
and calling an acquisition object function in the object storage through the application program interface to acquire the key value pair.
3. The method of claim 1, wherein the shared data is stored in a blockchain, and wherein determining whether the target application server successfully preempts the lock resource based on the lock name and a unique hostname of the target application server comprises:
judging whether the service name corresponding to the lock name is located in the object storage;
if the service name is located in the object storage, judging whether the key value contains the unique host name of the target application server;
and if the key value contains the unique host name of the target application server, the target application server successfully seizes the lock resource.
4. The method according to claim 3, wherein after the determining whether the service name corresponding to the lock name is located in the object store, further comprising:
if the key name is not located in the object storage, determining that no application server occupies a lock resource, and resetting the key value pair in the uploading object function through the application program interface; and executing the step of judging whether the service name corresponding to the lock name is located in the object storage.
5. The method of claim 3, further comprising, after the determining whether the key value contains a unique host name of the target application server:
step a, if the key value does not have the unique host name of the target application server, the target application server fails to seize lock resources;
step b, updating a key value in the uploading object function to be a unique host name of a first application server, wherein the first application server is any one of the application servers except the target application server;
step c, executing the step of calling the object obtaining function in the object storage through the application program interface to obtain the update key value in the object obtaining function;
step d, judging whether the update key value contains the unique host name of the first application server;
step e, if the host name of the first application server is contained in the updating key value, the first application server successfully seizes the lock resource; otherwise, returning to execute the step b, the step c and the step d.
6. The method of claim 1, wherein after operating the target service, the method further comprises:
and deleting the lock name in the object storage to release the lock resource.
7. The method of claim 1, wherein if the key value further comprises an 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 comprises:
updating the initial time to be the current time;
monitoring a timestamp of the target application server for accessing shared data based on the initial time and the current time;
comparing the timestamp with a preset time;
and if the timestamp is greater than or equal to the preset time, deleting the lock name in the object storage to release the lock resource.
8. A distributed lock handling apparatus, comprising:
the device comprises a setting module, a processing module and a processing module, wherein the setting module is used for setting a key value pair in an uploading object function through an application program interface stored in an object, the key value pair comprises a key name and a key value, the key name is set as a lock name of a distributed lock, the key value is set as a unique host name containing a target application server, the lock name is determined by a service name of a target service to be operated when a plurality of application servers access shared data concurrently, and the target application server is any one of the plurality of application servers;
the judging module is used for judging whether the target application server successfully seizes lock resources according to the lock name and the unique host name of the target application server;
and the operation module is used for operating the target service if the target application server successfully seizes the lock resource.
9. An electronic device comprising a memory and a processor, the memory storing a computer program, wherein the processor implements the steps of the method of any one of claims 1 to 7 when executing the computer program.
10. A computer storage medium on which a computer program is stored, characterized in that the computer program, when being executed by a processor, carries out the steps of the method of any one of claims 1 to 7.
CN202110301625.1A 2021-03-22 2021-03-22 Distributed lock processing method and device, electronic equipment and storage medium Pending CN113010326A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202110301625.1A CN113010326A (en) 2021-03-22 2021-03-22 Distributed lock processing method and device, electronic equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202110301625.1A CN113010326A (en) 2021-03-22 2021-03-22 Distributed lock processing method and device, electronic equipment and storage medium

Publications (1)

Publication Number Publication Date
CN113010326A true CN113010326A (en) 2021-06-22

Family

ID=76404120

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202110301625.1A Pending CN113010326A (en) 2021-03-22 2021-03-22 Distributed lock processing method and device, electronic equipment and storage medium

Country Status (1)

Country Link
CN (1) CN113010326A (en)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113761491A (en) * 2021-07-29 2021-12-07 苏州浪潮智能科技有限公司 Storage volume access authority management method, device, terminal and storage medium
CN115866039A (en) * 2022-11-29 2023-03-28 北京达佳互联信息技术有限公司 Message processing method and device, electronic equipment and storage medium

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113761491A (en) * 2021-07-29 2021-12-07 苏州浪潮智能科技有限公司 Storage volume access authority management method, device, terminal and storage medium
CN113761491B (en) * 2021-07-29 2023-07-21 苏州浪潮智能科技有限公司 Storage volume access authority management method, device, terminal and storage medium
CN115866039A (en) * 2022-11-29 2023-03-28 北京达佳互联信息技术有限公司 Message processing method and device, electronic equipment and storage medium

Similar Documents

Publication Publication Date Title
CN108885568B (en) System and computer-implemented method for processing multiple data items within a data source through an on-demand code execution environment
US20210004258A1 (en) Method and Apparatus for Creating Virtual Machine
US10255102B2 (en) Transaction control arrangement for device management system
US9632878B1 (en) Verification of database table partitions during backup
US9633051B1 (en) Backup of partitioned database tables
US11132213B1 (en) Dependency-based process of pre-existing data sets at an on demand code execution environment
CN109375933B (en) Method and equipment for upgrading client
CN113010326A (en) Distributed lock processing method and device, electronic equipment and storage medium
US11086685B1 (en) Deployment of virtual computing resources with repeatable configuration as a resource set
CN112860282B (en) Cluster plug-in upgrading method, device and server
CN110888858A (en) Database operation method and device, storage medium and electronic device
CN110197064B (en) Process processing method and device, storage medium and electronic device
US20230342183A1 (en) Management method and apparatus for container cluster
CN111490963A (en) Data processing method, system, equipment and storage medium based on QUIC protocol stack
CN110535776A (en) Gateway current-limiting method, device, gateway, system and storage medium
EP4177742A1 (en) Multitenancy management method and apparatus
CN106161517B (en) The method and apparatus for realizing cloud storage access by cloud file system
CN111143033A (en) Operation execution method and device based on scalable operating system
CN116150273A (en) Data processing method, device, computer equipment and storage medium
CN115994124A (en) File scanning method, device, electronic equipment and computer readable storage medium
CN114662102A (en) File processing method and device and storage medium
CN111371606A (en) Method for specifying monitor ip when using look to deploy ceph cluster
US9548940B2 (en) Master election among resource managers
US11687496B2 (en) Synchronization of distributed data files
CN112583877B (en) Resource information processing method and device, storage medium and electronic equipment

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination