Lock Identification Allocation and Positioning Method, Device, Equipment and Storage Medium

By setting up multiple first-level nodes under the ZooKeeper root node, combining hash modulus and lock identification number adjustment, the problem of disconnecting the Reaper thread from ZK is solved, and the precise allocation and positioning of lock identification is achieved to ensure the stability and response efficiency of ZK.

CN115048228BActive Publication Date: 2025-07-11BEIJING QIYI CENTURY SCI & TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202210532882.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-05-11
Publication Date
2025-07-11
Estimated Expiration
2042-05-11

AI Technical Summary

Technical Problem

The problem of the Reaper thread being disconnected from ZooKeeper (ZK) and being unable to delete the lock, resulting in the slow response of ZK and eventually dragging down the system.

Method used

By setting at least two first-level nodes under the root node, performing hash modulus operation according to the target lock identification and the number of first-level nodes, allocating the lock identification to the corresponding first-level nodes, and adjusting the number of first-level nodes according to the number of lock identifications, reducing the number of lock identifications under each first-level node, and avoiding the lock identification read by the Reaper thread exceeding the threshold.

Benefits of technology

Effectively avoid disconnecting the Reaper thread from ZK, ensure accurate positioning and timely deletion of lock marks, and maintain the stability and response efficiency of ZK.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115048228B_ABST
    Figure CN115048228B_ABST
Patent Text Reader

Abstract

The present application relates to a method, device, equipment and storage medium for lock identifier allocation and positioning, and relates to the technical field of cloud computing. The lock identifier allocation method includes: obtaining the number of first-level nodes, where the first-level nodes refer to the child nodes of the root node, and the number of first-level nodes is at least two; obtaining a target lock identifier; according to the target lock identifier and the number of first-level nodes, finding the target first-level node corresponding to the target lock identifier; and allocating the target lock identifier to the target first-level node. The present application is used to solve the problem that the Reaper thread is disconnected from ZK and the lock cannot be deleted.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of cloud computing technology, and in particular, to a method, apparatus, device, and storage medium for lock identifier allocation and positioning. Background Art

[0002] ZooKeeper (abbreviated as ZK) is a distributed application coordination service software and an open-source software project. It provides distributed configuration services, synchronization services, and naming registration for large-scale distributed computing. A distributed lock is a way to control synchronous access to shared resources between distributed systems. In Internet development, ZK is often used to implement distributed locks. Curator is a high-level encapsulation of ZK and an open-source ZK client framework, which solves many very low-level detailed development tasks of ZK clients. Compared with operating native ZK, it provides more convenient functions, and Curator is often directly used to operate distributed locks in development.

[0003] Working principle of acquiring a lock: There is a lock on ZK with the lock identifier "my_lock". Client A wants to acquire the lock "my_lock" and sends a lock acquisition request to ZK. A temporary sequential node a is created under the lock "my_lock". When querying all the child nodes under the lock "my_lock", if the temporary sequential node a is the first in the sorting, then client A successfully acquires the lock "my_lock".

[0004] Working principle of releasing a lock: After client A acquires the lock, it may process some code logic. After the processing is completed, the lock will be released. Releasing the lock means that client A deletes the temporary sequential node a created under the lock "my_lock". After client A releases the lock "my_lock", other clients may successfully acquire the lock "my_lock".

[0005] Method of deleting a lock: The lock "my_lock" is a persistent node, and ZK cannot actively delete it. External explicit calls to ZK are required for deletion. When using Curator to delete a lock, a Reaper thread needs to be created to clean up the lock. The mechanism of this Reaper thread is to first read the names of all lock parent nodes in ZK, and then read all lock identifiers under each lock parent node each time, and check whether there are any lock child nodes under each lock identifier. If there are no lock child nodes under a lock identifier, then the lock corresponding to this lock identifier can be deleted.

[0006] When the number of all lock identifiers under each lock parent node read by the Reaper thread each time exceeds the threshold of the Reaper thread, it will cause the Reaper thread to disconnect from ZK, unable to delete the lock, the number of each node in ZK accumulates more and more, the response of ZK becomes slower and slower, and finally the system is dragged down. Summary of the Invention

[0007] The present application provides a method, apparatus, device and storage medium for lock identifier allocation and positioning, to solve the problem that the Reaper thread is disconnected from ZK and the lock cannot be deleted.

[0008] In a first aspect, the present application provides a method for lock identifier allocation, including:

[0009] Obtain the number of first-level nodes, where the first-level nodes refer to the child nodes of the root node, and the number of the first-level nodes is at least two;

[0010] Obtain a target lock identifier;

[0011] According to the target lock identifier and the number of the first-level nodes, find the target first-level node corresponding to the target lock identifier;

[0012] Allocate the target lock identifier to the target first-level node.

[0013] Optionally, the step of finding the target first-level node corresponding to the target lock identifier according to the target lock identifier and the number of the first-level nodes includes:

