Unique identifier generation method and device, equipment, medium and product

By obtaining and updating the maximum value of distributed identification from the database and determining the available identification interval based on the preset step size, the problems of inexpensive, simplicity and global uniqueness of unique identification generation in distributed systems are solved, and stable and reliable distributed unique identification generation is achieved.

CN119557378APending Publication Date: 2025-03-04BEIJING QIYI CENTURY SCI & TECH CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202411715743.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-27
Publication Date
2025-03-04

AI Technical Summary

Technical Problem

The prior art cannot realize cheap and simple distributed unique identification generation, especially in distributed table sub-scenarios, where self-increasing primary keys cannot guarantee global uniqueness, and methods that rely on third-party components have problems with unique identification duplication caused by clock callback.

Method used

By obtaining the maximum value of the assigned identification value from the database, performing an update command to update the maximum value, and determining the available identification interval based on the maximum value and the preset step size, a unique identification of the distributed data object is generated.

Benefits of technology

It realizes cheap, simple, stable and reliable distributed unique identifier generation, ensuring the global uniqueness of the identifier, reducing costs and simplifying operation steps.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119557378A_ABST
    Figure CN119557378A_ABST
Patent Text Reader

Abstract

The embodiment of the invention discloses a unique identifier generation method and device, equipment, a medium and a product, the method is applied to a target node in a plurality of nodes in a distributed system, the target node has distributed data, and the maximum value of numerical values of distributed identifiers obtained from a database is recorded as a first value. An updating command for the allocated identifier is executed, the updating command is used for indicating that when it is determined that the allocated identifier is not modified by other nodes except the target node, the maximum value of the numerical value of the allocated identifier is updated to a second value, the second value is determined according to a first value and a preset step length, and the first value and the second value can determine an available identifier interval; in this way, if the updating command is executed successfully, it is indicated that the available identification interval is the identification interval special for the target node, the global uniqueness of the available identification interval is guaranteed, the unique identification generated based on the available identification interval also has the global uniqueness, dependence on a third-party component is not needed, cost is reduced, and operation steps are simplified.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computers, and in particular to a unique identification generation method, device, electronic equipment, storage medium and program product. Background Art

[0002] In the daily application development process, for distributed scenarios, data can be split into multiple distributed data, such as splitting a data table into multiple distributed sub-tables, and generating unique identifiers for objects in the distributed data (such as each record in the distributed sub-table) to facilitate the management of distributed data.

[0003] Currently, the unique identifier can be generated by the self-incrementing primary key of the distributed sub-table. The self-incrementing primary key means that the value of the primary key field in the distributed sub-table is automatically increased each time a new record is inserted. Using the value of the primary key field as the unique identifier of the new record can ensure that the unique identifier is unique in a single table, but cannot meet the uniqueness of the unique identifier in all sub-tables. Therefore, this unique identifier generation method cannot meet the generation requirements of the global unique identifier for all sub-tables. When using open source third-party components such as the snowflake algorithm to generate a global unique identifier, since the generated global unique identifier depends on the system timestamp, there is a problem of timestamp errors caused by clock dialing back, which in turn causes the current generated global unique identifier to be repeated with the unique identifier that has already been generated.

[0004] Therefore, it is currently impossible to achieve cheap and easy distributed unique identifier generation. Summary of the invention

[0005] To solve the above technical problems, the embodiments of the present application provide a unique identifier generation method, device, electronic device, storage medium and program product to allocate each distributed node with its own identifier to be allocated, so as to realize the generation of cheap and simple distributed unique identifiers.

[0006] The embodiment of the present application provides a unique identifier generation method, which is applied to a target node among multiple nodes in a distributed system, wherein the target node has distributed data, and the method includes:

[0007] Obtain the maximum value of the values ​​of the assigned identifiers from the database, and record it as the first value;

[0008] Executing an update command for the allocated identifier, wherein the update command is used to instruct that when it is determined that the allocated identifier has not been modified by other nodes other than the target node, the maximum value is updated to a second value, and the second value is determined according to the first value and a preset step size;

[0009] If the update command is executed successfully, determining an available identification interval according to the first value and the second value;

[0010] A unique identifier of an object in the distributed data is determined according to the available identifier interval.

[0011] Optionally, the number of the objects is multiple, the first value and the second value are integers, the unique identifier is an integer, and determining the unique identifier of the object in the distributed data according to the available identifier interval includes:

[0012] Determine a unique identifier of an i-th object in the distributed data; i is a self-increasing positive integer, and the unique identifier of the first object is the sum of the first value and a first preset value;

[0013] If the unique identifier of the i-th object is smaller than the second value, the sum of the unique identifier of the i-th object and the first preset value is used as the unique identifier of the (i+1)-th object.

[0014] Optionally, the assigned identifier corresponds to a target category, i is a sorting sequence number of objects in a target object group, and the target object group is a set of objects corresponding to the target category in the distributed data.

[0015] Optionally, the method further includes:

[0016] If the unique identifier of the i-th object is equal to the second value, the operation of obtaining the maximum value of the assigned identifiers from the database is returned.

[0017] Optionally, the update command is specifically used to indicate:

[0018] updating the maximum value from the first value to a second value; or,

