A database scaling method and device and a computer readable storage medium

By setting sharding fields and calculating and updating sharding results in SQL statements, the problem of business interruption during database scaling was solved, enabling seamless data migration and transparent scaling, and simplifying operation and maintenance steps.

CN115481103BActive Publication Date: 2026-11-24CHINA UNIONPAY
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202211033983.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-08-26
Publication Date
2026-11-24
Estimated Expiration
2042-08-26

AI Technical Summary

Technical Problem

Existing database scaling methods require business interruption, consuming significant time and effort, and impacting application usability and transparency.

Method used

By setting sharding fields in SQL statements, data is evenly divided into data slots based on these fields. When the database cluster is expanded or shrunk, the updated sharding results are calculated to determine the migration route of the data slots. Data migration is carried out from the original node to the updated node. The node routing is performed by using both original and updated sharding rules to ensure the transparency and seamlessness of the data migration process.

Benefits of technology

It enables in-situ scaling up and down without application downtime, simplifies operation and maintenance steps, ensures normal application transactions, and makes the data migration process transparent and seamless.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115481103B_ABST
    Figure CN115481103B_ABST
Patent Text Reader

Abstract

This invention provides a database scaling method, apparatus, and computer-readable storage medium. The method includes: setting a sharding field for a database table in an SQL statement; uniformly dividing data into a set number of data slots according to the sharding field; and calculating the original sharding result corresponding to each data slot; in the event of database cluster scaling, calculating the updated sharding result corresponding to each data slot based on the updated number of nodes; determining the migration route direction for the data slots based on the original and updated sharding results; and migrating data from the original nodes to the updated nodes according to the migration route direction. Using this method, data scaling can be implemented online without application downtime, without affecting application transactions, and simplifies the operation and maintenance steps.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of databases, and specifically relates to a database scaling method, apparatus, and computer-readable storage medium. Background Technology

[0002] This section is intended to provide background or context for embodiments of the invention as set forth in the claims. The description herein is not an admission that it is prior art simply because it is included in this section.

[0003] The problem of scaling up and down distributed databases has existed for a long time. In the past, the solution was to create a new database cluster, stop the original cluster's services, migrate the data, and then switch the services to the new cluster.

[0004] This approach has a significant impact on applications, requiring not only business interruptions but also substantial time and effort for data migration and business switching. Therefore, a scaling method that is user-friendly and transparent to applications is particularly urgent and important. Summary of the Invention

[0005] To address the problems existing in the prior art, a database scaling method, apparatus, and computer-readable storage medium are proposed, which can solve the aforementioned problems.

[0006] The present invention provides the following solutions.

[0007] Firstly, a database scaling method is proposed. The database includes one or more proxies and multiple data nodes connected to the proxies. The method includes: setting a sharding field in the database table in an SQL statement; evenly dividing the data into a set number of data slots according to the sharding field; and calculating the original sharding result corresponding to the data slots; in the case of scaling up or down the database cluster, calculating the updated sharding result corresponding to the data slots according to the updated number of nodes; determining the migration route direction of the data slots based on the original sharding result and the updated sharding result; and migrating the data from the original nodes to the updated nodes according to the migration route direction of the data slots.

[0008] In one implementation, node routing is performed using the original database sharding rules before data migration of the data slots; during data migration of the data slots, node routing is performed using a coexistence rule of the original database sharding rules and the updated database sharding rules; and after data migration of the data slots is completed, node routing is performed using the updated database sharding rules.

[0009] In one implementation, the data migration process also includes: using update sharding rules to route nodes for insert statements.

[0010] In one implementation, the data migration process further includes: calculating two types of database sharding results using the original database sharding rules and the updated database sharding rules for each of the update, select, or delete statements; executing the statements on both the original and updated nodes corresponding to the two types of database sharding results; and having the proxy merge the response results of the statements executed on the original and updated nodes.

