A method and device for generating serial number
By obtaining and comparing the timestamps of the payment system clock, updating the clock version and combining the host identification and random information to generate serial numbers, the problem of serial number duplication caused by system clock adjustment is solved and the stability and security of the payment system are improved.
Patent Information
- Application Number
- CN202011078375.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-10-10
- Publication Date
- 2024-10-29
- Estimated Expiration
- 2040-10-10
AI Technical Summary
In the payment system of financial institutions, serial numbers generated based on timestamps are easily duplicated due to system clock adjustments, which increases the probability of generating duplicate serial numbers and may cause fatal errors.
By obtaining the first timestamp and the second timestamp of the system clock, if the second timestamp is no later than the first timestamp, the system clock version is updated, and a serial number is generated based on the host identification and the updated clock version, and confusion information is added Such as increasing the serial number and random number to reduce the probability of repetition.
It effectively distinguishes the same timestamp generated under different system clocks, reduces the probability of serial number duplication, and improves system stability and security.
Smart Images

Figure CN112182005B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software construction, and in particular to a method and device for generating a serial number. Background Art
[0002] A large number of serial numbers need to be generated every day in the payment system of financial institutions. The payment system of financial institutions is generally deployed in a distributed cluster, and each host in the cluster will generate a serial number, such as an order serial number. However, the amount of data that the payment system generally has to process is very large, and the access to the payment system is highly concurrent. In a short period of time, tens of thousands of serial numbers may need to be generated, and the serial numbers cannot be repeated in the entire distributed cluster.
[0003] However, in current technical solutions, serial numbers are mostly generated based on timestamps, which is very dependent on the stability of the host system clock. If the host system clock is adjusted, such as the system time is adjusted from 21:00 to 20:30, the timestamp generated before the system clock is adjusted forward may still be generated again. Although the serial number generation structure contains random numbers, the same timestamp still greatly increases the probability of generating duplicate serial numbers, which may cause fatal errors for financial institutions. Summary of the invention
[0004] The present invention provides a method and device for generating a serial number, which solves the problem in the prior art that the same timestamp greatly increases the probability of generating a duplicate serial number.
[0005] In a first aspect, the present invention provides a method for generating a serial number, comprising: obtaining a first timestamp and a second timestamp; the first timestamp is a timestamp generated by a system clock for the i-th serial number; the second timestamp is a timestamp generated by a system clock for the i+1-th serial number; i is a positive integer; if the second timestamp is not later than the first timestamp, updating the first clock version of the system clock to a second clock version; generating a serial number based at least on a host identifier of the host, the second clock version and a timestamp generated by the system clock.
[0006] In the above method, after obtaining the first timestamp and the second timestamp, if the second timestamp is not later than the first timestamp, it means that the first timestamp and the second timestamp are repeated. Then, by introducing the clock version, and updating the first clock version of the first system clock to the second clock version, the same timestamp that may be generated under the two system clocks is distinguished by the clock version, and a serial number is generated according to the updated second clock version, the host identifier and the timestamp generated by the second system clock. Therefore, the probability of generating a duplicate serial number will not be increased due to the adjustment of the system clock.
[0007] Optionally, before obtaining the first timestamp and the second timestamp, it also includes: determining that the system clock is adjusted from the first system clock to the second system clock; the first timestamp is: the latest timestamp generated by the first system clock before the first system clock is adjusted to the second system clock; the second timestamp is: the earliest timestamp generated by the second system clock after the first system clock is adjusted to the second system clock; updating the first clock version of the system clock to the second clock version includes: using the second clock version as the clock version of the second system clock.
[0008] In the above method, after determining that the system clock is adjusted from the first system clock to the second system clock, the latest timestamp generated by the first system clock and the earliest timestamp generated by the second system clock are obtained. As long as the second timestamp is not later than the first timestamp, it is possible that the timestamps are the same. Therefore, by using the second clock version as the clock version of the second system clock, the same timestamps generated by different system clocks can be distinguished.
[0009] Optionally, generating a serial number at least based on the host identifier of the host, the second clock version and the timestamp generated by the system clock includes: generating a serial number based on obfuscation information, the host identifier, the second clock version and the timestamp generated by the system clock; the obfuscation information includes a self-incrementing serial number and / or a random number.
[0010] In the above method, by obfuscating information, the randomness of the serial number can be further increased and the probability of serial number duplication can be reduced.
[0011] Optionally, the number of digits of the self-incrementing sequence number is determined based on the maximum required number of serial numbers generated by the system within a historical unit time.
[0012] Under the above method, the maximum number of required serial numbers generated by the system within a historical unit time determines the possibility of the serial number generating the same timestamp. If the number of digits of the self-incrementing serial number is set appropriately, the self-incrementing serial number can be used to further distinguish the same timestamp and generate different serial numbers.
[0013] Optionally, the accuracy of the timestamp generated by the system clock is at least millisecond level.
[0014] In the above manner, the possibility of duplication of the timestamp generated by the system clock is reduced through the accuracy above the millisecond level.
[0015] Optionally, the first clock version and the second clock version are two clock versions that increase continuously.
[0016] In the above manner, the two clock versions that are incremented continuously increase the recognizability of the clock version, so that the system clock change situation can be known through the clock version in the serial number.
[0017] Optionally, if the second timestamp is later than the first timestamp, the first clock version continues to be used as the clock version of the system clock; and a serial number is generated based at least on the host identifier, the first clock version and the timestamp generated by the system clock.
[0018] In the above manner, if the second timestamp is later than the first timestamp, it is impossible to generate the same timestamp, and there is no need to generate a new clock version, so the first clock version continues to be used as the clock version of the system clock, saving clock versions.
[0019] In a second aspect, the present invention provides a serial number generating device, comprising: an acquisition module, used to obtain a first timestamp and a second timestamp; the first timestamp is a timestamp generated by the system clock for the i-th serial number; the second timestamp is a timestamp generated by the system clock for the i+1-th serial number; i is a positive integer; a processing module, used to update the first clock version of the system clock to the second clock version if the second timestamp is not later than the first timestamp; and generate a serial number based on at least the host identifier of the host, the second clock version and the timestamp generated by the system clock.
[0020] Optionally, the processing module is also used to: determine that the system clock is adjusted from a first system clock to a second system clock; the first timestamp is: the latest timestamp generated by the first system clock before the first system clock is adjusted to the second system clock; the second timestamp is: the earliest timestamp generated by the second system clock after the first system clock is adjusted to the second system clock; the processing module is specifically used to: use the second clock version as the clock version of the second system clock.
[0021] Optionally, the processing module is specifically used to: generate a serial number according to the obfuscation information, the host identifier, the second clock version and the timestamp generated by the system clock; the obfuscation information includes a self-incrementing serial number and / or a random number.
[0022] Optionally, the number of digits of the self-incrementing sequence number is determined based on the maximum required number of serial numbers generated by the system within a historical unit time.
[0023] Optionally, the accuracy of the timestamp generated by the system clock is at least millisecond level.
[0024] Optionally, the first clock version and the second clock version are two clock versions that increase continuously.
[0025] Optionally, if the second timestamp is later than the first timestamp, the first clock version continues to be used as the clock version of the system clock; and a serial number is generated based at least on the host identifier, the first clock version and the timestamp generated by the system clock.
[0026] The beneficial effects of the above-mentioned second aspect and each optional device of the second aspect can refer to the beneficial effects of the above-mentioned first aspect and each optional method of the first aspect, and will not be repeated here.
[0027] In a third aspect, the present invention provides a computer device, including a program or an instruction, which, when executed, is used to execute the above-mentioned first aspect and each optional method of the first aspect.
[0028] In a fourth aspect, the present invention provides a storage medium, comprising a program or an instruction, which, when executed, is used to execute the above-mentioned first aspect and each optional method of the first aspect.
[0029] These and other aspects of the present invention will become more apparent from the following description of the embodiments. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0031] Figure 1 A flowchart corresponding to a method for generating a serial number provided in an embodiment of the present invention;
[0032] Figure 2 A schematic diagram of the structure of a serial number in a serial number generation method provided by an embodiment of the present invention;
[0033] Figure 3 A schematic diagram of a specific flow chart corresponding to a method for generating a serial number provided in an embodiment of the present invention;
[0034] Figure 4 A schematic diagram of the structure of a serial number generating device provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0035] In order to make the purpose, technical scheme and advantages of the present invention clearer, the present invention will be further described in detail below with reference to the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0036] A large number of serial numbers need to be generated every day in the payment system of financial institutions. The payment system of financial institutions is generally deployed in a distributed cluster, and the serial numbers cannot be repeated in the entire distributed cluster. However, in the current technical solutions, serial numbers are mostly generated based on timestamps, which is very dependent on the stability of the system clock of the host. If the system clock of the host is adjusted, the timestamp that has been generated may still be generated again. Although the generation structure of the serial number contains random numbers, the same timestamp still greatly increases the probability of generating duplicate serial numbers, which may cause fatal errors for financial institutions.
[0037] For this reason, Figure 1 As shown, an embodiment of the present application provides a method for generating a serial number.
[0038] Step 101: Obtain a first timestamp and a second timestamp.
[0039] The first timestamp is a timestamp generated by the system clock for the i-th serial number; the second timestamp is a timestamp generated by the system clock for the i+1-th serial number; i is a positive integer.
[0040] Step 102: If the second timestamp is not later than the first timestamp, then update the first clock version of the system clock to the second clock version.
[0041] Step 103: Generate a serial number at least according to the host identifier of the host, the second clock version and the timestamp generated by the system clock.
[0042] It should be noted that the i-th serial number and the i+1-th serial number are continuous serial numbers, and the system clock has a clock version attribute. If the second timestamp is not later than the first timestamp, it is still possible to generate duplicate timestamps without updating the clock version. After introducing the clock version, the same timestamps can be distinguished.
[0043] In an optional implementation manner, a possible situation before step 101 is: determining that the system clock is adjusted from the first system clock to the second system clock.
[0044] The first timestamp is: the latest timestamp generated by the first system clock before the first system clock is adjusted to the second system clock; the second timestamp is: the earliest timestamp generated by the second system clock after the first system clock is adjusted to the second system clock.
[0045] In this case, the specific steps of updating the first clock version of the system clock to the second clock version in step 102 are as follows:
[0046] The second clock version is used as the clock version of the second system clock.
[0047] For example, the system clock is the first system clock clock1, and the clock version is v1. For example, at 21:29:05 on September 20, 2020, clock1 generates a first timestamp of 20200920212905, and the serial number includes the clock version and the first timestamp, that is, v120200920212905. At 21:30, clock1 is adjusted forward by 30 minutes, and the system clock is adjusted to the second system clock clock2. Clock2 starts timing at 21:00. For example, at 21:01:05 on September 20, 2020, clock2 generates a second timestamp of 20200920210105. Obviously, the second timestamp is not later than the first timestamp, so the version v1 is updated to v2. If the second system clock is reset and returns to 21:29:05 on September 20, 2020, the timestamp generated is 20200920212905, so the serial number will also include the clock version and the second timestamp, that is, v220200920212905, so that the timestamp generated by the previous v1 version can be distinguished from the timestamp generated by the v2 version through v1 and v2.
[0048] It should be noted that in order to solve the problem of serial number duplication caused by system clock adjustment, you can also use a server with a unified and stable system clock to generate a timestamp. Each time a serial number is generated, a clock server is remotely called to generate a timestamp.
[0049] Step 102 is directed to a situation where the second timestamp is no later than the first timestamp. Another situation is as follows:
[0050] If the second timestamp is later than the first timestamp, the first clock version continues to be used as the clock version of the system clock; and a serial number is generated based at least on the host identifier, the first clock version and the timestamp generated by the system clock.
[0051] For example, the system clock is the first system clock clock1, and the clock version is v1. For example, at 21:23:05 on September 20, 2020, clock1 generates the first timestamp of 20200920212305. The serial number includes the clock version and the first timestamp, that is, v120200920212305. At 21:30, clock1 is adjusted forward by 5 minutes, and the system clock is adjusted to the second system clock clock2. Clock2 starts timing at 21:25. For example, at 21:25:05 on September 20, 2020, clock2 generates the second timestamp of 20200920212505. Obviously, the second timestamp is later than the first timestamp, so the timestamps generated later will not be repeated with the first timestamp and the previous timestamps. The first clock version can be continued to be used, and the timestamps at the same time can also be distinguished.
[0052] In an optional implementation manner, step 103 may specifically be:
[0053] A serial number is generated according to the obfuscation information, the host identifier, the second clock version and the timestamp generated by the system clock; the obfuscation information includes a self-incrementing serial number and / or a random number.
[0054] It should be noted that even under the same clock version, it is still possible to generate the same timestamp. For example, if the accuracy of the timestamp is at the second level, it is possible that multiple requests for generating serial numbers are received within one second, and multiple serial numbers with the same timestamp will be generated within one second. Then, these serial numbers with the same timestamp can be further distinguished by adding obfuscation information. The host identifier can uniquely identify a host that generates an order number in the cluster, which can be obtained based on the IP address. The auto-increment sequence number can be continuously incremented, such as 000, 001, 002, 003...999; it can also be incremented according to a certain rule, such as first increasing odd numbers and then increasing even numbers, such as first 000, 002, 004...998, then 001, 003, 005...999. The random number further increases the randomness of the serial number and further reduces the probability of generating duplicate serial numbers.
[0055] It should be noted that, in an optional implementation manner, the number of digits of the self-incrementing sequence number is determined based on the maximum required number of serial numbers generated by the system within a historical unit time.
[0056] For example, if the maximum number of serial numbers generated by the system within a historical unit time (such as 1 second) is 900, then only three digits of the self-incrementing sequence number are needed in decimal to completely distinguish the 900 generated serial numbers.
[0057] In an optional implementation, the accuracy of the timestamp generated by the system clock is at least at the millisecond level.
[0058] For example, if the accuracy is milliseconds, the timestamp format is "year-month-day-hour-minute-second-millisecond". For example, at 22:23:05:002 on September 20, 2020, the timestamp is 202009 20222305 002.
[0059] In an optional implementation manner, the first clock version and the second clock version are two clock versions that increase continuously.
[0060] In the above method, by setting consecutive clock versions, such as v1 and v2, the adjustment information of the system clock can be indicated.
[0061] comprehensive Figure 1 The method shown, the structure of the serial number is as follows Figure 2 As shown, it includes the following parts:
[0062] Clock Version (clockVersion): It can identify the number of times the current clock has been adjusted. If the timestamp currently generated is smaller than the timestamp generated last time, the version number can be incremented by one to ensure the overall orderliness of the serial number. The length of this part can be customized. Considering that the frequency of system clock adjustment is very low, and it is divided into front and back shifting, only the front shifting needs to adjust the clock version. The 6-bit length can support 1 million clock front shifts, which is enough to ensure the overall incrementality. After exceeding 1 million times (increased to 999999), it will restart from 0.
[0063] Timestamp: The timestamp is the current time of the system clock. The format can be generated in YYYYMMDDHHMMSS format, a total of 14 digits, such as 20200804102601.
[0064] Host ID (machineId): The length of the host ID can be customized. The longer the length, the more hosts the cluster supports. The generation rules can be mapped according to the IP, as shown in the following table.
[0065] IP Host ID 192.168.1.1 0 192.168.1.2 1
[0066] Table 1 Mapping table of IP addresses and host identifiers
[0067] Auto-increment sequence number (sequence): The length can be customized, starting from 0 and increasing. After increasing to the maximum value, it starts from 0 again.
[0068] Random number (randomNumber): The length can be customized. The purpose of the random number is to prevent exhaustive enumeration and attacks.
[0069] Figure 1 Under the method shown, since the clock adjustment is taken into consideration, the clock version is added to the serial number composition, so that even if the clock is forwarded, it can be guaranteed that the generated serial number will not be repeated. The clock version can also be set to be generated incrementally, which can ensure the orderliness of the serial number as a whole. Secondly, this method can be executed independently on each host in the cluster, with high execution efficiency and strong scalability, and is very suitable for use in a distributed cluster environment. In the above method, the instability of the system clock of the host in the distributed system cluster environment is taken into account, and a solution of adding a clock version to the component of the serial number is proposed, which effectively solves the problem of serial number duplication caused by clock adjustment.
[0070] Combine the following Figure 3 , a method for generating a serial number provided by the present application is described in detail.
[0071] Step (1): Get the last generated timestamp prevTime (first timestamp) of the serial number and the current timestamp of the system clock (second timestamp). If it is the first time to obtain the serial number, the last generated timestamp (first timestamp) prevTime is 0. The current timestamp currTime of the local clock is used as the timestamp component in the serial number ID.
[0072] Step (2): Compare the last generated timestamp prevTime with the current timestamp currTime of the local clock. If currTime is greater than or equal to prevTime, the clock version clockVersion does not need to be adjusted. If currTime is less than prevTime, it means that the clock has been moved forward. The clock version clockVersion is incremented by one and is persisted so that the latest clock version can be read again after the application is restarted.
[0073] Step (3): Obtain the host ID, self-incrementing serial number and random number respectively.
[0074] Step (4): Concatenate the clock version clockVersion, timestamp timestamp, machine number machineId, auto-increment sequence number sequence and random number randomNumber according to the serial number composition rules. If the number of digits of each component is insufficient during concatenation, the front part of the component is padded with 0.
[0075] Step (5): Replace the last acquisition time prevTime with the timestamp currTime generated this time for comparison during the next generation.
[0076] Step (6): Return the serial number.
[0077] like Figure 4As shown, the present invention provides a serial number generating device, including: an acquisition module 401, used to obtain a first timestamp and a second timestamp; the first timestamp is a timestamp generated by the system clock for the i-th serial number; the second timestamp is a timestamp generated by the system clock for the i+1-th serial number; i is a positive integer; a processing module 402, used to update the first clock version of the system clock to the second clock version if the second timestamp is not later than the first timestamp; generate a serial number based on at least the host identifier of the host, the second clock version and the timestamp generated by the system clock.
[0078] Optionally, the processing module 402 is also used to: determine that the system clock is adjusted from a first system clock to a second system clock; the first timestamp is: the latest timestamp generated by the first system clock before the first system clock is adjusted to the second system clock; the second timestamp is: the earliest timestamp generated by the second system clock after the first system clock is adjusted to the second system clock; the processing module 402 is specifically used to: use the second clock version as the clock version of the second system clock.
[0079] Optionally, the processing module 402 is specifically used to: generate a serial number according to the obfuscation information, the host identifier, the second clock version and the timestamp generated by the system clock; the obfuscation information includes a self-incrementing serial number and / or a random number.
[0080] Optionally, the number of digits of the self-incrementing sequence number is determined based on the maximum required number of serial numbers generated by the system within a historical unit time.
[0081] Optionally, the accuracy of the timestamp generated by the system clock is at least millisecond level.
[0082] Optionally, the first clock version and the second clock version are two clock versions that increase continuously.
[0083] Optionally, if the second timestamp is later than the first timestamp, the first clock version continues to be used as the clock version of the system clock; and a serial number is generated based at least on the host identifier, the first clock version and the timestamp generated by the system clock.
[0084] Based on the same inventive concept, an embodiment of the present invention further provides a computer device, including a program or instruction. When the program or instruction is executed, the serial number generating method and any optional method provided in the embodiment of the present invention are executed.
[0085] Based on the same inventive concept, an embodiment of the present invention further provides a computer-readable storage medium, including a program or instructions. When the program or instructions are executed, the serial number generation method provided in the embodiment of the present invention and any optional method are executed.
[0086] Although the preferred embodiments of the present invention have been described, those skilled in the art may make other changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the present invention.
[0087] Obviously, those skilled in the art can make various changes and modifications to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include these modifications and variations.
Claims
1. A method for generating a serial number, characterized in that: include: Get the first timestamp and the second timestamp; The first timestamp is a timestamp generated by the system clock for the i-th serial number; The second timestamp is a timestamp generated by the system clock for the i+1th serial number; i is a positive integer; If the second timestamp is not later than the first timestamp, updating the first clock version of the system clock to the second clock version; Generate a serial number based at least on the host identifier of the host, the second clock version, and a timestamp generated by the system clock; Before obtaining the first timestamp and the second timestamp, the method further includes: Determining that the system clock is adjusted from the first system clock to the second system clock; The first timestamp is: the latest timestamp generated by the first system clock before the first system clock is adjusted to the second system clock; the second timestamp is: the earliest timestamp generated by the second system clock after the first system clock is adjusted to the second system clock; The updating of the first clock version of the system clock to the second clock version includes: The second clock version is used as the clock version of the second system clock.
2. The method according to claim 1, characterized in that The generating a serial number at least according to the host identifier of the host, the second clock version and the timestamp generated by the system clock includes: A serial number is generated according to the obfuscation information, the host identifier, the second clock version and the timestamp generated by the system clock; the obfuscation information includes a self-incrementing serial number and / or a random number.
3. The method according to claim 2, characterized in that The number of digits of the self-incrementing sequence number is determined based on the maximum number of serial numbers generated by the system within a historical unit time.
4. The method according to any one of claims 1 to 3, characterized in that: The accuracy of the timestamp generated by the system clock is at least millisecond level.
5. The method according to any one of claims 1 to 3, characterized in that: The first clock version and the second clock version are two clock versions that increase continuously.
6. The method according to any one of claims 1 to 3, characterized in that: Also includes: If the second timestamp is later than the first timestamp, the first clock version continues to be used as the clock version of the system clock; A serial number is generated at least according to the host identifier, the first clock version and a timestamp generated by the system clock.
7. A serial number generating device, characterized in that: include: An acquisition module, used to acquire a first timestamp and a second timestamp; The first timestamp is a timestamp generated by the system clock for the i-th serial number; The second timestamp is a timestamp generated by the system clock for the i+1th serial number; i is a positive integer; A processing module, configured to update the first clock version of the system clock to the second clock version if the second timestamp is not later than the first timestamp; generate a serial number based at least on the host identifier of the host, the second clock version and the timestamp generated by the system clock; The processing module is further used to: determine that the system clock is adjusted from the first system clock to the second system clock; the first timestamp is: the latest timestamp generated by the first system clock before the first system clock is adjusted to the second system clock; The second timestamp is: the earliest timestamp generated by the second system clock after the first system clock is adjusted to the second system clock; The processing module is further configured to: use the second clock version as the clock version of the second system clock.
8. A computer device, characterized in that: The method comprises a program or an instruction, and when the program or the instruction is executed, the method according to any one of claims 1 to 6 is performed.
9. A computer-readable storage medium, characterized in that: The method comprises a program or an instruction, and when the program or the instruction is executed, the method according to any one of claims 1 to 6 is performed.
Citation Information
Patent Citations
Multi-version data acquisition method and device
CN103020058A
Serial number generating method and device, and a server
CN105072160A