[0019] When it is determined that the allocated identifier is locked by the target node, the maximum value is updated to a second value; when the allocated identifier is locked by the target node, the value of the allocated identifier cannot be modified by the other nodes.

[0020] Optionally, the method further includes:

[0021] When the target node is shut down, the fragment identification interval in the database is updated according to the remaining identification interval in the available identification interval, so that when the target node is reinitialized, the available identification interval is determined according to the fragment identification interval.

[0022] Optionally, the method further includes:

[0023] If the number of times the update command fails to execute exceeds a preset number, the preset step size is adjusted, and the operation of obtaining the maximum value of the assigned identifier from the database is returned to be executed.

[0024] Optionally, the numerical value of the allocated identifier is stored in a relational data table; and the distributed data includes distributed sub-tables.

[0025] The embodiment of the present application further provides a unique identifier generation device, which is applied to a target node among multiple nodes in a distributed system, wherein the target node has distributed data, and the device includes:

[0026] A maximum value obtaining unit, used to obtain the maximum value of the values ​​of the assigned identifiers from the database, recorded as a first value;

[0027] a command execution unit, configured to execute an update command for the allocated identifier, wherein the update command is configured to instruct that when it is determined that the allocated identifier has not been modified by any node other than the target node, the maximum value is updated to a second value, wherein the second value is determined according to the first value and a preset step size;

[0028] an identification interval determining unit, configured to determine an available identification interval according to the first value and the second value if the update command is executed successfully;

[0029] An identification determination unit is used to determine a unique identification of an object in the distributed data according to the available identification interval.

[0030] Optionally, the number of the objects is multiple, the first value and the second value are integers, the unique identifier is an integer, and the identifier determination unit includes:

[0031] a first determining unit, configured to determine a unique identifier of an i-th object in the distributed data; i is a self-increasing positive integer, and the unique identifier of the first object is the sum of the first value and a first preset value;

[0032] The second determining unit is configured to use the sum of the unique identifier of the i-th object and the first preset value as the unique identifier of the (i+1)-th object if the unique identifier of the i-th object is less than the second value.

[0033] Optionally, the assigned identifier corresponds to a target category, i is a sorting sequence number of objects in a target object group, and the target object group is a set of objects corresponding to the target category in the distributed data.

[0034] Optionally, the device further comprises:

[0035] The third determining unit is used to return to execute the operation of obtaining the maximum value of the assigned identifiers from the database if the unique identifier of the i-th object is equal to the second value.

[0036] Optionally, the update command is specifically used to indicate:

[0037] updating the maximum value from the first value to a second value; or,

[0038] When it is determined that the allocated identifier is locked by the target node, the maximum value is updated to a second value; when the allocated identifier is locked by the target node, the value of the allocated identifier cannot be modified by the other nodes.

[0039] Optionally, the device further comprises:

[0040] The fragment recovery unit is used to update the fragment identification interval in the database according to the remaining identification interval in the available identification interval when the target node is shut down, so as to determine the available identification interval according to the fragment identification interval when the target node is reinitialized.

[0041] Optionally, the device further comprises:

[0042] The compensation adjustment unit is used to adjust the preset step size if the number of times the update command fails to execute exceeds a preset number, and return to execute the operation of obtaining the maximum value of the assigned identifier from the database.

[0043] Optionally, the numerical value of the allocated identifier is stored in a relational data table; and the distributed data includes distributed sub-tables.

[0044] The present application also provides an electronic device, the electronic device comprising a processor and a memory:

[0045] The memory is used to store a computer program and transmit the computer program to the processor;

[0046] The processor is used to execute the unique identifier generation method according to the instructions in the computer program.

[0047] An embodiment of the present application also provides a computer-readable storage medium, wherein the computer-readable storage medium is used to store a computer program, and the computer program is used to execute the unique identifier generation method.

[0048] The embodiment of the present application also provides a computer program product including a computer program, which, when executed on a computer device, enables the computer device to execute the unique identifier generating method.

[0049] The embodiment of the present application provides a unique identification generation method, device, electronic device, storage medium and program product, the method is applied to a target node among multiple nodes in a distributed system, the target node has distributed data, the maximum value of the value of the assigned identification is obtained from the database and recorded as the first value, indicating that the identification corresponding to the value less than the first value has been occupied, and the unoccupied available identification interval can be determined accordingly. Execute an update command for the assigned identification, the update command is used to indicate that when it is determined that the assigned identification has not been modified by other nodes other than the target node, the maximum value of the value of the assigned identification is updated to a second value, the second value is determined according to the first value and the preset step length, the first value and the second value can determine the available identification interval, so that if the update command is executed successfully, it means that no other node updates the assigned identification, that is, the identification corresponding to the value greater than the first value is not occupied by other nodes, so the available identification interval is not occupied by other nodes, but is successfully occupied by the target node, that is, the available identification interval is an identification interval dedicated to the target node, which ensures the global uniqueness of the available identification interval, and the unique identification of the object in the distributed data can be determined based on the available identification interval, and the generated unique identification is also globally unique. That is to say, each node in the distributed system shares the maximum value of the assigned identifier in the database. This maximum value can ensure the global uniqueness of the available identifier interval, thereby ensuring the global uniqueness of the unique identifier generated for the object. That is, the unique identifier of distributed data can be determined with the help of the assigned identifier without relying on third-party components, thereby realizing the generation of cheap, simple, stable and reliable distributed unique identifiers, reducing costs, simplifying operating steps, and meeting the distributed data application needs in more situations. BRIEF DESCRIPTION OF THE DRAWINGS