[0011] In one implementation, data migration includes one or more migration tasks, each task being used to migrate one or more data slots. Each migration task includes: connecting to a broker and starting a session; setting the session's transaction isolation level to read-committed and configuring the session to auto-commit; starting a transaction; reading the data slot to be migrated from the original node; inserting the data slot into the update node according to the update sharding rules through the broker; if the newly inserted data slot in the update node has a duplicate primary key, deleting the data slot in the update node and re-inserting it; if the newly inserted data slot in the update node is normal, deleting the data slot to be migrated from the original node; and committing the transaction successfully or rolling it back if it fails.

[0012] In one implementation, the migration task includes: when reading the data slot to be migrated from the original node, locking the data slot to be migrated using read-write lock syntax.

[0013] In one implementation, the number of data slots is increased.

[0014] In one implementation, the method further includes: calculating the hash value of the sharding field on behalf of the agent; taking the modulo of the number of data slots based on the hash value of the sharding field to obtain the number of each data slot; and taking the modulo of the number of data nodes based on the number of data slots to obtain the routing data node corresponding to each data slot.

[0015] Secondly, a database scaling device is provided, comprising: an original sharding module, which sets the sharding field of the database table in an SQL statement, evenly divides the data into a set number of data slots according to the sharding field, and calculates the original sharding result corresponding to the data slots; an updated sharding module, which calculates the updated sharding result corresponding to the data slots according to the updated number of nodes when the database cluster is scaling up or down; and a data migration module, which determines the migration route direction of the data slots according to the original sharding result and the updated sharding result, and performs data migration from the original node to the updated node according to the migration route direction of the data slots.

[0016] Thirdly, a database scaling device is provided, comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, the instructions being executed by the at least one processor to enable the at least one processor to perform: the method of the first aspect.

[0017] Fourthly, a computer-readable storage medium is provided, which stores a program that, when executed by a multi-core processor, causes the multi-core processor to perform the method of the first aspect.

[0018] One of the advantages of the above implementation method is that it provides an implementation method that can be scaled up or down in place, and can be implemented online without application downtime, without affecting application transactions, and simplifies the operation and maintenance implementation steps.

[0019] Other advantages of the present invention will be explained in more detail below with reference to the accompanying drawings.

[0020] It should be understood that the above description is merely an overview of the technical solution of the present invention, so as to enable a clearer understanding of the technical means of the present invention, and thus enable its implementation in accordance with the contents of the specification. To make the above and other objects, features, and advantages of the present invention more apparent and understandable, specific embodiments of the present invention are illustrated below. Attached Figure Description

[0021] By reading the detailed description of the exemplary embodiments below, those skilled in the art will understand the advantages and benefits of this document, as well as other advantages and benefits. The accompanying drawings are for illustrative purposes only and are not intended to limit the invention. Furthermore, the same reference numerals denote the same parts throughout the drawings. In the drawings:

[0022] Figure 1 This is a schematic diagram of a database scaling device according to an embodiment of the present invention.

[0023] Figure 2 This is a schematic flowchart of a database scaling method according to an embodiment of the present invention.

[0024] Figure 3 This is a schematic diagram of the database structure according to an embodiment of the present invention;

[0025] Figure 4 This is a schematic diagram of a database expansion / shrinkage device according to another embodiment of the present invention.

[0026] In the accompanying drawings, the same or corresponding reference numerals indicate the same or corresponding parts. Detailed Implementation

[0027] Exemplary embodiments of the present disclosure will now be described in more detail with reference to the accompanying drawings. While exemplary embodiments of the present disclosure are shown in the drawings, it should be understood that the present disclosure may be implemented in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided so that this disclosure will be thorough and complete, and will fully convey the scope of the disclosure to those skilled in the art.

[0028] In the description of embodiments of this application, it should be understood that terms such as “comprising” or “having” are intended to indicate the presence of features, numbers, steps, behaviors, components, portions or combinations thereof disclosed in this specification, and are not intended to exclude the possibility of the presence of one or more other features, numbers, steps, behaviors, components, portions or combinations thereof.