[0014] Obtain the hash value corresponding to the target lock identifier;

[0015] Perform a modulo operation on the hash value according to the number of the first-level nodes to obtain a target serial number;

[0016] Obtain the target first-level node corresponding to the target serial number.

[0017] Optionally, the method further includes:

[0018] Obtain the number of lock identifiers under any one of the first-level nodes;

[0019] Adjust the number of the first-level nodes according to the number of the lock identifiers.

[0020] Optionally, the step of adjusting the number of the first-level nodes according to the number of the lock identifiers includes:

[0021] If the number of the lock identifiers is greater than a first threshold, increase the number of the first-level nodes;

[0022] If the number of the lock identifiers is less than a second threshold, decrease the number of the first-level nodes, where the second threshold is less than the first threshold.

[0023] Optionally, after adjusting the number of the first-level nodes according to the number of the lock identifiers, the method further includes:

[0024] Obtain the adjusted number of the first-level nodes;

[0025] Find the adjusted first-level node corresponding to the target lock identifier according to the target lock identifier and the adjusted quantity.

[0026] Reassign the target lock identifier to the adjusted first-level node.

[0027] In a second aspect, the present application provides a method for locating a lock identifier, including:

[0028] Determine whether the quantity of the first-level nodes is in an adjusted state, where the first-level nodes refer to the child nodes of the root node, and the quantity of the first-level nodes is at least two;

[0029] If the quantity of the first-level nodes is not in an adjusted state, obtain the adjusted quantity of the first-level nodes, obtain the target lock identifier, find the target first-level node corresponding to the target lock identifier according to the target lock identifier and the adjusted quantity of the first-level nodes, and search for the target lock identifier under the target first-level node to obtain or release the lock corresponding to the target lock identifier.

[0030] Optionally, after determining whether the quantity of the first-level nodes is in an adjusted state, the method further includes:

[0031] If the quantity of the first-level nodes is in an adjusted state, obtain the quantity of the first-level nodes before adjustment, the adjusted quantity of the first-level nodes, and the target lock identifier, find the first-level node before adjustment corresponding to the target lock identifier according to the target lock identifier and the quantity of the first-level nodes before adjustment. If the target lock identifier is found under the first-level node before adjustment, use the first-level node before adjustment as the target first-level node corresponding to the target lock identifier. If the target lock identifier is not found under the first-level node before adjustment, find the adjusted first-level node corresponding to the target lock identifier according to the target lock identifier and the adjusted quantity of the first-level nodes, use the adjusted first-level node as the target first-level node, and search for the target lock identifier under the target first-level node to obtain or release the lock corresponding to the target lock identifier.

[0032] Optionally, the method further includes:

[0033] If no lock identifier location request is received from the client within a preset duration, change the quantity of the first-level nodes from the adjusted state to the non-adjusted state.

[0034] In a third aspect, the present application provides a lock identifier allocation device, including:

[0035] A first acquisition module, configured to acquire the number of first-level nodes, where the first-level nodes refer to the child nodes of the root node, and the number of the first-level nodes is at least two;

[0036] A second acquisition module, configured to acquire a target lock identifier;

[0037] A first search module, configured to search for a target first-level node corresponding to the target lock identifier according to the target lock identifier and the number of the first-level nodes;

[0038] A first processing module, configured to allocate the target lock identifier to the target first-level node.

[0039] In a fourth aspect, the present application provides a lock identifier positioning device, including:

[0040] A second processing module, configured to determine whether the number of first-level nodes is in an adjustment state, where the first-level nodes refer to the child nodes of the root node, and the number of the first-level nodes is at least two;

[0041] A second search module, configured to, if the number of the first-level nodes is not in an adjustment state, acquire the adjusted number of the first-level nodes, acquire a target lock identifier, search for a target first-level node corresponding to the target lock identifier according to the target lock identifier and the adjusted number of the first-level nodes, and search for the target lock identifier under the target first-level node, so as to acquire or release the lock corresponding to the target lock identifier.

[0042] In a fifth aspect, the present application provides an electronic device, including: a processor, a memory, and a communication bus, where the processor and the memory complete communication with each other through the communication bus; the memory is configured to store a computer program; the processor is configured to execute the program stored in the memory to implement the lock identifier allocation method described in the first aspect, or to implement the lock identifier positioning method described in the second aspect.

[0043] In a sixth aspect, the present application provides a computer-readable storage medium, storing a computer program, where the computer program, when executed by a processor, implements the lock identifier allocation method described in the first aspect, or implements the lock identifier positioning method described in the second aspect.

