Serial number generation methods, apparatus, devices, storage media and program products

By allocating a unique, fixed, consecutive number segment to each service node in the distributed system and caching it in local memory, the problem of frequent database access during the distributed sequence number generation process is solved, improving system availability and throughput, and enhancing fault tolerance.

CN123115152APending Publication Date: 2026-11-17INDUSTRIAL AND COMMERCIAL BANK OF CHINA
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202610987395.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-07-03
Publication Date
2026-11-17

AI Technical Summary

Technical Problem

In existing technologies, frequent database access during distributed sequence number generation increases system network overhead and load, making performance dependent on the database's processing capacity. This can easily lead to service interruptions or delays during failures or jitter, reducing system availability and throughput.

Method used

When the service node cluster starts up, it competes to obtain its own exclusive and non-overlapping fixed consecutive number segments, which are cached in local memory. Subsequently, it monotonically increments the sequence number within the local number segment, and only accesses the database when the number segment is exhausted.

Benefits of technology

This reduces the frequency of database access, avoids network I/O and database lock contention, enhances the system's fault tolerance and throughput under high concurrency, and reduces dependence on the database.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123115152A_ABST
    Figure CN123115152A_ABST
Patent Text Reader

Abstract

This application provides a serial number generation method, apparatus, device, storage medium, and program product, relating to the field of distributed systems. The method includes, in response to a startup event of a service node cluster, competing with other service nodes from a preset total serial number space in a database to obtain a fixed number segment uniquely corresponding to the target node as a target number segment; upon receiving a serial number generation request from a client, starting from the target serial number, allocating several serial numbers to be returned within the target number segment in a monotonically increasing manner, wherein, if the current target number segment is not exhausted, the target node does not access the database to compete for new fixed number segments during the allocation of serial numbers to be returned; taking the next integer after the maximum value among the serial numbers to be returned as the new target serial number, and returning the serial number to be returned to the client. The method of this application can achieve weak dependency on the database.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of distributed systems, and more particularly to a serial number generation method, apparatus, device, storage medium, and program product. Background Technology

[0002] A distributed sequence ID is a globally unique identifier used to uniquely identify data entities in a distributed system environment. With the continuous growth of internet business scale, system architecture has generally evolved from monolithic applications to distributed, microservice architectures. Traditional database auto-incrementing IDs can no longer meet the uniqueness requirements in cross-node and cross-database scenarios. Distributed sequence IDs, while ensuring global uniqueness, need to possess characteristics such as high performance, trend-incrementing, and high availability. Currently, they are widely used in various core business scenarios such as order numbers, transaction serial numbers, and log identifiers.

[0003] Currently, the mainstream solutions for generating distributed sequence numbers mainly fall into the following categories: First, centralized allocation methods based on database auto-incrementing fields or centralized services, where the generation of all sequence numbers relies on a single or a few central nodes for incremental counting and distribution; second, decentralized algorithms, such as the Snowflake algorithm and its variants based on a combination of timestamps, machine identifiers, and sequence numbers, where each node independently calculates and generates sequence numbers; and third, the use of completely random globally unique identifiers (such as UUIDs), which can be generated locally without coordination. These solutions have solved the uniqueness problem of sequence numbers to varying degrees and have been widely used in their respective applicable scenarios.

[0004] However, in the aforementioned existing technologies, each service node typically needs to frequently interact with the database or other centralized coordination components during each allocation process to obtain the next available sequence number range or confirm the uniqueness of the current sequence number when generating distributed sequence numbers. This high-frequency database access not only increases the system's network overhead and database load, but also makes the performance of the sequence number generation service highly dependent on the database's processing power. This makes it susceptible to service interruptions or latency spikes when the database experiences momentary failures or network fluctuations, thus reducing the availability and throughput of the entire distributed sequence number generation system. Summary of the Invention

[0005] This application provides a serial number generation method, apparatus, device, storage medium, and program product to solve the following problems in the prior art: the high-frequency database access of each service node when generating distributed serial numbers not only increases the network overhead and database load of the system, but also makes the performance of the serial number generation service highly dependent on the database processing capacity. It is easy to cause service interruption or latency spike when the database experiences momentary failure or network jitter, thereby reducing the availability and throughput of the entire distributed serial number generation system.

[0006] In a first aspect, this application provides a serial number generation method, which is applied to a target node in a service node cluster, the method comprising:

[0007] In response to the startup event of the service node cluster, the fixed number segment uniquely corresponding to the target node is obtained from the preset total sequence number space of the database by competing with other service nodes. The fixed number segment is a continuous range of numbers, and the fixed number segments corresponding to different service nodes do not overlap with each other.

[0008] Store the target number segment and the next target sequence number to be allocated in the target number segment into the local memory of the target node;

[0009] After receiving a sequence number generation request from the client, starting from the target sequence number, a number of sequence numbers to be returned are allocated in the target number segment in a monotonically increasing manner. Where the current target number segment is not exhausted, the target node does not access the database to compete for new fixed number segments during the allocation of the sequence numbers to be returned.