[0029] Unless otherwise stated, " / " means "or". For example, A / B can mean A or B. In this article, "and / or" is merely a way of describing the relationship between related objects, indicating that there can be three relationships. For example, A and / or B can mean: A exists alone, A and B exist simultaneously, and B exists alone.

[0030] The terms "first," "second," etc., are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of technical features indicated. Therefore, a feature defined with "first," "second," etc., may explicitly or implicitly include one or more of that feature. In the description of embodiments of this application, unless otherwise stated, "a plurality of" means two or more.

[0031] All code in this application is exemplary, and those skilled in the art can conceive of various variations without departing from the spirit of this application, depending on factors such as the programming language used, specific needs, and personal habits.

[0032] First see Figure 1 The illustration schematically shows an environment 100 in which an exemplary implementation according to the present disclosure can be used.

[0033] Figure 1 A schematic diagram of an example of a computing device 100 according to an embodiment of the present disclosure is shown. It should be noted that... Figure 1 This can be seen as a schematic diagram of the hardware operating environment for the database scaling method. The database scaling device in this embodiment can be a PC, laptop, or other terminal device.

[0034] like Figure 1As shown, the database expansion / shrinkage device may include: a processor 1001, such as a CPU; a network interface 1004; a user interface 1003; a memory 1005; and a communication bus 1002. The communication bus 1002 is used to enable communication between these components. The user interface 1003 may include a display screen or an input unit such as a keyboard; optionally, the user interface 1003 may also include a standard wired interface or a wireless interface. The network interface 1004 may optionally include a standard wired interface or a wireless interface (such as a Wi-Fi interface). The memory 1005 may be high-speed RAM or non-volatile memory, such as a disk drive. Optionally, the memory 1005 may also be a storage device independent of the aforementioned processor 1001.

[0035] Those skilled in the art will understand that Figure 1 The database scaling device structure shown does not constitute a limitation on the database scaling device, and may include more or fewer components than shown, or combine certain components, or have different component arrangements.

[0036] like Figure 1 As shown, the memory 1005, which serves as a computer storage medium, may include an operating system, a network communication module, a user interface module, and a database scaling program. The operating system is a program that manages and controls the hardware and software resources of the database scaling device, supporting the operation of the database scaling program and other software or programs.

[0037] exist Figure 1 In the database scaling device shown, the user interface 1003 is mainly used to receive requests and data sent by the first terminal, the second terminal, and the monitoring terminal; the network interface 1004 is mainly used to connect the backend server to the backend server for data communication; and the processor 1001 can be used to call the database scaling program stored in the memory 1005 and perform the following operations:

[0038] In the SQL statement, set the sharding field for the database table, and evenly divide the data into a set number of data slots based on the sharding field, and calculate the original sharding result corresponding to the data slots; in the case of database cluster scaling up or down, calculate the updated sharding result corresponding to the data slots based on the updated number of nodes; determine the migration route direction for the data slots based on the original sharding result and the updated sharding result; and migrate the data from the original nodes to the updated nodes according to the migration route direction of the data slots.

[0039] Therefore, database scaling can be achieved without application downtime or disruption to application transactions, simplifying the operation and maintenance process.

[0040] Figure 2 A flowchart illustrating a method for performing database scaling according to an embodiment of the present disclosure is shown. This method can be, for example, by... Figure 1 The method is executed by the computing device 100 shown. It should be understood that the method 200 may also include additional boxes not shown and / or the boxes shown may be omitted, and the scope of this disclosure is not limited in this respect.

[0041] Step 210: Set the sharding field of the database table in the SQL statement, divide the data evenly into a set number of data slots according to the sharding field, and calculate the original sharding result corresponding to the data slots; the original sharding result is the result of sharding the data slots according to the original sharding rules.