[0044] The above technical solution provided by the embodiments of the present application has the following advantages compared with the prior art: In the embodiments of the present application, the number of first-level nodes is obtained, where the first-level nodes refer to the child nodes of the root node, and the number of first-level nodes is at least two. The target lock identifier is obtained, and according to the target lock identifier and the number of first-level nodes, the target first-level node corresponding to the target lock identifier is found, and the target lock identifier is assigned to the target first-level node. By setting first-level nodes under the root node, setting the number of first-level nodes to at least two, and according to the target lock identifier and the number of first-level nodes, the target lock identifier is assigned to the corresponding target first-level node, reducing the number of lock identifiers under each first-level node, thereby avoiding the situation where the number of lock identifiers under a first-level node read by the Reaper thread each time exceeds the threshold of the Reaper thread, resulting in the disconnection of the Reaper thread from ZK and the inability to delete the lock.

[0045] By determining whether the number of first-level nodes is in an adjustment state, when the number of first-level nodes is not in an adjustment state, according to the target lock identifier and the adjusted number of first-level nodes, the target first-level node corresponding to the target lock identifier is found, and then the target lock identifier is found under the target first-level node, so as to obtain or release the lock corresponding to the target lock identifier. When the number of first-level nodes is not in an adjustment state, according to the target lock identifier and the adjusted number of first-level nodes, the target first-level node corresponding to the target lock identifier can be accurately located, and then the target lock identifier can be accurately found under the target first-level node, enabling accurate positioning after the lock identifier is assigned, and then realizing the next operation of obtaining or releasing the lock corresponding to the target lock identifier. Description of the Drawings

[0046] The drawings here are incorporated into the specification and form a part of this specification, showing embodiments consistent with the present invention and used together with the specification to explain the principles of the present invention.

[0047] Figure 1 It is a schematic diagram of the distribution of each node in ZK in a certain situation in the prior art;

[0048] Figure 2 It is a schematic diagram of the distribution of each node in ZK in another situation in the prior art;

[0049] Figure 3 It is a schematic diagram of the distribution of each node in ZK in a specific embodiment of the present application;

[0050] Figure 4 It is a schematic flowchart of the method for lock identifier allocation in the embodiments of the present application;

[0051] Figure 5 It is a schematic diagram of the distribution of each node in ZK in a specific embodiment of the present application;

[0052] Figure 6 Schematic flowchart of the method for lock identifier positioning in the embodiments of the present application;

[0053] Figure 7 Schematic structural diagram of the lock identifier allocation device in the embodiments of the present application;

[0054] Figure 8 Schematic structural diagram of the lock identifier positioning device in the embodiments of the present application;

[0055] Figure 9 Schematic structural diagram of the electronic device in the embodiments of the present application. Specific embodiments

[0056] To make the objectives, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present application.

[0057] The term "and / or" in this article is merely a description of the association relationship of associated objects, indicating that there can be three relationships. For example, A and / or B can represent three situations: A exists alone, A and B exist simultaneously, and B exists alone. In addition, the character " / " in this article generally represents an "or" relationship between the front and rear associated objects.

[0058] The inventor analyzed the distribution of each node in ZK in the prior art and found that: as Figure 1 shown, it is a schematic diagram of the distribution of each node in ZK in one case of the prior art. Figure 1 In, " / " represents the root node in ZK, lock1 and lock2 represent different lock identifiers, and xxxxxxxxxx represents the child nodes of the lock, that is, the temporary sequential nodes. Figure 1 In, the root node " / " is the parent node of the locks lock1 and lock2. The number of root nodes is only 1, while the number of lock identifiers will continue to increase, ultimately causing the number of lock identifiers read by the Reaper thread under the root node each time to exceed the threshold of the Reaper thread, resulting in the disconnection of the Reaper thread from ZK and the inability to delete the lock.

[0059] As Figure 2 shown, it is a schematic diagram of the distribution of each node in ZK in another case of the prior art. Figure 2In it, " / " represents the root node in ZK, " / video" is a child node of the root node and also the parent node of the lock. The name of " / video" is the business system used to distinguish different business requirements, and each business system is independent of each other. "lock1" and "lock2" represent different lock identifiers, and "xxxxxxxxxx" represents the child node of the lock, that is, the ephemeral sequential node. In each business system, there is only one first-level node as the parent node of the lock. In this business system, when looking up the lock identifier, it can be directly found under the first-level node. However, in each business system, the number of first-level nodes is only 1, while the number of lock identifiers will continue to increase, eventually causing the number of lock identifiers read by the Reaper thread under the first-level node each time to exceed the threshold of the Reaper thread, resulting in the Reaper thread disconnecting from ZK and being unable to delete the lock.