[0010] The next integer after the maximum value in the sequence number to be returned is taken as the new target sequence number, and the sequence number to be returned is returned to the client.

[0011] Secondly, this application provides a serial number generation device, which is applied to a target node in a service node cluster, the device comprising:

[0012] The number segment allocation module is used to, in response to the start event of the service node cluster, compete to obtain a fixed number segment uniquely corresponding to the target node from the preset total sequence number space of the database as the target number segment. The fixed number segment is a continuous range of numbers, and the fixed number segments corresponding to different service nodes do not overlap with each other.

[0013] Store the target number segment and the next target sequence number to be allocated in the target number segment into the local memory of the target node;

[0014] The sequence number allocation module is used to, after receiving a sequence number generation request from the client, allocate a number of serial numbers to be returned within the target number segment starting from the target sequence number in a monotonically increasing manner. Wherein, if the current target number segment is not exhausted, the target node does not access the database to compete for new target number segments during the allocation of the serial numbers to be returned.

[0015] The next integer after the maximum value in the sequence number to be returned is taken as the new target sequence number, and the sequence number to be returned is returned to the client.

[0016] Thirdly, this application provides an electronic device, including: a processor, and a memory communicatively connected to the processor;

[0017] The memory stores computer-executed instructions;

[0018] The processor executes computer execution instructions stored in the memory to implement the method as described in the first aspect.

[0019] Fourthly, this application provides a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, are used to implement the method described in the first aspect.

[0020] Fifthly, this application provides a computer program product, including a computer program that, when executed by a processor, implements the method described in the first aspect.

[0021] The serial number generation method, apparatus, device, storage medium, and program product provided in this application have the following technical advantages:

[0022] This method reduces the frequency of database access during sequence number generation from "interaction required for every allocation" to "interaction only during cluster startup or when a sequence number is exhausted" through two core techniques: obtaining fixed sequence number segments through contention in response to startup events and storing them in local memory; and avoiding database access until the sequence number segments are exhausted. This means that most sequence number allocation operations are completed in local memory, avoiding frequent network I / O and database lock contention, thus significantly reducing database load and network latency. Since the runtime no longer heavily relies on real-time database responses, even if the database experiences brief failures or network fluctuations, nodes can continue to provide services based on locally cached sequence numbers, enhancing the system's fault tolerance and throughput under high concurrency. Furthermore, because each node independently allocates its own fixed, non-overlapping sequence numbers, there is no need for a centralized coordinator to participate in each allocation, avoiding single-node bottlenecks that limit overall performance. Attached Figure Description

[0023] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.

[0024] Figure 1 This is a schematic diagram of the serial number generation method provided in the embodiments of this application;

[0025] Figure 2This is a schematic flowchart of a method for competing for target number segments provided in an embodiment of this application;

[0026] Figure 3 This is a flowchart illustrating a method for allocating sequence numbers when the target number segment is exhausted, as provided in an embodiment of this application.

[0027] Figure 4 A schematic diagram of a serial number generation device provided in an embodiment of this application;

[0028] Figure 5 This is a schematic diagram of the electronic device structure provided in an embodiment of this application.

[0029] The accompanying drawings illustrate specific embodiments of this application, which will be described in more detail below. These drawings and descriptions are not intended to limit the scope of the concept in any way, but rather to illustrate the concept of this application to those skilled in the art through reference to particular embodiments. Detailed Implementation

[0030] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numbers in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this application as detailed in the appended claims.

[0031] It should be noted that the serial number generation method, apparatus, device, storage medium, and program product provided in this application can be used in the distributed field, or in any field other than the distributed field. The application field of the serial number generation method, apparatus, device, storage medium, and program product in this application is not limited.

[0032] To address the following problems in existing technologies: the high-frequency database accesses by each service node during distributed sequence number generation not only increase the system's network overhead and database load, but also make the performance of the sequence number generation service highly dependent on the database's processing power. This makes it susceptible to service interruptions or latency spikes when the database experiences momentary failures or network fluctuations, thus reducing the availability and throughput of the entire distributed sequence number generation system. The technical concept of this application is as follows:

[0033] When the service node cluster starts up, it competes to obtain its own exclusive and non-overlapping fixed consecutive number segments from the database and caches them in local memory. When it receives a sequence number generation request, it allocates sequence numbers only in a monotonically increasing manner within the local fixed number segments. As long as the current number segment is not exhausted, it will not access the database again.

[0034] The technical solution of this application and how the technical solution of this application solves the above-mentioned technical problems are described in detail below with specific embodiments. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments. The embodiments of this application will now be described with reference to the accompanying drawings.

[0035] This embodiment provides a serial number generation method applied to a distributed service node cluster consisting of multiple service nodes. Each service node is an independent application process built on a mainstream Java application framework and deployed in a containerized infrastructure, such as a container group managed by a container orchestration platform. The service node cluster backend shares a common database system for storing node number allocation information and business configuration information. Clients initiate serial number generation requests to any service node via a network interface (such as the HTTP protocol).