[0050] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in the present application. For ordinary technicians in this field, other drawings can also be obtained based on these drawings.

[0051] Figure 1 A flowchart of a unique identifier generation method provided in an embodiment of the present application;

[0052] Figure 2 A schematic diagram of an identification generation record information provided in an embodiment of the present application;

[0053] Figure 3 A schematic diagram of the attributes of each field in the identification generation record information in an embodiment of the present application;

[0054] Figure 4 A schematic diagram of a specific process for generating a unique identifier provided in an embodiment of the present application;

[0055] Figure 5 A structural block diagram of a unique identifier generation device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0056] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.

[0057] The specific implementation of a unique identifier generation method and device provided in the embodiments of the present application is described in detail below with reference to the accompanying drawings.

[0058] refer to Figure 1 As shown, it is a flowchart of a unique identifier generation method provided in an embodiment of the present application. The method can be applied to a target node among multiple nodes in a distributed system. It can be implemented in any programming language. The method includes the following steps.

[0059] S101, obtaining the maximum value of the values ​​of the allocated identifiers from a database, and recording it as a first value.

[0060] In an embodiment of the present application, the value of the assigned identifier can be recorded. Specifically, the value of the assigned identifier can be recorded by an identifier generation record. The value of the assigned identifier is used to reflect the distribution of the value of the unique identifier in the distributed system. If the value of the unique identifier is assigned to a node in the distributed system, the node has the right to use the value of the unique identifier, and the corresponding other nodes will no longer use the value of the unique identifier. The unique identifier can be a numerical identifier, in which case the value of the unique identifier is the unique identifier itself; the unique identifier can also be a comprehensive identifier including a numerical value, in which case the value of the unique identifier is the numerical value included in the unique identifier. The distributed system can include multiple distributed nodes, and any of the multiple distributed nodes can be used as a target node. The distributed node is, for example, a server.

[0061] The value of the assigned identifier may include the maximum value (max_id) of the assigned identifier, so that a node that modifies the maximum value of the assigned identifier from a first value to a second value is equivalent to being assigned a unique identifier between the first value and the second value.

[0062] The assigned identifier can correspond to a category in different categories (business_type), that is, the unique identifiers corresponding to data of different categories can be determined separately, so that data of different categories can be numbered separately. Data of different categories are usually processed separately, so they may not have unique identifiers. Separate numbering of distributed data of different categories can reduce unnecessary accumulation of unique identifiers. Different categories can also be called different business scenarios, such as order category (ORDER), product category (PRODUCT) and material category (MATERIAL). Any category in the different categories is taken as the target category, and the target category and the assigned identifier corresponding to the target category will be used as the research object for explanation later. For example, the target category is the order category (ORDER).

[0063] The values ​​of the assigned identifiers may be stored in a relational data table. When the values ​​of the assigned identifiers are recorded by using the identifier generation record information, the identifier generation record information may be embodied in the form of a relational data table in a relational database, which is called an identifier generation record table.

[0064] refer to Figure 2 FIG. 1 is a schematic diagram of an identification generation record information provided in an embodiment of the present application, with reference to FIG. Figure 3 As shown, it is a schematic diagram of the attributes of each field in the identification generation record information in the embodiment of the present application. The record identifier (id) is an auto-increment primary key, which is used to indicate the numerical value of the assigned identifier corresponding to different categories (for example, the maximum value of the numerical value of the assigned identifier under different categories). The record identifier is actually used to distinguish different categories (business_type). The identification generation record information can also include the update time (modified_time) and creation time (create_time) of the numerical value of each assigned identifier, and can also include the logical deletion identifier (deleted) of the numerical value of each assigned identifier, which is used to indicate whether it is logically deleted, and 0 means that it is not logically deleted. The identification generation record can also include a default step (step) to indicate the number of unique identifiers obtained in a single time. Among them, the data types of the record identifier, maximum value and default step are large integers (i.e., bigint), the data type of the logical deletion identifier is a micro integer (i.e., tinyint), the data type of the category is a variable string (varchar), and the data types of the update time and creation time are date and time types (datatime).

[0065] refer to Figure 2 As shown, taking the order category (ORDER) as an example, its corresponding record identifier (id) is 1, the maximum value of the assigned identifier (max_id) is 142180075, and the default step size (step) is 10000.

[0066] In an embodiment of the present application, when the target node needs to obtain a unique identifier, it can obtain the maximum value of the assigned identifier from the database, recorded as the first value, for example, obtain the maximum value of the assigned identifier whose category is the target category (query the value of max_id that satisfies business_type=ORDER (for example, 1000), the first value is recorded as lastMaxId, then lastMaxId=1000), recorded as the first value, indicating that the identifier corresponding to the value less than the first value has been occupied, and the unoccupied available identifier interval can be determined based on this, that is, the target node can obtain the right to use the unique identifier whose value is greater than the first value. The first value can be an integer. For example, if the maximum value of the assigned identifier corresponding to the target category is 1000, it means that the unique identifier with a value of 0-1000 has been allocated, and the target node can obtain a value above 1000 to determine the unique identifier.

