Identifier generation method and device of distributed system and computer equipment
By obtaining the current and reference timestamps in the distributed system, determining clock rollback and taking corresponding measures, the problem of non-unique identifiers caused by inaccurate clocks in the Snowflake algorithm is solved, ensuring the accuracy and sequence of identifier generation.
Patent Information
- Application Number
- CN202511750481.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-26
- Publication Date
- 2026-03-06
AI Technical Summary
In distributed systems, the identifiers generated by the Snowflake algorithm may be non-unique or not generated in chronological order due to inaccurate system clocks or rollbacks, affecting data tracking and differentiation.
By obtaining the current timestamp and reference timestamp of the distributed system, it is determined whether a clock rollback has occurred. If a clock rollback occurs, a preset response mechanism is used to handle the identifier generation request. If a clock rollback does not occur, a target algorithm that depends on the system clock is used to generate the identifier.
This ensures the accuracy of identifier generation, avoids identifier duplication and non-serialization issues caused by clock rollback, and guarantees the accuracy of the system clock.
Smart Images

Figure CN121614475A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of big data technology applications, and in particular to a method, apparatus and computer equipment for generating identifiers for a distributed system. Background Technology
[0002] With the development of distributed technologies, distributed systems have become the standard architecture for handling large-scale data and high-concurrency requests. In distributed systems, generating globally unique identifiers (IDs) is crucial for tracking and distinguishing each data item.
[0003] The Snowflake Algorithm can quickly generate globally unique IDs without relying on a database, and these IDs also have a certain degree of time ordering.
[0004] However, the Snowflake algorithm relies on the system clock to generate timestamps. If the system clock is inaccurate or undergoes rollback or adjustment, the generated IDs may not be unique or generated in chronological order. Summary of the Invention
[0005] Therefore, it is necessary to provide a method, apparatus, and computer device for generating identifiers in a distributed system that can ensure the accuracy of identifiers generated in the distributed system, in order to address the aforementioned technical problems.
[0006] Firstly, this application provides a method for generating identifiers in a distributed system, including:
[0007] When an identifier generation request is triggered, the current timestamp of the distributed system and the pre-stored reference timestamp are obtained; the reference timestamp is the timestamp of the last successful identifier generation.
[0008] Based on the current timestamp and the reference timestamp, determine whether a clock rollback has occurred;
[0009] If a clock rollback is determined to have occurred, the identifier generation request is processed based on a preset response mechanism;
[0010] If it is determined that no clock rollback has occurred, an identifier corresponding to the identifier generation request is generated based on the target algorithm; the target algorithm is an algorithm that generates identifiers based on the system clock of the distributed system.
[0011] In one embodiment, processing the identifier generation request based on a preset response mechanism includes any one of the following:
[0012] The identifier generation request was rejected.
[0013] After the system time of the distributed system returns to normal, the identifier corresponding to the identifier generation request is generated based on the target algorithm.
[0014] Based on the backup time source and the target algorithm, respond to the identifier generation request and generate the identifier corresponding to the identifier generation request.
[0015] In one embodiment, the method further includes:
[0016] If the generated identifier is the same as a previously generated identifier, the business rollback mechanism is triggered to roll back the current business operation to the stable state before the identifier was generated.
[0017] In one embodiment, generating the identifier corresponding to the identifier generation request based on the backup time source and the target algorithm includes:
[0018] Based on the backup time source, determine the new current timestamp;
[0019] If, based on the new current timestamp and the reference timestamp, it is determined that the backup time source has not undergone clock rollback, the identifier is generated based on the backup time source and the target algorithm.
[0020] In one embodiment, the method further includes:
[0021] If the backup time source is determined to be abnormal based on the new current timestamp and the reference timestamp, then a prompt message indicating that the backup time source is abnormal will be output.
[0022] In one embodiment, determining whether a clock rollback has occurred based on the current timestamp and the reference timestamp includes:
[0023] Obtain the difference between the reference timestamp and the preset tolerance threshold;
[0024] If the current timestamp is less than the difference, it is determined that a clock rollback has occurred.
[0025] Secondly, this application also provides an identifier generation apparatus for a distributed system, comprising:
[0026] The acquisition module is used to acquire the current timestamp and the pre-stored reference timestamp of the distributed system when an identifier generation request is triggered; the reference timestamp is the timestamp of the last successful identifier generation.
[0027] The determination module is used to determine whether a clock rollback has occurred based on the current timestamp and the reference timestamp.
[0028] The first processing module is used to process the identifier generation request based on a preset response mechanism when it is determined that a clock rollback has occurred.
[0029] The second processing module is used to generate the identifier corresponding to the identifier generation request based on the target algorithm, provided that no clock rollback has occurred; the target algorithm is an algorithm that generates identifiers based on the system clock of the distributed system.
[0030] Thirdly, this application also provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to perform the following steps:
[0031] When an identifier generation request is triggered, the current timestamp of the distributed system and the pre-stored reference timestamp are obtained; the reference timestamp is the timestamp of the last successful identifier generation.
[0032] Based on the current timestamp and the reference timestamp, determine whether a clock rollback has occurred;
[0033] If a clock rollback is determined to have occurred, the identifier generation request is processed based on a preset response mechanism;
[0034] If it is determined that no clock rollback has occurred, an identifier corresponding to the identifier generation request is generated based on the target algorithm; the target algorithm is an algorithm that generates identifiers based on the system clock of the distributed system.
[0035] Fourthly, this application also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, performs the following steps:
[0036] When an identifier generation request is triggered, the current timestamp of the distributed system and the pre-stored reference timestamp are obtained; the reference timestamp is the timestamp of the last successful identifier generation.
[0037] Based on the current timestamp and the reference timestamp, determine whether a clock rollback has occurred;
[0038] If a clock rollback is determined to have occurred, the identifier generation request is processed based on a preset response mechanism;
[0039] If it is determined that no clock rollback has occurred, an identifier corresponding to the identifier generation request is generated based on the target algorithm; the target algorithm is an algorithm that generates identifiers based on the system clock of the distributed system.
[0040] Fifthly, this application also provides a computer program product, including a computer program that, when executed by a processor, performs the following steps:
[0041] When an identifier generation request is triggered, the current timestamp of the distributed system and the pre-stored reference timestamp are obtained; the reference timestamp is the timestamp of the last successful identifier generation.
[0042] Based on the current timestamp and the reference timestamp, determine whether a clock rollback has occurred;
[0043] If a clock rollback is determined to have occurred, the identifier generation request is processed based on a preset response mechanism;
[0044] If it is determined that no clock rollback has occurred, an identifier corresponding to the identifier generation request is generated based on the target algorithm; the target algorithm is an algorithm that generates identifiers based on the system clock of the distributed system.
[0045] The aforementioned identifier generation method, apparatus, and computer device for distributed systems, when triggering an identifier generation request, first obtain the current timestamp of the distributed system and a pre-stored reference timestamp, where the reference timestamp is the timestamp of the last successful identifier generation. Then, based on the current timestamp and the reference timestamp, it can be determined whether a clock rollback has occurred. If a clock rollback is determined to have occurred, the identifier generation request is processed based on a preset response mechanism. If a clock rollback is determined not to have occurred, the identifier corresponding to the identifier generation request is generated based on a target algorithm that depends on the system clock of the distributed system. In the process of generating the identifier corresponding to the identifier generation request, the occurrence of a clock rollback in the system is considered, and different methods are used to process the identifier generation request under different circumstances, thereby ensuring the accuracy of the system clock as much as possible, and thus ensuring the accuracy of the generated identifier. Attached Figure Description
[0046] To more clearly illustrate the technical solutions in the embodiments of this application or related technologies, the drawings used in the description of the embodiments of this application or related technologies will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0047] Figure 1 This is an application environment diagram of an identifier generation method for a distributed system in one embodiment.
[0048] Figure 2 This is a flowchart illustrating an identifier generation method for a distributed system in one embodiment.
[0049] Figure 3 This is a structural block diagram of an identifier generation device for a distributed system in one embodiment.
[0050] Figure 4 This is an internal structural diagram of a computer device in one embodiment. Detailed Implementation
[0051] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.
[0052] It should be noted that the terms "first," "second," etc., used in this application can be used to describe various elements, but these elements are not limited by these terms. These terms are only used to distinguish the first element from the second element. The terms "comprising" and "having," and any variations thereof, used in this application, are intended to cover non-exclusive inclusion. The term "multiple" used in this application refers to two or more. The term "and / or" used in this application refers to one of the embodiments, or any combination of multiple embodiments.
[0053] The identifier generation method for distributed systems provided in this application can be applied to, for example... Figure 1 This is illustrated in any node device within the distributed system 104. This distributed system is a computer system composed of multiple geographically dispersed or logically independent deployment nodes (including but not limited to physical servers, virtual servers, container instances, cloud hosts, etc.) interconnected via network communication links. The distributed system may include one master node device and multiple slave node devices. These node devices work collaboratively to jointly support core business services (such as transaction processing, order management, data storage, microservice clusters, etc.). Each deployment node possesses independent hardware resources (CPU, memory, storage) and a local system clock. The node's local system clock can be synchronized with an external time server via the Network Time Protocol (NTP) or can operate independently. Each node independently deploys an algorithm instance, such as the Snowflake algorithm, to locally generate a distributed unique identifier (ID). This ID serves as a globally unique identifier for business data (such as a database primary key, business serial number, message identifier, etc.).
[0054] In one exemplary embodiment, such as Figure 2 As shown, a method for generating identifiers in a distributed system is provided, which can be applied to... Figure 1 Taking any node in a distributed system as an example, the explanation includes the following steps:
[0055] S201, when an identifier generation request is triggered, obtain the current timestamp of the distributed system and the pre-stored reference timestamp; the reference timestamp is the timestamp of the last successful identifier generation.
[0056] In this embodiment, a global variable (or a cache or record in a database) can be maintained in the distributed system to store the timestamp of the most recent successful ID generation, i.e., the reference timestamp. For example, the most recently successfully generated ID may include one or more IDs. This embodiment does not limit the number of IDs generated by the reference timestamp; this embodiment only focuses on the timestamp information of the most recently successfully generated ID.
[0057] In this embodiment, each time an ID is generated, when an identifier generation request is triggered, a pre-stored reference timestamp can be obtained from the aforementioned global variables. Then, the current timestamp of the distributed system can be obtained based on the difference between the current system time and the preset start timestamp.
[0058] S202, based on the current timestamp and the reference timestamp, determines whether a clock rollback has occurred.
[0059] In this embodiment, it can be determined whether a clock rollback has occurred based on the difference between the current timestamp and the reference timestamp. For example, if the current timestamp is less than or equal to the reference timestamp, it is determined that a clock rollback has occurred.
[0060] Preferably, to avoid misjudgments and ensure the accuracy of determining whether a clock rollback has occurred, a reasonable tolerance threshold can be set, such as several hundred milliseconds or several seconds. The occurrence of a clock rollback is then determined based on this tolerance threshold. In other words, in this embodiment, as an optional implementation, the difference between a reference timestamp and a preset tolerance threshold can be obtained. If the current timestamp is less than this difference, a clock rollback is determined to have occurred, ensuring the accuracy of the determination.
[0061] S203, if it is determined that a clock rollback has occurred, process the identifier generation request based on a preset response mechanism.
[0062] In this embodiment, upon determining that a clock rollback has occurred, the identifier generation request can be processed based on a preset response mechanism. It is understood that the preset response mechanism in this embodiment is a clock rollback recovery mechanism. Furthermore, upon detecting a clock rollback, relevant logs can be recorded, including the current time, system time, and tolerance threshold, for subsequent analysis and debugging. It should be noted that the current time refers to the time when the identifier generation request was triggered, and the system time refers to the time corresponding to the local system clock of the node responding to the identifier generation request and running the target algorithm.
[0063] S204, if it is determined that no clock rollback has occurred, generate the identifier corresponding to the identifier generation request based on the target algorithm; the target algorithm is an algorithm that generates identifiers based on the system clock of the distributed system.
[0064] In this embodiment, if it is determined that no clock rollback has occurred, the current timestamp of the system is determined, and based on the current timestamp and the above target algorithm, the identifier corresponding to the identifier generation request is generated.
[0065] In the aforementioned identifier generation method for a distributed system, when an identifier generation request is triggered, the first step is to obtain the current timestamp of the distributed system and a pre-stored reference timestamp. The reference timestamp is the timestamp of the last successful identifier generation. Then, based on the current timestamp and the reference timestamp, it can be determined whether a clock rollback has occurred. If a clock rollback has occurred, the identifier generation request is processed based on a preset response mechanism. If a clock rollback has not occurred, the identifier corresponding to the identifier generation request is generated based on a target algorithm that depends on the system clock of the distributed system. In the process of generating the identifier corresponding to the identifier generation request, the occurrence of a clock rollback in the system is considered. Therefore, different methods are used to process the identifier generation request under different circumstances, ensuring the accuracy of the system clock as much as possible, thereby ensuring the accuracy of the generated identifier.
[0066] In one embodiment, as an optional implementation, processing the identifier generation request based on a preset response mechanism may include any of the following:
[0067] 1) Reject the identifier generation request.
[0068] In this embodiment, once clock rollback is detected, the identifier generation request can be immediately rejected, and an error message can be returned to the caller. While this method is relatively simple and direct, it may lead to business interruption or delay.
[0069] 2) After the system time of the distributed system returns to normal, generate the identifier corresponding to the request based on the target algorithm.
[0070] In this embodiment, upon detecting clock rollback, the response to identifier generation requests can be paused initially. After a period of time (e.g., a few seconds or minutes), the system time is rechecked. If the system time has returned to normal (i.e., greater than the reference timestamp plus a safety margin), the identifier corresponding to the identifier generation request is generated based on the target algorithm.
[0071] The target algorithm is one that generates identifiers based on the system clock of a distributed system. For target algorithms that heavily rely on system time for identifier generation, if the system time is adjusted backward (i.e., reverted), the generated identifier may be smaller than the previous one, leading to identifier conflicts or duplication. The target algorithm in this embodiment may include, but is not limited to, the snowflake algorithm. This application embodiment only uses the snowflake algorithm as an example; any algorithm that generates identifiers based on the system clock of a distributed system can be applied to this application embodiment.
[0072] Additionally, in this embodiment, it should be noted that if the system time remains abnormal after a period of waiting, the waiting and checking process can be repeated until the time returns to normal or the maximum number of retries is reached.
[0073] 3) Based on the backup time source and target algorithm, respond to the identifier generation request and generate the identifier corresponding to the identifier generation request.
[0074] In this embodiment, if the distributed system is configured with a backup time source (such as a backup list of an NTP server), it can switch to the backup time source when a clock rollback is detected from the primary time source. The backup time source is used to reacquire the current time. Based on the current time and the target algorithm described above, the system responds to the identifier generation request and generates the identifier corresponding to the identifier generation request.
[0075] In this embodiment, as a preferred implementation, the current time can be reacquired based on the backup time source, and a new current timestamp can be determined based on the reacquired current time and the preset start timestamp. If it is determined that the backup time source has not undergone clock rollback based on the new current timestamp and the reference timestamp, the identifier corresponding to the identifier generation request can be generated based on the backup time source and the target algorithm.
[0076] As an alternative implementation, it can be determined whether a clock rollback has occurred in the backup time source based on the difference between the new current timestamp and the reference timestamp. For example, if the new current timestamp is less than or equal to the reference timestamp, it is determined that a clock rollback has occurred in the backup time source.
[0077] As another optional implementation, the difference between the reference timestamp and the preset threshold can also be obtained. If the new current timestamp is less than the difference, it is determined that a clock rollback has occurred in the backup time source.
[0078] Furthermore, as an alternative implementation, if the backup time source is determined to be abnormal, more complex remedial measures may need to be taken. For example, a prompt message indicating that the backup time source is abnormal may be output to notify the system administrator to intervene.
[0079] Additionally, it should be noted that in this embodiment, regardless of the preset response mechanism used, notifications and alarms can be sent to the system administrator or relevant team. As an example, the notification content may include the clock rollback detection time, system time, preset tolerance threshold, remedial measures already taken, and whether further manual intervention is required.
[0080] In this embodiment, based on rejecting the identifier generation request, waiting for the system time of the distributed system to return to normal, generating the identifier corresponding to the identifier generation request based on the target algorithm, and responding to the identifier generation request based on the backup time source and the target algorithm, any of the preset response mechanisms can be used to process the identifier generation request. In the event of a clock rollback, the preset response mechanism can be used to remedy the clock rollback, thereby ensuring the accuracy of the identifier generated in response to the identifier generation request.
[0081] Typically, in some critical business scenarios, ID generation may be closely related to business operations, such as primary key generation in database transactions. In these scenarios, if a clock rollback occurs during identifier generation, a rollback or retry mechanism may be needed to revert to the previous stable state. Building upon the above embodiments, in one embodiment, the method further includes: if the generated identifier is identical to a previously generated identifier, a business rollback mechanism is triggered to roll back the current business operation to the stable state before identifier generation.
[0082] In this embodiment, if a clock rollback is detected, and the generated identifier is identical to a previously generated identifier after the system time returns to normal, a business rollback mechanism can be triggered to roll back the current business operation to a stable state before the identifier was generated. For example, in the generation of primary keys in a database transaction, if a clock rollback is detected causing the generated identifier to be identical to a previously generated identifier, the operation can be rolled back to the previous stable state (such as a database transaction rollback) and the business operation can be retried. In addition, during the rollback process, it is also necessary to ensure the consistency and integrity of the data.
[0083] In this embodiment, if the generated identifier is the same as the historically generated identifier, the business rollback mechanism is triggered to roll back the current business operation to the stable state before the identifier was generated. This can avoid business anomalies caused by identifier duplication, ensure data consistency and business continuity in critical scenarios, and reduce manual intervention in business.
[0084] To facilitate understanding by those skilled in the art, the identifier generation method for distributed systems provided in this disclosure is described in detail below. This method may include:
[0085] S1, when an identifier generation request is triggered, obtain the current timestamp of the distributed system and the pre-stored reference timestamp; the reference timestamp is the timestamp of the last successful identifier generation.
[0086] S2, obtain the difference between the reference timestamp and the preset tolerance threshold.
[0087] S3 determines that a clock rollback has occurred if the current timestamp is less than the difference.
[0088] S4, if a clock rollback is determined to have occurred, process the identifier generation request based on a preset response mechanism, wherein processing the identifier generation request based on the preset response mechanism includes any one of the following:
[0089] The identifier generation request was rejected.
[0090] After the system time of the distributed system returns to normal, generate the identifier corresponding to the request based on the target algorithm;
[0091] Based on the backup time source, a new current timestamp is determined. If, based on the new current timestamp and the reference timestamp, it is determined that no clock rollback has occurred at the backup time source, an identifier corresponding to the generation request is generated based on the backup time source and the target algorithm.
[0092] S5. If the generated identifier is the same as the previously generated identifier in response to the identifier generation request, the business rollback mechanism is triggered to roll back the current business operation to the stable state before the identifier was generated.
[0093] It should be noted that the descriptions of the above steps can be found in the relevant descriptions in the above embodiments, and their effects are similar, so they will not be repeated here.
[0094] It should be understood that although the steps in the flowcharts of the above embodiments are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowcharts of the above embodiments may include multiple steps or multiple stages. These steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the steps or stages in other steps. It is understood that the steps in different embodiments can be freely combined as needed, and all non-contradictory solutions formed by such combinations are within the scope of protection of this application.
[0095] Based on the same inventive concept, this application also provides an identifier generation apparatus for a distributed system to implement the identifier generation method for the distributed system described above. The solution provided by this apparatus is similar to the implementation described in the above method; therefore, the specific limitations in one or more embodiments of the identifier generation apparatus for a distributed system provided below can be found in the limitations of the identifier generation method for a distributed system described above, and will not be repeated here.
[0096] In one exemplary embodiment, such as Figure 3 As shown, an identifier generation device for a distributed system is provided, comprising: an acquisition module, a determination module, a first processing module, and a second processing module, wherein:
[0097] The acquisition module is used to obtain the current timestamp and the pre-stored reference timestamp of the distributed system when an identifier generation request is triggered; the reference timestamp is the timestamp of the last successful identifier generation.
[0098] The determination module is used to determine whether a clock rollback has occurred based on the current timestamp and the reference timestamp.
[0099] The first processing module is used to process the identifier generation request based on a preset response mechanism when it is determined that a clock rollback has occurred.
[0100] The second processing module is used to generate an identifier corresponding to the identifier generation request based on the target algorithm, provided that no clock rollback has occurred. The target algorithm is an algorithm that generates identifiers based on the system clock of the distributed system.
[0101] The identifier generation device for the distributed system provided in this embodiment can execute the above method embodiment, and its implementation principle and technical effect are similar, so they will not be described again here.
[0102] Based on the above embodiments, optionally, the first processing module is used to perform any one of the following steps:
[0103] The identifier generation request was rejected.
[0104] After the system time of the distributed system returns to normal, generate the identifier corresponding to the request based on the target algorithm;
[0105] Based on the backup time source and target algorithm, respond to the identifier generation request and generate the identifier corresponding to the identifier generation request.
[0106] The identifier generation device for the distributed system provided in this embodiment can execute the above method embodiment, and its implementation principle and technical effect are similar, so they will not be described again here.
[0107] Optionally, based on the above embodiments, the apparatus further includes: a rollback module, wherein:
[0108] The rollback module is used to trigger a business rollback mechanism if the generated identifier is the same as a previously generated identifier, and roll back the current business operation to the stable state before the identifier was generated.
[0109] The identifier generation device for the distributed system provided in this embodiment can execute the above method embodiment, and its implementation principle and technical effect are similar, so they will not be described again here.
[0110] Based on the above embodiments, optionally, the first processing module includes: a first processing unit, wherein:
[0111] The first processing unit is used to determine a new current timestamp based on the backup time source; and, if it is determined based on the new current timestamp and the reference timestamp that no clock rollback has occurred at the backup time source, it generates an identifier based on the backup time source and the target algorithm.
[0112] The identifier generation device for the distributed system provided in this embodiment can execute the above method embodiment, and its implementation principle and technical effect are similar, so they will not be described again here.
[0113] Based on the above embodiments, optionally, the above device further includes: an output module, wherein:
[0114] The output module is used to output a message indicating that the backup time source is abnormal if it is determined to be abnormal based on the new current timestamp and the reference timestamp.
[0115] The identifier generation device for the distributed system provided in this embodiment can execute the above method embodiment, and its implementation principle and technical effect are similar, so they will not be described again here.
[0116] Based on the above embodiments, optionally, the determining module includes: an acquisition unit and a determining unit, wherein:
[0117] The acquisition unit is used to obtain the difference between the reference timestamp and the preset tolerance threshold.
[0118] The determination unit is used to determine if a clock rollback has occurred when the current timestamp is less than the difference.
[0119] The identifier generation device for the distributed system provided in this embodiment can execute the above method embodiment, and its implementation principle and technical effect are similar, so they will not be described again here.
[0120] Each module in the identifier generation device of the aforementioned distributed system can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in or independent of the processor in a computer device, or stored in the memory of a computer device in software form, so that the processor can call and execute the operations corresponding to each module.
[0121] In one exemplary embodiment, a computer device is provided, which may be a server, and its internal structure diagram may be as follows: Figure 4 As shown, this computer device includes a processor, memory, input / output interfaces (I / O), and a communication interface. The processor, memory, and I / O interfaces are connected via a system bus, and the communication interface is also connected to the system bus via the I / O interfaces. The processor provides computational and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system, computer programs, and a database. The internal memory provides the environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The database stores reference timestamps. The I / O interfaces are used for exchanging information between the processor and external devices. The communication interface is used for communication with external terminals via a network connection. When executed by the processor, the computer program implements a distributed system identifier generation method.
[0122] Those skilled in the art will understand that Figure 4 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.
[0123] In one exemplary embodiment, a computer device is provided, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to perform the following steps:
[0124] When an identifier generation request is triggered, the current timestamp of the distributed system and the pre-stored reference timestamp are obtained; the reference timestamp is the timestamp of the last successful identifier generation.
[0125] Based on the current timestamp and the reference timestamp, determine whether a clock rollback has occurred;
[0126] If a clock rollback is determined to have occurred, the identifier generation request will be processed based on a preset response mechanism.
[0127] Assuming no clock rollback has occurred, generate the identifier corresponding to the identifier generation request based on the target algorithm; the target algorithm is an algorithm that generates identifiers based on the system clock of the distributed system.
[0128] In one embodiment, the processor, while executing the computer program, also performs any one of the following steps:
[0129] The identifier generation request was rejected.
[0130] After the system time of the distributed system returns to normal, generate the identifier corresponding to the request based on the target algorithm;
[0131] Based on the backup time source and target algorithm, respond to the identifier generation request and generate the identifier corresponding to the identifier generation request.
[0132] In one embodiment, the processor, when executing a computer program, also performs the following steps:
[0133] If the generated identifier is the same as a previously generated identifier, the business rollback mechanism will be triggered to roll back the current business operation to the stable state before the identifier was generated.
[0134] In one embodiment, the processor, when executing a computer program, also performs the following steps:
[0135] Determine a new current timestamp based on the backup time source;
[0136] Based on the new current timestamp and reference timestamp, and assuming that no clock rollback has occurred at the backup time source, an identifier is generated based on the backup time source and the target algorithm.
[0137] In one embodiment, the processor, when executing a computer program, also performs the following steps:
[0138] If the backup time source is determined to be faulty based on the new current timestamp and reference timestamp, a message indicating that the backup time source is faulty will be output.
[0139] In one embodiment, the processor, when executing a computer program, also performs the following steps:
[0140] Obtain the difference between the reference timestamp and the preset tolerance threshold;
[0141] If the current timestamp is less than the difference, a clock rollback is determined to have occurred.
[0142] In one embodiment, a computer-readable storage medium is provided having a computer program stored thereon, the computer program performing the following steps when executed by a processor:
[0143] When an identifier generation request is triggered, the current timestamp of the distributed system and the pre-stored reference timestamp are obtained; the reference timestamp is the timestamp of the last successful identifier generation.
[0144] Based on the current timestamp and the reference timestamp, determine whether a clock rollback has occurred;
[0145] If a clock rollback is determined to have occurred, the identifier generation request will be processed based on a preset response mechanism.
[0146] Assuming no clock rollback has occurred, generate the identifier corresponding to the identifier generation request based on the target algorithm; the target algorithm is an algorithm that generates identifiers based on the system clock of the distributed system.
[0147] In one embodiment, when the computer program is executed by a processor, it also performs any one of the following steps:
[0148] The identifier generation request was rejected.
[0149] After the system time of the distributed system returns to normal, generate the identifier corresponding to the request based on the target algorithm;
[0150] Based on the backup time source and target algorithm, respond to the identifier generation request and generate the identifier corresponding to the identifier generation request.
[0151] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:
[0152] If the generated identifier is the same as a previously generated identifier, the business rollback mechanism will be triggered to roll back the current business operation to the stable state before the identifier was generated.
[0153] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:
[0154] Determine a new current timestamp based on the backup time source;
[0155] Based on the new current timestamp and reference timestamp, and assuming that no clock rollback has occurred at the backup time source, an identifier is generated based on the backup time source and the target algorithm.
[0156] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:
[0157] If the backup time source is determined to be faulty based on the new current timestamp and reference timestamp, a message indicating that the backup time source is faulty will be output.
[0158] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:
[0159] Obtain the difference between the reference timestamp and the preset tolerance threshold;
[0160] If the current timestamp is less than the difference, a clock rollback is determined to have occurred.
[0161] In one embodiment, a computer program product is provided, including a computer program that, when executed by a processor, performs the following steps:
[0162] When an identifier generation request is triggered, the current timestamp of the distributed system and the pre-stored reference timestamp are obtained; the reference timestamp is the timestamp of the last successful identifier generation.
[0163] Based on the current timestamp and the reference timestamp, determine whether a clock rollback has occurred;
[0164] If a clock rollback is determined to have occurred, the identifier generation request will be processed based on a preset response mechanism.
[0165] Assuming no clock rollback has occurred, generate the identifier corresponding to the identifier generation request based on the target algorithm; the target algorithm is an algorithm that generates identifiers based on the system clock of the distributed system.
[0166] In one embodiment, when the computer program is executed by a processor, it also performs any one of the following steps:
[0167] The identifier generation request was rejected.
[0168] After the system time of the distributed system returns to normal, generate the identifier corresponding to the request based on the target algorithm;
[0169] Based on the backup time source and target algorithm, respond to the identifier generation request and generate the identifier corresponding to the identifier generation request.
[0170] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:
[0171] If the generated identifier is the same as a previously generated identifier, the business rollback mechanism will be triggered to roll back the current business operation to the stable state before the identifier was generated.
[0172] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:
[0173] Determine a new current timestamp based on the backup time source;
[0174] Based on the new current timestamp and reference timestamp, and assuming that no clock rollback has occurred at the backup time source, an identifier is generated based on the backup time source and the target algorithm.
[0175] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:
[0176] If the backup time source is determined to be faulty based on the new current timestamp and reference timestamp, a message indicating that the backup time source is faulty will be output.
[0177] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:
[0178] Obtain the difference between the reference timestamp and the preset tolerance threshold;
[0179] If the current timestamp is less than the difference, a clock rollback is determined to have occurred.
[0180] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, data stored, data displayed, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of the relevant data must comply with relevant regulations.
[0181] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. Any references to memory, databases, or other media used in the embodiments provided in this application can include at least one of non-volatile memory and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can take many forms, such as Static Random Access Memory (SRAM) or Dynamic Random Access Memory (DRAM). The databases involved in the embodiments provided in this application may include at least one type of relational database and non-relational database. Non-relational databases may include, but are not limited to, blockchain-based distributed databases. The processors involved in the embodiments provided in this application may be general-purpose processors, central processing units, graphics processing units, digital signal processors, programmable logic devices, quantum computing-based data processing logic devices, artificial intelligence (AI) processors, etc., and are not limited to these.
[0182] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this application.
[0183] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are specific and detailed, they should not be construed as limiting the scope of this patent application. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this application should be determined by the appended claims.
Claims
1. A method of generating an identifier for a distributed system, the method comprising: The method comprises: when triggering an identifier generation request, obtaining a current timestamp of a distributed system and a pre-stored reference timestamp; the reference timestamp is a timestamp of last successful identifier generation; based on the current timestamp and the reference timestamp, determining whether clock rollback occurs; in a case where it is determined that clock rollback occurs, processing the identifier generation request based on a preset response mechanism; in a case where it is determined that clock rollback does not occur, generating an identifier corresponding to the identifier generation request based on a target algorithm; the target algorithm is an algorithm for generating an identifier depending on a system clock of the distributed system.
2. The method of claim 1, wherein, The processing of the identifier generation request based on the preset response mechanism comprises any one of the following: rejecting the identifier generation request; after waiting for the system time of the distributed system to recover to normal, generating an identifier corresponding to the identifier generation request based on the target algorithm; based on a backup time source and the target algorithm, responding to the identifier generation request to generate an identifier corresponding to the identifier generation request.
3. The method of claim 2, wherein, The method further comprises: if the generated identifier is repeated with a historically generated identifier, triggering a business rollback mechanism to roll back a current business operation to a stable state before identifier generation.
4. The method of claim 2, wherein, The generation of the identifier corresponding to the identifier generation request based on the backup time source and the target algorithm comprises: determining a new current timestamp based on the backup time source; in a case where it is determined that the backup time source does not have clock rollback based on the new current timestamp and the reference timestamp, generating the identifier based on the backup time source and the target algorithm.
5. The method of claim 4, wherein, The method further comprises: if it is determined that the backup time source is abnormal based on the new current timestamp and the reference timestamp, outputting prompt information that the backup time source is abnormal.
6. The method of claim 1, wherein, The determination of whether clock rollback occurs based on the current timestamp and the reference timestamp comprises: obtaining a difference between the reference timestamp and a preset tolerance threshold; in a case where the current timestamp is less than the difference, determining that clock rollback occurs.
7. An identifier generation apparatus of a distributed system, characterized by comprising: The apparatus comprises: an obtaining module configured to, when triggering an identifier generation request, obtain a current timestamp of a distributed system and a pre-stored reference timestamp; the reference timestamp is a timestamp of last successful identifier generation; a determining module configured to, based on the current timestamp and the reference timestamp, determine whether clock rollback occurs; a first processing module configured to, in a case where it is determined that clock rollback occurs, process the identifier generation request based on a preset response mechanism; a second processing module configured to, in a case where it is determined that clock rollback does not occur, generate an identifier corresponding to the identifier generation request based on a target algorithm; the target algorithm is an algorithm for generating an identifier depending on a system clock of the distributed system.
8. A computer device comprising a memory and a processor, the memory storing a computer program, characterized in that, The processor executes the computer program to implement the steps of the method in any one of claims 1 to 6.
9. A 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 method in any one of claims 1 to 6.
10. A computer program product comprising a computer program, characterized in that, The computer program, which is executed by a processor, implements the steps of the method according to any one of claims 1 to 6.