[0036] In this embodiment, the serial number is composed of three parts: a fixed business prefix (4 alphanumeric characters), a date (8 digits, format yyyyMMdd), and a pure numeric serial number body (11 digits, padded with zeros on the left).

[0037] Figure 1 This is a schematic diagram of the serial number generation method provided in the embodiments of this application, such as... Figure 1 As shown, the method includes:

[0038] S101. In response to the startup event of the service node cluster, the fixed number segment uniquely corresponding to the target node is obtained from the preset total sequence number space of the database by competing with other service nodes. The fixed number segment is a continuous range of numbers, and the fixed number segments corresponding to different service nodes do not overlap with each other.

[0039] During the initialization phase, a numbering table needs to be pre-created in the database. This table contains two fields: `node_id` (an auto-incrementing primary key of type BIGINT) and `container_id` (a string type, used to uniquely identify container instances, such as the container name or UUID). Simultaneously, a record `total_nodes` is maintained in the global configuration table, representing the expected total number of nodes in the current cluster. For example, the total number of nodes could be 10, and this number can be manually set by operations personnel during deployment.

[0040] When each service node starts, it first reads the value of `total_nodes` from the global configuration table, denoted as N. Then, the service node inserts a record into the numbering table, filling in only its own `container_id`. It should be noted that since the `node_id` field is an auto-incrementing primary key, the database system generates a globally unique, strictly incrementing integer ID for this insertion operation. This integer ID is the node number in this round of the cluster, denoted as `node_index` (counting from 1). Due to the atomicity of the auto-increment operation, the `node_index` obtained by multiple concurrently started nodes will necessarily be different and cover the range from 1 to N. If the actual number of nodes exceeds N, some nodes may obtain numbers greater than N, which may further trigger alarms or node expansion processes. This embodiment will not elaborate on this further.

[0041] During the competition for fixed number segments, the service node calculates its own dedicated fixed number segment based on node_index and N. Specifically:

[0042] The preset total sequence number space is the entire range of 11 decimal digits, from 0 to 99,999,999,999, totaling 100 billion digits. To avoid ambiguity, in practical applications, the actual usable range of ordinary number segments can be from 1,000,000,000 to 99,999,999,999, i.e., digits starting with 0 are removed and retained. Each service node obtains a fixed number segment length of floor, where floor = total usable length / N, and the total usable length is the actual usable range of ordinary number segments, with the start and end values ​​determined by the following formula:

[0043] Initial value = 1,000,000,000 + (node_index - 1) × segment length;

[0044] End value = Start value + Segment length – 1;

[0045] For example, when N=10 and the segment length is 9,900,000,000, the number range of node 1 is [1,000,000,000 to 10,899,999,999], node 2 is [10,900,000,000 to 20,799,999,999], and so on. Since each service node has a unique node_index and the segment length is fixed, the fixed number ranges of each node do not overlap.

[0046] S102. Store the target number segment and the next target sequence number to be allocated in the target number segment into the local memory of the target node;

[0047] In this step, the service node stores the start and end values ​​of the target number segment calculated in the previous steps, as well as the current pointer to be allocated, in its local memory. The current pointer to be allocated (i.e., the next target sequence number to be allocated) is initialized to the start value of the target number segment. This local memory can be, for example, Java heap memory. Simultaneously, the service node records the business identifier corresponding to this number segment, which can be, for example, a business number read from a configuration file. This way, subsequent sequence number generation does not require reliance on the database.

[0048] S103. After receiving the sequence number generation request from the client, starting from the target sequence number, allocate a number of sequence numbers to be returned in the target number segment in a monotonically increasing manner. Where the current target number segment is not exhausted, the target node does not access the database to compete for new fixed number segments during the allocation of sequence numbers to be returned.

[0049] The client first sends a network request to any service node. The request body contains: bizTag (business number), date (in the format yyyyMMdd), and count (the number of serial numbers required, not exceeding 1000). Then, the service node finds the corresponding number segment data in its local memory based on bizTag.

[0050] The service node reads the current pointer to be allocated, `current`, from local memory and checks if `current + count - 1` is less than or equal to the end value (i.e., the target number segment is not exhausted). If satisfied, the following operations are performed:

[0051] Use the old current value as the starting sequence number for this allocation.

[0052] For each integer from `old current` to `old current + count - 1`, concatenate the business prefix and date to form a complete sequence number string. For example, the sequence number string could be: `BIZ01-20260623-00001000001`. It should be noted that because `current` is always monotonically increasing, and atomic operations ensure that no two threads within the same service node modify the pointer simultaneously, the sequence numbers generated within the same service node are strictly incremental and non-repeating. Furthermore, because the number ranges of different service nodes do not overlap, a globally unique sequence number can be obtained.