[0042] Step 220: In the event of scaling up or down the database cluster, calculate the updated sharding result corresponding to the data slot based on the updated number of nodes; this updated sharding result is also the result of sharding the data slot according to the updated sharding rules.

[0043] Step 230: Determine the migration route direction of the data slot based on the original database sharding results and the updated database sharding results;

[0044] Step 240: Migrate data from the original node to the updated node according to the migration route of the data slot.

[0045] refer to Figure 3 The database includes one or more proxies and multiple data nodes connected to the proxies, as described below. Figure 3The embodiments of this application will be described in detail with specific examples. For example, 1) When it is necessary to expand the data nodes from 2 nodes to 4 nodes. 2) Under the original database sharding rules: 1024 slots are alternately routed to node 1 and node 2, such as: slot number 0 is routed to node 1, slot number 1 is routed to node 2, slot number 2 is routed to node 1, slot number 3 is routed to node 2, and so on. 3) Under the updated database sharding rules: 1024 slots are alternately routed to node 1, node 2, node 3 and node 4, such as: slot number 0 is routed to node 1, slot number 1 is routed to node 2, slot number 2 is routed to node 3, slot number 3 is routed to node 4, and so on. 4) Relocation route: It can be seen that the routing rules for slots numbered 0 and 1 remain unchanged, while slots numbered 2 and 3 need to be relocated. Slot 2 is relocated from node 1 to node 3, and slot 3 is relocated from node 2 to node 4. Therefore, in this scenario, approximately half of all 1024 slots need to be relocated.

[0046] Slot Old database sharding rules (2 shards) New database sharding rules (4 shards) Data migration direction 0 DN 1 DN 1 none 1 DN 2 DN 2 none 2 DN 1 DN 3 DN1->DN3 3 DN 2 DN 4 DN2->DN4 4 DN 1 DN 1 none …… …… …… …… 1023 DN 2 DN 4 DN2->DN4

[0047] In one implementation, in step 210, the agent can calculate the hash value of the sharding field, and take the modulo of the number of data slots based on the hash value of the sharding field to obtain the number of each data slot; in step 220, in the case of scaling up or down the database cluster, the updated sharding result corresponding to the data slot is calculated based on the updated number of nodes.

[0048] In one implementation, in step 210 or step 220, the number of data nodes can be moduloed according to the slot number to obtain the data node to which each data slot should be routed.

[0049] In one implementation, to ensure the database operates correctly before, during, and after data migration, node routing can be performed using the original sharding rules before data migration to the data slot; during data migration to the data slot, node routing can be performed using a coexistence rule of the original sharding rules and the updated sharding rules; and after data migration to the data slot is completed, node routing can be performed using the updated sharding rules.

[0050] In one implementation, the old and new rules coexist during the migration process until the migration is complete. To ensure the correctness of SQL statement execution results when both database sharding rules coexist, the statement routing can be adapted and modified. Specifically, this can include: during the data migration process, using update database sharding rules for node routing for INSERT statements. That is, all newly added data is written according to the update database sharding rules.

[0051] In one implementation, the process may further include, during the data migration, calculating two types of sharding results using the original sharding rules and the updated sharding rules for update, select, or delete statements respectively, executing the statements on both the original and updated nodes corresponding to the two sharding results, and having the proxy merge the response results of the statements executed by the original and updated nodes.

[0052] For example, if an update statement (update xx set a=1 where shard_key=xxx) is calculated to need to be executed on node 1 according to the original sharding rules, but according to the update sharding rules it needs to be executed on node 3, the agent will find the union of the two and formulate an execution plan that executes on both nodes 1 and 3. Assuming the data to be updated has not yet been migrated and is still stored on node 1, the update on node 1 will execute successfully, hitting one row of data. At the same time, the update on node 3 will execute successfully, but will not hit any data. The agent will merge the responses from both and return them to the application, hitting one row of data.