[0067] S102, executing an update command for the allocated identifier, the update command is used to indicate that when it is determined that the allocated identifier has not been modified by other nodes other than the target node, the maximum value of the value of the allocated identifier is updated to a second value, and the second value is determined according to the first value and a preset step size.

[0068] After obtaining the maximum value of the assigned identifier, the maximum value can be recorded as the first value, and the second value can be determined based on the first value, for example, the second value can be determined based on the first value and the preset step length, and the second value can be the sum of the first value and the preset step length. In this way, the first value and the second value constitute an identifier interval with a length of the preset step length, and when the target node successfully obtains the identifier interval, it can use the value in the identifier interval to determine the unique identifier.

[0069] The preset step size may be a default step size in the identification generation record information, or may be obtained by adjusting the default step size in the identification generation record information, or may be a step size determined by the target node according to other policies. The preset step size may be an integer, and when the first value is an integer, the second value is also an integer.

[0070] In the embodiment of the present application, an update command for the allocated identifier can be executed, and the update command is used to indicate that when it is determined that the allocated information has not been modified by other nodes other than the target node, the maximum value of the value of the allocated identifier is updated to the second value. That is, the second value can be updated to the relational data table, which is equivalent to obtaining the identifier interval determined by the first value and the second value.

[0071] That is to say, under the condition of ensuring the uniqueness of the modification of the allocated identifier, the target node can update the maximum value of the value of the allocated identifier from the first value to the second value, which means that the unique identifier with a value less than the second value has been allocated, and other nodes cannot obtain the unique identifier less than the second value, so that the identifier interval composed of the first value and the second value belongs exclusively to the target node. In this way, if the update command is executed successfully, it means that no other node has updated the allocated identifier, that is, the identifier corresponding to the value greater than the first value is not occupied by other nodes, so the available identifier interval is not occupied by other nodes, but is successfully occupied by the target node.

[0072] When it is determined that the assigned identifier has not been modified by other nodes other than the target node, the assigned identifier is updated, which can be achieved by optimistic locking or pessimistic locking to ensure the uniqueness of the identifier interval. The setting of optimistic locking or pessimistic locking can be determined based on the attributes of the identifier generation record information.

[0073] In the case of being implemented by optimistic locking, before updating the maximum value of the value of the allocated identifier, it can be checked whether the maximum value of the value of the allocated identifier is the first value. If so, it means that it has not been modified by other nodes, and the allocated identifier can be updated. If not, it means that it has been modified by other nodes, and the allocated identifier cannot be updated at this time, so that multiple nodes can modify the allocated identifier separately. Specifically, the maximum value of the value of the allocated identifier can be updated from the first value to the second value, that is, when it is determined that the maximum value of the value of the allocated identifier is the first value, the maximum value of the value of the allocated identifier is updated to the second value. In this way, the competition problem of distributed multiple nodes can be solved by the method of Compare And Swap (CAS) without locking the database.

[0074] Taking the allocated identifier corresponding to the target category as an example, the preset step size is step, the first value is recorded as lastMaxId, the second value is recorded as lastMaxId+step, and the identifier generation record table is represented by table_id_factory. Then, in the identifier generation record table, when the maximum value of the allocated identifier of the target category (for example, ORDER) in the category (business_type) is the first value (the value of max_id is the same as the value of lastMaxId), the maximum value of the allocated identifier is updated to the second value.

[0075] In the case of implementing by means of pessimistic locking, the target node can lock the allocated identifier when obtaining the maximum value of the value of the allocated identifier, so that it cannot be modified by other nodes, and unlock the allocated identifier after updating the allocated identifier, so that other nodes can modify the allocated identifier after the target node is updated, so that multiple nodes can sequentially modify the maximum value of the value of the allocated identifier. Therefore, when it is determined that the allocated identifier is locked by the target node, the maximum value of the value of the allocated identifier can be updated to the second value, and when the allocated identifier is locked by the target node, the value of the allocated identifier cannot be modified by other nodes. Of course, if the allocated identifier is not locked by the target node, it can be locked by other nodes, and the target node does not update the allocated identifier.

[0076] The update command indicates that the maximum value of the numerical value of the assigned identifier is modified from the first value to the second value. The programming language of the update command can be expressed as:

[0077] UPDATE table_id_factory SET max_id=max_id+step WHERE business_type=ORDER AND max_id=lastMaxId.

[0078] When lastMaxId=1000 and step is 1000, the above programming language is expressed as:

[0079] UPDATE table_id_factory SET max_id=max_id+1000 WHERE business_type=ORDER AND max_id=1000.

[0080] S103: If the update command is executed successfully, determine the available identification interval according to the first value and the second value.

[0081] In an embodiment of the present application, when each node in a distributed system shares identification generation record information, if the update command is executed successfully, it means that no other node has updated the allocated identification, that is, the identification corresponding to the numerical value greater than the first value is not occupied by other nodes, and therefore the available identification interval is not occupied by other nodes, but is successfully occupied by the target node, that is, the available identification interval is an identification interval dedicated to the target node, which ensures the global uniqueness of the available identification interval, and the unique identification of the object in the distributed data can be determined based on the available identification interval, and the generated unique identification is also globally unique.