[0053] In the case of target number segment exhaustion, when current reaches or exceeds the end value, it indicates that the current target number segment has been exhausted. At this time, the service node can no longer allocate new sequence numbers through this target number segment, and the service node needs to re-execute the contention steps of the startup phase.

[0054] Step 1: Insert another record into the number table to obtain a new auto-incrementing node_id;

[0055] Step 2: Calculate a new fixed number segment as the target number segment based on the new node_id and the current total number of nodes N in the cluster;

[0056] Step 3: Load the new target number segment and the current pointer to be allocated (initialized to the starting value of the new target number segment, as before) into local memory, replacing the old number segment data. Then, the service node continues to respond to client requests using the new number segment.

[0057] S104. Take the next integer after the maximum value in the sequence number to be returned as the new target sequence number, and return the sequence number to be returned to the client.

[0058] This step uses an atomic operation to increment the value of current by count, thus obtaining a new target sequence number. Subsequently, the service node returns these newly assigned sequence numbers as a response to the client.

[0059] Figure 1 The provided serial number generation method has the following technical advantages:

[0060] This method reduces the frequency of database access during sequence number generation from "interaction required for every allocation" to "interaction only during cluster startup or when a sequence number is exhausted" through two core techniques: obtaining fixed sequence number segments through contention in response to startup events and storing them in local memory; and avoiding database access until the sequence number segments are exhausted. This means that most sequence number allocation operations are completed in local memory, avoiding frequent network I / O and database lock contention, thus significantly reducing database load and network latency. Since the runtime no longer heavily relies on real-time database responses, even if the database experiences brief failures or network fluctuations, nodes can continue to provide services based on locally cached sequence numbers, enhancing the system's fault tolerance and throughput under high concurrency. Furthermore, because each node independently allocates its own fixed, non-overlapping sequence numbers, there is no need for a centralized coordinator to participate in each allocation, avoiding single-node bottlenecks that limit overall performance.

[0061] exist Figure 1 Based on the method shown, this embodiment provides a method for obtaining a fixed number segment uniquely corresponding to a target node from a preset total sequence number space in the database, competing with other service nodes as the target number segment. Figure 2 This is a schematic flowchart of a method for competing for target number segments provided in an embodiment of this application, as shown below. Figure 2 As shown, the method includes:

[0062] S201. Apply an exclusive lock to the numbering table that records multiple node numbers. The exclusive lock is used to prevent other service nodes from performing read and write operations on the numbering table.

[0063] Specifically, during the initialization phase, a number table needs to be created in the database beforehand. The number table includes the following fields:

[0064] node_id: An integer used as the service node number, with a value range of 1 to N (N is the preset total number of nodes in the cluster). Each number can only be used by one service node.

[0065] status: a character type that indicates whether the number has been assigned, for example, "assigned" or "free";

[0066] container_id: A string type that records the identifier of the service node that occupies this number.

[0067] When each service node starts up, it first connects to the database and starts a database transaction. Then, an exclusive lock is applied to the numbering table. The purpose of the exclusive lock is that at any given time, only one service node can perform read and write operations on the numbering table. All other nodes that attempt to access the table must wait until the exclusive lock is released, thus ensuring the atomicity and mutual exclusion of the subsequent numbering selection process.

[0068] S202. Among multiple node numbers, determine the candidate numbers that have not been assigned to other service nodes, and determine the target number that uniquely corresponds to the target node among the candidate numbers;

[0069] In this step, while holding an exclusive lock, the service node scans all records in the number table to find those node_ids with a status of "free", forming a candidate number set. The candidate numbers have not yet been occupied by any service node.

[0070] Next, the service node selects a number from the candidate number set as its target number. The selection strategy can be:

[0071] Strategy 1: Take the smallest available number;

[0072] Strategy 2: Select a random available number.

[0073] In this embodiment, strategy 1 can be adopted. For example, if numbers 1 and 2 have been assigned and numbers 3, 4, and 5 are idle, then the service node selects number 3 as the target number.

[0074] After selecting a target number, the service node updates the status of the record corresponding to the target number to "assigned" and writes its own identifier into the container_id field. At this point, the service node has successfully locked the target number.

[0075] If, during the process of holding an exclusive lock, it is found that all numbers have been assigned, it means that the number of service nodes actually started exceeds the preset value of N. At this time, the service node can trigger an alarm and exit or enter a waiting state, waiting for the operation and maintenance personnel to adjust the configuration.

[0076] S203. Obtain the range of numbers corresponding to the target number as the target number range and release the exclusive lock. Different candidate numbers correspond to different ranges of numbers.

[0077] In this step, the service node calculates its own fixed number range according to a predetermined formula based on the selected target number (denoted as node_index) and the total number of cluster nodes N read from another global configuration table in the database. The calculation formula is the same as in the previous embodiment, thereby obtaining the target number range of the service node.

[0078] Figure 2 The method shown has the following technical effects:

[0079] Exclusive locks ensure that each node obtains a unique node number, thus securing a fixed number range that does not overlap, fundamentally preventing sequence number conflicts.

[0080] Following the aforementioned embodiments, during the startup phase, the service node obtains a unique fixed number segment, i.e., the target number segment, through competition. It stores the start and end values ​​of this target number segment, along with the current pointer to be allocated (pointing to the next sequence number to be allocated), in its local memory. During normal operation, each time the service node receives a sequence number generation request from a client, it monotonically and incrementally allocates several consecutive sequence numbers starting from the current pointer to be allocated, until the target number segment is exhausted. Based on this, this embodiment provides a method for allocating sequence numbers when the target number segment is exhausted. This method introduces the concepts of "initialization date" and "disaster recovery number segment" to safely reset the number segment or switch to backup resources without relying on external coordination services. Figure 3 This is a flowchart illustrating the method for allocating sequence numbers when the target number segment is exhausted, as provided in the embodiments of this application. Figure 3 As shown, the method includes:

[0081] S301. When a serial number generation request is received for the first time, the date information contained in the first received serial number generation request shall be used as the initialization date.

[0082] When a service node first receives a sequence number generation request from a client, regardless of which client the request originates from or what business identifier it carries, the service node extracts the date information from the request. This information could be from a date field in the request parameters, in the format yyyyMMdd. The service node then uses this date information as its initialization date for this runtime cycle and stores it in its local memory. Once set, this initialization date remains unchanged in subsequent processing unless explicitly updated according to later steps.

[0083] It should be noted that the purpose of the initialization date is to establish a time baseline. Service nodes compare the dates of subsequent requests with this baseline to determine whether a time window has been crossed (e.g., a day has passed), thereby deciding whether it is safe to reset ordinary number segments without generating duplicate sequence numbers.

[0084] S302. When the target number segment is exhausted, determine whether the date information in the latest received sequence number generation request is the same as the initialization date;

[0085] When the current pending pointer of a service node reaches or exceeds the end value of the target number segment, it indicates that the target number segment has been exhausted. At this time, the service node will no longer allocate sequence numbers from the target number segment, but will enter the following judgment logic:

[0086] The service node retrieves the date information carried in the most recently received sequence number generation request, which can be exemplified as currentDate;

[0087] If currentDate equals the initialization date, it means the time window has not changed since the service node started, for example, it's still within the same day. In this case, if the target number range is directly reset and allocated from the beginning, it will generate the same number as a previously allocated sequence number, thus violating global uniqueness. Therefore, the service node cannot reset the target number range and must take other measures;

[0088] If currentDate is greater than the initialization date, it means that a new time window has been entered, for example, the next day or later. In this case, even if the numbers already assigned in the original target number range are duplicated in the new time window, it will not cause a conflict. This is because the sequence number usually contains a date prefix, so the sequence numbers for different dates are different as a whole. Therefore, it is safe to reset the target number range and start assigning from the beginning.

[0089] If so, execute S303, terminate the use of the target number segment, and allocate a new sequence number in the disaster recovery number segment corresponding to the target node;

[0090] When currentDate equals the initialization date, the service node performs the following operations:

[0091] The service node terminates the use of the target number range and marks the target number range as "exhausted", and will no longer attempt to allocate from it in the future.

[0092] Then, the service node retrieves a pre-defined disaster recovery number segment from its local memory or database. The disaster recovery number segment and the target number segment do not overlap, and their starting values ​​are different. For example, the target number segment starts with a non-zero number, while the disaster recovery number segment starts with 0. This design physically divides the ordinary number segment and the disaster recovery number segment into two independent digital spaces, preventing numerical overlap between the two segments. At the same time, the different starting values ​​make it easier for operations and maintenance personnel to quickly determine which type of number segment is being used based on the sequence number prefix, which is beneficial for monitoring and troubleshooting.

[0093] Disaster recovery number segments can be acquired by service nodes during startup or through atomic database operations upon initial need. This method of acquiring disaster recovery number segments is similar to the method of acquiring target number segments, but different numbering tables or different segment identifiers can be used. After acquiring a disaster recovery number segment, the service node uses its initial value as the new current unallocated pointer and begins to monotonically increment the allocation of sequence numbers within the disaster recovery number segment.

[0094] Within the disaster recovery number range, service nodes still follow a monotonically increasing principle and do not rely on a database. Disaster recovery number ranges typically have a large capacity, for example, a total capacity of approximately 10 billion digits, sufficient to support long-term emergency use.

[0095] If not, then execute S304, allocate a new sequence number starting from the starting value of the target number segment, and use the date information in the latest received sequence number generation request as the new initialization date;

[0096] When currentDate is greater than the initialization date, the service node resets the current unallocated pointer to the starting value of the target number segment, which is the startId calculated at the time of startup, so that the target number segment can be allocated from the beginning.

[0097] Then, the service node updates the initialization date stored locally to the latest currentDate. Afterward, the new initialization date will become the benchmark for the next determination.