[0053] Therefore, it is possible to ensure that SQL statements execute correctly when the old and new database sharding rules coexist during the migration process.

[0054] In one implementation, data migration includes one or more migration tasks, each migration task being used to migrate one or more data slots. The migration tasks include:

[0055] (1) Connect to the proxy and start a session;

[0056] (2) Set the transaction isolation level of the session to read-committed;

[0057] (3) Set the session to autocommit (set autocommit=1);

[0058] (4) Start a transaction;

[0059] (5) Read the data slot to be migrated from the original node; for example, the following statement can be used:

[0060] / *+DRDB:datanode=xxx* / select *from xx where `slot_id`in(xx)LOCK INSHARE MODE; / / Comment syntax can specify the data node name

[0061] (6) Insert the data slot into the update node according to the update sharding rules through the agent; for example, the following statement can be used: INSERT INTO xx(col1,col2..)values(xx,xx);

[0062] (7) Optionally, if the newly added data slot of the update node has a duplicate primary key, then delete the data slot of the update node and perform a re-insertion; for example, the following statement can be used:

[0063] / *+DRDB:datanode=xxx* / DELETE FROM xx WHERE…

[0064] INSERT INTO xx(col1,col2..)values(xx,xx);

[0065] The above steps are to resolve situations where new data, after being inserted into the update node according to the database sharding rules, actually conflicts with the old data that has not been migrated. This step compensates for and corrects the data, deleting the new data with the conflicting primary key and retaining the old data.

[0066] (8) Optionally, if the newly added data slot of the inserted update node is normal, then delete the data slot to be migrated from the original node: for example, the following statement can be used:

[0067] / *+DRDB:datanode=xxx* / DELETE FROM xx WHERE…

[0068] (9) The transaction is successfully committed or fails and is rolled back.

[0069] In one implementation, to ensure that the execution results of the application's SQL statements are not affected by the migration, a read-write lock syntax can be used to lock the data slot to be migrated when reading it from the original node. Specifically, in the migration task described above, when reading data from the data slot on the original node, a read-write lock syntax (lock in share mode) is used to lock this part of the data to be migrated. This way, other sessions will stop responding when performing changes or deletions (update, delete) on this data, thus ensuring data consistency.

[0070] In one implementation, to minimize the impact of locking, the number of data slots can be increased, so that less data is locked each time.

[0071] By adopting the embodiments of this application, the convenience of scaling up and down can be significantly improved. Users only need to select the number of nodes to add or remove on the cloud management platform (such as DBaaS or public cloud platform), and the platform can start the scaling up and down steps without the need for intervention from operations and maintenance or application developers. Furthermore, during the scaling up and down process, normal application transactions do not need to be interrupted, and the correctness of SQL statement execution remains unaffected. In addition, the scaling up and down method is an in-situ upgrade, requiring no additional cluster construction. This solution also supports scheduled, segmented scaling up and down; for systems with large amounts of data, data migration can be performed multiple times with specified execution times to avoid peak transaction periods.

[0072] It should be noted that steps not described in detail in this embodiment can be referred to... Figure 1 The descriptions of the relevant steps in the illustrated implementation are not repeated here.

[0073] In the description of this specification, the references to terms such as "some possible implementations," "some implementations," "example," "specific example," or "some examples," etc., indicate that a specific feature, structure, material, or characteristic described in connection with that implementation or example is included in at least one implementation or example of the present invention. In this specification, the illustrative expressions of the above terms do not necessarily refer to the same implementation or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in one or more implementations or examples. Moreover, without contradiction, those skilled in the art can combine and integrate the different implementations or examples described in this specification, as well as the features of different implementations or examples.

