Distributed identity number generation method, electronic device, and storage medium
By obtaining the number of servers and the restart method to generate and allocate machine numbers, the problem of non-reusable machine numbers and restart conflicts in the distributed identity number generation of the Snowflake algorithm is solved, thus improving generation efficiency and adaptability.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-28
- Publication Date
- 2026-03-31
AI Technical Summary
The existing Snowflake algorithm has several drawbacks when generating distributed identity identifiers, including the inability to reuse machine numbers, their depletion over time, the inability to obtain workIDs or workID conflicts when a single instance restarts, compatibility issues with automatic instance restarts in virtualization environments such as Docker, and the lack of support for dynamic cluster expansion within a version, resulting in low generation efficiency.
By obtaining the number of servers, restart method, and the target machine number with the largest value, machine numbers are generated and assigned to servers, and the target machine number is updated to ensure the uniqueness of the machine number. The machine number is verified by comparison, which supports version updates and server expansion, and improves generation efficiency.
It enables the server to obtain a unique machine number even after a restart, reducing distributed identity number conflicts, improving generation efficiency, and adapting to high-concurrency and virtualized environments.
Smart Images

Figure CN115099190B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of data processing technology, and in particular to a method for generating distributed identity identifiers, an electronic device, and a storage medium. Background Technology
[0002] During the application of universal settlement, the program has a large demand for generating batch numbers, serial numbers, and policy numbers. However, the current generation method in the storage process is time-consuming and not suitable for high-concurrency scenarios after application. After searching online, it was found that there is a similar generation logic to the Snowflake algorithm. However, after comparing it with the current program requirements, it was found that the Snowflake algorithm programs on the market have defects, such as the execution machine workID, i.e., the machine number, is not reusable and will be exhausted over time. There are also issues such as the inability to obtain workID or workID conflicts when a single instance restarts. There are also compatibility issues in scenarios such as automatic restart and migration of instances in virtualization environments such as Docker (application container engine). There is also the problem of not supporting dynamic cluster expansion within the version and the lack of flexibility in generating distributed identity identifiers. Summary of the Invention
[0003] The following is an overview of the subject matter described in detail herein. This overview is not intended to limit the scope of the claims.
[0004] This invention provides a method, electronic device, and storage medium for generating distributed identity identifiers, enabling the server to re-acquire the machine number after a restart, ensuring that the machine number is unique and improving the efficiency of distributed identity identifier generation.
[0005] In a first aspect, embodiments of the present invention provide a distributed identity identifier generation method, comprising: obtaining the number of servers, the restart method, and the target machine number with the largest value; generating and allocating the machine number to the servers according to the number and the restart method, and updating the target machine number, wherein the servers and the machine numbers correspond one-to-one; comparing the updated target machine number with a server number threshold, and verifying the machine number according to the comparison result.
[0006] In some embodiments, generating and allocating the machine number to the server based on the quantity and the restart method includes: when the restart method is to update the project version, resetting the machine number in the project version before the update, generating a new machine number based on the quantity of servers, and allocating the new machine number to the server.
[0007] In some embodiments, generating and allocating the machine number to the server based on the quantity and the restart method includes: when the restart method is an intra-project version restart or a server expansion restart, after the first reset time, determining a new machine number based on the number of servers and the target machine number before the update, and allocating the new machine number to the server.
[0008] In some embodiments, verifying the machine number based on the comparison result includes at least one of the following: when the comparison result is that the updated target machine number is greater than the server number threshold, determining that the machine number is duplicated; when the comparison result is that the updated target machine number is less than or equal to the server number threshold, determining that the machine number is unique.
[0009] In some embodiments, after determining that the machine number is duplicated when the comparison result is that the updated target machine number is greater than the server number threshold, the generation method further includes: determining the server corresponding to the machine number that is greater than the server number threshold, and performing overflow marking processing on the determined server.
[0010] In some embodiments, the distributed identity identifier further includes a symbol bit, a timestamp, and a sequence number. The generation method further includes: obtaining the sequence number, wherein one machine number corresponds to multiple sequence numbers; obtaining a start time and a current time, and determining the timestamp and a letter prefix based on the time difference between the start time and the current time; sequentially sorting and combining the symbol bit, the timestamp, the machine number, and the sequence number to obtain an initial distributed identity identifier; performing a base conversion on the initial distributed identity identifier to obtain an intermediate distributed identity identifier; and combining the intermediate distributed identity identifier with the letter prefix to obtain a final distributed identity identifier.
[0011] In some embodiments, before combining the intermediate distributed identity number with the letter prefix to obtain the final distributed identity number, the generation method further includes: determining the number of digits in the intermediate distributed identity number; if the number of digits is less than a preset number of digits, padding with zeros until the number of digits reaches the preset number of digits.
[0012] In some embodiments, the method further includes at least one of the following: setting a filling strategy for the final distributed identity number, and storing the final distributed identity number in a target cache area according to the filling strategy; setting a retrieval strategy for the final distributed identity number, and retrieving the final distributed identity number from the target cache area according to the retrieval strategy.
[0013] In a second aspect, embodiments of the present invention provide an electronic device, including: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the distributed identity number generation method described in the first aspect above.
[0014] Thirdly, embodiments of the present invention provide a computer-readable storage medium storing computer-executable instructions, which are used to execute the computer program to implement the distributed identity number generation method described in the first aspect above.
[0015] The beneficial effects of this invention include: This invention obtains the number of servers, the restart method, and the target machine number with the largest value; generates and allocates the machine number to the server based on the number and restart method, and updates the target machine number, wherein there is a one-to-one correspondence between the server and the machine number; when the restart method is to update the project version, the machine number in the previous project version is reset, and a machine number is generated and allocated to the server based on the number of servers. When the restart method is an intra-project version restart or a server expansion restart, after the first reset time, a new machine number is determined based on the number of servers and the previous target machine number, and the new machine number is allocated to the server. The updated target machine number is compared with a server number threshold, and the machine number is verified based on the comparison result. Generating and allocating machine numbers to servers based on the number of servers and the restart method ensures that the server can still obtain a machine number after restarting. By comparing the updated target machine number with the server number threshold, the machine number is ensured to be unique, improving the generation efficiency of distributed identity identifiers.
[0016] Other features and advantages of the invention will be set forth in the description which follows, and will be apparent in part from the description, or may be learned by practicing the invention. The objects and other advantages of the invention may be realized and obtained by means of the structures particularly pointed out in the description, claims, and drawings. Attached Figure Description
[0017] The accompanying drawings are provided to further understand the technical solutions of the present invention and constitute a part of the specification. They are used together with the embodiments of the present invention to explain the technical solutions of the present invention, and do not constitute a limitation on the technical solutions of the present invention.
[0018] Figure 1 This is a schematic diagram of the system architecture platform for the distributed identity number generation method provided in an embodiment of the present invention;
[0019] Figure 2 This is a flowchart of a distributed identity number generation method provided in an embodiment of the present invention;
[0020] Figure 3 This is a flowchart of a distributed identity number generation method provided in another embodiment of the present invention;
[0021] Figure 4 This is a flowchart of a distributed identity number generation method provided in another embodiment of the present invention;
[0022] Figure 5 This is a flowchart of a distributed identity number generation method provided in another embodiment of the present invention;
[0023] Figure 6 This is a flowchart of a distributed identity number generation method provided in another embodiment of the present invention;
[0024] Figure 7 This is a flowchart of a distributed identity number generation method provided in another embodiment of the present invention;
[0025] Figure 8 This is a flowchart of a distributed identity number generation method provided in another embodiment of the present invention;
[0026] Figure 9 This is a flowchart of a distributed identity number generation method provided in another embodiment of the present invention;
[0027] Figure 10 This is a flowchart of a distributed identity number generation method provided in another embodiment of the present invention. Detailed Implementation
[0028] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the invention.
[0029] In the description of this invention, "several" means one or more, "more than" means two or more, "greater than," "less than," and "exceeding" are understood to exclude the stated number, while "above," "below," and "within" are understood to include the stated number. The use of "first" and "second" in the description is merely for distinguishing technical features and should not be construed as indicating or implying relative importance, or implicitly indicating the number of indicated technical features, or implicitly indicating the order of the indicated technical features.
[0030] In the description of this invention, unless otherwise explicitly defined, terms such as "set up," "install," and "connect" should be interpreted broadly, and those skilled in the art can reasonably determine the specific meaning of the above terms in this invention in conjunction with the specific content of the technical solution.
[0031] It should be noted that although functional modules are divided in the device schematic diagram and a logical order is shown in the flowchart, in some cases, the steps shown or described may be performed in a different order than the module division in the device or the order in the flowchart. The terms "first," "second," etc., in the specification, claims, and the aforementioned drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence.
[0032] First, let's clarify some of the terms used in this invention:
[0033] Artificial Intelligence (AI) is a new branch of computer science that studies, develops, and applies theories, methods, technologies, and systems to simulate, extend, and expand human intelligence. It aims to understand the essence of intelligence and produce intelligent machines that can react in a way similar to human intelligence. Research in this field includes robotics, speech recognition, image recognition, natural language processing, and expert systems. AI can simulate the information processes of human consciousness and thought. Furthermore, AI utilizes digital computers or machines controlled by digital computers to simulate, extend, and expand human intelligence, perceiving the environment, acquiring knowledge, and using that knowledge to achieve optimal results.
[0034] Natural Language Processing (NLP): NLP uses computers to process, understand, and utilize human language (such as Chinese and English). NLP is a branch of artificial intelligence and an interdisciplinary field of computer science and linguistics, often referred to as computational linguistics. NLP includes syntactic analysis, semantic analysis, and discourse understanding. It is commonly used in machine translation, handwritten and printed character recognition, speech recognition and text-to-speech conversion, information retrieval, information extraction and filtering, text classification and clustering, sentiment analysis, and opinion mining. It involves data mining, machine learning, knowledge acquisition, knowledge engineering, artificial intelligence research, and linguistic research related to language computation.
[0035] Information Extraction (NER) is a text processing technique that extracts factual information such as entities, relationships, and events from natural language text and outputs it as structured data. Information extraction is a technique for extracting specific information from text data. Text data is composed of specific units, such as sentences, paragraphs, and chapters. Text information is composed of smaller, specific units, such as characters, words, phrases, sentences, paragraphs, or combinations of these units. Extracting noun phrases, names of people, and place names from text data is an example of text information extraction. Of course, text information extraction techniques can extract information of various types.
[0036] Snowflake Algorithm: The Snowflake Algorithm is an open-source distributed ID (Identity Document) generation algorithm from Twitter. It primarily generates global IDs using a 64-bit long integer, incorporating a timestamp and ensuring the ID remains auto-incrementing. The 64 bits are divided into four parts: the first part is a 1-bit (0); the second part is a 41-bit timestamp; the third part is a 10-bit worker machine ID, divided into two parts: 5 bits for the data center number (representing a maximum of 2^5, or 32 data centers) and 5 bits for the machine ID (representing a maximum of 2^5, or 32 machines); the fourth part is a 12-bit sequence number, distinguishing IDs generated within the same millisecond. In short, an ID generated from the same data center and machine at the same millisecond might have different IDs within the same second, and the final 12-bit sequence number distinguishes these different IDs within that same second. The Snowflake Algorithm guarantees: 1. Generated IDs increment over time; 2. No duplicate IDs exist in the entire distributed system.
[0037] Distributed IDs: In complex distributed systems, it is necessary to uniquely identify large amounts of data and messages. Traditional database auto-incrementing primary keys, or monolithic auto-incrementing primary keys, are no longer sufficient. Distributed IDs can quickly and stably generate unique primary keys, without relying on the database, generating IDs entirely in memory. This results in high performance and high availability, generating millions of IDs per second. Furthermore, it implements multiple generation methods, which can be freely configured and used as needed.
[0038] A distributed system is a software system built on a network. Due to the nature of software, distributed systems possess high cohesion and transparency. Therefore, the difference between a network and a distributed system lies more in the high-level software (especially the operating system) than in the hardware. In a distributed system, a group of independent computers presents a unified whole to the user, appearing as a single system. The system possesses various common physical and logical resources, can dynamically allocate tasks, and the distributed physical and logical resources exchange information through the computer network. A distributed operating system exists within the system to manage computer resources globally. Typically, for the user, a distributed system presents only one model or paradigm. A layer of middleware sits above the operating system, responsible for implementing this model. A famous example of a distributed system is the World Wide Web, where everything appears as a single document (web page).
[0039] Docker is an open-source application container engine that allows developers to package their applications and dependencies into a portable container and then deploy it to any popular Linux or Windows operating system. It can also achieve virtualization. Containers are completely sandboxed and have no interface with each other.
[0040] Redis, short for Remote Dictionary Server, is a key-value storage system. Similar to Memcached, it supports a wider range of value types, including string, list, set, sorted set (zset), and hash. These data types support push / pop, add / remove, intersection, union, difference, and more complex operations, all of which are atomic. Furthermore, Redis supports various sorting methods. Like Memcached, data is cached in memory for efficiency. However, Redis periodically writes updated data to disk or appends modification operations to a log file, and it also implements master-slave synchronization.
[0041] Lua: A small scripting language that can be easily called by C / C++ code and vice versa, making it widely applicable in applications. It can be used not only as an extension scripting language but also as a regular configuration file, replacing XML, INI, and other file formats, and is easier to understand and maintain. Written in standard C, Lua's code is concise and elegant, and it can be compiled and run on almost all operating systems and platforms. A complete Lua interpreter is only about 200k, and among all scripting engines, Lua is the fastest. All of this makes Lua the best choice for embedded scripting.
[0042] In existing technologies, during the application of universal settlement, the program has a large demand for generating batch numbers, serial numbers, and policy numbers. However, the current generation methods in the storage process are time-consuming and unsuitable for high-concurrency scenarios after application. Online searches revealed that there are similar generation logics to the Snowflake algorithm. However, after comparing with the current program requirements, it was found that the Snowflake algorithm programs on the market have defects, such as the execution machine workID (machine number) being non-reusable and depleting over time, the inability to obtain workID or workID conflicts when a single instance restarts, compatibility issues in scenarios such as automatic instance restart and migration in virtualization environments such as Docker, lack of support for dynamic cluster expansion within the same version, and insufficient flexibility in generating distributed identity identifiers.
[0043] Based on this, embodiments of the present invention provide a method, electronic device, and storage medium for generating distributed identity identifiers, enabling servers to re-acquire machine numbers after restarting, ensuring that each machine number is unique and improving the efficiency of distributed identity identifier generation. The embodiments of the present invention obtain the number of servers, the restart method, and the target machine number with the largest value; generate and allocate the machine number to the server according to the number and restart method, and update the target machine number, wherein there is a one-to-one correspondence between the server and the machine number; when the restart method is updating a project version, the machine number in the previous project version is reset, and machine numbers are generated and allocated to the server according to the number of servers. When the restart method is a restart within a project version or a server expansion restart, after the first reset time, a new machine number is determined according to the number of servers and the previous target machine number, and the new machine number is allocated to the server. The updated target machine number is compared with a server number threshold, and the machine number is verified based on the comparison result. Generating and allocating machine numbers to servers according to the number of servers and the restart method ensures that the server can still obtain a machine number after restarting. By comparing the updated target machine number with the server number threshold, the uniqueness of the machine number is ensured, thus improving the efficiency of distributed identity identifier generation.
[0044] The embodiments of this invention can acquire and process relevant data based on artificial intelligence technology. Artificial intelligence (AI) refers to the theories, methods, technologies, and application systems that utilize digital computers or machines controlled by digital computers to simulate, extend, and expand human intelligence, perceive the environment, acquire knowledge, and use that knowledge to obtain optimal results.
[0045] Foundational technologies for artificial intelligence generally include sensors, dedicated AI chips, cloud computing, distributed storage, big data processing, operating / interactive systems, and mechatronics. AI software technologies mainly encompass computer vision, robotics, biometrics, speech processing, natural language processing, and machine learning / deep learning.
[0046] The distributed identity identifier generation method provided in this invention relates to the fields of artificial intelligence and digital healthcare technology. This method can be applied to a terminal, a server, or software running on either a terminal or a server. In some embodiments, the terminal can be a smartphone, tablet, laptop, desktop computer, etc.; the server can be configured as an independent physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server providing basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN, and big data and artificial intelligence platforms; the software can be the implementation of the distributed identity identifier generation method, but is not limited to the above forms.
[0047] This invention can be used in a wide variety of general-purpose or special-purpose computer system environments or configurations. Examples include: personal computers, server computers, handheld or portable devices, tablet devices, multiprocessor systems, microprocessor-based systems, set-top boxes, programmable consumer electronics, network PCs, minicomputers, mainframe computers, and distributed computing environments including any of the above systems or devices. This invention can be described in the general context of computer-executable instructions, such as program modules, that are executed by a computer. Generally, program modules include routines, programs, objects, components, data structures, etc., that perform specific tasks or implement specific abstract data types. This invention can also be practiced in distributed computing environments where tasks are performed by remote processing devices connected via a communication network. In distributed computing environments, program modules can reside in local and remote computer storage media, including storage devices.
[0048] The embodiments of the present invention will be further described below with reference to the accompanying drawings.
[0049] like Figure 1 As shown, Figure 1 This is a schematic diagram of the system architecture platform for the distributed identity number generation method provided in the embodiments of the present invention.
[0050] The system architecture platform 100 of this embodiment includes one or more processors 110 and a memory 120. Figure 1 The example uses a processor 110 and a memory 120.
[0051] Processor 110 and memory 120 can be connected via a bus or other means. Figure 1 Taking the example of a connection between China and Israel via a bus.
[0052] Memory 120, as a non-transitory computer-readable storage medium, can be used to store non-transitory software programs and non-transitory computer-executable programs. Furthermore, memory 120 may include high-speed random access memory, and may also include non-transitory memory, such as at least one disk storage device, flash memory device, or other non-transitory solid-state storage device. In some embodiments, memory 120 may optionally include memory 120 remotely located relative to processor 110, and these remote memories can be connected to the system architecture platform 100 via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.
[0053] Those skilled in the art will understand that Figure 1 The device structure shown does not constitute a limitation on the system architecture platform 100 and may include more or fewer components than shown, or combine certain components, or have different component arrangements.
[0054] like Figure 2 As shown, Figure 2 This is a flowchart of a distributed identity number generation method provided in an embodiment of the present invention. The distributed identity number generation method of the present invention includes, but is not limited to, steps S200, S210 and S220.
[0055] Step S200: Obtain the number of servers, the restart method, and the target machine number with the largest value;
[0056] Step S210: Generate and assign machine numbers to the server according to the quantity and restart method, and update the target machine number, wherein the server and the machine number correspond one-to-one;
[0057] Step S220: Compare the updated target machine number with the server quantity threshold, and verify the machine number based on the comparison result.
[0058] In this embodiment of the invention, the number of servers, restart method, and the target machine number with the largest value are obtained; machine numbers are generated and allocated to servers according to the number and restart method, and the target machine number is updated, wherein there is a one-to-one correspondence between servers and machine numbers; the updated target machine number is compared with a server number threshold, and the machine number is verified based on the comparison result. Generating and allocating machine numbers to servers according to the number and restart method ensures that the machine number can still be obtained after the server restarts. By comparing the updated target machine number with the server number threshold, the uniqueness of the machine number is ensured, reducing the probability of machine number duplication and thus distributed identity number conflicts, thereby improving the generation efficiency of distributed identity numbers.
[0059] For example, in this embodiment of the invention, the machine number is generated by Redis auto-incrementing. The Redis key records the largest machine number in the current version, that is, the target machine number with the largest value. By obtaining the number of servers and the restart method, a new machine number is generated by Redis auto-incrementing, and the new machine number is assigned to the server and the Redis key is updated. Since the machine number has an upper limit on the number of digits, the generated machine number also has an upper limit, that is, the server threshold. The Redis key and the server threshold are compared to ensure that the machine number is unique.
[0060] like Figure 3 As shown, Figure 3 This is a flowchart of a distributed identity identifier generation method provided in another embodiment of the present invention. The distributed identity identifier generation method of the present invention includes, but is not limited to, steps S300, S310 and S320.
[0061] Step S300: Restart by updating the project version;
[0062] Step S310: Reset the machine number in the previous project version;
[0063] Step S320: Generate new machine numbers based on the number of servers and assign the new machine numbers to the servers.
[0064] In this embodiment of the invention, the number of servers, the restart method, and the target machine number with the largest value are obtained; machine numbers are generated and allocated to servers according to the number and restart method, and the target machine number is updated, wherein there is a one-to-one correspondence between servers and machine numbers; when the restart method is to update the project version, the machine number in the project version before the update is reset, a new machine number is generated according to the number of servers, and the new machine number is allocated to the servers.
[0065] For example, let the old project version be project version A, and the machine IDs in project version A be machine 0, machine 1, machine 2, and machine 3. The new project version is project version B. After updating the project version from project version A to project version B, the machine IDs in the previous project version are reset (i.e., the machine IDs in project version A are reset). Simultaneously, the target machine ID with the largest value is reset, i.e., Rediskey: machine 4. New machine IDs are generated based on the number of servers after the project version update, and these new machine IDs are assigned to the servers. If there are five servers after the update, the first server to successfully acquire the lock is assigned the new machine ID—machine 0, and the Rediskey of the first server to successfully acquire the lock is reset to -1 to avoid wasting machine IDs. The second server to successfully acquire the lock is assigned the new machine ID—machine 1, and so on. Finally, the target machine ID with the largest value is recorded, i.e., the updated target machine ID Rediskey.
[0066] like Figure 4 As shown, Figure 4 This is a flowchart of a distributed identity identifier generation method provided in another embodiment of the present invention. The distributed identity identifier generation method of the present invention includes, but is not limited to, steps S400, S410 and S420.
[0067] Step S400: The restart method is either a restart within the project version or a server expansion restart;
[0068] Step S410: After the first reset time, determine the new machine number based on the number of servers and the target machine number before the restart;
[0069] Step S420: Assign the new machine number to the server.
[0070] In this embodiment of the invention, the number of servers, the restart method, and the target machine number with the largest value are obtained. Machine numbers are generated and allocated to servers based on the number and restart method, and the target machine number is updated. There is a one-to-one correspondence between servers and machine numbers. When the restart method is an intra-project version restart or a server expansion restart, after the first reset time, a new machine number is determined based on the number of servers and the target machine number before the update, and the new machine number is allocated to the servers. This reduces the probability of duplicate machine numbers leading to distributed identity number conflicts and improves the efficiency of distributed identity number generation.
[0071] For example, when the restart method is a restart within a project version, let the project version be project version A. Before the restart, the machine numbers used in project version A are machine 0, machine 1, machine 2, and machine 3, which is the target machine number with the largest value. The Redis key is machine 3. After the restart, since machines 0 to 3 are already occupied, in order to reduce conflicts caused by the reuse of machine numbers, the new machine number is determined by the number of servers and the target machine number before the restart, and the new machine number is assigned to the servers. After the restart, four servers complete the lock acquisition. The first server to complete the lock acquisition is assigned the new machine number as machine 4, the second server to complete the lock acquisition is assigned the new machine number as machine 5, and so on. The last server to complete the lock acquisition is assigned the new machine number as machine 7, and the target machine number, i.e., the Redis key, is updated to machine 7.
[0072] For example, when the restart method is server expansion restart, let the project version be project version A. Before the restart, the machine numbers used in project version A are machine 0, machine 1, machine 2 and machine 3, which is the target machine number with the largest value. The Redis key is machine 3. After the restart, since machines 0 to 3 have been occupied, in order to reduce conflicts caused by the reuse of machine numbers, the new machine number is determined by the number of servers and the target machine number before the restart, and the new machine number is assigned to the server. Due to the expansion process, after the restart, six servers complete the lock acquisition. The first server to complete the lock acquisition is assigned the new machine number as machine 4, the second server to complete the lock acquisition is assigned the new machine number as machine 5, and so on. The last server to complete the lock acquisition is assigned the new machine number as machine 9, and the target machine number, i.e., the Redis key, is updated to machine 9.
[0073] like Figure 5 and Figure 6 As shown, Figure 5 This is a flowchart of a distributed identity number generation method provided in another embodiment of the present invention. Figure 6 This is a flowchart of a distributed identity number generation method provided in another embodiment of the present invention. The distributed identity number generation method of the present invention includes, but is not limited to, step S500 and / or step S600.
[0074] Step S500: When the comparison result shows that the updated target machine number is greater than the server quantity threshold, it is determined that the machine number is duplicated.
[0075] Step S600: When the comparison result shows that the updated target machine number is less than or equal to the server quantity threshold, the machine number is determined to be unique.
[0076] In this embodiment of the invention, since the number of digits in the machine number has an upper limit, the number of generated machine numbers also has an upper limit, namely the server threshold. The updated target machine number is compared with the server number threshold, and the machine number is verified based on the comparison result. If the comparison result is that the updated target machine number is greater than the server number threshold, it is determined that the machine number is duplicated. If the comparison result is that the updated target machine number is less than the server number threshold, it is determined that the machine number is unique. This reduces the probability of machine number duplication leading to distributed identity number conflicts and improves the generation efficiency of distributed identity numbers.
[0077] For example, assuming the maximum number of bits for a machine ID is 8, meaning the maximum machine ID is 2 to the power of 8, or 256, then the server threshold is 256. When the updated target machine ID, i.e., the Rediskey, is 256, equal to the server threshold, the machine ID is determined to be unique, and no overflow has occurred. When the updated target machine ID, i.e., the Rediskey, is 257, exceeding the server threshold, a machine ID overflow has occurred, indicating a duplicate machine ID. This reduces the probability of duplicate machine IDs leading to distributed identity conflicts, thus improving the efficiency of distributed identity ID generation.
[0078] like Figure 7 As shown, Figure 7 This is a flowchart of a distributed identity number generation method provided in another embodiment of the present invention. The distributed identity number generation method of the present invention includes, but is not limited to, steps S700 and S710.
[0079] Step S700: Determine the server corresponding to the machine number that is greater than the server number threshold;
[0080] Step S710: Perform overflow marking on the identified server.
[0081] In this embodiment of the invention, since the number of digits in the machine number has an upper limit, the generated machine number also has an upper limit, namely the server threshold. The updated target machine number is compared with the server number threshold, and the machine number is verified according to the comparison result. When the comparison result is that the updated target machine number is greater than the server number threshold, the machine number is duplicated. The server corresponding to the machine number greater than the server threshold is determined, and the determined server is marked with an overflow flag to reduce the probability of machine number duplication and thus distributed identity number conflict, thereby improving the generation efficiency of distributed identity number.
[0082] For example, in this embodiment of the invention, a Lua script is used to determine whether the target machine number is greater than the server threshold. If the target machine number is greater than the server threshold, the server corresponding to the machine number greater than the server threshold is marked with an overflow flag, and its machine number is assigned a value of -1. Alternatively, the machine number is obtained, and the server with a machine number of -1 is marked with an overflow flag. Distributed identity identifiers generated by servers with machine numbers of -1 or carrying overflow flags are not extracted or used. This reduces the probability of duplicate machine numbers leading to distributed identity identifier conflicts and improves the efficiency of distributed identity identifier generation.
[0083] like Figure 8 As shown, Figure 8 This is a flowchart of a distributed identity number generation method provided in another embodiment of the present invention. The distributed identity number generation method of the present invention includes, but is not limited to, flag S800, step S810, step S820, step S830 and step S840.
[0084] Step S800: Obtain the serial number;
[0085] Step S810: Obtain the start time and current time, and determine the timestamp and letter prefix based on the time difference between the start time and current time;
[0086] Step S820: Sort and combine the symbol bit, timestamp, machine number and sequence number in sequence to obtain the initial distributed identity identifier;
[0087] Step S830: Perform a base conversion on the initial distributed identity number to obtain an intermediate distributed identity number;
[0088] Step S840: Combine the intermediate distributed identity number with the letter prefix to obtain the final distributed identity number.
[0089] In this embodiment of the invention, the distributed identity identifier further includes a symbol bit, a timestamp, and a sequence number. The sequence number is obtained, whereby it is generated by the server, and one machine number corresponds to multiple sequence numbers. The start time and current time are obtained, and the timestamp and letter prefix are determined based on the time difference between the start time and the current time. The letter prefix also includes a determination of the target cache region and Redis. The symbol bit, timestamp, machine number, and sequence number are sequentially sorted and combined to obtain an initial distributed identity identifier. The initial distributed identity identifier undergoes a base conversion to obtain an intermediate distributed identity identifier. The intermediate distributed identity identifier is combined with the letter prefix to obtain the final distributed identity identifier.
[0090] For example, in this embodiment of the invention, the initial distributed identity identifier is a fifty-bit binary number, including a sign bit, which is fixed at zero to indicate that all identity identifiers are positive integers; a twenty-seven-bit timestamp, which indicates the time difference between the current time and the start time, in seconds, supporting a maximum of 134,217,728 seconds, or approximately 4.25 years; an eight-bit machine number, supporting a maximum of 256 servers; and a fourteen-bit serial number, indicating that each server can generate 16,384 serial numbers per second. The timestamp and letter prefix are determined by obtaining the serial number and the time difference between the start time and the current time. The sign bit, timestamp, machine number, and serial number are sorted and combined in sequence to obtain the initial distributed identity identifier. The initial distributed identity identifier is then converted to a 49-bit binary number (with the sign bit removed) to a fifteen-bit decimal intermediate distributed identity identifier. The intermediate distributed identity identifier is then combined with the letter prefix to obtain the final distributed identity identifier.
[0091] Additionally, it should be noted that in this embodiment of the invention, the serial number, after being generated by the server, is stored in a circular target cache area. The serial number is retrieved from this target cache area. The circular target cache area has multiple slots, each corresponding to a machine ID; that is, one slot corresponds to a serial number generated by one server. Furthermore, this embodiment of the invention also includes a circular extraction and filling flag area, which corresponds to the target cache area and also has multiple slots. Each slot stores extraction flags and filling flags. When a slot in the extraction storage area has an extraction flag of 1, it indicates that the corresponding slot in the target cache area can be used to extract the serial number. When a slot in the extraction storage area has a filling flag of 1, it indicates that the corresponding slot in the target cache area can be used to fill the serial number.
[0092] like Figure 9 As shown, Figure 9 This is a flowchart of a distributed identity number generation method provided in another embodiment of the present invention. The distributed identity number generation method of the present invention includes, but is not limited to, steps S900 and S910.
[0093] Step S900: Determine the number of bits in the intermediate distributed identity identifier;
[0094] Step S910: If the number of digits is less than the preset number of digits, pad with zeros until the number of digits reaches the preset number of digits.
[0095] In this embodiment, since the initial distributed identity number may not necessarily be a complete intermediate distributed identity number after the base conversion, the number of digits of the intermediate distributed identity number is judged. If the number of digits of the intermediate distributed identity number is less than the preset number of digits, zeros are added until the number of digits of the intermediate distributed identity number reaches the preset number of digits.
[0096] For example, when the initial distributed identity number is a 50-bit binary number, the 49-bit binary number after removing the first sign bit will be converted to a base. If the converted intermediate distributed identity number is an 11-bit decimal number, zeros will be added to make it a 15-bit decimal number. If the converted intermediate distributed identity number is already a 15-bit decimal number, zeros will not be added.
[0097] like Figure 10 As shown, Figure 10 This is a flowchart of a distributed identity number generation method provided in another embodiment of the present invention. The distributed identity number generation method of the present invention includes, but is not limited to, steps S1000 and S1010.
[0098] Step S1000: Set the filling strategy for the final distributed identity number, and store the final distributed identity number in the target cache area according to the filling strategy;
[0099] Step S1010: Set the extraction strategy for the final distributed identity number, and extract the final distributed identity number from the target cache area according to the extraction strategy.
[0100] In this embodiment of the invention, a filling strategy for the final distributed identity number is set, and the final distributed identity number is stored in the target cache area according to the filling strategy. The filling strategy includes an allow filling strategy and a deny filling strategy. When the filling strategy is an allow filling strategy, the final distributed identity number is filled and stored in the target cache area. When the filling strategy is a deny filling strategy, the final distributed identity number is prohibited from being filled and stored in the target cache area. A retrieval strategy for the final distributed identity number is also set, and the final distributed identity number is retrieved from the target cache area according to the retrieval strategy. The retrieval strategy includes an allow retrieval strategy and a deny retrieval strategy. When the retrieval strategy is an allow retrieval strategy, the final distributed identity number is retrieved from the target cache area. When the retrieval strategy is a deny retrieval strategy, the retrieval of the final distributed identity number is prohibited from being retrieved from the target cache area, thereby improving the generation efficiency of the distributed identity number.
[0101] For example, in this embodiment of the invention, two corresponding rings are provided: one is a ring-shaped target cache area, and the other is a ring-shaped extraction and filling flag area. Both the target cache area and the extraction and filling flag area have multiple corresponding slots. Each slot in the target cache area corresponds to a machine number and is used to store the final distributed identity identifier. The slots in the extraction and filling flag area are used to store the extraction flag and the filling flag. When the extraction flag is 1, an extraction permission policy is triggered; when the extraction flag is 0, an extraction rejection policy is triggered. Similarly, when the filling flag is 1, an extraction permission policy is triggered; when the filling flag is 0, an extraction rejection policy is triggered. Initially, the final distributed identity identifier fills the target cache area, and all filling flags in the slots of the extraction and filling flag area are set to 0, triggering the rejection policy. The extraction flag is then set to 1, and the final distributed identity identifier is extracted from the target cache area. For each slot's final distributed identity identifier extracted, the extraction flag of the next slot is pre-checked. When the last final distributed identity identifier in the current slot is extracted, the tail cursor is refreshed, and the extraction flag of the corresponding slot in the extraction and filling flag area is set to 0, and the filling flag is set to 1. When the percentage of final distributed identity identifiers in the target cache area falls below a preset ratio, such as 50%, asynchronous filling of final distributed identity identifiers is performed, one slot at a time. When filling a slot, the extraction flag for that slot is set to 1, and the filling flag for the next slot is pre-checked. After filling a slot, the trailing cursor is refreshed, and the filling flag is set to 0. This process continues, continuously extracting and filling final distributed identity identifiers into the target cache area to ensure that there is no shortage or overflow of final distributed identity identifiers in the target cache area. This improves the efficiency of distributed identity identifier generation.
[0102] In addition, embodiments of the present invention also provide an electronic device, which includes: a memory, a processor, and a computer program stored in the memory and executable on the processor.
[0103] The processor and memory can be connected via a bus or other means.
[0104] Memory, as a non-transitory computer-readable storage medium, can be used to store non-transitory software programs and non-transitory computer-executable programs. Furthermore, memory may include high-speed random access memory, and may also include non-transitory memory, such as at least one disk storage device, flash memory device, or other non-transitory solid-state storage device. In some embodiments, memory may optionally include memory remotely located relative to the processor, and these remote memories can be connected to the processor via a network. Examples of such networks include, but are not limited to, the Internet, intranets, local area networks, mobile communication networks, and combinations thereof.
[0105] It should be noted that the electronic device in this embodiment can be applied to the distributed identity number generation method as described in the above embodiments. The electronic device in this embodiment has the same inventive concept as the distributed identity number generation method as described in the above embodiments. Therefore, these embodiments have the same implementation principle and technical effect, which will not be described in detail here.
[0106] The non-transient software program and instructions required to implement the distributed identity number generation method as described in the above embodiments are stored in memory. When executed by a processor, the distributed identity number generation method as described in the above embodiments is executed, for example, the method described above is executed. Figure 2 Method steps S200 to S220, Figure 3 Method steps S310 to S320 in the text Figure 4 Method steps S400 to S420 Figure 5 Method steps S500, Figure 6 Method steps S600, Figure 7 Method steps S700 to S710, Figure 8 Method steps S800 to S840 Figure 9 Method steps S900 to S910, Figure 10 The method steps S1000 to S1010.
[0107] The electronic device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs.
[0108] Furthermore, embodiments of the present invention also provide a computer-readable storage medium storing computer-executable instructions that are executed by a processor or controller, for example, by a processor in the above-described electronic device embodiments, causing the processor to perform the distributed identity number generation method as described in the above embodiments, for example, performing the above-described... Figure 2 Method steps S200 to S220, Figure 3 Method steps S310 to S320 in the text Figure 4 Method steps S400 to S420 Figure 5 Method steps S500, Figure 6 Method steps S600, Figure 7 Method steps S700 to S710, Figure 8Method steps S800 to S840 Figure 9 Method steps S900 to S910, Figure 10 The method steps S1000 to S1010.
[0109] It will be understood by those skilled in the art that all or some of the steps and systems in the methods disclosed above can be implemented as software, firmware, hardware, and suitable combinations thereof. Some or all of the physical components can be implemented as software executed by a processor, such as a central processing unit, digital signal processor, or microprocessor, or as hardware, or as an integrated circuit, such as an application-specific integrated circuit. Such software can be distributed on a computer-readable medium, which can include computer storage media (or non-transitory media) and communication media (or transient media). As is known to those skilled in the art, the term computer storage media includes volatile and non-volatile, removable and non-removable media implemented in any method or technology for storing information (such as computer-readable instructions, data structures, program modules, or other data). Computer storage media includes, but is not limited to, RAM, ROM, EEPROM, flash memory or other memory technologies, CD-ROM, digital versatile disc (DVD) or other optical disc storage, magnetic cartridges, magnetic tape, disk storage or other magnetic storage devices, or any other medium that can be used to store desired information and is accessible to a computer. Furthermore, as is known to those skilled in the art, communication media typically contain computer-readable instructions, data structures, program modules, or other data in modulated data signals such as carrier waves or other transmission mechanisms, and may include any information delivery medium.
[0110] It will be understood by those skilled in the art that Figure 2-10 The technical solutions shown do not constitute a limitation on the embodiments of the present invention, and may include more or fewer steps than shown, or combine certain steps, or different steps.
[0111] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs.
[0112] Those skilled in the art will understand that all or some of the steps in the methods disclosed above, as well as the functional modules / units in the systems and devices, can be implemented as software, firmware, hardware, or suitable combinations thereof.
[0113] The terms "first," "second," "third," "fourth," etc. (if present) in the specification and accompanying drawings of this invention are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that embodiments of the invention described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover a non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0114] It should be understood that in this invention, "at least one (item)" refers to one or more, and "more than one" refers to two or more. "And / or" describes the relationship between related objects, indicating that three relationships can exist. For example, "A and / or B" can represent three cases: only A exists, only B exists, and both A and B exist simultaneously, where A and B can be singular or plural. The character " / " generally indicates that the preceding and following related objects are in an "or" relationship. "At least one (item) of the following" or similar expressions refer to any combination of these items, including any combination of single or plural items. For example, at least one (item) of a, b, or c can represent: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", where a, b, and c can be single or multiple.
[0115] In the several embodiments provided by this invention, it should be understood that the disclosed apparatus and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of the units described above is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between devices or units may be electrical, mechanical, or other forms.
[0116] The units described above as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0117] Furthermore, the functional units in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0118] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes multiple instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods of the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing programs, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0119] The above is a detailed description of the preferred embodiments of the present invention. However, the present invention is not limited to the above embodiments. Those skilled in the art can make various equivalent modifications or substitutions without departing from the spirit of the present invention. All such equivalent modifications or substitutions are included within the scope defined by the claims of the present invention.
Claims
1. A method for distributed identity number generation, the method comprising: The distributed identity number comprises a machine number, and the generation method comprises: obtaining the number of servers, the restart mode and the target machine number with the largest value; generating and assigning the machine number to the servers according to the number and the restart mode, and updating the target machine number, wherein the servers correspond to the machine numbers one by one; wherein generating and assigning the machine number to the servers according to the restart mode comprises: when the restart mode is updating a project version, resetting the recorded target machine number with the largest value to an initial value -1 by the first server successfully obtaining a distributed lock, and generating and assigning a new machine number starting from 0 and increasing for the servers of the new project version according to the number of servers; when the restart mode is a restart within a project version or a restart after server expansion, determining a new machine number according to the number of servers and the target machine number before the restart after a first resetting time, and assigning the new machine number to the servers; comparing the updated target machine number with a server number threshold, and verifying the machine number according to the comparison result.
2. The distributed identity number generation method of claim 1, wherein, The verification of the machine number according to the comparison result comprises at least one of the following: when the comparison result is that the updated target machine number is greater than the server number threshold, determining that the machine number is repeated; when the comparison result is that the updated target machine number is less than or equal to the server number threshold, determining that the machine number is unique.
3. The distributed identity number generation method of claim 1, wherein, After determining that the machine number is repeated when the comparison result is that the updated target machine number is greater than the server number threshold, the generation method further comprises: determining the servers corresponding to the machine numbers greater than the server number threshold, and marking the determined servers as overflow.
4. The distributed identity number generation method according to claim 1, wherein the distributed identity number further comprises a sign bit, a timestamp and a serial number, and the generation method further comprises: obtaining the serial number, wherein one machine number corresponds to multiple serial numbers; obtaining a start time and a current time, determining the timestamp and an alphabetical prefix according to the time difference between the start time and the current time; sequentially sorting and combining the sign bit, the timestamp, the machine number and the serial number to obtain an initial distributed identity number; performing a base conversion on the initial distributed identity number to obtain an intermediate distributed identity number; combining the intermediate distributed identity number with the alphabetical prefix to obtain a final distributed identity number.
5. The distributed identity number generation method of claim 4, wherein, Before combining the intermediate distributed identity number with the alphabetical prefix to obtain the final distributed identity number, the generation method further comprises: judging the number of bits of the intermediate distributed identity number, and when the number of bits is less than a preset number of bits, zero padding is performed to the number of bits to reach the preset number of bits.
6. The distributed identity number generation method of claim 4, wherein, Further comprising at least one of the following: setting a padding strategy for the final distributed identity number, and storing the final distributed identity number in a target cache area according to the padding strategy; The extraction strategy of the final distributed identity number is set, and the final distributed identity number is extracted from the target cache area according to the extraction strategy.
7. An electronic device, comprising: Comprise: A memory, a processor, and a computer program stored in the memory and capable of running on the processor, wherein the processor executes the computer program to implement the distributed identity number generation method in any one of claims 1 to 6.
8. A computer-readable storage medium, characterized in that, Computer executable instructions are stored, and the computer executable instructions are executed by a processor to implement the distributed identity number generation method in any one of claims 1 to 6.
Citation Information
Patent Citations
Method, device and system for generating machine ID in distributed system
CN109936642A
Method and device for quickly generating unique ID of distributed system
CN114153846A