[0098] The service node then assigns sequence numbers starting from the beginning of the target number range, following a normal monotonically increasing pattern. Because the date has changed, even if the newly assigned sequence number is the same as a previous one, the entire sequence number string remains globally unique due to the different date prefix.

[0099] S305. Return the newly assigned serial number and the current initialization date to the client.

[0100] Regardless of the scenario described above, after successfully allocating a batch of sequence numbers, the service node needs to return the following information to the client:

[0101] The newly assigned sequence number list contains several complete sequence number strings generated this time, including the business prefix, date, and incrementing numbers.

[0102] Current initialization date: This refers to the initialization date of the current record within the node. It could be the date of the initial record or a newly updated date. Clients can use this initialization date for subsequent verification or logging.

[0103] It should be noted that the purpose of returning the initialization date is to allow the client to be aware of the date base status inside the node, which is convenient for debugging and auditing; at the same time, if the client sends a different date in a subsequent request, the service node can make the correct judgment based on the initialization date it has recorded.

[0104] Figure 3 The method shown has the following technical effects:

[0105] 1. By introducing an initialization date and comparing it with the request date, it ensures that when the number segment is exhausted on the same date, it will not be blindly reset, but will switch to the disaster recovery number segment, thus preventing the serial number from being duplicated due to the reset;

[0106] 2. When the requested date is greater than the initialization date, the ordinary number segment is automatically reset and the base date is updated, so that the node can reuse the original number segment space in the new time window without having to compete with the database for new number segments again, which can reduce database dependency and competition overhead.

[0107] exist Figure 3 Based on the method shown, after determining that the date information in the latest received sequence number generation request is the same as the initialization date, the method further includes:

[0108] From the database's preset disaster recovery sequence number space, compete for the disaster recovery number segment corresponding to the target node. The disaster recovery number segments corresponding to different service nodes do not overlap with each other.

[0109] Specifically, during the system initialization phase, the database is divided into two independent sequence number spaces:

[0110] Ordinary sequence number space: a fixed number segment used for competition when service nodes start up in the aforementioned embodiments. Its number range starts with a non-zero number, for example, 1,000,000,000 to 99,999,999,999.

[0111] Disaster recovery sequence number space: specifically used for emergency scenarios, its number range starts with 0, for example 0 to 999,999,999, and does not overlap with the ordinary sequence number space at all.

[0112] After confirming that the date information in the latest received sequence number generation request is the same as the initialization date, the service node enters the stage of competing for disaster recovery number segments. The implementation method is the same as the competition for fixed number segments during service node startup, and will not be elaborated upon here. Furthermore, the service node can write the results of this disaster recovery number segment competition, such as the service node identifier, disaster recovery node number, disaster recovery number segment range, and allocation time, into the disaster recovery number segment allocation table in the database, so that it can claim the original segment number during subsequent restarts or fault recovery.

[0113] The technical advantage of the above scheme is that nodes only compete for disaster recovery number segments when their target number segment is exhausted and the date remains unchanged, rather than pre-occupying them at startup. This allows disaster recovery space to be flexibly shared by multiple nodes, improving overall resource utilization. Furthermore, when the number of cluster nodes changes, simply adjusting the N value (number of nodes) and re-competing allows the disaster recovery number segments to be automatically and evenly redistributed without manual intervention.

[0114] Based on the foregoing embodiments, the method further includes:

[0115] In response to the startup event, read the current state of the global switch in the database;

[0116] If the current state is a triggered state, then the step of competing for a fixed number segment is executed;

[0117] If the current state is non-triggered, the step of competing for a fixed number segment will not be executed, and a new sequence number will be allocated within the number segment range already assigned to the target node. After all service nodes have competed to obtain the corresponding fixed number segment, the global switch is set to non-triggered state.

[0118] Specifically, a record is set in the global configuration table with a field named switch_status. This field is a boolean value that represents the status of the global switch. The initial value of this field is "triggered state" (e.g., true). It is set to true by the operations and maintenance personnel or the deployment script during a full restart or the first deployment. After all service nodes have completed a round of number segment competition, the last service node to complete the competition sets it to "non-triggered state" (false).

[0119] After the service node starts, it first connects to the database and queries the value of the switch_status field in the global configuration table. The specific branches are as follows:

[0120] If switch_status is "triggered": it indicates that this is a full restart or the first deployment. All nodes need to compete for the fixed number segment again. The service nodes compete for the fixed number segment. The method of competing for the fixed number segment can be referred to the above embodiment.

[0121] If switch_status is "non-triggered": This indicates that the service node cluster is in a normal rolling restart or single-node failure recovery scenario. The existing number segment division should not be disrupted. In this case, the node enters "recovery mode." In recovery mode:

[0122] The service node uses its own container_id as the query condition to look up the most recent allocation record in the node allocation record table. If a valid record is found, that is, the start and end values ​​of the number segment are not empty, the start and end values ​​of the record are loaded into local memory, and the current allocation pointer is set to the start value, or restored from the pointer position of the record in the last run.