[0060] Therefore, the inventor thought of setting the number of first-level nodes in each business system to at least two, and distributing the lock identifiers to the corresponding first-level nodes, thereby reducing the number of lock identifiers under each first-level node. As Figure 3 shown, it is a schematic diagram of the distribution of each node in ZK in a specific embodiment of the present application. Figure 3 In it, " / " represents the root node in ZK, " / video_1" is the first first-level node in the business system "video", " / video_2" is the second first-level node in the business system "video", "lock1" and "lock2" represent different lock identifiers, and "xxxxxxxxxx" represents the child node of the lock, that is, the ephemeral sequential node. The lock identifier "lock1" is assigned to the first-level node " / video_1", and the lock identifier "lock2" is assigned to the first-level node " / video_2", reducing the number of lock identifiers under each first-level node.

[0061] In the embodiment of the present application, a method for allocating lock identifiers is provided. This method can be applied to a server. Of course, it can also be applied to other electronic devices, such as terminals (mobile phones, tablets, etc.). In the embodiment of the present application, this method is described by taking the application to a server as an example.

[0062] In the embodiment of the present application, as Figure 4 shown, the method flow of lock identifier allocation mainly includes:

[0063] Step 401, obtain the number of first-level nodes, where the first-level node refers to the child node of the root node, and the number of first-level nodes is at least two.

[0064] Step 402, obtain the target lock identifier.

[0065] The lock identifier can uniquely determine the lock. The lock identifier is generally a string. For example, the target lock identifier is "lock1".

[0066] Step 403: Search for the target first-level node corresponding to the target lock identifier according to the target lock identifier and the number of first-level nodes.

[0067] In a specific embodiment, searching for the target first-level node corresponding to the target lock identifier according to the target lock identifier and the number of first-level nodes includes: obtaining the hash value corresponding to the target lock identifier; performing a modulo operation on the hash value according to the number of first-level nodes to obtain a target serial number; and obtaining the target first-level node corresponding to the target serial number.

[0068] For example: the number of first-level nodes is n = 4, and the identifiers of the first-level nodes are sys_name_1, sys_name_2, sys_name_3, and sys_name_4 respectively. Performing a modulo operation on the hash value corresponding to the target lock identifier according to the number of first-level nodes to obtain the target serial number means taking the remainder obtained by dividing the hash value by 4 as the target serial number. The remainder obtained by dividing the hash value by 4 is one of 0, 1, 2, 3. The target serial number 0 corresponds to the first-level node sys_name_1, the target serial number 1 corresponds to the first-level node sys_name_2, the target serial number 2 corresponds to the first-level node sys_name_3, and the target serial number 3 corresponds to the first-level node sys_name_4.

[0069] By using the hash algorithm to obtain the hash value corresponding to the target lock identifier, the hash value can be calculated through displacement, improving the processing speed.

[0070] Step 404: Assign the target lock identifier to the target first-level node.

[0071] In a specific embodiment, the lock identifier assignment method further includes: obtaining the number of lock identifiers under any first-level node; and adjusting the number of first-level nodes according to the number of lock identifiers.

[0072] The Reaper thread is used to clean up the locks. The mechanism of this Reaper thread is to first read the names of all first-level nodes in ZK, and then read all lock identifiers under each first-level node each time to check whether there are sub-nodes of the lock under each lock identifier. If there are no sub-nodes of the lock under the lock identifier, the lock corresponding to this lock identifier can be deleted.

[0073] When the Reaper thread deletes a lock, it will read all lock identifiers under each first-level node each time. When reading the lock identifiers under any first-level node, it will obtain the number of lock identifiers under this first-level node, and then adjust the number of first-level nodes according to the number of lock identifiers.

[0074] It is possible to adjust the number of first-level nodes in a timely manner according to the number of lock identifiers under any first-level node, effectively coping with the sudden increase or decrease in the number of lock identifiers and maintaining the stability of ZK.

[0075] In a specific embodiment, the number of first-level nodes is adjusted according to the number of lock identifiers, including: if the number of lock identifiers is greater than a first threshold, the number of first-level nodes is increased; if the number of lock identifiers is less than a second threshold, the number of first-level nodes is decreased, where the second threshold is less than the first threshold.

[0076] In a specific embodiment, the first threshold is factor*c, where factor is a preset scaling factor parameter, and c is the maximum number of lock identifiers allowed under each first-level node. The second threshold is (1 - factor)*c. For example, when factor = 0.8 and c = 10, the first threshold is factor*c = 0.8*10 = 8, and the second threshold is (1 - factor)*c = (1 - 0.8)*10 = 2. When the number of lock identifiers is greater than 8, the number of first-level nodes is increased to achieve capacity expansion, and the number of lock identifiers under each first-level node is reduced, which can effectively handle the sudden increase in the number of lock identifiers. When the number of lock identifiers is less than 2, the number of first-level nodes is reduced to achieve capacity reduction, and the number of lock identifiers under each first-level node is increased, which can effectively handle the sudden decrease in the number of lock identifiers.

