Serial number generation method, device, equipment, storage medium and program product
By decomposing the sequence composition in high concurrency scenarios and using preset basis conversion methods, the problem of excessively long sequence bits and duplicate IDs in traditional sequence generation schemes is solved, and the uniqueness and efficiency of sequence numbers are achieved.
Patent Information
- Application Number
- CN202411609761.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-12
- Publication Date
- 2025-04-01
AI Technical Summary
The traditional sequence generation scheme ultimately generates a long sequence bit, which may generate duplicate IDs, making it difficult to ensure the uniqueness of the sequence, especially in high concurrency scenarios.
When receiving the service sequence generation request, the instance identification and date offset value corresponding to the application instance are determined, and sequence allocation is performed through the counter pool of the application instance to obtain the counter count value. Then, based on the instance identification, date offset value, counter identification of counter pool, and counter count value, it is combined into an initial sequence and converted into a unique sequence number in the preset binary.
It effectively avoids the problem of too long serial bits, and in multiple applications and multiple instances, the uniqueness of the serial number is ensured by decomposing the sequence composition and adopting preset binary conversion in multiple applications and multiple instances.
Smart Images

Figure CN119739709A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of communication technology, and in particular to a method, device, equipment, storage medium and program product for generating a serial number. Background Art
[0002] Sequence is one of the most commonly used data in application systems. It is required in various environments such as database primary keys and business codes. The generated serial number (also called sequence value) is used to identify specific objects or businesses in the application system to ensure that each individual has a unique identity. Sequence numbers can generally be generated through traditional database sequences. Although this method can ensure uniqueness, each generated sequence number requires database input and output, which has great limitations. In high-concurrency scenarios, the uniqueness of sequences of other applications cannot be guaranteed.
[0003] The traditional snowflake algorithm is a distributed ID generation algorithm, the core of which is to generate a 64-bit integer with timestamps, machine identifiers, etc. The snowflake algorithm is very practical in high-concurrency scenarios with multiple applications and multiple instances that require global uniqueness. However, due to the limitations of its own computing model, the snowflake algorithm not only generates a longer sequence of bits, but also has a probability of generating duplicate IDs, making it difficult to ensure the uniqueness of the sequence. Summary of the invention
[0004] The main purpose of the present application is to provide a method, device, equipment, storage medium and program product for generating a serial number, aiming to solve the technical problem that the traditional sequence generation scheme generates a relatively long sequence of bits, may generate duplicate IDs, and is difficult to ensure the uniqueness of the sequence.
[0005] To achieve the above purpose, the present application proposes a method for generating a serial number, the method comprising:
[0006] When receiving a sequence generation request for a business, determining an instance identifier and a date offset value corresponding to the application instance, wherein the instance identifier is allocated to each application instance by a master control center of the business system;
[0007] Perform sequence allocation through the counter pool of the application instance to obtain a counter count value;
[0008] Combining an initial sequence according to the instance identifier, the date offset value, the counter identifier of the counter pool and the counter count value;
[0009] The initial sequence is converted into a unique sequence number in a preset base.
[0010] In one embodiment, before the step of determining the instance identifier and the date offset value corresponding to the application instance when receiving the sequence generation request of the service, the instance identifier allocation process includes:
[0011] When receiving a registration request from an application instance, obtain the current sequence value;
[0012] Performing a lock sequence detection in the allocated application sequence table according to the current sequence value and a preset rule;
[0013] When the detection result passes, the detection sequence value corresponding to the detection result is used as the instance identifier of the application instance.
[0014] In one embodiment, the step of performing a lock sequence detection in the allocated application sequence table according to the current sequence value and a preset rule includes:
[0015] Add one to the current sequence value to obtain a detection sequence value;
[0016] According to the detection sequence value, detecting whether there is a used sequence value in the allocated application sequence table of the database;
[0017] If the used sequence value exists in the allocated application sequence table, return to execute the step of adding one to the current sequence value to obtain the detection sequence value until the used sequence value does not exist in the allocated application sequence table, and save the detection sequence value corresponding to the non-existence of the used sequence value to the database.
[0018] In one embodiment, the process of determining the date offset value includes:
[0019] Get the start date of the application instance when it is created;
[0020] determining a current date of the sequence generation request;
[0021] A date offset value of the application instance is calculated according to the start date and the current date.
[0022] In one embodiment, the step of performing sequence allocation through the counter pool of the application instance to obtain the counter count value includes:
[0023] Generate a counting instruction of a counter pool of the application instance according to the sequence generation request;
[0024] Traversing the counter pool according to the counting instruction to determine available counters;
[0025] The available counters are sequentially allocated in ascending order to obtain counter count values.
[0026] In one embodiment, before the step of determining the instance identifier and date offset value corresponding to the application instance when receiving the sequence generation request of the service, the step further includes:
[0027] Get the number of instances and concurrent requests of the business system;
[0028] Perform fluctuation prediction based on the number of instances and the number of concurrent requests to determine the fluctuation range of the number of requests of the business system;
[0029] When the numerical boundary of the request quantity fluctuation range reaches a preset threshold, determining the sequence length margin by the numerical boundary;
[0030] Adjusting the initial sequence length according to the sequence length margin to obtain a preset sequence length;
[0031] Accordingly, the step of combining the instance identifier, the date offset value, the counter identifier of the counter pool and the counter count value into an initial sequence includes:
[0032] An initial sequence of the preset sequence length is combined according to the instance identifier, the date offset value, the counter identifier of the counter pool, and the counter count value.
[0033] In addition, to achieve the above purpose, the present application also proposes a serial number generating device, the device comprising:
[0034] An application instance module, used to determine an instance identifier and a date offset value corresponding to an application instance when receiving a sequence generation request of a business, wherein the instance identifier is allocated to each application instance by a master control center of the business system;
[0035] A counter module, used for performing sequence allocation through a counter pool of the application instance to obtain a counter count value;
[0036] An initial sequence module, used for combining an initial sequence according to the instance identifier, the date offset value, the counter identifier of the counter pool and the counter count value;
[0037] The base conversion module is used to convert the initial sequence into a unique serial number of a preset base.
[0038] In addition, to achieve the above-mentioned purpose, the present application also proposes a serial number generating device, which includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program is configured to implement the steps of the serial number generating method as described above.
[0039] In addition, to achieve the above-mentioned purpose, the present application also proposes a storage medium, which is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by a processor, the steps of the serial number generation method described above are implemented.
[0040] In addition, to achieve the above-mentioned purpose, the present application also provides a computer program product, which includes a computer program, and when the computer program is executed by a processor, the steps of the serial number generation method described above are implemented.
[0041] One or more technical solutions proposed in this application have at least the following technical effects: when receiving a sequence generation request from a business, this application first determines the instance identifier and date offset value corresponding to the application instance, and the instance identifier is obtained by allocating it to each application instance through the general control center of the business system; then the sequence is allocated through the counter pool of the application instance to obtain the counter count value; then, an initial sequence is formed based on the instance identifier, the date offset value, the counter identifier of the counter pool, and the counter count value; finally, the initial sequence is converted into a unique serial number in a preset base. Since this application decomposes the sequence composition into four parts, including the instance identifier, the date offset value, the counter identifier, and the counter count value, and uses a preset base for conversion, it can not only avoid the situation where the number of digits is long, but also ensure the uniqueness of the serial number through the above four parts in the scenario of multiple applications and multiple instances. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.
[0043] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
[0044] Figure 1 A flowchart of the first embodiment of the method for generating a serial number of the present application is provided;
[0045] Figure 2 A schematic diagram of sequence splitting provided in Example 1 of the present application;
[0046] Figure 3 This is an example diagram of the sequence value composition provided in Example 1 of the present application;
[0047] Figure 4 A flowchart of the second embodiment of the method for generating a serial number of the present application is provided;
[0048] Figure 5 A schematic diagram of a process for allocating instance IDs by a master control center provided in Embodiment 2 of the present application;
[0049] Figure 6 A flowchart of the third embodiment of the method for generating a serial number of the present application is provided;
[0050] Figure 7 A schematic diagram of the count value of the allocation counter provided in Embodiment 3 of the present application;
[0051] Figure 8 This is a schematic diagram of the module structure of the serial number generating device according to an embodiment of the present application;
[0052] Fig. 9 Schematic diagram of the device structure of the hardware operating environment involved in the serial number generation method in the embodiment of the present application.
[0053] The realization of the purpose, functional features and advantages of this application will be further explained in conjunction with embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION
[0054] It should be understood that the specific embodiments described herein are only used to explain the technical solutions of the present application and are not used to limit the present application.
[0055] In order to better understand the technical solution of the present application, a detailed description will be given below in conjunction with the accompanying drawings and specific implementation methods.
[0056] It should be noted that the execution subject of this embodiment can be a computing service device with instance identification allocation, sequence allocation and unique serial number generation functions, such as a personal computer, server, etc., or an electronic device capable of realizing the above functions, a serial number generation device that executes the serial number generation method of this application, etc., and this embodiment does not limit this. The following takes the serial number generation device as an example to illustrate this embodiment and the following embodiments.
[0057] Based on this, the present application embodiment provides a method for generating a serial number, referring to Figure 1 , Figure 1 A flowchart of the first embodiment of the serial number generation method of the present application is provided.
[0058] In this embodiment, the serial number generation method includes steps S10 to S40:
[0059] Step S10: upon receiving a sequence generation request of a service, determining an instance identifier and a date offset value corresponding to the application instance, wherein the instance identifier is allocated to each application instance by a master control center of the service system.
[0060] It should be noted that the sequence generation request is a request that requires identifying a specific business in the business system to generate a specific sequence.
[0061] Among them, the application instance is the instance of each application in the business system. Generally, there are multiple instances under one application.
[0062] Since the serial number is used to identify a specific business in the business system to ensure that each individual has a unique identity. Therefore, in order to ensure the uniqueness of the sequence in a multi-application multi-instance scenario, this embodiment divides the initial composition of the serial number into four parts, see Figure 2 As shown, Figure 2 A schematic diagram of sequence splitting provided in Example 1 of the present application.
[0063] The first part is the instance identifier (instance ID for short). In the business system, each application instance will be pre-assigned a unique ID (the value of the instance ID depends on the allocation of the business system's master control center, where the master control center is the core part of the entire business system for centralized management, monitoring and coordination), so that even if the count values generated by different instances are the same, they can be clearly distinguished and the uniqueness is guaranteed by this identifier.
[0064] The second part is the date offset value, which represents the difference in days relative to a base start date. Each application instance will get a start date when it is created, and then the date offset value is calculated using the current date and the start date.
[0065] In a feasible implementation, the process of determining the date offset value of this embodiment includes: obtaining the start date of the application instance when it is created; determining the current date of the sequence generation request; and calculating the date offset value of the application instance based on the start date and the current date.
[0066] It should be noted that the start date is the base date assigned when the application instance is created. The current date is the date when a serial number needs to be generated for the business.
[0067] For example, for ease of understanding, assuming that the start date is 20240101 and the current date is 20240102, the date offset value is 20240102-20240101=1. Assuming that the length of the date offset value in the serial number is four digits, the date offset value is 0001.
[0068] In this implementation, by calculating the date offset value of the application instance, the date difference point of the serial number can be increased to further ensure the uniqueness of the sequence in a multi-instance scenario.
[0069] Step S20: performing sequence allocation through the counter pool of the application instance to obtain the counter count value.
[0070] It should be noted that the counter pool is a collection of counters, which are used to record the number of times an application instance occurs under a specific event. Each application instance includes multiple counters, and the counter pool can solve the resource competition problem when multiple application instances are running concurrently.
[0071] like Figure 2 As shown, the third part of the initial composition of the sequence number is the counter ID (i.e., counter identification). Each application instance has a counter pool, which contains multiple available counters. The counter pool initializes a certain number of counters according to the configuration. Each counter has a counter ID, which is used to identify the identity of the current counter.
[0072] The fourth part is the counter count value. Each counter is distributed in the counter pool. The counter is a two-dimensional concept. The first dimension represents the business type, and the second dimension represents the count value. Each counter has its own independent count value for each business type.
[0073] Specifically, considering that the sequence number order in each application instance must be kept correct, the increment of the counter should ensure single-threaded operation to avoid disorder. However, there are high-concurrency scenarios between application instances, so a counter pool can be used to solve the problem of resource competition during concurrency. Therefore, the sequence allocation process can be done by sequential increment or by segmentation, and this implementation does not limit this.
[0074] In this implementation, the counter pool and the independent counters can effectively disperse and process concurrent requests, thus avoiding resource contention.
[0075] Step S30: Combining an initial sequence according to the instance identifier, the date offset value, the counter identifier of the counter pool and the counter count value.
[0076] In this embodiment, if Figure 2 As shown, the instance identifier, date offset value, counter identifier of the counter pool and counter count value are arranged to obtain an initial sequence.
[0077] Furthermore, before step S10 of this embodiment, the steps may be included: obtaining the number of instances and the number of concurrent requests of the business system; performing fluctuation prediction based on the number of instances and the number of concurrent requests to determine the fluctuation range of the number of requests of the business system; when the numerical boundary of the fluctuation range of the number of requests reaches a preset threshold, determining the sequence length margin through the numerical boundary; adjusting the initial sequence length according to the sequence length margin to obtain a preset sequence length; correspondingly, the step S30 includes: combining the initial sequence of the preset sequence length according to the instance identifier, the date offset value, the counter identifier of the counter pool and the counter count value.
[0078] It should be noted that the number of instances refers to the number of application instances under the business running in the business system. For example, if a business deploys multiple identical copies on different servers or environments, the number of these copies is the number of instances. The number of concurrent requests refers to the number of requests initiated to the business system at the same time in the same time period.
[0079] It should be noted that the request quantity fluctuation range refers to the magnitude of the difference between the number of concurrent requests received by the business system and the number of concurrent requests received previously within a period of time. It represents the difference range of the increased or decreased number of concurrent requests between the maximum and minimum values (i.e., the numerical boundaries) to describe the instability and changes in the number of concurrent requests.
[0080] The preset threshold is pre-set in the sequence number generating device and is used to determine whether the fluctuation range of the request quantity exceeds the threshold of the original sequence length carrying capacity.
[0081] That is to say, as the number of concurrent requests fluctuates, the edge of the range reached by the fluctuation has been equal to or exceeded the preset threshold. At this time, the length of the sequence number needs to be adjusted.
[0082] It should be noted that the sequence length margin is the margin set to cope with the excess of the sequence when the fluctuation range of the number of requests exceeds the original sequence length carrying capacity. By reserving a certain sequence length margin, the integrity and accuracy can be ensured. Or in some designs or plans, in order to cope with possible changes or expansions, a certain sequence length margin is deliberately reserved to provide flexibility.
[0083] The preset sequence length is the sequence length obtained by adding the sequence length margin to the original sequence length, and is used for the current sequence number generation.
[0084] Specifically, first, the maximum request number fluctuation range generated under different numbers of instances and concurrent requests can be analyzed based on the historical data of the business system. Then, the maximum number that needs to be represented is determined based on the request number fluctuation range. Since an original sequence length is set, when the numerical boundary of the request number fluctuation range of the actual number of instances and concurrent requests reaches the preset threshold, the length of the sequence number can be dynamically adjusted to obtain the sequence length margin to cope with possible unexpected growth. Finally, the original sequence length is added to the sequence length margin to obtain the preset sequence length, which is used for this sequence number generation.
[0085] In this implementation, the sequence number length is dynamically adjusted by real-time monitoring of the number of instances and the amount of concurrent requests, thereby ensuring that the generation of the sequence number can adapt to different situations.
[0086] Step S40: converting the initial sequence into a unique serial number in a preset base.
[0087] It should be noted that the preset base is pre-set, and the base represented by the initial sequence of digits is converted into other bases, such as hexadecimal, hexadecimal, etc., which is not limited in this embodiment.
[0088] For ease of understanding, the hexadecimal system is used as an example for illustration, but this is not intended to limit the present solution. The process of combining the above four parts is as follows:
[0089] First, when building a sequence, use hexadecimal to reduce the number of digits. The sequence length is pre-set and can be determined based on the expected number of system instances and concurrent requests. For example, if there are 100 instances, 1,000 concurrent requests are processed at the same time, and the system is expected to run for 20 years, it is recommended to use a sequence number of at least 10 digits. The construction instructions are based on a 10-digit sequence number as an example.
[0090] First, confirm that the maximum value of the 10-digit hexadecimal system is ZZZZZZZZZZ, and convert it to decimal as 3656158440062975. The decimal range that can be expressed by the 10-digit hexadecimal system is 0-3656158440062975. If the number is simply increased and accumulated, each application instance has its own independent logic, and the uniqueness of the generated code for controlling multiple instances cannot be guaranteed. Therefore, the serial number generated in this embodiment needs to carry an instance identifier, such as Figure 2 The first part of the ID is used to ensure uniqueness.
[0091] The above decimal code needs to be further split into two parts: [application instance identifier] + [counter]. Therefore, the count value range cannot be simply set to 0-3656158440062975, but the value range must be set to 0-3649999999999999. Then the value range of the instance ID is 000-364. Assume that the fixed first three digits of the generated code are used for instance identification, for example, the application has three instances 1, 2, and 3. Then the value range of instance 1 is 0010000000000000-0019999999999999; the value range of instance 2 is 0020000000000000-002999999999999999. Next, four digits are used as the date offset value, and the range of the date offset value is 0000-9999. Assuming the starting date is 20240101, the offset value of 20240102 is 1, and the date offset value is 0001. Two more digits are taken as the counter ID, and the counter ID value is 00-99. Finally, the remaining seven digits are used for the counter count value, and the setting range is 0000000-9999999. Among them, since the sequence number order within each instance must be kept correct, this means that the increment of the counter should ensure single-threaded operation to avoid disorder. However, this will limit high concurrency scenarios. Therefore, this embodiment needs to design a counter pool to determine the counter count value to solve the problem of resource competition during concurrency.
[0092] In summary, after the above construction sequence number, refer to Figure 3 , Figure 3 The sequence value composition example diagram provided in Example 1 of the present application is divided into four parts:
[0093] 1) Instance ID (3 digits): ranges from 000 to 364, and missing digits are filled with 0. For example, if 1 is obtained, it is represented as 001.
[0094] 2) Date offset value (4 digits): 0000 to 9999, with missing digits filled with 0. For example, offset value 2 is 0002.
[0095] 3) Counter ID (2 bits): range 00 to 99. If the counter ID is 0, it will be padded with zeros to 00.
[0096] 4) Counter count value (7 bits): 0000000 to 9999999, the sequence value is 1533, padded with zeros, such as 0001533.
[0097] After combining the above parts, we get a string of decimal numbers with the value of 0010002000001533, and then convert the final decimal into hexadecimal to get the corresponding 10-digit unique code (fill in 0 on the left if there are missing digits).
[0098] In this implementation, through the above construction, the sequence generation process does not rely on database reading and writing, can effectively cope with multi-application and multi-instance environments, and ensure the uniqueness of each instance.
[0099] In the technical solution provided by this embodiment, first, upon receiving a sequence generation request from a service, the instance identifier and date offset value corresponding to the application instance are determined, and then the sequence is allocated through the counter pool of the application instance to obtain the counter count value; then, the initial sequence is combined according to the instance identifier, the date offset value, the counter identifier of the counter pool, and the counter count value; finally, the initial sequence is converted into a unique serial number in a preset base. Since this embodiment adopts the hexadecimal system, the sequence is innovatively decomposed into four parts, including the instance identifier, the date offset value, the counter ID, and the counter count value, ensuring uniqueness in a multi-application, multi-instance environment.
[0100] Based on the first embodiment of the present application, in the second embodiment of the present application, the same or similar contents as those in the above-mentioned embodiment 1 can be referred to the above introduction, and will not be repeated in the following. Figure 1 and Figure 4 , Figure 4 A flowchart of the second embodiment of the serial number generation method of the present application is provided.
[0101] Before step S10 of this example, the process of assigning the instance identifier includes:
[0102] Step S01: upon receiving a registration request for an application instance, obtaining a current sequence value.
[0103] It should be noted that the registration request is a request to the system to create an application instance when the application starts. The current sequence value is the most recent sequence value in the system.
[0104] Step S02: performing a lock sequence detection in the allocated application sequence table according to the current sequence value and a preset rule.
[0105] It should be noted that the allocated application sequence table lists application instances that have been allocated instance IDs.
[0106] The value of the instance ID in this embodiment depends on the allocation of the system's master control center. When the application is started, the master control application needs to be notified, and the master control application allocates the instance ID according to the rules. At this time, it is necessary to check the allocated application sequence table to prevent duplicate allocation of instance IDs.
[0107] Furthermore, the master control application also needs to recycle these ID values for recycling, so the application instance also needs to notify the master control application when it is offline. The application needs to interact with the master control center when it is started and offline.
[0108] Step S03: When the detection result passes, the detection sequence value corresponding to the detection result is used as the instance identifier of the application instance.
[0109] In this embodiment, when no duplicate ID is detected in the allocated application sequence table, the corresponding detection sequence value can be used as the instance identifier of the application instance. Through the above generation process, it is possible to support sequence generation of non-database applications without relying on middleware such as databases.
[0110] Furthermore, step S02 of this example includes the steps of: adding one to the current sequence value to obtain a detection sequence value; based on the detection sequence value, detecting whether a used sequence value exists in the allocated application sequence table of the database; if the used sequence value exists in the allocated application sequence table, returning to execute the step of adding one to the current sequence value to obtain a detection sequence value until the used sequence value does not exist in the allocated application sequence table, and saving the detection sequence value corresponding to the non-existence of the used sequence value to the database.
[0111] It should be noted that the detection sequence value is a sequence value used to detect whether there is a duplicate ID in the allocated application sequence table.
[0112] For easier understanding, refer to Figure 5 , Figure 5 A flow chart of the master control center allocating instance IDs provided in the second embodiment of the present application. When a registration request is received, the sequence number is not directly increased by 1, but a record detection of the lock sequence number is required in the allocated application sequence table in the database. Therefore, during the detection process, the current sequence number is increased by one to obtain the detection sequence value; then the allocated application sequence table is compared to see if there is the same ID; if so, it returns and increases by one again. If not, the detection sequence value corresponding to the sequence value that does not exist is stored in the database.
[0113] The above-mentioned record detection of the lock sequence number has the following advantages:
[0114] 1) Recycling of sequence values: To efficiently utilize sequence number resources, used sequence values can be allocated again after the instance is restarted or the ID is reallocated.
[0115] 2) Dynamics of instance restart: In a production environment, instances may be restarted frequently, which raises two issues: 1) The instance ID is not strongly associated with the offline application instance. Therefore, there is a probability that it will not be automatically released when the instance is shut down, resulting in some sequence values being marked as allocated but actually associated with the stopped instance, forming a "zombie" sequence number. Therefore, it is necessary to perform maintenance operations regularly to detect and recycle these sequence numbers that are no longer bound to active instances to prevent resource waste and maintain system consistency. Such a mechanism ensures the rationality of sequence number allocation and the stability of the system. In addition, for the ID released on the same day, it can be marked as frozen, no reallocation is performed on the same day, and it is thawed the next day. If the ID has allocated resources reaching 80% on the same day, the early warning mechanism can be activated and manual intervention can be carried out.
[0116] In the technical solution provided by this embodiment, by orderly allocating and recycling instance IDs, it can be ensured that the sequence codes of different instances do not conflict with each other. The regular cleaning mechanism of abnormal instances ensures the uniqueness of the sequence and the accuracy of the system data.
[0117] Based on the above embodiments of the present application, in the third embodiment of the present application, the same or similar contents as those in the above embodiments can be referred to the above introduction, and will not be described in detail later. Figure 1 and Figure 6 , Figure 6 A flowchart of the third embodiment of the serial number generation method of the present application is provided.
[0118] In this example, step S20 includes steps S21 to S23:
[0119] Step S21: generating a counting instruction of the counter pool of the application instance according to the sequence generation request.
[0120] It should be noted that the counting instruction is an instruction for counting the counter pool of the application instance.
[0121] Step S22: traverse the counter pool according to the counting instruction to determine available counters.
[0122] Step S23: Sequentially assign the available counters in ascending order to obtain counter count values.
[0123] For ease of understanding, refer to Figure 7 , Figure 7A schematic diagram of allocating counter count values provided for Example 3 of the present application. A counter pool is established within the application, and the counter pool initializes a certain number of counters according to the configuration. A counter entity has two key member variables, the counter ID and the current sequence value sequence (as shown in the figure, the first counter, id is 0, the current sequence value sequence is 0; the second counter, id is 2, the current sequence value sequence is 2; the third counter, id is 3, the current sequence value sequence is 3). Among them, the counter ID is used to identify the identity of the current counter, and the sequence of each counter is independent.
[0124] When a count request to obtain a sequence number is received, the counter pool is traversed to find an available (unoccupied or idle) counter. Then a single counter logic operation is performed on the selected counter (as shown in the figure, lock, SEQUNCE plus 1, unlock; this design ensures that each counter has its own unique identity and can process count requests independently while avoiding concurrent conflicts), and the new sequence value (i.e., counter count value) is obtained by incrementing the value of the counter.
[0125] In the technical solution provided in this embodiment, through the counter pool and the independent counter, concurrent requests can be effectively dispersed and processed, resource competition is avoided, and the efficiency and correctness of sequence generation in a high-concurrency environment are ensured.
[0126] It should be noted that the above examples are only used to understand the present application and do not constitute a limitation on the serial number generation method of the present application. More simple transformations based on this technical concept are all within the scope of protection of the present application.
[0127] This application also provides a serial number generation device, please refer to Figure 8 , Figure 8 This is a schematic diagram of the module structure of the serial number generating device according to the embodiment of the present application; the serial number generating device comprises:
[0128] The application instance module 801 is used to determine the instance identifier and date offset value corresponding to the application instance when receiving a sequence generation request of the business. The instance identifier is allocated to each application instance by the main control center of the business system;
[0129] The counter module 802 is used to perform sequence allocation through the counter pool of the application instance to obtain a counter count value;
[0130] An initial sequence module 803, configured to combine an initial sequence according to the instance identifier, the date offset value, the counter identifier of the counter pool and the counter count value;
[0131] The base conversion module 804 is used to convert the initial sequence into a unique sequence number of a preset base.
[0132] In this embodiment, the serial number generating device also includes an identification allocation module, which is used to obtain a current sequence value when a registration request for an application instance is received; perform a lock sequence detection in an allocated application sequence table according to preset rules based on the current sequence value; when the detection result passes, use the corresponding detection sequence value in the detection result as the instance identification of the application instance.
[0133] Furthermore, the identification allocation module is also used to add one to the current sequence value to obtain a detection sequence value; based on the detection sequence value, detect whether there is a used sequence value in the allocated application sequence table of the database; if the used sequence value exists in the allocated application sequence table, return to execute the step of adding one to the current sequence value to obtain the detection sequence value until the used sequence value does not exist in the allocated application sequence table, and save the detection sequence value corresponding to the non-existence of the used sequence value to the database.
[0134] Furthermore, the application instance module 801 is also used to obtain the start date of the application instance when it is created; determine the current date of the sequence generation request; and calculate the date offset value of the application instance based on the start date and the current date.
[0135] Furthermore, the counter module 802 is also used to generate a counting instruction for the counter pool of the application instance according to the sequence generation request; traverse the counter pool according to the counting instruction to determine the available counters; and sequence the available counters in ascending order to obtain the counter count value.
[0136] Furthermore, the sequence number generating device further includes a sequence length preset module, which is used to obtain the number of instances and the number of concurrent requests of the business system; perform fluctuation prediction based on the number of instances and the number of concurrent requests to determine the fluctuation range of the number of requests of the business system; when the numerical boundary of the fluctuation range of the number of requests reaches a preset threshold, determine the sequence length margin through the numerical boundary; adjust the initial sequence length according to the sequence length margin to obtain a preset sequence length;
[0137] Correspondingly, the initial sequence module 803 is further used to combine the initial sequence of the preset sequence length according to the instance identifier, the date offset value, the counter identifier of the counter pool and the counter count value.
[0138] Other embodiments or specific implementations of the serial number generating device of the present application can refer to the above-mentioned method embodiments and will not be described in detail here.
[0139] The serial number generating device provided by the present application adopts the serial number generating method in the above-mentioned embodiment, which can solve the technical problem that the number of bits of the final generated sequence of the traditional sequence generating scheme is long, duplicate IDs may be generated, and it is difficult to ensure the uniqueness of the sequence. Compared with the prior art, the beneficial effects of the serial number generating device provided by the present application are the same as the beneficial effects of the serial number generating method provided by the above-mentioned embodiment, and the other technical features in the serial number generating device are the same as the features disclosed in the above-mentioned embodiment method, which will not be repeated here.
[0140] The present application provides a serial number generating device, which includes: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the serial number generating method in the above-mentioned embodiment 1.
[0141] Reference below Fig. 9 , Fig. 9 The device structure diagram of the hardware operating environment involved in the serial number generation method in the embodiment of the present application shows a schematic diagram of the structure of the serial number generation device suitable for implementing the embodiment of the present application. The serial number generation device in the embodiment of the present application may include but is not limited to mobile terminals such as mobile phones, laptops, digital broadcast receivers, PDAs (Personal Digital Assistants), PADs (Portable Application Descriptions), PMPs (Portable Media Players), vehicle-mounted terminals (such as vehicle-mounted navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Fig. 9 The serial number generating device shown is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
[0142] like Fig. 9As shown, the serial number generating device may include a processing device 1001 (e.g., a central processing unit, a graphics processor, etc.), which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM: Read Only Memory) 1002 or a program loaded from a storage device 1003 to a random access memory (RAM: Random Access Memory) 1004. In RAM1004, various programs and data required for the operation of the serial number generating device are also stored. The processing device 1001, ROM1002, and RAM1004 are connected to each other through a bus 1005. An input / output (I / O) interface 1006 is also connected to the bus. Generally, the following systems can be connected to the I / O interface 1006: an input device 1007 including, for example, a touch screen, a touch pad, a keyboard, a mouse, an image sensor, a microphone, an accelerometer, a gyroscope, etc.; an output device 1008 including, for example, a liquid crystal display (LCD: Liquid Crystal Display), a speaker, a vibrator, etc.; a storage device 1003 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 1009. The communication device 1009 can allow the serial number generating device to communicate with other devices wirelessly or by wire to exchange data. Although the serial number generating device with various systems is shown in the figure, it should be understood that it is not required to implement or have all the systems shown. More or fewer systems can be implemented or have alternatively.
[0143] In particular, according to the embodiments disclosed in the present application, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present application include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication device, or installed from a storage device 1003, or installed from a ROM 1002. When the computer program is executed by the processing device 1001, the above-mentioned functions defined in the method of the embodiment disclosed in the present application are executed.
[0144] The serial number generating device provided by the present application adopts the serial number generating method in the above embodiment, which can solve the technical problem that the number of bits of the final generated sequence of the traditional sequence generating scheme is long, duplicate IDs may be generated, and it is difficult to ensure the uniqueness of the sequence. Compared with the prior art, the beneficial effects of the serial number generating device provided by the present application are the same as the beneficial effects of the serial number generating method provided by the above embodiment, and the other technical features in the serial number generating device are the same as the features disclosed in the method of the previous embodiment, which will not be repeated here.
[0145] It should be understood that the various parts disclosed in this application can be implemented by hardware, software, firmware or a combination thereof. In the description of the above embodiments, specific features, structures, materials or characteristics can be combined in any one or more embodiments or examples in a suitable manner.
[0146] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
[0147] The present application provides a computer-readable storage medium having computer-readable program instructions (ie, computer programs) stored thereon, and the computer-readable program instructions are used to execute the serial number generation method in the above-mentioned embodiment.
[0148] The computer-readable storage medium provided in the present application may be, for example, a USB flash drive, but is not limited to electrical, magnetic, optical, electromagnetic, infrared or semiconductor systems, systems or devices, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In this embodiment, the computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, system or device. The program code contained on the computer-readable storage medium may be transmitted using any appropriate medium, including but not limited to: wires, optical cables, RF (Radio Frequency), etc., or any suitable combination of the above.
[0149] The computer-readable storage medium may be included in the serial number generating device; or may exist independently without being assembled into the serial number generating device.
[0150] The above-mentioned computer-readable storage medium carries one or more programs. When the above-mentioned one or more programs are executed by the serial number generating device, the serial number generating device: when receiving a sequence generation request of a business, determines the instance identifier and date offset value corresponding to the application instance, and the instance identifier is allocated to each application instance by the main control center of the business system; performs sequence allocation through the counter pool of the application instance to obtain the counter count value; combines an initial sequence according to the instance identifier, the date offset value, the counter identifier of the counter pool and the counter count value; and converts the initial sequence into a unique serial number in a preset base.
[0151] Computer program code for performing the operations of the present application may be written in one or more programming languages or a combination thereof, including object-oriented programming languages such as Java, Smalltalk, C++, and conventional procedural programming languages such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., via the Internet using an Internet service provider).
[0152] The flowchart and block diagram in the accompanying drawings illustrate the possible architecture, functions and operations of the system, method and computer program product according to various embodiments of the present application. In this regard, each box in the flowchart or block diagram can represent a module, a program segment or a part of a code, and the module, a program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram and / or flowchart and the combination of boxes in the block diagram and / or flowchart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0153] The modules involved in the embodiments described in the present application may be implemented by software or hardware, wherein the name of the module does not constitute a limitation on the unit itself in some cases.
[0154] The readable storage medium provided by the present application is a computer-readable storage medium, which stores computer-readable program instructions (i.e., computer programs) for executing the above-mentioned method for generating a sequence number, and can solve the technical problem that the number of bits of the sequence generated by the traditional sequence generation scheme is relatively long, duplicate IDs may be generated, and it is difficult to ensure the uniqueness of the sequence. Compared with the prior art, the beneficial effects of the computer-readable storage medium provided by the present application are the same as those of the method for generating a sequence number provided by the above-mentioned embodiment, and will not be described in detail here.
[0155] The present application also provides a computer program product, including a computer program, which implements the steps of the serial number generation method as described above when executed by a processor.
[0156] The computer program product provided by the present application can solve the technical problem that the number of bits of the sequence generated by the traditional sequence generation scheme is relatively long, duplicate IDs may be generated, and it is difficult to ensure the uniqueness of the sequence. Compared with the prior art, the beneficial effects of the computer program product provided by the present application are the same as those of the sequence number generation method provided by the above embodiment, and will not be repeated here.
[0157] The above descriptions are only some embodiments of the present application, and are not intended to limit the patent scope of the present application. All equivalent structural changes made using the contents of the present application specification and drawings under the technical concept of the present application, or direct / indirect applications in other related technical fields are included in the patent protection scope of the present application.
Claims
1. A method for generating a sequence number, characterized in that: The method includes: When receiving a sequence generation request for a business, determining an instance identifier and a date offset value corresponding to the application instance, wherein the instance identifier is allocated to each application instance by a master control center of the business system; Perform sequence allocation through the counter pool of the application instance to obtain a counter count value; Combining an initial sequence according to the instance identifier, the date offset value, the counter identifier of the counter pool and the counter count value; The initial sequence is converted into a unique sequence number in a preset base.
2. The method according to claim 1, characterized in that Before the step of determining the instance identifier and date offset value corresponding to the application instance when receiving the sequence generation request of the service, the instance identifier allocation process includes: When receiving a registration request from an application instance, obtain the current sequence value; Performing a lock sequence detection in the allocated application sequence table according to the current sequence value and a preset rule; When the detection result passes, the detection sequence value corresponding to the detection result is used as the instance identifier of the application instance.
3. The method according to claim 2, characterized in that The step of performing a lock sequence detection in the allocated application sequence table according to the current sequence value and a preset rule comprises: Add one to the current sequence value to obtain a detection sequence value; According to the detection sequence value, detecting whether there is a used sequence value in the allocated application sequence table of the database; If the used sequence value exists in the allocated application sequence table, return to execute the step of adding one to the current sequence value to obtain the detection sequence value until the used sequence value does not exist in the allocated application sequence table, and save the detection sequence value corresponding to the non-existence of the used sequence value to the database.
4. The method according to claim 1, characterized in that The process of determining the date offset value includes: Obtain the start date of the application instance when it is created; determining a current date of the sequence generation request; A date offset value of the application instance is calculated according to the start date and the current date.
5. The method according to any one of claims 1 to 4, characterized in that The step of performing sequence allocation through the counter pool of the application instance to obtain the counter count value includes: Generate a counting instruction of a counter pool of the application instance according to the sequence generation request; Traversing the counter pool according to the counting instruction to determine available counters; The available counters are sequentially allocated in ascending order to obtain counter count values.
6. The method according to any one of claims 1 to 4, characterized in that Before the step of determining the instance identifier and date offset value corresponding to the application instance when receiving the sequence generation request of the service, the method further includes: Get the number of instances and concurrent requests of the business system; Perform fluctuation prediction based on the number of instances and the number of concurrent requests to determine the fluctuation range of the number of requests of the business system; When the numerical boundary of the request quantity fluctuation range reaches a preset threshold, determining the sequence length margin by the numerical boundary; Adjusting the initial sequence length according to the sequence length margin to obtain a preset sequence length; Accordingly, the step of combining the instance identifier, the date offset value, the counter identifier of the counter pool and the counter count value into an initial sequence includes: An initial sequence of the preset sequence length is combined according to the instance identifier, the date offset value, the counter identifier of the counter pool, and the counter count value.
7. A serial number generating device, characterized in that: The device comprises: An application instance module, used to determine an instance identifier and a date offset value corresponding to an application instance when receiving a sequence generation request of a business, wherein the instance identifier is allocated to each application instance by a master control center of the business system; A counter module, used for performing sequence allocation through a counter pool of the application instance to obtain a counter count value; An initial sequence module, used for combining an initial sequence according to the instance identifier, the date offset value, the counter identifier of the counter pool and the counter count value; The base conversion module is used to convert the initial sequence into a unique serial number of a preset base.
8. A serial number generating device, characterized in that: The device comprises: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program is configured to implement the steps of the serial number generating method according to any one of claims 1 to 6.
9. A storage medium, characterized in that: The storage medium is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by a processor, the steps of the serial number generation method according to any one of claims 1 to 6 are implemented.
10. A computer program product, characterized in that The computer program product comprises a computer program, and when the computer program is executed by a processor, the steps of the serial number generation method according to any one of claims 1 to 6 are implemented.
Citation Information
Cited By
Time sequence counter control method and circuit, electronic equipment and storage medium
CN120766733A