[0074] The method flowcharts for embodiments of this application describe certain operations as different steps performed in a certain order. Such flowcharts are illustrative and not restrictive. Some steps described herein may be grouped together and performed in a single operation, some steps may be divided into multiple sub-steps, and some steps may be performed in an order different from that shown herein. The various steps shown in the flowcharts may be implemented in any way by any circuit structure and / or tangible mechanism (e.g., software running on a computer device, hardware (e.g., logic functions implemented by a processor or chip), and / or any combination thereof).

[0075] Based on the same technical concept, embodiments of the present invention also provide a database scaling device for executing the database scaling method provided in any of the above embodiments. Figure 4This is a schematic diagram of a database scaling device provided for an embodiment of the present invention.

[0076] like Figure 4 As shown, device 4 includes:

[0077] The original database sharding module 410 sets the sharding field of the database table in the SQL statement, divides the data evenly into a set number of data slots according to the sharding field, and calculates the original database sharding result corresponding to the data slot.

[0078] The database sharding module 420 is used to calculate the updated database sharding results corresponding to the data slots based on the updated number of nodes when the database cluster is expanded or shrunk.

[0079] The data migration module 430 is used to determine the migration route direction of the data slot based on the original database sharding results and the updated database sharding results; and to perform data migration from the original node to the updated node based on the migration route direction of the data slot.

[0080] It should be noted that the apparatus in the embodiments of this application can implement each process of the aforementioned method and achieve the same effect and function, which will not be elaborated here.

[0081] According to some embodiments of this application, a non-volatile computer storage medium is provided for a database scaling method, which stores computer-executable instructions configured to be executed when run by a processor: the method of the above embodiments.

[0082] The various embodiments in this application are described in a progressive manner. Similar or identical parts between embodiments can be referred to interchangeably. Each embodiment focuses on its differences from other embodiments. In particular, the descriptions of the apparatus, device, and computer-readable storage medium embodiments are simplified because they are substantially similar to the method embodiments; relevant details can be found in the descriptions of the method embodiments.

[0083] The apparatus, device, and computer-readable storage medium provided in this application correspond one-to-one with the method. Therefore, the apparatus, device, and computer-readable storage medium also have similar beneficial technical effects as their corresponding methods. Since the beneficial technical effects of the method have been described in detail above, the beneficial technical effects of the apparatus, device, and computer-readable storage medium will not be repeated here.

[0084] Those skilled in the art will understand that embodiments of the present invention can be provided as methods, apparatus (devices or systems), or computer-readable storage media. Therefore, the present invention can take the form of a completely hardware implementation, a completely software implementation, or an implementation combining software and hardware aspects. Furthermore, the present invention can take the form of a computer-readable storage medium embodied on one or more computer-readable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-readable program code.

[0085] This invention is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (devices or systems), and computer-readable storage media according to embodiments of the invention. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart illustrations and / or block diagrams. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.

[0086] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.

[0087] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.

[0088] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.

[0089] Memory may include non-persistent storage in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM. Memory is an example of computer-readable media.

[0090] Computer-readable media, including both permanent and non-permanent, removable and non-removable media, can store information using any method or technology. Information can be computer-readable instructions, data structures, program modules, or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, magnetic magnetic disk storage or other magnetic storage devices, or any other non-transfer medium that can be used to store information accessible by a computing device. Furthermore, although the operations of the method of the invention are described in a specific order in the accompanying drawings, this does not require or imply that these operations must be performed in that specific order, or that all of the operations shown must be performed to achieve the desired result. Additionally or alternatively, certain steps may be omitted, multiple steps may be combined into one step, and / or one step may be broken down into multiple steps.

[0091] While the spirit and principles of the invention have been described with reference to several specific embodiments, it should be understood that the invention is not limited to the disclosed specific embodiments, and the division of aspects does not imply that features in these aspects cannot be combined for benefit; such division is merely for ease of description. The invention is intended to cover various modifications and equivalent arrangements included within the spirit and scope of the appended claims.

Claims