[0077] If the number of lock identifiers is greater than the first threshold, the number of first-level nodes is increased to achieve capacity expansion, and the number of lock identifiers under each first-level node is reduced, which can effectively handle the sudden increase in the number of lock identifiers and prevent the number of lock identifiers under each first-level node from being too large, resulting in the number of lock identifiers read by the Reaper thread under each first-level node exceeding the threshold of the Reaper thread, causing the Reaper thread to disconnect from ZK and unable to delete the lock. If the number of lock identifiers is less than the second threshold, the number of first-level nodes is reduced to achieve capacity reduction, and the number of lock identifiers under each first-level node is increased, which can effectively handle the sudden decrease in the number of lock identifiers and timely reduce the number of first-level nodes to save storage space in ZK.

[0078] In a specific embodiment, after adjusting the number of first-level nodes according to the number of lock identifiers, the lock identifier allocation method further includes: obtaining the adjusted number of first-level nodes; finding the adjusted first-level node corresponding to the target lock identifier according to the target lock identifier and the adjusted number; and reallocating the target lock identifier to the adjusted first-level node.

[0079] After adjusting the number of first-level nodes according to the number of lock identifiers and reallocating the target lock identifier to the adjusted first-level node according to the target lock identifier and the adjusted number, the target lock identifier can change in a timely manner following the adjustment of the first-level node, avoiding the situation where the first-level node corresponding to the target lock identifier before adjustment cannot be found, resulting in the inability to find the target lock identifier.

[0080] In a specific embodiment, as Figure 5 shown, it is a schematic diagram of the distribution of each node in ZK.Figure 5 In it, / represents the root node in ZK, / sys_name_1 is the first first-level node in the business system sys_name, / sys_name_2 is the second first-level node in the business system sys_name, / sys_name_3 is the third first-level node in the business system sys_name, / sys_name_4 is the fourth first-level node in the business system sys_name, lock1, lock2, lock3, and lock4 represent different lock identifiers, and xxxxxxxxxx represents the child nodes of the lock, that is, ephemeral sequential nodes. The lock identifier lock1 is assigned to the first-level node / sys_name_1, the lock identifier lock2 is assigned to the first-level node / sys_name_2, the lock identifier lock3 is assigned to the first-level node / sys_name_3, and the lock identifier lock4 is assigned to the first-level node / sys_name_4, reducing the number of lock identifiers under each first-level node. The Reaper thread first reads the identifiers of each first-level node in ZK, that is, / sys_name_1, / sys_name_2, / sys_name_3, / sys_name_4, and then reads all the lock identifiers under one first-level node each time to check whether there are child nodes under the lock corresponding to each lock identifier. If there are no child nodes under the lock corresponding to the lock identifier, this lock can be deleted.

[0081] In an embodiment of the present application, a method for locating a lock identifier is provided. This method can be applied to a server. Of course, it can also be applied to other electronic devices, such as terminals (mobile phones, tablets, etc.). In an embodiment of the present application, this method is described by taking its application to a server as an example.

[0082] In an embodiment of the present application, as Figure 6 shown, the method flow for locating a lock identifier mainly includes:

[0083] Step 601, determine whether the number of first-level nodes is in an adjustment state. If not, execute step 602.

[0084] Among them, a first-level node refers to a child node of the root node, and the number of first-level nodes is at least two.

[0085] The number of first-level nodes not being in an adjustment state indicates that the expansion or contraction has ended. The number of first-level nodes being in an adjustment state indicates that the expansion or contraction has not ended.

[0086] Step 602, obtain the adjusted number of first-level nodes, obtain the target lock identifier, find the target first-level node corresponding to the target lock identifier according to the target lock identifier and the adjusted number of first-level nodes, and find the target lock identifier under the target first-level node to obtain or release the lock corresponding to the target lock identifier.

[0087] In a specific embodiment, to find a target first-level node corresponding to a target lock identifier according to the target lock identifier and the adjusted quantity of first-level nodes, the method includes: obtaining a hash value corresponding to the target lock identifier; performing a modulo operation on the hash value according to the adjusted quantity of first-level nodes to obtain a target serial number; and obtaining the target first-level node corresponding to the target serial number.

[0088] In a specific embodiment, the lock identifier positioning method further includes: if the quantity of first-level nodes is in an adjusted state, obtaining the quantity of first-level nodes before adjustment, the quantity of first-level nodes after adjustment, and the target lock identifier, finding the first-level node before adjustment corresponding to the target lock identifier according to the target lock identifier and the quantity of first-level nodes before adjustment, if the target lock identifier is found under the first-level node before adjustment, using the first-level node before adjustment as the target first-level node corresponding to the target lock identifier, if the target lock identifier is not found under the first-level node before adjustment, finding the first-level node after adjustment corresponding to the target lock identifier according to the target lock identifier and the quantity of first-level nodes after adjustment, using the first-level node after adjustment as the target first-level node, and finding the target lock identifier under the target first-level node, so as to acquire or release the lock corresponding to the target lock identifier.

