A method for implementing high-availability sequences in distributed databases
By using multiple computing nodes and sequence libraries in a distributed database and designing custom algorithms and caching mechanisms, the high availability and uniqueness issues of sequence values in a distributed database are solved, efficient and reliable sequence value generation is achieved, and the development and operation and maintenance processes are simplified.
Patent Information
- Application Number
- CN202310139533.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-02-20
- Publication Date
- 2025-09-12
- Estimated Expiration
- 2043-02-20
AI Technical Summary
In distributed databases, traditional sequence value generation methods have problems such as low high availability, difficulty in ensuring sequence value repeatability, and high development and operation complexity. Especially in high-concurrency scenarios, it is difficult to meet business needs.
In a distributed database, by directly providing sequence value services on multiple computing nodes, using two sequence libraries to provide services in parallel, designing a custom algorithm to ensure the uniqueness and high availability of sequence values, using a single-machine relational database to persist sequence information, and computing nodes caching sequence values to improve concurrency.
It achieves high reliability, non-repeatability and high performance of sequence values, simplifies the development and operation and maintenance of application systems, improves the concurrency and availability of sequence generation, and avoids the time rollback and additional resource requirements in traditional methods.
Smart Images

Figure CN116028505B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of distributed databases, and in particular relates to a method for realizing high-availability sequences in a distributed database. Background Art
[0002] With the rapid development of computer technology and the ever-expanding business scope of its applications, more and more data is being generated and recorded in databases. Over time, the amount of data in a single database table has grown, leading to a significant decline in read and write performance. Furthermore, the traditional single-node, standalone deployment model of relational databases fails to address high availability and can no longer meet the high-performance, high-availability, and high-concurrency requirements of business applications. Consequently, in recent years, more and more enterprises have begun transitioning to distributed databases. To address the large volume of data in a single table and the resulting decline in read and write performance, distributed databases split a table from a single-node relational database across multiple standalone relational databases, using records as the dimension. To provide enhanced read and write performance and support higher concurrency, distributed databases typically deploy multiple compute nodes within a distributed database to parse and execute SQL statements, assemble SQL execution results, and perform operations. Users can connect to any of the compute nodes through a distributed database client to perform operations. In a standalone relational database, a single table can use a primary key sequence to generate a unique value within the table to identify each record. In a distributed database, when a single table is split into different stand-alone relational databases, there is a need for a function that can provide unique sequence values across multiple tables.
[0003] Traditional unique sequence value solutions rely on a separate system deployed independently from the distributed database. Users must first connect to the system to obtain the independent sequence value, assemble SQL statements, and then send them to the distributed database. For example, storing a continuously incrementing key in Redis offers the advantage of mature and reliable solutions that provide unique sequence values. However, deploying the system independently requires additional hardware and software, additional development and operations personnel, and requires users to maintain connections to two systems simultaneously, which undoubtedly complicates the work of application system developers and operations personnel.
[0004] Traditional unique sequence value implementations often rely on memory to ensure higher concurrency. For example, the snowflake algorithm uses the computer clock as a seed and a specific algorithm to generate a unique sequence value. While these methods offer advantages such as simplicity and high performance, they also suffer from low availability and lack effective solutions for machine clock rollbacks, making duplicate sequence values more likely to occur. Consequently, memory-based methods are typically used in situations where uniqueness requirements are less stringent. Summary of the Invention
[0005] The present invention aims to solve the defects and problems of the existing technology. In a distributed database, sequence values need to take into account high availability, high performance, and strict non-repeatability, so that the distributed database can provide sequence values more efficiently, reliably, and securely. The present invention uses multiple computing nodes in the distributed database to directly provide sequence values and automatically updates the sequence cache. Two sequence libraries are used to provide sequence services at the same time to ensure the high availability of sequence services. The uniqueness of sequences between different sequence libraries is achieved through customized algorithm rules to ensure the uniqueness of sequence values in the entire distributed system. Application developers no longer need to worry about non-business issues such as sequence acquisition and duplication of sequence values, so they can focus their work on their own business.
[0006] To solve the above technical problems, the present invention provides a method for implementing a high-availability sequence in a distributed database, which specifically includes:
[0007] Step 1: Create a sequence library in each of two unrelated databases. To ensure the stability of the sequence library, the maximum number of connections allowed to a sequence library at the same time should be limited. If the number of requests from the current computing node at the same time exceeds the maximum number of connections set by the sequence library, the sequence library should refuse to accept new requests.
[0008] Step 2: Create a sequence table in each of the two sequence libraries created in Step 1. The field information of the sequence table includes: table primary key, sequence name, sequence starting value, sequence step, and the number of sequence values that a single computing node should cache. The sequence name is used to distinguish different sequences. The sequence starting value is the initial value when calculating the sequence. Each newly generated sequence is updated in the sequence table. The sequence step is the growth rate of the sequence value, which is recorded as the difference between every two consecutive sequences.
[0009] Step 3: Create a stored procedure in each of the two sequence libraries created in Step 1. The stored procedure is a program fragment used to process specific complex logic. The stored procedure receives a sequence name as an input parameter and outputs a string as an output parameter. The stored procedure converts the received sequence name to uppercase, finds the corresponding sequence record in the sequence table based on the sequence name, and updates its sequence start value according to a preset algorithm. If the update is successful, it returns a string in a specified format; if the update fails, it returns a preset string.
[0010] Step 4: After step 3 is completed, create sequence records in the two sequence tables respectively; specifically, create a sequence record with the same sequence name in each of the two sequence tables, the sequence step lengths of the two sequence records with the same name are the same and are multiples of 10, the units digit of the sequence starting value is the same, but the tens digit is an odd number and the tens digit is an even number;
[0011] Step 5: The computing node receives the user's request to extract a sequence, and searches the local cache for an available sequence based on the sequence name. If an available sequence is available, a sequence value is consumed and returned to the user. If no sequence is available, the sequence name is sent to the sequence library to obtain the sequence. Specifically, the computing node confirms whether the sequence library is normal through heartbeat detection. If one of the sequence libraries is abnormal, a sequence is generated in the other sequence library. If both sequence libraries are normal, one of them is randomly selected for sequence generation. If a failure occurs, another library is reselected to generate the sequence. If the heartbeat detection results of both sequence libraries are abnormal, the computing node will continue to wait until the sequence library returns to normal. The computing node calls the stored procedure on the selected sequence library to obtain the return value.
[0012] Step 6: After receiving the request from the computing node, the sequence library returns a string in the specified format to the computing node;
[0013] Step 7: After the computing node obtains the string in the specified format returned by the sequence library, it calculates the sequence value and returns it to the user.
[0014] In step 1, the single-point database is created on an independently deployed MySQL; the sequence is generated by any one of the two sequence libraries.
[0015] In step 2, the table primary key name is id, the type is BIGINT, the length is 20, and the primary key auto-increment attribute of the stand-alone database is used; the field seq_name is used to store the sequence name, the type is VARCHAR, and the length is 255; the field curr_val stores the starting value of the sequence, the type is BIGINT, and the length is 20; the field step_val is used to save the step length of the sequence, the type is INT, and the length is 11; the field cache_nums is used to save the cache number of the computing node, the type is INT, and the length is 11; create a unique index on the field seq_name.
[0016] In step 3, the stored procedures in the two sequence libraries are the same, both are written in SQL and deployed on a specific MySQL. The stored procedure is named focus_sequence_func and is of type FUNCTION. The above stored procedure defines a receiving parameter of type VACHAR, length 255, name v_seq_name, return type VARCHAR, length 64, and the string in the specified format is a string consisting of a sequence starting value, a string link symbol, a numerical value to be added to the sequence starting value, a string link symbol, and a sequence step; the preset string includes -1 and -2; the preset algorithm is: new sequence starting value = current sequence starting value + number of sequences that the computing node should cache × sequence step.
[0017] The specific steps in step 3 include:
[0018] Step 3-1: Predefine four variables: v_curr_val of long type, v_step_val of int type, v_increment of int type, and v_updateRowCount of int type; these four variables are used to store the sequence starting value, sequence step length, the value to be increased from the sequence starting value, and the number of sequence records to be changed, respectively;
[0019] Step 3-2: Convert the incoming v_seq_name to uppercase letters, convert the seq_name in the table to uppercase letters, and query the sequence records with the same two values; calculate the v_increment value using the formula: v_increment = cache_nums * step_val; determine whether the v_cur_val value is empty. If it is empty, return the string -2; if not, continue with step 3-3;
[0020] Step 3-3: Calculate the new starting value of the sequence using the formula: new v_curr_val = v_curr_val + v_increment. Use the new v_curr_val value to update the sequence records queried in step 3-2. Determine the number of updated sequence records. If the number is 0, return the string -1. Otherwise, return the specified format string. The string format is: v_curr_val value, English comma, v_increment value, English comma, v_step_val value.
[0021] In step 5, after the computing node is started, heartbeat detection is established according to the address, account number, password, and two sequence libraries. The computing node initializes a Map data structure, uses the schema name of the distributed database concatenated with the ## symbol and then the sequence name as the key, and uses a sequence value queue as the value. The user requests the sequence value of the specified name from the computing node, and the computing node executes the following steps:
[0022] Step 5-1: The computing node uses the key to query the Map data structure. If the obtained value is not empty, it executes step 5-2. If the obtained value is empty, it constructs a new empty sequence value queue and saves it in the Map data structure, and continues to execute step 5-2.
[0023] Step 5-2: Check the sequence value queue obtained in step 5-1. If it is not empty, extract a specific sequence value from it and return it to the requester. If the sequence value queue is empty, execute step 5-4. Check the length of the sequence value queue. If the previous length of the sequence value queue is greater than 100 and the current length of the sequence value queue is less than half of the previous length, execute step 5-4. If the previous length of the sequence value queue is less than 100 and the current length of the sequence value queue is less than 50, execute step 5-4. If the length of the sequence value queue does not meet the above two conditions, execute step 5-3.
[0024] Step 5-3: The computing node completes the return sequence value operation and enters the waiting state for the next request.
[0025] Step 5-4: The computing node requests the sequence library to generate a new batch of sequence values; checks whether the two sequence libraries are normal. If normal, randomly calls the stored procedure of one of them to obtain the return result string; if the string is -1 or -2, repeats steps 5-4 for the other sequence library; otherwise, parses the returned string and executes step 7.
[0026] In step 7, after receiving the string in the specified format, the computing node parses and extracts the sequence starting value, sequence growth value and sequence step size, calculates the sequence value, and returns one of the sequence values to the user, and caches the remaining sequence values in the local memory for next use.
[0027] In step 7, the calculation node calculates the sequence value, including: the first step, calculating the maximum value of the sequence, the calculation formula is: sequence maximum value = sequence starting value + sequence growth value; the second step, calculating the specific sequence values one by one, the calculation formula is: sequence value = sequence starting value + sequence step length; the third step, judging whether the second step sequence value is greater than the first step sequence maximum value, if so, the sequence generation step ends, if not, the second step is repeated.
[0028] Step 7 also includes updating the local sequence cache with the calculated sequence value; extracting v_curr_val, v_increment and v_step_val in sequence from the return value of the sequence library according to the rules in step 3-3; increasing the value v_step_val successively based on the returned v_curr_val; after each addition, if the new v_curr_val is greater than or equal to the sum of the returned v_curr_val and v_increment, the operation is terminated; if the new v_curr_val is less than the sum of the returned v_curr_val and v_increment, the value is placed in the sequence value queue obtained in step 5-2.
[0029] The beneficial effects achieved by the present invention are:
[0030] (1) The present invention solves the problem that traditional sequences cannot achieve reliable persistence by using memory to store sequences by using a stand-alone relational database to persist sequence information.
[0031] (2) The present invention ensures high reliability of sequence generation by using multiple, at least two, sequence libraries;
[0032] (3) The present invention designs a new sequence generation algorithm to ensure that sequence values never repeat, thus avoiding the problems of time rollback and excessive sequence value length faced by traditional time-based snowflake algorithms;
[0033] (4) The present invention improves the concurrency of the sequence generation function and enhances performance by caching sequence values in multiple computing nodes and providing sequence services to users by the computing nodes. BRIEF DESCRIPTION OF THE DRAWINGS
[0034] Figure 1 A flowchart of implementing a high-availability sequence in a distributed database according to an embodiment of the present invention;
[0035] Figure 2 A schematic diagram of the connection between the business system and the computing node in an embodiment of the present invention;
[0036] Figure 3 Schematic diagram of the timing of user request sequence and computing node response in an embodiment of the present invention. DETAILED DESCRIPTION
[0037] In order to make the purpose, technical solutions and advantages of the present invention clearer, the initialization and use of the sequence service will be described in detail below with reference to the accompanying drawings:
[0038] Figure 1 This is a flow chart of a high-availability client load balancing method based on a database middleware cluster in an embodiment of the present invention, which specifically includes:
[0039] Step 1: Create two sequence libraries; initialize two single-point databases as sequence libraries. Specifically: create a database on each of the two MySQL servers. MySQL is a stand-alone relational database that is used as part of the underlying data storage of a distributed database. The sequence library is used to store basic sequence information and also to generate specific sequence values. Initialization parameters include: sequence library name, sequence library character set, sequence library account, sequence library password, and maximum number of connections. The maximum number of connections is used to limit the maximum number of computing nodes a sequence library can maintain connections with. If the current number of computing nodes is greater than the set maximum number of connections, new computing node requests will be rejected.
[0040] The use of at least two (or more) sequence libraries ensures high reliability of sequence generation.
[0041] Step 2: Create a sequence table in each of the two sequence libraries. Specifically, create a sequence table in each of the two sequence libraries. The field information of the sequence table includes: table primary key, sequence name, sequence starting value, sequence step size, and number of cached sequence values. The sequence name is used to distinguish different sequences. The sequence starting value refers to the starting value calculated by the generation algorithm each time a sequence is generated. The sequence step size refers to the growth rate of the sequence value, that is, the difference between each two consecutive sequences. The number of cached sequence values refers to the total number of sequence values generated each time a sequence value is generated.
[0042] The table's primary key is named id, of type BIGINT and length 20, using the auto-increment property of the primary key in a stand-alone database. The seq_name field is used to store the sequence name, of type VARCHAR and length 255. The curr_val field stores the starting value of the sequence, of type BIGINT and length 20. The step_val field is used to store the sequence step length, of type INT and length 11. The cache_nums field is used to store the cache number of the computing node, of type INT and length 11. Create a unique index on the seq_name field.
[0043] By using a stand-alone relational database to persist sequence information, the problem that traditional sequences cannot achieve reliable persistence when using memory to store sequences is solved.
[0044] Step 3: Create stored procedures in the two sequence libraries respectively. Specifically: A stored procedure is a program fragment written in SQL that can be deployed on a specific MySQL to process specific complex logic. It receives a parameter sequence name and outputs a specific numerical value. The above stored procedure will first convert the received sequence name to uppercase, and then find the corresponding record in the sequence table based on the sequence name. Update its sequence starting value according to a specific preset algorithm. If the update is successful, the result is returned in the specified format. If the update fails, the number -1 is returned. The rules of the aforementioned preset algorithm are: new sequence starting value = current starting value of the sequence + number of sequence caches * sequence growth step. The aforementioned return format is: a string consisting of sequence starting value, English comma, sequence growth value, English comma and sequence growth step. The specific process of the aforementioned stored procedure includes:
[0045] Step 3-1: Predefine four variables: v_curr_val of long type, v_step_val of int type, v_increment of int type, and v_updateRowCount of int type. These four variables are used to store the starting value of the sequence, the step length of the sequence, the cumulative increase of the sequence, and the number of changed sequence records, respectively.
[0046] Step 3-2: Convert the incoming v_seq_name to uppercase, convert the seq_name in the table to uppercase, and find the sequence records with the same value. Calculate the v_increment value using the formula: v_increment = cache_nums * step_val. Determine whether the v_cur_val value is empty. If it is empty, return the string -2. If it is not empty, continue with step 3-3.
[0047] Step 3-3: Calculate the new starting value for the sequence using the formula: new v_curr_val = v_curr_val + v_increment. Use the new v_curr_val value to update the sequence records found in step 3-2. Determine the number of records affected by the update operation. If the number is zero, return a string of -1; otherwise, return a string in the specified format: v_curr_val value, comma, v_increment value, comma, v_step_val value.
[0048] The stored procedure code snippet used in step 3 is as follows:
[0049]
[0050] By designing a new sequence generation algorithm, we ensure that sequence values will never be repeated, avoiding problems such as time rollback and excessive sequence value length faced by traditional time-based snowflake algorithms.
[0051] Step 4: After step 3 is completed, create sequence records in the two sequence tables according to actual needs. Specifically, for a sequence with the same name, create a sequence record in each of the two sequence tables. The only difference between the two records is the parity of the starting value of the ten digit, for example, one is 0 and the other is 1. The other digits must remain consistent.
[0052] Step 5: The computing node responds to the user's request to obtain the sequence and checks whether the sequence value is cached in the memory. If so, it returns it; otherwise, it requests the sequence library.
[0053] When a compute node receives a request to retrieve a sequence, it first searches its local cache for an available sequence based on the sequence name. If so, it consumes a value and returns it to the user. If not, it sends the sequence name to the sequence repository to retrieve the sequence. More specifically, the compute node connects to two sequence repositories using its username and password; it randomly selects one of the available repositories; and it calls a stored procedure on the aforementioned repository to obtain the return value.
[0054] The specific process of computing nodes includes:
[0055] Step 5-1: The computing node uses the schema name, concatenated with the ## symbol, and then concatenated with the requested sequence name to form a key; the computing node uses this key to query the Map data structure. If the obtained value is not empty, the computing node executes step 5-2. If the obtained value is empty, the computing node first constructs a new empty sequence value queue and saves it in the Map data structure, and then continues with step 5-2.
[0056] Step 5-2: Check the sequence value queue obtained in step 5-1. If it is not empty, extract a specific sequence value from it and return it to the requester. If the sequence value queue is empty, execute step 5-4. Check the length of the aforementioned sequence value queue. If the length of the previous sequence value queue is greater than 100 and the current sequence value queue length is less than half of the previous length, execute step 5-4. If the length of the previous sequence value queue is less than 100 and the current sequence value queue length is less than 50, execute step 5-4. If the sequence value queue length does not meet the above two conditions, execute step 5-3.
[0057] Step 5-3: The computing node completes the return sequence value operation and enters the waiting state for the next request.
[0058] Step 5-4: The compute node requests the sequence library to generate a new batch of sequence values. Check whether both sequence libraries are functioning properly. If so, randomly call the stored procedure in one of them and obtain the returned result string. If the string is -1, repeat steps 5-4 with the other sequence library. Otherwise, parse the returned string.
[0059] Step 6: The sequence library receives the compute node's request, calls a stored procedure to update the sequence value, and returns the specified result to the compute node. Specifically, when the sequence library receives a request from a compute node for a sequence value with a specific sequence name, it calls a stored procedure within the library, passing the sequence name as a parameter. Upon receiving the sequence name, the stored procedure converts it to uppercase, locates the corresponding record in the sequence table, and calculates a new sequence starting value using a specified algorithm. The stored procedure then returns the calculated new sequence starting value, along with the sequence increment and step size, as a string to the compute node.
[0060] Step 7: The computing node parses the return result, returns the specific sequence value to the user, and caches the remaining sequence values. Specifically: after the computing node obtains the return value of the sequence library, it parses and extracts the sequence starting value, sequence cumulative growth value and sequence step value. The computing node calculates the sequence value according to a certain algorithm and returns one of them to the user. The rest are cached in the local memory for next use. The aforementioned algorithm is divided into three steps: the first step is to calculate the maximum value of this batch of sequences, specifically the maximum value of the sequence = the starting value of the sequence + the cumulative growth value of the sequence; the second step is the sequence value = the starting value of the sequence + the sequence step; the third step is to determine whether the sequence value of the second step is greater than the maximum value of the sequence in the first step. If it is greater, the sequence generation step ends. If it is less, the second step is repeated. The specific process of the aforementioned computing node calculating the sequence value includes: according to the rules in step 3-3, extracting v_curr_val, v_increment and v_step_val in sequence. Based on the returned v_curr_val, the value v_step_val is increased successively. After each addition, if the new v_curr_val is greater than or equal to the sum of the returned v_curr_val and v_increment, the operation ends. If the new v_curr_val is less than the sum of the returned v_curr_val and v_increment, the value is placed in the sequence value queue obtained in step 5-2.
[0061] The code snippet used to calculate the sequence value in step 7 is:
[0062]
[0063] Figure 2 This is a schematic diagram of a structure for implementing a high-availability sequence in a distributed database according to an embodiment of the present invention, including a computing cluster and a storage cluster in the distributed database connected to distributed database users.
[0064] The distributed database users include individual users, business systems, third-party management tools and other software or personnel; the computing cluster consists of at least one computing node; the storage cluster consists of no less than two sequence libraries (sequence library A and sequence library B), each of which has an independent sequence table and a stored procedure for updating the sequence.
[0065] For example, in a distributed database environment, the primary key of a table needs to be globally unique. At the same time, due to the length limit of the primary key field, the common UUID algorithms on the market cannot even be stored in the database table because the field is too long. In this case, we can only rely on the present invention to provide the primary key value.
[0066] For example, the common snowflake algorithm uses a timestamp as a seed when generating a sequence. If the timing service rolls back, duplicate sequences will appear, directly violating the sequence's requirement for permanent uniqueness and causing significant harm to users. However, the present invention leverages a database to timely update the sequence's starting value, ensuring that the sequence always grows in a positive direction and never repeats.
[0067] For example, the commonly used distributed high-availability sequence implementation method of distributed number segments requires deploying a timing cluster and sequence service cluster in addition to the existing system. This not only requires additional hardware and software resources, but also requires maintaining availability between the sequence services. Furthermore, each new request must be split into two calls to different systems. However, this invention is natively integrated into the distributed database, allowing users to use the sequence directly, just like using traditional databases such as Oracle and MySQL, without any additional modifications or learning.
[0068] At present, after more than two years of research and development experiments, the implementation plan designed based on the concept of this case has been fully launched in the company's core business. Under the test of business, no sequence-related accidents have occurred, and it has effectively supported the daily TPS visits of tens of millions.
[0069] Figure 3 This is a time sequence flow chart of a distributed database user request sequence in an embodiment of the present invention, including users, computing nodes, and sequence libraries. The specific steps include:
[0070] Step 301: A distributed database user requests a specific sequence;
[0071] Step 302: The computing node checks the local sequence cache and returns the sequence value if any;
[0072] Step 303: The computing node requests the sequence library to generate a new sequence value;
[0073] Step 304-1: The sequence library calls a stored procedure and updates the sequence start value according to the algorithm;
[0074] Step 304-2: The sequence library returns the stored procedure execution result to the computing node;
[0075] Step 305-1: The computing node obtains the result returned by the sequence library, parses and extracts the specific sequence value, and caches it in the memory of the computing node;
[0076] Step 305 - 2 : The computing node extracts a portion of sequence values from the sequence cache and returns them to the user.
[0077] The present invention provides a method for generating unique high-availability sequence values in a distributed database based on a stand-alone relational database. The stand-alone relational database ensures that the sequence will never be repeated, but does not solve the problem of high availability. The present invention ensures high availability of sequence generation services through two sequence libraries, and the two sequence libraries can provide services to the outside world at the same time. The present invention ensures that sequence values in different sequence libraries will never be repeated by having the same step length but only ten digits different. The present invention improves the performance and concurrency of sequence distribution by caching sequence values in multiple computing nodes. Different computing nodes will automatically supplement or discard the obtained sequence values according to the current cache quantity.
[0078] The beneficial effects achieved by the present invention are:
[0079] (1) The present invention uses a stand-alone relational database to persistently store sequence information, solving the problem that traditional sequences cannot achieve persistence when using memory to store sequences;
[0080] (2) The present invention ensures high reliability of sequence generation capability by using multiple, at least two, sequence libraries;
[0081] (3) The present invention designs a new sequence generation algorithm to ensure that sequence values never repeat, thus avoiding the problems of time rollback and excessive sequence value length faced by traditional time-based snowflake algorithms;
[0082] (4) The present invention improves the concurrency of the sequence generation function and enhances performance by caching sequence values in multiple computing nodes and providing sequence services to users by the computing nodes.
[0083] The above embodiments do not limit the present invention in any way. Any other improvements and applications made to the above embodiments in an equivalent manner fall within the scope of protection of the present invention.
Claims
1. A method for implementing a high-availability sequence in a distributed database, characterized in that: include: Step 1: Create a sequence library in two unrelated databases; To ensure the stability of the sequence library, the maximum number of connections allowed by a sequence library at the same time should be limited. If the number of requests from the current computing node at the same time exceeds the maximum number of connections set by the sequence library, the sequence library should refuse to accept new requests. Step 2: Create a sequence table in each of the two sequence libraries created in Step 1. The field information of the sequence table includes: table primary key, sequence name, sequence starting value, sequence step, and the number of sequence values that a single computing node should cache. The sequence name is used to distinguish different sequences. The sequence starting value is the initial value when calculating the sequence. Each newly generated sequence is updated in the sequence table. The sequence step is the growth rate of the sequence value, which is recorded as the difference between every two consecutive sequences. Step 3: Create a stored procedure in each of the two sequence libraries created in Step 1. The stored procedure is a program fragment used to process specific complex logic. The stored procedure receives a sequence name as an input parameter and outputs a string as an output parameter. The stored procedure converts the received sequence name to uppercase, finds the corresponding sequence record in the sequence table based on the sequence name, and updates its sequence start value according to a preset algorithm. If the update is successful, it returns a string in a specified format; if the update fails, it returns a preset string. Step 4: After step 3 is completed, create sequence records in the two sequence tables respectively; specifically, create a sequence record with the same sequence name in each of the two sequence tables, the sequence step lengths of the two sequence records with the same name are the same and are multiples of 10, the units digit of the sequence starting value is the same, but the tens digit is an odd number and the tens digit is an even number; Step 5: The computing node receives the user's request to extract a sequence, and searches the local cache for an available sequence based on the sequence name. If an available sequence is available, a sequence value is consumed and returned to the user. If no sequence is available, the sequence name is sent to the sequence library to obtain the sequence. Specifically, the computing node confirms whether the sequence library is normal through heartbeat detection. If one of the sequence libraries is abnormal, a sequence is generated in the other sequence library. If both sequence libraries are normal, one of them is randomly selected for sequence generation. If a failure occurs, another library is reselected to generate the sequence. If the heartbeat detection results of both sequence libraries are abnormal, the computing node will continue to wait until the sequence library returns to normal. The computing node calls the stored procedure on the selected sequence library to obtain the return value. Step 6: After receiving the request from the computing node, the sequence library returns a string in the specified format to the computing node; Step 7: After the computing node obtains the string in the specified format returned by the sequence library, it calculates the sequence value and returns it to the user.
2. The method for implementing a high-availability sequence in a distributed database according to claim 1, wherein: In step 1, a single-point database is created on an independently deployed MySQL; and sequences are generated by either of the two sequence libraries.
3. The method for implementing a high-availability sequence in a distributed database according to claim 2, wherein: In step 2, the table primary key name is id, the type is BIGINT, the length is 20, and the primary key auto-increment attribute of the stand-alone database is used; the field seq_name is used to store the sequence name, the type is VARCHAR, and the length is 255; the field curr_val stores the starting value of the sequence, the type is BIGINT, and the length is 20; the field step_val is used to save the step length of the sequence, the type is INT, and the length is 11; the field cache_nums is used to save the cache number of the computing node, the type is INT, and the length is 11; Create a unique index on the field seq_name.
4. The method for implementing a high-availability sequence in a distributed database according to claim 3, wherein: In step 3, the stored procedures in the two sequence libraries are the same, both are written in SQL and deployed on a specific MySQL. The stored procedure is named focus_sequence_func and is of type FUNCTION. The above stored procedure defines a receiving parameter of type VACHAR, length 255, name v_seq_name, return type VARCHAR, length 64, and the string in the specified format is a string consisting of a sequence starting value, a string link symbol, a numerical value to be added to the sequence starting value, a string link symbol, and a sequence step; the preset string includes -1 and -2; the preset algorithm is: new sequence starting value = current sequence starting value + number of sequences that the computing node should cache × sequence step.
5. The method for implementing a high-availability sequence in a distributed database according to claim 4, wherein: The specific steps in step 3 include: Step 3-1: Predefine four variables: v_curr_val of long type, v_step_val of int type, v_increment of int type, and v_updateRowCount of int type; these four variables are used to store the sequence starting value, sequence step length, the value to be increased from the sequence starting value, and the number of sequence records to be changed, respectively; Step 3-2: Convert the incoming v_seq_name to uppercase letters, convert the seq_name in the table to uppercase letters, and query the sequence records with the same two values; calculate the v_increment value using the formula: v_increment = cache_nums * step_val; determine whether the v_cur_val value is empty. If it is empty, return the string -2; if not, continue with step 3-3; Step 3-3: Calculate the new starting value of the sequence using the formula: new v_curr_val = v_curr_val + v_increment. Use the new v_curr_val value to update the sequence records queried in step 3-2. Determine the number of updated sequence records. If the number is 0, return the string -1. Otherwise, return the specified format string. The string format is: v_curr_val value, English comma, v_increment value, English comma, v_step_val value.
6. The method for implementing a high-availability sequence in a distributed database according to claim 5, wherein: In step 5, after the computing node is started, heartbeat detection is established according to the address, account number, password, and two sequence libraries. The computing node initializes a Map data structure, uses the schema name of the distributed database concatenated with the ## symbol and then the sequence name as the key, and uses a sequence value queue as the value. The user requests the sequence value of the specified name from the computing node, and the computing node executes the following steps: Step 5-1: The computing node uses the key to query the Map data structure. If the obtained value is not empty, it executes step 5-2. If the obtained value is empty, it constructs a new empty sequence value queue and saves it in the Map data structure, and continues to execute step 5-2. Step 5-2: Check the sequence value queue obtained in step 5-1. If it is not empty, extract a specific sequence value from it and return it to the requester. If the sequence value queue is empty, execute step 5-4. Check the length of the sequence value queue. If the previous length of the sequence value queue is greater than 100 and the current length of the sequence value queue is less than half of the previous length, execute step 5-4. If the previous length of the sequence value queue is less than 100 and the current length of the sequence value queue is less than 50, execute step 5-4. If the length of the sequence value queue does not meet the above two conditions, execute step 5-3. Step 5-3: The computing node completes the return sequence value operation and enters the waiting state for the next request. Step 5-4: The computing node requests the sequence library to generate a new batch of sequence values; checks whether the two sequence libraries are normal. If normal, randomly calls the stored procedure of one of them to obtain the return result string; if the string is -1 or -2, repeats steps 5-4 for the other sequence library; otherwise, parses the returned string and executes step 7.
7. The method for implementing a high-availability sequence in a distributed database according to claim 6, wherein: In step 7, after receiving the string in the specified format, the computing node parses and extracts the sequence starting value, sequence growth value and sequence step size, calculates the sequence value, and returns one of the sequence values to the user, and caches the remaining sequence values in the local memory for next use.
8. The method for implementing a high-availability sequence in a distributed database according to claim 7, wherein: In step 7, the calculation node calculates the sequence value, including: the first step, calculating the maximum value of the sequence, the calculation formula is: sequence maximum value = sequence starting value + sequence growth value; the second step, calculating the specific sequence values one by one, the calculation formula is: sequence value = sequence starting value + sequence step length; the third step, judging whether the second step sequence value is greater than the first step sequence maximum value, if so, the sequence generation step ends, if not, the second step is repeated.
9. The method for implementing a high-availability sequence in a distributed database according to claim 8, wherein: Step 7 also includes updating the local sequence cache with the calculated sequence value; extracting v_curr_val, v_increment and v_step_val in sequence from the return value of the sequence library according to the rules in step 3-3; increasing the value v_step_val successively based on the returned v_curr_val; after each addition, if the new v_curr_val is greater than or equal to the sum of the returned v_curr_val and v_increment, the operation is terminated; if the new v_curr_val is less than the sum of the returned v_curr_val and v_increment, the value is placed in the sequence value queue obtained in step 5-2.
Citation Information
Patent Citations
Data storage method and device for distributed database and computer equipment
CN112347076A
Implementation method and device of serial number generation method for ensuring high availability of service
CN114172792A