[0082] Taking the assigned identifier corresponding to the target category as an example, the first value is recorded as lastMaxId, the preset step length is recorded as step, and the second value is recorded as lastMaxId+step. The available identifier interval determined based on the first value and the second value is lastMaxId~(lastMaxId+step). For example, if the first value is 1000 and the preset step length is 1000, the second value is 2000, that is, the available identifier interval is 1001~2000.

[0083] The available identification interval can be stored in the local memory of the target node for use when the unique identification of the target node is determined. The available identification interval can be identified by the interval lower limit value and the interval upper limit value. The interval lower limit value is the first value (lastMaxId), and the interval upper limit value is recorded as the local maximum identification (localMaxId), which can be the second value (lastMaxId+step), that is, localMaxId=lastMaxId+step. Set the initial value of the current identification to the first value, and the current identification can be recorded as start, that is, before the unique identification is determined, start=lastMaxId.

[0084] If the update command fails to execute, it means that the maximum value of the value of the allocated identifier has been modified by other nodes, for example, locked by other nodes, or the allocated identifier has been modified to other values ​​instead of the first value read, so the acquisition of the identification interval of the first value and the second value fails. You can return to execute S101 and re-acquire a new first value, a new second value and a new identification interval.

[0085] If the update command fails to execute more than the preset number of times, it means that there are always other nodes that obtain the identification interval at the same time as the target node. It may be because the length of the identification interval obtained by multiple nodes is the same, which causes multiple nodes to usually use up the identifiers in the identification interval at the same time. At this time, the preset step size can be adjusted, and return to execute S101, so that the unique identifier obtained by the target node each time is different from that of other nodes. Therefore, the length of time for exhausting the identifier in the interval is different from that of other nodes. Then, when the identification interval is obtained next time, the time when other nodes obtain the identification interval can be avoided, and the peak application of the identification interval can be realized, wherein the adjustment of the preset step size can be to increase the preset step size or to reduce the preset step size. In this way, the identification interval can be effectively obtained even in a high concurrency scenario. The preset number of times can be determined according to the actual situation, for example, it can be 1, 2, 3, etc.

[0086] S104: Determine a unique identifier of an object in the distributed data according to the available identifier interval.

[0087] When the available identification interval is determined in the aforementioned manner and the uniqueness of the available identification interval between each node is guaranteed, the target node can determine the unique identification based on the available identification interval, that is, the unique identification of the object in the distributed data can be determined, for example, the numerical value in the available identification interval is used as the unique identification of the object, or the numerical value in the available identification interval and other identifications are combined as the unique identification of the object.

[0088] In the process of determining the unique identifier of an object in distributed data, as long as the local uniqueness of the unique identifier is guaranteed, the global uniqueness of the unique identifier can be guaranteed. In this way, each node in the distributed system shares the maximum value of the assigned identifier in the database. The global uniqueness of the available identifier interval can be guaranteed through this maximum value, thereby ensuring the global uniqueness of the unique identifier generated for the object. That is, the unique identifier of distributed data can be determined with the help of the assigned identifier without relying on third-party components, thereby realizing the generation of cheap, simple, stable and reliable distributed unique identifiers, reducing costs, simplifying operating steps, and meeting the distributed data application needs in more situations.

[0089] Specifically, the target node may have distributed data, which is obtained by segmenting the total data and is assigned to the target node for processing. The distributed data is also unique between each node. The distributed data is, for example, a distributed sub-table.

[0090] Distributed data may have multiple objects, such as multiple records in a distributed sub-table. When the number of objects is multiple, the first value and the second value are integers, and the unique identifier is also an integer, the unique identifier of the object can be determined in the following manner: determine the unique identifier of the i-th object in the distributed data. If the unique identifier of the i-th object is less than the second value, it means that the used unique identifier is not the maximum value of the available identifier interval, and the unique identifier in the available identifier interval has not been used up. Then the sum of the unique identifier of the i-th object and the first preset value is used as the unique identifier of the i+1-th object, so that the unique identifier of each object can be determined in turn. Wherein i is a self-increasing positive integer, and the unique identifier of the first object is the sum of the first value and the first preset value. Wherein, the first preset value can be 1 or other values. When the first preset value is 1, the unique identifier covers each integer in the available identifier interval, and when the first preset value is 2, the unique identifier covers each even number or each odd number in the available identifier interval. In this way, the unique identifier can be determined locally by CAS.

[0091] In the process of determining a unique identifier for an object, i is incremented, and for each i, the unique identifier of the i-th object can be used as the current identifier. The new current identifier is determined based on the sum of the current identifier and the first preset value. Specifically, the unique identifier corresponding to i before the increment is used as the current identifier, and the unique identifier (id) corresponding to i after the increment is determined based on the sum of the current identifier (start) and the first preset value. The unique identifier (id) corresponding to i after the increment is used as the new current identifier (start), and the first preset value is represented as n. It can be incremented by CAS. For each i, the unique identifier can be determined by assigning id=start+n and start=id twice. When the first preset value is 1, the aforementioned assignment process can be expressed as id=start+1 (id is assigned to start+1), start=id.

