Serial number generation method and device for cluster system
By assigning a unique identifier and setting basic parameters to each node in the cluster system, and combining timestamp and sequence number mechanisms, the uniqueness and efficiency issues of serial number generation in the cluster system are solved, realizing the generation of globally unique serial numbers in a high-concurrency environment, thus improving system performance and reliability.
Patent Information
- Application Number
- CN202510832184.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-20
- Publication Date
- 2025-11-04
AI Technical Summary
In cluster systems, existing serial number generation methods cannot guarantee global uniqueness and efficiency in high-concurrency scenarios, and are prone to duplicate serial numbers and performance bottlenecks.
By assigning a unique node identifier to each node in the cluster system and setting basic parameters for serial numbers, including timestamp start time, timestamp precision, initial serial number value, and maximum serial number count value, each node independently generates a serial number containing the current timestamp, node identifier, and serial number. Time synchronization is achieved using a network time protocol, and the serial number generation process is optimized through load information and a serial number borrowing mechanism.
In high-concurrency scenarios, it efficiently generates globally unique serial numbers, reduces competition and conflicts between nodes, improves the performance and reliability of serial number generation, and adapts to the needs of different business scenarios.
Smart Images

Figure CN120893389A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer data processing technology, and more specifically, to a method and apparatus for generating serial numbers for cluster systems. Background Technology
[0002] In computer systems, serial numbers serve as unique identifiers for data records and are widely used in numerous business scenarios such as order management, log recording, and database operations. With the rapid development of information technology, distributed and clustered system architectures have gradually become mainstream. However, achieving global uniqueness of serial numbers in these complex system environments presents many challenges.
[0003] Traditional serial number generation methods, such as simply using an auto-incrementing field in the database, can guarantee uniqueness well in a single database environment. However, in a clustered environment, when multiple nodes access the database simultaneously to perform auto-incrementing operations, contention and conflicts can easily occur, leading to the generation of duplicate serial numbers. Another common method is to use a timestamp as part of the serial number. Although timestamps have a certain degree of order and uniqueness, in high-concurrency scenarios, a large number of requests may be generated within the same millisecond, and relying solely on timestamps cannot guarantee uniqueness. Furthermore, the precision of timestamps limits their application in large-scale concurrent systems.
[0004] Existing serial number generation methods perform poorly in cluster environments, failing to balance uniqueness, ordering, and efficiency. Therefore, a new method is urgently needed to efficiently and reliably generate globally unique serial numbers in a clustered environment to meet increasingly complex business requirements. Summary of the Invention
[0005] The technical problem to be solved by the present invention is to provide a method and apparatus for generating serial numbers in a cluster system, which can efficiently generate globally unique serial numbers in high-concurrency scenarios.
[0006] To solve the above-mentioned technical problems, the present invention adopts the following technical solution: In a first aspect, embodiments of the present invention provide a method for generating serial numbers in a cluster system, the cluster system comprising multiple nodes, the method comprising the following steps: S1. Initialize the cluster system, assign a unique node identifier to each node, and set the basic parameters of the serial number. The basic parameters of the serial number include the timestamp start time, timestamp precision, initial value of the serial number, and maximum count value of the serial number. S2. Each node independently receives the serial number generation request and generates its own serial number; the serial number includes the current timestamp, node identifier, and sequence number. S3. Send the serial number and the business association information corresponding to the serial number.
[0007] In conjunction with the first aspect, in the first possible implementation of the first aspect, S1 further includes: synchronizing the time of all nodes in the cluster system through a network time protocol; in S1, the timestamp precision, the initial value of the sequence number, and the maximum count value of the sequence number are stored in a database.
[0008] In conjunction with the first possible implementation of the first aspect, in the second possible implementation of the first aspect, S2 specifically includes: S21. Retrieve the timestamp precision, initial serial number value, and maximum serial number count value from the database and store them in the node; S22. Receive a serial number generation request, obtain the current timestamp according to the timestamp precision; obtain a serial number according to the initial serial number value, wherein the serial number is the maximum used serial number plus 1; if the serial number is greater than the maximum serial number count value, reset the serial number to the initial serial number value. S23. Concatenate the current timestamp, node identifier, and sequence number to generate a serial number.
[0009] In conjunction with the second possible implementation of the first aspect, in the third possible implementation of the first aspect, each node performs step S21 only once until some or all of the parameters of timestamp precision, initial value of sequence number and maximum count value of sequence number change; the node completes steps S22 and S23 in the node memory.
[0010] In conjunction with the first aspect, in the fourth possible implementation of the first aspect, the method for generating serial numbers for a cluster system further includes: Obtain the modified configuration information and save it in the node cache; the modified configuration information includes the timestamp precision and the effective time. For serial number generation requests received within the effective time period, obtain the current timestamp according to the modified timestamp precision, and generate a serial number.
[0011] In conjunction with the first aspect, in a fifth possible implementation of the first aspect, the method further includes: Report load information, which includes the number of requests currently being processed by the node and the number of remaining available sequence numbers; Based on the load information, a serial number generation request is received.
[0012] In conjunction with the fifth possible implementation of the first aspect, in the sixth possible implementation of the first aspect, receiving the serial number generation request based on the load information includes: Compare the number of requests currently being processed and generated by a node with a first threshold. If the number of requests currently being processed and generated is greater than or equal to the first threshold, then no serial number generation request is sent to that node, and the next node is polled. If the number of requests currently being processed and generated is less than the first threshold, then proceed to the next step. The remaining number of available sequence numbers of the node is compared with the second threshold. If the remaining number of available sequence numbers is greater than or equal to the second threshold, a serial number generation request is sent to the node. If the remaining number of available sequence numbers is less than the second threshold, no serial number generation request is sent to the node, and the next node is polled. If the remaining number of available sequence numbers of all nodes is less than the second threshold, a serial number generation request is randomly sent to the node until the remaining number of available sequence numbers of all nodes is 0, and the sequence number is reset to the initial value.
[0013] In conjunction with the first aspect, in the seventh possible implementation of the first aspect, the method for generating serial numbers for a cluster system further includes: Calculate the ratio between the current sequence number of a node and the maximum count value of the sequence number. If the ratio reaches a third threshold, issue a request to borrow the sequence number. The node is the requesting node. Based on the load information of other nodes, a portion of the sequence number of another node is assigned to the requesting node; the other node is the lending node. The borrowing node caches a portion of the sequence number borrowed by the borrowing node in the requesting node, and the requesting node uses the borrowed sequence number within the current timestamp; After use, the requesting node returns the unused sequence number to the lending node and updates the sequence number status of the requesting node.
[0014] In conjunction with the first aspect, in the eighth possible implementation of the first aspect, the method for generating serial numbers for a cluster system further includes: Compare the number of serial number generation requests currently being processed by the node with the fourth threshold. If the number of serial number generation requests currently being processed is less than the fourth threshold, then generate a serial number in advance and store the pre-generated serial number in the node cache. If a new serial number generation request is received, the pre-generated serial number is retrieved from the node cache. Determine the validity of the retrieved pre-generated serial number. If valid, use the retrieved pre-generated serial number as the serial number for the new serial number generation request.
[0015] Secondly, embodiments of the present invention provide a serial number generation device for a cluster system, comprising: The initialization configuration module is used to assign a unique node identifier to each node and set the basic parameters of the serial number. The basic parameters of the serial number include the timestamp start time, timestamp precision, initial value of the serial number, and maximum count value of the serial number. The generation module is used for each node to independently receive a serial number generation request and generate its own serial number; the serial number includes the current timestamp, node identifier, and sequence number; The sending module is used to send the serial number and the business association information corresponding to the serial number.
[0016] Compared with existing technologies, the serial number generation method and apparatus for cluster systems of the present invention can efficiently generate globally unique serial numbers in high-concurrency scenarios. The method includes the following steps: S1, initializing the cluster system by assigning a unique node identifier to each node and setting basic serial number parameters, including timestamp start time, timestamp precision, initial sequence number value, and maximum sequence number count value; S2, each node independently receives a serial number generation request and generates its own serial number; the serial number includes the current timestamp, node identifier, and sequence number; S3, sending the serial number and the corresponding business association information. The serial number generation method and apparatus of the present invention can efficiently and reliably generate unique serial numbers in different business scenarios, meeting actual business needs and possessing good application prospects and promotional value. Attached Figure Description
[0017] To more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings used in the embodiments of the present invention will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0018] Figure 1 This is a system architecture diagram of an embodiment of the present invention; Figure 2 This is a flowchart of the method according to an embodiment of the present invention; Figure 3 This is a structural block diagram of the device according to an embodiment of the present invention. Detailed Implementation
[0019] To enable those skilled in the art to better understand the technical solutions of the present invention, the present invention will be further described in detail below with reference to the accompanying drawings and specific embodiments. Obviously, the described embodiments are only some, not all, of the embodiments of the present invention. Based on the embodiments of the present invention, embodiments obtained by those skilled in the art without prior creative effort should all fall within the scope of protection of the present invention.
[0020] Those skilled in the art will understand that, unless specifically stated otherwise, the singular forms “a,” “an,” “the,” and “the” used herein may also include the plural forms. It should be further understood that the term “comprising” as used in this specification means the presence of the stated features, integers, steps, operations, elements, and / or components, but does not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components, and / or groups thereof. The terms “first,” “second,” and “third,” etc., used herein are used to distinguish different objects, not to describe a particular order of objects. In embodiments of the invention, words such as “exemplary” or “for example” are used to indicate that something is an example, illustration, or description. Any embodiment or design described as “exemplary” or “for example” in embodiments of the invention should not be construed as preferred or advantageous over other embodiments or designs. Specifically, the use of words such as “exemplary” or “for example” is intended to present the relevant concepts in a specific manner. In the description of the invention, unless otherwise stated, “a plurality” means two or more.
[0021] It will be understood by those skilled in the art that, unless otherwise defined, all terms used herein (including technical and scientific terms) have the same meaning as commonly understood by one of ordinary skill in the art to which this invention pertains. It should also be understood that terms such as those defined in general dictionaries should be understood to have the same meaning as in the context of the prior art, and should not be interpreted in an idealized or overly formal sense unless defined as herein.
[0022] Cluster mode is a technical architecture that connects multiple computers or servers together to work as a single system. In a serial number generation system, multiple servers can be used. Each server acts as a node. Multiple nodes can work simultaneously, generating serial numbers independently, thereby improving serial number generation efficiency.
[0023] High concurrency performance refers to a system's ability to handle a large number of user requests simultaneously. For example, in shopping festival scenarios, there is a sudden surge in traffic, generating a large number of order demands in a short period of time, each order having a corresponding order number. In this situation, a serial number generation system with high concurrency performance is required.
[0024] like Figure 1 As shown, the cluster system in this embodiment includes multiple nodes and a cluster management platform, which contains a database. The database stores timestamp precision, initial serial number values, and maximum serial number counts for each node to access. Data interaction is achieved between the nodes and the cluster management platform, and between the cluster management platform and the database, through communication connections. For example, a cluster environment consisting of 10 nodes is deployed in the system. The cluster system of this invention can be applied to different business scenarios, such as flash sales and registration scenarios.
[0025] like Figure 2 As shown, the serial number generation method for a cluster system according to an embodiment of the present invention includes the following steps: S1. Initialize the cluster system, assign a unique node identifier to each node, and set the basic parameters of the serial number. The basic parameters of the serial number include the timestamp start time, timestamp precision, initial value of the serial number, and maximum count value of the serial number.
[0026] When setting up or starting the cluster system, each node in the cluster is initialized and configured. A unique node identifier is assigned to each node. This node identifier is unique throughout the entire cluster. Simultaneously, basic parameters for the serial number are set, including the timestamp start time (e.g., the timestamp start time is set to the time the system is first put into use), the timestamp precision (e.g., milliseconds, microseconds, etc.), the initial value of the serial number (generally set to 0), and the maximum count value of the serial number (set to an appropriate value according to actual business needs, such as 1024, etc.).
[0027] S2. Each node independently receives a serial number generation request and generates its own serial number. The serial number includes the current timestamp, node identifier, and sequence number. For example, a complete serial number can be obtained by concatenating the serial number in the order of current timestamp first, node identifier in the middle, and sequence number last. To facilitate storage and transmission, the concatenated serial number can also be encoded, such as using Base64 encoding.
[0028] S3. Send the serial number and the business association information corresponding to the serial number.
[0029] In the above method, in step S1, a unique node identifier is assigned to each node. Each node has its own unique node identifier, and these identifiers are not repeated. The node identifier is a component of the serial number. This ensures that serial numbers generated between different nodes will not be duplicated. Depending on the applicable scenario, such as high concurrency performance requirements, the timestamp precision is pre-set. Each node obtains a timestamp according to the set timestamp precision. Timestamp precision can be in the range of seconds, milliseconds, microseconds, etc. The higher the concurrency performance requirements, the smaller the timestamp precision unit. The initial value and maximum count value of the sequence number for each node are set. The initial values of the sequence numbers for each node can be the same or different. Different initial values can avoid duplicate sequence numbers in the initial stage, balance the sequence number growth rate of each node, and reduce the probability of conflicts within the same timestamp. The maximum count values of the sequence numbers for each node can be the same or different. Having the same maximum count value ensures that the sequence number generation logic of all nodes is consistent, facilitating batch configuration, monitoring, and troubleshooting. The same maximum count value for the same sequence number ensures that the sequence numbers of each node have the same cycle period under the same timestamp precision.
[0030] The system sets the start time of the timestamp for each node. All nodes have the same start time for their timestamps. Time synchronization across all nodes in the cluster system is achieved through a network time protocol. This ensures time consistency and accuracy across all nodes. The timestamp is a component of the serial number. Because all nodes have the same start time for their timestamps, the serial numbers can be sorted by timestamp for easy retrieval. The system can send serial number generation requests to one or more nodes. Each node independently receives the request and generates its own serial number. Multiple nodes can work simultaneously, making it suitable for high-concurrency scenarios and improving efficiency. The serial number includes the current timestamp, node identifier, and sequence number. Serial numbers generated by the same node may have the same or different current timestamps, but their sequence numbers are different. This ensures that there are no duplicate serial numbers generated by the same node. Serial numbers generated by different nodes may have the same or different current timestamps and sequence numbers, but their node identifiers are different. This ensures that there are no duplicate serial numbers generated by different nodes. By setting the serial number, the system achieves global uniqueness for each serial number. This method sends a serial number and its corresponding business association information. The business association information originates from the serial number generation request. This information can be a business type identifier, such as order type (regular order / flash sale order), transaction type (payment / refund), or business scenario (order placement / logistics tracking); user-related information, such as user ID or merchant number; or business parameters, such as order amount, product quantity, or transaction currency. The serial number and business association information are associated and sent together to the requester for business processing. Simultaneously, the information is recorded locally, including the generated serial number, corresponding timestamp, node identifier, sequence number, and relevant business request information (such as request source and request type) for subsequent querying and auditing. Furthermore, the issued serial numbers can be status-managed, such as marked as used or expired.
[0031] To further improve operational efficiency, preferably, in step S1, the timestamp precision, initial sequence number value, and maximum sequence number count value are stored in a database. The database is independent of each node. The timestamp start time is uniformly set by the system in each node. Step S2 specifically includes: S21. Retrieve the timestamp precision, initial serial number value, and maximum serial number count value from the database and store them in the node.
[0032] S22. Receive a serial number generation request, obtain the current timestamp according to the timestamp precision; obtain the serial number according to the initial serial number value, wherein the serial number is the maximum used serial number plus 1; if the serial number is greater than the maximum serial number count value, reset the serial number to the initial serial number value.
[0033] When a node in the cluster receives a request to generate a serial number, it first retrieves the current timestamp. The timestamp precision is set according to the initial configuration, for example, retrieving a timestamp accurate to milliseconds.
[0034] S23. Concatenate the current timestamp, node identifier, and sequence number to generate a serial number.
[0035] Before starting the job, each node retrieves the timestamp precision, initial sequence number value, and maximum sequence number count from the database and stores them in its memory. This way, during the serial number generation process, each node doesn't need to retrieve these parameters every time a serial number is generated. Throughout the job, each node performs step S21 only once, until some or all of these parameters change. Thus, during the job, i.e., the serial number generation process, each node retrieves the timestamp precision, initial sequence number value, and maximum sequence number count from its own memory and completes steps S22 and S23 in its own memory. This reduces the frequency of database access and data retrieval by each node during serial number generation, improving job efficiency. Especially in high-concurrency scenarios, if each node had to access the database and retrieve data for every serial number generated, it would be difficult to guarantee the normal operation of the job.
[0036] The efficiency requirements for serial number generation vary across different scenarios. Preferably, the serial number generation method for cluster systems according to this embodiment of the invention further includes: S41. Obtain the modified configuration information and save the modified configuration information in the node cache; the modified configuration information includes the modified timestamp precision and the effective time; S42. For the serial number generation request received within the effective time, obtain the current timestamp according to the modified timestamp precision, and generate a serial number.
[0037] This preferred example, based on different business scenarios, especially high-concurrency scenarios, allows for adjustment of timestamp precision to generate corresponding serial numbers. When business scenarios change, especially high-concurrency scenarios, it's necessary to adjust the timestamp precision. The system resets the timestamp precision and sets the effective time for this new precision. The effective time is set based on the start and duration of the high-concurrency scenario. Within this effective time, the current timestamp is retrieved according to the new timestamp precision, and a serial number is generated. In this way, by changing the timestamp precision, a serial number that meets business requirements is generated. When the effective time ends, each node generates a serial number according to the originally set timestamp precision.
[0038] The system receives instructions to dynamically adjust timestamp precision through management interfaces (e.g., REST API, configuration center). These instructions include the new timestamp precision (e.g., from milliseconds to microseconds) and the effective time. Each node obtains the new configuration information via a heartbeat or subscription mechanism, caches the new timestamp precision parameters locally, and records the effective time. When the effective time arrives, the node retrieves the current timestamp according to the new precision. For example, when switching from milliseconds to microseconds, the timestamp's numerical length increases (e.g., from a 13-bit millisecond timestamp to a 16-bit microsecond timestamp).
[0039] Due to variations in timestamp precision, the number of bits in the timestamp may differ. To ensure the retrievability of serial numbers with different timestamp precisions, the length of the timestamp field, determined by the highest timestamp precision supported by the system, is used as the length of all timestamps. For example, if the highest precision is microseconds (16 bits), then when the precision is milliseconds, zeros are padded to the high bits of the timestamp to make its length also 16 bits (e.g., the millisecond timestamp "1689000000000" is padded with zeros to "0001689000000000"). For instance, the overall serial number concatenation rule is as follows: the timestamp, node identifier, and sequence number are concatenated according to a fixed length, with the timestamp fixed at 16 bits, the node identifier fixed at 4 bits, and the sequence number fixed at 10 bits, for a total serial number length of 30 bits.
[0040] In the above embodiment, the system randomly sends serial number generation requests to each node. When one node receives a large number of serial number generation requests, another node remains idle. This inevitably leads to resource waste and reduced generation efficiency. Therefore, preferably, the method further includes: S51. Report load information, which includes the number of requests currently being processed by the node and the number of remaining available sequence numbers. The number of remaining available sequence numbers is the difference between the maximum sequence number count and the current sequence number.
[0041] S52. Receive a serial number generation request based on the load information.
[0042] In this preferred embodiment, each node reports its load information to the cluster management platform in real time. Based on the load information of each node, the cluster management platform allocates new serial number generation requests. If one node is currently processing a large number of generation requests, while another node is processing a smaller number, the cluster management platform can allocate new serial number generation requests to the other node. In this way, based on the load information, the cluster management platform allocates new serial number generation requests to nodes with less workload, thereby achieving a balance of workload among the nodes and preventing generation requests from being concentrated on one or a few nodes while other nodes remain idle, thus avoiding resource waste. The participation of all nodes further improves work efficiency.
[0043] In the preferred embodiment above, receiving the serial number generation request based on the load information includes: S521. Compare the number of requests currently being processed by a node with a first threshold. If the number of requests currently being processed is greater than or equal to the first threshold, do not send a serial number generation request to the node and poll the next node. If the number of requests currently being processed is less than the first threshold, proceed to the next step, i.e., step S522. S522. Compare the number of remaining available sequence numbers of the node with the second threshold. If the number of remaining available sequence numbers is greater than or equal to the second threshold, send a serial number generation request to the node. If the number of remaining available sequence numbers is less than the second threshold, do not send a serial number generation request to the node, poll the next node. If the number of remaining available sequence numbers of all nodes is less than the second threshold, randomly send a serial number generation request to the node until the number of remaining available sequence numbers of all nodes is 0, and reset the sequence number to the initial value.
[0044] In this preferred embodiment, the first threshold is a maximum set value for the number of generation requests currently processed by a node. If the number of generation requests currently processed by a node is greater than or equal to the first threshold, the node processes the currently received generation requests and no longer accepts new generation requests. If the number of generation requests currently processed by a node is less than the first threshold, a second threshold needs to be considered. The second threshold is a minimum set value for the number of remaining available sequence numbers of a node. For example, the second threshold is 20% of the maximum sequence number count. If the number of remaining available sequence numbers of a node is greater than or equal to the second threshold, the node can accept new generation requests, and the system sends a new serial number generation request to the node. If the number of remaining available sequence numbers of a node is less than the second threshold, the node has fewer available sequence numbers, and the system does not send a new serial number generation request to the node, but polls the next node. This step can make full use of the resources of each node and balance the workload of each node. If the number of remaining available sequence numbers of all nodes is less than the second threshold, a serial number generation request is randomly sent to the nodes until the number of remaining available sequence numbers of all nodes is 0, and the sequence numbers are reset to their initial values. After all available sequence numbers on all nodes have been used up, all nodes reset their sequence numbers, recalculating them from their initial values. This method makes full use of nodes, avoiding situations where some nodes receive too many generation requests while others remain idle or receive few, thus preventing resource waste.
[0045] All nodes in the cluster reset their sequence numbers simultaneously. If some nodes haven't used up their sequence numbers, other nodes, even when using the maximum sequence number count, must wait until all nodes have used their maximum count before resetting their sequence numbers. This ensures all nodes participate in the work and reduces resource idleness. If a node receives a large number of generation requests and doesn't have enough remaining sequence numbers, it will cause a delay in sequence number generation. Therefore, preferably, the sequence number generation method for the cluster system further includes: S61. Calculate the ratio between the current sequence number of a node and the maximum count value of the sequence number. If the ratio reaches a third threshold, issue a request to borrow the sequence number. The node is the requesting node.
[0046] S62. Based on the load information of other nodes, allocate a portion of the sequence number of another node to the requesting node; the other node is the lending node.
[0047] S63. A portion of the sequence number borrowed by the lending node is cached in the requesting node. The requesting node uses the borrowed sequence number within the current timestamp. When using it, the node identifier in the serial number generated by the requesting node is the lending node identifier, not the requesting node identifier. This ensures that the serial number generated by the requesting node using the borrowed sequence number will not duplicate the serial number generated by the requesting node using its own sequence number, guaranteeing the uniqueness of the serial number throughout the system.
[0048] S64. After use, the requesting node returns the unused serial number to the lending node and updates the serial number status of the requesting node.
[0049] In the preferred embodiment described above, the third threshold is 0.8. If the ratio between a node's current sequence number and its maximum sequence number count reaches the third threshold, then the node has few usable sequence numbers. In this case, the system borrows sequence numbers from other nodes. The lending node has a significant number of unused sequence numbers. A portion of the sequence numbers borrowed by the lending node are cached in the requesting node. The requesting node uses the borrowed sequence numbers within the current timestamp without waiting for a timestamp update. This preferred embodiment avoids blocking requests by the requesting node while waiting for a timestamp update. When the requesting node's sequence number is close to or has reached its maximum count, if the requesting node does not borrow sequence numbers from other nodes, it cannot continue generating new serial numbers within the current timestamp. It must wait for a timestamp update to reset the sequence number and continue generating new serial numbers. After the requesting node has finished using the sequence numbers, it returns the unused sequence numbers to the lending node, which can still use the returned unused sequence numbers. Simultaneously, the requesting node updates its sequence number status. This ensures that all generation requests currently received by the node will generate corresponding serial numbers, and it also makes full use of the sequence number resources of other nodes, which is equivalent to other nodes also participating in this batch of generation requests, further utilizing the allocation of resources among nodes.
[0050] A node only begins generating a serial number when it receives a serial number generation request. When a node does not receive a serial number generation request, it remains idle. This can lead to low generation efficiency under high concurrency. To further improve generation efficiency, preferably, the serial number generation method for a cluster system further includes: S71. Compare the number of serial number generation requests currently being processed by the node with the fourth threshold. If the number of serial number generation requests currently being processed is less than the fourth threshold, then generate a serial number in advance and store the pre-generated serial number in the node cache. S72. If there is a new serial number generation request, retrieve the pre-generated serial number from the node cache; S73. Determine the validity of the retrieved pre-generated serial number. If valid, use the retrieved pre-generated serial number as the serial number for the new serial number generation request.
[0051] When a node is currently processing a small number of serial number generation requests, serial numbers can be pre-generated and stored in the node cache. When a new serial number generation request is received, the node retrieves the pre-generated serial number from its cache. The validity of the retrieved pre-generated serial number is checked; if valid, it is used as the serial number for the new request. The validity of the retrieved pre-generated serial number is determined by comparing the timestamp in the pre-generated serial number with the timestamp of the new serial number generation request received by the node. If they match, the pre-generated serial number is valid; otherwise, it is invalid. Invalid pre-generated serial numbers are discarded, recorded, and no longer used. This preferred embodiment avoids requests concentrating on a few nodes, distributing the serial number generation pressure across nodes, preventing single nodes from becoming overloaded due to high concurrency, and improving the overall processing efficiency of the cluster. This preferred approach reduces the risk of some nodes being idle or overloaded. Even if individual nodes fail, other nodes can still take over requests, ensuring system stability.
[0052] like Figure 3 As shown, this embodiment of the invention also provides a serial number generation device for a cluster system, comprising: The initialization configuration module is used to assign a unique node identifier to each node and set the basic parameters of the serial number. The basic parameters of the serial number include the timestamp start time, timestamp precision, initial value of the serial number, and maximum count value of the serial number. The generation module is used for each node to independently receive a serial number generation request and generate its own serial number; the serial number includes the current timestamp, node identifier, and sequence number; The sending module is used to send the serial number and the business association information corresponding to the serial number.
[0053] Preferably, the initialization configuration module stores the timestamp precision, initial sequence number value, and maximum sequence number count value in the database. The initialization configuration module is also used to synchronize the time of all nodes in the cluster system via a network time protocol.
[0054] Preferably, the generation module specifically includes: Retrieval Unit: Used to retrieve the timestamp precision, initial serial number value, and maximum serial number count value from the database and store them in the node; Acquisition Unit: Used to receive a serial number generation request, obtain the current timestamp according to the timestamp precision; obtain the serial number according to the initial serial number value, wherein the serial number is the maximum used serial number plus 1; if the serial number is greater than the maximum serial number count value, then reset the serial number to the initial serial number value.
[0055] Generation unit: used to concatenate the current timestamp, node identifier and sequence number to generate a serial number.
[0056] Preferably, the serial number generation device for the cluster system further includes: a modification module: used to obtain modified configuration information and save the modified configuration information in the node cache; the modified configuration information includes the modified timestamp precision and the effective time; The generation module is further configured to, for the serial number generation request received within the effective time period, obtain the current timestamp according to the modified timestamp precision, and generate a serial number.
[0057] Preferably, the serial number generation device for the cluster system further includes: Reporting module: used to report load information, which includes the number of requests currently being processed by the node and the number of remaining available sequence numbers; Receiving module: Used to receive a serial number generation request based on the load information.
[0058] The receiving module includes: First comparison unit: used to compare the number of requests currently being processed and generated by a node with a first threshold. If the number of requests currently being processed and generated is greater than or equal to the first threshold, no serial number generation request is sent to the node, and the next node is polled. If the number of requests currently being processed and generated is less than the first threshold, the process proceeds to the second comparison unit. The second comparison unit is used to compare the number of remaining available sequence numbers of the node with the second threshold. If the number of remaining available sequence numbers is greater than or equal to the second threshold, a serial number generation request is sent to the node. If the number of remaining available sequence numbers is less than the second threshold, no serial number generation request is sent to the node, and the next node is polled. If the number of remaining available sequence numbers of all nodes is less than the second threshold, a serial number generation request is randomly sent to the node until the number of remaining available sequence numbers of all nodes is 0, and the sequence number is reset to the initial value.
[0059] Preferably, the serial number generation device for the cluster system further includes: Calculation module: used to calculate the ratio between the current sequence number of a node and the maximum count value of the sequence number. If the ratio reaches the third threshold, a request to borrow the sequence number is issued, and the node is the requesting node. Allocation module: Used to allocate a portion of the sequence number of another node to the requesting node based on the load information of other nodes; the other node is the lending node; Cache module: used to cache part of the sequence number borrowed by the lending node in the requesting node, and the requesting node uses the borrowed sequence number within the current timestamp; Return module: After use, the requesting node returns the unused serial number to the lending node and updates the serial number status of the requesting node.
[0060] Preferably, the serial number generation device for the cluster system further includes: Comparison module: Used to compare the number of serial number generation requests currently being processed by the node with the fourth threshold. If the number of serial number generation requests currently being processed is less than the fourth threshold, a serial number is pre-generated and stored in the node cache. Retrieval module: Used to retrieve a pre-generated serial number from the node cache when a new serial number generation request is received; Judgment module: Used to determine the validity of the retrieved pre-generated serial number. If valid, the retrieved pre-generated serial number is used as the serial number for the new serial number generation request.
[0061] The method and apparatus of this invention ensure the global uniqueness of serial numbers in a cluster environment by combining the current timestamp, node identifier, and sequence number, from three dimensions: time, node, and sequence number. Even in high-concurrency scenarios, when multiple nodes simultaneously request the generation of serial numbers, duplicate serial numbers will not occur. Because the serial number contains the current timestamp information, the generated serial number has temporal order. This makes sorting, retrieval, and other operations on data based on serial numbers more efficient. For example, in an order management system, sorting by serial number allows for quick retrieval of the order of orders.
[0062] The method and apparatus of this invention feature high performance and low conflict. Each node independently maintains a sequence number, and the auto-increment and reset mechanism of the sequence number within the same timestamp reduces competition and conflicts between nodes. Simultaneously, it avoids frequent database access for auto-increment operations, significantly improving the performance of serial number generation and meeting the needs of large-scale concurrent services. All nodes in the cluster achieve time synchronization using the Network Time Protocol (NTP), ensuring that the timestamps obtained by each node are consistent in accuracy. Each node autonomously maintains an independent sequence number, which is only incremented internally by that node. No communication or coordination of sequence numbers is required between nodes. When there is a request to generate a serial number within the same timestamp, each node directly increments its own sequence number by 1. If a node's sequence number reaches the maximum count value, the node resets its sequence number to the initial value and enters a waiting state until the next timestamp arrives before continuing to generate a serial number. Preferably, when all nodes' sequence numbers reach the maximum count value, all nodes synchronously reset their sequence numbers to the initial value.
[0063] Traditional serial number generation methods typically rely on an auto-incrementing field in the database, requiring database access for each serial number generation. This can become a performance bottleneck in high-concurrency scenarios. In this embodiment, however, the serial number auto-increment operation is performed entirely within the node's memory, without database access. Only during system initialization are the initial serial number value and maximum serial number count read from the database; subsequent generation is independent of the database. Thus, each node's serial number auto-increment operation is completed locally within its own memory, eliminating the need to share resources with other nodes. Within the same timestamp, different nodes' serial numbers start auto-incrementing from different initial values (the initial serial number values for each node can be differentiated during initialization), and the maximum serial number count (e.g., 1024) is significantly larger than the number of nodes in the cluster.
[0064] The method and apparatus of this invention are flexible and scalable. Through initial configuration, parameters such as timestamp precision, initial sequence number value, and maximum sequence number count can be flexibly set to adapt to the needs of different business scenarios. When the cluster size changes, only a unique node identifier needs to be assigned to the new node to quickly integrate it into the cluster, ensuring the normal operation of the entire cluster system and the uniqueness of the serial number.
[0065] The various embodiments in this specification are described in a progressive manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on its differences from other embodiments. In particular, the device embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the description of the method embodiments. Those skilled in the art will understand that the modules in the devices of the embodiments can be adaptively changed and placed in one or more devices different from that embodiment. Modules, units, or components in the embodiments can be combined into a single module, unit, or component, and further, they can be divided into multiple sub-modules, sub-units, or sub-components. Except where at least some of such features and / or processes or units are mutually exclusive, any combination can be used to combine all features disclosed in this specification (including the accompanying claims, abstract, and drawings) and all processes or units of any method or device so disclosed. Unless expressly stated otherwise, each feature disclosed in this specification (including the accompanying claims, abstract, and drawings) may be replaced by an alternative feature that serves the same, equivalent, or similar purpose.
[0066] The method provided in this invention can specifically run on a server. The server includes components such as an input unit, a processor unit, an output unit, a communication unit, a storage unit, and peripheral units. These components communicate through one or more buses. The server structure can be either a bus topology or a star topology. In this invention embodiment, the server includes a server used by a financial institution to manage loan information data, a server used by a court system to handle case filing, and a server used by a legal service department to handle pre-litigation review. Specifically: The input unit is used to enable user interaction with the server and / or input of information into the server. For example, the input unit can receive numeric or character information input by the user to generate signal input related to user settings or function control. In specific embodiments of the present invention, the input unit can be a touch panel or other human-computer interaction interface, such as physical input keys, a mouse, or a joystick.
[0067] The processor unit serves as the control center of the server, connecting various parts of the server via various interfaces and lines. It executes software programs and / or modules stored in the storage unit, and accesses data stored in the storage unit to perform various server functions and / or process data. The processor unit can be composed of integrated circuits (ICs), such as a single packaged IC or multiple packaged ICs with the same or different functions connected together. For example, the processor unit can include only a central processing unit (CPU), or it can be a combination of a GPU, a digital signal processor (DSP), and a control chip (e.g., a baseband chip) in a communication unit. In this embodiment of the invention, the CPU can be a single processing core or include multiple processing cores.
[0068] The communication unit is used to establish a communication channel, enabling the server to connect to remote devices through the communication channel.
[0069] The output units include, but are not limited to, image output units and audio output units. Image output units are used to output text, images, and / or video. Image output units may include display panels, such as those configured using LCD (Liquid Crystal Display), OLED (Organic Light-Emitting Diode), or field emission display (FED) formats.
[0070] The storage unit can be used to store software programs and modules. The processing unit executes various server functions and performs data processing by running the software programs and modules stored in the storage unit. The storage unit mainly includes a program storage area and a data storage area. The program storage area can store the operating system and at least one application program required for a function. In a specific embodiment of the present invention, the storage unit may include volatile memory, such as nonvolatile random access memory (NVRAM), phase change RAM (PRAM), magnetoresistive RAM (MRAM), etc., and may also include non-volatile memory, such as at least one disk storage device, electrically erasable programmable read-only memory (EEPROM), or flash memory devices, such as NOR flash memory or NAND flash memory. The non-volatile memory stores the operating system and application programs executed by the processing unit. The processing unit loads the running program and data from the non-volatile memory into memory and stores the digital content in a large number of storage devices.
[0071] A power supply is used to power various components of a server to maintain its operation, including external power supplies that directly power the server, such as AC adapters. In some embodiments of the invention, the power supply can also be defined more broadly, for example, including a power management system, a charging system, a power fault detection circuit, a power converter or inverter, a power status indicator (such as a light-emitting diode), and any other components associated with the generation, management, and distribution of power in the server.
[0072] Those skilled in the art will understand that all or part of the processes in the above embodiments can be implemented by a computer program instructing related hardware. The program can be stored in a computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. The storage medium can be a magnetic disk, optical disk, read-only memory (ROM), or random access memory (RAM), etc.
[0073] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in the present invention should be included within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.
Claims
1. A method for generating serial numbers in a cluster system, characterized in that, The cluster system comprises multiple nodes, and the method includes the following steps: S1. Initialize the cluster system, assign a unique node identifier to each node, and set the basic parameters of the serial number. The basic parameters of the serial number include the timestamp start time, timestamp precision, initial value of the serial number, and maximum count value of the serial number. S2. Each node independently receives the serial number generation request and generates its own serial number; the serial number includes the current timestamp, node identifier, and sequence number. S3. Send the serial number and the business association information corresponding to the serial number.
2. The serial number generation method for a cluster system according to claim 1, characterized in that, S1 further includes: synchronizing the time of all nodes in the cluster system through a network time protocol; in S1, the timestamp precision, the initial value of the sequence number, and the maximum count value of the sequence number are stored in a database.
3. The serial number generation method for a cluster system according to claim 2, characterized in that, S2 specifically includes: S21. Retrieve the timestamp precision, initial serial number value, and maximum serial number count value from the database and store them in the node; S22. Receive a serial number generation request, obtain the current timestamp according to the timestamp precision; obtain a serial number according to the initial serial number value, wherein the serial number is the maximum used serial number plus 1; if the serial number is greater than the maximum serial number count value, reset the serial number to the initial serial number value. S23. Concatenate the current timestamp, node identifier, and sequence number to generate a serial number.
4. The serial number generation method for a cluster system according to claim 3, characterized in that, Each node performs step S21 only once, until some or all of the parameters for retrieving timestamp precision, initial serial number value, and maximum serial number count value change; the node completes steps S22 and S23 in its memory.
5. The serial number generation method for a cluster system according to claim 1, characterized in that, Also includes: Obtain the modified configuration information and save the modified configuration information in the node cache; The modified configuration information includes the modified timestamp precision and the effective time; For serial number generation requests received within the effective time period, obtain the current timestamp according to the modified timestamp precision, and generate a serial number.
6. The serial number generation method for a cluster system according to claim 1, characterized in that, The method further includes: Report load information, which includes the number of requests currently being processed by the node and the number of remaining available sequence numbers; Based on the load information, a serial number generation request is received.
7. The serial number generation method for a cluster system according to claim 6, characterized in that, The step of receiving a serial number generation request based on the load information includes: Compare the number of requests currently being processed and generated by a node with a first threshold. If the number of requests currently being processed and generated is greater than or equal to the first threshold, then no serial number generation request is sent to that node, and the next node is polled. If the number of requests currently being processed and generated is less than the first threshold, then proceed to the next step. The remaining number of available sequence numbers of the node is compared with the second threshold. If the remaining number of available sequence numbers is greater than or equal to the second threshold, a serial number generation request is sent to the node. If the remaining number of available sequence numbers is less than the second threshold, no serial number generation request is sent to the node, and the next node is polled. If the remaining number of available sequence numbers of all nodes is less than the second threshold, a serial number generation request is randomly sent to the node until the remaining number of available sequence numbers of all nodes is 0, and the sequence number is reset to the initial value.
8. The serial number generation method for a cluster system according to claim 1, characterized in that, Also includes: Calculate the ratio between the current sequence number of a node and the maximum count value of the sequence number. If the ratio reaches a third threshold, issue a request to borrow the sequence number. The node is the requesting node. Based on the load information of other nodes, a portion of the sequence number of another node is assigned to the requesting node; the other node is the lending node. The borrowing node caches a portion of the sequence number borrowed by the borrowing node in the requesting node, and the requesting node uses the borrowed sequence number within the current timestamp; After use, the requesting node returns the unused sequence number to the lending node and updates the sequence number status of the requesting node.
9. The serial number generation method for a cluster system according to claim 1, characterized in that, Also includes: Compare the number of serial number generation requests currently being processed by the node with the fourth threshold. If the number of serial number generation requests currently being processed is less than the fourth threshold, then generate a serial number in advance and store the pre-generated serial number in the node cache. If a new serial number generation request is received, the pre-generated serial number is retrieved from the node cache. Determine the validity of the retrieved pre-generated serial number. If valid, use the retrieved pre-generated serial number as the serial number for the new serial number generation request.
10. A serial number generation device for a cluster system, characterized in that, include: The initialization configuration module is used to assign a unique node identifier to each node and set the basic parameters of the serial number. The basic parameters of the serial number include the timestamp start time, timestamp precision, initial value of the serial number, and maximum count value of the serial number. The generation module is used for each node to independently receive a serial number generation request and generate its own serial number; the serial number includes the current timestamp, node identifier, and sequence number; The sending module is used to send the serial number and the business association information corresponding to the serial number.