CN-113190547-B - Global unique and incremental ID generation method and system
Abstract
The invention provides a global and incremental ID generation method and system, wherein a ZooKeeper server generates a ZooKeeper persistent sequence node corresponding to a target ID server under the condition that the target ID server is detected to be unregistered, the ZooKeeper server generates a machine code identifier of the target ID server according to the machine code identifiers stored in the ZooKeeper persistent sequence node corresponding to a registered ID server and the ascending sequence of the machine code identifiers, the machine code identifier of the target ID server is stored in the corresponding ZooKeeper persistent sequence node to finish registration of the target ID server, and the target ID server generates a system ID according to the machine code identifier of the target ID server, wherein the system ID comprises a timestamp of a first preset bit number, the machine code identifier of a second preset bit number and a self-increment sequence of a third preset bit number. The invention maintains the machine code identification of the ID server through the ZooKeeper server, thereby avoiding the problem of generating the repeated system ID caused by the non-unique machine code identification of the ID server in the system.
Inventors
- XU JIAHONG
- CHEN HUABING
- HUANG JINLONG
- ZENG KE
Assignees
- 深圳市茁壮网络股份有限公司
Dates
- Publication Date
- 20260508
- Application Date
- 20200114
Claims (6)
- 1. A global and incremental ID generation method, applied to a global and incremental ID generation system, the ID generation system comprising a ZooKeeper server and a plurality of ID servers, the method comprising: The ZooKeeper server generates a ZooKeeper persistent sequence node corresponding to a target ID server when detecting that the target ID server is unregistered, wherein the target ID server is any one ID server in the ID generation system; The ZooKeeper server generates the machine code identification of the target ID server according to the machine code identification stored in the ZooKeeper persistent sequence node corresponding to the registered ID server and the ascending sequence of the machine code identification, stores the machine code identification of the target ID server in the corresponding ZooKeeper persistent sequence node, completes the registration of the target ID server and ensures the uniqueness of the machine code identification of each ID server in the system; The target ID server generates a system ID according to the self machine code identification, wherein the system ID comprises a timestamp with a first preset bit number, a machine code identification with a second preset bit number and a self-increment sequence with a third preset bit number; The method further comprises the steps of: The target ID server obtains the current system time and the latest time for generating the system ID; the target ID server judges whether the latest time for generating the system ID is greater than the current system time; If the latest time for generating the system ID is not greater than the current system time, the target ID server does not generate clock callback, the target ID server acquires a self-increment sequence value of the system ID corresponding to the latest time for generating the system ID, the target ID server judges whether the self-increment sequence value is greater than a preset increment maximum value, if so, the local clock of the target ID server counts from the next millisecond to obtain the latest current system time, generates the system ID according to the self machine code identification and the latest current system time and updates the latest time for generating the system ID and the self-increment sequence value of the corresponding system ID, wherein the latest current system time is the original current system time +1 millisecond, the self-increment sequence value is 0, and if not, the target ID server generates the system ID according to the self machine code identification and the current system time and updates the latest time for generating the system ID and the self-increment sequence value of the corresponding system ID; If the latest time for generating the system ID is greater than the current system time, the local clock of the target ID server is callback, the target ID server calculates callback time deviation value generated between the latest time for generating the system ID and the current system time due to clock callback, the target ID server judges whether the callback time deviation value is less than a callback threshold value, if not, the target ID server starts an alarm mechanism, and if not, the local clock of the target ID server continuously counts twice the callback threshold time to obtain the latest current system time; The target ID server judges whether the latest time for generating the system ID is larger than the latest current system time, if the latest time for generating the system ID is larger than the latest current system time, the target ID server starts an alarm mechanism, and if the latest time for generating the system ID is not larger than the latest current system time, the target ID server returns to execute the self-increment sequence value of the system ID corresponding to the latest time for acquiring the generated system ID.
- 2. The method of claim 1, wherein prior to the target ID server generating a system ID from its own machine code identification, the method further comprises: the ZooKeeper server acquires the local system time of the target ID server under the condition that the target ID server is detected to be registered; The ZooKeeper server obtains the average value of the local system time of all the ID servers providing service; the ZooKeeper server judges whether the absolute value of the local system time of the target ID server and the average value is smaller than a threshold value; If the target ID server is smaller than the threshold, the ZooKeeper server prompts that the target ID server is started successfully; If the threshold value is greater than or equal to the threshold value, the ZooKeeper server prompts the target ID server to fail to start, and an alarm mechanism is started.
- 3. The method of claim 1, wherein the machine code identification of the target ID server comprises a machine room code and a machine code.
- 4. The method according to claim 1, wherein the method further comprises: each ID server in the ID generation system implements clock synchronization based on a network time synchronization protocol.
- 5. A global and incremental ID generation system comprising a ZooKeeper server and a plurality of ID servers; The ZooKeeper server is used for generating a ZooKeeper persistent sequence node corresponding to a target ID server under the condition that the target ID server is detected to be unregistered, the target ID server is any one ID server in the ID generation system, the machine code identification of the target ID server is generated according to the machine code identifications stored in the ZooKeeper persistent sequence node corresponding to the registered ID server and the ascending order of the machine code identifications, the machine code identifications of the target ID server are stored in the corresponding ZooKeeper persistent sequence nodes, the registration of the target ID server is completed, and the uniqueness of the machine code identifications of each ID server in the system is ensured; The target ID server is used for generating a system ID according to the machine code identification of the target ID server, wherein the system ID comprises a time stamp with a first preset bit number, a machine code identification with a second preset bit number and a self-increment sequence with a third preset bit number; The target ID server is further configured to: acquiring current system time and generating the latest time of the system ID; judging whether the latest time for generating the system ID is greater than the current system time; If the latest time for generating the system ID is not greater than the current system time, the target ID server does not generate clock callback, the target ID server acquires a self-increment sequence value of the system ID corresponding to the latest time for generating the system ID, the target ID server judges whether the self-increment sequence value is greater than a preset increment maximum value, if so, the local clock of the target ID server counts from the next millisecond to obtain the latest current system time, generates the system ID according to the self machine code identification and the latest current system time and updates the latest time for generating the system ID and the self-increment sequence value of the corresponding system ID, wherein the latest current system time is the original current system time +1 millisecond, the self-increment sequence value is 0, and if not, the target ID server generates the system ID according to the self machine code identification and the current system time and updates the latest time for generating the system ID and the self-increment sequence value of the corresponding system ID; If the latest time for generating the system ID is greater than the current system time, the local clock of the target ID server is callback, the target ID server calculates callback time deviation value generated between the latest time for generating the system ID and the current system time due to clock callback, the target ID server judges whether the callback time deviation value is less than a callback threshold value, if not, the target ID server starts an alarm mechanism, and if not, the local clock of the target ID server continuously counts twice the callback threshold time to obtain the latest current system time; The target ID server judges whether the latest time for generating the system ID is larger than the latest current system time, if the latest time for generating the system ID is larger than the latest current system time, the target ID server starts an alarm mechanism, and if the latest time for generating the system ID is not larger than the latest current system time, the target ID server returns to execute the self-increment sequence value of the system ID corresponding to the latest time for acquiring the generated system ID.
- 6. The system of claim 5, wherein the ZooKeeper server is further configured to: Before the target ID server generates a system ID according to the machine code identification of the target ID server, and under the condition that the target ID server is detected to be registered, acquiring the local system time of the target ID server; acquiring an average value of local system time of all ID servers providing services; judging whether the absolute value of the local system time of the target ID server and the average value is smaller than a threshold value or not; If the target ID server is smaller than the threshold value, prompting that the target ID server is started successfully; And if the starting time is not less than the threshold value, prompting that the starting of the target ID server fails, and starting an alarm mechanism.
Description
Global unique and incremental ID generation method and system Technical Field The invention relates to the technical field of databases, in particular to a global and incremental ID generation method and system. Background In a distributed system, when a large amount of data cannot be accepted by a table, a database and table splitting operation is involved. In order to facilitate the operations of inquiring, updating, migrating and the like of the data after the database is divided into tables, a system unique main key ID needs to be generated, and the system unique main key ID has the characteristics of uniqueness, digital type and trend increment of the ID in the whole system, and is short and convenient for inquiring. In the distributed system, a plurality of servers for generating the system unique primary key ID exist, and how to ensure that the system unique primary key ID generated by the plurality of servers is not repeated is a technical problem to be solved in the field. Disclosure of Invention In view of this, the present invention provides a global and incremental ID generation method and system, which ensures the uniqueness of the system IDs generated by multiple servers in the distributed system. In order to achieve the above purpose, the specific technical scheme provided by the invention is as follows: A global and incremental ID generation method applied to a global and incremental ID generation system comprising a ZooKeeper server and a plurality of ID servers, the method comprising: The ZooKeeper server generates a ZooKeeper persistent sequence node corresponding to a target ID server when detecting that the target ID server is unregistered, wherein the target ID server is any one ID server in the ID generation system; The ZooKeeper server generates the machine code identification of the target ID server according to the machine code identification stored in the ZooKeeper persistent sequence node corresponding to the registered ID server and the ascending sequence of the machine code identification, stores the machine code identification of the target ID server in the corresponding ZooKeeper persistent sequence node, and completes the registration of the target ID server; the target ID server generates a system ID according to the machine code identification of the target ID server, wherein the system ID comprises a time stamp with a first preset bit number, a machine code identification with a second preset bit number and a self-increment sequence with a third preset bit number. Optionally, before the target ID server generates the system ID according to its own machine code identifier, the method further includes: the ZooKeeper server acquires the local system time of the target ID server under the condition that the target ID server is detected to be registered; The ZooKeeper server obtains the average value of the local system time of all the ID servers providing service; the ZooKeeper server judges whether the absolute value of the local system time of the target ID server and the average value is smaller than a threshold value; If the target ID server is smaller than the threshold, the ZooKeeper server prompts that the target ID server is started successfully; If the threshold value is not smaller than the threshold value, the ZooKeeper server prompts the target ID server to fail to start, and an alarm mechanism is started. Optionally, the machine code identification of the target ID server includes a machine room code and a machine code. Optionally, the method further comprises: each ID server in the ID generation system implements clock synchronization based on a network time synchronization protocol. Optionally, the method further comprises: The target ID server obtains the current system time and the latest time for generating the system ID in a storage period adjacent to the current system time; the target ID server calculates a time deviation value between the current system time and the latest time of generating the system ID; And the target ID server determines an ID generation strategy according to the time deviation value and executes the ID generation strategy. Optionally, the target ID server calculates a time offset value between the current system time and a latest time of generating a system ID, including: the target ID server judges whether the latest time for generating the system ID is greater than the current system time; If the current system time is greater than the current system time, the target ID server calculates a callback time deviation value generated between the latest time for generating the system ID and the current system time due to clock callback; if not, the target ID server calculates an incremental time offset value between the current system time and the latest time of generating the system ID. Optionally, the target ID server determines an ID generation policy according to the time deviation value, and executes the ID generation policy, inclu