[0092] Taking the first preset value of 1 as an example, i starts to increment from 1. When i is 1, the unique identifier (id) of the first object is the sum of the first value and 1, which is expressed as id=start+1 (id is assigned to start+1), that is, lastMaxId+1. At this time, the current identifier is updated to the unique identifier of the first object, that is, start=id (start is assigned to the aforementioned id), that is, lastMaxId+1; i increments to 2, and the unique identifier of the second object is the sum of the current identifier and 1. At this time, the current identifier is updated, and the unique identifier of the second object is expressed as id=start+1, that is, lastMaxId+2. The current identifier is updated to the unique identifier of the second object, that is, start=id, that is, lastMaxId+2; and so on, until start==localMaxId (the current identifier is equal to the local maximum identifier, that is, the second value).

[0093] Among them, when the assigned identifier corresponds to the target category, i is the sorting number of the object in the target object group in the distributed data, and the target object group is the set of objects corresponding to the target category in the distributed data, so that objects of different categories can be sorted separately, thereby determining the unique identifiers of objects of different categories respectively. That is, the i-th object in the target category means that the ranking is i among all objects corresponding to the target category, so that the unique identifier of each object in the target category can be determined.

[0094] In addition, if the unique identifier of the i-th object is equal to the second value (i.e., start == localMaxId), it means that all unique identifiers in the available identifier interval are used, so the unique identifier cannot be determined based on the available identifier interval, and a new identifier interval needs to be obtained. Then, it is possible to return to execute S101, that is, to re-obtain the maximum value of the allocated value, so as to re-determine the new first value, the new second value, and the new available identifier interval. The re-determination process is consistent with the above process and will not be repeated.

[0095] In an embodiment of the present application, after the target node determines the available identification interval, if the available identification interval is used up, a new identification interval can be obtained. If the available identification interval is not used up and the target node needs to be shut down, the fragment identification interval in the database can be updated according to the remaining identification interval in the available identification interval when the target node is shut down, so as to realize the recycling of the fragment identification interval. The lower limit of the remaining identification interval is the sum of the unique identification of the i-th object corresponding to the current i value and the first preset value (for example, 1), and the upper limit is the second value. For example, for the available representation interval of 1001 to 2000, the current i value is 400, and the unique identification of the i-th object is 1400, then the lower limit of the remaining identification interval is 1401, and the upper limit is 2000, that is, the remaining identification interval is 1401 to 2000.

[0096] The fragment identification interval is used to record the unused identification interval. The fragment identification intervals of each node can be recorded uniformly without distinction, so that the fragment identification intervals can be reallocated to each node, and the target node can apply for an available identification interval from the fragment identification interval; or the fragment identification interval of each node can have a node identifier, so that the target node obtains the fragment identification interval corresponding to the target node identifier, that is, the remaining identification interval of the target node is waiting for the target node to use it again. In this way, when the target node is reinitialized, the available identification interval can be determined based on the fragment identification interval, so that the identification interval fragments can be used first, reducing the unnecessary accumulation of unique identifiers.

[0097] The embodiment of the present application provides a unique identifier generation method, which is applied to a target node among multiple nodes in a distributed system, the target node has distributed data, and the maximum value of the value of the assigned identifier is obtained from the database and recorded as the first value, indicating that the identifier corresponding to the value less than the first value has been occupied, and the unoccupied available identifier interval can be determined accordingly. An update command for the assigned identifier is executed, and the update command is used to indicate that when it is determined that the assigned identifier has not been modified by other nodes other than the target node, the maximum value of the value of the assigned identifier is updated to a second value, and the second value is determined according to the first value and a preset step length. The first value and the second value can determine the available identifier interval, so that if the update command is executed successfully, it means that no other node updates the assigned identifier, that is, the identifier corresponding to the value greater than the first value is not occupied by other nodes, so the available identifier interval is not occupied by other nodes, but is successfully occupied by the target node, that is, the available identifier interval is an identifier interval dedicated to the target node, which ensures the global uniqueness of the available identifier interval, and the unique identifier of the object in the distributed data can be determined based on the available identifier interval, and the generated unique identifier is also globally unique. That is to say, each node in the distributed system shares the maximum value of the assigned identifier in the database. This maximum value can ensure the global uniqueness of the available identifier interval, thereby ensuring the global uniqueness of the unique identifier generated for the object. That is, the unique identifier of distributed data can be determined with the help of the assigned identifier without relying on third-party components, thereby realizing the generation of cheap, simple, stable and reliable distributed unique identifiers, reducing costs, simplifying operating steps, and meeting the distributed data application needs in more situations.

[0098] Based on the unique identifier generation method provided in the above embodiment, the embodiment of the present application also provides a specific process for generating a unique identifier, taking the assigned identifier corresponding to the target category as an example, referring to Figure 4 As shown, it is a schematic diagram of a specific process of generating a unique identifier provided in an embodiment of the present application. When an application for an identifier space is required, the following steps can be performed:

[0099] 201. First query the database for the maximum value of the assigned identifier whose category (business_type) is the target category (eg, ORDER), that is, query the value of max_id (eg, 1000) satisfying business_type=ORDER. The first value is recorded as lastMaxId, and then lastMaxId=1000.

