A resource lock management method and device, electronic equipment and storage medium
By managing resource locks using hash tables and doubly linked lists, and dynamically creating and inserting lock nodes, the problems of lock conflicts and memory consumption caused by excessively large lock granularity are solved, achieving high concurrency and high throughput resource lock management.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- SANGFOR TECH INC
- Filing Date
- 2023-04-10
- Publication Date
- 2026-05-29
Smart Images

Figure CN116521943B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a resource lock management method, apparatus, electronic device, and storage medium. Background Technology
[0002] Resource locks are mechanisms used in computers to coordinate the concurrent execution of multiple threads. Related technologies include global locks and table-level locks in databases. Global locks can lock the entire database instance, while table-level locks can lock the entire table. The drawback of these technologies is that the lock granularity is too large, leading to a high probability of lock contention and low system concurrency. Summary of the Invention
[0003] To address the aforementioned problems, embodiments of the present invention provide a resource lock management method, apparatus, electronic device, and storage medium, to at least resolve the issues of excessively large lock granularity and low system concurrency in related technologies.
[0004] The technical solution of this invention is implemented as follows:
[0005] In a first aspect, embodiments of the present invention provide a resource lock management method, the method comprising:
[0006] Determine the hash value corresponding to the unique identifier of the resource to be locked;
[0007] Determine the first hash bucket corresponding to the hash value in the preset hash table; wherein, the preset hash table includes multiple hash buckets, the multiple hash buckets correspond to the same doubly linked list, and each hash bucket corresponds to a segment of the doubly linked list; each lock node on the doubly linked list represents a resource lock for a resource to be locked;
[0008] Based on the linked list range corresponding to the first hash bucket, the lock node of the resource to be locked is determined;
[0009] Obtain the resource lock of the resource to be locked from the lock node of the resource to be locked.
[0010] In the above scheme, determining the lock node of the resource to be locked based on the linked list interval corresponding to the first hash bucket includes:
[0011] Determine whether the linked list range corresponding to the first hash bucket includes the lock node of the resource to be locked;
[0012] If the linked list range corresponding to the first hash bucket does not include the lock node of the resource to be locked, then the lock node of the resource to be locked is created.
[0013] Insert the newly created lock node of the resource to be locked into the doubly linked list;
[0014] If the linked list interval corresponding to the first hash bucket includes the lock node of the resource to be locked, then the lock node of the resource to be locked is determined from the linked list interval corresponding to the first hash bucket.
[0015] In the above scheme, after determining the lock node of the resource to be locked from the linked list interval corresponding to the first hash bucket, the method further includes:
[0016] Get the current reference count of the resource lock for the resource to be locked in memory;
[0017] If the current reference count is the same as the expected original value, then the current reference count of the resource lock of the resource to be locked in memory is incremented by 1.
[0018] In the above scheme, inserting the newly created lock node of the resource to be locked into the doubly linked list includes:
[0019] Obtain the newly created lock node for the resource to be locked;
[0020] Determine the insertion position of the lock node for the resource to be locked;
[0021] Insert the lock node of the resource to be locked into the insertion position;
[0022] Update the index of the hash bucket corresponding to the lock node of the resource to be locked; the index of each hash bucket points to the head node of the corresponding linked list interval; the lock nodes in the linked list interval corresponding to each hash bucket are sorted in ascending order of hash value.
[0023] In the above scheme, determining the insertion position of the lock node of the resource to be locked includes:
[0024] Determine the second hash bucket corresponding to the hash value of the lock node of the resource to be locked;
[0025] Traverse the linked list interval starting from the head node of the linked list interval corresponding to the second hash bucket;
[0026] Determine whether the hash value of the current node being traversed is greater than or equal to the hash value of the lock node of the resource to be locked, and whether the first operation result is equal to the second operation result; the first operation result represents the operation result of right-shifting the hash value of the resource to be locked; the second operation result represents the operation result of right-shifting the hash value of the current node;
[0027] If the first operation result is equal to the second operation result, and the hash value of the current node is greater than or equal to the hash value of the lock node of the resource to be locked, the insertion position is determined to be between the previous node of the current node and the current node.
[0028] Secondly, embodiments of the present invention provide a resource lock management device, the device comprising:
[0029] The first determining module is used to determine the hash value corresponding to the unique identifier of the resource to be locked;
[0030] The second determining module is used to determine the first hash bucket corresponding to the hash value in the preset hash table; wherein, the preset hash table includes multiple hash buckets, the multiple hash buckets correspond to the same doubly linked list, and each hash bucket corresponds to a segment of the doubly linked list; each lock node on the doubly linked list represents a resource lock for a resource to be locked;
[0031] The third determining module is used to determine the lock node of the resource to be locked based on the linked list range corresponding to the first hash bucket;
[0032] The acquisition module is used to acquire the resource lock of the resource to be locked from the lock node of the resource to be locked.
[0033] Thirdly, embodiments of the present invention provide an electronic device, including a processor and a memory, which are interconnected. The memory is used to store a computer program, which includes program instructions. The processor is configured to invoke the program instructions to execute the steps of the resource lock management method provided in the first aspect of the present invention.
[0034] Fourthly, embodiments of the present invention provide a computer-readable storage medium, comprising: the computer-readable storage medium storing a computer program. When the computer program is executed by a processor, it implements the steps of the resource lock management method provided in the first aspect of the present invention.
[0035] This embodiment determines the first hash bucket corresponding to the unique identifier of the resource to be locked by determining the hash value. Based on the linked list range corresponding to the first hash bucket, the lock node of the resource to be locked is determined, and the resource lock of the resource to be locked is obtained from the lock node. The preset hash table includes multiple hash buckets, each corresponding to the same doubly linked list. Each hash bucket corresponds to a segment of the doubly linked list, and each lock node on the doubly linked list represents a resource lock for a resource to be locked. In this embodiment, when accessing a resource to be locked, the corresponding lock node is obtained from the doubly linked list based on the hash value corresponding to the unique identifier of the resource, and the resource lock of the resource to be locked is obtained from the lock node. This embodiment manages resource locks based on hash tables and doubly linked lists. Each resource lock corresponds to a node in the doubly linked list, and each resource to be locked can have its own dedicated lock. The granularity of resource locks is the smallest, which can reduce the probability of lock conflicts in concurrent scenarios and improve system concurrency. Attached Figure Description
[0036] Figure 1 This is a schematic diagram illustrating the implementation process of a resource lock management method provided in an embodiment of the present invention;
[0037] Figure 2 This is a schematic diagram illustrating the correspondence between a hash table and a doubly linked list provided in an embodiment of the present invention;
[0038] Figure 3 This is a schematic diagram of a hash table and a doubly linked list data structure provided in an embodiment of the present invention;
[0039] Figure 4 This is a schematic diagram illustrating the creation process of a hash table and a doubly linked list provided in an application embodiment of the present invention;
[0040] Figure 5A This is a schematic diagram of a process for finding the insertion position of a locked node according to an application embodiment of the present invention;
[0041] Figure 5B This is a schematic diagram of a process for inserting a lock node at a specified position according to an application embodiment of the present invention;
[0042] Figure 5C This is a schematic diagram of a process for updating a bucket index provided in an application embodiment of the present invention;
[0043] Figure 6 This is a schematic diagram of a lock node acquisition process provided in an application embodiment of the present invention;
[0044] Figure 7 This is a schematic diagram of a resource lock deletion process provided in an application embodiment of the present invention;
[0045] Figure 8 This is a schematic diagram of a resource lock management device provided in an embodiment of the present invention;
[0046] Figure 9 This is a schematic diagram of an electronic device provided in an embodiment of the present invention. Detailed Implementation
[0047] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0048] A database is a shared resource used by multiple users. When multiple users concurrently access data, multiple transactions may occur that access the same data simultaneously. If concurrent operations are not controlled, incorrect data may be read and stored, compromising database consistency.
[0049] Locking is a crucial technique for implementing database concurrency control. Before a transaction operates on a data object, it requests a lock from the system. Once locked, the transaction gains control over the data object, preventing other transactions from updating it until the lock is released. Concurrent access to shared resources without locking can lead to data inconsistencies. To address concurrency issues, locking is typically used before accessing shared resources, ensuring that only one thread accesses the resource at a time.
[0050] Lock granularity refers to the granularity of the locked resource, such as a data item, record, file, or the entire database. The finer the lock granularity, the higher the parallelism of transactions. Generally, the larger the lock granularity, the higher the probability of multiple unrelated transactions contending for the lock, meaning more severe lock conflicts and lower concurrency and throughput of the entire system.
[0051] The relevant technologies include the following resource locking schemes:
[0052] 1. Coarse-grained locks / large locks.
[0053] Typical examples are global locks and table-level locks in MySQL databases, which are the two most granular types of locks in MySQL. Global locks can lock the entire database instance, while table-level locks can lock the entire table.
[0054] Their advantages are simple implementation and low resource consumption; their disadvantages are large lock granularity, high probability of lock conflicts, and low concurrency.
[0055] 2. Segmented lock based on static hash table.
[0056] A hash table `lock_hash_table` containing N locks is created in advance. An integer hash value `hash_int` is calculated based on the unique identifier `key` of the resource to be locked. Then, the modulo operation is performed on N to locate the index of a hash bucket in the hash table, `bucket_index = hash_int mod N`. Finally, the lock `lock_hash_table[bucket_index]` corresponding to that bucket is the fine-grained lock that needs to be used for the resource to be locked.
[0057] The advantage of this approach is that it reduces the granularity of locks compared to large locks, thereby reducing lock conflicts and improving concurrency and throughput. The disadvantage is that the granularity of locks usually decreases as the number of locks N increases, so a large number of locks need to be created in advance, consuming a lot of memory resources. On the other hand, it is also necessary to select a suitable hash algorithm.
[0058] 3. Hash lock based on segmented locks and HashMap.
[0059] For each resource to be locked, assuming its unique identifier is `key`, a dedicated lock `dedicated_lock` is dynamically created for it when locking is required. Note that this is created on demand, not pre-created. After creation, `dedicated_lock` is stored in a HashMap, `HashMap[key] = dedicated_lock`. When other threads concurrently lock the resource, they can retrieve the created `dedicated_lock` from the HashMap, and its reference count is incremented by 1. When releasing `dedicated_lock`, its reference count is decremented by 1. When the reference count reaches 0, the lock is removed from the HashMap to release the resource, `HashMap.Delete(key)`. Furthermore, to ensure thread safety when storing and retrieving `dedicated_lock` in the HashMap, segmented locks are used to optimize thread synchronization.
[0060] The advantage of this scheme is that it has the smallest lock granularity, with each resource having its own dedicated lock. The locks are also dynamically created and destroyed, which saves memory resources. The disadvantage is that in order to ensure thread safety, segmented locks need to be introduced. If there is a large conflict among the segmented locks, it will bring a lot of additional time overhead.
[0061] It is evident that the above three solutions have some problems to varying degrees:
[0062] 1. The lock granularity is too large, resulting in severe lock conflicts, low concurrency, and low throughput;
[0063] 2. It consumes a lot of memory resources and is highly dependent on the quality of the hash algorithm.
[0064] 3. The additional time overhead is too large, and it is only suitable for some application scenarios where locks occupy a long time.
[0065] To address the shortcomings of the aforementioned related technologies, embodiments of the present invention provide a resource lock management method that minimizes lock granularity and improves system concurrency. To illustrate the technical solution described in this invention, specific embodiments are provided below.
[0066] Figure 1This is a schematic diagram illustrating the implementation flow of a resource lock management method provided in an embodiment of the present invention. The execution subject of the resource lock management method is a thread accessing a resource to be locked. (Refer to...) Figure 1 Resource lock management methods include:
[0067] S101, Determine the hash value corresponding to the unique identifier of the resource to be locked.
[0068] In this embodiment, the resource to be locked can be a data item, record, file, or database, etc. The unique identifier refers to the identification information that can indicate the identity of the resource to be locked. For example, the name of a file can be used as the unique identifier of the file, and the database identifier (DBID) of a database can be used as the unique identifier of the database.
[0069] Perform a hash calculation on the unique identifier of the resource to be locked to obtain the hash value corresponding to the unique identifier of the resource to be locked.
[0070] Here, the hash algorithm used for hash calculation in this embodiment can be selected according to the actual situation, and this embodiment does not limit the hash algorithm.
[0071] S102, determine the first hash bucket corresponding to the hash value in the preset hash table; wherein, the preset hash table includes multiple hash buckets, the multiple hash buckets correspond to the same doubly linked list, and each hash bucket corresponds to a segment of the doubly linked list; each lock node on the doubly linked list represents a resource lock for a resource to be locked.
[0072] In this embodiment, a hash table is pre-set, which includes multiple hash buckets, and the multiple hash buckets correspond to the same doubly linked list.
[0073] A hash table is a data structure that allows direct access based on a key value. It accesses records by mapping the key value to a location in the table, thus speeding up the search process. This mapping function is called a hash function, and the array that stores the records is called a hash table.
[0074] A doubly linked list is a type of linked list where each data node has two pointers, one pointing to its immediate successor and the other to its immediate predecessor. Therefore, starting from any node in a doubly linked list, it is very convenient to access its predecessor and successor nodes.
[0075] In this embodiment, all hash buckets of the preset hash table correspond to the same doubly linked list. The doubly linked list is composed of many lock nodes connected together. Each hash bucket points to a lock node of the doubly linked list, and each hash bucket corresponds to a segment of the doubly linked list.
[0076] A pre-defined hash table serves as the index for resource locks, optimizing their access. Essentially, it's an array of lock pointers of length N, where N is typically a power of 2 for easier bitwise operations. Each element of this array is called a bucket, storing a pointer to the first lock in that bucket. Each time a resource lock is accessed, a hash calculation is performed based on the unique identifier key of the resource to be locked. This hash is then modulo N to locate the corresponding bucket, allowing for quick identification of the resource lock's access position.
[0077] A doubly linked list is used to store all created resource locks. Lock nodes within each bucket are tightly connected, with `prev` and `next` pointers pointing to their predecessor and successor nodes, respectively. Furthermore, the lock nodes within a bucket are sorted in ascending order based on their key hash, and the bucket points to the node with the smallest key hash, saving subsequent lookup time.
[0078] Each lock node stores the resource lock information of a resource to be locked. The lock node can also store information such as the unique identifier of the resource to be locked, the hash value, and the reference count of the resource lock.
[0079] like Figure 2 As shown, Figure 2 This is a schematic diagram illustrating the correspondence between a hash table and a doubly linked list provided in an embodiment of the present invention. The hash table serves as an index for the resource locks and includes N buckets (hash buckets), where N is typically a power of 2, for example, 64. A directed doubly linked list stores all resource locks, with lock nodes within the same bucket closely connected and sorted in ascending order of keyHash. Each bucket points to the lock node with the smallest keyHash within that bucket.
[0080] like Figure 2 In the context of the lock, bucket0 points to the lock node Lock0. Lock0 stores the unique identifier key of the resource to be locked, keyHash, rwLock (resource lock), and the reference count of the resource lock.
[0081] Lock0 and Lock1 both belong to bucket0, but Lock1's keyHash is greater than Lock0's keyHash, so Lock1 is after Lock0, and the index of bucket0 points to Lock0.
[0082] In practical applications, there are many types of resource locks. Based on lock granularity, they can be divided into row-level locks, table-level locks, and page-level locks. Based on lock level, they can be divided into shared locks and exclusive locks. Based on usage, they can be divided into optimistic locks and pessimistic locks. Based on locking method, they can be divided into automatic locks and explicit locks. Based on operation, they can be divided into data locks (DML, DataLocks) and data dictionary locks (DDL, Dictionary Locks). This embodiment does not limit the type of resource lock; it can be any type of resource lock.
[0083] In this embodiment, the first hash bucket corresponding to the hash value can be determined according to the consistent hash algorithm. The key is obtained by hash calculation based on the unique identifier key of the resource to be locked. Then, the modulo of keyHash with N is used to locate the first hash bucket.
[0084] S103, based on the linked list range corresponding to the first hash bucket, determine the lock node of the resource to be locked.
[0085] Each hash bucket corresponds to a segment of a doubly linked list. For example, lock nodes under the same hash bucket are sorted in ascending order of hash value, and the index of the hash bucket points to the lock node with the smallest hash value under that hash bucket.
[0086] You can start by traversing the linked list range from the lock node with the smallest hash value in the hash bucket to find the lock node for the resource to be locked.
[0087] For example, if a lock node stores a unique identifier and hash value of the resource to be locked, the lock node for the resource to be locked can be determined based on the hash value or the unique identifier. If the hash value or the unique identifier is the same, then the currently traversed node is considered the lock node for the resource to be locked.
[0088] S104, Obtain the resource lock of the resource to be locked from the lock node of the resource to be locked.
[0089] Each lock node stores resource lock information for a resource to be locked. This resource lock information can be a string of characters or a data structure. After a thread acquires the resource lock from the lock node, it can use the resource lock to access the resource.
[0090] When multiple threads access a resource to be locked, the threads are placed in a preset queue in a preset order, and only one thread is allowed to use the resource lock to access the resource at a time, thereby preventing other threads from preempting the resource.
[0091] This embodiment determines the first hash bucket corresponding to the unique identifier of the resource to be locked by determining the hash value. Based on the linked list range corresponding to the first hash bucket, the lock node of the resource to be locked is determined, and the resource lock of the resource to be locked is obtained from the lock node. The preset hash table includes multiple hash buckets, each corresponding to the same doubly linked list. Each hash bucket corresponds to a segment of the doubly linked list, and each lock node on the doubly linked list represents a resource lock for a resource to be locked. In this embodiment, when accessing a resource to be locked, the corresponding lock node is obtained from the doubly linked list based on the hash value corresponding to the unique identifier of the resource to be locked, and the resource lock of the resource to be locked is obtained from the lock node. This embodiment manages resource locks based on hash tables and doubly linked lists. Each resource lock corresponds to a node in the doubly linked list, and each resource to be locked can have its own dedicated lock, making the granularity of resource locks the smallest possible, reducing the probability of lock conflicts in concurrent scenarios, and improving system concurrency.
[0092] This embodiment provides a resource lock with minimal granularity in a low-cost manner (low memory overhead, low additional time overhead), which can be used to achieve high-performance, high-concurrency, and high-throughput concurrent programming in various business scenarios.
[0093] For example, in a user-space file system (FUSE), tens of thousands of files need to be accessed concurrently. In this embodiment, a resource lock can be set for each file in FUSE. When multiple threads access files in FUSE concurrently, since each file has its own dedicated lock, the probability of lock conflicts in concurrent scenarios can be effectively reduced, the system concurrency can be improved, and high-performance file reading and writing can be achieved.
[0094] In Online Transaction Processing (OLTP), OLTP represents a highly transactional system, typically a highly available online system that primarily handles small transactions and queries. In such systems, a single database often processes hundreds or even thousands of transactions per second, with thousands or even tens of thousands of SELECT statements executed per second. Typical OLTP systems include e-commerce systems, banking systems, and securities systems; for example, eBay's business database is a typical OLTP database. In MySQL OLTP scenarios, a large number of concurrent DML statements (including INSERT, UPDATE, and DELETE) access critical data structures protected by the global lock `lock_sys->mutex`, causing severe lock contention and a sharp decline in performance. This embodiment addresses this by setting a resource lock for each page in the database, with the page's unique identifier being `page_no`. During concurrent queries, a dedicated fine-grained lock is obtained for each page based on its `page_no`. Since each page has its own dedicated lock, lock contention can be significantly reduced, improving system concurrency.
[0095] This embodiment can be applied to cloud computing platforms such as Kubernetes and Redis. In Kubernetes, a ConfigMap instance can be created to store a hash table and a doubly linked list. Each lock node in the doubly linked list corresponds to a Kubernetes resource, and a dedicated resource lock can be set for each resource in Kubernetes. When multiple clients of a Kubernetes tenant concurrently access Kubernetes resources, the probability of lock contention is reduced and Kubernetes concurrency is improved because each resource has its own dedicated lock.
[0096] In one embodiment, determining the lock node for the resource to be locked based on the linked list range corresponding to the first hash bucket includes:
[0097] Determine whether the linked list range corresponding to the first hash bucket includes the lock node of the resource to be locked;
[0098] If the linked list range corresponding to the first hash bucket does not include the lock node of the resource to be locked, then the lock node of the resource to be locked is created.
[0099] Insert the newly created lock node of the resource to be locked into the doubly linked list;
[0100] If the linked list interval corresponding to the first hash bucket includes the lock node of the resource to be locked, then the lock node of the resource to be locked is determined from the linked list interval corresponding to the first hash bucket.
[0101] As mentioned above, if the lock node stores a unique identifier and hash value of the resource to be locked, the lock node of the resource to be locked can be determined based on the hash value or the unique identifier.
[0102] If the linked list interval corresponding to the first hash bucket does not have a lock node for the resource to be locked, a new lock node is created to store the resource lock for the resource to be locked, and the newly created lock node is inserted into the doubly linked list.
[0103] This embodiment does not require the pre-construction of a large number of lock nodes, but instead creates new lock nodes according to actual needs, resulting in lower memory consumption.
[0104] Figure 3 This is a schematic diagram of a hash table and a doubly linked list data structure provided in an embodiment of the present invention, mainly including three structures:
[0105] FGLock (Fine-Grained Lock) is a fine-grained lock used to manage global configurations such as keyshifts (right shift of hash values), index buckets, and an ordered doubly linked list of storage locks.
[0106] LockList is an ordered doubly linked list that stores locks and is used to manage the head node of the list and the number of lock nodes.
[0107] A lock node is a wrapper around a resource lock, additionally recording important information such as the key (unique identifier), keyHash, and count (resource lock reference count). Each lock node includes a pointer to the previous node (prev) and a pointer to the next node (nest).
[0108] In practical applications, the FGLock structure is created and initialized first.
[0109] During initialization, you can optionally input the initial size of the index, which is the initial number of buckets (hash buckets). The default value is 1024. Each bucket (essentially a pointer) occupies 8 bytes of memory on a 64-bit system. With a size of 1024, only 8 * 1024 = 8KB of memory is needed, consuming relatively little memory. The size can be adjusted appropriately according to actual needs. In scenarios with particularly high concurrency, increasing the size can optimize the lock's access performance.
[0110] like Figure 4 As shown, Figure 4 This is a schematic diagram illustrating the creation process of a hash table and a doubly linked list according to an application embodiment of the present invention. The process includes:
[0111] S401, Input the size of the index.
[0112] The default size is 1024, where Size represents the number of buckets (hash buckets).
[0113] S402, determine if Size is a power of 2.
[0114] If yes, execute S404; otherwise, execute S403.
[0115] S403 converts size to a power of 2 that is slightly larger than it.
[0116] S404 calculates keyshifts based on size, which facilitates subsequent hash modulo operations and transportation.
[0117] Keyshifts=sizeof(int)-log2(size)
[0118] S405, Create index buckets.
[0119] Buckets=malloc(sizeof(Lock*)*size)
[0120] S406, Create a doubly linked list linked_list.
[0121] linked_list=&LockList{head:&Lock{}}
[0122] After creating the hash table and doubly linked list, there are no lock nodes in the doubly linked list. In this embodiment, lock nodes are created according to actual needs, which can reduce the overhead of memory resources.
[0123] In one embodiment, inserting the newly created lock node of the resource to be locked into the doubly linked list includes:
[0124] Obtain the newly created lock node for the resource to be locked;
[0125] Determine the insertion position of the lock node for the resource to be locked;
[0126] Insert the lock node of the resource to be locked into the insertion position;
[0127] Update the index of the hash bucket corresponding to the lock node of the resource to be locked; the index of each hash bucket points to the head node of the corresponding linked list interval; the lock nodes in the linked list interval corresponding to each hash bucket are sorted in ascending order of hash value.
[0128] For example, based on the hash value of the unique identifier of the resource to be locked, the hash bucket corresponding to the lock node of the resource to be locked is determined, and the insertion position of the lock node of the resource to be locked is found in that hash bucket. After inserting the lock node of the resource to be locked at that insertion position, the pointer of the lock node and the index of the hash bucket are updated.
[0129] In one embodiment, determining the insertion position of the lock node of the resource to be locked includes:
[0130] Determine the second hash bucket corresponding to the hash value of the lock node of the resource to be locked;
[0131] Traverse the linked list interval starting from the head node of the linked list interval corresponding to the second hash bucket; the lock nodes in the linked list interval corresponding to each hash bucket are sorted in ascending order according to the size of the hash value;
[0132] Determine whether the hash value of the current node being traversed is greater than or equal to the hash value of the lock node of the resource to be locked, and whether the first operation result is equal to the second operation result; the first operation result represents the operation result of right-shifting the hash value of the resource to be locked; the second operation result represents the operation result of right-shifting the hash value of the current node;
[0133] If the first operation result is equal to the second operation result, and the hash value of the current node is greater than or equal to the hash value of the lock node of the resource to be locked, the insertion position is determined to be between the previous node of the current node and the current node.
[0134] In one embodiment, determining the second hash bucket corresponding to the hash value of the lock node of the resource to be locked includes:
[0135] The hash value of the lock node of the resource to be locked is right-shifted to obtain the first operation result;
[0136] The second hash bucket is determined based on the result of the first calculation.
[0137] For example, perform a right shift 16-bit XOR operation on the hash value.
[0138] Hash tables typically use hash modulo to locate buckets. This requires converting the hash value to decimal and then performing a modulo operation with the hash table length to obtain the remainder, which is relatively inefficient. This embodiment uses a right shift operation on the hash value to locate the bucket. Bitwise operations operate directly on the memory data without needing to be converted to decimal, thus resulting in very fast processing speed and improved data lookup performance.
[0139] In another embodiment, it is also necessary to determine whether the second hash bucket is empty, that is, to determine whether there is a lock node in the linked list interval corresponding to the second hash bucket. If the second hash bucket is empty (no lock node), then the traversal starts from the hash bucket adjacent to the left of the second hash bucket. If the hash bucket adjacent to the left of the second hash bucket is also empty, then the traversal starts from the head node of the doubly linked list.
[0140] In this embodiment, the lock nodes in the linked list interval are connected in ascending order according to their hash values. The linked list interval is traversed starting from the head node of the linked list interval corresponding to the second hash bucket, that is, starting from the lock node with the smallest hash value. At the beginning of the traversal, the current node is the head node of the linked list interval (the lock node with the smallest hash value).
[0141] Determine whether the hash value of the current node is greater than or equal to the hash value of the lock node of the resource to be locked, and whether the result of the right shift operation on the hash value of the resource to be locked is equal to the result of the right shift operation on the hash value of the current node (i.e., determine whether they correspond to the same hash bucket). If they correspond to the same hash bucket, and the hash value of the current node is greater than or equal to the hash value of the lock node of the resource to be locked, then determine that the insertion position of the lock node of the resource to be locked is between the previous node of the current node and the current node.
[0142] Since the lock nodes of a doubly linked list are connected in ascending order of hash value, if the hash value of the current node is greater than or equal to the hash value of the lock node of the resource to be locked, it means that the current node is after the lock node of the resource to be locked. Therefore, the lock node of the resource to be locked is inserted before the current node, that is, the lock node of the resource to be locked is the node before the current node.
[0143] In one embodiment, the method further includes:
[0144] If the hash value of the current node is less than the hash value of the lock node of the resource to be locked, and the first calculation result is equal to the second calculation result, then it is determined whether the next node of the current node is empty;
[0145] If the next node of the current node is empty, then the insertion position is determined to be the next node of the current node.
[0146] If the next node of the current node is empty, it means that the current node is the last node of the doubly linked list. If the hash value of the current node is less than the hash value of the lock node of the resource to be locked, it means that the current node is located before the lock node of the resource to be locked. Therefore, the lock node of the resource to be locked is inserted after the current node, that is, the lock node of the resource to be locked is the next node of the current node.
[0147] In one embodiment, the method further includes:
[0148] If the next node of the current node is not empty, then determine whether the next node of the current node belongs to the subsequent hash bucket;
[0149] If the next node of the current node belongs to the subsequent hash bucket, then the insertion position is determined to be between the next node of the current node and the current node.
[0150] If the next node of the current node is not empty, it means there are lock nodes following the current node. To determine if the next node belongs to the next hash bucket, we can perform a right shift operation on the hash value of the next node. If the right shift results are the same, they belong to the same hash bucket; if the results are different, they do not belong to the same hash bucket. Since hash buckets are adjacent, the next node of the current node belongs to the next hash bucket. In this case, the lock node for the resource to be locked is inserted between the next node of the current node and the current node.
[0151] In one embodiment, the method further includes:
[0152] If the next node of the current node does not belong to the subsequent hash bucket, then continue traversing the doubly linked list starting from the next node of the current node until the insertion position is found.
[0153] If the next node of the current node and the lock node of the resource to be locked belong to the same hash bucket, it means that the lock nodes of the linked list range have not been traversed. Continue traversing the doubly linked list until the insertion point of the lock node of the resource to be locked is found.
[0154] This embodiment does not require the pre-creation of a large number of resident resource locks, but dynamically creates resource locks on demand, so it occupies less memory resources and has a low dependence on the quality of the hash algorithm.
[0155] In one embodiment, after inserting the lock node of the resource to be locked into the insertion position, the method further includes:
[0156] Update the predecessor and successor nodes of the lock node of the resource to be locked;
[0157] Set the first pointer of the predecessor node to the lock node of the resource to be locked;
[0158] If the successor node is not empty, the second pointer of the successor node is set to the lock node of the resource to be locked.
[0159] In practical applications, the first pointer is the nest pointer, and the second pointer is the prev pointer. Each lock node has one nest pointer and one prev pointer.
[0160] After inserting the lock node of the resource to be locked into the doubly linked list, update the predecessor and successor nodes of the lock node. Set the nest pointer of the predecessor node to point to the lock node of the resource to be locked. Since the successor node may be null, if the successor node is not null, set its prev pointer to point to the lock node of the resource to be locked. Updating the lock node's pointers facilitates subsequent lock node lookups.
[0161] In one embodiment, updating the index of the hash bucket corresponding to the lock node of the resource to be locked includes:
[0162] Determine whether there are any other nodes besides the lock node of the resource to be locked in the third hash bucket corresponding to the lock node of the resource to be locked.
[0163] If the third hash bucket has no other nodes, then the index of the third hash bucket is set to the lock node of the resource to be locked.
[0164] After inserting the lock node of the resource to be locked into the third hash bucket, if the third hash bucket only contains the lock node of the resource to be locked, then the lock node of the resource to be locked is the head node of the linked list range of the third hash bucket, and the index of the third hash bucket is set to point to the lock node of the resource to be locked.
[0165] In one embodiment, the method further includes:
[0166] If the third hash bucket has other nodes, then determine whether the hash value of the lock node of the resource to be locked is less than the hash value of the lock node pointed to by the index of the third hash bucket;
[0167] If it is less than, then the index of the third hash bucket is set to point to the lock node of the resource to be locked.
[0168] If the third hash bucket contains other lock nodes besides the one for the resource to be locked, then it further checks whether the hash value of the lock node for the resource to be locked is less than the hash value of the lock node pointed to by the index of the third hash bucket. If it is less, it means that the lock node for the resource to be locked is the head node of the third hash bucket, and the index of the third hash bucket is set to point to the lock node for the resource to be locked. The index of the hash bucket is then updated to facilitate subsequent lock node lookups.
[0169] In one embodiment, after determining the lock node of the resource to be locked from the linked list interval corresponding to the first hash bucket, the method further includes:
[0170] Get the current reference count of the resource lock for the resource to be locked in memory;
[0171] If the current reference count is the same as the expected original value, then the current reference count of the resource lock of the resource to be locked in memory is incremented by 1.
[0172] This embodiment employs Compare-And-Swap (CAS) technology to achieve lock-free operation. CAS is an atomic operation that can be used in multithreaded programming to implement uninterrupted data swapping, thereby avoiding data inconsistency problems caused by the uncertain execution order and unpredictable interruptions when multiple threads simultaneously modify the same data. CAS involves three operands: memory location, expected original value, and new value. When performing a CAS operation, the value at the memory location is compared with the expected original value. If they match, the processor automatically updates the value at that location with the new value; if they do not match, the processor does nothing. Only one thread will succeed when multiple threads execute a CAS operation simultaneously.
[0173] Lock-free, simply put, means avoiding the direct use of locks, reducing the overhead of locks in the system. Compared to lock-based algorithms, lock-free algorithms have a distinct characteristic: suspending a thread while performing data access does not prevent other threads from continuing execution. This means that at any given time, multiple lock-free threads can access the same data simultaneously without data races or corruption.
[0174] To address thread safety issues in concurrent scenarios, this embodiment does not introduce additional locks, as these would generate significant overhead and become new system bottlenecks. This embodiment employs CAS atomic operations to achieve lock-free operation, avoiding the overhead of context switching and scheduling, significantly reducing overhead, and minimizing the additional time overhead introduced by lock management, thereby achieving high concurrency and high throughput for the entire system.
[0175] In one embodiment, after inserting the newly created lock node of the resource to be locked into the doubly linked list, the method further includes:
[0176] Set the reference count of the resource lock for the newly created resource to be locked to 1.
[0177] For newly created resource locks, set their reference count to 1.
[0178] refer to Figure 5A , Figure 5B and Figure 5C , Figure 5A This is a schematic diagram of a process for finding the insertion position of a locked node according to an application embodiment of the present invention. Figure 5B This is a schematic diagram of a process for inserting a lock node at a specified position according to an application embodiment of the present invention. Figure 5C This is a schematic diagram of a process for updating a bucket index provided in an application embodiment of the present invention. Figure 5A , Figure 5B and Figure 5C When combined, this forms a flowchart illustrating the process of inserting a lock node into a doubly linked list.
[0179] Finding the insertion position includes steps S501-S521, inserting at the specified position includes steps S522-S528, and updating the index includes steps S529-S536.
[0180] The process includes:
[0181] S501, input the lock to be inserted, newLock.
[0182] S502, locate the index bucket based on newLock.keysheh.
[0183] Bucket=newLock.keysheh>>keyshifts
[0184] S503, attempt to insert newLock into the range corresponding to the bucket in linked_list.
[0185] linked_list is a doubly linked list.
[0186] S504, find the position where newLock is inserted into linked_list.
[0187] S505 checks if the bucket is not empty.
[0188] If the value is not empty, execute S506; if the value is empty, execute S508.
[0189] S506, starting from the bucket, find the insertion point.
[0190] currentSearch=bucket
[0191] S507, start traversing from currentSearch.
[0192] Start traversing from the head node of the bucket.
[0193] S508, find the first non-empty leftBucket to the left of the bucket.
[0194] S509, determine if leftBucket is not empty.
[0195] If the value is empty, execute S510; otherwise, execute S511.
[0196] S510, start the search from the head node of the doubly linked list.
[0197] currentSearch=linked_list.head
[0198] S511, start the search from the left Bucket.
[0199] currentSearch=leftBucket
[0200] S512, determine currentSearch.keyHash==newLock.keyHash&¤tSearch.key==newLock.key.
[0201] Determine whether the hash value of the current node being traversed is the same as the hash value of the lock to be inserted, and whether their unique identifiers are the same.
[0202] If they are the same, execute S513; otherwise, execute S515.
[0203] S513 indicates that currentSearch is an existing copy of newLock.
[0204] If they are the same, it means that newLock already exists.
[0205] S514 returns false, indicating that a copy of newLock already exists and there is no need to insert it again.
[0206] S515 determines whether the current node is in the same bucket as newLock and is the successor node of newLock.
[0207] currentSearch.keyHash>>keyshifts==newLock.keyHash>>keyshifts&¤tSearch.keyHash>=newLock.keyHash.
[0208] The right shift operation of the current node's hash value and the right shift operation of the newLock's hash value yield the same result, indicating that they are in the same bucket.
[0209] If the hash value of the current node is greater than or equal to the hash value of newLock, it means that the current node is the successor node of newLock.
[0210] If yes, execute S516; otherwise, execute S517.
[0211] S516 indicates that the insertion position in the linked_list is (currentSearch.prev, nil, currentSearch).
[0212] The insertion position is between the previous node and the current node.
[0213] S517, check if currentSearch.next is not empty.
[0214] Determine if the next node of the current node is not empty. If it is not empty, execute S519; otherwise, execute S518.
[0215] S518 indicates that the insertion position in the linked_list is (currentSearch, nil, nil).
[0216] The insertion position is the node after the current node.
[0217] S519 determines whether the next node of the current node belongs to the subsequent Bucket.
[0218] currentSearch.next.keyHash>>keyshifts>newLock.keyHash>>keyshifts
[0219] If the result of right-shifting the hash value of the next node of the current node is greater than the result of right-shifting the hash value of newLock, then the next node of the current node is considered to belong to the subsequent Bucket.
[0220] If yes, execute S520; otherwise, execute S521.
[0221] S520 indicates that the insertion position in the linked_list is (currentSearch, nil, currentSearch.next).
[0222] The insertion position is between the next node of the current node and the current node.
[0223] S521, No insertion point found, continue searching for the next node.
[0224] currentSearch=currentSearch.next
[0225] S522, insert newLock at the searched position (left.nil, right).
[0226] S523, update the predecessor and successor nodes of newLock to left and right respectively.
[0227] newLock.prev = left
[0228] newLock.next = right
[0229] Set the reference count to 1
[0230] newLock.count = 1
[0231] S524, whether CAS was successfully used to point the Next pointer of the predecessor node left to the newly inserted node newLock.
[0232] CAS(&left.next,right,newLock)
[0233] If yes, execute S526; otherwise, execute S525.
[0234] S525, Insertion failed, indicating that other threads have made concurrent modifications. Retry.
[0235] S526, determine if the successor node right is not empty.
[0236] If yes, execute S527; otherwise, complete the insertion.
[0237] S527, whether CAS was successfully used to point the Prev pointer of the successor node right to the newly inserted node newLock.
[0238] CAS(&right.prev,left,newLock)
[0239] If yes, execute S528; otherwise, execute S525.
[0240] S528, insertion successful.
[0241] S529, attempt to update the index.
[0242] S530 checks if the bucket is empty.
[0243] Determine if the bucket containing newLock is empty before inserting newLock into the linked list.
[0244] If yes, execute S531; otherwise, execute S533.
[0245] S531, whether to use CAS to modify the bucket pointer to newLock.
[0246] CAS(&bucket,nil,newLock) sets the index of bucket to point to newLock.
[0247] If yes, execute S535; otherwise, execute S532.
[0248] S532, modification failed, indicating that other threads performed concurrent modifications. Retry is required.
[0249] S533, bucket.keyHash>newLock.keyHash.
[0250] Determine if the hash value of the lock node pointed to by the bucket's index is greater than the hash value of newLock.
[0251] If yes, execute S534; otherwise, execute S535.
[0252] S534, whether to use CAS to modify the bucket pointer to newLock.
[0253] CAS(&bucket,bucket,newLock) determines whether CAS was successfully used to modify the index of bucket to point to newLock.
[0254] If yes, execute S535; otherwise, execute S532.
[0255] S535, index update successful, or no index update required.
[0256] S536 returns true, indicating that the insertion was successful.
[0257] In one embodiment, determining whether the linked list range corresponding to the first hash bucket includes the lock node of the resource to be locked includes:
[0258] Traverse the linked list interval starting from the head node of the linked list interval corresponding to the first hash bucket; the lock nodes in the linked list interval corresponding to each hash bucket are sorted in ascending order according to the size of the hash value;
[0259] Determine whether the current node being traversed is the lock node of the resource to be locked;
[0260] If the current node is not the lock node of the resource to be locked, then it is determined whether the hash value of the resource to be locked is greater than the hash value of the current node, and whether the third operation result is equal to the fourth operation result; the third operation result represents the operation result of right shifting the hash value of the resource to be locked; the fourth operation result represents the operation result of right shifting the hash value of the current node.
[0261] If the hash value of the resource to be locked is greater than the hash value of the current node, and the result of the third operation is equal to the result of the fourth operation, then continue to traverse the next node of the current node until the last node of the linked list interval is reached.
[0262] If the hash value of the resource to be locked is less than the hash value of the current node, or the result of the third operation is not equal to the result of the fourth operation, then it is determined that the linked list interval corresponding to the first hash bucket does not include the lock node of the resource to be locked.
[0263] In this embodiment, the hash value is determined to belong to the same hash bucket by a right shift operation. The specific implementation process is detailed in the above embodiment and will not be repeated here.
[0264] In one embodiment, determining whether the currently traversed node is the lock node for the resource to be locked includes:
[0265] Determine whether the hash value of the resource to be locked is equal to the hash value of the current node;
[0266] If they are equal, then the current node is determined to be the lock node for the resource to be locked.
[0267] Here, whether the current node is a lock node for the resource to be locked is determined by whether the hash values are the same. If they are the same, it is a lock node for the resource to be locked; if they are different, it is not a lock node for the resource to be locked.
[0268] Furthermore, if the lock node stores a unique identifier for the resource to be locked, it can also be used to determine whether the current node is the lock node for that resource. If the unique identifiers are the same, then it is the lock node for the resource to be locked; otherwise, it is not.
[0269] refer to Figure 6 , Figure 6 This is a schematic diagram of a lock node acquisition process provided in an application embodiment of the present invention. The process includes:
[0270] S601, Enter the unique identifier key of the resource to be locked.
[0271] S602, obtain the hash value keyHash by performing SipHash based on the key value.
[0272] S603, locate the index bucket based on keyHash.
[0273] bucket = keyHash >> keyshifts
[0274] S604, traverse the linked list under bucket.
[0275] currentLock = bucket, and iterates from the head node under this bucket.
[0276] S605, is currentLock not empty?
[0277] Determine whether the current node being traversed is not empty.
[0278] If yes, execute S606; otherwise, execute S611.
[0279] S606, determine whether the current lock is the lock corresponding to the resource.
[0280] currentLock.keyHash==keyHash&¤tLock.key==key.
[0281] If yes, execute S607; otherwise, execute S609.
[0282] S607, determine if the current lock is being deleted.
[0283] currentLock.count == 0
[0284] Otherwise, execute S608; otherwise, execute S611.
[0285] S608, whether to increment the reference count of the lock by 1 upon success.
[0286] CAS(¤tLock.count, count, count+1)
[0287] If yes, execute S614; otherwise, execute S611.
[0288] S609, currentLock.keyHash<keyHash&¤t.keyHash> >keyshifts==keyHash>>keyshifts.
[0289] Determine whether the hash value of the current lock node is less than the keyHash of the resource to be locked, and whether the current lock node and the lock node of the resource to be locked are in the same bucket.
[0290] If yes, execute S610; otherwise, execute S611.
[0291] S610, currentLock points to the next node.
[0292] `currentLock = currentLock.next` continues traversing the linked list from the next node.
[0293] S611 indicates that the lock you are looking for does not exist or is being deleted, and you need to create a new lock.
[0294] S612, whether newLock was successfully inserted into the doubly linked list linked_list.
[0295] If yes, execute S613; otherwise, execute S603.
[0296] S613 returns the newly created newLock.
[0297] S614 returns the previously created currentLock.
[0298] In one embodiment, after using the resource lock on the resource to be locked, the method further includes:
[0299] Decrement the reference count of the lock node of the resource to be locked by 1;
[0300] Determine whether the reference count of the lock node of the resource to be locked is 0;
[0301] If the reference count of the lock node of the resource to be locked is 0, then the lock node of the resource to be locked is deleted from the doubly linked list.
[0302] After using a resource lock, the user needs to explicitly call the lock deletion interface to indicate that the lock is no longer needed. In this embodiment, the resource lock's reference count will be decremented by 1. If the reference count is 0, it means no user is currently using the lock, and the resource lock will be automatically deleted to release the resource.
[0303] In another embodiment, in addition to modifying the reference count, it is also necessary to update the hash bucket index and modify the pointers of the predecessor and successor nodes.
[0304] refer to Figure 7 , Figure 7 This is a schematic diagram of a resource lock deletion process provided in an application embodiment of the present invention. The process includes:
[0305] S701, enter the resource lock to be deleted.
[0306] S702, decrement the reference count of the lock by 1.
[0307] S703, determine if the reference count of the lock is 0.
[0308] If the reference count is 0, then execute S704.
[0309] S704, attempting to remove the resource lock to release the resource.
[0310] S705 If the index of the bucket points to the lock, then the index needs to be updated to the successor node of the lock.
[0311] S706, locate the index based on lock.keyHash.
[0312] Locate the bucket based on the hash value of the lock.
[0313] Bucket=lock.keyhash>>keyshifts
[0314] S707 uses CAS to modify the bucket to point to the successor node of the lock.
[0315] S708, attempting to remove the lock node from the linked_list.
[0316] Try removing the lock node from the doubly linked list.
[0317] S709, obtain the predecessor and successor nodes of the lock node.
[0318] Left = lock.prev
[0319] Right = lock.nest
[0320] S710, whether CAS was successfully used to modify the successor node of left to right.
[0321] CAS(&left,nest,lock,right)
[0322] If yes, execute S711; otherwise, execute S714.
[0323] S711, whether the successor node right is not null.
[0324] If yes, execute S712; otherwise, execute S713.
[0325] S712, whether CAS was used to successfully modify the predecessor node of right to left.
[0326] CAS(&right,prev,lock,left)
[0327] If yes, execute S713; otherwise, execute S714.
[0328] S713, successfully removed the lock node from the doubly linked list.
[0329] S714, modification failed, indicating that other threads performed concurrent modifications. Retry is required.
[0330] This embodiment can dynamically create and destroy resource locks as needed, releasing memory resources in a timely manner and reducing memory overhead.
[0331] In practical applications, on a host with 8 logical CPUs and 16GB of memory, 16 threads concurrently write a total of 100,000 small files. Before writing a file, the corresponding lock needs to be acquired to simulate a scenario that ensures file writing safety under concurrency. The test data for various schemes are shown in Table 1 below:
[0332]
[0333] Table 1
[0334] As can be seen, the total time consumption of this embodiment is the shortest, which shows that it is the best in terms of concurrency and additional time overhead; in addition, the memory usage is also the lowest except for coarse-grained locks / big locks, which is also expected, because coarse-grained locks / big locks only have one global big lock, while this embodiment also needs to maintain some indexes and fine-grained locks.
[0335] Therefore, this embodiment achieves the smallest lock granularity, the highest system concurrency, low memory overhead, and allows for dynamic creation and destruction of locks on demand, with low dependence on the quality of the hash algorithm. Furthermore, it has minimal additional time overhead and is suitable for a wide range of application scenarios.
[0336] It should be understood that the sequence number of each step in the above embodiments does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention.
[0337] It should be understood that, when used in this specification and the appended claims, the terms "comprising" and "including" indicate the presence of the described features, integrals, steps, operations, elements and / or components, but do not exclude the presence or addition of one or more other features, integrals, steps, operations, elements, components and / or collections thereof.
[0338] It should be noted that the technical solutions described in the embodiments of the present invention can be combined arbitrarily without conflict.
[0339] In addition, in the embodiments of the present invention, "first," "second," etc. are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence.
[0340] refer to Figure 8 , Figure 8 This is a schematic diagram of a resource lock management device provided in an embodiment of the present invention, as shown below. Figure 8 As shown, the device includes:
[0341] The first determining module is used to determine the hash value corresponding to the unique identifier of the resource to be locked;
[0342] The second determining module is used to determine the first hash bucket corresponding to the hash value in the preset hash table; wherein, the preset hash table includes multiple hash buckets, the multiple hash buckets correspond to the same doubly linked list, and each hash bucket corresponds to a segment of the doubly linked list; each lock node on the doubly linked list represents a resource lock for a resource to be locked;
[0343] The third determining module is used to determine the lock node of the resource to be locked based on the linked list range corresponding to the first hash bucket;
[0344] The acquisition module is used to acquire the resource lock of the resource to be locked from the lock node of the resource to be locked.
[0345] In one embodiment, the third determining module determines the lock node of the resource to be locked based on the linked list range corresponding to the first hash bucket, specifically for:
[0346] Determine whether the linked list range corresponding to the first hash bucket includes the lock node of the resource to be locked;
[0347] If the linked list range corresponding to the first hash bucket does not include the lock node of the resource to be locked, then the lock node of the resource to be locked is created.
[0348] Insert the newly created lock node of the resource to be locked into the doubly linked list;
[0349] If the linked list interval corresponding to the first hash bucket includes the lock node of the resource to be locked, then the lock node of the resource to be locked is determined from the linked list interval corresponding to the first hash bucket.
[0350] In one embodiment, the apparatus further includes a reference counting module, configured to: obtain the current reference count of the resource lock of the resource to be locked in memory; and if the current reference count is the same as the expected original value, increment the current reference count of the resource lock of the resource to be locked in memory by 1.
[0351] In one embodiment, the third determining module inserts the newly created lock node of the resource to be locked into the doubly linked list, specifically for:
[0352] Obtain the newly created lock node for the resource to be locked;
[0353] Determine the insertion position of the lock node for the resource to be locked;
[0354] Insert the lock node of the resource to be locked into the insertion position;
[0355] Update the index of the hash bucket corresponding to the lock node of the resource to be locked; the index of each hash bucket points to the head node of the corresponding linked list interval; the lock nodes in the linked list interval corresponding to each hash bucket are sorted in ascending order of hash value.
[0356] In one embodiment, the third determining module determines the insertion position of the lock node of the resource to be locked, specifically for:
[0357] Determine the second hash bucket corresponding to the hash value of the lock node of the resource to be locked;
[0358] Traverse the linked list interval starting from the head node of the linked list interval corresponding to the second hash bucket;
[0359] Determine whether the hash value of the current node being traversed is greater than or equal to the hash value of the lock node of the resource to be locked, and whether the first operation result is equal to the second operation result; the first operation result represents the operation result of right-shifting the hash value of the resource to be locked; the second operation result represents the operation result of right-shifting the hash value of the current node;
[0360] If the first operation result is equal to the second operation result, and the hash value of the current node is greater than or equal to the hash value of the lock node of the resource to be locked, the insertion position is determined to be between the previous node of the current node and the current node.
[0361] In one embodiment, the third determining module determines the second hash bucket corresponding to the hash value of the lock node of the resource to be locked, specifically for:
[0362] The hash value of the lock node of the resource to be locked is right-shifted to obtain the first operation result;
[0363] The second hash bucket is determined based on the result of the first calculation.
[0364] In one embodiment, the device further includes:
[0365] The insertion position determination module is used to determine whether the next node of the current node is empty if the hash value of the current node is less than the hash value of the lock node of the resource to be locked, or the first operation result is not equal to the second operation result; if the next node of the current node is empty, the insertion position is determined to be the next node of the current node.
[0366] In one embodiment, the insertion position determination module is further configured to:
[0367] If the next node of the current node is not empty, then determine whether the next node of the current node belongs to the subsequent hash bucket;
[0368] If the next node of the current node belongs to the subsequent hash bucket, then the insertion position is determined to be between the next node of the current node and the current node.
[0369] In one embodiment, the device further includes:
[0370] The pointer points to the update module, which is used to update the predecessor and successor nodes of the lock node of the resource to be locked; the first pointer of the predecessor node is set to the lock node of the resource to be locked; if the successor node is not empty, the second pointer of the successor node is set to the lock node of the resource to be locked.
[0371] In one embodiment, the third determining module updates the index of the hash bucket corresponding to the lock node of the resource to be locked, specifically for:
[0372] Determine whether there are any other nodes besides the lock node of the resource to be locked in the third hash bucket corresponding to the lock node of the resource to be locked.
[0373] If the third hash bucket has no other nodes, then the index of the third hash bucket is set to the lock node of the resource to be locked.
[0374] In one embodiment, the third determining module is further configured to:
[0375] If the third hash bucket has other nodes, then determine whether the hash value of the lock node of the resource to be locked is less than the hash value of the lock node pointed to by the index of the third hash bucket;
[0376] If it is less than, then the index of the third hash bucket is set to point to the lock node of the resource to be locked.
[0377] In one embodiment, the third determining module determines whether the linked list range corresponding to the first hash bucket includes the lock node of the resource to be locked, specifically for:
[0378] Traverse the linked list interval starting from the head node of the linked list interval corresponding to the first hash bucket; the lock nodes in the linked list interval corresponding to each hash bucket are sorted in ascending order according to the size of the hash value;
[0379] Determine whether the current node being traversed is the lock node of the resource to be locked;
[0380] If the current node is not the lock node of the resource to be locked, then it is determined whether the hash value of the resource to be locked is greater than the hash value of the current node, and whether the third operation result is equal to the fourth operation result; the third operation result represents the operation result of right shifting the hash value of the resource to be locked; the fourth operation result represents the operation result of right shifting the hash value of the current node.
[0381] If the hash value of the resource to be locked is greater than the hash value of the current node, and the result of the third operation is equal to the result of the fourth operation, then continue to traverse the next node of the current node until the last node of the linked list interval is reached.
[0382] If the hash value of the resource to be locked is less than the hash value of the current node, or the result of the third operation is not equal to the result of the fourth operation, then it is determined that the linked list interval corresponding to the first hash bucket does not include the lock node of the resource to be locked.
[0383] In one embodiment, the device further includes:
[0384] The deletion module is used to decrement the reference count of the lock node of the resource to be locked by 1; determine whether the reference count of the lock node of the resource to be locked is 0; if the reference count of the lock node of the resource to be locked is 0, then delete the lock node of the resource to be locked from the doubly linked list.
[0385] In practical applications, the first determining module, the third determining module, and the acquisition module can be implemented by processors in electronic devices, such as central processing units (CPUs), digital signal processors (DSPs), microcontroller units (MCUs), or field-programmable gate arrays (FPGAs).
[0386] It should be noted that the resource lock management device provided in the above embodiments is only illustrated by the division of the modules described above when performing resource lock management. In actual applications, the above processing can be assigned to different modules as needed, that is, the internal structure of the device can be divided into different modules to complete all or part of the processing described above. In addition, the resource lock management device and the resource lock management method embodiments provided in the above embodiments belong to the same concept, and their specific implementation process can be found in the method embodiments, which will not be repeated here.
[0387] The aforementioned resource lock management device can be in the form of an image file. After execution, this image file can run as a container or virtual machine to implement the resource lock management method described in this application. However, it is not limited to the image file format; any software that can implement the resource lock management method described in this application is within the scope of protection of this application.
[0388] Based on the hardware implementation of the above program modules, and in order to implement the method of the embodiments of this application, the embodiments of this application also provide an electronic device. Figure 9 This is a schematic diagram of the hardware structure of the electronic device according to an embodiment of this application, as shown below. Figure 9 As shown, the electronic device includes:
[0389] A communication interface enables information exchange with other devices, such as network devices.
[0390] The processor, connected to the communication interface, enables information interaction with other devices and, when running a computer program, executes the methods provided by one or more technical solutions on the electronic device side. The computer program is stored in memory.
[0391] Of course, in practical applications, the various components in an electronic device are coupled together through a bus system. It can be understood that the bus system is used to achieve communication and connection between these components. In addition to the data bus, the bus system also includes a power bus, a control bus, and a status signal bus. However, for the sake of clarity, in... Figure 9 The general will label all buses as bus systems.
[0392] The aforementioned electronic devices can be in cluster form, such as a cloud computing platform. A cloud computing platform is a business model that uses computing virtualization, network virtualization, and storage virtualization technologies to organize multiple independent server physical hardware resources into a pool of resources. It is a software-defined resource structure based on the development of virtualization technology, which can provide resource capabilities in the form of virtual machines, containers, etc. By eliminating the fixed relationship between hardware and operating system, relying on network connectivity for unified resource scheduling, and then providing the required virtual resources and services, it is a new type of IT software delivery model with characteristics such as flexibility, elasticity, distributed nature, multi-tenancy, and on-demand.
[0393] Current cloud computing platforms support several service models:
[0394] SaaS (Software as a Service): Cloud computing platform users do not need to purchase software, but instead rent software deployed on the cloud computing platform. Users do not need to maintain the software, as the software service provider will manage and maintain the software in its entirety.
[0395] PaaS (Platform as a Service): Cloud computing platform users (usually software developers) can build new applications or extend existing applications on the architecture provided by the cloud computing platform without having to purchase development, quality control or production servers.
[0396] IaaS (Infrastructure as a Service): Cloud computing platforms provide data centers, infrastructure hardware and software resources via the Internet. Cloud computing platforms under the IaaS model can provide servers, operating systems, disk storage, databases and / or information resources.
[0397] The memory in this application embodiment is used to store various types of data to support the operation of the electronic device. Examples of such data include any computer program used to operate on the electronic device.
[0398] It is understood that memory can be volatile or non-volatile, or both. Non-volatile memory can be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), ferromagnetic random access memory (FRAM), flash memory, magnetic surface memory, optical disc, or compact disc read-only memory (CD-ROM); magnetic surface memory can be disk storage or magnetic tape storage. Volatile memory can be random access memory (RAM), which is used as an external cache. By way of example, but not limitation, many forms of RAM are available, such as Static Random Access Memory (SRAM), Synchronous Static Random Access Memory (SSRAM), Dynamic Random Access Memory (DRAM), Synchronous Dynamic Random Access Memory (SDRAM), Double Data Rate Synchronous Dynamic Random Access Memory (DDRSDRAM), Enhanced Synchronous Dynamic Random Access Memory (ESDRAM), SyncLink Dynamic Random Access Memory (SLDRAM), and Direct Rambus Random Access Memory (DRRAM). The memories described in the embodiments of this application are intended to include, but are not limited to, these and any other suitable types of memory.
[0399] The methods disclosed in the embodiments of this application can be applied to a processor or implemented by a processor. The processor may be an integrated circuit chip with signal processing capabilities. During implementation, each step of the above method can be completed by integrated logic circuits in the processor's hardware or by instructions in software form. The processor may be a general-purpose processor, a DSP, or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The processor can implement or execute the methods, steps, and logic block diagrams disclosed in the embodiments of this application. A general-purpose processor may be a microprocessor or any conventional processor, etc. The steps of the methods disclosed in the embodiments of this application can be directly manifested as execution by a hardware decoding processor, or execution by a combination of hardware and software modules in the decoding processor. The software modules may be located in a storage medium, which is located in memory. The processor reads the program from the memory and, in conjunction with its hardware, completes the steps of the aforementioned method.
[0400] Optionally, when the processor executes the program, it implements the corresponding processes implemented by the electronic device in the various methods of the embodiments of this application. For the sake of brevity, these will not be described in detail here.
[0401] In an exemplary embodiment, this application also provides a storage medium, namely a computer storage medium, specifically a computer-readable storage medium, such as a first memory storing a computer program, which can be executed by a processor of an electronic device to complete the steps described in the aforementioned method. The computer-readable storage medium may be a memory such as FRAM, ROM, PROM, EPROM, EEPROM, Flash Memory, magnetic surface memory, optical disc, or CD-ROM.
[0402] In the several embodiments provided in this application, it should be understood that the disclosed apparatus, electronic devices, and methods can be implemented in other ways. The apparatus embodiments described above are merely illustrative. For example, the division of units is only a logical functional division, and in actual implementation, there may be other division methods, such as: multiple units or components may be combined, or integrated into another system, or some features may be ignored or not executed. In addition, the coupling, direct coupling, or communication connection between the various components shown or discussed may be through some interfaces, and the indirect coupling or communication connection between devices or units may be electrical, mechanical, or other forms.
[0403] The units described above as separate components may or may not be physically separate. The components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of the units may be selected to achieve the purpose of this embodiment according to actual needs.
[0404] In addition, each functional unit in the various embodiments of this application can be integrated into one processing unit, or each unit can be a separate unit, or two or more units can be integrated into one unit; the integrated unit can be implemented in hardware or in the form of hardware plus software functional units.
[0405] Those skilled in the art will understand that all or part of the steps of the above method embodiments can be implemented by hardware related to program instructions. The aforementioned program can be stored in a computer-readable storage medium. When the program is executed, it performs the steps of the above method embodiments. The aforementioned storage medium includes various media that can store program code, such as mobile storage devices, ROM, RAM, magnetic disks, or optical disks.
[0406] Alternatively, if the integrated units described above are implemented as software functional modules and sold or used as independent products, they can also be stored in a computer-readable storage medium. Based on this understanding, the technical solutions of the embodiments of this application, or the parts that contribute to related technologies, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as mobile storage devices, ROM, RAM, magnetic disks, or optical disks.
[0407] It should be noted that the technical solutions described in the embodiments of this application can be combined arbitrarily without conflict.
[0408] In addition, in this application example, "first," "second," etc. are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence.
[0409] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
1. A resource lock management method, characterized in that, include: Determine the hash value corresponding to the unique identifier of the resource to be locked; Determine the first hash bucket corresponding to the hash value in the preset hash table; wherein, the preset hash table includes multiple hash buckets, the multiple hash buckets correspond to the same doubly linked list, and each hash bucket corresponds to a segment of the doubly linked list; each lock node on the doubly linked list represents a resource lock for a resource to be locked; Determine whether the linked list range corresponding to the first hash bucket includes the lock node of the resource to be locked; If the linked list range corresponding to the first hash bucket does not include the lock node of the resource to be locked, then the lock node of the resource to be locked is created. Determine the second hash bucket corresponding to the hash value of the newly created lock node for the resource to be locked; Traverse the linked list interval starting from the head node of the linked list interval corresponding to the second hash bucket; Determine whether the hash value of the current node being traversed is greater than or equal to the hash value of the lock node of the resource to be locked, and whether the first operation result is equal to the second operation result; the first operation result represents the operation result of right-shifting the hash value of the resource to be locked; the second operation result represents the operation result of right-shifting the hash value of the current node; If the first operation result is equal to the second operation result, and the hash value of the current node is greater than or equal to the hash value of the lock node of the resource to be locked, the insertion position of the lock node of the resource to be locked is determined to be between the previous node of the current node and the current node. Insert the lock node of the resource to be locked into the insertion position; Obtain the resource lock of the resource to be locked from the lock node of the resource to be locked.
2. The method as described in claim 1, characterized in that, Determining whether the linked list range corresponding to the first hash bucket includes the lock node of the resource to be locked includes: If the linked list interval corresponding to the first hash bucket includes the lock node of the resource to be locked, then the lock node of the resource to be locked is determined from the linked list interval corresponding to the first hash bucket.
3. The method as described in claim 2, characterized in that, After determining the lock node of the resource to be locked from the linked list interval corresponding to the first hash bucket, the method further includes: Get the current reference count of the resource lock for the resource to be locked in memory; If the current reference count is the same as the expected original value, then the current reference count of the resource lock of the resource to be locked in memory is incremented by 1.
4. The method as described in claim 1, characterized in that, The step of inserting the lock node of the resource to be locked into the insertion position includes: Update the index of the hash bucket corresponding to the lock node of the resource to be locked; the index of each hash bucket points to the head node of the corresponding linked list interval; the lock nodes in the linked list interval corresponding to each hash bucket are sorted in ascending order of hash value.
5. The method as described in claim 1, characterized in that, The step of determining the second hash bucket corresponding to the hash value of the lock node of the resource to be locked includes: The hash value of the lock node of the resource to be locked is right-shifted to obtain the first operation result; The second hash bucket is determined based on the result of the first calculation.
6. The method as described in claim 1, characterized in that, The method further includes: If the hash value of the current node is less than the hash value of the lock node of the resource to be locked, or the first calculation result is not equal to the second calculation result, then determine whether the next node of the current node is empty; If the next node of the current node is empty, then the insertion position is determined to be the next node of the current node.
7. The method as described in claim 6, characterized in that, The method further includes: If the next node of the current node is not empty, then determine whether the next node of the current node belongs to the subsequent hash bucket; If the next node of the current node belongs to the subsequent hash bucket, then the insertion position is determined to be between the next node of the current node and the current node.
8. The method as described in claim 1, characterized in that, After inserting the lock node of the resource to be locked into the insertion position, the method further includes: Update the predecessor and successor nodes of the lock node of the resource to be locked; Set the first pointer of the predecessor node to the lock node of the resource to be locked; If the successor node is not empty, the second pointer of the successor node is set to the lock node of the resource to be locked.
9. The method as described in claim 4, characterized in that, Updating the index of the hash bucket corresponding to the lock node of the resource to be locked includes: Determine whether there are any other nodes besides the lock node of the resource to be locked in the third hash bucket corresponding to the lock node of the resource to be locked. If the third hash bucket has no other nodes, then the index of the third hash bucket is set to the lock node of the resource to be locked.
10. The method as described in claim 9, characterized in that, The method further includes: If the third hash bucket has other nodes, then determine whether the hash value of the lock node of the resource to be locked is less than the hash value of the lock node pointed to by the index of the third hash bucket; If it is less than, then the index of the third hash bucket is set to point to the lock node of the resource to be locked.
11. The method as described in claim 1, characterized in that, Determining whether the linked list range corresponding to the first hash bucket includes the lock node of the resource to be locked includes: Traverse the linked list interval starting from the head node of the linked list interval corresponding to the first hash bucket; the lock nodes in the linked list interval corresponding to each hash bucket are sorted in ascending order according to the size of the hash value; Determine whether the current node being traversed is the lock node of the resource to be locked; If the current node is not the lock node of the resource to be locked, then it is determined whether the hash value of the resource to be locked is greater than the hash value of the current node, and whether the third operation result is equal to the fourth operation result; the third operation result represents the operation result of right shifting the hash value of the resource to be locked; the fourth operation result represents the operation result of right shifting the hash value of the current node. If the hash value of the resource to be locked is greater than the hash value of the current node, and the result of the third operation is equal to the result of the fourth operation, then continue to traverse the next node of the current node until the last node of the linked list interval is reached. If the hash value of the resource to be locked is less than the hash value of the current node, or the result of the third operation is not equal to the result of the fourth operation, then it is determined that the linked list interval corresponding to the first hash bucket does not include the lock node of the resource to be locked.
12. The method as described in claim 1, characterized in that, After using the resource lock on the resource to be locked, the method further includes: Decrement the reference count of the lock node of the resource to be locked by 1; Determine whether the reference count of the lock node of the resource to be locked is 0; If the reference count of the lock node of the resource to be locked is 0, then the lock node of the resource to be locked is deleted from the doubly linked list.
13. A resource lock management device, characterized in that, include: The first determining module is used to determine the hash value corresponding to the unique identifier of the resource to be locked; The second determining module is used to determine the first hash bucket corresponding to the hash value in the preset hash table; wherein, the preset hash table includes multiple hash buckets, the multiple hash buckets correspond to the same doubly linked list, and each hash bucket corresponds to a segment of the doubly linked list; each lock node on the doubly linked list represents a resource lock for a resource to be locked; The third determining module is used to determine whether the linked list interval corresponding to the first hash bucket includes the lock node of the resource to be locked; if the linked list interval corresponding to the first hash bucket does not include the lock node of the resource to be locked, then a lock node of the resource to be locked is created; determine the second hash bucket corresponding to the hash value of the newly created lock node of the resource to be locked; traverse the linked list interval starting from the head node of the linked list interval corresponding to the second hash bucket; determine whether the hash value of the current traversed node is greater than or equal to the hash value of the lock node of the resource to be locked, and whether the first operation result is equal to the second operation result; the first operation result represents the operation result of right shifting the hash value of the resource to be locked; the second operation result represents the operation result of right shifting the hash value of the current node; if the first operation result is equal to the second operation result, and the hash value of the current node is greater than or equal to the hash value of the lock node of the resource to be locked, determine that the insertion position of the lock node of the resource to be locked is between the previous node of the current node and the current node; insert the lock node of the resource to be locked at the insertion position. The acquisition module is used to acquire the resource lock of the resource to be locked from the lock node of the resource to be locked.
14. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the resource lock management method as described in any one of claims 1 to 12.
15. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, the computer program including program instructions that, when executed by a processor, cause the processor to perform the resource lock management method as described in any one of claims 1 to 12.