Database global identifier generation method and apparatus, electronic device, and storage medium
By generating a global identifier by updating the sequence number and machine number in the distributed database, the problems of poor global uniqueness and insufficient availability are solved, achieving higher concurrency and availability, and avoiding machine clock dependence.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- JINZHUAN INFORMATION TECHNOLOGY CO LTD
- Filing Date
- 2022-12-27
- Publication Date
- 2026-07-03
AI Technical Summary
In existing technologies, distributed database globally unique identifier generation methods suffer from poor global uniqueness, poor concurrency, and poor availability. In particular, the SnowFlake algorithm relies on machine clocks, leading to duplicate identifier generation and insufficient UUID availability.
By obtaining the identifier of the business thread, the request and historical global identifiers are obtained. It is determined whether the sequence number has not been assigned. Based on the determination result, the sequence number and machine number are updated to generate a global identifier in the database. The machine number is used to ensure global uniqueness and avoid dependence on the machine clock.
It improves the concurrency and global uniqueness of distributed databases, enhances the availability of identifiers, and addresses the shortcomings of the SnowFlake algorithm and UUID, thereby achieving improvements in global uniqueness, concurrency, and availability.
Smart Images

Figure CN115982188B_ABST
Abstract
Description
Technical Field
[0001] This application relates to database technology, and more particularly to a method, apparatus, electronic device, and storage medium for generating a global database identifier. Background Technology
[0002] A distributed database connects multiple physically dispersed nodes to form a logically unified database. Data is divided into multiple subsets and distributed across different nodes, distributing the load on a single node to multiple nodes to achieve read and write capacity expansion. However, this makes it difficult for a distributed database to generate globally unique identifiers.
[0003] In existing technologies, there are two main methods for generating globally unique identifiers for distributed databases. One is the SnowFlake algorithm (a technical term, a database unique identifier generation algorithm), which consists of timestamp information, machine identifier, sequence number, and sign bit. The other is UUID (Universally Unique Identifier), which contains 32 hexadecimal digits, divided into five segments by hyphens.
[0004] The SnowFlake algorithm is heavily reliant on the machine clock. If the machine clock of the machine executing the job service is inconsistent with the standard clock, the machine clock needs to be rolled back. This can lead to the generation of duplicate identifiers, poor global uniqueness, and poor parallelism. Furthermore, UUID exceeds the length of the basic data type in most mainstream programming languages, making it unsuitable for many scenarios and resulting in poor usability. Summary of the Invention
[0005] This application provides a method, apparatus, electronic device, and storage medium for generating global identifiers in a database, to enhance the global uniqueness, concurrency, and availability of global identifiers in a database.
[0006] In a first aspect, embodiments of this application provide a method for generating a global database identifier, the method comprising:
[0007] The identifier retrieval request generated when the business thread has a database access requirement is used to obtain the global identifier of the database, and the historical global identifier of the database obtained by the business thread;
[0008] Determine if there are any unassigned sequence numbers for the business threads in the historical global identifier;
[0009] Based on the judgment result, control the update of the business thread's sequence number and machine number;
[0010] Generate a global database identifier based on the updated serial number, machine number, and thread identifier of the business thread.
[0011] Secondly, embodiments of this application also provide a database global identifier generation apparatus, which includes:
[0012] The request retrieval module is used to retrieve the identifier retrieval request generated by the business thread when it has a database access requirement. This request is used to retrieve the global identifier of the database, and the historical global identifier of the database retrieved by the business thread.
[0013] The serial number determination module is used to determine whether there are any unassigned serial numbers for the business threads in the historical global identifiers;
[0014] The identifier update module is used to control the update of the sequence number and machine number of the business thread based on the judgment result.
[0015] The identifier generation module is used to generate a global database identifier based on the updated sequence number, machine number, and thread identifier of the business thread.
[0016] Thirdly, embodiments of this application also provide an electronic device, which includes:
[0017] One or more processors;
[0018] Storage device for storing one or more programs;
[0019] When one or more programs are executed by one or more processors, the one or more processors implement any of the database global identifier generation methods provided in the embodiments of this application.
[0020] Fourthly, embodiments of this application also provide a storage medium including computer-executable instructions, which, when executed by a computer processor, are used to perform any of the database global identifier generation methods provided in embodiments of this application.
[0021] This application obtains an identifier retrieval request generated when a business thread has a database access requirement, used to obtain a global database identifier, and the historical global database identifier obtained by the business thread. It then determines whether there are any unassigned sequence numbers for the sequence number corresponding to the business thread in the historical global identifier. Based on the determination result, it updates the sequence number and machine number of the business thread. Finally, it generates a global database identifier based on the updated sequence number, machine number, and thread identifier of the business thread. By maintaining a set of sequence numbers, the business thread can improve the concurrency of the distributed database. The machine number ensures global uniqueness. Generating the global database identifier based on the updated sequence number, machine number, and thread identifier of the business thread does not depend on the machine clock, ensuring global uniqueness. The structure is simple and highly available. Therefore, the technical solution of this application solves the problems of the SnowFlake algorithm, which heavily relies on the machine clock, leading to duplicate identifier generation, poor global uniqueness, poor parallelism, and poor UUID availability. It achieves the effect of enhancing the global uniqueness, concurrency, and availability of the global database identifier. Attached Figure Description
[0022] Figure 1 This is a flowchart of a database global identifier generation method according to Embodiment 1 of this application;
[0023] Figure 2 This is a flowchart of a database global identifier generation method according to Embodiment 2 of this application;
[0024] Figure 3 This is a flowchart of a database global identifier generation method according to Embodiment 3 of this application;
[0025] Figure 4 This is a schematic diagram of the structure of a database global identifier generation device according to Embodiment 4 of this application;
[0026] Figure 5 This is a schematic diagram of the structure of an electronic device according to Embodiment 5 of this application. Detailed Implementation
[0027] To enable those skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present application, and not all embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative effort should fall within the scope of protection of the present application.
[0028] It should be noted that the terms "first" and "second," etc., in the specification, claims, and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this application described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0029] Example 1
[0030] Figure 1 This is a flowchart of a database global identifier generation method provided in Embodiment 1 of this application. This embodiment can be applied to the situation of generating globally unique identifiers for the execution threads of each data node in a distributed database. The method can be executed by a database global identifier generation device, which can be implemented in software and / or hardware and specifically configured in the data nodes of the distributed database, such as a server.
[0031] See Figure 1 The method for generating a global database identifier, as shown, specifically includes the following steps:
[0032] S110. Obtain the identifier retrieval request generated when the business thread has a database access requirement, which is used to obtain the global identifier of the database, and the historical global identifier of the database obtained by the business thread.
[0033] A business thread can be a thread that implements a business function within a data node of a distributed database. A globally unique identifier is a unique identifier across the entire distributed database. For example, a globally unique identifier can be an `unsigned long long` value. During execution, a business thread requires at least one database globally unique identifier to uniquely identify the thread, its data, or messages. When a business thread needs to access the database, it generates an identifier retrieval request to obtain the database globally unique identifier and sends this request to the database node it needs to access. Specifically, the identifier retrieval request includes the number of identifiers to be retrieved, as well as the historical globally unique identifiers retrieved by the business thread. The historical globally unique identifier can be the last globally unique identifier retrieved by the business thread before sending the identifier retrieval request.
[0034] S120. Determine whether there is an unassigned sequence number for the sequence number corresponding to the business thread in the historical global identifier.
[0035] The sequence number can be a set of numbers maintained for each business thread, used to generate a global identifier. Unassigned sequence numbers can be those that have not been assigned in the historical global identifier. Specifically, the sequence number can be incremented by 1 each time it is assigned. The maximum sequence number can be determined based on the number of bits in the sequence number. When the sequence number corresponding to a business thread in the historical global identifier is less than the maximum sequence number, it can be determined that there are unassigned sequence numbers; when the sequence number corresponding to a business thread in the historical global identifier is equal to the maximum sequence number, it can be determined that there are no unassigned sequence numbers. The number of bits in the sequence number can be preset according to requirements. For example, a 32-bit sequence number would result in approximately 4.29 billion sequences.
[0036] S130. Based on the judgment result, control the update of the serial number and machine number of the business thread.
[0037] The machine number can serve as a globally unique identifier. Specifically, the machine number can be generated by the GTM (Global Transaction Management) component in the distributed database. This ensures the machine number is globally unique and allows for the parallel maintenance of machine numbers for multiple different business threads. Furthermore, the generated machine number can be persisted to disk, and a restart of the distributed database system will not affect the generation of the global identifier.
[0038] Based on the determination of whether unassigned sequence numbers exist, the update method for the business thread's sequence number and machine number is determined. Specifically, if no unassigned sequence numbers exist, a new machine number needs to be obtained. Since the machine number has changed, the sequence number can remain the same as before; for example, the sequence number can restart from 0 and increment. If unassigned sequence numbers exist, a new machine number does not need to be obtained. In this case, the machine number remains unchanged, and the sequence number can increment according to a preset rule; for example, the sequence number is incremented by 1.
[0039] S140. Generate a global database identifier based on the updated serial number, machine number, and thread identifier of the business thread.
[0040] The thread identifier for a business thread can be a unique identifier. The number of bits in the globally unique identifier can be set according to actual needs. For example, the number of bits in the globally unique identifier can be based on the length of the basic data types in the programming language; or it can be set according to the application domain. For example, the application domain can be: database, distributed database, middleware, distributed middleware, and distributed storage, etc. For example, the globally unique identifier can be 64 bits, consisting of a 32-bit sequence number, an 11-bit thread identifier, and a 21-bit machine number.
[0041] The updated sequence number, machine number, and thread identifier of the business thread are combined in a preset order to generate a global database identifier. The order of the sequence number, machine number, and thread identifier can be adjusted according to actual needs, and this application does not impose a specific limitation on it.
[0042] With the rapid development of digital technology, databases, as the underlying infrastructure, are facing new challenges. Traditional centralized databases, due to their single point of failure, are increasingly unable to meet the needs of digital development. Distributed databases, with their horizontal scalability, high availability, and strong consistency, can resolve the contradiction between the explosive growth of data volume and the limited capacity and performance of traditional centralized databases. Compared to centralized databases, distributed databases connect physically dispersed nodes to form a logically unified database. Data is divided into multiple subsets and distributed across different nodes, distributing the load on a single node across multiple nodes to achieve scalable read and write capabilities. However, this also presents a challenge in generating a global identifier.
[0043] During code execution tracing, a unique value is needed to associate with the corresponding business operation; this is the purpose of a global identifier. For centralized databases, a unique global identifier can be achieved by adding an auto-incrementing identifier or timestamp to the database's own incrementing value. However, for distributed systems with different applications, nodes, clusters, and data centers, the generation method used for centralized databases cannot be used.
[0044] In existing technologies, there are two main methods for generating globally unique identifiers (GUIDs) in distributed databases. One method is the SnowFlake algorithm, which generates a long (a technical term, the name of a data type) type GUID, consisting of a 41-bit timestamp, a 10-bit machine identifier, a 12-bit sequence number, and a 1-bit sign bit, totaling 64 bits. The SnowFlake algorithm is heavily reliant on the machine clock; if the machine clock of the server executing the job is inconsistent with the standard clock, a clock rollback is required, leading to duplicate GUID generation. The other method is UUID, whose standard structure contains 32 hexadecimal digits, divided into five segments by hyphens, in the form of 8-4-4-4-12 (36 characters, 16 bytes, 128 bits). This exceeds the length of basic data types in most mainstream programming languages, making it unsuitable for many scenarios.
[0045] The technical solution of this embodiment obtains the identifier acquisition request generated when a business thread has a database access requirement, used to obtain the database global identifier, and the historical global identifier of the database obtained by the business thread; determines whether there is an unassigned sequence number for the sequence number corresponding to the business thread in the historical global identifier; based on the determination result, controls the updating of the sequence number and machine number of the business thread; and generates the database global identifier based on the updated sequence number, machine number, and thread identifier of the business thread. By maintaining a set of sequence numbers, the business thread can improve the concurrency of the distributed database, ensure global uniqueness through the machine number, and generate the database global identifier based on the updated sequence number, machine number, and thread identifier of the business thread, without relying on the machine clock, ensuring global uniqueness, with a simple structure and strong availability. Therefore, the technical solution of this application solves the problems of duplicate identifier generation, poor global uniqueness, poor parallelism, and poor UUID availability caused by the SnowFlake algorithm's strong reliance on the machine clock, achieving the effect of enhancing the global uniqueness, concurrency, and availability of the database global identifier.
[0046] Example 2
[0047] Figure 2 This is a flowchart of a database global identifier generation method provided in Embodiment 2 of this application. The technical solution of this embodiment is further refined based on the above technical solution.
[0048] Furthermore, the statement "based on the judgment result, control the update of the sequence number and machine number of the business thread" is refined to: "If there is no unallocated sequence number, update the machine number of the business thread through the global transaction management component; set the sequence number of the business thread to zero, and increment the sequence number after setting it to zero to update the sequence number".
[0049] Accordingly, "generate a global database identifier based on the updated serial number, machine number, and thread identifier of the business thread" is refined to "generate a global database identifier based on the updated serial number, machine number, and thread identifier of the business thread" to generate a global database identifier.
[0050] See Figure 2 The method for generating a global identifier for a database, as shown, includes:
[0051] S210. Obtain the identifier retrieval request generated when the business thread has a database access requirement, which is used to obtain the global identifier of the database, and the historical global identifier of the database obtained by the business thread.
[0052] S220. Determine whether there is an unassigned sequence number for the sequence number corresponding to the business thread in the historical global identifier.
[0053] S230. If there is no unassigned sequence number, update the machine number of the business thread through the global transaction management component.
[0054] The global transaction management component is used to generate machine numbers in the distributed database. It can guarantee that the machine number is unique globally, maintain multiple machine numbers for different threads in parallel, and the machine number generated by the global transaction management component can be persisted to disk. When the system restarts, it will not affect the generation of the machine number.
[0055] If there are no unassigned sequence numbers, meaning all sequence numbers corresponding to the current machine number have been used, then in order to ensure the uniqueness of the generated global identifier, the machine number of the business thread needs to be updated through the global transaction management component to guarantee the uniqueness of the global identifier.
[0056] In an optional implementation instance, updating the machine number of the business thread through the global transaction management component includes: if the machine number of the business thread is the same as the first machine number in the local machine number thread, then updating the machine number of the business thread according to the second machine number in the machine number thread; the first machine number and the second machine number are generated by the global transaction management component; and the first machine number and the second machine number in the machine number thread are updated respectively through the global transaction management component.
[0057] The first machine number is a machine number stored in the local machine number thread, used to update the machine number of the business thread. The second machine number is also a machine number stored in the local machine number thread, used to update the first machine number. The local machine number thread is a thread maintained by the database node where the business thread resides, used to maintain the two machine numbers obtained from the global transaction management component, in order to improve the response speed to the business thread obtaining the machine number. Specifically, when the data node of the distributed data starts, the local machine number thread in the data node obtains two machine numbers from the global transaction management component, stores them as the second machine number and the first machine number respectively, and enters a waiting state. The generation time of the first machine number is before the generation time of the second machine number.
[0058] If the machine number of the business thread is the same as the first machine number in the local machine number thread, it means that the first machine number has already been used by the business thread. Therefore, the machine number of the business thread is updated according to the second machine number in the machine number thread. After the business thread obtains the second machine number, both machine numbers maintained in the machine number thread have been used. At this time, the machine number thread obtains a new machine number through the global transaction management component and updates the first and second machine numbers in the machine number thread.
[0059] If the machine number of the business thread is the same as the first machine number in the local machine number thread, the machine number of the business thread is updated according to the second machine number in the machine number thread to ensure that the machine number is not reused. The first and second machine numbers are generated by the global transaction management component to ensure the global uniqueness of the machine number. The global transaction management component updates the first and second machine numbers in the machine number thread respectively to prepare for the business thread to obtain the machine number next time, thereby improving the efficiency of the business thread in obtaining the machine number.
[0060] In an optional implementation instance, updating the machine number of the business thread through the global transaction management component includes: if the machine number of the business thread is different from the first machine number in the machine number thread, then updating the machine number of the business thread according to the first machine number in the machine number thread.
[0061] If the machine number of the business thread is different from the first machine number in the machine number thread, it means that the first machine number is not being used by the business thread. Therefore, the machine number of the business thread is updated according to the first machine number in the machine number thread.
[0062] It should be noted that after updating the machine number of the business thread based on the first machine number in the machine number thread, it is not necessary to update the first and second machine numbers in the machine number thread. This allows other business threads to still obtain the first machine number when they obtain the machine number, thus improving the utilization efficiency of the first machine number.
[0063] If the machine number of the business thread is different from the first machine number in the machine number thread, the machine number of the business thread is updated according to the first machine number in the machine number thread. This allows the first machine number to be obtained directly, thus improving the efficiency of machine number updates.
[0064] S240. Set the sequence number of the business thread to zero, and increment the sequence number after setting it to zero to update the sequence number.
[0065] Because the sequence number was already at its maximum value before the new machine number was obtained, the sequence number of the business thread is set to zero after the machine number is updated. This allows the new machine number to be used to generate a new global identifier corresponding to a new set of sequence numbers. Increasing the value of the zeroed sequence number can be done by incrementing it by 1 whenever there is a need to obtain the global identifier.
[0066] S250. Generate a global database identifier based on the updated serial number, the updated machine number, and the thread identifier of the business thread.
[0067] Based on the updated serial number, the updated machine number, and the thread identifier of the business thread, a global database identifier is generated in a preset combination order.
[0068] The technical solution of this embodiment updates the machine number of the business thread through the global transaction management component if no unassigned sequence number exists. When no unassigned sequence number exists, a new machine number is obtained, ensuring that the combination of machine number and sequence number is unique, thus guaranteeing the uniqueness of the global identifier. The business thread's sequence number is set to zero, and an auto-incrementing value is applied to the zeroed sequence number to update it. After updating the machine number, a set of auto-incrementing sequence numbers is maintained to improve the concurrency of global identifier generation. Based on the updated sequence number, the updated machine number, and the business thread's thread identifier, a global identifier is generated in the database, ensuring the uniqueness of the global identifier.
[0069] In one optional embodiment, based on the judgment result, the sequence number and machine number of the business thread are updated, including: if there is an unallocated sequence number, the sequence number corresponding to the business thread is incremented by a preset step size to update the sequence number; correspondingly, a global database identifier is generated based on the updated sequence number, machine number and thread identifier of the business thread, including: generating a global database identifier based on the updated sequence number, machine number and thread identifier of the business thread.
[0070] If unassigned sequence numbers exist, meaning the sequence numbers corresponding to the current machine number have not all been used, the sequence number corresponding to the business thread is incremented by a preset step size to update the sequence number. The preset step size can be a pre-defined increment value; generally, to fully utilize the sequence numbers, the preset step size is set to 1 for easier calculation and improved efficiency in generating the global identifier. If unassigned sequence numbers exist, there is no need to update the machine number; therefore, a database global identifier is generated based on the updated sequence number, machine number, and the thread identifier of the business thread.
[0071] If an unassigned sequence number exists, the sequence number corresponding to the business thread is incremented by a preset step size to update the sequence number, which allows for rapid sequence number updates. Correspondingly, a global database identifier is generated based on the updated sequence number, machine number, and thread identifier of the business thread, ensuring the uniqueness of the global database identifier.
[0072] Example 3
[0073] Figure 3 This is a flowchart of a database global identifier generation method provided in Embodiment 3 of this application. The technical solution of this embodiment is further refined based on the above technical solution.
[0074] Furthermore, the statement "If the machine number of the business thread is different from the first machine number in the machine number thread, then update the machine number of the business thread according to the first machine number in the machine number thread" is refined to: "Generate a new machine number through the global transaction management component; update the first machine number according to the second machine number through the machine number thread, and update the second machine number according to the new machine number" to update the machine number.
[0075] See Figure 3 The method for generating a global identifier for a database, as shown, includes:
[0076] S310. Obtain the identifier retrieval request generated when the business thread has a database access requirement, which is used to obtain the global identifier of the database, and the historical global identifier of the database obtained by the business thread.
[0077] S320. Determine whether there is an unassigned sequence number for the sequence number corresponding to the business thread in the historical global identifier.
[0078] S330. If the machine number of the business thread is the same as the first machine number in the local machine number thread, then update the machine number of the business thread according to the second machine number in the machine number thread; the first machine number and the second machine number are generated by the global transaction management component.
[0079] S340. Generate a new machine number through the global transaction management component.
[0080] If the machine number of the business thread is the same as the first machine number in the local machine number thread, the machine number thread changes from the waiting state to the starting state and sends a retrieval request to the global transaction management component. The global transaction management component generates a new machine number and sends it to the machine number thread.
[0081] In one optional embodiment, generating a new machine number through a global transaction management component includes: incrementing the second machine number by an auto-incrementing value through the global transaction management component to obtain a new machine number.
[0082] The global transaction management component receives a new machine number retrieval request from the machine number thread, performs an auto-increment operation on the second machine number, and uses the incremented result as the new machine number. Specifically, the global transaction management component can maintain an auto-increment sequence of machine numbers. When a new machine number retrieval request arrives, the global transaction management component increments the previously sent machine number by 1, sends the result as the new machine number to the business thread, and updates its own machine number sending record. It should be noted that the auto-increment value of the second machine number performed by the global transaction management component can be different each time.
[0083] By using the global transaction management component to auto-increment the second machine number to obtain a new machine number, new machine numbers can be generated quickly. The logic is simple, easy to implement, and will not result in duplicates, thus improving the efficiency of generating new machine numbers.
[0084] S350: The machine number thread updates the first machine number based on the second machine number, and updates the second machine number based on the new machine number.
[0085] The machine number thread assigns the second machine number to the first machine number, updates the first machine number, assigns the new machine number to the second machine number, and updates the second machine number. This way, when subsequent business threads retrieve the machine number, they compare the first and second machine numbers sequentially, ensuring that both machine numbers are used.
[0086] S360. Set the serial number of the business thread to zero and increment the serial number after setting it to zero to update the serial number.
[0087] S370. Generate a global database identifier based on the updated serial number, the updated machine number, and the thread identifier of the business thread.
[0088] The technical solution of this embodiment generates a new machine number through a global transaction management component, ensuring the global nature of the generated new machine number; the machine number thread updates the first machine number based on the second machine number, and updates the second machine number based on the new machine number, thereby updating the two machine numbers. When the business thread updates the machine number, it compares the first machine number and the second machine number in turn to ensure that both machine numbers are utilized.
[0089] Example 4
[0090] Figure 4 The diagram shown is a schematic representation of a database global identifier generation device according to Embodiment 4 of this application. This embodiment is applicable to generating globally unique identifiers for the execution threads of each data node in a distributed database. It is configured in the data nodes of the distributed database. The specific structure of the database global identifier generation device is as follows:
[0091] The request acquisition module 410 is used to acquire the identifier acquisition request generated when the business thread has a database access requirement, which is used to acquire the global identifier of the database, and the historical global identifier of the database acquired by the business thread.
[0092] The serial number determination module 420 is used to determine whether there are unassigned serial numbers in the historical global identifier corresponding to the business thread;
[0093] The identifier update module 430 is used to control the update of the sequence number and machine number of the business thread based on the judgment result.
[0094] The identifier generation module 440 is used to generate a global database identifier based on the updated sequence number, machine number, and thread identifier of the business thread.
[0095] The technical solution of this embodiment obtains the identifier acquisition request generated by the business thread when it has a database access requirement, which is used to obtain the database global identifier, and the historical global identifier of the database obtained by the business thread through the request acquisition module; the sequence number judgment module determines whether there is an unassigned sequence number for the sequence number corresponding to the business thread in the historical global identifier; the identifier update module updates the sequence number and machine number of the business thread according to the judgment result; and the identifier generation module generates the database global identifier based on the updated sequence number, machine number, and thread identifier of the business thread. By maintaining a set of sequence numbers, the business thread can improve the concurrency of the distributed database, and the machine number ensures global uniqueness. By generating the database global identifier based on the updated sequence number, machine number, and thread identifier of the business thread, it does not depend on the machine clock, ensures global uniqueness, has a simple structure, and strong availability. Therefore, the technical solution of this application solves the problems of the SnowFlake algorithm, which relies heavily on the machine clock, resulting in duplicate identifier generation, poor global uniqueness, poor parallelism, and poor UUID availability, and achieves the effect of enhancing the global uniqueness, concurrency, and availability of the database global identifier.
[0096] Optionally, the identifier update module 430 includes:
[0097] The machine number update unit is used to update the machine number of the business thread through the global transaction management component if no unassigned sequence number exists.
[0098] The serial number update unit is used to set the serial number of the business thread to zero and then increment the serial number by an auto-increment to update the serial number.
[0099] Correspondingly, the identifier generation module 440 includes:
[0100] The identifier combination unit is used to generate a global database identifier based on the updated sequence number, the updated machine number, and the thread identifier of the business thread.
[0101] Optional, the serial number update unit includes:
[0102] The second machine number replacement subunit is used to update the machine number of the business thread according to the second machine number in the machine number thread if the machine number of the business thread is the same as the first machine number in the local machine number thread; the first machine number and the second machine number are generated by the global transaction management component.
[0103] The local machine number update subunit is used to update the first and second machine numbers in the machine number thread respectively through the global transaction management component.
[0104] Optional, the thread machine number update sub-unit includes:
[0105] The first machine number replacement subunit is used to update the machine number of the business thread according to the first machine number in the machine number thread if the machine number of the business thread is different from the first machine number in the machine number thread.
[0106] The local machine number update subunit is specifically used to generate a new machine number through the global transaction management component; the machine number thread updates the first machine number based on the second machine number, and updates the second machine number based on the new machine number.
[0107] The local machine number update subunit is also used to auto-increment the second machine number through the global transaction management component to obtain a new machine number.
[0108] Optionally, the identifier update module 430 includes:
[0109] The serial number auto-increment unit is used to auto-increment the serial number corresponding to the business thread according to a preset step size if there is an unassigned serial number, so as to update the serial number.
[0110] Correspondingly, the identifier generation module 440 includes:
[0111] The identifier combination unit is used to generate a global database identifier based on the updated sequence number, machine number, and thread identifier of the business thread.
[0112] The database global identifier generation device provided in this application embodiment can execute the database global identifier generation method provided in any embodiment of this application, and has the corresponding functional modules and beneficial effects of executing the database global identifier generation method.
[0113] Example 5
[0114] Figure 5 This is a schematic diagram of the structure of an electronic device provided in Embodiment 5 of this application, as shown below. Figure 5 As shown, the electronic device includes a processor 510, a memory 520, an input device 530, and an output device 540; the number of processors 510 in the electronic device can be one or more. Figure 5 Taking a processor 510 as an example; the processor 510, memory 520, input device 530, and output device 540 in the electronic device can be connected via a bus or other means. Figure 5 Taking the example of a connection between China and Israel via a bus.
[0115] The memory 520, as a computer-readable storage medium, can be used to store software programs, computer-executable programs, and modules, such as the program instructions / modules corresponding to the database global identifier generation method in this embodiment (e.g., request acquisition module 410, serial number judgment module 420, identifier update module 430, and identifier generation module 440). The processor 510 executes various functional applications and data processing of the electronic device by running the software programs, instructions, and modules stored in the memory 520, thereby implementing the aforementioned database global identifier generation method.
[0116] The memory 520 may primarily include a program storage area and a data storage area. The program storage area may store the operating system and at least one application program required for a given function; the data storage area may store data created based on terminal usage. Furthermore, the memory 520 may include high-speed random access memory and may also include non-volatile memory, such as at least one disk storage device, flash memory device, or other non-volatile solid-state storage device. In some instances, the memory 520 may further include memory remotely located relative to the processor 510, which can be connected to the electronic device via a network. Examples of such networks include, but are not limited to, the Internet, intranets, local area networks, mobile communication networks, and combinations thereof.
[0117] Input device 530 can be used to receive input character information and generate key signal inputs related to user settings and function control of the electronic device. Output device 540 may include display devices such as a display screen.
[0118] Example 6
[0119] Embodiment Six of this application also provides a storage medium containing computer-executable instructions. When executed by a computer processor, the computer-executable instructions are used to execute a database global identifier generation method. The method includes: obtaining an identifier acquisition request generated when a business thread has a database access requirement, used to acquire a database global identifier, and a historical global identifier of the database acquired by the business thread; determining whether there is an unassigned sequence number in the historical global identifier corresponding to the business thread; controlling the updating of the sequence number and machine number of the business thread according to the determination result; and generating a database global identifier based on the updated sequence number, machine number, and thread identifier of the business thread.
[0120] Of course, the computer-executable instructions provided in the embodiments of this application are not limited to the method operations described above, but can also perform related operations in the database global identifier generation method provided in any embodiment of this application.
[0121] Based on the above description of the implementation methods, those skilled in the art can clearly understand that this application can be implemented using software and necessary general-purpose hardware, and of course, it can also be implemented using hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as a computer floppy disk, read-only memory (ROM), random access memory (RAM), flash memory, hard disk, or optical disk, etc., including several instructions to cause an electronic device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments of this application.
[0122] It is worth noting that in the embodiments of the search device described above, the various units and modules included are only divided according to functional logic, but are not limited to the above division, as long as the corresponding functions can be achieved; in addition, the specific names of each functional unit are only for easy differentiation and are not used to limit the scope of protection of this application.
[0123] Note that the above are merely preferred embodiments and the technical principles employed in this application. Those skilled in the art will understand that this application is not limited to the specific embodiments described herein, and various obvious changes, readjustments, and substitutions can be made without departing from the scope of protection of this application. Therefore, although this application has been described in detail through the above embodiments, this application is not limited to the above embodiments, and may include many other equivalent embodiments without departing from the concept of this application, the scope of which is determined by the scope of the appended claims.
Claims
1. A database global identification generation method, characterized by, include: The identifier retrieval request generated when the business thread has a database access requirement is used to obtain the database global identifier, and the historical global identifier of the database obtained by the business thread; Determine whether there is an unassigned sequence number for the sequence number corresponding to the business thread in the historical global identifier; Based on the judgment result, control the update of the sequence number of the business thread and the machine number of the business thread; Generate the global identifier of the database based on the updated serial number, machine number, and thread identifier of the business thread; The step of controlling the updating of the sequence number and machine number of the business thread based on the judgment result includes: If no unassigned sequence number exists, the machine number of the business thread is updated through the global transaction management component; Set the sequence number of the business thread to zero, and then increment the sequence number by an auto-increment to update the sequence number.
2. The method according to claim 1, characterized in that, The step of generating the database global identifier based on the updated sequence number, machine number, and thread identifier of the business thread includes: The global identifier of the database is generated based on the updated serial number, the updated machine number, and the thread identifier of the business thread.
3. The method according to claim 2, characterized in that, The step of updating the machine number of the business thread through the global transaction management component includes: If the machine number of the business thread is the same as the first machine number in the local machine number thread, then the machine number of the business thread is updated according to the second machine number in the local machine number thread; the first machine number and the second machine number are generated by the global transaction management component. The first and second machine numbers in the machine number thread are updated respectively using the global transaction management component.
4. The method according to claim 2, characterized in that, The step of updating the machine number of the business thread through the global transaction management component also includes: If the machine number of the business thread is different from the first machine number in the machine number thread, then the machine number of the business thread is updated according to the first machine number in the machine number thread.
5. The method according to claim 3, characterized in that, The step of updating the first machine number and the second machine number in the machine number thread respectively through the global transaction management component includes: A new machine number is generated using the global transaction management component; The machine number thread updates the first machine number based on the second machine number, and then updates the second machine number based on the new machine number.
6. The method according to claim 5, characterized in that, The process of generating a new machine number through the global transaction management component includes: The second machine number is incremented by the global transaction management component to obtain the new machine number.
7. The method according to claim 1, characterized in that, The step of controlling the updating of the sequence number and machine number of the business thread based on the judgment result includes: If there are unassigned sequence numbers, the sequence number corresponding to the business thread is incremented by a preset step size to update the sequence number; Accordingly, generating the database global identifier based on the updated sequence number, machine number, and thread identifier of the business thread includes: The database global identifier is generated based on the updated serial number, the machine number, and the thread identifier of the business thread.
8. A database global identifier generation device, characterized in that, include: The request acquisition module is used to acquire the identifier acquisition request generated by the business thread when it has a database access requirement, which is used to acquire the global identifier of the database, and the historical global identifier of the database acquired by the business thread. The serial number determination module is used to determine whether there is an unassigned serial number in the historical global identifier corresponding to the business thread; The identifier update module is used to control the update of the sequence number and machine number of the business thread based on the judgment result. The identifier generation module is used to generate the global identifier of the database based on the updated serial number, machine number, and thread identifier of the business thread; The identifier update module includes: The machine number update unit is used to update the machine number of the business thread through the global transaction management component if there is no unassigned sequence number. The serial number update unit is used to set the serial number of the business thread to zero and then increment the serial number by an auto-increment to update the serial number.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the program, it implements the database global identifier generation method as described in any one of claims 1-7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When executed by the processor, the program implements the database global identifier generation method as described in any one of claims 1-7.