[0100] 202. Execute an update command for the allocated identifier, where the update command indicates that the maximum value of the value of the allocated identifier is modified from the first value to the second value. The programming language of the update command can be expressed as:

[0101] UPDATE table_id_factory SET max_id=max_id+step WHERE business_type=ORDER AND max_id=lastMaxId.

[0102] When lastMaxId=1000 and step is 1000, the above programming language is expressed as:

[0103] UPDATE table_id_factory SET max_id=max_id+1000 WHERE business_type=ORDER AND max_id=1000.

[0104] If the update command is executed successfully, execute S203; if the update command fails to execute, it means that the allocated identifier is modified by other nodes and the available identifier interval acquisition fails, and it can return to execute 201 to re-acquire the available identifier interval.

[0105] 203. If the update command is executed successfully, the available identification interval is determined, and the available identification interval can be stored in the local memory of the target node for use when the unique identification of the target node is determined. The available identification interval can be identified by the interval lower limit value and the interval upper limit value. The interval lower limit value is the first value (lastMaxId), and the interval upper limit value is recorded as the local maximum identification (localMaxId), which can be the second value (lastMaxId+step), that is, localMaxId=lastMaxId+step. Set the initial value of the current identification to the first value, and the current identification can be recorded as start, that is, before the unique identification is determined, start=lastMaxId.

[0106] 204. In the process of determining a unique identifier for an object, i is incremented, and for each i, the unique identifier of the i-th object can be used as the current identifier.

[0107] 205. Determine a new current identifier based on the sum of the current identifier and the first preset value. Specifically, take the unique identifier corresponding to i before the self-increment as the current identifier, determine the unique identifier (id) corresponding to i after the self-increment based on the sum of the current identifier (start) and the first preset value, and take the unique identifier (id) corresponding to i after the self-increment as the new current identifier (start). The first preset value is represented as n, and can be self-incremented by CAS. For each i, the unique identifier can be determined by assigning id=start+n and start=id twice, until start==localMaxId (the current identifier is equal to the local maximum identifier, that is, the second value). When the first preset value is 1, the above assignment process can be expressed as id=start+1 (id is assigned to start+1), start=id.

[0108] If start == localMaxId (the current identifier is equal to the local maximum identifier, ie, the second value), it means that the available identifier interval in the local memory has been exhausted and the latest interval needs to be obtained again. The process may return to execute 201 to obtain a new identifier interval.

[0109] Based on the above unique identifier generation method, the present application embodiment also provides a unique identifier generation device, referring to Figure 5 As shown, it is a structural block diagram of a unique identifier generation device provided in an embodiment of the present application, wherein the device is applied to a target node among multiple nodes in a distributed system, and the target node has distributed data, including:

[0110] A maximum value obtaining unit 110 is used to obtain the maximum value of the values ​​of the assigned identifiers from the database, which is recorded as a first value;

[0111] A command execution unit 120, configured to execute an update command for the allocated identifier, wherein the update command is used to instruct that when it is determined that the allocated identifier has not been modified by other nodes other than the target node, the maximum value is updated to a second value, wherein the second value is determined according to the first value and a preset step size;

[0112] an identification interval determining unit 130, configured to determine an available identification interval according to the first value and the second value if the update command is executed successfully;

[0113] The identifier determination unit 140 is configured to determine a unique identifier of an object in the distributed data according to the available identifier interval.

[0114] Optionally, the number of the objects is multiple, the first value and the second value are integers, the unique identifier is an integer, and the identifier determination unit includes:

[0115] a first determining unit, configured to determine a unique identifier of an i-th object in the distributed data; i is a self-increasing positive integer, and the unique identifier of the first object is the sum of the first value and a first preset value;

[0116] The second determining unit is configured to use the sum of the unique identifier of the i-th object and the first preset value as the unique identifier of the (i+1)-th object if the unique identifier of the i-th object is less than the second value.

[0117] Optionally, the assigned identifier corresponds to a target category, i is a sorting sequence number of objects in a target object group, and the target object group is a set of objects corresponding to the target category in the distributed data.

[0118] Optionally, the device further comprises:

[0119] The third determining unit is used to return to execute the operation of obtaining the maximum value of the assigned identifiers from the database if the unique identifier of the i-th object is equal to the second value.

[0120] Optionally, the update command is specifically used to indicate:

[0121] updating the maximum value from the first value to a second value; or,

[0122] When it is determined that the allocated identifier is locked by the target node, the maximum value is updated to a second value; when the allocated identifier is locked by the target node, the value of the allocated identifier cannot be modified by the other nodes.

[0123] Optionally, the device further comprises:

[0124] The fragment recovery unit is used to update the fragment identification interval in the database according to the remaining identification interval in the available identification interval when the target node is shut down, so as to determine the available identification interval according to the fragment identification interval when the target node is reinitialized.

[0125] Optionally, the device further comprises:

[0126] The compensation adjustment unit is used to adjust the preset step size if the number of times the update command fails to execute exceeds a preset number, and return to execute the operation of obtaining the maximum value of the assigned identifier from the database.

[0127] Optionally, the numerical value of the allocated identifier is stored in a relational data table; and the distributed data includes distributed sub-tables.

[0128] The present application also provides an electronic device, the electronic device comprising a processor and a memory:

[0129] The memory is used to store a computer program and transmit the computer program to the processor;

[0130] The processor is used to execute the unique identifier generation method according to the instructions in the computer program.

[0131] An embodiment of the present application also provides a computer-readable storage medium, wherein the computer-readable storage medium is used to store a computer program, and the computer program is used to execute the unique identifier generation method.

[0132] The embodiment of the present application also provides a computer program product including a computer program, which, when executed on a computer device, enables the computer device to execute the unique identifier generating method.

[0133] Through the description of the above implementation methods, it can be known that those skilled in the art can clearly understand that all or part of the steps in the above-mentioned embodiment method can be implemented by means of software plus a general hardware platform. Based on such an understanding, the technical solution of the present application can be embodied in the form of a software product, which can be stored in a storage medium, such as a read-only memory (ROM) / RAM, a magnetic disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network communication device such as a router) to execute the methods described in each embodiment of the present application or some parts of the embodiments.

[0134] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can refer to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can refer to the partial description of the method embodiment. The equipment and system embodiments described above are merely schematic, in which the modules described as separate components may or may not be physically separated, and the components displayed as modules may or may not be physical modules, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules can be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative work.

[0135] The above is only a preferred embodiment of the present application and is not intended to limit the protection scope of the present application. It should be noted that, for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the present application, and these improvements and modifications should also be regarded as the protection scope of the present application.

Claims

1. A method for generating a unique identifier, characterized in that: Applied to a target node among a plurality of nodes in a distributed system, the target node having distributed data, the method comprising: Obtain the maximum value of the values ​​of the assigned identifiers from the database, and record it as the first value; Executing an update command for the allocated identifier, wherein the update command is used to instruct that when it is determined that the allocated identifier has not been modified by other nodes other than the target node, the maximum value is updated to a second value, and the second value is determined according to the first value and a preset step size; If the update command is executed successfully, determining an available identification interval according to the first value and the second value; A unique identifier of an object in the distributed data is determined according to the available identifier interval.

2. The method according to claim 1, characterized in that The number of the objects is multiple, the first value and the second value are integers, the unique identifier is an integer, and determining the unique identifier of the object in the distributed data according to the available identifier interval includes: Determine a unique identifier of an i-th object in the distributed data; i is a self-increasing positive integer, and the unique identifier of the first object is the sum of the first value and a first preset value; If the unique identifier of the i-th object is smaller than the second value, the sum of the unique identifier of the i-th object and the first preset value is used as the unique identifier of the (i+1)-th object.

3. The method according to claim 2, characterized in that The assigned identifier corresponds to a target category, the i is a sorting sequence number of objects in a target object group, and the target object group is a set of objects corresponding to the target category in the distributed data.

4. The method according to claim 2, characterized in that: The method further comprises: If the unique identifier of the i-th object is equal to the second value, the operation of obtaining the maximum value of the assigned identifiers from the database is returned.

5. The method according to any one of claims 1 to 4, characterized in that: The update command is specifically used to indicate: updating the maximum value from the first value to a second value; or, When it is determined that the allocated identifier is locked by the target node, updating the maximum value to a second value; When the allocated identifier is locked by the target node, the value of the allocated identifier cannot be modified by the other nodes.

6. The method according to any one of claims 1 to 4, characterized in that: The method further comprises: When the target node is shut down, the fragment identification interval in the database is updated according to the remaining identification interval in the available identification interval, so that when the target node is reinitialized, the available identification interval is determined according to the fragment identification interval.

7. The method according to any one of claims 1 to 4, characterized in that: The method further comprises: If the number of times the update command fails to execute exceeds a preset number, the preset step size is adjusted, and the operation of obtaining the maximum value of the assigned identifier from the database is returned to be executed.

8. The method according to any one of claims 1 to 4, characterized in that: The values ​​of the assigned identifiers are stored in a relational data table; the distributed data includes distributed sub-tables.

9. A unique identifier generating device, characterized in that: Applied to a target node among a plurality of nodes in a distributed system, the target node having distributed data, the device comprising: A maximum value obtaining unit, used to obtain the maximum value of the values ​​of the assigned identifiers from the database, recorded as a first value; a command execution unit, configured to execute an update command for the allocated identifier, wherein the update command is configured to instruct that when it is determined that the allocated identifier has not been modified by any node other than the target node, the maximum value is updated to a second value, wherein the second value is determined according to the first value and a preset step size; an identification interval determining unit, configured to determine an available identification interval according to the first value and the second value if the update command is executed successfully; An identification determination unit is used to determine a unique identification of an object in the distributed data according to the available identification interval.

10. An electronic device, characterized in that: The electronic device comprises a processor and a memory: The memory is used to store a computer program and transmit the computer program to the processor; The processor is used to execute the unique identifier generation method described in any one of claims 1-8 according to the instructions in the computer program.

11. A computer-readable storage medium, characterized in that: The computer-readable storage medium is used to store a computer program, and the computer program is used to execute the unique identifier generation method described in any one of claims 1-8.

12. A computer program product comprising a computer program, characterized in that When it is run on a computer device, the computer device executes the unique identifier generation method described in any one of claims 1 to 8.

Citation Information

Cited By

  • User management method and device

    CN121389089A