1. A database scaling method, characterized in that, The database includes one or more proxies and multiple data nodes connected to the proxies, and the method includes: In the SQL statement, set the sharding field of the database table, divide the data evenly into a set number of data slots according to the sharding field, and calculate the original sharding result corresponding to the data slots; In the event of scaling up or down the database cluster, the updated database sharding result corresponding to the data slot is calculated based on the updated number of nodes. Based on the original database sharding results and the updated database sharding results, determine the migration route direction for the data slot; Data is migrated from the original node to the updated node according to the migration route direction of the data slot; in, Before data migration is performed in the data slots, node routing is performed using the original database sharding rules; During the data migration process of the data slot, a coexistence rule of the original database sharding rules and the updated database sharding rules is used for node routing; After the data migration in the data slot is completed, the node routing is performed using the updated database sharding rules; The data migration process also includes: for Insert statements, using the updated database sharding rules to perform node routing; The data migration process also includes: calculating two types of database sharding results using the original database sharding rules and the updated database sharding rules for update, select, or delete statements, executing statements on both the original and updated nodes corresponding to the two types of database sharding results, and having the proxy merge the response results of the statements executed by the original and updated nodes.

2. The method according to claim 1, characterized in that, The data migration includes one or more migration tasks, each migration task being used to migrate one or more data slots. The migration tasks include: Connect to the broker and start a session, set the transaction isolation level of the session to read-committed, and set the session to auto-commit; Start a transaction, read the data slot to be migrated from the original node, and insert the data slot into the update node according to the update sharding rules through the proxy; If the newly added data slot inserted into the update node has a duplicate primary key, then delete the data slot of the update node and re-insert it; If the insertion of the new data slot into the updated node is successful, then delete the data slot to be migrated from the original node: The transaction either commits successfully or rolls back if it fails.

3. The method according to claim 2, characterized in that, The relocation task includes: When reading the data slot to be migrated from the original node, the data slot to be migrated is locked using read-write lock syntax.

4. The method according to claim 1, characterized in that, include: Increase the number of data slots.

5. The method according to claim 1, characterized in that, Also includes: The agent calculates the hash value of the sharding field, and takes the modulo of the number of data slots based on the hash value of the sharding field to obtain the number of each data slot; as well as, The number of data nodes is moduloed based on the data slot number to obtain the routing data node corresponding to each data slot.

6. A database scaling device, characterized in that, The device includes: The original database sharding module sets the sharding field of the database table in the SQL statement, divides the data evenly into a set number of data slots according to the sharding field, and calculates the original database sharding result corresponding to the data slot. The database sharding update module is used to calculate the updated database sharding result corresponding to the data slot based on the updated number of nodes when the database cluster is expanded or shrunk. The data migration module is used to determine the migration route direction of the data slot based on the original database sharding result and the updated database sharding result; to migrate data from the original node to the updated node according to the migration route direction of the data slot; and to perform node routing using the original database sharding rules before data migration of the data slot; to perform node routing using a coexistence rule of the original database sharding rules and the updated database sharding rules during data migration of the data slot; and to perform node routing using the updated database sharding rules after data migration of the data slot is completed. During the data migration process, the module further includes: performing node routing using the updated database sharding rules for Insert statements; and further includes: calculating two types of database sharding results using the original database sharding rules and the updated database sharding rules for Update statements, Select statements, or Delete statements, executing the statements on both the original node and the updated node corresponding to the two types of database sharding results, and having a proxy merge the response results of the statements executed by the original node and the updated node.

7. A database scaling device, characterized in that, include: At least one processor; And a memory communicatively connected to at least one processor; wherein the memory stores instructions executable by at least one processor, the instructions being executed by at least one processor to enable at least one processor to perform: the method as described in any one of claims 1-5.

8. A computer-readable storage medium storing a program that, when executed by a multi-core processor, causes the multi-core processor to perform the method as described in any one of claims 1-5.

Citation Information

Patent Citations

  • Method, device and system for expanding and shrinking capacity of database cluster

    CN110874384A