[0123] If no record is found, for example, if the node is a newly added node and the switch is in a non-triggered state, the service node can choose to wait for manual intervention from the operations and maintenance personnel.

[0124] The technical effects of the above solution are as follows:

[0125] 1. In rolling restart or single-node failure recovery scenarios, the global switch is in a non-triggered state. Service nodes can reuse existing number segments without re-competing, thereby significantly reducing the pressure of database write operations and lock contention, shortening node startup time, and preventing number segment fragmentation caused by frequent re-segmentation.

[0126] 2. During off-peak hours, maintenance personnel can perform a full restart, set the global switch to the triggered state, and complete the number segment redistribution. In daily elastic scaling or fault recovery, service nodes can quickly restore the original number segments without affecting the ongoing serial number generation task.

[0127] Figure 4 This is a schematic diagram of a serial number generation device provided in an embodiment of this application. The device 40 is applied to a target node in a service node cluster, such as... Figure 4 As shown, the device 40 includes:

[0128] Number segment allocation module 401 is used to, in response to the start event of the service node cluster, compete to obtain a fixed number segment uniquely corresponding to the target node from the preset total sequence number space of the database as the target number segment. The fixed number segment is a continuous range of numbers, and the fixed number segments corresponding to different service nodes do not overlap with each other.

[0129] Store the target number segment and the next target sequence number to be allocated in the target number segment into the local memory of the target node;

[0130] The sequence number allocation module 402 is used to allocate a number of serial numbers to be returned in a monotonically increasing manner within the target number segment after receiving a serial number generation request from the client. Where the current target number segment is not exhausted, the target node does not access the database to compete for new target number segments during the allocation of serial numbers to be returned.

[0131] The next integer after the maximum value in the sequence number to be returned is used as the new target sequence number, and the sequence number to be returned is returned to the client.

[0132] Figure 5 This is a schematic diagram of the electronic device structure provided in the embodiments of this application, such as... Figure 5 As shown, the device 50 includes at least one processor 501 and a memory 502. Optionally, the device 50 also includes a communication component 503. The processor 501, memory 502, and communication component 503 are connected via a bus 504.

[0133] In a specific implementation, at least one processor 501 executes computer execution instructions stored in memory 502, causing at least one processor 501 to perform the above-described method.

[0134] The specific implementation process of processor 501 can be found in the above method embodiments, and its implementation principle and technical effect are similar. It will not be repeated here.

[0135] This application also provides a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, implement the above-described method.

[0136] This application also provides a computer program product, including a computer program that, when executed by a processor, implements the above-described method.

[0137] It should be noted that, for the sake of simplicity, the foregoing method embodiments are all described as a series of actions. However, those skilled in the art should understand that this application is not limited to the described order of actions, as some steps may be performed in other orders or simultaneously according to this application. Furthermore, those skilled in the art should also understand that the embodiments described in the specification are all optional embodiments, and the actions and modules involved are not necessarily essential to this application.

[0138] It should be further noted that although the steps in the flowchart are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowchart may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these sub-steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the sub-steps or stages of other steps.

[0139] It should be understood that the above-described device embodiments are merely illustrative, and the device of this application can also be implemented in other ways. For example, the division of units / modules in the above embodiments is only a logical functional division, and there may be other division methods in actual implementation. For example, multiple units, modules, or components may be combined, or integrated into another system, or some features may be ignored or not executed.

[0140] Furthermore, unless otherwise specified, the functional units / modules in the various embodiments of this application can be integrated into one unit / module, or each unit / module can exist physically separately, or two or more units / modules can be integrated together. The integrated units / modules described above can be implemented in hardware or as software program modules.

[0141] When integrated units / modules are implemented in hardware, the hardware can be digital circuits, analog circuits, etc. The physical implementation of the hardware structure includes, but is not limited to, transistors, memristors, etc. Unless otherwise specified, the processor can be any suitable hardware processor, such as a CPU, GPU, FPGA, DSP, and ASIC, etc. Unless otherwise specified, the storage unit can be any suitable magnetic or magneto-optical storage medium, such as Resistive Random Access Memory (RRAM), Dynamic Random Access Memory (DRAM), Static Random Access Memory (SRAM), Enhanced Dynamic Random Access Memory (EDRAM), High-Bandwidth Memory (HBM), Hybrid Memory Cube (HMC), etc.

[0142] If the integrated unit / module is implemented as a software program module and sold or used as an independent product, it can be stored in a computer-readable storage device (CMD). Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a memory and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods of the various embodiments of this application. The aforementioned memory includes various media capable of storing program code, such as a USB flash drive, read-only memory (ROM), random access memory (RAM), portable hard drive, magnetic disk, or optical disk.

[0143] In the above embodiments, the descriptions of each embodiment have their own emphasis. For parts not described in detail in a certain embodiment, please refer to the relevant descriptions of other embodiments. The technical features of the above embodiments can be combined arbitrarily. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as the combination of these technical features does not contradict each other, it should be considered within the scope of this specification.

