A globally unique identifier generation method and device, electronic equipment and storage medium
By using the Raft algorithm to generate machine identifiers in a distributed system, and combining sequence and table partitioning identifiers, the resource waste caused by relying on third-party middleware is solved, achieving high-performance, globally unique identifier generation, which is suitable for distributed systems.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- BEIJING QIYI CENTURY SCI & TECH CO LTD
- Filing Date
- 2022-12-28
- Publication Date
- 2026-05-05
AI Technical Summary
In complex distributed systems, existing technologies for generating globally unique identifiers rely on third-party middleware, leading to excessive resource deployment and failing to meet the needs of large-scale data scenarios.
The Raft algorithm is used to generate machine identifiers for the target data, and random sequence and table identifiers are generated. Globally unique identifiers are generated based on these identifiers to avoid dependence on third-party software.
It generates high-performance, globally unique, and highly available identifiers with minimal resource deployment, offering fast response times, simple design, and easy integration.
Smart Images

Figure CN115983201B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of distributed systems technology, and in particular to a method, apparatus, electronic device, and storage medium for generating globally unique identifiers. Background Technology
[0002] In complex distributed systems, it is often necessary to uniquely identify large amounts of data and messages. Distributed systems typically contain multiple business systems, and the data within these systems is constantly growing. Therefore, generating globally unique identifiers is a pressing issue that needs to be addressed.
[0003] The current main solution is to use an auto-incrementing sequence in the database. However, this method has a limited capacity and cannot meet the needs of scenarios with large amounts of data. As a result, various methods for generating identifiers that rely on third-party middleware have emerged. For example, globally unique identifiers can be generated using MySQL (My Structured Query Language) and the Snowflake algorithm, or using ZooKeeper (a distributed application coordination service) and the Snowflake algorithm. However, since these methods rely on third-party software, which requires additional resources, they require a significant amount of resources to generate identifiers. Summary of the Invention
[0004] The purpose of this invention is to provide a method, apparatus, electronic device, and storage medium for generating globally unique identifiers, so as to generate globally unique identifiers with fewer resources. The specific technical solution is as follows:
[0005] In a first aspect of this invention, a method for generating a globally unique identifier is provided, the method comprising:
[0006] Receive a request to generate a globally unique identifier for the target data;
[0007] The Raft algorithm is used to generate the machine identifier of the machine to which the target data belongs;
[0008] Randomly generate sequence identifiers and table partition identifiers;
[0009] Based on the machine identifier, the sequence identifier, and the table partition identifier, a globally unique identifier for the target data is generated.
[0010] Optionally, generating the machine identifier of the machine to which the target data belongs using the Raft algorithm includes:
[0011] Generate a machine identifier array;
[0012] Read the message storage file to get the currently allocated numbers;
[0013] Based on the assigned numbers, traverse the machine identifier array to obtain unassigned numbers, and use the unassigned numbers as unassigned machine identifiers.
[0014] Optionally, the method further includes:
[0015] Record the unassigned numbers into the message storage file.
[0016] Optionally, before generating the machine identifier array, the following steps are included:
[0017] One node is elected from among multiple nodes to serve as the leader node.
[0018] Optionally, the method further includes:
[0019] Set the sign bit;
[0020] Generate a timestamp for the target data based on a preset time and the time the request is received;
[0021] A globally unique identifier for the target data is generated based on the symbol bit, the timestamp, the machine identifier, the sequence identifier, and the table partition identifier.
[0022] In a second aspect of the present invention, a globally unique identifier generation apparatus is also provided, the apparatus comprising:
[0023] The request receiving module is used to receive requests for generating a globally unique identifier for the target data.
[0024] The machine identifier generation module is used to generate the machine identifier of the machine to which the target data belongs using the Raft algorithm;
[0025] The sequence identifier and table partition identifier generation module is used to randomly generate sequence identifiers and table partition identifiers;
[0026] The first globally unique identifier generation module is used to generate a globally unique identifier for the target data based on the machine identifier, the sequence identifier, and the table partition identifier.
[0027] Optionally, the machine identifier generation module includes:
[0028] The machine identifier array generation unit is used to generate the machine identifier array;
[0029] The message storage file reading unit is used to read the message storage file and obtain the currently allocated numbers;
[0030] An unassigned machine identifier generation unit is used to traverse the machine identifier array based on the assigned numbers, obtain unassigned numbers, and use the unassigned numbers as unassigned machine identifiers.
[0031] Optionally, the machine identifier generation module is further used for:
[0032] Record the unassigned numbers into the message storage file.
[0033] Optionally, the machine identifier generation module further includes:
[0034] The leader node election unit is used to elect a leader node from among multiple nodes.
[0035] Optionally, the device further includes:
[0036] The sign bit setting module is used to set the sign bit;
[0037] The timestamp generation module is used to generate a timestamp for the target data based on a preset time and the time the request is received.
[0038] The second globally unique identifier generation module is used to generate a globally unique identifier for the target data based on the symbol bit, the timestamp, the machine identifier, the sequence identifier, and the table partition identifier.
[0039] In another aspect of the present invention, a computer-readable storage medium is also provided, wherein a computer program is stored therein, and when the computer program is executed by a processor, it implements any of the globally unique identifier generation methods described above.
[0040] In another aspect of the present invention, a computer program product containing instructions is also provided, which, when run on a computer, causes the computer to execute any of the globally unique identifier generation methods described above.
[0041] This invention provides a method, apparatus, electronic device, and storage medium for generating globally unique identifiers. The method involves receiving a request to generate a globally unique identifier for target data; generating a machine identifier for the machine to which the target data belongs using the Raft algorithm; randomly generating a sequence identifier and a partition identifier; and generating a globally unique identifier for the target data based on the machine identifier, the sequence identifier, and the partition identifier. Since the Raft algorithm is used to generate the machine identifier for the machine to which the target data belongs, and thus the globally unique identifier is generated, no third-party software is required, thus eliminating the need for additional resource deployment and enabling the generation of globally unique identifiers with minimal resource usage. Attached Figure Description
[0042] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the accompanying drawings used in the description of the embodiments or the prior art will be briefly introduced below.
[0043] Figure 1 This is a flowchart of the first globally unique identifier generation method provided in the embodiments of the present invention;
[0044] Figure 2 This is a flowchart of the second globally unique identifier generation method provided in this embodiment of the invention;
[0045] Figure 3 This is a schematic diagram of a globally unique identifier field provided in an embodiment of the present invention;
[0046] Figure 4 This is a flowchart of a method for generating globally unique identifiers based on the Raft algorithm and the Snowflake algorithm, provided in an embodiment of the present invention.
[0047] Figure 5 This is a schematic diagram of a globally unique identifier generation device provided in an embodiment of the present invention;
[0048] Figure 6 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present invention. Detailed Implementation
[0049] The technical solutions of the present invention will now be described with reference to the accompanying drawings in the embodiments of the present invention.
[0050] This invention provides a method for generating globally unique identifiers. The method can be executed by any node in a distributed system, specifically a server, client terminal, desktop computer, laptop, smartphone, or tablet. See also... Figure 1 , Figure 1 The method shown specifically includes:
[0051] S110, Receive the global unique identifier generation request for the target data.
[0052] The target data can be any kind of data, such as video data, text data, audio data, message data, etc.
[0053] S120, use the Raft algorithm to generate the machine identifier of the machine to which the target data belongs.
[0054] The Raft algorithm breaks down the consensus algorithm into several key modules, such as leader election, log replication, and security. Alternatively, other consensus algorithms, such as Paxos, can also be used. Compared to Paxos, Raft reduces the number of states to consider by implementing a stronger consensus mechanism.
[0055] The Raft algorithm is used to generate an unassigned machine identifier, which is then used as the machine identifier of the machine to which the target data belongs. This ensures the uniqueness of the machine identifier of the machine to which the target data belongs, and in turn, guarantees the uniqueness of the generated globally unique identifier.
[0056] S130, randomly generate sequence identifiers and table partition identifiers.
[0057] Sequence identifiers are used to generate different identifiers for different data during concurrent data processing, thereby ensuring the uniqueness of each data's globally unique identifier. For example, if concurrent data occurs on the same machine at the same time, sequence identifiers can generate different sequence identifiers for different data on the same machine at the same time, thus generating different globally unique identifiers and ensuring the uniqueness of the globally unique identifiers.
[0058] The partition identifier can be obtained through random generation, for example, by randomly generating a 7-digit partition identifier; or,
[0059] The table partition identifier can be generated based on the attribute characteristics of the target data using preset rules. The attribute characteristics of the target data can indicate its category, such as whether the target data is user identity information or user identification information. The preset rules can be set to any rule that can generate the identifier, and there is no limitation on the specific preset rules. For example, if the target data is a mobile phone number, its attribute characteristics indicate that it belongs to the identity identification category. The corresponding preset rule is to take the last 7 digits of the mobile phone number. Therefore, based on the attribute characteristics of the target data, the table partition identifier generated by the preset rule is the last 7 digits of the mobile phone number. In this embodiment, the specific attribute characteristics and preset rules used are not limited here and can be set according to actual needs.
[0060] S140 generates a globally unique identifier for the target data based on the machine identifier, sequence identifier, and table partition identifier.
[0061] When generating a globally unique identifier, there are no restrictions on the positions of the machine identifier, sequence identifier, and table partition identifier. A globally unique identifier can be obtained by randomly combining the machine identifier, sequence identifier, and table partition identifier.
[0062] In this embodiment of the invention, a globally unique identifier generation request for target data is received; a machine identifier of the machine to which the target data belongs is generated using the Raft algorithm; a sequence identifier and a partition identifier are randomly generated; and a globally unique identifier for the target data is generated based on the machine identifier, sequence identifier, and partition identifier. Since the machine identifier of the target data's machine is generated using the Raft algorithm, and thus the globally unique identifier is generated, no third-party software is required, thus eliminating the need for additional resource deployment and enabling the generation of globally unique identifiers with minimal resource deployment.
[0063] The globally unique identifier generated by the globally unique identifier generation method provided in this embodiment of the invention can meet the following requirements: (1) Globally unique: Since the machine identifier is generated based on the Raft algorithm, the uniqueness of the machine identifier can be guaranteed. Furthermore, since the globally unique identifier is generated based on the machine identifier, sequence identifier, and table partition identifier, the globally unique identifier can be guaranteed to be globally unique. (2) High performance: High availability and low latency, infinitely close to 100% availability and fast response of globally unique identifier generation. (3) Easy access: Since the machine identifier of the machine to which the target data belongs is generated using the Raft algorithm, and then the globally unique identifier is generated, it does not need to rely on third-party software, and the design and implementation are simple.
[0064] In one possible implementation, see Figure 2 Step S120 above, which uses the Raft algorithm to generate the machine identifier of the machine to which the target data belongs, can specifically include:
[0065] S1201, Generate machine identifier array.
[0066] Generate a machine identifier array containing multiple numbers, where each number represents a machine identifier. Specifically, an array of machine identifiers can be generated, starting from 0 and spaced 1s apart, containing a preset number of numbers. The preset number can be determined based on the actual number of machines in the distributed system. Specifically, the preset number can be selected from the numbers corresponding to powers of 2 (where n is a natural number), choosing a number greater than and closest to the number of machines in the distributed system. For example, if the distributed system contains 128 machines, the preset number is 128, and the machine identifier array contains 128 numbers, thus enabling unique identification of the 128 machines. This array can be [0, 1, 2, 3, 4… 127], where number 0 in the array is machine identifier 0, number 1 is machine identifier 1, and so on, until number 127 is machine identifier 127, thus achieving 128 machine identifiers from 128 numbers. In one example, the distributed system contains approximately 1000 machines, with 1024 selected as the preset number. The machine identifier array contains 1024 numbers, which can be [0,1,2,3,4……1023].
[0067] S1202, Read the message storage file to obtain the currently allocated numbers.
[0068] The message storage file stores the assigned numbers; this message storage file can be a commit log (communication log) file. The message storage file can store one or more assigned numbers, each number representing a machine identifier that has been assigned. In other words, by reading the message storage file, one can obtain the machine identifiers that have been assigned so far.
[0069] S1203: Based on the assigned numbers, traverse the machine identifier array, obtain the unassigned numbers, and use the unassigned numbers as unassigned machine identifiers.
[0070] Based on the already assigned numbers, the numbers in the machine identifier array are traversed sequentially according to their order. Each time a number is encountered, it is checked whether it is already assigned. If so, the next number is traversed, and the process is repeated. If not, the next number is an unassigned number, the traversal ends, and this unassigned number is used as the machine identifier to be assigned. In other words, the traversal stops as soon as an unassigned number is encountered. The first unassigned number encountered is used as the machine identifier to be assigned.
[0071] In one example, there is only one assigned number. When determining whether a number in the array is an assigned number, the system first iterates to the first number in the array and checks if that number is the same as one of the assigned numbers. If they are different, the number iterates to is the unassigned number. If they are the same, the next number is the unassigned number.
[0072] In one example, there are multiple assigned numbers. To determine whether a number in the array is an assigned number, the number traversed in the array can be compared with each of the assigned numbers in turn. If the traversed number is the same as any of the assigned numbers, then the next number is traversed. If the traversed number is different from all the assigned numbers, then the traversed number is an unassigned number. In one example, the machine identifier array is [0,1,2,3,4……127]. The assigned numbers are 0 and 1. When traversing the machine identifier array, the first step is to traverse to the number 0. Since there are multiple assigned numbers, the number 0 needs to be compared with each of the assigned numbers in turn. If the number 0 is the same as the assigned number 0, then the next number, number 1, is traversed. The number 1 is compared with each of the assigned numbers in turn. First, it is checked whether the number 1 is the same as the assigned number 0. If they are different, then it is checked whether the number 1 is the same as the assigned number 1. If they are the same, then the next number, number 2, is traversed. The number 2 is compared with each of the assigned numbers in turn. Finally, it is determined that the number 2 is different from all the assigned numbers. This number 2 is the unassigned number.
[0073] In this embodiment of the invention, the currently allocated numbers are obtained by reading the message storage file, and then the machine identifier array is traversed according to the allocated numbers to obtain the unallocated numbers. Since the unallocated numbers are the unallocated machine identifiers, the uniqueness of the machine identifiers is guaranteed, thereby ensuring the uniqueness of the globally unique identifiers.
[0074] In one possible implementation, Figure 2 The method may further include recording unassigned numbers in a message storage file. These unassigned numbers represent the machine identifiers that are about to be assigned. In other words, recording the machine identifiers about to be assigned in the message storage file ensures that the message storage file contains all assigned machine identifiers in a timely manner. In this embodiment of the invention, by recording unassigned numbers in the message storage file, the message storage file is updated promptly, ensuring that the latest assigned numbers are obtained each time the message storage file is read. That is, it ensures that all assigned machine identifiers up to the present are read, guaranteeing that the unassigned numbers obtained later are indeed unassigned, thus ensuring the uniqueness of the machine identifier and consequently the uniqueness of the globally unique identifier.
[0075] However, when the globally unique identifier generation method provided in this embodiment of the invention is deployed on a single node, if that node malfunctions, it may be unable to properly assign machine identifiers to the data. In this case, the method of this embodiment can be deployed on all nodes. In one possible implementation, the globally unique identifier generation method provided in this embodiment of the invention is deployed on all machines capable of generating the target data; each machine is a node, and this machine can be a server, client, etc. Based on this, in Figure 2 Before step S1201, the process may specifically include: electing a node as the leader node from among multiple nodes.
[0076] The Raft algorithm triggers leader election through a heartbeat mechanism. It selects the leader node from multiple candidate nodes that receives more than half of the votes. If the leader node malfunctions, another leader election is triggered to elect a new leader.
[0077] The above process elects a leader node from multiple nodes; specifically, it can elect a leader machine from multiple machines. This way, if the machine currently acting as the leader node malfunctions, another machine can be elected as the new leader node, and the subsequent process of allocating machine identifiers can proceed normally.
[0078] Furthermore, since the globally unique identifier generation method provided in this embodiment of the invention is deployed on all machines capable of generating target data, it is possible that multiple machines may assign the same identifier to different data at the same time. For example, at the same time, two machines may assign identifiers to two different data, and at this time, the message storage files read by the two machines are the same, resulting in the assignment of the same machine identifier to the two data, which may lead to the same globally unique identifier. However, since this embodiment of the invention selects only one node as the leader node, the machine identifier is generated by electing a node as the leader node, which prevents the situation where the same machine identifier is assigned when multiple nodes generate machine identifiers simultaneously, thereby ensuring that the generated machine identifier is completely unique, and thus ensuring the uniqueness of the globally unique identifier.
[0079] In one possible implementation, Figure 1 On this basis, Figure 1 The method shown also includes:
[0080] Step 1: Set the sign bit.
[0081] The sign bit is typically the highest bit of a globally unique identifier. For a binary number, a highest bit of 0 indicates a positive number, and a highest bit of 1 indicates a negative number. Because globally unique identifiers are generally generated as positive numbers, the sign bit is usually set to 0.
[0082] Step 2: Generate a timestamp for the target data based on the preset time and the time the request was received.
[0083] The timestamp is the time difference between the time the request was received and the preset time. The preset time can be set to a fixed value according to requirements. In an example, if the preset time is set to a fixed time of 1:00:00 AM on January 1, 2022, and the time the request was received is 1:00:30 AM on January 1, 2022, then the timestamp is the time difference between 1:00:30 AM and 1:00:00 AM on January 1, 2022, which is 30 seconds. Setting the timestamp ensures that the globally unique identifier increments sequentially over time.
[0084] Step 3: Generate a globally unique identifier for the target data based on the sign bit, timestamp, machine identifier, sequence identifier, and table partition identifier.
[0085] The number of bits for the timestamp, machine identifier, sequence identifier, and table partition identifier can be set according to requirements. Furthermore, there are no restrictions on the order of the sign bit, timestamp, machine identifier, sequence identifier, and table partition identifier. In one example, step three can generate a globally unique identifier for the target data by combining the sign bit, timestamp, machine identifier, sequence identifier, and table partition identifier in that order.
[0086] In one possible implementation, see Figure 3 , Figure 3 This is a schematic diagram of a globally unique identifier field provided in an embodiment of the present invention, such as... Figure 3As shown, the globally unique identifier can be 64 bits, with a 1-bit sign bit, a 41-bit timestamp, a 7-bit machine identifier, an 8-bit first preset bit length, and a 7-bit second preset bit length. This 64-bit globally unique identifier can be a 64-bit integer, occupying 8 bytes, and can be displayed as a long integer. The globally unique identifier can increment sequentially over time. The 41-bit timestamp can be in milliseconds, and a 41-bit timestamp in milliseconds can be used for 69 years: (1L<<41) / (1000L*60*60*24*365) = 69 years. The 7-bit machine identifier can support a server cluster deployment of up to 128 servers. The 8-bit sequence identifier can generate 256 globally unique identifiers per millisecond, or 256,000 globally unique identifiers per second, in a single-server scenario. The 7-bit table partition identifier supports table partitioning of up to 128 servers. The globally unique identifier in this embodiment of the invention can be seen as an optimization of the Snowflake algorithm. It redefines the number of bits in the sequence identifier and machine identifier in the field, and adds a table partition identifier on the basis of the Snowflake algorithm. It also adjusts the field composition of the Snowflake algorithm to achieve classification of the globally unique identifier, thereby further enhancing the uniqueness of the globally unique identifier.
[0087] In one possible implementation, see Figure 4 Taking a single video dataset as an example, the method for generating a globally unique identifier using the Snowflake algorithm can be implemented as follows:
[0088] S401, Received a request to generate a globally unique identifier for video data.
[0089] S402 sets the first sign bit of the globally unique identifier to 0.
[0090] S403, calculate the difference between the time the request was received and the preset time, and generate a 41-bit timestamp;
[0091] In this example, assuming the preset time is 1:00:00 on January 1, 2022, and the time the request is received is 1:00:30 on January 1, 2022, a 41-bit timestamp is generated for the 30-second difference.
[0092] S404 uses the Raft algorithm to elect a leader node.
[0093] S405, the leader node generates a machine identifier array Array[0,1,2,3,4……127]; this array contains 128 numbers.
[0094] S406, the leader node reads the commitlog file and reads the currently allocated numbers.
[0095] S407: Based on the assigned numbers, the leader node traverses the array Array to obtain the unassigned numbers.
[0096] The leader node iterates through the array Array based on the allocated numbers, and uses the first unallocated number encountered in the array as the identifier for the machine to be assigned.
[0097] S408, the leader node records the unassigned number to the commitlog file. In this example, the unassigned number used as the machine identifier is 7 digits.
[0098] S409 sends the unassigned numbers to the snowflake algorithm.
[0099] S410, randomly generate an 8-digit sequence identifier and a 7-digit table identifier.
[0100] The S411 snowflake algorithm concatenates the 1-bit sign bit, 41-bit timestamp, 7-bit machine identifier, 8-bit sequence identifier, and 7-bit table identifier in sequence to generate a 64-bit globally unique identifier.
[0101] In existing technologies, using the Snowflake algorithm to generate globally unique identifiers may lead to duplicate identifiers when multiple data are processed concurrently, resulting in non-globally unique identifiers. However, in this embodiment of the invention, the Raft algorithm is used to generate the machine identifier of the target data's origin, thus generating a globally unique identifier. This ensures the uniqueness of the globally unique identifier. Compared to using the Snowflake algorithm, this method guarantees the globally unique identifier even when multiple data are processed concurrently. Furthermore, it does not rely on third-party software, requiring minimal resource deployment. The Snowflake algorithm is optimized by adding table partitioning identifiers, allowing for the categorization of globally unique identifiers and facilitating subsequent data processing.
[0102] This invention also provides a globally unique identifier generation device, see [link to relevant documentation]. Figure 5 , Figure 5 The apparatus shown includes:
[0103] Request receiving module 51 is used to receive the global unique identifier generation request for the target data;
[0104] The machine identifier generation module 52 is used to generate the machine identifier of the machine to which the target data belongs using the Raft algorithm;
[0105] Sequence identifier and table partition identifier generation module 53 is used to randomly generate sequence identifiers and table partition identifiers;
[0106] The first globally unique identifier generation module 54 is used to generate a globally unique identifier for the target data based on the machine identifier, sequence identifier, and table partition identifier.
[0107] In one possible implementation, the machine identifier generation module includes:
[0108] The machine identifier array generation unit is used to generate the machine identifier array;
[0109] The message storage file reading unit is used to read the message storage file and obtain the currently allocated numbers;
[0110] The unassigned machine identifier generation unit is used to traverse the machine identifier array based on the assigned numbers, obtain the unassigned numbers, and use the unassigned numbers as unassigned machine identifiers.
[0111] In one possible implementation, the machine identifier generation module is further used for:
[0112] Record unassigned numbers to the message storage file.
[0113] In one possible implementation, the machine identifier generation module further includes:
[0114] The leader node election unit is used to elect a leader node from among multiple nodes.
[0115] In one possible implementation, the above-described apparatus further includes:
[0116] The sign bit setting module is used to set the sign bit;
[0117] The timestamp generation module is used to generate timestamps for target data based on a preset time and the time the request is received.
[0118] The second globally unique identifier generation module is used to generate a globally unique identifier for target data based on the sign bit, timestamp, machine identifier, sequence identifier, and table partition identifier.
[0119] Regarding the apparatus in the above embodiments, the specific manner in which each module performs its operation has been described in detail in the embodiments related to the method, and will not be elaborated upon here.
[0120] This invention also provides an electronic device, such as... Figure 6 As shown, it includes a processor 61, a communication interface 62, a memory 63, and a communication bus 64, wherein the processor 61, the communication interface 62, and the memory 63 communicate with each other through the communication bus 64.
[0121] Memory 63 is used to store computer programs;
[0122] When the processor 61 executes the program stored in the memory 63, it implements any of the above-mentioned methods for generating a globally unique identifier.
[0123] The communication bus mentioned above can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. This communication bus can be divided into address bus, data bus, control bus, etc. For ease of illustration, only one thick line is used to represent it in the diagram, but this does not mean that there is only one bus or one type of bus.
[0124] The communication interface is used for communication between the aforementioned terminal and other devices.
[0125] The memory may include random access memory (RAM) or non-volatile memory, such as at least one disk storage device. Optionally, the memory may also be at least one storage device located remotely from the aforementioned processor.
[0126] The processors mentioned above can be general-purpose processors, including central processing units (CPUs), network processors (NPs), etc.; they can also be digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components.
[0127] In another embodiment of the present invention, a computer-readable storage medium is also provided, wherein a computer program is stored therein, and when the computer program is executed by a processor, it implements the globally unique identifier generation method described in any of the above embodiments.
[0128] In another embodiment of the present invention, a computer program product containing instructions is also provided, which, when run on a computer, causes the computer to execute any of the globally unique identifier generation methods described in the above embodiments.
[0129] In the above embodiments, implementation can be achieved entirely or partially through software, hardware, firmware, or any combination thereof. When implemented using software, it can be implemented entirely or partially in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of the present invention are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that integrates one or more available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., solid state disk (SSD)).
[0130] It should be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0131] The various embodiments in this specification are described in a related manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the system embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the descriptions of the method embodiments.
[0132] The above description is merely a preferred embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention are included within the scope of protection of the present invention.
Claims
1. A method for generating a globally unique identifier, characterized in that, The method is deployed on a machine capable of generating the target data, with one machine constituting one node. The method includes: Receive a request to generate a globally unique identifier for the target data; The machine identifier of the machine to which the target data belongs is generated using the Raft algorithm, specifically including: using the Raft algorithm to elect a node as the leader node from multiple nodes; The process of allocating machine identifiers using the leader node includes: generating a machine identifier array; reading a message storage file to obtain the currently allocated numbers; and based on the allocated numbers, traversing the machine identifier array to obtain unallocated numbers and sending them to the snowflake algorithm as machine identifiers. Randomly generate sequence identifiers and table partition identifiers; Based on the machine identifier, the sequence identifier, and the table partition identifier, a globally unique identifier for the target data is generated.
2. The method according to claim 1, characterized in that, The method further includes: Record the unassigned numbers into the message storage file.
3. The method according to claim 1, characterized in that, The method further includes: Set the sign bit; Generate a timestamp for the target data based on a preset time and the time the request is received; A globally unique identifier for the target data is generated based on the symbol bit, the timestamp, the machine identifier, the sequence identifier, and the table partition identifier.
4. A globally unique identifier generation device, characterized in that, The device is deployed on a machine capable of generating the target data, with one machine constituting one node. The device includes: The request receiving module is used to receive requests for generating a globally unique identifier for the target data. The machine identifier generation module is used to generate the machine identifier of the machine to which the target data belongs using the Raft algorithm. Specifically, it includes: electing a node as the leader node from multiple nodes using the Raft algorithm; allocating machine identifiers using the leader node, including: generating a machine identifier array; reading a message storage file to obtain the currently allocated numbers; and based on the allocated numbers, traversing the machine identifier array to obtain unallocated numbers and sending them to the Snowflake algorithm as machine identifiers. The sequence identifier and table partition identifier generation module is used to randomly generate sequence identifiers and table partition identifiers; The first globally unique identifier generation module is used to generate a globally unique identifier for the target data based on the machine identifier, the sequence identifier, and the table partition identifier.
5. An electronic device, characterized in that, It includes a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus; Memory, used to store computer programs; A processor, when executing a program stored in memory, implements the steps of the method according to any one of claims 1-3.
6. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements the steps of the method according to any one of claims 1-3.
Citation Information
Patent Citations
Global primary key generation method and device
CN111708831A