Global sequence number generation method, apparatus, device, medium, and product
By recording the reception time and generating a global sequence number when receiving a task request, the low performance and insufficient uniqueness of generating globally unique sequence codes in existing technologies are solved, realizing an efficient and concise method for generating global sequence numbers, which is suitable for distributed environments.
Patent Information
- Application Number
- CN202111520425.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-13
- Publication Date
- 2026-01-02
- Estimated Expiration
- 2041-12-13
AI Technical Summary
Existing technologies suffer from low performance, difficulty in expansion, low query efficiency due to unordered sequence codes, and inability to guarantee uniqueness due to reliance on server clock when generating globally unique sequence codes.
By receiving task requests, recording the receiving time, obtaining relevant sequence codes, and generating a first timestamp through locking, a global sequence number is generated by combining the server identifier, the second timestamp, and the random code, ensuring global uniqueness and order in high-concurrency scenarios.
It achieves efficient generation of globally unique serial numbers, avoids performance bottlenecks, simplifies system complexity, is suitable for distributed scenarios, has high concurrency and high availability, and meets the needs of Internet big data scenarios.
Smart Images

Figure CN114168607B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present disclosure relates to the field of big data, in particular to the field of distributed data processing, and more particularly to a global sequence number generation method, device, equipment, medium and program product. BACKGROUND
[0002] Developers have used global unique sequence codes in the process of research and development, such as UUID, database auto-increment sequence, or snowflake algorithm in distributed scenarios. The database auto-increment sequence has low performance and is difficult to expand, which can easily lead to technical bottlenecks. For UUID, although it is simple and has no network consumption, the sequence code is unordered, the query efficiency is low, and it cannot be trend-incremented. The snowflake algorithm can roughly meet the generation order and is simple and efficient, but it is heavily dependent on the server clock. If the clock is wrong, it cannot guarantee global uniqueness. SUMMARY
[0003] In view of the above problems, the present disclosure provides a global sequence number generation method, device, equipment, medium and program product which improves generation efficiency while guaranteeing global uniqueness.
[0004] According to a first aspect of the present disclosure, a global sequence number generation method is provided, comprising: receiving a task request and recording a receiving time; obtaining a related sequence code based on the task request and the receiving time; generating a first timestamp through a locking process based on the receiving time; and generating the global sequence number based on the related sequence code and the first timestamp.
[0005] According to an embodiment of the present disclosure, the related sequence code is obtained based on the task request and the receiving time, comprising: obtaining a server identifier based on the task request, wherein the server identifier comprises an address identifier; generating a second timestamp based on the receiving time, wherein the unit of the lowest bit of the second timestamp is greater than the unit of the highest bit of the first timestamp; generating a random code; and generating the related sequence code through sorting based on the server identifier, the second timestamp and the random code.
[0006] According to an embodiment of the present disclosure, the server identifier further comprises a service group identifier, which is used to reflect the service group where the server is located, wherein for the same server, multiple service group identifiers are included.
[0007] According to an embodiment of the present disclosure, the first timestamp is generated by a locking process based on the receiving moment, including: obtaining a second task request, recording a second moment; locking the second task request to ensure uniqueness of the second task request at the moment in concurrent tasks; obtaining a first moment, the first moment corresponding to a first task request, the first task request including a previous task request of the second task request; determining whether the second moment is greater than the first moment; and in the case that the second moment is greater than the first moment, using the second moment as the first timestamp.
[0008] According to an embodiment of the present disclosure, the method further includes: in the case that the second moment is not greater than the first moment, adding a fixed preset value to the value of the first moment to obtain a third moment; and using the third moment as the first timestamp.
[0009] According to an embodiment of the present disclosure, the first timestamp includes millisecond-level information.
[0010] According to an embodiment of the present disclosure, the second timestamp includes year-month-day-hour-minute-second-level information.
[0011] A second aspect of the present disclosure provides a device for generating a global sequence number, including: a task receiving module, a related sequence code generating module, a first timestamp generating module and a global sequence number generating module, wherein the task receiving module is configured to receive a task request and record a receiving moment; the related sequence code generating module is configured to obtain a related sequence code based on the task request and the receiving moment; the first timestamp generating module is configured to generate a first timestamp by a locking process based on the receiving moment; and the global sequence number generating module is configured to generate the global sequence number based on the related sequence code and the first timestamp.
[0012] According to an embodiment of the present disclosure, the related sequence code generating module includes: a server identification generating module, a second timestamp generating module, a random code generating module and a related sequence code sorting module, wherein the server identification generating module is configured to obtain a server identification based on the task request, wherein the server identification includes an address identification; the second timestamp generating module is configured to generate a second timestamp based on the receiving moment, wherein the unit of the lowest bit of the second timestamp is greater than the unit of the highest bit of the first timestamp; the random code generating module is configured to generate a random code; and the related sequence code sorting module is configured to generate the related sequence code by sorting based on the server identification, the second timestamp and the random code.
[0013] The third aspect of the present disclosure provides an electronic device, comprising: one or more processors; a memory for storing one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors perform the method of the global sequence number generation.
[0014] The fourth aspect of the present disclosure further provides a computer-readable storage medium having stored thereon executable instructions that, when executed by a processor, cause the processor to perform the method of the global sequence number generation.
[0015] The fifth aspect of the present disclosure further provides a computer program product comprising a computer program which, when executed by a processor, implements the method of the global sequence number generation. BRIEF DESCRIPTION OF DRAWINGS
[0016] The above and other objects, features and advantages of the present disclosure will become more apparent from the following description when taken in conjunction with the accompanying drawings, in which:
[0017] Figure 1 An application scenario diagram of a method, apparatus, device, medium and program product of global sequence number generation according to an embodiment of the present disclosure is schematically shown.
[0018] Figure 2 A flowchart of a method of global sequence number generation according to an embodiment of the present disclosure is schematically shown.
[0019] Figure 3 A flowchart of a method of related sequence code generation according to an embodiment of the present disclosure is schematically shown.
[0020] Figure 4A A flowchart of a method of first timestamp generation according to an embodiment of the present disclosure is schematically shown.
[0021] Figure 4B A flowchart of another method of first timestamp generation according to an embodiment of the present disclosure is schematically shown.
[0022] Figure 5A A structural block diagram of a global sequence number generation apparatus according to an embodiment of the present disclosure is schematically shown.
[0023] Figure 5B A structural block diagram of another sequence number generation apparatus according to an embodiment of the present disclosure is schematically shown.
[0024] Figure 6 A schematic diagram of the working principle of volatile according to an embodiment of the present disclosure is schematically shown.
[0025] Figure 7A block diagram of an electronic device suitable for implementing a method of a global sequence number according to an embodiment of the present disclosure is schematically shown. DETAILED DESCRIPTION
[0026] Hereinafter, embodiments of the present disclosure will be described with reference to the accompanying drawings. However, it is to be understood that the present disclosure is not limited to the embodiments described herein, but can be implemented in various ways. The embodiments will be variously described with reference to the accompanying drawings so as to convey the principle of the present disclosure to those skilled in the art. It is to be understood that the embodiments are illustrative of the present disclosure and are not restrictive thereof. In the following detailed description, numerous specific details are set forth in order to provide a thorough understanding of the present disclosure. However, it will be apparent to one skilled in the art that the present disclosure can be practiced without these specific details. In other instances, well-known structures and functions have not been described in detail in order to avoid obscuring aspects of the present disclosure.
[0027] The terms used herein are merely used to describe specific embodiments, and are not intended to limit the present disclosure. The terms "include" and "have" and the like used herein indicate the presence of the described features, steps, operations, and / or components, but do not preclude the presence or addition of one or more other features, steps, operations, or components.
[0028] All terms used herein, including technical and scientific terms, have the same meanings as those generally understood by those skilled in the art unless otherwise defined. It should be noted that the terms used herein should be interpreted as having meanings consistent with the context of the present specification, and should not be interpreted in an idealized or excessively formal manner.
[0029] In the case of using expressions similar to "at least one of A, B, and C, etc.", it should generally be interpreted to include at least one of the items enumerated, but not limited to the items enumerated (e.g., "a system having at least one of A, B, and C" should include a system having A alone, a system having B alone, a system having C alone, a system having A and B together, a system having A and C together, a system having B and C together, and / or a system having A, B, and C together, etc.).
[0030] Before the technical solutions of the present disclosure are described, the technical terms in the art are first described as follows:
[0031] Global sequence number: In the case of implementing a separate database and table, the database self-incrementing primary key cannot guarantee the global uniqueness of the self-incrementing primary key, and therefore a global sequence number generating operation is needed to guarantee the global uniqueness.
[0032] volatile: Java language modifier, related to the memory model, ensures that each instruction cannot be omitted due to compiler optimization, and requires direct reading of the value each time.
[0033] synchronized: Java language keyword, representing the meaning of a lock, and the locked code block can ensure that only one thread can access the locked code block when multiple threads are concurrently executed.
[0034] In the prior art, there are the following solutions:
[0035] Solution one: using database increment to make the whole database unique. However, in the single database single table scenario, this solution causes the database pressure to increase.
[0036] Solution two: UUID, generating a 32-length hexadecimal string, i.e., 128-bit long data. However, in this solution, the sequence code sorting is in a disordered state, and on the basis of this solution, time can be spliced, but the sequence number is too long.
[0037] Solution three: snowflake algorithm, according to the time self-increment sorting, and using the distributed system to avoid ID collision. However, this solution depends on the server clock, and needs to spend on independent development and deployment.
[0038] Solution four: using Redis to generate ID, when the performance of using database to generate sequence number is not enough, using Redis for single-thread operation to generate globally unique ID. Specifically, the atomic operation INCR and INCRBY of Redis are used. However, this solution needs to introduce a new component, which increases the system complexity.
[0039] These solutions in the prior art cannot be perfect, some of which have low efficiency and are prone to performance bottlenecks, some of which depend on third-party components and increase the system complexity. Some generated unique sequences are disordered and have low query efficiency. And basically, the time, business meaning, etc. of the globally unique sequence cannot be directly seen. After understanding and considering different implementation manners, the present application uses the java memory model and the lock mechanism to efficiently generate globally unique sequences by obtaining the server IP, and meets the requirement that the sequence code generated by each server is globally incremental. The called server and the time sequence can be immediately seen, and in the multi-machine load deployment scenario, as long as the server IP is unique in the same network segment, the requirement can be met.
[0040] Embodiments of the present disclosure provide a method for generating a global sequence number, the method comprising: receiving a task request and recording a receiving time; obtaining a relevant sequence code based on the task request and the receiving time; generating a first timestamp through a locking process based on the receiving time; and generating the global sequence number based on the relevant sequence code and the first timestamp. Embodiments of the present disclosure ensure the global uniqueness of the current first timestamp in the locking phase through the first timestamp generated by the locking process. In particular, when dealing with high concurrency threads, the locking operation ensures that the first timestamp generated based on the task request is generated in order according to the queue, and the global sequence number generated in combination with the relevant sequence code is globally unique. At the same time, the method is fast and simple to implement, and can be widely applied in different implementation scenarios, especially in distributed scenarios.
[0041] Figure 1 An application scenario diagram of the method for generating a global sequence number according to an embodiment of the present disclosure is schematically shown.
[0042] As Figure 1 shown, the application scenario 100 according to this embodiment can include terminals 101, 102, 103, a network 104, and a server 105. The network 104 is a medium for providing a communication link between the terminal devices 101, 102, 103 and the server 105. The network 104 can include various connection types, such as wired, wireless communication links, or optical fiber cables, etc.
[0043] A user can use the terminal devices 101, 102, 103 to interact with the server 105 through the network 104 to receive or send messages, etc. Various communication client applications can be installed on the terminal devices 101, 102, 103, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social platform software, etc. (only as examples).
[0044] The terminal devices 101, 102, 103 can be various electronic devices with display screens and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, desktop computers, etc.
[0045] The server 105 can be a server providing various services, such as a background management server providing support for websites browsed by users using the terminal devices 101, 102, 103 (only as an example). The background management server can analyze and process received user requests and other data, and feed back the processing results (such as web pages, information, or data generated according to user requests, etc.) to the terminal devices.
[0046] It should be noted that the method for generating a global sequence number provided by the embodiments of the present disclosure can generally be executed by the server 105. Accordingly, the apparatus for generating a global sequence number provided by the embodiments of the present disclosure can generally be arranged in the server 105. The method for generating a global sequence number provided by the embodiments of the present disclosure can also be executed by a server or a server cluster different from the server 105 and capable of communicating with the terminal device 101, 102, 103 and / or the server 105. Accordingly, the apparatus for generating a global sequence number provided by the embodiments of the present disclosure can also be arranged in a server or a server cluster different from the server 105 and capable of communicating with the terminal device 101, 102, 103 and / or the server 105.
[0047] It should be understood that Figure 1 The number of terminal devices, networks and servers in the above-mentioned scenario is only illustrative. According to the needs of implementation, there can be any number of terminal devices, networks and servers. The following will be described based on the scenario described above, and the method for generating a global sequence number provided by the embodiments of the present disclosure will be described in detail. Figure 1 Figures 2-4B The method for generating a global sequence number provided by the embodiments of the present disclosure will be described in detail.
[0048] Figure 2 An illustrative flowchart of a method for generating a global sequence number according to an embodiment of the present disclosure is shown.
[0049] As shown in Figure 2 The method for generating a global sequence number of this embodiment includes operations S210-S230, and the transaction processing method can be executed by a sequence number generation server.
[0050] In operation S210, a task request is received, and the time of reception is recorded.
[0051] According to the embodiments of the present disclosure, the server of the distributed database can receive a task request, which at least includes a call request, an insertion request, etc. Among them, the call request exists in the form of a thread.
[0052] According to the embodiments of the present disclosure, the time of reception includes recording the time in units of year, month, day, hour, minute, second and millisecond.
[0053] In operation S220, a relevant sequence code is obtained based on the task request and the time of reception.
[0054] For example, the server receives the call request at 11:33:05, 678 milliseconds on October 18, 2021. Then, the time is recorded.
[0055] In operation S230, a first timestamp is generated through a locking process based on the time of reception.
[0056] According to an embodiment of the present disclosure, the first timestamp comprises millisecond level information.
[0057] Of course, the first timestamp also comprises units below the millisecond level, such as the microsecond level.
[0058] For example, the first timestamp is preselected as the millisecond value 678, because before the millisecond value 678 is selected as the first timestamp, the locking process needs to be performed to ensure the uniqueness of the current first timestamp. Therefore, the millisecond value 678 only exists as reference information, and the value of the first timestamp should be obtained after the locking process.
[0059] Specifically, for example, in a high concurrency scenario, it is necessary to distinguish between multiple thread invocation requests. In the Java environment, the synchronized locking process is performed, and the code block accessed by the thread is locked, so that only one thread in the above multiple threads can access the locked code block. After the current thread is processed, the next thread is repeatedly locked, and the above process is repeated, so that the thread executed at the current time has uniqueness, and the first timestamp generated thereby also has uniqueness.
[0060] However, because the first timestamp only comprises three numerical values, the numerical values will be repeated after multiple thread invocation requests are processed. Therefore, the related sequence code needs to be combined with other related sequence codes to generate a global sequence number, so as to ensure the uniqueness of the global sequence number.
[0061] In operation S240, the global sequence number is generated based on the related sequence code and the first timestamp.
[0062] According to an embodiment of the present disclosure, the order of the related sequence code and the first timestamp in the global sequence number is not limited.
[0063] According to an embodiment of the present disclosure, the first timestamp generated by the locking process ensures the global uniqueness of the current first timestamp in the locking stage. In particular, when dealing with high concurrency threads, the locking operation ensures that the first timestamp generated based on the task request is generated in order according to the queue, and the global sequence number generated in combination with the related sequence code is globally unique. At the same time, the method is fast and simple to implement, and can be widely applied in different implementation environments, especially in distributed architecture scenarios.
[0064] Figure 3 A flowchart of a method for generating a related sequence code according to an embodiment of the present disclosure is schematically shown.
[0065] As shown in Figure 3 The method for generating a related sequence code according to the embodiment comprises operations S310-S340.
[0066] In operation S310, a server identifier is acquired based on the task request, wherein the server identifier comprises an address identifier.
[0067] According to an embodiment of the present disclosure, the address identifier comprises IP information of the server.
[0068] For example, in the case of being in the same network segment, the IP information can only take the last three bits, which can be 202. In this way, redundant IP information is removed, and the sequence is more concise.
[0069] According to an embodiment of the present disclosure, the server identifier further comprises a service group identifier, which is used to reflect the service group in which the server is located, wherein a plurality of service group identifiers are included for the same server.
[0070] For example, if the deployment scheme is to deploy multiple services on the same server, the service group in which the same server is located can be distinguished by a configuration file mark. For example, A1 and A2 are used to distinguish different service groups in which the same server is located. The business identifier is used to distinguish different businesses of the same server, so that the global sequence number is more reasonable.
[0071] In operation S320, a second timestamp is generated based on the receiving time, wherein the unit of the lowest bit of the second timestamp is greater than the unit of the highest bit of the first timestamp.
[0072] For example, the second timestamp is October 18, 2021, 11:33:05, and the unit of the lowest bit of the second timestamp is second, which is greater than the unit of the highest bit of the first timestamp, which is millisecond.
[0073] In operation S330, a random code is generated.
[0074] According to an embodiment of the present disclosure, the random code comprises random letters, random numbers, etc.
[0075] According to an embodiment of the present disclosure, the random code further comprises a bit number.
[0076] For example, the random code uses a random letter with a bit number of 3, such as brb.
[0077] In operation S340, the related sequence code is generated by sorting based on the server identifier, the second timestamp, and the random code.
[0078] According to an embodiment of the present disclosure, the sorting of the server identifier, the second timestamp, and the random code in the related sequence code is not limited.
[0079] For example, the global sequence number can be "202brb211018Y113305T678", "202brb211018Y113305T" belongs to the relevant sequence code. Among them, "202" belongs to the address identifier, "brb" belongs to the random code, "211018Y113305" belongs to the second timestamp, wherein "Y" is used to divide the number, which is convenient for users to quickly identify, and "T" also serves the purpose of dividing the number. "678" belongs to the first timestamp.
[0080] For another example, the global sequence number can be "A1brb211018Y113305T678" or "A2bts211018Y113305T928", wherein "A1" belongs to the service group identifier, indicating that the calling request belongs to the first service group sent by the server; "A2" belongs to the service group identifier, indicating that the calling request belongs to the second service group sent by the server. By analogy, no more elaboration. It is worth mentioning that the above ordering is sorted according to the principle of facilitating user identification. Moreover, since the global sequence number here is different service groups of the same server, the address identifier is the same, and thus, in order to avoid the long and redundant global sequence number, the address identifier is not used to construct the global sequence number.
[0081] It should be noted that the above S310-S320 only represent different acquisition methods for different relevant sequence code components, and there is no logical relationship between them, so the execution order of the above S310-S320 is not limited.
[0082] Embodiments of the present disclosure, the server identifier supports multi-server deployment; the second timestamp is used to view the generation date and specific generation time; the random code can be used as a secondary guarantee to prevent duplication. Based on this, the relevant sequence code generated in this way can guarantee the generation of a large number of and non-repeating global sequence numbers.
[0083] Figure 4A A flowchart of a method for generating a first timestamp according to an embodiment of the present disclosure is schematically shown. Figure 6 A schematic diagram showing the working principle of volatile according to an embodiment of the present disclosure is schematically shown.
[0084] As Figure 4A shown, the method for generating a first timestamp of this embodiment includes operations S410-S450.
[0085] In operation S410, a second task request is obtained, and a second time is recorded.
[0086] According to the embodiment of the present disclosure, in the case of the first time of obtaining the calling request or the long time for obtaining the task request, there is no first task request and first time, and the second time corresponding to the second task request (i.e. the current time) is directly taken as the first time stamp.
[0087] Specifically, a preset interval time is set, and if the two requests exceed the preset interval time, the current time of receiving the task request is directly obtained, and the current time is taken as the first time stamp.
[0088] In operation S420, the second task request is locked to ensure the uniqueness of the second task request at the time in the concurrent task.
[0089] In operation S430, the first time is obtained, and the first time corresponds to the first task request, and the first task request includes the last task request of the second task request.
[0090] For example, the called code block is first locked by synchronized, and then the current second time is compared with the first time of the last request, and the first time is obtained according to the global variable modified by volatile.
[0091] The modifier volatile has the following characteristics: first, the instruction rearrangement is prohibited; second, the memory visibility is guaranteed. The present application mainly uses the memory visibility characteristic of the modifier volatile.
[0092] Figure 6 A schematic diagram of the working principle of volatile according to the embodiment of the present disclosure is shown.
[0093] As Figure 6 shown, for the global variable i modified by volatile, the modifier volatile can guarantee that when the value of the global variable i is changed by thread A, the value of the global variable i will be immediately flushed back to the main memory. Therefore, if at this time, thread B is also calling the value of the global variable i, for the value of the global variable i called in the working memory B, the value is also invalid. Thus, thread B is forced to read the new value of the global variable i from the main memory into the working memory B. Finally, at any same time, it is guaranteed that the values of the global variable i seen by thread A and thread B are the same value. Of course, the number of threads is only illustrative, which is not limited here.
[0094] In operation S440, it is judged whether the second time is greater than the first time.
[0095] In operation S450, in the case where the second time is greater than the first time, the second time is taken as the first time stamp.
[0096] Embodiments of the present disclosure ensure uniqueness when a concurrent task request is received by locking, and based on such uniqueness, determine whether the current time is greater than the previous time, and in the case of being greater, use the current second time as the first timestamp, ensuring fast generation of the first timestamp.
[0097] Figure 4B A flowchart of another method for generating a first timestamp according to an embodiment of the present disclosure is schematically shown.
[0098] As Figure 4B The method for generating a first timestamp of the embodiment, in addition to the operations S410-S450, further includes operations S460-S470.
[0099] In operation S460, in the case where the second time is not greater than the first time, a fixed preset value is added to the value of the first time to obtain a third time.
[0100] In operation S470, the third time is used as the first timestamp.
[0101] According to an embodiment of the present disclosure, the fixed preset value is based on the situation, and in general cases, the fixed preset value is 1, and further, in the case where the second time is not greater than the first time, the first time is processed by adding 1 to obtain the third time.
[0102] For example, when the second time is 678 and the first time is 679, 1 is added to the first time to obtain the third time as 680, and 680 is taken as the value of the first timestamp.
[0103] It should be noted that in the embodiments of the present disclosure, S460-S470 and S450 have no time sequence relationship, and only represent different operations performed in the case of two different determination results.
[0104] In the case where the second time is not greater than the first time, the embodiments of the present disclosure do not perform termination processes such as alarms. Instead, the operation of adding a fixed preset value to the original last received time is performed, so that the method of the present disclosure has a fault tolerance mechanism, which can straighten out the time sequence when the time sequence is chaotic, and further, the service of generating a global sequence number is not terminated, ensuring the effective performance of the service.
[0105] Compared with the current mainstream global unique sequence generation scheme, the embodiment of the present disclosure does not rely on third-party components and does not need to generate additional network consumption. Secondly, when multiple servers are deployed, the server can be uniquely and explicitly determined. And it can be seen that the time of generating the unique sequence code and the order of generating the unique sequence code have business meanings. In addition, through personal PC testing, the time consumption is basically within the range of 2.5S-3.2S when calling 1 million times, which has high concurrency and high availability, and meets the rapid development of the Internet big data scene.
[0106] Based on the above Figures 2-4B The method, the present disclosure also provides a global sequence number generation device. The device will be described in detail below. Figures 5A-5B
[0107] Figure 5A The structure block diagram of the global sequence number generation device according to the embodiment of the present disclosure is schematically shown.
[0108] As Figure 5A shown, the global sequence number generation device 500 of the embodiment includes a task receiving module 510, a related sequence code generation module 520, a first timestamp generation module 530, and a global sequence number generation module 540.
[0109] The task receiving module 510 is configured to receive a task request and record the receiving time. In an embodiment, the task receiving module 510 can be configured to perform the operation S210 described above, and details are not repeated here.
[0110] The related sequence code generation module 520 is configured to obtain a related sequence code based on the task request and the receiving time. In an embodiment, the related sequence code generation module 520 can be configured to perform the operation S220 described above, and details are not repeated here.
[0111] The first timestamp generation module 530 is configured to generate a first timestamp through a locking process based on the receiving time. In an embodiment, the first timestamp generation module 530 can be configured to perform the operation S230 described above, and details are not repeated here.
[0112] The global sequence number generation module 540 is configured to generate the global sequence number based on the related sequence code and the first timestamp. In an embodiment, the global sequence number generation module 540 can be configured to perform the operation S240 described above, and details are not repeated here.
[0113] Embodiments of the present disclosure ensure the global uniqueness of the current first timestamp in the locking stage by generating the first timestamp through the locking process. In particular, when dealing with high concurrency threads, the locking operation ensures that the first timestamp generated based on the task request is generated in queue order, and the global sequence number generated in combination with the relevant sequence code is globally unique. At the same time, the method is fast and simple to implement, and can be widely applied in different distributed implementation environments.
[0114] Figure 5B An illustrative structure block diagram of another sequence number generation apparatus according to an embodiment of the present disclosure is shown.
[0115] As shown in Figure 5B the relevant sequence code generation module 520 includes a server identification generation module 521, a second timestamp generation module 522, a random code generation module 523, and a relevant sequence code sorting module 524,
[0116] wherein,
[0117] The server identification generation module 521 is configured to obtain a server identification based on the task request, wherein the server identification includes an address identification.
[0118] The second timestamp generation module 522 is configured to generate a second timestamp based on the receiving time, wherein the unit of the lowest bit of the second timestamp is greater than the unit of the highest bit of the first timestamp.
[0119] The random code generation module 523 is configured to generate a random code; and
[0120] The relevant sequence code sorting module 524 is configured to generate the relevant sequence code by sorting based on the server identification, the second timestamp, and the random code.
[0121] According to an embodiment of the present disclosure, any of the task receiving module 510, the related sequence code generating module 520, the first timestamp generating module 530 and the global sequence number generating module 540 can be combined in one module, or any of them can be split into multiple modules. Alternatively, at least part of the function of one or more of these modules can be combined with at least part of the function of other modules, and implemented in one module. According to an embodiment of the present disclosure, at least one of the task receiving module 510, the related sequence code generating module 520, the first timestamp generating module 530 and the global sequence number generating module 540 can be at least partially implemented as a hardware circuit, such as a field programmable gate array (FPGA), a programmable logic array (PLA), a system on chip, a system on substrate, a system on package, an application specific integrated circuit (ASIC), or any other reasonable way of integrating or packaging a circuit, etc. hardware or firmware, or any one of the three implementation ways of software, hardware and firmware or any appropriate combination of any of them. Alternatively, at least one of the task receiving module 510, the related sequence code generating module 520, the first timestamp generating module 530 and the global sequence number generating module 540 can be at least partially implemented as a computer program module which can perform corresponding functions when it is run.
[0122] Figure 7 A block diagram of an electronic device suitable for implementing the method of generating a global sequence number according to an embodiment of the present disclosure is schematically shown.
[0123] As shown in Figure 7 The electronic device 700 according to an embodiment of the present disclosure includes a processor 701 which can perform various appropriate actions and processes according to programs stored in a read-only memory (ROM) 702 or loaded from a storage portion 708 into a random access memory (RAM) 703. The processor 701 can include, for example, a general-purpose microprocessor (such as a CPU), an instruction set processor and / or a related chipset, and / or a special-purpose microprocessor (such as an application specific integrated circuit (ASIC)), etc. The processor 701 can also include an on-board memory for cache use. The processor 701 can include a single processing unit or multiple processing units for performing different actions of the method processes according to an embodiment of the present disclosure.
[0124] In the RAM 703, various programs and data required for the operation of the electronic device 700 are stored. The processor 701, the ROM 702, and the RAM 703 are connected to each other via the bus 704. The processor 701 performs various operations of the method flow according to the embodiments of the present disclosure by executing the programs in the ROM 702 and / or the RAM 703. It should be noted that the programs can also be stored in one or more memories other than the ROM 702 and the RAM 703. The processor 701 can also perform various operations of the method flow according to the embodiments of the present disclosure by executing the programs stored in the one or more memories.
[0125] According to an embodiment of the present disclosure, the electronic device 700 can further include an input / output (I / O) interface 705, which is also connected to the bus 704. The electronic device 700 can further include one or more of the following components connected to the I / O interface 705: an input part 706 including a keyboard, a mouse, etc.; an output part 707 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage part 708 including a hard disk, etc.; and a communication part 709 including a network interface card such as a LAN card, a modem, etc. The communication part 709 performs communication processing via a network such as the Internet. A drive 710 is also connected to the I / O interface 705 as necessary. A removable medium 711 such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc. is mounted on the drive 710 as necessary, so that a computer program read out therefrom is installed in the storage part 708 as necessary.
[0126] The present disclosure also provides a computer readable storage medium, which can be included in the device / apparatus / system described in the above embodiments; or can exist separately without being assembled into the device / apparatus / system. The above computer readable storage medium carries one or more programs, when the one or more programs are executed, the method according to the embodiments of the present disclosure is implemented.
[0127] According to an embodiment of the present disclosure, the computer readable storage medium can be a nonvolatile computer readable storage medium, for example, can include, but is not limited to, a portable computer diskette, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any appropriate combination thereof. In the present disclosure, the computer readable storage medium can be any tangible medium that contains or stores a program that can be used by or in connection with an instruction execution system, apparatus, or device. For example, according to an embodiment of the present disclosure, the computer readable storage medium can include one or more memories, such as the ROM 702 and / or the RAM 703 described above, and / or one or more memories other than the ROM 702 and the RAM 703.
[0128] Embodiments of the present disclosure also include a computer program product, which includes a computer program containing program codes for executing the methods shown in the flowcharts. When the computer program product is run in a computer system, the program codes are used to make the computer system implement the global sequence number generation method provided by the embodiments of the present disclosure.
[0129] The above-described functions defined in the system / device of the embodiments of the present disclosure are performed when the computer program is executed by the processor 701. According to an embodiment of the present disclosure, the system, device, module, unit, etc. described above can be implemented by computer program modules.
[0130] In one embodiment, the computer program can rely on a tangible storage medium such as an optical storage device, a magnetic storage device, etc. In another embodiment, the computer program can also be transmitted, distributed, and downloaded in the form of a signal on a network medium, and be downloaded and installed through the communication part 709, and / or be installed from the detachable medium 711. The program codes contained in the computer program can be transmitted by any appropriate network medium, including but not limited to wireless, wired, etc., or any appropriate combination thereof.
[0131] In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 709, and / or be installed from the detachable medium 711. When the computer program is executed by the processor 701, the above-described functions defined in the system of the embodiments of the present disclosure are performed. According to an embodiment of the present disclosure, the system, device, apparatus, module, unit, etc. described above can be implemented by computer program modules.
[0132] According to embodiments of the present disclosure, program code of the computer program for performing the methods provided by the embodiments of the present disclosure can be written in any combination of one or more programming languages, and can be implemented in a computer program product. Specifically, the computer program can be implemented in a high-level procedural and / or object-oriented programming language, and / or in assembly / machine language. The programming language includes, but is not limited to, Java, C++, python, “C” language, or similar programming languages. The program code can execute entirely on the user's computing device, partly on the user's device, and partly on a remote computing device, or entirely on the remote computing device or server. In the latter scenario, the remote computing device can be connected to the user's computing device through any kind of network, including a local area network (LAN) or a wide area network (WAN), or the connection can be made to an external computing device, such as through the Internet using an Internet Service Provider (ISP).
[0133] The computer program product of the present disclosure can be a computer program product, which is a machine-readable medium (or computer readable medium) having stored therein a sequence of instructions executable by a machine such as a personal digital assistant (PDA), a laptop, a desktop computer, or a server. Alternatively, the computer program product can be a propagated signal per se generated by using the program code, and the program code can be stored on a machine-readable medium that can be read by a machine such as a personal digital assistant (PDA), a laptop, a desktop computer, or a server. The machine-readable medium can be a machine-readable storage medium that stores program codes, or a machine-readable transmission medium that carries program codes. For example, the program code can be stored on a removable storage medium, such as a floppy diskette, a hard disk, a CD-ROM, a DVD, a Blu-ray Disc, a memory stick, or a memory card, or a storage medium that is fixedly installed within a machine, such as a hard disk. The program code can also be transmitted by a machine-readable transmission medium, such as a modem, a network cable, a wireless signal, or a combination thereof, or transmitted by a computer network, such as the Internet.
[0134] Those skilled in the art will understand that features of various embodiments and / or claims of the present disclosure can be combined or / and integrated with one another, even though such a combination or integration is not expressly mentioned in the present disclosure. In particular, features of various embodiments and / or claims of the present disclosure can be combined and / or integrated with one another without departing from the spirit and teachings of the present disclosure. All such combinations and / or integrations are within the scope of the present disclosure.
[0135] The above describes embodiments of the present disclosure. However, these embodiments are merely for illustrative purposes, and are not intended to limit the scope of the present disclosure. Although each embodiment is described above separately, this does not mean that the measures in each embodiment cannot be used advantageously in combination. The scope of the present disclosure is defined by the appended claims and their equivalents. Those skilled in the art can make various substitutions and modifications without departing from the scope of the present disclosure, and these substitutions and modifications should all fall within the scope of the present disclosure.
Claims
1. A method for generating a global serial number, characterized in that, The method includes: Receive task requests and record the time of receipt; Based on the task request and the receiving time, obtain the relevant sequence code; Based on the received time, a first timestamp is generated through locking processing; and The global sequence number is generated based on the relevant sequence code and the first timestamp; The step of generating a first timestamp based on the receiving time through locking includes: obtaining a second task request and recording a second time; locking the second task request to ensure the uniqueness of the second task request at that time when concurrent tasks are performed; obtaining a first time, the first time corresponding to the first task request, the first task request including the previous task request of the second task request; determining whether the second time is greater than the first time; and if the second time is greater than the first time, using the second time as the first timestamp. The step of generating a first timestamp based on the receiving time through locking further includes: adding a fixed preset value to the value of the first time to obtain a third time when the second time is not greater than the first time; and using the third time as the first timestamp.
2. The method according to claim 1, characterized in that, The step of obtaining the relevant sequence code based on the task request and the receiving time includes: The server identifier is obtained based on the task request, wherein the server identifier includes an address identifier; A second timestamp is generated based on the receiving time, wherein the unit of the least significant bit of the second timestamp is greater than the unit of the most significant bit of the first timestamp; Generate random codes; and The relevant sequence code is generated by sorting the server identifier, the second timestamp, and the random code.
3. The method according to claim 2, characterized in that, The server identifier also includes a service group identifier, which reflects the service group to which the server belongs. For the same server, there are multiple service group identifiers.
4. The method according to claim 1, characterized in that, The first timestamp includes millisecond-level information.
5. The method according to claim 2, characterized in that, The second timestamp includes year, month, day, hour, minute, and second information.
6. An apparatus for generating a global serial number, characterized in that, The device includes: a task receiving module, a related sequence code generation module, a first timestamp generation module, and a global sequence number generation module. in, The task receiving module is used to receive task requests and record the receiving time; The relevant sequence code generation module is used to obtain the relevant sequence code based on the task request and the receiving time; The first timestamp generation module is used to generate a first timestamp based on the received time through a locking process; and The global serial number generation module is used to generate the global serial number based on the relevant serial code and the first timestamp; The step of generating a first timestamp based on the receiving time through locking includes: obtaining a second task request and recording a second time; locking the second task request to ensure the uniqueness of the second task request at that time when concurrent tasks are performed; obtaining a first time, the first time corresponding to the first task request, the first task request including the previous task request of the second task request; determining whether the second time is greater than the first time; and if the second time is greater than the first time, using the second time as the first timestamp. The step of generating a first timestamp based on the receiving time through locking further includes: adding a fixed preset value to the value of the first time to obtain a third time when the second time is not greater than the first time; and using the third time as the first timestamp.
7. The apparatus according to claim 6, characterized in that, The related sequence code generation module includes: a server identifier generation module, a second timestamp generation module, a random code generation module, and a related sequence code sorting module. in, The server identifier generation module is used to obtain a server identifier based on the task request, wherein the server identifier includes an address identifier; The second timestamp generation module is used to generate a second timestamp based on the receiving time, wherein the unit of the least significant bit of the second timestamp is greater than the unit of the most significant bit of the first timestamp; The random code generation module is used to generate random codes; and The related sequence code sorting module is used to generate the related sequence code by sorting based on the server identifier, the second timestamp, and the random code.
8. An electronic device, comprising: One or more processors; Storage device for storing one or more programs. Wherein, when the one or more programs are executed by the one or more processors, the one or more processors perform the method according to any one of claims 1 to 5.
9. A computer-readable storage medium having executable instructions stored thereon, which, when executed by a processor, cause the processor to perform the method according to any one of claims 1 to 5.
10. A computer program product comprising a computer program that, when executed by a processor, implements the method according to any one of claims 1 to 5.
Citation Information
Patent Citations
Generation method, device and system of globally monotonously incremental ID and equipment
CN109639775A
Global ID generation method and device for service entity
CN110737806A