[0144] Other embodiments of this application will readily occur to those skilled in the art upon consideration of the specification and practice of the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of this application that follow the general principles of this application and include common knowledge or customary techniques in the art not disclosed herein. The specification and examples are to be considered exemplary only, and the true scope and spirit of this application are indicated by the following claims.

[0145] It should be understood that this application is not limited to the precise structure described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of this application is limited only by the appended claims.

Claims

1. A serial number generation method, characterized in that, The method is applied to a target node in a service node cluster, and the method includes: In response to the startup event of the service node cluster, the fixed number segment uniquely corresponding to the target node is obtained from the preset total sequence number space of the database by competing with other service nodes. The fixed number segment is a continuous range of numbers, and the fixed number segments corresponding to different service nodes do not overlap with each other. Store the target number segment and the next target sequence number to be allocated in the target number segment into the local memory of the target node; After receiving a sequence number generation request from the client, starting from the target sequence number, a number of sequence numbers to be returned are allocated in the target number segment in a monotonically increasing manner. Where the current target number segment is not exhausted, the target node does not access the database to compete for new fixed number segments during the allocation of the sequence numbers to be returned. The next integer after the maximum value in the sequence number to be returned is taken as the new target sequence number, and the sequence number to be returned is returned to the client.

2. The method according to claim 1, characterized in that, The step of competing with other service nodes in the preset total sequence number space of the database to obtain a fixed number segment uniquely corresponding to the target node as the target number segment includes: An exclusive lock is applied to an ID table that records multiple node IDs, the exclusive lock being used to prevent other service nodes from performing read and write operations on the ID table; Among the plurality of node numbers, candidate numbers that have not been assigned to other service nodes are determined, and a target number that uniquely corresponds to the target node is determined from among the candidate numbers; Obtain the range of numbers corresponding to the target number as the target number range, and release the exclusive lock, wherein different candidate numbers correspond to different ranges of numbers.

3. The method according to claim 1, characterized in that, The method further includes: When the serial number generation request is received for the first time, the date information contained in the first received serial number generation request is used as the initialization date; When the target number segment is exhausted, it is determined whether the date information in the latest received sequence number generation request is the same as the initialization date; If so, then the use of the target number segment will be terminated, and a new sequence number will be allocated in the disaster recovery number segment corresponding to the target node; If not, a new sequence number is allocated starting from the starting value of the target number segment, and the date information in the latest received sequence number generation request is used as the new initialization date; The newly assigned serial number and the current initialization date are returned to the client.

4. The method according to claim 3, characterized in that, The disaster recovery number segment and the target number segment do not overlap with each other, and the starting values ​​of the disaster recovery number segment and the target number segment are different.

5. The method according to claim 3, characterized in that, After determining that the date information in the latest received sequence number generation request is the same as the initialization date, the method further includes: From the preset disaster recovery sequence number space of the database, the disaster recovery number segment corresponding to the target node competes for the disaster recovery number segment, and the disaster recovery number segments corresponding to different service nodes do not overlap with each other.

6. The method according to claim 1, characterized in that, The method further includes: In response to the startup event, read the current state of the global switch in the database; If the current state is a triggered state, then the step of competing for the fixed number segment is executed; If the current state is a non-triggered state, the step of competing for the fixed number segment will not be executed, and a new sequence number will be allocated in the number segment range that the target node has been allocated. After all the service nodes have competed to obtain the corresponding fixed number segment, the global switch will be set to the non-triggered state.

7. A serial number generation device, characterized in that, The device is applied to a target node in a service node cluster, and the device includes: The number segment allocation module is used to, in response to the start event of the service node cluster, compete to obtain a fixed number segment uniquely corresponding to the target node from the preset total sequence number space of the database as the target number segment. The fixed number segment is a continuous range of numbers, and the fixed number segments corresponding to different service nodes do not overlap with each other. Store the target number segment and the next target sequence number to be allocated in the target number segment into the local memory of the target node; The sequence number allocation module is used to, after receiving a sequence number generation request from the client, allocate a number of serial numbers to be returned within the target number segment starting from the target sequence number in a monotonically increasing manner. Wherein, if the current target number segment is not exhausted, the target node does not access the database to compete for new target number segments during the allocation of the serial numbers to be returned. The next integer after the maximum value in the sequence number to be returned is taken as the new target sequence number, and the sequence number to be returned is returned to the client.

8. An electronic device, characterized in that, include: A processor, and a memory communicatively connected to the processor; The memory stores computer-executed instructions; The processor executes computer execution instructions stored in the memory to implement the method as described in any one of claims 1 to 6.

9. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions, which, when executed by a processor, are used to implement the method as described in any one of claims 1 to 6.

10. A computer program product, characterized in that, Includes a computer program that, when executed by a processor, implements the method of any one of claims 1 to 6.