A globally unique and incremental id generation method and system
By managing the machine code identifiers of the ID server and synchronizing the clock through the ZooKeeper server, the problem of duplicate unique primary key IDs generated by multiple servers in a distributed system is solved, thus ensuring the uniqueness and reliability of the system IDs.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- SHENZHEN IPANEL TECH LTD
- Filing Date
- 2020-01-14
- Publication Date
- 2026-05-08
AI Technical Summary
In distributed systems, how to ensure that the system-unique primary key IDs generated by multiple servers are not duplicated, especially when there are multiple servers used to generate system-unique primary key IDs in a distributed system? Existing technologies rely on the uniqueness of machine identifiers, which leads to high manual maintenance costs and clock rollback causing duplicate ID generation.
The system maintains the machine code identifier of each ID server within the ZooKeeper server, generates machine code identifiers for unregistered ID servers in ascending order of machine code identifiers, and achieves clock synchronization through a network time synchronization protocol, thus resolving the problem of duplicate ID generation caused by clock rollback.
This ensures the uniqueness of system IDs generated by the ID server, reduces manual maintenance costs, avoids duplicate ID generation caused by clock rollback, and improves system reliability and efficiency.
Smart Images

Figure CN113190547B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of database technology, and more specifically, to a global and incremental ID generation method and system. Background Technology
[0002] In distributed systems, when a single table cannot handle a massive amount of data, database sharding and table partitioning become necessary. To facilitate querying, updating, and migrating of the sharded data, a unique primary key ID needs to be generated. This unique primary key ID has the following characteristics: it is unique throughout the entire system; it is a numeric type and its value increases sequentially; and it is short and easy to query.
[0003] In distributed systems, there are multiple servers used to generate unique primary key IDs. Ensuring that the unique primary key IDs generated by these servers are not duplicated has become a pressing technical problem in this field. Summary of the Invention
[0004] In view of this, the present invention provides a global and incremental ID generation method and system to ensure the uniqueness of system IDs generated by multiple servers in a distributed system.
[0005] To achieve the above-mentioned objectives, the present invention provides the following specific technical solution:
[0006] A global and incremental ID generation method is applied to a global and incremental ID generation system, the ID generation system including a ZooKeeper server and multiple ID servers, the method comprising:
[0007] If the ZooKeeper server detects that the target ID server is not registered, it generates a ZooKeeper persistent sequential node corresponding to the target ID server, where the target ID server is any ID server in the ID generation system.
[0008] The ZooKeeper server generates the machine code identifier of the target ID server according to the machine code identifier stored in the ZooKeeper persistent sequence node corresponding to the registered ID server, in ascending order of the machine code identifier, and stores the machine code identifier of the target ID server in the corresponding ZooKeeper persistent sequence node, thus completing the registration of the target ID server.
[0009] The target ID server generates a system ID based on its own machine code identifier. The system ID includes a first preset number of timestamps, a second preset number of machine code identifiers, and a third preset number of auto-incrementing sequences.
[0010] Optionally, before the target ID server generates a system ID based on its own machine code identifier, the method further includes:
[0011] When the ZooKeeper server detects that the target ID server has been registered, it obtains the local system time of the target ID server.
[0012] The ZooKeeper server obtains the average local system time of all ID servers that are currently providing services;
[0013] The ZooKeeper server determines whether the absolute value of the local system time of the target ID server and the average value is less than a threshold.
[0014] If the value is less than the threshold, the ZooKeeper server indicates that the target ID server has started successfully.
[0015] If the value is not less than the threshold, the ZooKeeper server will indicate that the target ID server failed to start and will activate the alarm mechanism.
[0016] Optionally, the machine code identifier of the target ID server includes the data center code and the machine code.
[0017] Optionally, the method further includes:
[0018] Each ID server in the ID generation system achieves clock synchronization based on a network time synchronization protocol.
[0019] Optionally, the method further includes:
[0020] The target ID server obtains the current system time and the latest time of generating the system ID within the storage period adjacent to the current system time;
[0021] The target ID server calculates the time deviation between the current system time and the latest time when the system ID was generated.
[0022] The target ID server determines the ID generation strategy based on the time deviation value and executes the ID generation strategy.
[0023] Optionally, the target ID server calculates the time deviation between the current system time and the latest time that generated the system ID, including:
[0024] The target ID server determines whether the latest time of generating the system ID is greater than the current system time;
[0025] If it is greater than the current system time, the target ID server calculates the time deviation between the latest time of generating the system ID and the current system time due to the clock rollback.
[0026] If the time is not greater than the current system time, the target ID server calculates the incremental time deviation between the current system time and the latest time when the system ID was generated.
[0027] Optionally, the target ID server determines an ID generation strategy based on the time deviation value and executes the ID generation strategy, including:
[0028] The target ID server, provided that the incremental time deviation value is not less than 0, obtains the auto-incrementing sequence value of the system ID corresponding to the latest time of generating the system ID;
[0029] The target ID server determines whether the auto-incrementing sequence value is greater than a preset maximum increment value;
[0030] If so, the local clock of the target ID server starts counting from the next millisecond to obtain the latest current system time, generates a system ID based on its own machine code identifier and the latest current system time, and updates the latest time of the generated system ID and the corresponding auto-incrementing sequence value of the system ID;
[0031] If not, the target ID server generates a system ID based on its own machine code identifier and the current system time, and updates the latest time of the generated system ID and the auto-incrementing sequence value of the corresponding system ID.
[0032] Optionally, the target ID server determines an ID generation strategy based on the time deviation value and executes the ID generation strategy, including:
[0033] The target ID server determines whether the callback time deviation value is less than the callback threshold.
[0034] If the value is not less than the callback threshold, the target ID server will activate an alarm mechanism.
[0035] If the time is less than the threshold, the local clock of the target ID server continues to count for twice the callback threshold time to obtain the latest current system time.
[0036] The target ID server determines whether the latest time of generating the system ID is greater than the latest current system time.
[0037] If the latest time of generating the system ID is greater than the latest current system time, the target ID server will activate an alarm mechanism;
[0038] If the latest time of generating the system ID is not greater than the latest current system time, the target ID server returns the step of calculating the incremental time deviation between the current system time and the latest time of generating the system ID.
[0039] A global and incremental ID generation system, comprising a ZooKeeper server and multiple ID servers;
[0040] The ZooKeeper server is used to generate a ZooKeeper persistent sequence node corresponding to the target ID server when it is detected that the target ID server is not registered. The target ID server can be any ID server in the ID generation system. Based on the machine code identifier stored in the ZooKeeper persistent sequence node corresponding to the registered ID server, the machine code identifier of the target ID server is generated in ascending order of the machine code identifier, and the machine code identifier of the target ID server is stored in the corresponding ZooKeeper persistent sequence node to complete the registration of the target ID server.
[0041] The target ID server is used to generate a system ID based on its own machine code identifier. The system ID includes a first preset number of timestamps, a second preset number of machine code identifiers, and a third preset number of auto-incrementing sequences.
[0042] Optionally, the ZooKeeper server is also used for:
[0043] Before the target ID server generates a system ID based on its own machine code identifier, and if the target ID server is detected to be registered, the local system time of the target ID server is obtained.
[0044] Get the average local system time of all ID servers that are currently providing services;
[0045] Determine whether the absolute value of the local system time of the target ID server and the average value is less than a threshold;
[0046] If the value is less than the threshold, it indicates that the target ID server has started successfully.
[0047] If the value is not less than the threshold, a message indicating that the target ID server failed to start will be displayed, and an alarm mechanism will be activated.
[0048] Optionally, the machine code identifier of the target ID server includes the data center code and the machine code.
[0049] Optionally, each ID server in the ID generation system is also used to achieve clock synchronization based on a network time synchronization protocol.
[0050] Optionally, the target ID server is further used for:
[0051] Get the current system time and the latest time when the system ID was generated;
[0052] Calculate the time deviation between the current system time and the latest time when the system ID was generated;
[0053] The ID generation strategy is determined based on the time deviation value, and the ID generation strategy is executed.
[0054] Optionally, the target ID server is specifically used for:
[0055] Determine whether the latest time of generating the system ID is greater than the current system time;
[0056] If it is greater than the current system time, calculate the time difference between the latest time of generating the system ID and the current system time due to clock rollback.
[0057] If it is not greater than the current system time, calculate the incremental time deviation value between the current system time and the latest time of generating the system ID.
[0058] Optionally, the target ID server is specifically used for:
[0059] If the incremental time deviation value is not less than 0, obtain the auto-incrementing sequence value of the system ID corresponding to the latest time of generating the system ID;
[0060] Determine whether the incrementing sequence value is greater than the preset maximum increment value;
[0061] If so, the local clock of the target ID server starts counting from the next millisecond to obtain the latest current system time, generates a system ID based on its own machine code identifier and the latest current system time, and updates the latest time of the generated system ID and the corresponding auto-incrementing sequence value of the system ID;
[0062] If not, generate a system ID based on its own machine code identifier and the current system time, and update the latest time of the generated system ID and the auto-incrementing sequence value of the corresponding system ID.
[0063] Optionally, the target ID server is specifically used for:
[0064] Determine whether the callback time deviation value is less than the callback threshold;
[0065] If the value is not less than the aforementioned callback threshold, an alarm mechanism will be activated.
[0066] If the time is less than the callback threshold, the local clock of the target ID server continues to count for twice the callback threshold time to obtain the latest current system time.
[0067] Determine whether the latest time of generating the system ID is greater than the latest current system time;
[0068] If the latest time of generating the system ID is greater than the latest current system time, an alarm mechanism is activated.
[0069] If the latest time of generating the system ID is not greater than the latest current system time, return to the step of calculating the incremental time deviation between the current system time and the latest time of generating the system ID.
[0070] Compared with the prior art, the beneficial effects of the present invention are as follows:
[0071] This invention discloses a global and incremental ID generation method. By maintaining the machine code identifier of each ID server in the system through a ZooKeeper server, the method generates machine code identifiers for unregistered ID servers according to the machine code identifiers stored in the ZooKeeper persistent sequential nodes corresponding to the registered ID servers, following the incremental order of the machine code identifiers. This ensures the uniqueness of the machine code identifier of each ID server in the system, thereby ensuring the uniqueness of the system ID generated by the ID server based on its own machine code identifier, which includes a timestamp of a first preset number of digits, a machine code identifier of a second preset number of digits, and an auto-incrementing sequence of a third preset number of digits. Attached Figure Description
[0072] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on the provided drawings without creative effort.
[0073] Figure 1 This is a schematic diagram of the structure of a system ID generated using the snowflake algorithm in the prior art;
[0074] Figure 2 This is a flowchart illustrating a global and incremental ID generation method disclosed in an embodiment of the present invention;
[0075] Figure 3 This is a flowchart illustrating another global and incremental ID generation method disclosed in an embodiment of the present invention;
[0076] Figure 4 This is a flowchart illustrating another global and incremental ID generation method disclosed in an embodiment of the present invention;
[0077] Figure 5 This is a flowchart illustrating another global and incremental ID generation method disclosed in an embodiment of the present invention;
[0078] Figure 6 This is a schematic diagram of the structure of a global and incremental ID generation system disclosed in an embodiment of the present invention. Detailed Implementation
[0079] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0080] Existing technology uses the snowflake algorithm to generate a 64-bit binary positive integer, which is then converted into a decimal ID. (See also: [link to relevant documentation]). Figure 1 The 64-bit binary positive integer generated using the Snowflake algorithm includes: a 1-bit identifier, a 41-bit timestamp, a 10-bit machine identifier, and a 12-bit sequence value. The 1-bit identifier is always 0; the 41-bit timestamp does not store the current system time, but rather the difference between timestamps (current system timestamp - start timestamp); the 10-bit machine identifier can be deployed across 1024 nodes; and the 12-bit sequence value is counted in milliseconds, allowing each node to generate 4096 IDs per millisecond. This method of generating IDs using the Snowflake algorithm can generate 4.096 million IDs per second, demonstrating high performance. Because the timestamp is auto-incrementing in the high-order bits and the sequence value is low-order, the entire ID is trend-incrementing, generating globally unique and increasing IDs.
[0081] The inventors studied the above-mentioned method of generating IDs using the snowflake algorithm and found that the method relies on the uniqueness of the machine identifier. If the machine identifier is not unique in the distributed system, resulting in the generation of duplicate IDs, and there are multiple servers used to generate IDs in the distributed system, manual maintenance is costly. How to ensure the uniqueness of the machine identifier of multiple servers in the system has become a technical problem that urgently needs to be solved in this field.
[0082] To address the aforementioned technical problems, this invention discloses a global and incremental ID generation method, applicable to a global and incremental ID generation system. The ID generation system includes a ZooKeeper server and multiple ID servers. The ZooKeeper server maintains the machine code identifier of each ID server within the system, ensuring the uniqueness of the machine code identifier of each ID server in the system. This, in turn, ensures the uniqueness of the system ID generated by the ID server based on its own machine code identifier, which includes a timestamp of a first preset number of bits, a machine code identifier of a second preset number of bits, and an auto-incrementing sequence of a third preset number of bits.
[0083] Please see Figure 2 The global and incremental ID generation method disclosed in this embodiment specifically includes the following steps:
[0084] S101: If the ZooKeeper server detects that the target ID server is not registered, it generates a ZooKeeper persistent sequential node corresponding to the target ID server;
[0085] The target ID server can be any ID server in the ID generation system.
[0086] When an ID server starts up, it needs to interact with the ZooKeeper server. The ZooKeeper server checks whether the ID server has been registered. If it has not been registered, it registers the ID server. If it has been registered, the ID server no longer needs to interact with the ZooKeeper server when it is detected that the ID server has started up successfully, and it can generate the system ID. Therefore, the process of the ID server generating the system ID has very little dependence on the ZooKeeper server.
[0087] S102: The ZooKeeper server generates the machine code identifier of the target ID server according to the machine code identifier stored in the ZooKeeper persistent sequence node corresponding to the registered ID server, in ascending order of the machine code identifier, and stores the machine code identifier of the target ID server in the corresponding ZooKeeper persistent sequence node, thereby completing the registration of the target ID server.
[0088] If the machine code identifiers of the registered ID servers are 0000000001, 000000002, and 0000000003, the machine code identifier of the target ID server will be generated as 0000000004, following the ascending order of the machine code identifiers.
[0089] If the machine code identifier includes a 2-digit data center code and an 8-digit machine code, such as the machine code identifiers of the registered ID server in data center 01 being 0100000001, 0100000002, and 0100000003, the machine code identifier of the target ID server in data center 01 generated in ascending order of machine code identifiers will be 0100000004.
[0090] ZooKeeper persistent sequential nodes are used to store the machine code identifier of the corresponding target ID server. The ZooKeeper server determines whether the target ID server has been registered by checking whether the machine code identifier is stored in the ZooKeeper persistent sequential node corresponding to the target ID server.
[0091] S103: The target ID server generates a system ID based on its own machine code identifier. The system ID includes a timestamp of a first preset number of bits, a machine code identifier of a second preset number of bits, and an auto-incrementing sequence of a third preset number of bits.
[0092] Specifically, the system ID can include a 1-bit identifier, a 41-bit timestamp, a 10-bit machine code identifier, and a 12-bit auto-incrementing sequence. It should be noted that the values of the first, second, and third preset number of bits can be adjusted according to business needs.
[0093] This embodiment discloses a global and incremental ID generation method. By maintaining the machine code identifier of each ID server in the system through the ZooKeeper server, the machine code identifier of the unregistered ID server is generated according to the machine code identifier stored in the ZooKeeper persistent sequential node corresponding to the registered ID server, in the order of increasing machine code identifier. This ensures the uniqueness of the machine code identifier of each ID server in the system, and thus ensures the uniqueness of the system ID generated by the ID server based on its own machine code identifier, which includes a timestamp of a first preset number of digits, a machine code identifier of a second preset number of digits, and an auto-incrementing sequence of a third preset number of digits.
[0094] The inventors also discovered through research that the above method of generating IDs using the snowflake algorithm relies on the machine's clock. In a distributed scenario, the clocks on each ID server cannot be completely synchronized, and server clock rollback is frequently encountered, usually between 10ms and 100ms. If the ID server clock rollback occurs, it will lead to the generation of duplicate IDs.
[0095] To address the problem of duplicate ID generation caused by ID server clock rollback, this invention provides a global and incremental ID generation method that offers solutions from two aspects.
[0096] One method to resolve the issue of duplicate ID generation caused by server clock rollback is to determine whether the ID server has experienced clock rollback before starting the ID server. For details, please refer to [link to relevant documentation]. Figure 3 A global and incremental ID generation method includes the following steps:
[0097] S201: If the ZooKeeper server detects that the target ID server is not registered, it generates a ZooKeeper persistent sequential node corresponding to the target ID server;
[0098] S202: The ZooKeeper server generates the machine code identifier of the target ID server according to the machine code identifier stored in the ZooKeeper persistent sequence node corresponding to the registered ID server, in ascending order of the machine code identifier, and stores the machine code identifier of the target ID server in the corresponding ZooKeeper persistent sequence node, thereby completing the registration of the target ID server.
[0099] S203: When the ZooKeeper server detects that the target ID server has been registered, it obtains the local system time of the target ID server;
[0100] S204: The ZooKeeper server obtains the average local system time of all ID servers that are currently providing services;
[0101] S205: The ZooKeeper server determines whether the absolute value of the local system time of the target ID server and the average value is less than a threshold.
[0102] If the value is not less than the threshold, S206: The ZooKeeper server indicates that the target ID server failed to start and initiates an alarm mechanism.
[0103] If the value is less than the threshold, S207: The ZooKeeper server indicates that the target ID server has started successfully;
[0104] S208: The target ID server generates a system ID based on its own machine code identifier. The system ID includes a timestamp of a first preset number of bits, a machine code identifier of a second preset number of bits, and an auto-incrementing sequence of a third preset number of bits.
[0105] In this method, the ZooKeeper server detects whether the registered ID server has undergone clock rollback at startup, thus avoiding the problem of duplicate ID generation caused by the ID server that has undergone clock rollback generating system IDs.
[0106] Another method to address the issue of duplicate ID generation caused by server clock rollback is to synchronize the clocks of each ID server in the system based on a network time synchronization protocol, such as synchronizing the current system time of all ID servers every 3 seconds (this time is configurable). However, network latency may cause clock rollback. Therefore, for each ID server, such as the target ID server in this embodiment, the current system time and the latest time of generating the system ID are obtained. The time deviation between the current system time and the latest time of generating the system ID is calculated, and the ID generation strategy is determined and executed based on the time deviation.
[0107] For details, please refer to Figure 4 The above-mentioned global and incremental ID generation method includes the following steps:
[0108] S301: The target ID server obtains the current system time and the latest time since the system ID was generated;
[0109] It should be noted that the target ID server stores the latest time when the system ID was generated, accurate to milliseconds, and stores the auto-incrementing sequence value corresponding to the system ID, in the format of milliseconds - auto-incrementing sequence value.
[0110] S302: The target ID server calculates the time deviation between the current system time and the latest time when the system ID was generated;
[0111] When the latest time when the target ID server generates the system ID is greater than the current system time, the target ID server calculates the time rollback deviation between the latest time when the system ID is generated and the current system time due to clock rollback.
[0112] When the latest time when the target ID server generates the system ID is not greater than the current system time, the target ID server calculates the incremental time deviation value between the current system time and the latest time when the system ID is generated.
[0113] S303: The target ID server determines the ID generation strategy based on the time deviation value and executes the ID generation strategy.
[0114] ID generation strategies generally fall into three categories: normal incrementing, delayed waiting, and timeout exception. For details, please refer to [link / reference needed]. Figure 5 The global and incremental ID generation method shown:
[0115] S401: The target ID server obtains the current system time and the latest time when the system ID was generated;
[0116] S402: The target ID server determines whether the latest time that generated the system ID is greater than the current system time;
[0117] If the latest time of generating the system ID is not greater than the current system time, and the target ID server has not performed a clock rollback, execute S403: The target ID server obtains the auto-incrementing sequence value of the system ID corresponding to the latest time of generating the system ID;
[0118] S404: The target ID server determines whether the auto-incrementing sequence value is greater than the preset maximum increment value;
[0119] If the auto-incrementing sequence value is greater than the preset maximum increment value, such as 4096, the timestamp corresponding to the current system time has been used up. Execute S405: The local clock of the target ID server starts counting from the next millisecond to obtain the latest current system time.
[0120] That is, the latest current system time is the original current system time + 1 millisecond, and the auto-incrementing sequence value is 0.
[0121] S406: The target ID server generates a system ID based on its own machine code identifier and the latest current system time, and updates the latest time of the generated system ID and the corresponding auto-incrementing sequence value of the system ID;
[0122] If the auto-incrementing sequence value is not greater than the preset maximum increment value, i.e., the normal incrementing situation, execute S406;
[0123] If the latest time of generating the system ID is greater than the current system time, that is, the local clock of the target ID server has been rolled back, S407: The target ID server calculates the rollback time deviation value between the latest time of generating the system ID and the current system time due to the clock rollback.
[0124] S408: The target ID server determines whether the callback time deviation value is less than the callback threshold;
[0125] If the callback time deviation is not less than the callback threshold, that is, the callback duration is greater than the tolerable limit, execute S409: Target ID server starts alarm mechanism;
[0126] If the callback time deviation is less than the callback threshold, that is, the callback duration is not greater than the tolerable limit, execute S410: the local clock of the target ID server continues to count for twice the callback threshold time to obtain the latest current system time;
[0127] S411: The target ID server determines whether the latest time that generated the system ID is greater than the latest current system time;
[0128] If the latest time that generated the system ID is greater than the latest current system time, execute S409;
[0129] If the latest time of generating the system ID is not greater than the latest current system time, execute S403.
[0130] In this method, the ID server stores the latest time of the generated system ID and the corresponding auto-incrementing sequence value, compares the latest time of the generated system ID with the current system time, determines whether the local clock of the ID server has been rolled back, and determines the corresponding ID generation strategy to solve the problem of duplicate ID generation caused by server clock rollback.
[0131] Based on the global and incremental ID generation method disclosed in the above embodiments, this embodiment correspondingly discloses a global and incremental ID generation system. Please refer to [link / reference needed]. Figure 6 This includes ZooKeeper service 100 and multiple ID servers 200;
[0132] The ZooKeeper server 100 is used to generate a ZooKeeper persistent sequence node corresponding to the target ID server when it is detected that the target ID server is not registered. The target ID server is any ID server 200 in the ID generation system. Based on the machine code identifier stored in the ZooKeeper persistent sequence node corresponding to the registered ID server, the machine code identifier of the target ID server is generated in ascending order of the machine code identifier, and the machine code identifier of the target ID server is stored in the corresponding ZooKeeper persistent sequence node to complete the registration of the target ID server.
[0133] The target ID server 200 is used to generate a system ID based on its own machine code identifier. The system ID includes a first preset number of timestamps, a second preset number of machine code identifiers, and a third preset number of auto-incrementing sequences.
[0134] Optionally, the ZooKeeper server 401 is also used for:
[0135] Before the target ID server generates a system ID based on its own machine code identifier, and if the target ID server is detected to be registered, the local system time of the target ID server is obtained.
[0136] Get the average local system time of all ID servers that are currently providing services;
[0137] Determine whether the absolute value of the local system time of the target ID server and the average value is less than a threshold;
[0138] If the value is less than the threshold, it indicates that the target ID server has started successfully.
[0139] If the value is not less than the threshold, a message indicating that the target ID server failed to start will be displayed, and an alarm mechanism will be activated.
[0140] Optionally, the machine code identifier of the target ID server includes the data center code and the machine code.
[0141] Optionally, each ID server in the ID generation system is also used to achieve clock synchronization based on a network time synchronization protocol.
[0142] Optionally, the target ID server is further used for:
[0143] Get the current system time and the latest time when the system ID was generated;
[0144] Calculate the time deviation between the current system time and the latest time when the system ID was generated;
[0145] The ID generation strategy is determined based on the time deviation value, and the ID generation strategy is executed.
[0146] Optionally, the target ID server is specifically used for:
[0147] Determine whether the latest time of generating the system ID is greater than the current system time;
[0148] If it is greater than the current system time, calculate the time difference between the latest time of generating the system ID and the current system time due to clock rollback.
[0149] If it is not greater than the current system time, calculate the incremental time deviation value between the current system time and the latest time of generating the system ID.
[0150] Optionally, the target ID server is specifically used for:
[0151] If the incremental time deviation value is not less than 0, obtain the auto-incrementing sequence value of the system ID corresponding to the latest time of generating the system ID;
[0152] Determine whether the incrementing sequence value is greater than the preset maximum increment value;
[0153] If so, the local clock of the target ID server starts counting from the next millisecond to obtain the latest current system time, generates a system ID based on its own machine code identifier and the latest current system time, and updates the latest time of the generated system ID and the corresponding auto-incrementing sequence value of the system ID;
[0154] If not, generate a system ID based on its own machine code identifier and the current system time, and update the latest time of the generated system ID and the auto-incrementing sequence value of the corresponding system ID.
[0155] Optionally, the target ID server is specifically used for:
[0156] Determine whether the callback time deviation value is less than the callback threshold;
[0157] If the value is not less than the aforementioned callback threshold, an alarm mechanism will be activated.
[0158] If the time is less than the callback threshold, the local clock of the target ID server continues to count for twice the callback threshold time to obtain the latest current system time.
[0159] Determine whether the latest time of generating the system ID is greater than the latest current system time;
[0160] If the latest time of generating the system ID is greater than the latest current system time, an alarm mechanism is activated.
[0161] If the latest time of generating the system ID is not greater than the latest current system time, return to the step of calculating the incremental time deviation between the current system time and the latest time of generating the system ID.
[0162] This embodiment discloses a global and incremental ID generation system. It maintains the machine code identifier of each ID server within the system through a ZooKeeper server. Based on the machine code identifiers stored in the ZooKeeper persistent sequential nodes corresponding to registered ID servers, it generates machine code identifiers for unregistered ID servers in ascending order of machine code identifiers. This ensures the uniqueness of the machine code identifier of each ID server in the system, thereby guaranteeing the uniqueness of the system ID generated by the ID server based on its own machine code identifier, which includes a timestamp of a first preset number of digits, a machine code identifier of a second preset number of digits, and an auto-incrementing sequence of a third preset number of digits.
[0163] Meanwhile, ZooKeeper servers detect whether registered ID servers have undergone clock rollback during startup, thus avoiding the problem of duplicate ID generation caused by ID servers that have undergone clock rollback generating system IDs.
[0164] The ID server stores the latest time of the generated system ID and its corresponding auto-incrementing sequence value. It then compares the latest time of the generated system ID with the current system time to determine whether the ID server's local clock has rolled back, and determines the corresponding ID generation strategy to solve the problem of duplicate ID generation caused by server clock rollback.
[0165] The above description of the disclosed embodiments enables those skilled in the art to make or use the invention. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the invention. Therefore, the invention is not to be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A global and incremental ID generation method, characterized in that, An ID generation system applied globally and incrementally, the ID generation system including a ZooKeeper server and multiple ID servers, the method comprising: If the ZooKeeper server detects that the target ID server is not registered, it generates a ZooKeeper persistent sequential node corresponding to the target ID server, where the target ID server is any ID server in the ID generation system. The ZooKeeper server generates the machine code identifier of the target ID server according to the machine code identifier stored in the ZooKeeper persistent sequence node corresponding to the registered ID server, in ascending order of the machine code identifier, and stores the machine code identifier of the target ID server in the corresponding ZooKeeper persistent sequence node, thereby completing the registration of the target ID server and ensuring the uniqueness of the machine code identifier of each ID server in the system. The target ID server generates a system ID based on its own machine code identifier. The system ID includes a first preset number of timestamps, a second preset number of machine code identifiers, and a third preset number of auto-incrementing sequences. The method further includes: The target ID server obtains the current system time and the latest time when the system ID was generated; The target ID server determines whether the latest time of generating the system ID is greater than the current system time; If the latest time of generating the system ID is not greater than the current system time, and the target ID server has not performed a clock rollback, the target ID server obtains the auto-incrementing sequence value of the system ID corresponding to the latest time of generating the system ID. The target ID server determines whether the auto-incrementing sequence value is greater than a preset maximum increment value. If so, the target ID server's local clock starts counting from the next millisecond to obtain the latest current system time, generates a system ID based on its own machine code identifier and the latest current system time, and updates the latest time of generating the system ID and the corresponding auto-incrementing sequence value of the system ID. The latest current system time is the original current system time + 1 millisecond, and the auto-incrementing sequence value is 0. If not, the target ID server generates a system ID based on its own machine code identifier and the current system time, and updates the latest time of generating the system ID and the corresponding auto-incrementing sequence value of the system ID. If the latest time of generating the system ID is greater than the current system time, the target ID server's local clock is rolled back. The target ID server calculates the rollback time deviation between the latest time of generating the system ID and the current system time due to the clock rollback. The target ID server determines whether the rollback time deviation is less than a rollback threshold. If it is not less than the rollback threshold, the target ID server activates an alarm mechanism. If it is less than the rollback threshold, the target ID server's local clock continues to count for twice the rollback threshold time to obtain the latest current system time. The target ID server determines whether the latest time of generating the system ID is greater than the latest current system time; if the latest time of generating the system ID is greater than the latest current system time, the target ID server activates an alarm mechanism; if the latest time of generating the system ID is not greater than the latest current system time, the target ID server returns to the step of obtaining the auto-incrementing sequence value of the system ID corresponding to the latest time of generating the system ID.
2. The method according to claim 1, characterized in that, Before the target ID server generates a system ID based on its own machine code identifier, the method further includes: When the ZooKeeper server detects that the target ID server has been registered, it obtains the local system time of the target ID server. The ZooKeeper server obtains the average local system time of all ID servers that are currently providing services; The ZooKeeper server determines whether the absolute value of the local system time of the target ID server and the average value is less than a threshold. If the value is less than the threshold, the ZooKeeper server indicates that the target ID server has started successfully. If the value is greater than or equal to the threshold, the ZooKeeper server will indicate that the target ID server failed to start and will activate the alarm mechanism.
3. The method according to claim 1, characterized in that, The machine code identifier of the target ID server includes the data center code and the machine code.
4. The method according to claim 1, characterized in that, The method further includes: Each ID server in the ID generation system achieves clock synchronization based on a network time synchronization protocol.
5. A global and incremental ID generation system, characterized in that, Includes ZooKeeper servers and multiple ID servers; The ZooKeeper server is used to generate a ZooKeeper persistent sequence node corresponding to the target ID server when it is detected that the target ID server is not registered. The target ID server can be any ID server in the ID generation system. Based on the machine code identifier stored in the ZooKeeper persistent sequence node corresponding to the registered ID server, the machine code identifier of the target ID server is generated in ascending order of the machine code identifier, and the machine code identifier of the target ID server is stored in the corresponding ZooKeeper persistent sequence node to complete the registration of the target ID server and ensure the uniqueness of the machine code identifier of each ID server in the system. The target ID server is used to generate a system ID based on its own machine code identifier. The system ID includes a first preset number of timestamps, a second preset number of machine code identifiers, and a third preset number of auto-incrementing sequences. The target ID server is also used for: Get the current system time and the latest time when the system ID was generated; Determine whether the latest time of generating the system ID is greater than the current system time; If the latest time of generating the system ID is not greater than the current system time, and the target ID server has not performed a clock rollback, the target ID server obtains the auto-incrementing sequence value of the system ID corresponding to the latest time of generating the system ID. The target ID server determines whether the auto-incrementing sequence value is greater than a preset maximum increment value. If so, the target ID server's local clock starts counting from the next millisecond to obtain the latest current system time, generates a system ID based on its own machine code identifier and the latest current system time, and updates the latest time of generating the system ID and the corresponding auto-incrementing sequence value of the system ID. The latest current system time is the original current system time + 1 millisecond, and the auto-incrementing sequence value is 0. If not, the target ID server generates a system ID based on its own machine code identifier and the current system time, and updates the latest time of generating the system ID and the corresponding auto-incrementing sequence value of the system ID. If the latest time of generating the system ID is greater than the current system time, the local clock of the target ID server will be rolled back. The target ID server will calculate the rollback time deviation between the latest time of generating the system ID and the current system time due to the clock rollback. The target ID server determines whether the callback time deviation value is less than the callback threshold; if it is not less than the callback threshold, the target ID server activates an alarm mechanism; if it is less than the callback threshold, the local clock of the target ID server continues to count for twice the callback threshold time to obtain the latest current system time. The target ID server determines whether the latest time of generating the system ID is greater than the latest current system time; if the latest time of generating the system ID is greater than the latest current system time, the target ID server activates an alarm mechanism; if the latest time of generating the system ID is not greater than the latest current system time, the target ID server returns to the step of obtaining the auto-incrementing sequence value of the system ID corresponding to the latest time of generating the system ID.
6. The system according to claim 5, characterized in that, The ZooKeeper server is also used for: Before the target ID server generates a system ID based on its own machine code identifier, and if the target ID server is detected to be registered, the local system time of the target ID server is obtained; Get the average local system time of all ID servers that are currently providing services; Determine whether the absolute value of the local system time of the target ID server and the average value is less than a threshold; If the value is less than the threshold, it indicates that the target ID server has started successfully. If the value is not less than the threshold, a message indicating that the target ID server failed to start will be displayed, and an alarm mechanism will be activated.
Citation Information
Patent Citations
Method, equipment and system for generating ID
CN102769667A
A method and apparatus for generating data object identifiers
CN109240848A