[0089] According to the quantity of lock identifiers under the first-level nodes, the quantity of first-level nodes will be adjusted in time for expansion or contraction. Expansion means increasing the quantity of first-level nodes, and contraction means decreasing the quantity of first-level nodes. During the process of expansion or contraction, some lock identifiers may be allocated under the first-level nodes before adjustment, and some lock identifiers may be allocated under the first-level nodes after adjustment. Therefore, first find the first-level node before adjustment corresponding to the target lock identifier according to the target lock identifier and the quantity of first-level nodes before adjustment. If the target lock identifier is found under the first-level node before adjustment, use the first-level node before adjustment as the target first-level node to end the lock identifier positioning process. If the target lock identifier is not found under the first-level node before adjustment, it means that the target lock identifier has been allocated under the first-level nodes after adjustment, then find the first-level node after adjustment corresponding to the target lock identifier according to the target lock identifier and the quantity of first-level nodes after adjustment, and use the first-level node after adjustment as the target first-level node. By finding the target lock identifier under the first-level node before adjustment and under the first-level node after adjustment, it is ensured that the target lock identifier can be accurately found, and then it is convenient to perform the next operation of acquiring or releasing the lock corresponding to the target lock identifier.

[0090] In a specific embodiment, the lock identifier positioning method further includes: if no lock identifier positioning request sent by the client is received within a preset duration, changing the quantity of first-level nodes from the adjusted state to the non-adjusted state.

[0091] When the client needs to acquire or release a lock, it will send a lock identifier location request to ZK to find the target first-level node corresponding to the target lock identifier, and search for the target lock identifier under the target first-level node, so as to acquire or release the lock corresponding to the target lock identifier.

[0092] If the lock identifier location request sent by the client is not received within the preset duration, it means that the expansion or contraction has ended, and then the number of first-level nodes is changed from the adjusted state to the non-adjusted state.

[0093] In a specific embodiment, the number of first-level nodes not being in the adjusted state means that the number of first-level nodes before adjustment is empty. The number of first-level nodes before adjustment being empty means that the expansion or contraction has ended. The number of first-level nodes being in the adjusted state means that the number of first-level nodes before adjustment is not empty. The number of first-level nodes before adjustment not being empty means that the expansion or contraction has not ended yet. Changing the number of first-level nodes from the adjusted state to the non-adjusted state means clearing the number of first-level nodes before adjustment.

[0094] In summary, in the embodiment of the present application, the number of first-level nodes is obtained, where the first-level nodes refer to the child nodes of the root node, and the number of first-level nodes is at least two. The target lock identifier is obtained, and according to the target lock identifier and the number of first-level nodes, the target first-level node corresponding to the target lock identifier is found, and the target lock identifier is allocated to the target first-level node. The number of first-level nodes is obtained, where the first-level nodes refer to the parent nodes of the lock nodes, and the number of first-level nodes is at least two. The identifier of the lock node is obtained, and according to the identifier of the lock node and the number of first-level nodes, the target first-level node corresponding to the lock node is found, and the lock node is allocated to the target first-level node. By setting first-level nodes under the root node, setting the number of first-level nodes to at least two, and allocating the target lock identifier and the lock node to the corresponding target first-level node according to the target lock identifier, the identifier of the lock node and the number of first-level nodes, the number of lock identifiers and lock nodes under each first-level node is reduced, thereby avoiding the situation that the number of lock identifiers and lock nodes under a first-level node read by the Reaper thread each time exceeds the threshold of the Reaper thread, resulting in the disconnection between the Reaper thread and ZK and the inability to delete the lock node.

[0095] By determining whether the number of first-level nodes is in an adjustment state, when the number of first-level nodes is not in an adjustment state, according to the target lock identifier and the adjusted number of first-level nodes, the target first-level node corresponding to the target lock identifier is searched, and then the target lock identifier is searched under the target first-level node, so as to acquire or release the lock corresponding to the target lock identifier. When the number of first-level nodes is not in an adjustment state, according to the target lock identifier and the adjusted number of first-level nodes, searching for the target first-level node corresponding to the target lock identifier can accurately locate the target first-level node corresponding to the target lock identifier, and then the target lock identifier can be accurately found under the target first-level node, enabling precise positioning after the lock identifier is allocated, and then realizing the next operation of acquiring or releasing the lock corresponding to the target lock identifier.

[0096] Based on the same concept, an apparatus for allocating lock identifiers is provided in an embodiment of the present application. For the specific implementation of this apparatus, reference may be made to the description in the method embodiment section, and repeated parts will not be elaborated. As Figure 7 shown, the apparatus mainly includes:

[0097] A first acquisition module 701, configured to acquire the number of first-level nodes, where the first-level nodes refer to the child nodes of the root node, and the number of first-level nodes is at least two;

[0098] A second acquisition module 702, configured to acquire a target lock identifier;

