Distributed serial number generation method and apparatus
By setting personalized character ranges and dynamically expanding number pool generation methods in a distributed system, the problem of uncontrollable serial number generation is solved, realizing personalized customization of serial numbers and improved availability in high-concurrency scenarios.
Patent Information
- Application Number
- CN202110866837.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-07-29
- Publication Date
- 2025-12-23
- Estimated Expiration
- 2041-07-29
AI Technical Summary
Existing distributed sequence number generation methods suffer from problems in distributed systems, such as uncontrollable and uncustomizable sequence number length, high probability of duplication, uncustomizable characters, and low system availability in high-concurrency scenarios.
By customizing the character range, dynamically changing the serial number length, generating random serial numbers using a number pool, and dynamically expanding capacity in high-concurrency scenarios, personalized customization needs can be supported, and number utilization can be improved.
It achieves controllable and unpredictable serial number length, supports personalized customization, and improves number utilization and system availability in high-concurrency scenarios.
Smart Images

Figure CN113704278B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of data processing, and in particular to a distributed sequence number generation method and device. BACKGROUND
[0002] In a distributed system, a global unique identifier is often used to define different types of data, such as the unique identifier of an order, the unique identifier of a message, and the unique identifier of a data link. The unique identifier is convenient for indexing, occupies a relatively small position, and is easy to store. When generating a globally unique sequence number in a distributed scenario, the following requirements are usually met: the sequence number cannot be too long, such as an order number that cannot exceed 6 digits; the sequence number generation rule cannot be predictable, such as an order number that cannot expose the current order total; the limited number resources cannot be wasted, such as the situation where the number length is upgraded when there are unused numbers; the number characters support personalized customization, such as the situation where the number cannot contain 4; the sequence number cannot be repeated, such as the situation where the order number cannot be repeated; the high-concurrency scenario is supported, such as the situation where the order number generation cannot be blocked during a flash sale.
[0003] The existing distributed sequence number generation methods mainly include UUID, self-incrementing sequence, snowflake algorithm, timestamp, and random number. However, the existing methods for generating sequence numbers are not ideal. The UUID generated sequence number is too long, theoretically has the possibility of repetition, and the characters of the sequence number cannot be customized; the self-incrementing sequence is generated by incrementing, the rule is predictable, and in some scenarios where the number generation amount needs to be hidden, the business secrets are exposed, and the characters of the sequence number cannot be customized; the sequence number generated by the snowflake algorithm is too long, which is prone to produce repeated numbers, and the algorithm has a timestamp interval, which causes the number to be discontinuous, the number utilization rate to be low, and the like; the timestamp is prone to produce repeated numbers in a high-concurrency scenario, which reduces the system availability, the generated number is discontinuous, the number utilization rate is low, and the characters cannot be customized; the reliability of the random number is low, and the random number is prone to produce repeated numbers, which increases the system complexity when the de-duplication logic is written, and the characters cannot be customized. SUMMARY
[0004] The present application provides a distributed sequence number generation method and device to solve the defect that the length of the sequence number in the prior art is uncontrollable and cannot be customized in a distributed scenario, and by individually setting the character range, the length of the sequence number is dynamically changed to meet the personalized customization demand and improve the number utilization rate.
[0005] The application provides a distributed serial number generation method, comprising: judging whether the current capacity of a cache queue is greater than a preset threshold according to a received acquisition request; based on the current capacity of the cache queue being greater than the preset threshold, acquiring available serial numbers from the cache queue; otherwise, determining that a number pool is not exhausted according to a pre-acquired number pool state, extracting serial numbers randomly generated based on a preset number length from the number pool to the cache queue to update the cache queue.
[0006] According to the distributed serial number generation method provided by the application, before the step of extracting serial numbers randomly generated based on a preset number length from the number pool to the cache queue, the method comprises: based on the preset number length, the number pool randomly generates serial numbers based on a preset cache update increment; and updating the count of the corresponding first character in the number pool according to the first character of the generated serial number.
[0007] According to the distributed serial number generation method provided by the application, the number pool randomly generates serial numbers based on a preset cache update increment, comprising: randomly selecting a first character of a serial number; determining a starting coordinate based on the count corresponding to the first character; and traversing the number pool according to a preset traversal rule and the starting coordinate to obtain the remaining characters and form a random serial number.
[0008] According to the distributed serial number generation method provided by the application, the step of determining a starting coordinate based on the count corresponding to the first character comprises: based on the count corresponding to the first character, determining the used number of permutation combinations of the remaining digits of the serial number corresponding to the first character; determining the row coordinate of the last character as the starting coordinate of the last character according to the used number of permutation combinations; and sequentially selecting intermediate characters from the last character to the first character, and determining the row coordinate of the currently selected intermediate character as the starting coordinate of the currently selected intermediate character based on the used number of permutation combinations and the row coordinates of all previously determined characters.
[0009] According to the distributed serial number generation method provided by the application, before the step of the number pool randomly generating serial numbers based on a preset cache update increment, the method further comprises: setting a number length; setting a character range based on a demand; and randomly generating a two-dimensional group of the number pool according to the number length and the character range.
[0010] According to the distributed serial number generation method provided by the application, the step of extracting serial numbers randomly generated based on a preset number length from the number pool to the cache queue comprises: after the number pool randomly generates serial numbers based on a preset cache update increment, acquiring the randomly generated serial numbers based on the preset cache update increment and adding the serial numbers to the cache queue.
[0011] According to the distributed serial number generation method provided by the application, the method further comprises: determining that the number pool is exhausted according to a pre-acquired number pool state, triggering expansion of the number pool, increasing the serial number length by one bit to form a new number length; initializing the number pool based on the new number length, and clearing the count of the number pool; and extracting serial numbers from the expanded number pool to the cache queue to update the cache queue and update the count of the corresponding serial numbers in the expanded number pool.
[0012] The application further provides a distributed serial number generation device, comprising: a judgment module configured to judge, according to a received acquisition request, whether the current capacity of the cache queue is greater than a preset threshold value;
[0013] An extraction module is configured to acquire available serial numbers from the cache queue based on the current capacity of the cache queue being greater than the preset threshold value; and an update module is configured to extract serial numbers randomly generated based on a preset number length from the number pool to the cache queue to update the cache queue based on the current capacity of the cache queue being greater than the preset threshold value and the number pool not being exhausted according to a pre-acquired number pool state.
[0014] The application further provides an electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the distributed serial number generation method according to any one of the above when executing the program.
[0015] The application further provides a non-transitory computer-readable storage medium having a computer program stored thereon, wherein the computer program is executable on a processor to implement the steps of the distributed serial number generation method according to any one of the above.
[0016] The distributed serial number generation method and device provided by the application generate random serial numbers by using a number pool, limit the number length, make the generated serial numbers unpredictable, support individualized serial number customization, improve the number utilization rate, and support dynamic expansion; the preset number length is used to limit the length; and the serial numbers are extracted from the number pool to the cache queue, so that the cache increment and the threshold value can be adjusted at any time, the availability in a concurrent scenario is further improved, the number generation performance is flexibly adjusted, and the distributed high-concurrent scenario is supported. BRIEF DESCRIPTION OF DRAWINGS
[0017] In order to more clearly illustrate the technical solutions in the application or the prior art, the following will briefly introduce the drawings needed to be used in the embodiments or the prior art description. Obviously, the drawings in the following description are some embodiments of the application, and those skilled in the art can also obtain other drawings according to these drawings without any creative effort.
[0018] Figure 1 This is one of the flowcharts of the distributed sequence number generation method provided by the present invention;
[0019] Figure 2 This is the second flowchart of the distributed sequence number generation method provided by the present invention;
[0020] Figure 3 This is a schematic diagram of the initialization of the number pool provided by the present invention;
[0021] Figures 4-6 This is a schematic diagram of the update cache queue provided by the present invention;
[0022] Figure 7 This is the third flowchart of the distributed sequence number generation method provided by the present invention;
[0023] Figure 8 This is a schematic diagram of the structure of the distributed serial number generation device provided by the present invention;
[0024] Figure 9 This is a schematic diagram of the structure of the electronic device provided by the present invention. Detailed Implementation
[0025] To make the objectives, technical solutions, and advantages of this invention clearer, the technical solutions of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of this invention. All other embodiments obtained by those skilled in the art based on the embodiments of this invention without creative effort are within the scope of protection of this invention.
[0026] Figures 1-2 The following are flowcharts illustrating a distributed sequence number generation method. (Refer to...) Figures 1-2 The method includes:
[0027] S01, based on the received retrieval request, determine whether the current capacity of the cache queue is greater than the preset threshold;
[0028] S02, based on the fact that the current capacity of the cache queue is greater than a preset threshold, obtain an available sequence number from the cache queue;
[0029] S03, otherwise, based on the pre-obtained number pool status, if it is determined that the number pool is not exhausted, extract the serial number randomly generated based on the preset number length from the number pool and add it to the cache queue to update the cache queue.
[0030] It should be noted that the execution subject of the distributed serial number generation method provided in the present application is an application system, which can be a computer device, such as a mobile phone, a tablet computer, a notebook computer, a palm computer, a vehicle-mounted electronic device, a wearable device, an ultra-mobile personal computer (UMPC), a netbook, or a personal digital assistant (PDA), etc.
[0031] The S0N in the specification does not represent the sequence of the distributed serial number generation method, and the following will be specifically combined with the Figures 2-7 The distributed serial number generation method of the present application is described.
[0032] In step S01, it is judged whether the current capacity of the cache queue is greater than the preset threshold according to the received acquisition request.
[0033] In the present embodiment, the cache queue is initialized before the acquisition request is received, specifically including: setting the cache update increment and the threshold, and creating the cache queue based on the data structure server redis. In addition, after the cache queue is initialized, the serial numbers based on the cache update increment are extracted from the initialized number pool to the cache queue in advance, so that the current capacity of the cache queue is greater than the preset threshold. After the acquisition request is accepted, it is judged whether the current capacity of the current cache queue is greater than the preset threshold, to judge whether the cache list needs to be updated, that is, whether the serial numbers satisfying the cache update increment need to be extracted from the number pool to the cache list, so that the number of serial numbers in the cache queue at least meets the preset threshold. It should be noted that the preset threshold can be greater than half of the cache update increment, and less than the cache update increment, which can be determined according to actual needs. For example, if the cache update increment of the cache queue is 10, the preset threshold can be 6, 7, 8 or 9.
[0034] In step S02, the serial numbers are acquired from the cache queue based on the current capacity of the cache queue being greater than the preset threshold.
[0035] It should be noted that when the current capacity of the cache queue is greater than the preset threshold, it indicates that the serial numbers in the current cache queue are sufficient, and the application system can acquire the corresponding serial numbers from the cache queue according to the acquisition request.
[0036] In step S03, otherwise, it is determined according to the pre-acquired number pool state that the number pool is not exhausted, and the serial numbers generated randomly based on the preset number length are extracted from the number pool to the cache queue to update the cache queue.
[0037] In an optional embodiment, before extracting the serial number randomly generated based on the preset number length from the number pool to the cache queue, the method comprises: based on the preset number length, the number pool randomly generates serial numbers based on the preset cache update increment; and updating the count of the corresponding first character in the number pool according to the first character of the generated serial number.
[0038] First, based on the preset number length, the number pool randomly generates serial numbers based on the preset cache update increment. It should be noted that the preset number length can be set according to actual use requirements, such as a number length of 6, i.e. the serial number formed by 6 digits. By specifying the number length, the purpose of limiting the length is achieved.
[0039] In addition, the number pool randomly generates serial numbers based on the preset cache update increment, which comprises: randomly selecting the first character of the serial number; determining the starting coordinate based on the count corresponding to the first character; and traversing the number pool according to the preset traversal rule and the starting coordinate to obtain the remaining characters to form a random serial number. It should be noted that the number of serial numbers formed satisfies the preset cache update increment. In addition, the traversal rule can be a traversal order from left to right and from top to bottom. By sequentially traversing the number pool to generate serial numbers, the utilization rate of the number pool is improved. It should be noted that if there is a serial number in the number pool that meets the preset cache increment, the current number pool state is that the number pool is not exhausted.
[0040] In an optional embodiment, before randomly generating serial numbers, the number pool is initialized, which comprises: setting the number length, such as 6; setting the character range based on the requirements, such as 0-9; generating a two-dimensional group of the number pool according to the number length and the character range; wherein the first column is a counter column, the counter count value is initialized to 0, and the second column to the seventh column is randomly filled with characters in the character range. By shuffling the character order of the number pool, the unpredictability of the generation rule is achieved, and the array content is stored in the database to complete the initialization of the number pool.
[0041] It should be noted that the database can use a MySQL database. By customizing the character range, personalized number generation requirements can be supported, such as requiring the number to not contain 4, which can set the available characters to not contain 4. If the number length is not limited, combined characters can be used as metacharacters to participate in number generation, such as “666”, “888”, “lucky”, “award”, etc.
[0042] In addition, the starting coordinate is determined based on the count corresponding to the first character, including: determining, based on the count corresponding to the first character, a used quantity of arrangement combinations of the sequence number and the remaining digits; determining, according to the used quantity of arrangement combinations, a row coordinate of the last character, and taking the row coordinate as the starting coordinate of the last character; selecting the intermediate characters in turn from the last character to the first character, and determining, based on the used quantity of arrangement combinations and the row coordinates of all the characters determined in advance, a row coordinate of the currently selected intermediate character, and taking the row coordinate as the starting coordinate of the currently selected intermediate character.
[0043] The starting coordinate is represented as:
[0044]
[0045] wherein j_i represents the row coordinate j of the i-th column of the number pool; counter[i] represents the used quantity of arrangement combinations of the sequence number and the remaining digits when the first character is i; l represents the length of the character range available for each digit of the sequence number; d represents the number length; k represents the i+1-th column of the number pool; and j_k represents the row coordinate of the i+1-th column of the number pool.
[0046] Secondly, the count corresponding to the first character in the number pool is updated according to the first character of the generated sequence number. It should be noted that when updating the count, the number of extracted sequence numbers is added to the count of the corresponding first character, that is, the cache update increment is added to the count of the current first character. In addition, a counter is used for counting.
[0047] In an optional embodiment, the method comprises:
[0048] The number pool is initialized, the number length is set to 6, the character range is set to 0-9, and the counter is initialized to 0, as shown in Figure 3 .
[0049] The cache queue is initialized, and the cache update increment of the cache queue is set to 10 and the threshold is set to 6.
[0050] In this embodiment, when the current capacity of the cache queue is not greater than the preset threshold, the cache queue is triggered to update the sequence number, specifically including: judging whether the sequence number in the number pool is exhausted according to the number pool state obtained in advance, and if not, extracting the sequence number generated based on the preset number length from the number pool to the cache queue. It should be noted that the number of extracted sequence numbers needs to meet the preset cache update increment.
[0051] In an optional embodiment, the sequence number randomly generated based on the preset number length is extracted from the number pool to the cache queue, including: after the number pool randomly generates the sequence number based on the preset cache update increment, the randomly generated sequence number is obtained based on the preset cache update increment, and is added to the cache queue. It should be noted that when the sequence number is added to the cache queue, the sequence number is sequentially added to the cache queue in the enqueue direction of the cache queue; when the sequence number is extracted from the cache queue, the sequence number in the cache queue is sequentially obtained in the dequeue direction of the cache queue.
[0052] In an optional embodiment, referring to Figures 4-6 , the number pool is determined to be not exhausted according to the pre-acquired number pool state, the sequence number randomly generated based on the preset number length is extracted from the number pool to the cache queue to update the cache queue, including:
[0053] Referring to Figure 4 , the first character row number j_0 of the sequence number is randomly selected as random(10), assuming that the row coordinate j=4, the first column of the selected first character is 6, digit_0=6, j_5=(counter[i])%10=0 is calculated according to the above starting coordinate calculation formula; j_4=(counter[i]-j_5)%10 2 =0; j_3=(counter[i]-j_4*10-j_5)%10 3 =0; j_2=(counter[i]-j_3*10 2 -j_4*10-j_5)%10 4 =0; j_1=(counter[i]-j_2*10 3 j_3*10 2 -j_4*10-j_5)%10 5 =0, wherein i represents the column index of the sequence number, j represents the row index of the sequence number, and digit_i represents the value of the ith column.
[0054] Referring to Figure 5 , after 10 numbers of the number pool are traversed according to the above coordinates, 65855 6 , 65855 1 , 65855 3 , 65855 0 , 65855 2 , 65855 4 , 65855 7 , 65855 8 , 65855 5 , 65855 9Ten serial numbers are extracted into the initialized cache queue based on the cache update increment, and the counter corresponding to the count of the first character 6 of the number pool is changed to 10.
[0055] After the application system extracts 4 serial numbers from the cache queue based on the received acquisition request, the current capacity of the cache queue is 6, which is not greater than the preset threshold 6. Assuming that the number pool is not exhausted, the corresponding serial numbers are extracted from the number pool for updating.
[0056] Reference Figure 6 When updating the cache queue for the second time, assuming that the row coordinate selected by the first character (digit_0) is still 4, j = 4, j_1 = 0, j_2 = 0, j_3 = 0, j_4 = 1, j_5 = 0 are calculated according to the starting coordinate calculation formula, and the count corresponding to the first character 6 of the counter is updated to 20. The newly added available serial numbers in the cache queue are: 6585 3 6、6585 3 1、6585 3 3、6585 3 0、6585 3 2、6585 3 4、6585 3 7、6585 3 8、6585 3 5、6585 3 9。
[0057] At this time, the remaining serial numbers in the cache queue are 6585 2 、6585 4 、6585 7 、6585 8 、6585 5 、6585 9 、6585 3 6、6585 3 1、6585 3 3、6585 3 0、6585 3 2、6585 3 4、6585 3 7、6585 3 8、6585 3 5、6585 3 9。 When the current capacity of the cache queue is less than the preset threshold next time, i.e., based on each acquisition request, 6585 2 、6585 4 、6585 7 、6585 8 、6585 5,65855 9 ,6585 3 6,6585 3 1,6585 3 3,6585 3 0 after, so that the remaining 6585 3 2,6585 3 4,6585 3 7,6585 3 8,6585 3 5,6585 3 9 six serial numbers, the third update cache queue. The third update cache queue can refer to the second update cache queue described above, which is not repeated here.
[0058] In an optional embodiment, the distributed serial number generation method further comprises: determining that the number pool is exhausted according to the pre-acquired number pool state, triggering the number pool expansion, increasing the serial number length by one bit to form a new number length; initializing the number pool based on the new number length and clearing the count of the number pool; extracting serial numbers from the expanded number pool to the cache queue to update the cache queue and update the count of the corresponding serial numbers in the expanded number pool. It should be noted that the way of extracting the corresponding serial numbers from the expanded number pool to the cache queue can refer to the description above, which is not repeated here. It should be noted that the upper limit value of the permutation combination number of the remaining characters corresponding to the first character of each serial number is pre-set, and when the count corresponding to the first character of each serial number meets the corresponding upper limit value, the number pool is exhausted, and the number pool needs to be expanded first, and then the serial numbers formed by the expanded number pool are extracted to the cache queue.
[0059] In an optional embodiment, referring to Figure 7 , the distributed serial number generation method comprises:
[0060] S11, initializing the number pool in the database;
[0061] S12, initializing the cache queue in the data structure server redis;
[0062] S13, the application system determines whether the current capacity of the cache queue is greater than the pre-set threshold value according to the received acquisition request; if not, the serial number is acquired from the cache queue; otherwise, the serial number is extracted from the number pool to update the cache queue;
[0063] Wherein, when the application system extracts the serial number from the number pool to update the cache queue, it comprises:
[0064] determining whether the number pool is exhausted according to a pre-acquired number pool state; if not, extracting a preset cache update increment of serial numbers from the number pool into a cache queue to successfully update the cache queue; and based on successfully updating the cache queue, acquiring the serial numbers from the updated cache queue according to a received acquisition request; otherwise, triggering number pool expansion to increase the length of the serial numbers by one bit; initializing the number pool and clearing the count of the number pool; extracting a preset cache increment of serial numbers from the expanded number pool into the cache queue to update the cache queue and update the count of the corresponding serial numbers in the expanded number pool.
[0065] In summary, the embodiment of the application generates random serial numbers by using a number pool, limits the length of the numbers, makes the generated serial numbers unpredictable, supports personalized serial number customization, improves the utilization rate of numbers, and can be dynamically expanded; by presetting the length of the numbers, the length is limited; by extracting serial numbers from the number pool into the cache queue, the cache increment and threshold can be adjusted at any time, the availability in the concurrent scenario is further improved, the number generation performance is flexibly adjusted, and the distributed high-concurrency scenario is supported.
[0066] The distributed serial number generation device provided by the application is described below, and the distributed serial number generation device described below can be correspondingly referred to the distributed serial number generation method described above.
[0067] Figure 8 A structure diagram of a distributed serial number generation device is shown, and the device includes:
[0068] The judgment module 81 determines whether the current capacity of the cache queue is greater than the preset threshold according to the received acquisition request.
[0069] The extraction module 82 acquires serial numbers from the cache queue based on the current capacity of the cache queue being greater than the preset threshold.
[0070] The update module 83 determines that the number pool is not exhausted according to the pre-acquired number pool state based on the current capacity of the cache queue being less than or equal to the preset threshold, extracts serial numbers randomly generated based on the preset number length from the number pool into the cache queue to update the cache queue.
[0071] In this embodiment, the judgment module 81 includes a comparison unit for comparing the current capacity of the cache queue with the preset threshold. If the current capacity of the cache queue is greater than the preset threshold, the extraction module 82 acquires serial numbers from the cache queue; if the current capacity of the cache queue is not greater than the preset threshold, the update module 83 updates the cache queue.
[0072] Further, the updating module 83 comprises: a judging unit configured to determine whether the number pool is exhausted according to the pre-acquired number pool state; and an extracting unit configured to extract, according to a case where the judging unit determines that the number pool is not exhausted, a serial number generated based on a preset number length from the number pool to the cache queue.
[0073] The extracting unit comprises: a receiving subunit configured to receive a determination result of the judging unit that the serial number in the number pool is not exhausted; and an extracting subunit configured to extract a serial number randomly generated based on the preset number length from the number pool.
[0074] In an optional embodiment, the updating module 83 further comprises an expansion unit configured to, according to a case where the judging unit determines that the number pool is exhausted, expand the number pool first, and then extract a corresponding serial number from the expanded number pool to the cache queue. The expansion unit comprises: an expansion subunit configured to, according to the pre-acquired number pool state, determine that the number pool is exhausted, trigger expansion of the number pool, increase the length of the serial number by one bit, and form a new number length; and an initialization unit configured to initialize the number pool based on the new number length, and clear the count of the number pool. It should be noted that, after the expansion unit expands the number pool, the updating unit extracts the serial number from the expanded number pool to the cache queue, so as to update the cache queue and update the count of the corresponding serial number in the expanded number pool.
[0075] Figure 9 An example of a schematic diagram of an entity structure of an electronic device is shown in Figure 9 The electronic device can include a processor 910, a communications interface 920, a memory 930, and a communications bus 940, wherein the processor 910, the communications interface 920, and the memory 930 can complete mutual communication through the communications bus 940. The processor 910 can invoke a logical instruction in the memory 930 to execute a distributed serial number generation method, which comprises: determining whether a current capacity of the cache queue is greater than a preset threshold according to a received acquisition request; acquiring an available serial number from the cache queue based on that the current capacity of the cache queue is greater than the preset threshold; or otherwise, determining that the number pool is not exhausted according to a pre-acquired number pool state, and extracting a serial number randomly generated based on a preset number length from the number pool to the cache queue, so as to update the cache queue.
[0076] In addition, the logic instructions in the memory 930 described above can be implemented in the form of software function units and sold or used as independent products, and can be stored in a computer readable storage medium. Based on such understanding, the technical solutions of the present application essentially or the parts that contribute to the prior art or parts of the technical solutions can be embodied in the form of a software product. The computer software product is stored in a storage medium, and includes a plurality of instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present application. The aforementioned storage medium includes: a U disk, a mobile hard disk, a read-only memory (ROM, Read-Only Memory), a random access memory (RAM, Random Access Memory), a magnetic disk or an optical disk, and various media that can store program codes.
[0077] In another aspect, the present application also provides a computer program product, which comprises a computer program stored on a non-transitory computer readable storage medium, and the computer program comprises program instructions, when the program instructions are executed by a computer, the computer can execute the distributed serial number generation method provided by the above-mentioned method, and the method comprises: judging whether the current capacity of the cache queue is greater than a preset threshold according to the received acquisition request; based on the current capacity of the cache queue being greater than the preset threshold, obtaining the available serial number from the cache queue; otherwise, determining that the number pool is not exhausted according to the pre-acquired number pool state, and extracting the serial number randomly generated based on the preset number length from the number pool to the cache queue to update the cache queue.
[0078] In another aspect, the present application also provides a non-transitory computer readable storage medium, which stores a computer program, and the computer program is executed by a processor to implement the distributed serial number generation method provided by the above-mentioned method, and the method comprises: judging whether the current capacity of the cache queue is greater than a preset threshold according to the received acquisition request; based on the current capacity of the cache queue being greater than the preset threshold, obtaining the available serial number from the cache queue; otherwise, determining that the number pool is not exhausted according to the pre-acquired number pool state, and extracting the serial number randomly generated based on the preset number length from the number pool to the cache queue to update the cache queue.
[0079] The device embodiments described above are only schematic, wherein the units described as separate components can or can not be physically separate, and the components displayed as units can or can not be physical units, i.e., they can be located in one place, or distributed on multiple network units. Part or all of the modules can be selected according to actual needs to achieve the purpose of the present embodiment. Those skilled in the art can understand and implement it without creative labor.
[0080] Those skilled in the art can clearly understand the implementation of the embodiments by the description of the above embodiments, and the embodiments can be implemented by means of software and necessary universal hardware platforms, and of course, can also be implemented by hardware. Based on such understanding, the above technical solutions can be embodied in the form of a software product, and the computer software product can be stored in a computer readable storage medium, such as a ROM / RAM, a magnetic disk, an optical disk, etc., and includes a plurality of instructions to make a computer device (which can be a personal computer, a server, or a network device, etc.) execute the method described in each embodiment or some parts of the embodiment.
[0081] Finally, it should be noted that: the above examples are only used to illustrate the technical solutions of the present application, and not to limit them; although the present application has been described in detail with reference to the foregoing examples, those skilled in the art should understand that: it can still modify the technical solutions recorded in the foregoing examples, or make equivalent replacement for some technical features thereof; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present application.
Claims
1. A method for distributed serial number generation, the method comprising: The method comprises: determining whether the current capacity of the cache queue is greater than a preset threshold according to the received acquisition request; based on the current capacity of the cache queue being greater than the preset threshold, obtaining a usable serial number from the cache queue; otherwise, according to the pre-acquired number pool state, it is determined that the number pool is not exhausted, and a serial number generated randomly based on a preset number length is extracted from the number pool to the cache queue to update the cache queue; before the step of extracting a serial number generated randomly based on a preset number length from the number pool to the cache queue, the method comprises: based on the preset number length, the number pool generates a serial number randomly based on a preset cache update increment; updating the count of the corresponding first character in the number pool according to the first character of the generated serial number; the number pool generates a serial number randomly based on a preset cache update increment, comprising: randomly selecting the first character of the serial number; determining the starting coordinates based on the count corresponding to the first character; according to the preset traversal rule and the starting coordinates, traversing the number pool to obtain the remaining characters and form a random serial number; the step of determining the starting coordinates based on the count corresponding to the first character, comprising: based on the count corresponding to the first character, determining the number of permutations and combinations of the remaining digits of the serial number corresponding to the first character that have been used; determining the row coordinates of the last character as the starting coordinates of the last character according to the number of permutations and combinations that have been used; from the last character to the first character, sequentially selecting intermediate characters, and determining the row coordinates of the currently selected intermediate character as the starting coordinates of the currently selected intermediate character based on the number of permutations and combinations that have been used and the row coordinates of all previously determined characters; the starting coordinates are represented as: ; where j_i represents the row coordinates of the i-th column of the number pool; counter[c] represents the number of permutations and combinations of the remaining digits of the serial number that have been used when the first character is c; l represents the length of the character range available for each digit of the serial number; d represents the number length; k represents the i+1-th column of the number pool; j_k represents the row coordinates of the i+1-th column of the number pool; before the step of generating a serial number randomly based on a preset number length, the method further comprises: setting the number length; setting the character range based on the demand; randomly generating a two-dimensional group of the number pool according to the number length and the character range.
2. The distributed serial number generation method of claim 1, wherein, the step of extracting a serial number generated randomly based on a preset number length from the number pool to the cache queue, comprising: after the number pool generates a serial number randomly based on a preset cache update increment, obtaining the randomly generated serial number based on a preset cache update increment and adding it to the cache queue.
3. The distributed serial number generation method of claim 1, wherein, The method further comprises: determining that the number pool is exhausted according to the pre-acquired number pool state, triggering the expansion of the number pool, increasing the length of the serial number by one to form a new number length; based on the new number length, initializing the number pool and clearing the count of the number pool; extracting serial numbers from the expanded number pool to the cache queue to update the cache queue and update the count of the corresponding serial numbers in the expanded number pool.
4. A distributed serial number generation apparatus characterized by comprising: The method comprises: determining, by a judging module, whether a current capacity of the cache queue is greater than a preset threshold according to the received request for obtaining; extracting, by an extracting module, available serial numbers from the cache queue based on that the current capacity of the cache queue is greater than the preset threshold; updating, by an updating module, the cache queue by extracting serial numbers randomly generated based on a preset number length from the number pool to the cache queue based on that the current capacity of the cache queue is greater than the preset threshold and that the number pool is determined to be not exhausted according to a previously obtained number pool state; the device is used for: extracting serial numbers randomly generated based on a preset number length from the number pool to the cache queue based on the preset number length before the extraction; updating the count of the corresponding first character in the number pool according to the first character of the generated serial number; the device is further used for: randomly selecting the first character of the serial number; determining the starting coordinate based on the count corresponding to the first character; traversing the number pool according to a preset traversal rule and the starting coordinate to obtain the remaining characters and form a random serial number; the device is further used for: determining the number of used permutations and combinations of the remaining digits of the serial number corresponding to the first character based on the count corresponding to the first character; determining the row coordinate of the last character as the starting coordinate of the last character according to the number of used permutations and combinations; selecting the intermediate characters in turn from the last character to the first character, and determining the row coordinate of the currently selected intermediate character as the starting coordinate of the currently selected intermediate character based on the number of used permutations and combinations and the row coordinates of all previously determined characters; the starting coordinate is represented as: ; wherein j_i represents the row coordinate j of the i-th column of the number pool; counter[c] represents the number of used permutations and combinations of the remaining digits of the serial number when the first character is c; l represents the length of the character range of each digit of the serial number; d represents the number length; k represents the i+1-th column of the number pool; j_k represents the row coordinate of the i+1-th column of the number pool; the device is further used for: setting the number length before the random generation of the serial numbers based on the preset cache update increment of the number pool based on the preset number length; setting the character range based on the demand; randomly generating the two-dimensional group of the number pool according to the number length and the character range.
5. An electronic device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, The processor executes the program to implement the steps of the distributed serial number generation method according to any one of claims 1 to 3.
6. A non-transitory computer-readable storage medium having stored thereon a computer program, characterized in that, The computer program is executed by the processor to implement the steps of the distributed serial number generation method according to any one of claims 1 to 3.
Citation Information
Patent Citations
ID management method and apparatus
CN108874946A