Distributed self-increasing sequence synchronization method and device, equipment and storage medium
By setting up master nodes and slave nodes in a distributed database cluster, and the master nodes uniformly manage the global self-increasing sequence values, the problems of low performance, high complexity and poor scalability of self-increasing sequence processing in a distributed database cluster are solved, and the problem of low self-increasing sequence processing performance, high complexity and poor scalability in a distributed database cluster is realized. Efficient and reliable self-increasing sequence allocation.
Patent Information
- Application Number
- CN202411551914.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-01
- Publication Date
- 2025-02-21
AI Technical Summary
The existing distributed database clusters have problems of low performance, high complexity and poor scalability when processing self-increasing sequences, which makes it difficult to ensure the uniqueness and continuity of self-increasing sequences in high concurrency scenarios.
By setting up a master node and multiple slave nodes in a distributed database cluster, the master node is responsible for maintaining the global self-increasing sequence value. The slave node assigns a local self-increasing sequence to the master node based on the preset cache parameters, and caches the local sequence. Each time a local self-increasing sequence is allocated, the master node calculates the starting value based on the global self-increasing sequence value and assigns continuous self-increasing sequence values.
This method improves the self-increasing sequence allocation efficiency of distributed systems in high concurrency, reduces the load of the main node and network communication frequency, avoids the performance bottleneck caused by lock competition, and ensures the uniqueness and continuity of the self-increasing sequence sex.
Smart Images