[0099] A first search module 703, configured to search for a target first-level node corresponding to the target lock identifier according to the target lock identifier and the number of first-level nodes;

[0100] A first processing module 704, configured to allocate the target lock identifier to the target first-level node.

[0101] Based on the same concept, a lock identifier positioning apparatus is provided in an embodiment of the present application. For the specific implementation of this apparatus, reference may be made to the description in the method embodiment section, and repeated parts will not be elaborated. As Figure 8 shown, the apparatus mainly includes:

[0102] A second processing module 801, configured to determine whether the number of first-level nodes is in an adjustment state, where the first-level nodes refer to the child nodes of the root node, and the number of first-level nodes is at least two;

[0103] A second search module 802, configured to, if the number of first-level nodes is not in an adjustment state, acquire the adjusted number of first-level nodes, acquire a target lock identifier, search for a target first-level node corresponding to the target lock identifier according to the target lock identifier and the adjusted number of first-level nodes, and search for the target lock identifier under the target first-level node, so as to acquire or release the lock corresponding to the target lock identifier.

[0104] Based on the same concept, an embodiment of the present application further provides an electronic device, such as Figure 9 As shown, the electronic device mainly includes: a processor 901, a memory 902, and a communication bus 903. Among them, the processor 901 and the memory 902 complete mutual communication through the communication bus 903. Among them, a program executable by the processor 901 is stored in the memory 902, and the processor 901 executes the program stored in the memory 902 to implement the following steps:

[0105] Obtain the number of first-level nodes, where the first-level nodes refer to the child nodes of the root node, and the number of first-level nodes is at least two; obtain the target lock identifier; according to the target lock identifier and the number of first-level nodes, find the target first-level node corresponding to the target lock identifier; assign the target lock identifier to the target first-level node;

[0106] Or,

[0107] Judge whether the number of first-level nodes is in an adjustment state, where the first-level nodes refer to the child nodes of the root node, and the number of first-level nodes is at least two; if the number of first-level nodes is not in an adjustment state, obtain the adjusted number of first-level nodes, obtain the target lock identifier, according to the target lock identifier and the adjusted number of first-level nodes, find the target first-level node corresponding to the target lock identifier, and find the target lock identifier under the target first-level node to obtain or release the lock corresponding to the target lock identifier.

[0108] The communication bus 903 mentioned in the above electronic device may be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The communication bus 903 can be divided into an address bus, a data bus, a control bus, etc. For the sake of convenience of representation, Figure 9 only a thick line is used to represent it in, but it does not mean that there is only one bus or one type of bus.

[0109] The memory 902 may include a Random Access Memory (RAM), or may also include a non-volatile memory, such as at least one disk memory. Optionally, the memory may also be at least one storage device located far from the aforementioned processor 901.

[0110] The above-mentioned processor 901 may be a general-purpose processor, including a Central Processing Unit (CPU), a Network Processor (NP), etc., or may also be a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), a Field-Programmable Gate Array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components.

[0111] In another embodiment of the present application, a computer-readable storage medium is further provided. A computer program is stored in the computer-readable storage medium. When the computer program runs on a computer, the computer is enabled to execute the lock identifier allocation method or the lock identifier positioning method described in the above embodiments.

[0112] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware, or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer instructions are loaded and executed on a computer, the processes or functions described in the embodiments of the present application are generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions are transmitted from one website, computer, server, or data center to another website, computer, server, or data center by wire (such as coaxial cable, optical fiber, Digital Subscriber Line (DSL)) or wirelessly (such as infrared, microwave, etc.). The computer-readable storage medium may be any available medium that can be accessed by a computer, or a data storage device such as a server or data center that includes one or more integrated available media. The available medium may be a magnetic medium (such as a floppy disk, a hard disk, a magnetic tape, etc.), an optical medium (such as a DVD), or a semiconductor medium (such as a solid-state drive), etc.

[0113] It should be noted that in this text, relational terms such as "first" and "second" are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the term "comprising", "including" or any other variant thereof is intended to cover non-exclusive inclusion, such that a process, method, article or device comprising a series of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article or device. Without further limitation, an element defined by the statement "comprising an..." does not exclude the presence of additional identical elements in the process, method, article or device comprising the said element.

[0114] The above are only specific embodiments of the present invention, enabling those skilled in the art to understand or implement the present invention. Various modifications to these embodiments will be obvious to those skilled in the art, and the general principles defined herein can be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention will not be limited to these embodiments shown herein, but rather to the broadest scope consistent with the principles and novel features claimed herein.

Claims

1. A method for allocating lock identifiers, characterized in that Including: Obtain the number of first-level nodes, where the first-level nodes refer to the child nodes of the root node, and the number of first-level nodes in each business system is set to at least two; Obtain the target lock identifier; According to the target lock identifier and the number of first-level nodes, find the target first-level node corresponding to the target lock identifier; Allocate the target lock identifier to the target first-level node.

