A method and apparatus for generating an identity number
By obtaining the timestamp and critical resource value of the ID to be generated, and using the CAS algorithm to ensure the global uniqueness of the ID, the problems of thread suspension and clock rollback in the existing technology are solved, and the efficient generation of globally unique IDs is achieved.
Patent Information
- Application Number
- CN202010489189.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-06-02
- Publication Date
- 2025-11-07
- Estimated Expiration
- 2040-06-02
AI Technical Summary
Existing technologies suffer from thread suspension and clock rollback issues when generating distributed unique IDs, failing to guarantee the global uniqueness of IDs and system performance.
A lock-free algorithm is adopted to obtain the timestamp of the ID to be generated and the critical resource value. The CAS algorithm is used to ensure the global uniqueness of the ID, avoid thread suspension, and record abnormal IDs in the case of clock rollback to ensure the uniqueness of the generated ID.
This improves system performance, ensures the global uniqueness of generated IDs, avoids problems caused by thread suspension and clock rollback, and ensures that IDs are generated in an orderly manner within the same timestamp.
Smart Images

Figure CN111680052B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The embodiment of the present application relates to the field of Finteh (Financial Technology), in particular to a method and device for generating an identity number. BACKGROUND
[0002] With the development of computer technology, more and more technologies are applied in the financial field, and the traditional financial industry is gradually changing to financial technology, but due to the safety and real-time requirements of the financial industry, higher requirements are also put forward for the technology. In the complex distributed system in the financial field, a large amount of data and messages often need to be uniquely identified. For example, in the system of a certain bank's financial, payment and other business products, data is growing, and after the data is divided into databases and tables, a unique ID is needed to identify a piece of data or message. The self-incrementing ID of the database obviously cannot meet the demand, so a system capable of generating a globally unique ID is very necessary.
[0003] The prior art is based on the snowflake algorithm to generate a distributed unique ID, and the generated ID is sorted in ascending order according to time as a whole. However, in the case of multi-threading, in order to ensure the uniqueness of the ID generated by the snowflake algorithm, the critical resources of the last generated ID are protected by locking, that is, the millisecond timestamp and the sequence number used by the last generated ID are protected by locking. When locking the critical resources, if the thread fails to compete, the thread will be suspended, thereby reducing the performance of the system, and when the clock back phenomenon occurs, although the snowflake algorithm adopts the following ways to handle the clock back phenomenon: throwing an exception and letting the system call side handle it according to business requirements, or the thread entering a waiting state and continuing to generate the ID until the time returns to normal, or not handling it and tolerating the repeated ID generated in the clock back time, but these processing methods cannot balance the availability of the system and cannot guarantee the global uniqueness of the ID, and repeated IDs will occur.
[0004] In summary, there is an urgent need for a method for generating an identity number to solve the problems of thread suspension and clock back phenomenon in the prior art. SUMMARY
[0005] The embodiment of the present application provides a method and device for generating an identity number to solve the problems of thread suspension and clock back phenomenon in the prior art.
[0006] In a first aspect, the embodiment of the present application provides a method for generating an identity number, comprising:
[0007] obtaining a timestamp of an identity number ID to be generated and a critical resource value of an ID located before the ID to be generated, the critical resource value comprising a timestamp and a sequence number;
[0008] determining the current ID and the critical resource value corresponding to the current ID according to the sequence number of the ID located before the ID to be generated and the timestamp of the ID to be generated when it is determined that the timestamp of the ID to be generated is consistent with the timestamp of the ID located before the ID to be generated;
[0009] determining whether the critical resource value corresponding to the current ID is updated into the critical resource library;
[0010] if yes, confirming that the current ID is generated successfully.
[0011] In the technical solution, the timestamp of the ID to be generated can be determined according to the start time and the running time of the system, and then when it is determined that the timestamp of the ID to be generated is consistent with the timestamp of the ID located before the ID to be generated, the current ID and the critical resource value corresponding to the current ID are determined according to the sequence number of the ID located before the ID to be generated and the timestamp of the ID to be generated, and when it is determined that the critical resource value corresponding to the current ID is updated into the critical resource library, it is confirmed that the current ID is generated successfully. Since a lock-free algorithm is used to replace locking in the case of multi-threading, and it is determined whether the critical resource value corresponding to the current ID is updated into the critical resource library, thread suspension can be avoided, the system performance is improved, and the global uniqueness of the ID is ensured, thereby solving the problem of thread suspension in the prior art.
[0012] Optionally, the method further comprises:
[0013] resetting the sequence number of the ID to be generated when it is determined that the timestamp of the ID to be generated is inconsistent with the timestamp of the ID located before the ID to be generated;
[0014] determining the current ID and the critical resource value corresponding to the current ID according to the timestamp and the sequence number of the ID to be generated.
[0015] In the technical solution, since the sequence number of the ID to be generated can be reset when it is determined that the timestamp of the ID to be generated is inconsistent with the timestamp of the ID located before the ID to be generated, it is ensured that the sequence number of the ID to be generated is incremented from zero within the same timestamp, so that as many sequence numbers as possible are generated within the same timestamp, and then the current ID and the critical resource value corresponding to the current ID are determined according to the timestamp and the sequence number of the ID to be generated, thereby ensuring the global uniqueness of the current ID.
[0016] Optionally, the determining the current ID according to the sequence number of the ID located before the ID to be generated and the timestamp of the ID to be generated comprises:
[0017] determining the sequence number of the ID to be generated according to the sequence number of the ID before the ID to be generated;
[0018] determining the current ID according to the time stamp and the sequence number of the ID to be generated.
[0019] In the above technical solution, the sequence number of the ID to be generated can be automatically determined in an incremental manner according to the sequence number of the ID before the ID to be generated, and the current ID can be determined according to the time stamp and the sequence number of the ID to be generated.
[0020] Optionally, the determining the sequence number of the ID to be generated according to the sequence number of the ID before the ID to be generated comprises:
[0021] incrementing the sequence number of the ID before the ID to be generated to determine the sequence number of the ID to be generated.
[0022] In the above technical solution, the sequence number of the ID to be generated can be automatically determined by incrementing the sequence number of the ID before the ID to be generated within the same time stamp.
[0023] Optionally, the method further comprises:
[0024] when it is determined that the critical resource value corresponding to the current ID has not been updated into the critical resource library, continuing to acquire the time stamp of the next ID to be generated.
[0025] In the above technical solution, when it is determined that the critical resource value corresponding to the current ID has not been updated into the critical resource library, it indicates that the critical resource value corresponding to the current ID already exists in the critical resource library and does not need to be updated, thereby ensuring the uniqueness of the ID, and the time stamp of the next ID to be generated can be continuously acquired to generate the ID.
[0026] Optionally, the acquiring the time stamp of the ID to be generated comprises:
[0027] acquiring the startup time of the system and the running time of the system;
[0028] determining the time stamp of the ID to be generated according to the startup time of the system and the running time of the system.
[0029] In the above technical solution, the time stamp of the ID to be generated can be calculated by performing mathematical processing on the startup time of the system and the running time of the system. Since the startup time of the system and the running time of the system are real and valid and cannot be reversed, the clock rollback phenomenon can be avoided, thereby avoiding the generation of duplicate IDs.
[0030] Optionally, the current ID is provided with a clock rollback identification bit.
[0031] the confirming the current ID generation success comprises:
[0032] determining whether the timestamp of the ID to be generated has clock rollover;
[0033] if yes, setting a clock rollover identification bit in the current ID to 1, and recording the timestamp of the ID generated before the clock rollover, and confirming the current ID generation success;
[0034] otherwise, setting the clock rollover identification bit in the current ID to 0, and confirming the current ID generation success.
[0035] In the technical solution, when it is determined that the timestamp of the ID to be generated has clock rollover, the clock rollover identification bit in the current ID is set to 1, and the timestamp of the ID generated before the clock rollover is recorded, until the clock rollover is recovered, and then the clock rollover identification bit in the current ID is set to 0, and the current ID generation success is confirmed. In this way, the service availability can be guaranteed even in the case of clock rollover, the generated ID is globally unique, and the ID is kept in order within the same millisecond, so that the generation of duplicate IDs is avoided.
[0036] In a second aspect, an apparatus for generating an identity identification number is also provided, comprising:
[0037] an obtaining unit configured to obtain a timestamp of an identity identification number (ID) to be generated and a critical resource value of an ID before the ID to be generated, the critical resource value comprising a timestamp and a serial number;
[0038] a processing unit configured to, when it is determined that the timestamp of the ID to be generated is consistent with the timestamp of the ID before the ID to be generated, determine a current ID and a critical resource value corresponding to the current ID according to the serial number of the ID before the ID to be generated and the timestamp of the ID to be generated, determine whether the critical resource value corresponding to the current ID is updated into a critical resource library, and if yes, confirm the current ID generation success.
[0039] Optionally, the processing unit is further configured to:
[0040] when it is determined that the timestamp of the ID to be generated is inconsistent with the timestamp of the ID before the ID to be generated, reset the serial number of the ID to be generated;
[0041] determine the current ID and the critical resource value corresponding to the current ID according to the timestamp and the serial number of the ID to be generated.
[0042] Optionally, the processing unit is specifically configured to:
[0043] determining the sequence number of the ID to be generated according to the sequence number of the ID before the ID to be generated;
[0044] determining the current ID according to the timestamp and the sequence number of the ID to be generated.
[0045] Optionally, the processing unit is specifically configured to:
[0046] incrementing the sequence number of the ID before the ID to be generated to determine the sequence number of the ID to be generated.
[0047] Optionally, the processing unit is further configured to:
[0048] when determining that the critical resource value corresponding to the current ID is not updated into the critical resource library, continuing to acquire the timestamp of the next ID to be generated.
[0049] Optionally, the processing unit is specifically configured to:
[0050] acquiring the startup time of the system and the running time of the system;
[0051] determining the timestamp of the ID to be generated according to the startup time of the system and the running time of the system.
[0052] Optionally, the current ID is provided with a clock backtracking identification bit;
[0053] the processing unit is specifically configured to:
[0054] determining whether the timestamp of the ID to be generated is clock backtracking;
[0055] if yes, setting the clock backtracking identification bit in the current ID to 1, recording the timestamp of the ID generated before the clock backtracking, and confirming that the current ID is generated successfully;
[0056] otherwise, setting the clock backtracking identification bit in the current ID to 0, and confirming that the current ID is generated successfully.
[0057] In a third aspect, an embodiment of the present application provides a computing device, comprising:
[0058] a memory configured to store a computer program;
[0059] a processor configured to invoke the computer program stored in the memory, and execute the method for generating an identity number according to the obtained program.
[0060] In a fourth aspect, an embodiment of the present application provides a computer readable storage medium, which stores a computer executable program. The computer executable program is used for making a computer execute the method for generating an identity number. BRIEF DESCRIPTION OF DRAWINGS
[0061] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed to be used in the embodiments will be briefly introduced. Obviously, the drawings in the following description are only some embodiments of the present application, and other drawings can be obtained by those skilled in the art without any creative effort based on these drawings.
[0062] Figure 1 A schematic diagram of a system architecture provided by an embodiment of the present application is shown in the figure.
[0063] Figure 2 A flowchart of a method for generating an identity number provided by an embodiment of the present application is shown in the figure.
[0064] Figure 3 A structure diagram of an ID provided by an embodiment of the present application is shown in the figure.
[0065] Figure 4 A structure diagram of a critical resource value provided by an embodiment of the present application is shown in the figure.
[0066] Figure 5 A diagram of an ID with a clock back identification bit provided by an embodiment of the present application is shown in the figure.
[0067] Figure 6 A flowchart of another method for generating an identity number provided by an embodiment of the present application is shown in the figure.
[0068] Figure 7 A structure diagram of an apparatus for generating an identity number provided by an embodiment of the present application is shown in the figure. DETAILED DESCRIPTION
[0069] In order to make the objects, technical solutions and advantages of the present application more clear, the present application will be further described in detail below with reference to the drawings. Obviously, the described embodiments are only some embodiments of the present application, but not all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without any creative effort belong to the protection scope of the present application.
[0070] Figure 1 A system architecture provided by an embodiment of the present application is shown in the figure. Figure 1 As shown in the figure, the system architecture can be a server 100, which includes a processor 110, a communication interface 120 and a memory 130.
[0071] The communication interface 120 is configured to communicate with a terminal device and transceive information transmitted by the terminal device.
[0072] The processor 110 is a control center of the server 100, and is connected with various parts of the server 100 through various interfaces and lines, and performs various functions of the server 100 and processes data by running or executing software programs / modules stored in the memory 130 and calling data stored in the memory 130. Optionally, the processor 110 can include one or more processing units.
[0073] The memory 130 is configured to store software programs / modules, and the processor 110 performs various function applications and data processing by running the software programs / modules stored in the memory 130. The memory 130 can mainly include a program storage area and a data storage area, wherein the program storage area can store an operating system, at least one application program required by a function, and the like; and the data storage area can store data created according to business processing, and the like. In addition, the memory 130 can include a high-speed random access memory, and can also include a nonvolatile memory, for example, at least one magnetic disk storage device, a flash memory device, or other volatile solid-state memory device.
[0074] It should be noted that the above Figure 1 The structure shown in the figure is only an example, and the embodiments of the present application are not limited thereto.
[0075] Based on the above description, Figure 2 An exemplary flow of a method for generating an identity identification number provided by the embodiments of the present application is shown, which can be executed by an apparatus for generating an identity identification number.
[0076] As Figure 2 shown, the flow specifically includes:
[0077] In step 201, a time stamp of an identity identification number ID to be generated and a critical resource value of an ID located before the ID to be generated are obtained.
[0078] In the embodiments of the present application, the time stamp of the ID to be generated is obtained by calculating the start time of the system and the running time of the system, so that the ID to be generated can be prevented from being affected by server or local time correction during running, thereby avoiding clock back phenomenon, to ensure the global uniqueness of the ID. In addition, the critical resource value refers to a time stamp and a serial number used by an ID located before the ID to be generated.
[0079] In step 202, when it is determined that the timestamp of the ID to be generated is consistent with the timestamp of the ID located before the ID to be generated, a current ID and a critical resource value corresponding to the current ID are determined according to the sequence number of the ID located before the ID to be generated and the timestamp of the ID to be generated.
[0080] In the embodiment of the present application, after the timestamp of the ID to be generated and the timestamp of the ID located before the ID to be generated are acquired, it is determined whether the timestamp of the ID to be generated is consistent with the timestamp of the ID located before the ID to be generated. When it is determined that the timestamp of the ID to be generated is consistent with the timestamp of the ID located before the ID to be generated, the sequence number of the ID located before the ID to be generated is incremented, the sequence number of the ID to be generated is determined, and then the current ID is determined according to the timestamp and the sequence number of the ID to be generated, and the critical resource value corresponding to the current ID is determined. When it is determined that the timestamp of the ID to be generated is not consistent with the timestamp of the ID located before the ID to be generated, the sequence number of the ID to be generated is reset, the current ID and the critical resource value corresponding to the current ID are determined according to the timestamp and the sequence number of the ID to be generated. The increment means that the sequence number is increased one by one, for example, the sequence number of the ID located before the ID to be generated is increased by one in the same timestamp, and the subsequent sequence number can be generated in this way. The critical resource value can include the timestamp and the sequence number.
[0081] Specifically, in the case of multi-threading, if the server or local time is used to calculate the timestamp in the snowflake algorithm, when the clock back phenomenon occurs due to the correction of the server or local time, the snowflake algorithm may generate an already generated ID, thereby destroying the uniqueness of the ID. Therefore, the start time of the Java virtual machine and the running time of the Java virtual machine are obtained by using RuntimeMXBean (the management interface of the Java virtual machine runtime system), and the start time of the Java virtual machine and the running time of the Java virtual machine are used to calculate the timestamp of the ID to be generated, so that the timestamp can be prevented from being affected by the correction of the server or local time during the running process, thereby avoiding the clock back phenomenon. Then, it is determined whether the timestamp of the ID to be generated is consistent with the timestamp of the ID located before the ID to be generated. When it is determined that the timestamp of the ID to be generated is consistent with the timestamp of the ID located before the ID to be generated, the sequence number of the ID located before the ID to be generated is incremented, the sequence number of the ID to be generated is determined, and then the current ID is composed according to the timestamp and the sequence number of the ID to be generated according to the snowflake algorithm. The structure of the ID can be as shown in Figure 3 The ID is a 64-bit long type integer, Figure 3The 1st bit in the ID is used to represent an identification bit. Since the generated ID generally uses a positive integer, the highest bit in binary is 1 to represent a negative number, so the highest bit is fixed to 0. The 41st bit is used to represent a millisecond-level timestamp. The length of 41 bits can be used for up to 69 years. The 10th bit is used to represent the ID of a worker node, including a 5-bit data center ID and a 5-bit worker machine ID, which can represent up to 1024 worker nodes. The 12th bit is used to represent a sequence number, which is different from the sequence number generated in the same millisecond. A worker node can generate up to 4096 sequence numbers in one millisecond. In addition, in specific applications, since the data center ID and the worker machine ID are determined, the snowflake algorithm needs to obtain the timestamp and the sequence number of the last generated ID each time the ID is generated, so as to determine whether the clock rollback phenomenon occurs and calculate the sequence number of this time. Therefore, the critical resource value to be protected is the timestamp and the sequence number used in the last generated ID. The embodiment of the present application is to combine the 41-bit timestamp and the 12-bit sequence number into an integer, and store it by using an AtomicLong type data structure. The atomic operation of the AtomicLong data type is realized by using the CAS (CompareAnd Swap, compare and replace) algorithm, so as to protect the atomicity of the critical resource value "read-modify-write". Since the first 41 bits are the timestamp and the last 12 bits are the sequence number, when the timestamp or the sequence number needs to be obtained, the bit operation can be used to quickly obtain it. The atomic operation refers to an operation that cannot be interrupted by a thread scheduling mechanism. That is, once this operation starts, it will run to the end without any thread switching operation in between. The structure of the critical resource value can be as shown in Figure 4
[0082] Step 203, determining whether the critical resource value corresponding to the current ID is updated to the critical resource library.
[0083] In the embodiment of the present application, after the current ID and the critical resource value corresponding to the current ID are determined, it is determined whether the critical resource value corresponding to the current ID is updated into the critical resource library. Specifically, after the critical resource value corresponding to the current ID is determined, the critical resource value in the critical resource library is updated to the critical resource value corresponding to the current ID by using the CAS algorithm, and it is determined whether the update is successful. The critical resource library can be a memory or a storage. The CAS algorithm is a lock-free algorithm. In the CAS algorithm, there are three operands, a memory position V to be read and written, a value A to be compared, and a new value B to be written. The CAS algorithm updates the value of V to the new value B in an atomic manner only when the value of V is equal to A, and otherwise, the CAS algorithm does not perform any operation. Since the CAS algorithm can detect interference from other threads, the atomic "read-modify-write" operation sequence can be realized without using a lock. For example, thread X and thread Y update the value of the same variable C (for example, 5) at the same time. Since thread X and thread Y access the value 5 of the same variable C at the same time, the value of the memory is copied to the working memory space of each thread, that is, the value 5 of the same variable C is copied to the working memory space of each thread. Therefore, the value to be compared of thread X and thread Y is 5. If thread X competes with thread Y, thread X can update the value 5 of the same variable C, while other threads fail to compete (the threads that fail will not be suspended, but will be notified of the failure in this competition and can attempt again). Thread X updates the value 5 of the same variable C to 6 and writes it to the memory. At this time, for thread Y, the value of the memory becomes 6, which is inconsistent with the value to be compared 5. Therefore, thread Y fails to operate, but can perform subsequent competition operations. In this way, it can be ensured that the same ID is not generated, thereby ensuring the global uniqueness of the ID.
[0084] In step 204, if it is determined that the critical resource value corresponding to the current ID is updated into the critical resource library, it is confirmed that the current ID is successfully generated.
[0085] In the embodiment of the present application, when it is determined that the critical resource value corresponding to the current ID is updated into the critical resource library, it is confirmed that the current ID is successfully generated. When it is determined that the critical resource value corresponding to the current ID is not updated into the critical resource library, the timestamp of the next ID to be generated is continuously obtained. In addition, the current ID is provided with a clock rollback identification bit. It is determined whether the timestamp of the ID to be generated has a clock rollback. If the timestamp of the ID to be generated has a clock rollback, the clock rollback identification bit in the current ID is set to 1, and the timestamp of the ID generated before the clock rollback is recorded. It is confirmed that the current ID is successfully generated. If the timestamp of the ID to be generated does not have a clock rollback, the clock rollback identification bit in the current ID is set to 0, and it is confirmed that the current ID is successfully generated. The clock rollback identification bit can be used to distinguish normal IDs and abnormal IDs when the clock rollback phenomenon occurs. The schematic diagram of the ID with the clock rollback identification bit can be as follows:Figure 5 As shown, specifically, such as the last generated global unique ID, assuming A, is generated at 20XX-XX-XX 10:00:00, the serial number of A can be written as 1000000 0 XXXX, the middle 0 represents the identification bit of whether the clock rollback occurs, and XXXX represents the sequence number that is incremented within the same millisecond. However, when a new global unique ID is generated, it is found that the current timestamp obtained becomes 9 o'clock, which is smaller than the timestamp 10 o'clock of the last generated A, so the clock rollback occurs, and the middle clock rollback identification bit becomes 1. Why does it become 1? Because if it is still 0, actually, at 9 o'clock before, the sequence number 0900000 0 XXXX can have been generated, so in order to ensure that the generated ID is globally unique, the clock rollback identification bit needs to be changed to 1, so the currently generated ID should be 0900000 1 XXXX, recorded as sequence number B. At the same time, since the clock rollback occurs, the timestamp of the last global unique ID before the clock rollback, that is, the timestamp 10 o'clock of A, needs to be recorded. When the clock rollback occurs, the clock rollback needs to be recovered in order to ensure that the generated ID is globally unique. After the clock rollback phenomenon is found, the timestamp of the system at this time is 9 o'clock, but the timestamp of A is 10 o'clock, so it can be known that the timestamp of the last global unique ID generated normally is 10 o'clock. Since the global unique ID between 9 o'clock and 10 o'clock has been generated normally before, in order to ensure that the ID is not already generated (that is, globally unique), the clock rollback identification bits in the abnormal time period between 9 o'clock and 10 o'clock need to be set to 1. After 10 o'clock, since no timestamp after 10 o'clock has been generated before the clock rollback occurs, it can be confirmed that the time interval of the abnormal “9 o'clock-10 o'clock” has been recovered, and then the clock rollback identification bit can be restored to 0. Among them, the clock rollback identification bits in the normal time period should all be 0, and the clock rollback identification bits in the abnormal time period should all be 1 when the clock rollback phenomenon occurs.
[0086] In order to better explain the embodiment of generating the identity identification number of the application, the following describes a process of generating an identity identification number provided by the embodiment of the application through a specific implementation scenario.
[0087] As Figure 6 shown, the process includes the following steps:
[0088] Step 601, obtaining the timestamp of the ID to be generated.
[0089] The timestamp of the ID to be generated is calculated according to the start time of the system and the running time of the system.
[0090] Step 602, read the critical resource value of the last ID to obtain the timestamp and sequence number of the last ID.
[0091] The critical resource value of the last ID is read, and the timestamp and sequence number of the last ID are obtained by processing the critical resource value using bit operation.
[0092] Step 603, determine whether the timestamp of the ID to be generated is consistent with the timestamp of the last ID, if yes, execute step 604, if no, execute step 605.
[0093] After obtaining the timestamp of the ID to be generated and the timestamp of the ID located before the ID to be generated, it is determined whether the timestamp of the ID to be generated is consistent with the timestamp of the ID located before the ID to be generated.
[0094] Step 604, increment the sequence number of the last ID to determine the sequence number of the ID to be generated.
[0095] When it is determined that the timestamp of the ID to be generated is consistent with the timestamp of the ID located before the ID to be generated, the sequence number of the ID located before the ID to be generated is incremented to determine the sequence number of the ID to be generated.
[0096] Step 605, reset the sequence number of the ID to be generated.
[0097] When it is determined that the timestamp of the ID to be generated is not consistent with the timestamp of the ID located before the ID to be generated, the sequence number of the ID to be generated is reset, i.e. the sequence number of the ID to be generated is set to zero and is incremented from zero.
[0098] Step 606, compose the current ID according to the snowflake algorithm and determine the critical resource value corresponding to the current ID.
[0099] According to the timestamp and sequence number of the ID to be generated, the current ID is constructed according to the form of the snowflake algorithm, and the critical resource value corresponding to the current ID is determined.
[0100] Step 607, determine whether the CAS algorithm successfully updates the critical resource value in the critical resource library, if yes, execute step 608, if no, execute step 601.
[0101] After determining the critical resource value corresponding to the current ID, it is determined whether the CAS algorithm can successfully update the critical resource value in the critical resource library to the critical resource value corresponding to the current ID.
[0102] Step 608, confirm that the current ID is successfully generated.
[0103] The above embodiment shows that the time stamp of the to-be-generated identity identification number ID can be determined according to the start time and the running time of the system. Since the start time and the running time of the system are real and valid and cannot be reversed, the clock rollback phenomenon can be avoided, so that the repeated ID can be avoided. When it is determined that the time stamp of the to-be-generated ID is consistent with the time stamp of the ID located before the to-be-generated ID, the current ID and the critical resource value corresponding to the current ID are determined according to the sequence number of the ID located before the to-be-generated ID and the time stamp of the to-be-generated ID. When it is determined that the critical resource value corresponding to the current ID is updated to the critical resource library, it is confirmed that the current ID is generated successfully. Since a lock-free algorithm is used to replace locking in the multi-threaded case, whether the critical resource value corresponding to the current ID is updated to the critical resource library can be determined, so that the thread suspension can be avoided, the system performance is improved, the global uniqueness of the ID is ensured, and the problems of thread suspension and clock rollback phenomenon in the prior art are solved.
[0104] Based on the same technical concept, Figure 7 An apparatus for generating an identity identification number is exemplarily shown, which can execute the flow of the method for generating an identity identification number.
[0105] As Figure 7 shown, the apparatus comprises:
[0106] The acquisition unit 701 is configured to acquire a time stamp of a to-be-generated identity identification number ID and a critical resource value of an ID located before the to-be-generated ID, wherein the critical resource value comprises a time stamp and a sequence number.
[0107] The processing unit 702 is configured to, when it is determined that the time stamp of the to-be-generated ID is consistent with the time stamp of the ID located before the to-be-generated ID, determine a current ID and a critical resource value corresponding to the current ID according to the sequence number of the ID located before the to-be-generated ID and the time stamp of the to-be-generated ID; determine whether the critical resource value corresponding to the current ID is updated to a critical resource library; and if yes, confirm that the current ID is generated successfully.
[0108] Optionally, the processing unit 702 is further configured to:
[0109] reset the sequence number of the to-be-generated ID when it is determined that the time stamp of the to-be-generated ID is inconsistent with the time stamp of the ID located before the to-be-generated ID;
[0110] determine the current ID and the critical resource value corresponding to the current ID according to the time stamp and the sequence number of the to-be-generated ID.
[0111] Optionally, the processing unit 702 is specifically configured to:
[0112] determining the sequence number of the ID to be generated according to the sequence number of the ID before the ID to be generated;
[0113] determining the current ID according to the timestamp and the sequence number of the ID to be generated.
[0114] Optionally, the processing unit 702 is specifically configured to:
[0115] incrementing the sequence number of the ID before the ID to be generated to determine the sequence number of the ID to be generated.
[0116] Optionally, the processing unit 702 is further configured to:
[0117] when determining that the critical resource value corresponding to the current ID is not updated into the critical resource library, continuing to acquire the timestamp of the next ID to be generated.
[0118] Optionally, the processing unit 702 is specifically configured to:
[0119] acquiring the startup time of the system and the running time of the system;
[0120] determining the timestamp of the ID to be generated according to the startup time of the system and the running time of the system.
[0121] Optionally, the current ID is provided with a clock backtracking identification bit;
[0122] The processing unit 702 is specifically configured to:
[0123] determining whether the timestamp of the ID to be generated occurs clock backtracking;
[0124] if yes, setting the clock backtracking identification bit in the current ID to 1, recording the timestamp of the ID generated before the clock backtracking, and confirming that the current ID is generated successfully;
[0125] otherwise, setting the clock backtracking identification bit in the current ID to 0, and confirming that the current ID is generated successfully.
[0126] Based on the same technical concept, the embodiment of the present application provides a computing device, comprising:
[0127] a memory configured to store a computer program;
[0128] a processor configured to invoke the computer program stored in the memory and execute the method for generating an identity number according to the obtained program.
[0129] Based on the same technical concept, the embodiment of the present application provides a computer readable storage medium, which stores a computer executable program, and the computer executable program is used for making a computer execute the method for generating an identity number.
[0130] Those skilled in the art should understand that the embodiments of the present application can be provided as a method, a system, or a computer program product. Therefore, the present application can adopt a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present application can adopt a form of a computer program product implemented on one or more computer usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer usable program codes.
[0131] The present application is described with reference to flowcharts and / or block diagrams of the method, device (system), and computer program product according to the present application. It should be understood that each flow and / or block in the flowcharts and / or block diagrams, and the combination of the flows and / or blocks in the flowcharts and / or block diagrams can be realized by computer program instructions. These computer program instructions can be provided to a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing devices to produce a machine, so that the instructions executed by the computer or other programmable data processing devices generate a device for realizing the functions specified in the flowcharts and / or block diagrams. Figure 1 one or more flows and / or blocks Figure 1 an apparatus for performing the functions specified in one or more flows and / or blocks.
[0132] These computer program instructions can also be stored in a computer readable memory capable of guiding a computer or other programmable data processing devices to work in a specific way, so that the instructions stored in the computer readable memory produce a product including instruction apparatus, which realizes the functions specified in the flowcharts and / or block diagrams. Figure 1 one or more flows and / or blocks Figure 1 one or more flows and / or blocks.
[0133] These computer program instructions can also be loaded into a computer or other programmable data processing device, so that a series of operation steps are executed on the computer or other programmable device to produce a computer implemented process, so that the instructions executed on the computer or other programmable device provide a process for realizing the functions specified in the flowcharts and / or block diagrams. Figure 1 one or more flows and / or blocks Figure 1 one or more flows and / or blocks.
[0134] While the preferred embodiments of the application have been described, additional variations and modifications can be made to these embodiments by those skilled in the art once they have the benefit of the present disclosure without departing from the spirit and scope of the application. Accordingly, it is intended that the appended claims include all such modifications and variations as fall within the scope of the present application.
[0135] It is apparent that those skilled in the art can make various changes and modifications to the application without departing from the spirit and scope of the application. It is therefore intended that the present application cover all such changes and modifications that are within the scope of this application and its equivalents.
Claims
1. A method of generating an identity number, characterized by The method comprises: acquiring a timestamp of an identity identification number (ID) to be generated and a critical resource value of an ID located before the ID to be generated, the critical resource value comprising a timestamp and a serial number; wherein the acquiring of the timestamp of the ID to be generated comprises: acquiring a startup time of a Java virtual machine and a running time of the Java virtual machine by using a RuntimeMXBean, and calculating the timestamp of the ID to be generated according to the startup time of the Java virtual machine and the running time of the Java virtual machine; when it is determined that the timestamp of the ID to be generated is consistent with the timestamp of the ID located before the ID to be generated, determining a current ID and a critical resource value corresponding to the current ID according to the serial number of the ID located before the ID to be generated and the timestamp of the ID to be generated; wherein the current ID is a 64-bit long type integer, 1 bit in the 64-bit long type integer is used to represent an identification bit, 41 bits are used to represent the timestamp, 5 bits are used to represent a data center ID, 5 bits are used to represent a work machine ID, and 12 bits are used to represent the serial number; determining whether the critical resource value corresponding to the current ID is updated into a critical resource library; if yes, confirming that the current ID is generated successfully.
2. The method of claim 1, wherein, The method further comprises: when it is determined that the timestamp of the ID to be generated is not consistent with the timestamp of the ID located before the ID to be generated, resetting the serial number of the ID to be generated; determining the current ID and the critical resource value corresponding to the current ID according to the timestamp and the serial number of the ID to be generated.
3. The method of claim 1, wherein, The determining of the current ID according to the serial number of the ID located before the ID to be generated and the timestamp of the ID to be generated comprises: determining the serial number of the ID to be generated according to the serial number of the ID located before the ID to be generated; determining the current ID according to the timestamp and the serial number of the ID to be generated.
4. The method of claim 3, wherein, The determining of the serial number of the ID to be generated according to the serial number of the ID located before the ID to be generated comprises: incrementing the serial number of the ID located before the ID to be generated to determine the serial number of the ID to be generated.
5. The method of claim 1, wherein, The method further comprises: when it is determined that the critical resource value corresponding to the current ID is not updated into the critical resource library, continuing to acquire a timestamp of a next ID to be generated.
6. The method of claim 1, wherein, The current ID is provided with a clock rollback identification bit; The confirming of the current ID being generated successfully comprises: determining whether the timestamp of the ID to be generated has a clock rollback; if yes, setting the clock rollback identification bit in the current ID to 1, recording a timestamp of an ID generated before the clock rollback, and confirming that the current ID is generated successfully; otherwise, setting the clock rollback identification bit in the current ID to 0, and confirming that the current ID is generated successfully.
7. An apparatus for generating an identity number, the apparatus comprising: The method comprises: An acquisition unit is configured to acquire a timestamp of an identity identification number (ID) to be generated and a critical resource value of an ID located before the ID to be generated, the critical resource value comprising a timestamp and a serial number; when acquiring the timestamp of the ID to be generated, the acquisition unit is configured to acquire a startup time of a Java virtual machine and a running time of the Java virtual machine by using a RuntimeMXBean, and calculate the timestamp of the ID to be generated according to the startup time of the Java virtual machine and the running time of the Java virtual machine; A processing unit is configured to, when determining that the timestamp of the ID to be generated is consistent with the timestamp of the ID located before the ID to be generated, determine a current ID and a critical resource value corresponding to the current ID according to the serial number of the ID located before the ID to be generated and the timestamp of the ID to be generated; determine whether the critical resource value corresponding to the current ID is updated into a critical resource library; if yes, confirm that the current ID is generated successfully; the current ID is a 64-bit long type integer, 1 bit of which is used to represent an identification bit, 41 bits of which are used to represent the timestamp, 5 bits of which are used to represent a data center ID, 5 bits of which are used to represent a work machine ID, and 12 bits of which are used to represent the serial number.
8. The apparatus of claim 7, wherein, The processing unit is further configured to: when determining that the timestamp of the ID to be generated is inconsistent with the timestamp of the ID located before the ID to be generated, reset the serial number of the ID to be generated; determine the current ID and the critical resource value corresponding to the current ID according to the timestamp and the serial number of the ID to be generated.
9. The apparatus of claim 7, wherein, The processing unit is specifically configured to: determine the serial number of the ID to be generated according to the serial number of the ID located before the ID to be generated; determine the current ID according to the timestamp and the serial number of the ID to be generated.
10. The apparatus of claim 9, wherein, The processing unit is specifically configured to: increment the serial number of the ID located before the ID to be generated to determine the serial number of the ID to be generated.
11. The apparatus of claim 7, wherein, The processing unit is further configured to: when determining that the critical resource value corresponding to the current ID is not updated into the critical resource library, continue to acquire a timestamp of a next ID to be generated.
12. The apparatus of claim 8, wherein, The current ID is provided with a clock rollback identification bit; The processing unit is specifically configured to: determine whether the timestamp of the ID to be generated is clocked back; if yes, set the clock rollback identification bit in the current ID to 1, record a timestamp of an ID generated before the clock rollback, and confirm that the current ID is generated successfully; otherwise, set the clock rollback identification bit in the current ID to 0, and confirm that the current ID is generated successfully.
13. A computing device, comprising: The memory is configured to store a computer program; The processor is configured to call the computer program stored in the memory, and execute the method according to any one of claims 1 to 6 according to the obtained program. The computer readable storage medium stores a computer executable program, and the computer executable program is configured to enable a computer to execute the method according to any one of claims 1 to 6.
14. A computer-readable storage medium, characterized in that,
Citation Information
Patent Citations
Identification generation method and device
CN107229555A