Figure CN119493799A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of distributed systems, and in particular to a distributed self-incrementing sequence synchronization method, apparatus, device and storage medium. Background Art
[0002] An auto-increment sequence is a special column in a relational table that automatically generates a unique integer for the column each time a new record is inserted. The integer is incremented based on the maximum value of all records in the table, and the increment is usually fixed, thus ensuring the uniqueness and increment of each record. When multiple nodes access the same database resource at the same time, data inconsistency may occur due to concurrent operations. In a distributed database cluster environment, since multiple nodes may generate auto-increment IDs at the same time, ID conflicts or duplications are prone to occur.
[0003] In order to solve the uniqueness and continuity problems of auto-increment sequences in distributed database clusters, a variety of solutions have been proposed in related technologies. Among them, some solutions use global locks or centralized sequence generators to ensure the global uniqueness of IDs, but this method can easily become a performance bottleneck in high-concurrency scenarios and increase the complexity and operation and maintenance costs of the system. Other solutions use distributed lock mechanisms to ensure the atomicity and isolation of each auto-increment operation, but frequent lock competition will still lead to performance degradation, and the reliability of the auto-increment sequence after a node failure cannot be guaranteed.
[0004] In summary, the existing distributed database cluster auto-increment sequence implementation scheme has problems such as low performance, high complexity and poor scalability. Therefore, it is necessary to study and propose a more efficient and reliable auto-increment sequence implementation scheme in a distributed database cluster environment to ensure the reliability of the auto-increment sequence to meet the high requirements of modern information systems for data processing and storage. Summary of the invention
[0005] The embodiments of the present application provide a distributed auto-increment sequence synchronization method, apparatus, device and storage medium, which are used to improve the problem that the distributed database cluster has high performance complexity and reduced performance in order to improve sequence continuity and uniqueness.
[0006] A first aspect of an embodiment of the present application provides a distributed self-incrementing sequence synchronization method, including:
[0007] In a distributed database cluster, one master node and multiple slave nodes are set, and the master node is used to maintain a global auto-increment sequence value;
[0008] The slave node requests the master node to allocate a local auto-increment sequence according to a preset cache parameter, and the master node calculates a starting value of the local auto-increment sequence according to a global auto-increment sequence value, and allocates a preset number of continuous auto-increment sequence values to the slave node;
[0009] The slave node caches the local auto-increment sequence locally;
[0010] When the local auto-increment sequence cached by the slave node is exhausted, a new allocation request is sent to the master node, and the master node reallocates a new local auto-increment sequence according to the current global auto-increment sequence value.
[0011] In some embodiments of the present application, after the slave node receives the local auto-increment sequence assigned by the master node, the slave node, when receiving a request from a user to insert a record, performs the following steps according to whether the user specifies an auto-increment value:
[0012] If the user does not specify an auto-increment value, the slave node uses the local auto-increment sequence start value in the cache and increments the value after use;
[0013] If the user specifies an auto-increment value, and the auto-increment value is greater than the maximum value of the local auto-increment sequence, the slave node requests a new local auto-increment sequence range from the master node; the master node calculates the starting value of the local auto-increment sequence according to the specified auto-increment value, and returns it to the slave node;
[0014] If the auto-increment value specified by the user is less than the maximum value of the local auto-increment sequence, the slave node returns the auto-increment value and updates the starting value of the local auto-increment sequence to the specified auto-increment value plus 1.
[0015] In some embodiments of the present application, when the slave node requests a new local auto-increment sequence, the master node recalculates the starting value of the local auto-increment sequence based on the current global auto-increment sequence value and the auto-increment value specified by the user, and allocates a preset number of consecutive auto-increment sequences; after receiving feedback from the master node, the slave node updates the local local auto-increment sequence starting value and the local auto-increment sequence maximum value.
[0016] In some embodiments of the present application, when the master node receives the self-increment sequence request from the slave node, the master node performs the following steps:
[0017] When the request comes from this node, if the user-specified auto-increment value is greater than or equal to the global auto-increment sequence value, the master node returns the specified value and updates the global auto-increment sequence value to the specified auto-increment value plus 1;
[0018] If the user does not specify an auto-increment value, the master node returns the current global auto-increment sequence value and increments the global auto-increment sequence value;
[0019] When the request comes from the slave node, if the auto-increment request value applied for allocation is greater than or equal to the current global auto-increment sequence value, the master node uses the request value as the starting value of the local auto-increment sequence, allocates a preset number of consecutive auto-increment sequences, and updates the global auto-increment sequence value to the maximum value of the allocated sequence.
[0020] In some embodiments of the present application, if the auto-increment request value requested by the slave node is less than the current global auto-increment sequence value, the master node uses the current global auto-increment sequence value as the starting value of the local auto-increment sequence and allocates a preset number of consecutive auto-increment sequences; the master node returns the allocated local auto-increment sequence to the slave node, and updates the global auto-increment sequence value to the maximum value of the allocated sequence.
[0021] In some embodiments of the present application, the method further includes:
[0022] The master node and the slave node respectively maintain frozen serial numbers in the distributed database cluster, and the frozen serial numbers are used to indicate the state change of the cluster, including the joining or disconnection of a node;
[0023] When adding an auto-increment sequence attribute to the relationship table, the master node records the frozen sequence number of the current cluster in the relationship description in the memory;
[0024] When the slave node requests the master node to allocate an auto-increment sequence, the master node first checks whether the frozen sequence number in the relationship description is consistent with the frozen sequence number of the current cluster;
[0025] If the frozen sequence numbers are consistent, the master node allocates an auto-increment sequence to the slave node according to the current process;
[0026] If the frozen sequence numbers are inconsistent, it means that the cluster status has changed, and the master node recalculates the global auto-increment sequence value according to the latest status of the cluster;
[0027] If the master node itself is transformed from the slave node to the new master node, the master node reads the maximum auto-increment sequence value of the relationship table from the disk as the new global auto-increment sequence value, and updates the frozen sequence number;
[0028] When the slave node detects that the frozen sequence number has changed, it re-applies for the local auto-increment sequence from the master node, and updates the local local auto-increment sequence start value and maximum value to the latest sequence range allocated by the master node.
[0029] In some embodiments of the present application, when a node in the cluster goes offline or a new node is added, the master node marks the change in cluster status by increasing the freezing sequence number, and continues to allocate new auto-incrementing sequence values after all nodes resynchronize the freezing sequence numbers.
[0030] In some embodiments of the present application, when the master node fails or crashes, the slave nodes in the cluster automatically elect a new master node. The new master node recalculates the global auto-increment sequence value based on the auto-increment value of the relationship table on the disk, and reallocates the local auto-increment sequence to the slave nodes.
[0031] A second aspect of an embodiment of the present application provides a distributed self-increment sequence synchronization device, including:
[0032] A node setting module, used to set one master node and multiple slave nodes in a distributed database cluster, wherein the master node is used to maintain a global auto-increment sequence value;
[0033] An auto-increment sequence allocation module is used to request the master node to allocate a local auto-increment sequence according to a preset cache parameter at the slave node, and the master node calculates the starting value of the local auto-increment sequence according to the global auto-increment sequence value, and allocates a preset number of continuous auto-increment sequence values to the slave node;
[0034] The slave node cache module is used to cache the local auto-increment sequence locally;
[0035] The allocation request module is used to send a new allocation request to the master node when the local auto-increment sequence cached by the slave node is exhausted, and the master node reallocates a new local auto-increment sequence according to the current global auto-increment sequence value.
[0036] A computer device provided in the third aspect of an embodiment of the present application includes: a processor, a storage medium and a bus, the storage medium stores machine-readable instructions executable by the processor, when the device is running, the processor and the storage medium communicate through the bus, and the processor executes the machine-readable instructions to perform the steps of the distributed self-increment sequence synchronization method provided in the first aspect.
[0037] A fourth aspect of an embodiment of the present application further provides a storage medium having a computer program stored thereon. When the computer program is executed by a processor, the steps of the distributed self-incrementing sequence synchronization method provided in the first aspect are executed.
[0038] It can be seen that the method of the embodiment of the present application can bring the following beneficial effects:
[0039] The present application realizes the reliable generation of self-incrementing sequence in a distributed environment by setting a master node and multiple slave nodes in a distributed database cluster environment, and the master node uniformly manages the global self-incrementing sequence value. In detail, the master node maintains a globally unique and increasing self-incrementing sequence value, and the slave node requests the allocation of a local self-incrementing sequence through a preset cache parameter, and caches the local sequence locally. Each time a local self-incrementing sequence is allocated, the master node calculates the allocation start value according to the global self-incrementing sequence value and generates multiple continuous self-incrementing sequence values. Since the slave node can cache a continuous local self-incrementing sequence locally, each slave node can independently handle the allocation and use of the self-incrementing sequence within a period of time, reducing frequent requests to the master node. This not only reduces the load of the master node, but also reduces the frequency of network communication, significantly improving the efficiency of self-incrementing sequence allocation in distributed systems under high concurrency. And compared to the global lock or distributed lock mechanism, the present application distributes multiple groups of continuous self-increment sequences to the slave nodes at one time through the master node, so that the slave nodes can generate sequence numbers independently locally, avoiding the system bottleneck caused by lock competition, and can optimize the system performance in high-concurrency scenarios without sacrificing data uniqueness, and achieve more efficient data distribution and processing. In addition, the present application adopts a division of labor and cooperation mode between the master node and the slave node. The slave node requests and caches the local self-increment sequence according to the cache parameters, and the master node is responsible for the uniqueness maintenance of the global sequence, which is conducive to ensuring the global uniqueness of the self-increment sequence while allowing the cluster size to be flexibly adjusted according to actual needs, that is, there is no need to redesign the overall architecture of sequence allocation when adding or reducing slave nodes. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.
[0041] Figure 1 It is a schematic diagram of an application scenario between a terminal and a server provided in an embodiment of the present application;
[0042] Figure 2 It is a flow chart of a distributed self-incrementing sequence synchronization method provided in an embodiment of the present application;
[0043] Figure 3 It is a workflow diagram of a slave node in a distributed self-incrementing sequence synchronization method provided in an embodiment of the present application;
[0044] Figure 4 It is a workflow diagram of the master node in the distributed self-increment sequence synchronization method provided in an embodiment of the present application;
[0045] Figure 5 It is a workflow diagram of freezing sequence numbers in the distributed self-increment sequence synchronization method provided in an embodiment of the present application;
[0046] Figure 6 It is a schematic diagram of the structure of a computer device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0047] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of this application.
[0048] The embodiments of the present application provide a distributed auto-increment sequence synchronization method, apparatus, device and storage medium, wherein the device may be an Internet of Things device and the storage medium may be a computer storage medium. Figure 1 ,The distributed self-incrementing sequence synchronization method can be integrated in a device, which can be a server or a terminal and other devices.
[0049] Among them, the server can be an independent physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, network acceleration services (Content Delivery Network, CDN), as well as big data and artificial intelligence platforms.
[0050] The terminal may be a smart phone, a tablet computer, a laptop computer, a desktop computer, a smart speaker, a smart watch, etc., but is not limited thereto. The terminal and the server may be directly or indirectly connected via wired or wireless communication, and this application does not limit this.
[0051] It should be noted that the steps executed in the server may also be executed in the terminal, and this embodiment does not limit this.
[0052] In the embodiments of the present application, "plurality" refers to two or more than two. In the embodiments of the present application, "first" and "second" are used to distinguish descriptions and should not be understood as implying relative importance.
[0053] It should be noted that the description order of the following embodiments is not intended to limit the preferred order of the embodiments.
[0054] See also Figure 2 In this embodiment, a distributed self-incrementing sequence synchronization method is provided, such as Figure 2 As shown, the specific process of the distributed self-increment sequence synchronization method can be as follows:
[0055] S1. In a distributed database cluster, set up a master node and multiple slave nodes. The master node is used to maintain the global auto-increment sequence value;
[0056] S2. The slave node requests the master node to allocate a local auto-increment sequence according to the preset cache parameters. The master node calculates the starting value of the local auto-increment sequence according to the global auto-increment sequence value, and allocates a preset number of continuous auto-increment sequence values to the slave node.
[0057] S3. The slave node caches the local auto-increment sequence locally;
[0058] S4. When the local auto-increment sequence cached by the slave node is exhausted, a new allocation request is sent to the master node, and the master node reallocates a new local auto-increment sequence based on the current global auto-increment sequence value.
[0059] The technical solution provided by the present application realizes the reliable generation of self-incrementing sequences in a distributed environment by setting a master node and multiple slave nodes in a distributed database cluster environment, and the master node uniformly manages the global self-incrementing sequence value. In detail, the master node maintains a globally unique and increasing self-incrementing sequence value, and the slave node requests the allocation of a local self-incrementing sequence through a preset cache parameter, and caches the local sequence locally. Each time a local self-incrementing sequence is allocated, the master node calculates the allocation start value according to the global self-incrementing sequence value and generates multiple continuous self-incrementing sequence values. Since the slave node can cache a continuous local self-incrementing sequence locally, each slave node can independently handle the allocation and use of the self-incrementing sequence within a period of time, reducing frequent requests to the master node. This not only reduces the load of the master node, but also reduces the frequency of network communication, significantly improving the efficiency of self-incrementing sequence allocation in distributed systems under high concurrency. And compared to the global lock or distributed lock mechanism, the present application distributes multiple groups of continuous self-increment sequences to the slave nodes at one time through the master node, so that the slave nodes can generate sequence numbers independently locally, avoiding the system bottleneck caused by lock competition, and can optimize the system performance in high-concurrency scenarios without sacrificing data uniqueness, and achieve more efficient data distribution and processing. In addition, the present application adopts a division of labor and cooperation mode between the master node and the slave node. The slave node requests and caches the local self-increment sequence according to the cache parameters, and the master node is responsible for the uniqueness maintenance of the global sequence, which is conducive to ensuring the global uniqueness of the self-increment sequence while allowing the cluster size to be flexibly adjusted according to actual needs, that is, there is no need to redesign the overall architecture of sequence allocation when adding or reducing slave nodes.
[0060] See also Figure 2 and Figure 3 In some embodiments, the master node is responsible for maintaining a globally unique auto-increment sequence value in a distributed database cluster. This sequence value will increase with each allocation operation to ensure that each generated sequence number is unique. The global auto-increment sequence value is an automatically increasing integer that is used to ensure that each record in the database has a unique identifier. In a distributed environment, multiple nodes will share this global value, and the master node is responsible for incrementing and allocating it. Each slave node requests the master node to allocate a local auto-increment sequence through a preset cache parameter. The master node calculates the starting value of the local sequence based on the current global auto-increment sequence value, and allocates a certain number of continuous auto-increment sequence values to the slave node at one time according to the cache parameter. The cache parameter is a system setting that controls the number of auto-increment sequences that can be cached locally each time a slave node requests a master node. This parameter is usually set based on the performance requirements and concurrent load of the system. The local auto-increment sequence is a set of continuous auto-increment sequence values assigned to the slave node by the master node. These values are used by the slave node for a period of time to avoid interacting with the master node every time a sequence number is generated. After the slave node obtains the local auto-increment sequence from the master node, it caches these sequence values in the local memory. The slave node can then directly obtain the auto-increment sequence values from the cache to process user requests. Local caching means that the slave node stores these sequence values locally so that it does not need to request the master node again when receiving an insert request for a new record, saving network overhead and time. When the local auto-increment sequence cached by the slave node is exhausted, a new allocation request is issued to the master node, and the master node reallocates a new local auto-increment sequence based on the current global auto-increment sequence value. Through cyclic requests and allocations, the slave node can always obtain a new auto-increment sequence to ensure the uniqueness and continuity of the sequence number. When the slave node has used up all the currently allocated auto-increment sequence values, the system automatically requests a new sequence value from the master node. This mechanism enables the slave node to process user requests without interruption and ensures that the generated sequence numbers remain unique and continuous globally.
[0061] Assume that a database cluster of a large e-commerce platform has a master node and three slave nodes. The slave nodes are responsible for processing different types of data insertion requests (such as order information, user information, and product information). The system configures the cache parameter seq_cache_extent = 100 for each slave node, which means that each time the slave node requests the master node, the master node will allocate 100 consecutive auto-increment sequence values to it. The master node maintains the current global auto-increment sequence value, for example, the current global value is 1000. When slave node A requests an auto-increment sequence, the master node will start from the global sequence 1000 and allocate sequence values of seq_cache_extent. Therefore, the local sequence value range obtained by slave node A is 1000-1099. After receiving the sequence values 1000-1099, slave node A caches these sequences into local memory. At this time, node A can directly use the locally cached auto-increment sequence values when processing user order insertion requests without frequent interaction with the master node. When slave node A has used up all the local auto-increment sequences, that is, 1099 has been used up, node A will send a request to the master node again. The global auto-increment sequence value of the master node has increased to 1100 at this time, so the master node will assign a new local auto-increment sequence value starting from 1100. In this way, each slave node can still achieve a globally unique auto-increment sequence number through the management of the master node while working independently, ensuring the reliability and consistency of the data.
[0062] In some embodiments, after the slave node receives the local auto-increment sequence assigned by the master node, when the slave node receives a request from a user to insert a record, the slave node performs the following steps according to whether the user specifies an auto-increment value:
[0063] If the user does not specify an auto-increment value, the slave node uses the starting value of the local auto-increment sequence in the cache and increments the value after use;
[0064] If the user specifies an auto-increment value, and the auto-increment value is greater than the maximum value of the local auto-increment sequence, the slave node requests a new local auto-increment sequence range from the master node; the master node calculates the starting value of the local auto-increment sequence based on the specified auto-increment value and returns it to the slave node;
[0065] If the auto-increment value specified by the user is less than the maximum value of the local auto-increment sequence, the slave node returns the auto-increment value and updates the starting value of the local auto-increment sequence to the specified auto-increment value plus 1.
[0066] In detail, when a user inserts a new record into the database, the slave node processes the record and generates a unique auto-incrementing sequence value. At this point, the slave node checks whether the user has specified an auto-incrementing sequence value for the record. When a user inserts a record, the system usually needs to generate a unique identifier (i.e., an auto-incrementing sequence value) for the record. Sometimes users specify a specific auto-incrementing value themselves, but in more cases, the system automatically generates the value.
[0067] If the user does not specify an auto-increment value, the slave node will directly use the locally cached local auto-increment sequence start value as the auto-increment value for the new record, and increment the value after use for the next use. The local auto-increment sequence start value is the first available value in the sequence range cached locally by the slave node. After each use, the start value will be incremented until it reaches the current cached local sequence maximum value.
[0068] If the user specifies an auto-increment value, and the value is greater than the maximum value of the currently cached local auto-increment sequence, the slave node cannot directly use the cached value, so it will request a new local auto-increment sequence range from the master node. The master node recalculates the local auto-increment sequence start value based on the auto-increment value specified by the user and returns the new sequence range to the slave node. In this case, the auto-increment value that the user wants to use exceeds the sequence range currently cached by the slave node. In order to ensure the uniqueness of the auto-increment value, the slave node needs to apply for a new auto-increment sequence range from the master node to ensure that the value does not conflict globally.
[0069] If the user-specified auto-increment value is less than or equal to the maximum value of the local auto-increment sequence currently cached by the slave node, then the value is within the range of the local cache of the slave node and can be used directly. Therefore, the slave node returns the specified value and updates the starting value of the local auto-increment sequence to the user-specified auto-increment value plus 1. When the user-specified auto-increment value falls within the range of the local auto-increment sequence currently cached by the slave node, the slave node can return the value directly without additional interaction with the master node. This situation allows more efficient processing of the user-specified auto-increment value within the local sequence range.
[0070] Assume that there is a master node and three slave nodes in the database cluster of a large e-commerce platform. The slave nodes are responsible for processing user order insertion requests, and each slave node locally caches 100 local auto-increment sequence values (i.e., seq_cache_extent = 100). Currently, the auto-increment sequence range cached by slave node A is 1000-1099.
[0071] The first is when the user does not specify an auto-increment value:
[0072] The user submits an order insertion request but does not specify an auto-increment value. At this time, node A directly uses the local cached local auto-increment sequence starting value 1000 as the auto-increment value of the order and increments the starting value to 1001 for next use.
[0073] The second case is when the user specifies an increment that is greater than the local maximum value:
[0074] The user submits an order insertion request and specifies an auto-increment value of 1200. Since this value is greater than the maximum value of 1099 cached by slave node A, slave node A cannot use this value directly. Therefore, it requests a new local auto-increment sequence range from the master node. The master node allocates a new local auto-increment sequence range (e.g., 1200-1299) to slave node A starting from 1200 based on the auto-increment value specified by the user to satisfy the request.
[0075] The third case is when the user specifies an increment that is less than the local maximum value:
[0076] The user submits an order insertion request and specifies an auto-increment value of 1050. Since this value is within the range of the currently cached local auto-increment sequence (i.e., less than 1099), slave node A can directly use the specified value and update the starting value to 1051 for next use.
[0077] Furthermore, when a slave node requests a new local auto-increment sequence, the master node recalculates the starting value of the local auto-increment sequence based on the current global auto-increment sequence value and the auto-increment value specified by the user, and allocates a preset number of consecutive auto-increment sequences; after receiving feedback from the master node, the slave node updates the local local auto-increment sequence starting value and the local auto-increment sequence maximum value.
[0078] In detail, when all the local auto-increment sequence values cached locally by the slave node are used up, the slave node will initiate a new auto-increment sequence request to the master node. The master node will reallocate new local auto-increment sequence values based on the current global auto-increment sequence value or the auto-increment value specified by the user. When the slave node has used up all the currently allocated local auto-increment sequences, in order to continue processing subsequent user insert requests, it must request a new set of auto-increment sequences from the master node. This mechanism ensures that the slave node can still efficiently process insert operations after the sequence is exhausted without sequence conflicts or duplications.
[0079] After receiving the request from the slave node, the master node will recalculate the starting value of the local auto-increment sequence based on the current global auto-increment sequence value or the user-specified auto-increment value, and allocate a new set of continuous auto-increment sequences. The master node ensures that the allocated local sequence range does not conflict with other nodes. If the slave node does not specify a specific auto-increment value, the master node will start from the current global auto-increment sequence value and allocate a new local auto-increment sequence value for it. If the slave node specifies a specific auto-increment value, the master node will calculate the new local auto-increment sequence starting value based on this value as the starting point, and ensure that the allocated sequence does not conflict with other nodes.
[0080] The new sequence assigned by the master node to the slave node is a set of continuous auto-increment sequences, so that the slave node can cache locally and continuously process multiple user insertion requests after receiving the new sequence, reducing frequent network interactions. The master node determines the number of auto-increment sequences assigned to the slave node each time based on the system's preset cache parameter seq_cache_extent. For example, when seq_cache_extent = 100, the master node will assign 100 continuous auto-increment sequence values to the slave node. The slave node caches these values locally to efficiently process user insertion requests.
[0081] Assume that there is a master node and multiple slave nodes in the database cluster of a large online retail platform. Each slave node needs to independently process the insertion request of user orders. Currently, the auto-increment sequence range cached by slave node A is 1000-1099, but this sequence range has been fully used and cannot continue to process new order requests.
[0082] Node A detects that the local auto-increment sequence in the local cache has been exhausted (that is, all sequence numbers have been assigned to orders), so it sends a request to the master node to allocate a new local auto-increment sequence.
[0083] Assuming that the global auto-increment sequence value of the current master node has grown to 1100, the master node will allocate a new set of continuous auto-increment sequences to slave node A starting from 1100. Since the system setting seq_cache_extend=100, the master node will allocate auto-increment sequences of 1100-1199 to slave node A.
[0084] After receiving the new sequence from node A, it caches it locally and immediately starts using these new sequence numbers to process subsequent user order insertion requests.
[0085] If the user specifies an auto-increment value when submitting an order, for example, the user wants the order number to be 1250, and the maximum sequence number currently cached by slave node A is 1199, the master node will reallocate the sequence based on the user-specified value 1250. The master node will use 1250 as the starting point and allocate a new set of sequences, such as 1250-1349, to ensure that there will be no conflict with the sequence numbers of other nodes.
[0086] In some embodiments, see Figure 2 and Figure 4 When the master node receives the self-increment sequence request from the slave node, it performs the following steps:
[0087] When the request comes from this node, if the user-specified auto-increment value is greater than or equal to the global auto-increment sequence value, the master node returns the specified value and updates the global auto-increment sequence value to the specified auto-increment value plus 1;
[0088] If the user does not specify an auto-increment value, the master node returns the current global auto-increment sequence value and increments the global auto-increment sequence value;
[0089] When a request comes from a slave node, if the auto-increment request value applied for allocation is greater than or equal to the current global auto-increment sequence value, the master node uses the request value as the starting value of the local auto-increment sequence, allocates a preset number of consecutive auto-increment sequences, and updates the global auto-increment sequence value to the maximum value of the allocated sequence.
[0090] Specifically, when a request comes from this node, the master node performs different operations depending on whether the user specifies an auto-increment value:
[0091] If the user specifies an auto-increment value, and the auto-increment value is greater than or equal to the global auto-increment sequence value:
[0092] The master node returns the auto-increment value specified by the user and updates the global auto-increment sequence value to the specified value plus 1.
[0093] If the user does not specify an auto-increment value:
[0094] The master node directly returns the current global auto-increment sequence value and increments the value to ensure that the auto-increment sequence used next time is still unique and continuous.
[0095] This node request refers to the auto-increment sequence request issued by the database instance that the master node is responsible for. When processing these requests, the master node can either assign the current global sequence value or adjust the starting point of the sequence value according to the auto-increment value specified by the user. The sequence number uniformly maintained by the master node maintains uniqueness and continuity in the entire database cluster. After each assignment, the value will increase.
[0096] When a request comes from a slave node, the master node processes it according to whether the user specifies an auto-increment value. If the user specifies an auto-increment value and the value is greater than or equal to the global auto-increment sequence value, the master node uses the user-specified auto-increment value as the starting value of the local auto-increment sequence, allocates a certain number of continuous auto-increment sequences, and updates the global auto-increment sequence value to the maximum value of the allocated sequence. If the user does not specify an auto-increment value, the master node starts from the current global auto-increment sequence value, allocates a certain number of continuous auto-increment sequence values to the slave node, and updates the global auto-increment sequence value to the maximum value of the allocated sequence. The slave node request is an auto-increment sequence request issued by other slave nodes in the database cluster. The master node needs to allocate a local auto-increment sequence according to the slave node's request to ensure that the sequence number generated by the slave node remains unique and continuous globally. The local auto-increment sequence is a set of continuous sequence numbers assigned by the master node to the slave node based on the global auto-increment sequence value, which is used for local caching and processing of user requests by the slave node.
[0097] Assume that the database cluster of an e-commerce platform contains a master node and multiple slave nodes. Currently, the global auto-increment sequence value of the master node is 2000, and slave node A and this node process different types of requests respectively. The following scenario shows how the master node processes requests from different sources:
[0098] This node requests an auto-increment sequence (the user does not specify an auto-increment value):
[0099] This node needs to generate a unique auto-increment sequence value for the inserted order record. The user did not specify an auto-increment value, so the master node returns the current global auto-increment sequence value 2000 to this node and increments the global auto-increment sequence value to 2001 to ensure that the next request is still unique.
[0100] This node requests an auto-increment sequence (the user specifies the auto-increment value):
[0101] When inserting a record, the user specifies the order number 2050. The master node detects that the auto-increment value specified by the user is greater than the current global auto-increment sequence value 2000. Therefore, the master node returns the specified value 2050 to this node and updates the global auto-increment sequence value to 2051.
[0102] Request an auto-increment sequence from a node (the user does not specify an auto-increment value):
[0103] The local auto-increment sequence of slave node A has been exhausted, so it requests a new local auto-increment sequence from the master node. The user did not specify an auto-increment value, so the master node allocates seq_cache_extent = 100 consecutive sequence numbers ranging from 2051 to 2150 based on the current global auto-increment sequence value of 2051, and assigns these values to slave node A, while updating the global auto-increment sequence value to 2151.
[0104] Request an auto-increment sequence from a node (the user specifies the auto-increment value):
[0105] Node A receives a user-specified auto-increment request, and the user wants the order number to be 2200. Since this value is greater than the current global auto-increment sequence value 2151, the master node allocates a new set of continuous auto-increment sequences starting from 2200, ranging from 2200 to 2299, and updates the global auto-increment sequence value to 2300 to ensure the uniqueness of subsequent sequence numbers.
[0106] Furthermore, if the auto-increment request value requested by the slave node is less than the current global auto-increment sequence value, the master node uses the current global auto-increment sequence value as the starting value of the local auto-increment sequence and allocates a preset number of consecutive auto-increment sequences; the master node returns the allocated local auto-increment sequence to the slave node, and updates the global auto-increment sequence value to the maximum value of the allocated sequence.
[0107] In detail, when a slave node sends an auto-increment sequence request to the master node, if the auto-increment value requested by the slave node is less than the current global auto-increment sequence value of the master node, the master node will not directly use the value specified by the slave node, but will reallocate the local auto-increment sequence range based on the current global auto-increment sequence value. The master node will allocate a continuous set of auto-increment sequences starting from the global auto-increment sequence value. The global auto-increment sequence value is managed uniformly by the master node to ensure unique and increasing sequence numbers throughout the cluster. When the auto-increment value specified by the slave node is less than the global auto-increment sequence value, using the specified value may cause sequence number conflicts. Therefore, the master node allocates according to the global auto-increment sequence value to avoid conflicts. The master node allocates a set of continuous local auto-increment sequences to the slave node based on the current global auto-increment sequence value. The number of allocations is determined by the system's cache parameter seq_cache_extent. This mechanism ensures that the slave node can continue to process insert requests without conflicting with the auto-increment sequences of other nodes. The set of continuous auto-increment sequences allocated by the master node is a range of sequence numbers used by the slave node, and the number is determined by the system configuration. For example, if seq_cache_ext ent = 100, the master node may allocate 100 consecutive sequence values to the slave node. After completing the allocation, the master node updates the global auto-increment sequence value to the maximum value of the allocated sequence to ensure that the subsequently allocated sequence numbers do not overlap with the previous sequence numbers. By continuously increasing the global auto-increment sequence value, the master node can ensure that the auto-increment sequences generated by all nodes are unique globally.
[0108] Assume that in a database cluster of a large retail platform, the global auto-increment sequence value of the master node is 3000. Node A sends a request to the master node because the local auto-increment sequence has been exhausted. The user wants the order number to be 2500, which is less than the current global auto-increment sequence value of 3000 of the master node. According to the technical solution of claim 5, the master node will perform the following operations:
[0109] Determine whether the auto-increment value is less than the global auto-increment sequence value:
[0110] After receiving the request from slave node A, the master node finds that the user-specified auto-increment value 2500 is less than the current global auto-increment sequence value 3000. To avoid conflicts, the master node does not directly use 2500, but uses the current global auto-increment sequence value as the starting point to reallocate the local auto-increment sequence.
[0111] Allocate a new local auto-increment sequence:
[0112] The master node allocates a new set of continuous sequences to slave node A based on the global auto-increment sequence value 3000. Assuming the system configuration seq_cache_extent = 100, the master node will allocate continuous sequence numbers 3000-3099 to slave node A and cache these sequences to slave node A.
[0113] Update the global auto-increment sequence value:
[0114] After completing the allocation, the master node updates the global auto-increment sequence value to 3100 to ensure that subsequent requests will not conflict with previous allocations.
[0115] In this way, the master node can ensure that the sequence numbers obtained from node A are unique and continuous, avoiding conflicts that may be caused by using a specified value such as 2500.
[0116] In some embodiments, see Figure 5 , the distributed self-incrementing sequence synchronization method also includes:
[0117] The master node and slave node maintain frozen serial numbers in the distributed database cluster. The frozen serial numbers are used to indicate the status changes of the cluster, including node joining or disconnection.
[0118] When adding an auto-increment sequence attribute to a relational table, the master node records the frozen sequence number of the current cluster in the relational description in memory;
[0119] When a slave node requests the master node to allocate an auto-increment sequence, the master node first checks whether the frozen sequence number in the relationship description is consistent with the frozen sequence number of the current cluster;
[0120] If the frozen sequence numbers are the same, the master node assigns an auto-increment sequence to the slave node according to the current process;
[0121] If the frozen sequence numbers are inconsistent, it means that the cluster status has changed, and the master node recalculates the global auto-increment sequence value based on the latest status of the cluster;
[0122] If the master node itself changes from a slave node to a new master node, the master node reads the maximum auto-increment sequence value of the relationship table from the disk as the new global auto-increment sequence value and updates the frozen sequence number;
[0123] When the slave node detects that the frozen sequence number has changed, it re-applies to the master node for the local auto-increment sequence and updates the local local auto-increment sequence start value and maximum value to the latest sequence range allocated by the master node.
[0124] In detail, the master node and the slave node each maintain a frozen sequence number in the distributed database cluster, and the frozen sequence number is used to record the status of the cluster. Whenever a node in the cluster is offline, restored, or added, the frozen sequence number will change accordingly, and all nodes confirm whether the cluster status is consistent by comparing the frozen sequence number. The frozen sequence number is a value that identifies the cluster status. Through the increase and decrease of the frozen sequence number, each node can perceive the changes in the cluster, so that the sequence number can be resynchronized when the status is inconsistent, ensuring the uniqueness and continuity of the auto-increment sequence in the distributed environment. When the cluster status changes, the master node and the slave node will update the frozen sequence number accordingly, and adjust the starting value and range of the auto-increment sequence according to the updated status.
[0125] When adding an auto-increment sequence attribute to a relationship table, the master node will record the frozen sequence number of the current cluster in the relationship description in memory. In this way, the relationship description of each node in the cluster will have a corresponding frozen sequence number for comparison and verification during subsequent requests. The relationship description refers to the description information of each relationship table in the cluster. The frozen sequence number is recorded in the relationship description as part of the cluster state, ensuring that when a node makes an auto-increment sequence request, the frozen sequence number can be used to determine whether the cluster state is synchronized.
[0126] When a slave node requests an auto-increment sequence, the master node first checks whether the frozen sequence number in the slave node's relationship description is consistent with the frozen sequence number of the current cluster. If the frozen sequence numbers are consistent, it means that the cluster state is stable, and the master node will assign the auto-increment sequence according to the normal process. If the frozen sequence numbers are inconsistent, it means that the cluster state has changed. The master node will recalculate the global auto-increment sequence value based on the latest state and update the frozen sequence number to ensure that the assigned auto-increment sequence remains unique and continuous.
[0127] By comparing the frozen sequence numbers, the master node can confirm whether there are any node status changes in the cluster (such as disconnection or recovery), and recalculate and assign the auto-increment sequence when necessary to ensure the consistency of the auto-increment sequence.
[0128] When the master node fails and switches from the slave node to the new master node, the new master node reads the maximum auto-increment sequence value of the relationship table from the disk as the global auto-increment sequence value, and updates the frozen sequence number to ensure the correctness and consistency of the auto-increment sequence. When the master node recovers, it needs to read the disk data to ensure that the starting value of the global auto-increment sequence is the latest, and reset the frozen sequence number to ensure the state of the cluster is synchronized and avoid inconsistencies.
[0129] After detecting that the frozen sequence number has changed, the slave node will reapply for the local auto-increment sequence from the master node and update the local local auto-increment sequence start value and maximum value to ensure that the auto-increment sequence generated subsequently remains unique and continuous. The slave node determines whether the cluster has changed by comparing the frozen sequence number. When the frozen sequence number is inconsistent, the slave node will re-obtain the latest auto-increment sequence range from the master node to ensure that the generated auto-increment sequence does not conflict with other nodes.
[0130] Assume that a distributed database cluster of a financial trading platform contains a master node and multiple slave nodes, and each node maintains the current frozen sequence number. The current cluster's frozen sequence number is 10, the global auto-increment sequence value of the master node is 5000, and the local auto-increment sequence range of the slave node A cache is 5000-5099.
[0131] Sequence request and allocation in normal state:
[0132] When the frozen sequence numbers are consistent, slave node A continues to obtain the auto-increment sequence from the local cache. At this time, the cluster state is stable, and the frozen sequence numbers of the master node and the slave node are both 10.
[0133] Node offline causes frozen sequence number update:
[0134] Assume that slave node B is offline due to a network failure, and the frozen sequence number of the cluster is updated from 10 to 11. The master node detects the change in the frozen sequence number and updates the frozen sequence number in the relationship description to 11.
[0135] The slave node detects the frozen sequence number change and re-requests the sequence:
[0136] When slave node A detects that the frozen sequence number has changed from 10 to 11, slave node A will send a new auto-increment sequence request to the master node. The master node reallocates the auto-increment sequence according to the new frozen sequence number to ensure the consistency of the sequence number.
[0137] Frozen sequence number recovery after master node failure:
[0138] If the master node fails and is replaced by a slave node as the new master node, the new master node will read the maximum auto-increment sequence value of the relationship table from the disk to ensure the continuity of the global auto-increment sequence value, and update the frozen sequence number to 12. After the frozen sequence number is synchronized, the nodes in the cluster will return to stability and continue to use the new auto-increment sequence.
[0139] Furthermore, when a node in the cluster goes offline or a new node is added, the master node marks the change in cluster status by increasing the frozen sequence number, and continues to assign new auto-increment sequence values after all nodes resynchronize the frozen sequence numbers.
[0140] In detail, when a node in the cluster goes offline, recovers, or a new node joins, the master node automatically detects the change in cluster status. The master node marks the update of the cluster status by increasing the freeze sequence number and broadcasts the updated freeze sequence number to all slave nodes.
[0141] The freeze sequence number is an integer value that identifies the state of the cluster. It increases as the node state (such as offline, recovery, and addition) changes. The increase in the freeze sequence number indicates that the state of the cluster has changed. Each node confirms whether the cluster state is consistent by synchronizing the freeze sequence number.
[0142] When the master node detects that a node is offline or newly added, it will automatically increase the frozen sequence number and update the cluster status to ensure the consistency of the global auto-increment sequence.
[0143] When the master node increases the frozen sequence number, it broadcasts the updated frozen sequence number to all slave nodes. After receiving the new frozen sequence number, the slave node compares it with the locally stored frozen sequence number. If the local frozen sequence number is found to be outdated, the slave node will resynchronize the latest frozen sequence number. The master node synchronizes the new frozen sequence number to all slave nodes in the cluster. After each slave node receives the updated frozen sequence number, it synchronizes the cluster status and re-applies for a new local auto-increment sequence as needed to ensure the uniqueness and continuity of the auto-increment sequence.
[0144] After all nodes have completed the synchronization of frozen sequence numbers, the master node continues to allocate new auto-increment sequences. At this point, the frozen sequence numbers of all nodes have been kept consistent, and the system can normally allocate and use new auto-increment sequence numbers. By synchronizing the frozen sequence numbers, each node can ensure the consistency of the cluster state, thereby avoiding the problem of discontinuous or repeated auto-increment sequence numbers caused by node disconnection or addition.
[0145] Assume that a distributed database cluster of an online trading platform contains a master node and three slave nodes. The current frozen sequence number of the cluster is 20, the global auto-increment sequence value of the master node is 7000, and the slave nodes A and B cache local auto-increment sequence values in the range of 7000-7099 and 7100-7199 respectively.
[0146] Assume that slave node C is offline due to a network problem. After the master node detects this state change, it automatically increases the freeze sequence number from 20 to 21 and broadcasts the new freeze sequence number to other slave nodes A and B to mark the change in cluster status.
[0147] After receiving the new frozen sequence number from nodes A and B, they will compare it with the locally stored frozen sequence number and find that the local frozen sequence number is 20, which is lower than the current frozen sequence number 21. Therefore, nodes A and B will synchronize the latest frozen sequence number and prepare to continue processing requests for the auto-increment sequence.
[0148] When all nodes have completed the synchronization of frozen sequence numbers, slave nodes A and B can normally request new local auto-increment sequences. For example, when the local sequence of node A is exhausted, it requests a new auto-increment sequence from the master node. The master node allocates a new set of local auto-increment sequences for node A, ranging from 7200 to 7299, based on the updated global auto-increment sequence value 7200.
[0149] Assuming that a slave node D is added to the cluster, the master node will increase the frozen sequence number from 21 to 22 again and broadcast the latest frozen sequence number to all existing nodes. At this time, all nodes will synchronize the frozen sequence number again to ensure that the auto-increment sequence allocation in the cluster remains consistent.
[0150] Furthermore, when the master node fails or crashes, the slave nodes in the cluster automatically elect a new master node. The new master node recalculates the global auto-increment sequence value based on the auto-increment value of the relationship table on the disk and reallocates the local auto-increment sequence to the slave nodes.
[0151] In detail, when the master node in the cluster fails or goes down, the other slave nodes can automatically detect the unavailable state of the master node. The system automatically selects a new master node from the remaining slave nodes through a distributed election algorithm to replace the role of the original master node. The distributed election algorithm is an algorithm used in distributed systems. When the master node fails, the slave nodes collaborate to select a new master node to ensure the continuous operation of the system. The election process is usually based on criteria such as node priority and node ID. The automatic election mechanism can quickly restore the master node role of the system, thereby ensuring that the system can maintain high availability in the event of a master node failure.
[0152] After the new master node is elected, it will read the latest state of the auto-increment sequence from the relationship table information on the disk, including the maximum auto-increment sequence value of the relationship table, and use this value as the starting point of the current global auto-increment sequence value to ensure the continuity and global uniqueness of the auto-increment sequence. Relationship table information refers to the metadata information of each relationship table stored on the disk, including the maximum auto-increment sequence value of the table. The new master node obtains the latest state of the global auto-increment sequence by reading this information, ensuring that the reallocated auto-increment sequence is consistent with the sequence before the failure. Reading disk information ensures that the global auto-increment sequence of the new master node can continue from the state before the failure, avoiding data inconsistency.
[0153] After calculating the current global auto-increment sequence value, the new master node will reallocate local auto-increment sequences to the slave nodes. Each slave node continues to independently generate auto-increment sequence numbers based on the local auto-increment sequence assigned by the new master node, ensuring that the auto-increment sequence allocation in the system can be seamless. The new master node allocates an independent sequence number range to each slave node based on the global auto-increment sequence value, which is used for the local cache of the slave node to support subsequent insertion operations. By reallocating the local auto-increment sequence, the new master node can ensure that the sequence number generation of each slave node remains unique and continuous, thereby restoring the normal operation of the system.
[0154] Assume that a financial institution has a distributed database cluster with one master node and three slave nodes, and the current global auto-increment sequence value of the master node is 9000. When the master node fails, the system is interrupted, and other slave nodes detect that the master node has failed and start the automatic election process.
[0155] The slave nodes automatically elect a new master node:
[0156] Assume that slave nodes A, B, and C detect that the master node is unavailable, and determine slave node B as the new master node based on the distributed election algorithm.
[0157] The new master node recalculates the global auto-increment sequence value:
[0158] The new master node B reads the latest auto-increment sequence information of the relationship table from the disk and finds that the maximum auto-increment sequence value of the relationship table is 9000. Therefore, the new master node uses this value as the starting point of the current global auto-increment sequence value to ensure the continuity of the auto-increment sequence.
[0159] The new master node redistributes the local auto-increment sequence to the slave nodes:
[0160] After determining the starting point of the global auto-increment sequence value, the new master node B continues to allocate local auto-increment sequences to other slave nodes. For example, the local auto-increment sequence of 9000-9099 is allocated to slave node A, and 9100-9199 is allocated to slave node C. After the allocation is completed, the system resumes normal operation.
[0161] Through this example, it can be seen that the present invention can quickly restore the system's auto-increment sequence allocation function when a master node fails, thereby maintaining high availability of the database cluster.
[0162] The embodiment of the present application further provides a distributed self-increment sequence synchronization device, comprising:
[0163] The node setting module is used to set a master node and multiple slave nodes in a distributed database cluster. The master node is used to maintain the global auto-increment sequence value;
[0164] The auto-increment sequence allocation module is used to request the master node to allocate a local auto-increment sequence according to the preset cache parameters at the slave node, and the master node calculates the starting value of the local auto-increment sequence according to the global auto-increment sequence value, and allocates a preset number of continuous auto-increment sequence values to the slave node;
[0165] The slave node cache module is used to cache the local auto-increment sequence locally;
[0166] The allocation request module is used to send a new allocation request to the master node when the local auto-increment sequence cached by the slave node is exhausted. The master node reallocates a new local auto-increment sequence according to the current global auto-increment sequence value.
[0167] In detail, the node setting module is used to set up a master node and multiple slave nodes in a distributed database cluster, and define the role division of the master node and the slave node. The master node is used to maintain the global auto-increment sequence value, and the slave node is responsible for requesting the local auto-increment sequence from the master node when receiving an insert request. The master node is responsible for maintaining the global auto-increment sequence value of the entire cluster and distributing it to ensure the global uniqueness of the auto-increment sequence. The slave node caches the local auto-increment sequence value locally to reduce frequent requests to the master node, thereby improving system performance.
[0168] The auto-increment sequence allocation module is used to allocate local auto-increment sequences from the master node when requested by the slave node according to the preset cache parameters. The master node calculates the starting value of the local auto-increment sequence based on the current global auto-increment sequence value, and allocates a preset number of consecutive auto-increment sequence values to the slave node. The cache parameter defines the number of auto-increment sequences that the slave node can obtain from the master node at one time, which is used to improve the processing efficiency of the slave node and reduce the load on the master node. When the local auto-increment sequence of the slave node is exhausted, the auto-increment sequence allocation module will continue to allocate new local auto-increment sequences from the master node to ensure the continuous availability of the slave node.
[0169] The slave node cache module is used to cache the local auto-increment sequence values received by the slave node locally for use by the slave node when processing insert requests. This cache mechanism can reduce the slave node's dependence on the master node and improve the efficiency of auto-increment sequence allocation. The local cache mechanism allows the slave node to independently process insert requests for a period of time after receiving the auto-increment sequence value. Through caching, the system avoids sending a request to the master node every time an auto-increment sequence is generated, thereby effectively reducing network communication and master node load.
[0170] The allocation request module is used to send a new allocation request to the master node when the local auto-increment sequence cached by the slave node is exhausted. After receiving the request, the master node recalculates and allocates a new local auto-increment sequence value based on the current global auto-increment sequence value to ensure that the slave node continues to process the insert operation. The allocation request module ensures that when the local cache of the slave node is exhausted, it can request a new local auto-increment sequence value from the master node in time to ensure the continuous operation of the system. The role of this module is to dynamically request and allocate new sequences to ensure the stability and scalability of the system under high concurrency.
[0171] Corresponding to the above method embodiment, the embodiment of the present invention further provides a computer device. The computer device described below and the distributed self-increment sequence synchronization method described above can refer to each other.
[0172] The computer equipment includes:
[0173] Memory for storing computer programs;
[0174] The processor is used to implement the steps of the distributed self-incrementing sequence synchronization method of the above method embodiment when executing a computer program:
[0175] S1. In a distributed database cluster, set up a master node and multiple slave nodes. The master node is used to maintain the global auto-increment sequence value;
[0176] S2. The slave node requests the master node to allocate a local auto-increment sequence according to the preset cache parameters. The master node calculates the starting value of the local auto-increment sequence according to the global auto-increment sequence value, and allocates a preset number of continuous auto-increment sequence values to the slave node.
[0177] S3. The slave node caches the local auto-increment sequence locally;
[0178] S4. When the local auto-increment sequence cached by the slave node is exhausted, a new allocation request is sent to the master node, and the master node reallocates a new local auto-increment sequence based on the current global auto-increment sequence value.
[0179] Figure 6 A schematic diagram of the structure of a computer device provided in an embodiment of the present application, which may be a computing device with data processing capabilities.
[0180] The device may include: a processor 301 and a memory 302 .
[0181] The memory 302 is used to store programs, and the processor 301 calls the programs stored in the memory 302 to execute the above method embodiment. The specific implementation method and technical effect are similar and will not be repeated here.
[0182] The memory 302 stores program codes, and when the program codes are executed by the processor 301, the processor 301 executes various steps of the method according to various exemplary embodiments of the present application described in the above “Exemplary Method” section of this specification.
[0183] The processor 301 may be a general-purpose processor, such as a central processing unit (CPU), a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components, and may implement or execute the methods, steps and logic block diagrams disclosed in the embodiments of the present application. A general-purpose processor may be a microprocessor or any conventional processor, etc. The steps of the method disclosed in the embodiments of the present application may be directly embodied as being executed by a hardware processor, or may be executed by a combination of hardware and software modules in the processor.
[0184] The memory 302, as a non-volatile storage medium, can be used to store non-volatile software programs, non-volatile computer executable programs and modules. The memory may include at least one type of storage medium, such as a flash memory, a hard disk, a multimedia card, a card-type memory, a random access memory (Random Access Memory, RAM), a static random access memory (Static Random Access Memory, SRAM), a programmable read-only memory (ProgrammableRead Only Memory, PROM), a read-only memory (Read Only Memory, ROM), an electrically erasable programmable read-only memory (Elect rically Erasable Programmable Read-Only Memory, EEPROM), a magnetic memory, a disk, an optical disk, etc. The memory is any other medium that can be used to carry or store a desired program code in the form of an instruction or data structure and can be accessed by a computer, but is not limited thereto. The memory 302 in the embodiment of the present application can also be a circuit or any other device that can realize a storage function, for storing program instructions and / or data.
[0185] Optionally, the present application also provides a program product, such as a storage medium, including a program, which is used to execute the above method embodiment when executed by a processor.
[0186] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0187] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0188] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of hardware plus software functional units.
[0189] The above-mentioned integrated unit implemented in the form of a software functional unit can be stored in a computer-readable storage medium. The above-mentioned software functional unit is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor (English: processor) to perform some steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (English: Read-Only Memo ry, abbreviated: ROM), random access memory (English: Random Access Memory, abbreviated: RAM), disk or optical disk and other media that can store program codes.
Claims
1. A distributed self-incrementing sequence synchronization method, characterized in that: include: In a distributed database cluster, one master node and multiple slave nodes are set, and the master node is used to maintain a global auto-increment sequence value; The slave node requests the master node to allocate a local auto-increment sequence according to a preset cache parameter, and the master node calculates a starting value of the local auto-increment sequence according to a global auto-increment sequence value, and allocates a preset number of continuous auto-increment sequence values to the slave node; The slave node caches the local auto-increment sequence locally; When the local auto-increment sequence cached by the slave node is exhausted, a new allocation request is sent to the master node, and the master node reallocates a new local auto-increment sequence according to the current global auto-increment sequence value.
2. The distributed self-incrementing sequence synchronization method according to claim 1, characterized in that: After the slave node receives the local auto-increment sequence assigned by the master node, when the slave node receives a request from a user to insert a record, the slave node performs the following steps according to whether the user specifies an auto-increment value: If the user does not specify an auto-increment value, the slave node uses the local auto-increment sequence start value in the cache and increments the value after use; If the user specifies an auto-increment value, and the auto-increment value is greater than the maximum value of the local auto-increment sequence, the slave node requests a new local auto-increment sequence range from the master node; the master node calculates the starting value of the local auto-increment sequence according to the specified auto-increment value, and returns it to the slave node; If the auto-increment value specified by the user is less than the maximum value of the local auto-increment sequence, the slave node returns the auto-increment value and updates the starting value of the local auto-increment sequence to the specified auto-increment value plus 1.
3. The distributed self-incrementing sequence synchronization method according to claim 2, characterized in that: When the slave node requests a new local auto-increment sequence, the master node recalculates the starting value of the local auto-increment sequence based on the current global auto-increment sequence value and the auto-increment value specified by the user, and allocates a preset number of continuous auto-increment sequences; after receiving feedback from the master node, the slave node updates the local local auto-increment sequence starting value and the local auto-increment sequence maximum value.
4. The distributed auto-increment sequence synchronization method according to any one of claims 1 to 3, characterized in that: When the master node receives the self-increment sequence request from the slave node, the master node performs the following steps: When the request comes from this node, if the user-specified auto-increment value is greater than or equal to the global auto-increment sequence value, the master node returns the specified value and updates the global auto-increment sequence value to the specified auto-increment value plus 1; If the user does not specify an auto-increment value, the master node returns the current global auto-increment sequence value and increments the global auto-increment sequence value; When the request comes from the slave node, if the auto-increment request value applied for allocation is greater than or equal to the current global auto-increment sequence value, the master node uses the request value as the starting value of the local auto-increment sequence, allocates a preset number of consecutive auto-increment sequences, and updates the global auto-increment sequence value to the maximum value of the allocated sequence.
5. The distributed self-incrementing sequence synchronization method according to claim 4, characterized in that: If the auto-increment request value requested by the slave node is less than the current global auto-increment sequence value, the master node uses the current global auto-increment sequence value as the starting value of the local auto-increment sequence and allocates a preset number of consecutive auto-increment sequences; the master node returns the allocated local auto-increment sequence to the slave node, and updates the global auto-increment sequence value to the maximum value of the allocated sequence.
6. The distributed self-incrementing sequence synchronization method according to claim 1, characterized in that: The method further comprises: The master node and the slave node respectively maintain frozen serial numbers in the distributed database cluster, and the frozen serial numbers are used to indicate the state change of the cluster, including the joining or disconnection of a node; When adding an auto-increment sequence attribute to the relationship table, the master node records the frozen sequence number of the current cluster in the relationship description in the memory; When the slave node requests the master node to allocate an auto-increment sequence, the master node first checks whether the frozen sequence number in the relationship description is consistent with the frozen sequence number of the current cluster; If the frozen sequence numbers are consistent, the master node allocates an auto-increment sequence to the slave node according to the current process; If the frozen sequence numbers are inconsistent, it means that the cluster status has changed, and the master node recalculates the global auto-increment sequence value according to the latest status of the cluster; If the master node itself is transformed from the slave node to the new master node, the master node reads the maximum auto-increment sequence value of the relationship table from the disk as the new global auto-increment sequence value, and updates the frozen sequence number; When the slave node detects that the frozen sequence number has changed, it re-applies for the local auto-increment sequence from the master node, and updates the local local auto-increment sequence start value and maximum value to the latest sequence range allocated by the master node.
7. The distributed self-incrementing sequence synchronization method according to claim 6, characterized in that: When a node in the cluster is disconnected or a new node is added, the master node marks the change in the cluster state by increasing the frozen sequence number, and continues to allocate new auto-increment sequence values after all nodes resynchronize the frozen sequence numbers.
8. The distributed self-incrementing sequence synchronization method according to claim 6 or 7, characterized in that: When the master node fails or crashes, the slave nodes in the cluster automatically elect a new master node. The new master node recalculates the global auto-increment sequence value based on the auto-increment value of the relationship table in the disk and reallocates the local auto-increment sequence to the slave nodes.
9. A distributed self-incrementing sequence synchronization device, characterized in that: include: A node setting module, used to set one master node and multiple slave nodes in a distributed database cluster, wherein the master node is used to maintain a global auto-increment sequence value; An auto-increment sequence allocation module is used to request the master node to allocate a local auto-increment sequence according to a preset cache parameter at the slave node, and the master node calculates the starting value of the local auto-increment sequence according to the global auto-increment sequence value, and allocates a preset number of continuous auto-increment sequence values to the slave node; The slave node cache module is used to cache the local auto-increment sequence locally; The allocation request module is used to send a new allocation request to the master node when the local auto-increment sequence cached by the slave node is exhausted, and the master node reallocates a new local auto-increment sequence according to the current global auto-increment sequence value.
10. A computer device, characterized in that: include: A processor, a storage medium and a bus, wherein the storage medium stores program instructions executable by the processor, and when the device is running, the processor communicates with the storage medium via the bus, and the processor executes the program instructions to perform the steps of the distributed self-incrementing sequence synchronization method as described in any one of claims 1 to 8.
11. A storage medium, characterized in that: The storage medium stores a computer program, and when the computer program is executed by the processor, the steps of the distributed self-incrementing sequence synchronization method according to any one of claims 1 to 8 are executed.
Citation Information
Patent Citations
Parallel data processing method based on distributed structure
CN103412897A
Implementation method and device of serial number generation method for ensuring high availability of service
CN114172792A
Data caching method and system based on high availability
CN115562849A
System and method for distributing data in a parallel processing system
US7873650B1
Cited By
Fine-grained disaster recovery method and device for analytical distributed database, and storage medium
CN122285392A
Fine-grained disaster recovery methods, devices, and storage media for analytical distributed databases
CN122285392B