2. The lock identification allocation method according to claim 1, wherein The step of finding the target first-level node corresponding to the target lock identifier according to the target lock identifier and the number of first-level nodes includes: Obtain the hash value corresponding to the target lock identifier; Perform a modulo operation on the hash value according to the number of first-level nodes to obtain a target serial number; Obtain the target first-level node corresponding to the target serial number.

3. The lock identifier allocation method according to claim 2, wherein The method further includes: Obtain the number of lock identifiers under any of the first-level nodes; Adjust the number of first-level nodes according to the number of lock identifiers.

4. The lock identification allocation method according to claim 3, wherein, The step of adjusting the number of first-level nodes according to the number of lock identifiers includes: If the number of lock identifiers is greater than a first threshold, increase the number of first-level nodes; If the number of lock identifiers is less than a second threshold, decrease the number of first-level nodes, where the second threshold is less than the first threshold.

5. The lock identification allocation method according to claim 3, wherein After the step of adjusting the number of first-level nodes according to the number of lock identifiers, the method further includes: Obtain the adjusted number of first-level nodes; According to the target lock identifier and the adjusted number, find the adjusted first-level node corresponding to the target lock identifier; Re-allocate the target lock identifier to the adjusted first-level node.

6. A method for lock identification and positioning, characterized in that, Including: Judge whether the number of first-level nodes is in an adjustment state, where the first-level nodes refer to the child nodes of the root node, and the number of first-level nodes in each business system is set to at least two; If the number of first-level nodes is not in an adjustment state, obtain the adjusted number of first-level nodes, obtain the target lock identifier, according to the target lock identifier and the adjusted number of first-level nodes, find the target first-level node corresponding to the target lock identifier, and find the target lock identifier under the target first-level node, so as to obtain or release the lock corresponding to the target lock identifier.

7. The lock identification positioning method according to claim 6, characterized in that, After the step of judging whether the number of first-level nodes is in an adjustment state, the method further includes: If the number of the first-level nodes is in an adjustment state, obtain the number of the first-level nodes before adjustment, the number of the first-level nodes after adjustment, and the target lock identifier. According to the target lock identifier and the number of the first-level nodes before adjustment, search for the first-level nodes before adjustment corresponding to the target lock identifier. If the target lock identifier is found under the first-level nodes before adjustment, use the first-level nodes before adjustment as the target first-level nodes corresponding to the target lock identifier. If the target lock identifier is not found under the first-level nodes before adjustment, according to the target lock identifier and the number of the first-level nodes after adjustment, search for the first-level nodes after adjustment corresponding to the target lock identifier, use the first-level nodes after adjustment as the target first-level nodes, and search for the target lock identifier under the target first-level nodes, so as to acquire or release the lock corresponding to the target lock identifier.

8. The lock identification positioning method according to claim 6 or 7, characterized in that, The method further includes: If no lock identifier location request sent by the client is received within the preset duration, change the number of the first-level nodes from the adjustment state to the non-adjustment state.

9. A lock identification allocation device, characterized in that, It includes: A first acquisition module, configured to acquire the number of the first-level nodes, where the first-level nodes refer to the child nodes of the root node, and the number of the first-level nodes in each service system is set to at least two; A second acquisition module, configured to acquire the target lock identifier; A first search module, configured to search for the target first-level nodes corresponding to the target lock identifier according to the target lock identifier and the number of the first-level nodes; A first processing module, configured to allocate the target lock identifier to the target first-level nodes.

10. A lock identification positioning device, characterized in that, It includes: A second processing module, configured to determine whether the number of the first-level nodes is in an adjustment state, where the first-level nodes refer to the child nodes of the root node, and the number of the first-level nodes in each service system is set to at least two; A second search module, configured to if the number of the first-level nodes is not in an adjustment state, acquire the number of the first-level nodes after adjustment, acquire the target lock identifier, search for the target first-level nodes corresponding to the target lock identifier according to the target lock identifier and the number of the first-level nodes after adjustment, and search for the target lock identifier under the target first-level nodes, so as to acquire or release the lock corresponding to the target lock identifier.

11. An electronic device, comprising: A processor, a memory, and a communication bus, where the processor and the memory complete mutual communication through the communication bus; the memory is used to store a computer program; the processor is used to execute the program stored in the memory to implement the lock identifier allocation method according to any one of claims 1 to 5, or to implement the lock identifier location method according to any one of claims 6 to 8.

12. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by the processor, it implements the lock identifier allocation method according to any one of claims 1 to 5, or implements the lock identifier location method according to any one of claims 6 to 8.

Citation Information

Patent Citations

  • Method for loading distributed tasks based on distributed lock

    CN107203429A

  • An R tree index optimization method of a multi-granularity distributed read-write lock based on leaf nodes

    CN109582678A