A load balancing method, device, equipment and medium
By implementing the load balancing method in the JDBC driver of the database, and dynamically adjusting the database cluster node pool, the problem of high load balance resource consumption in the database management system is solved, and the dynamic contraction and expansion of the database node and the load balance are realized.
Patent Information
- Application Number
- CN202410156421.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-02-04
- Publication Date
- 2025-01-28
- Estimated Expiration
- 2044-02-04
AI Technical Summary
The existing load balancing technology consumes a high resource in the database management system and is difficult to effectively reduce it.
By implementing the load balancing method in the JDBC driver of the database, determine the unusable nodes of the database cluster and dynamically adjust the node pool, and determine the availability of the node according to the number of connections and connection failures of the node, and realize the dynamic contraction and expansion of the database node.
The dynamic contraction and expansion of the database node is realized, and the load balancing of the database access is also realized, reducing the resource consumption during the load balancing process.
Smart Images

Figure CN118069354B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of information technology and provides a load balancing method, device, equipment and medium. Background Art
[0002] As we all know, the current mainstream load balancing methods are mainly divided into two types: hardware load balancing and software load balancing. Among them, the hardware load balancing method often uses a hardware load balancer connected to the database to achieve access load balancing. Since the main working principle of the hardware load balancer is to select a server with a lighter load according to a certain scheduling algorithm after accepting the client access request, and establish a connection with it to send the request to the database. Therefore, although the hardware load balancer has the advantages of high performance, high reliability and low latency, it is not suitable for enterprises of all sizes due to its high cost and complex configuration.
[0003] Software load balancing often uses proxy servers, virtual IPs, basic operating systems, and load balancing middleware to achieve access load balancing. Therefore, soft load balancing has the advantages of simple configuration, flexible use, low cost, and good stability. However, since its load capacity is limited by the performance of the server where the software is located, compared with hardware load balancing, software load balancing is more complex, has fewer functions, and has lower performance.
[0004] However, both hardware load balancing and software load balancing are external systems to the database management system and require separate resource consumption for processing. Therefore, how to reduce resource consumption in load balancing has become an urgent problem to be solved. Summary of the invention
[0005] The present application provides a load balancing method, apparatus, device and medium for solving the problem of high resource consumption in load balancing.
[0006] On the one hand, a load balancing method is provided, which is applied to a JDBC driver of a database, and the method comprises:
[0007] Determine whether there is an unavailable node in the unavailable node connection table of the database cluster;
[0008] If it is determined that there is an unavailable node in the unavailable node connection table of the database cluster, determine whether to start the live detection job;
[0009] If it is determined that the live detection job is started, determining whether each node is an unavailable node according to the number of connection establishments and connection failures of each node in the node pool of the database cluster;
[0010] For any node in the node pool, if it is determined that any node is an unavailable node, the any node is moved from the node pool to the unavailable node connection table.
[0011] Optionally, before determining whether there is an unavailable node in the unavailable node connection table of the database cluster, the method further includes:
[0012] In the URL connection string of the database cluster, an IPS attribute is added; wherein the IPS attribute is used to configure the IP addresses of all nodes of the database cluster;
[0013] In the database cluster, the node pool is newly added; wherein the node pool is a global connection control list ipVectors, each node in the node pool is a Vector object, and each node includes five elements: IP address, listening port, number of connection establishment times, number of connection failures, and whether it is a newly accessed IP node.
[0014] Optionally, after adding the node pool, the method further includes:
[0015] When parsing the URL connection string for the first time, the IPS attribute value is decomposed into multiple nodes using commas as separators;
[0016] Put each node into the node pool in the form of a first preset element, and start a new node monitoring operation; wherein the first preset element form includes the element values of the five elements of the corresponding node;
[0017] According to the newly added node monitoring operation, obtaining a database cluster node list is performed once at a preset interval, and the database cluster node list is compared with the nodes in the node pool to determine whether there is a newly added node in the database cluster node list;
[0018] If it is determined that there is a new node in the database cluster node list, the new node is placed in the node pool in a second preset element form; wherein the second preset element form includes element values of each of the five elements of the corresponding node.
[0019] Optionally, if it is determined that there is an unavailable node in the unavailable node list of the database cluster, after determining whether to start the liveness detection job, the method further includes:
[0020] If it is determined that the live detection operation is started, the target node selection method is determined according to the preset global variables;
[0021] According to the target node selection method, establishing connections with all nodes in the unavailable node connection table;
[0022] For any node in the unavailable node connection table, determine whether the connection with the any node is successful;
[0023] If it is determined that the connection with any of the nodes is successful, any of the nodes is moved from the unavailable node connection list to the node pool in the form of a second preset element; wherein the second preset element form includes the element values of the five elements of the corresponding node.
[0024] Optionally, the step of determining whether each node is an unavailable node according to the number of connection establishments and the number of connection failures of each node in the node pool of the database cluster includes:
[0025] If the target node selection mode is a round-robin allocation mode, the connection node is obtained from the node pool in a round-robin manner;
[0026] If the target node selection mode is a new node joining mode, obtaining a connection node according to the connection load of each node in the node pool;
[0027] Determine whether the fifth element of the connection node is false;
[0028] If it is determined that the fifth element of the connection node is false, the fifth element of the newly added node in the node pool is set to false, and the target node selection mode is set to a round-robin allocation mode.
[0029] Optionally, the step of determining whether each node is an unavailable node according to the number of connection establishments and the number of connection failures of each node in the node pool of the database cluster includes:
[0030] For the connection node, if it is determined that the connection node is an available node, try to establish a connection with the connection node;
[0031] Determine whether the number of connection failures of the connection node is greater than a preset number;
[0032] If it is determined that the number of connection failures of the connection node is greater than a preset number, the connection node is determined as an unavailable node.
[0033] Optionally, if it is determined that the number of connection failures of the connection node is greater than a preset number, the step of determining the connection node as an unavailable node includes:
[0034] Determine whether the number of connection failures of the connection node is 0;
[0035] If it is determined that the number of connection failures of the connection node is 0, the value of the number of connection failures is updated to 1, and the step of selecting a target node from the node pool to establish a connection is continued;
[0036] If it is determined that the number of connection failures of the connection node is not 0, the connection node is determined as an unavailable node.
[0037] On the one hand, a load balancing device is provided, which is applied to a JDBC driver of a database, and the device includes:
[0038] A first determining unit, used to determine whether there is an unavailable node in the unavailable node connection table of the database cluster;
[0039] A second determining unit, configured to determine whether to start a liveness search operation if it is determined that there is an unavailable node in the unavailable node connection table of the database cluster;
[0040] A third determining unit is used to determine whether each node is an unavailable node according to the number of connection establishments and connection failures of each node in the node pool of the database cluster if it is determined that the live detection job is started;
[0041] The node moving unit is used to move any node in the node pool from the node pool to the unavailable node connection table if it is determined that the any node is an unavailable node.
[0042] On the one hand, an electronic device is provided, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein any one of the above methods is implemented when the processor executes the computer program.
[0043] Compared with the prior art, the beneficial effects of this application are:
[0044] In an embodiment of the present application, when load balancing is performed on the database, the JDBC driver of the database can be used to perform load balancing. Specifically, first, it can be determined whether there are unavailable nodes in the unavailable node list of the database cluster; then, if it is determined that there are unavailable nodes in the unavailable node list of the database cluster, it can be determined whether the live-detection job is started; next, if it is determined that the live-detection job is started, it can be determined whether each node is an unavailable node based on the number of connection establishments and connection failures of each node in the node pool of the database cluster; finally, for any node in the node pool, if any node is determined to be an unavailable node, any node can be moved from the node pool to the unavailable node list. Therefore, in the embodiment of the present application, since the unavailable nodes in the node pool are moved to the unavailable node connection table, dynamic shrinking and expansion of the database nodes can be achieved, and since the unavailable nodes in the node pool are determined by the "number of connection establishment and connection failure times" of the nodes, while achieving dynamic shrinking and expansion of the database nodes, load balancing of database access can also be achieved, and since no additional hardware or software is used in the load balancing process, resource consumption can be further reduced in load balancing compared to the prior art. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the related technologies, the drawings required for use in the embodiments or the related technical descriptions are briefly introduced below. Obviously, the drawings described below are only the embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying any creative work.
[0046] Figure 1 A schematic diagram of an application scenario provided for an embodiment of the present application;
[0047] Figure 2 A schematic diagram of a flow chart of a load balancing method provided in an embodiment of the present application;
[0048] Figure 3 A schematic diagram of a load balancing device provided in an embodiment of the present application.
[0049] Markings in the figure: 10 - load balancing device, 101 - processor, 102 - memory, 103 - I / O interface, 104 - database, 30 - load balancing device, 301 - first determination unit, 302 - second determination unit, 303 - third determination unit, 304 - node moving unit, 305 - node loading unit. DETAILED DESCRIPTION
[0050] In order to make the purpose, technical scheme and advantages of the present application clearer, the technical scheme in the embodiment of the present application will be clearly and completely described below in conjunction with the drawings in the embodiment of the present application. Obviously, the described embodiment is only a part of the embodiment of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in the field without making creative work are within the scope of protection of the present application. In the absence of conflict, the embodiments in the present application and the features in the embodiments can be combined with each other arbitrarily. In addition, although the logical order is shown in the flow chart, in some cases, the steps shown or described can be performed in an order different from that here.
[0051] As we all know, the current mainstream load balancing methods are mainly divided into two types: hardware load balancing and software load balancing. Among them, the hardware load balancing method often uses a hardware load balancer connected to the database to achieve access load balancing. Since the main working principle of the hardware load balancer is to select a server with a lighter load according to a certain scheduling algorithm after accepting the client access request, and establish a connection with it to send the request to the database. Therefore, although the hardware load balancer has the advantages of high performance, high reliability and low latency, it is not suitable for enterprises of all sizes due to its high cost and complex configuration.
[0052] Software load balancing often uses proxy servers, virtual IPs, basic operating systems, and load balancing middleware to achieve access load balancing. Therefore, soft load balancing has the advantages of simple configuration, flexible use, low cost, and good stability. However, since its load capacity is limited by the performance of the server where the software is located, compared with hardware load balancing, software load balancing is more complex, has fewer functions, and has lower performance.
[0053] However, whether it is hardware load balancing or software load balancing, for the database management system, it is an external system and requires separate consumption of resources for processing.
[0054] Based on this, an embodiment of the present application provides a load balancing method, which is applied to the JDBC driver of the database. In this method, first, it can be determined whether there is an unavailable node in the unavailable node connection table of the database cluster; then, if it is determined that there is an unavailable node in the unavailable node connection table of the database cluster, it can be determined whether the live detection job is started; next, if it is determined that the live detection job is started, it can be determined whether each node is an unavailable node based on the number of connection establishments and connection failures of each node in the node pool of the database cluster; finally, for any node in the node pool, if any node is determined to be an unavailable node, any node can be moved from the node pool to the unavailable node connection table. Therefore, in the embodiment of the present application, since the unavailable nodes in the node pool are moved to the unavailable node connection table, dynamic shrinking and expansion of the database nodes can be achieved, and since the unavailable nodes in the node pool are determined by the "number of connection establishment and connection failure times" of the nodes, while achieving dynamic shrinking and expansion of the database nodes, load balancing of database access can also be achieved, and since no additional hardware or software is used in the load balancing process, resource consumption can be further reduced in load balancing compared to the prior art.
[0055] After introducing the design ideas of the embodiments of the present application, the following briefly introduces the application scenarios to which the technical solutions of the embodiments of the present application can be applied. It should be noted that the application scenarios introduced below are only used to illustrate the embodiments of the present application and are not limited. In the specific implementation process, the technical solutions provided by the embodiments of the present application can be flexibly applied according to actual needs.
[0056] like Figure 1 FIG. 1 is a schematic diagram of an application scenario provided by an embodiment of the present application. The application scenario may include a load balancing device 10 .
[0057] The load balancing device 10 can be used to load balance database access, for example, a personal computer (PC), a server and a laptop. The load balancing device 10 may include one or more processors 101, a memory 102, an I / O interface 103 and a database 104. Specifically, the processor 101 may be a central processing unit (CPU) or a digital processing unit. The memory 102 may be a volatile memory, such as a random-access memory (RAM); the memory 102 may also be a non-volatile memory, such as a read-only memory, a flash memory, a hard disk drive (HDD) or a solid-state drive (SSD); or the memory 102 may be any other medium that can be used to carry or store the desired program code in the form of instructions or data structures and can be accessed by a computer, but is not limited thereto. The memory 102 may be a combination of the above memories. The memory 102 may store some program instructions of the load balancing method provided in the embodiment of the present application, and these program instructions can be used to implement the steps of the load balancing method provided in the embodiment of the present application when executed by the processor 101, so as to solve the problem of high resource consumption in load balancing. The database 104 may be used to store data such as the unavailable node connection table, node pool, IP address, listening port, number of connection establishments, number of connection failures, etc. involved in the solution provided in the embodiment of the present application.
[0058] In the embodiment of the present application, the load balancing device 10 can obtain the unavailable node connection table through the I / O interface 103, and then the processor 101 of the load balancing device 10 will reduce resource consumption in load balancing according to the program instructions of the load balancing method provided in the embodiment of the present application in the memory 102. In addition, data such as the unavailable node connection table, node pool, IP address, listening port, number of connection establishments, number of connection failures, etc. can also be stored in the database 104.
[0059] Of course, the method provided in the embodiment of the present application is not limited to Figure 1 The application scenarios shown can also be used in other possible application scenarios, and the embodiments of the present application are not limited thereto. Figure 1 The functions that can be realized by each device in the application scenario shown will be described in the subsequent method embodiments, and will not be described in detail here. Below, the method of the embodiment of the present application will be introduced in conjunction with the accompanying drawings.
[0060] like Figure 2 FIG. 1 is a flow chart of a load balancing method provided in an embodiment of the present application. The method can be performed by Figure 1 The method is executed by the load balancing device 10 in the embodiment. Specifically, the process of the method is described as follows.
[0061] Step 201: Determine whether there is an unavailable node in the unavailable node connection table of the database cluster.
[0062] In order to enable unified access to different databases, in the embodiment of the present application, the JDBC driver of the database can be used to implement load balancing of database access. In addition, in order to reduce resource consumption in load balancing, in the embodiment of the present application, load balancing of database access is further implemented based on the "elastic scaling of database nodes" process.
[0063] Furthermore, when a new connection is created in the database cluster, first, a global variable isNewConnection can be preset to indicate whether the connection establishment follows the "new node joining mode" or the "round-robin allocation mode". Then, based on the preset global variable, it can be determined whether there is an unavailable node in the unavailable node connection table unConnectList of the database cluster, that is, whether there is a node in the unavailable node connection table.
[0064] Step 202: If it is determined that there is an unavailable node in the unavailable node connection table of the database cluster, determine whether to start the liveness detection operation.
[0065] In an embodiment of the present application, if it is determined that there are unavailable nodes in the unavailable node connection table of the database cluster, it is determined whether the detection job is started to detect the availability of each node in the unavailable node connection table and the node pool of the database cluster. Among them, the detection job can be a scheduled job (ScheduledThreadPoolExecutor) validTaskExecutor, and the validTaskExecutor job can execute the detection thread (detactedIpNode) once every 1 minute to regularly detect the availability of each node in the unavailable node connection table and the node pool.
[0066] Step 203: If it is determined that the liveness detection operation is started, then whether each node is an unavailable node is determined based on the number of connection establishments and connection failures of each node in the node pool of the database cluster.
[0067] If it is determined that the live detection operation is started, in order to more accurately and efficiently determine the availability of each node in the node pool, in the embodiment of the present application, the connection node can be determined based on the number of connection establishments (i.e., the number of successful connections) of each node in the node pool of the database cluster, and then, based on the number of connection failures of the connection node, it is determined whether the connection node is an unavailable node. The greater the number of connection establishments and the number of connection failures, the more it indicates that the corresponding node access load is too large.
[0068] Step 204: For any node in the node pool, if any node is determined to be an unavailable node, any node is moved from the node pool to the unavailable node connection table.
[0069] In an embodiment of the present application, since there are multiple nodes in the node pool, for any node in the node pool, if it is determined to be an unavailable node based on the live operation, then the node can be moved from the node pool to the unavailable node connection table to achieve dynamic shrinkage of the database node. Moreover, since the unavailable nodes in the node pool are determined by the "number of connections established and the number of connection failures" of the nodes, while achieving dynamic shrinkage of the database nodes, load balancing of database access can also be achieved. In addition, since no additional hardware or software is used in the load balancing process, compared with the prior art, resource consumption can be further reduced in load balancing.
[0070] In a possible implementation, in order to efficiently locate, obtain and modify the corresponding information of the node, in an embodiment of the present application, before determining whether there is an unavailable node in the unavailable node list of the database cluster, an IPS attribute may be added to the URL connection string of the database cluster, and a node pool may be added to the database cluster. Specifically, first, an IPS attribute may be added to the URL connection string of the database cluster; wherein the IPS attribute may be used to configure the IP addresses of all nodes (working nodes) of the database cluster, separated by commas; then, a node pool may be added to the database cluster; wherein the node pool may be a global connection control list ipVectors, and each node in the node pool is a Vector object, and the Vector object is presented in the form of a dynamic array ArryaList (for example, Vector <arraylist>), and each node includes five elements: "IP address, listening port, number of connection establishments, number of connection failures, and whether it is a newly connected IP node". Among them, the fifth element "whether it is a newly connected IP node" indicates whether the current node is a newly added node in the database cluster or an existing node in the database cluster.
[0071] Furthermore, since the node pool design uses Vector objects, it can effectively operate asynchronously in a multi-threaded environment to ensure the consistency and correctness of the database node element status. And since the Vector object is presented in the form of a dynamic array ArryaList, it can not only represent the connection information of a single node, but also facilitate and efficiently locate, obtain, and modify the corresponding information of the node.
[0072] In a possible implementation, in order to realize the loading of database nodes, in the embodiment of the present application, after adding a node pool, first, when parsing the URL connection string for the first time, the IPS attribute value can be decomposed into multiple nodes with commas as separators; then, each node can be put into the node pool in the form of a first preset element, wherein the first preset element form includes the element values of the five elements of the corresponding node; that is, it is put into the node pool in the form of a dynamic array ArryaList (for example, "IP, PORT, 0, 0, false"), and the newly added node listening job is started; next, according to the newly added node listening job (ScheduledThreadPoolE xecutor)showCluster is used to obtain the database cluster node list once at a preset interval (for example, once every minute), and compare the database cluster node list with the nodes in the node pool to determine whether there is a new node in the database cluster node list; finally, if it is determined that there is a new node in the database cluster node list, the new node can be put into the node pool in the form of a second preset element, wherein the second preset element form includes the element values of the five elements of the corresponding node (the new node), that is, it can also be put into the node pool in the form of a dynamic array ArryaList (for example, "IP, PORT, 0, 0, true").
[0073] In one possible implementation, in order to quickly load balance access to database cluster nodes, in an embodiment of the present application, if it is determined that there are unavailable nodes in the unavailable node list of the database cluster, then after determining whether the live detection job is started, it is also possible to determine whether the nodes in the unavailable node list are available, so as to further realize dynamic expansion of database nodes.
[0074] Specifically, first, if it is determined that the live detection job is started, the target node selection method can be determined according to the preset global variables; then, according to the target node selection method, connections can be established for all nodes in the unavailable node connection table (for example, establishing a socekt connection); then, for any node in the unavailable node connection table, it can be determined whether the connection with any node is successful; finally, if it is determined that the connection with any node is successful, any node can be moved from the unavailable node connection table to the node pool in the form of a second preset element (for example, "IP, PORT, 0, 0, true"), that is, any node is placed in the node pool (that is, the number of connection establishment times for any node is 0, the number of connection failures is 0, and it is a newly connected IP node), and is deleted from the unavailable node connection table.
[0075] Furthermore, since connections are established for all nodes in the unavailable node connection table by identifying the connection establishment mode (global variable isNewConnection), access load balancing for database cluster nodes can be quickly performed by accurately selecting an appropriate connection establishment method.
[0076] In a possible implementation, in order to be able to further quickly load balance access to database cluster nodes, in an embodiment of the present application, when determining whether each node is an unavailable node based on the number of connection establishments and connection failures of each node in the node pool of the database cluster, that is, when selecting a connection node in the node pool, you can first determine what mode isNewConnection is in, and then determine the target node selection method accordingly. Specifically, if the target node selection method is a round-robin allocation mode, the connection node is obtained from the node pool in a cyclical manner; that is, when the global variable isNewConnection is a round-robin allocation mode, the target node selection method can be determined as a round-robin allocation mode, and then, the connection node can be obtained from the node pool in a cyclical manner, that is, the next node of the node that established the last connection is obtained from the node pool as the connection node.
[0077] If the target node selection mode is the new node joining mode, the connection node can be obtained according to the connection load of each node in the node pool; that is, when the global variable isNewConnection is the new node joining mode, the target node selection mode can be determined as the new node joining mode, and then the connection load of each node in the node pool can be calculated, and the node with the least "connection establishment times" can be selected as the connection node. Among them, the bubble sort algorithm can be used to implement sequential sorting, and then the first node in the sorting can be the node with the smallest access load (connection node).
[0078] Then, it can be determined whether the fifth element of the connected node is false, that is, whether the connected node is a newly connected IP node; if the fifth element of the connected node is determined to be false, that is, it means that the connected node is an existing node in the database cluster, then the fifth element of the newly added node in the node pool can be set to false, and the newly added node can be marked as an existing node, and the target node selection method can be set to the round-robin allocation mode.
[0079] In one possible implementation, in order to further reduce resource consumption in load balancing, in an embodiment of the present application, when determining whether each node is an unavailable node based on the number of connection establishments and connection failures of each node in the node pool of the database cluster, for the connection node, if the connection node is determined to be an available node, an attempt can be made to establish a connection with the connection node, wherein a JDBC driver can be used to select node information to establish a socket connection with the database cluster node; further, it can be determined whether the number of connection failures of the connection node is greater than a preset number (for example, 2 times); if it is determined that the number of connection failures of the connection node is greater than the preset number, the connection node can be determined as an unavailable node.
[0080] Of course, if it is determined that the connection node successfully establishes the connection, the number of successful connections of the connection node can be increased by 1, that is, the value of the "number of connections established" element (the third element) is increased by 1.
[0081] In one possible implementation, in order to further reduce resource consumption in load balancing, in an embodiment of the present application, if it is determined that the number of connection failures of the connection node is greater than a preset number, then when the connection node is determined to be an unavailable node, it can be specifically determined whether the number of connection failures of the connection node is 0; if it is determined that the number of connection failures of the connection node is 0, it means that the current connection node has failed to connect for the first time, then the "number of connection failures" value of the connection node can be updated to 1, and the step of selecting a target node from the node pool to establish a connection is continued, that is, the method of establishing a connection is recursively called once, and the next connection node is selected from the node pool to establish a connection.
[0082] If it is determined that the number of connection failures of the connection node is not 0, the current connection node can be directly determined as an unavailable node, and then the connection node can be moved from the node pool to the unavailable node connection table to wait for detection by the liveness detection thread.
[0083] Furthermore, since the unavailable nodes in the node pool are moved to the unavailable node connection table, the database nodes can be dynamically scaled down. Moreover, since the unavailable nodes in the node pool are determined by the "number of connection establishment and connection failure" of the nodes, the load balancing of database access can be achieved while the database nodes are dynamically scaled down. In addition, since no additional hardware or software is used in the load balancing process, compared with the prior art, the resource consumption can be further reduced in the load balancing process.
[0084] In a possible implementation, in order to improve resource utilization, when starting a liveness detection operation, it is also possible to determine whether the number of liveness detections exceeds a preset maximum number of times. If it is determined that the number of liveness detections exceeds the preset maximum number of times, the liveness detection operation is terminated. On the contrary, if it is determined that the number of liveness detections does not exceed the preset maximum number of times, the node liveness detection operation is continued.
[0085] In one possible implementation, after determining whether there is an unavailable node in the unavailable node connection table of the database cluster, if it is determined that there is no unavailable node in the unavailable node connection table of the database cluster (that is, there is no node in the unavailable node connection table), a connection node can be directly determined from the node pool and a connection can be established with the connection node.
[0086] In summary, this application has the following advantages:
[0087] (1) Since the dynamic shrinkage and expansion of the database cluster nodes are monitored, not only can the database cluster node status be synchronized in a relatively timely manner, but also the load balancing of the database cluster access connection can be achieved. Moreover, since no additional hardware or software is used in the load balancing process, compared with the existing technology, resource consumption can be further reduced in load balancing.
[0088] For example, A. By covering the activity detection of unavailable nodes in the database cluster and the timely detection of newly added nodes, connection load balancing can be achieved in multiple scenarios such as abnormal database node offline, node shrinkage, and node expansion.
[0089] B. Use Vector objects through the node pool to effectively perform asynchronous operations in a multi-threaded environment to ensure the consistency and correctness of the database node status.
[0090] C. By using ArrayList objects to represent the connection information of a single node, it is convenient and efficient to locate, obtain, and modify the corresponding information of the node.
[0091] D. By identifying the connection establishment mode (isNewConnection), the adaptive method can be accurately selected to establish the connection, thereby quickly determining whether the database cluster node load is balanced.
[0092] (2) Since database connection establishment, new node detection, and node selection are divided into independent modules, the low coupling between modules can be ensured through the element drive of the node pool and the unavailable node list, thereby improving the flexibility and adaptability within the module.
[0093] Based on the same inventive concept, the embodiment of the present application provides a load balancing device 30, which is applied to the JDBC driver of the database, such as Figure 3 As shown, the load balancing device 30 includes:
[0094] A first determining unit 301 is used to determine whether there is an unavailable node in the unavailable node connection table of the database cluster;
[0095] The second determining unit 302 is used to determine whether to start the live detection operation if it is determined that there is an unavailable node in the unavailable node connection table of the database cluster;
[0096] The third determining unit 303 is used to determine whether each node is an unavailable node according to the number of connection establishment and connection failure of each node in the node pool of the database cluster if it is determined that the live detection job is started;
[0097] The node moving unit 304 is used to move any node in the node pool to the unavailable node connection table if it is determined that any node is an unavailable node.
[0098] Optionally, the load balancing device 30 further includes a node loading unit 305, and the node loading unit 305 is used to:
[0099] Add the IPS attribute to the database cluster's URL connection string. The IPS attribute is used to configure the IP addresses of all nodes in the database cluster.
[0100] In the database cluster, a new node pool is added; the node pool is the global connection control list ipVectors, each node in the node pool is a Vector object, and each node includes five elements: IP address, listening port, number of connection establishments, number of connection failures, and whether it is a newly connected IP node.
[0101] Optionally, the node loading unit 305 is further used for:
[0102] When parsing the URL connection string for the first time, the IPS attribute value is split into multiple nodes using commas as separators;
[0103] Put each node into the node pool in the form of a first preset element, and start a new node listening job; wherein the first preset element form includes the element values of the five elements of the corresponding node;
[0104] According to the newly added node monitoring job, the database cluster node list is obtained once at a preset interval, and the database cluster node list is compared with the nodes in the node pool to determine whether there is a newly added node in the database cluster node list;
[0105] If it is determined that there is a new node in the database cluster node list, the new node is placed in the node pool in the second preset element form; wherein the second preset element form includes element values of the five elements of the corresponding node.
[0106] Optionally, the node mobile unit 304 is further configured to:
[0107] If it is determined that the live detection operation is started, the target node selection method is determined according to the preset global variables;
[0108] According to the target node selection method, establish connections with all nodes in the unavailable node connection table;
[0109] For any node in the unavailable node connection table, determine whether the connection with any node is successful;
[0110] If it is determined that the connection with any node is successful, any node is moved from the unavailable node connection list to the node pool in the form of a second preset element; wherein the second preset element form includes the element values of the five elements of the corresponding node.
[0111] Optionally, the third determining unit 303 is further configured to:
[0112] If the target node selection method is the round-robin allocation mode, the connection node is obtained from the node pool in a round-robin manner;
[0113] If the target node selection mode is the new node joining mode, the connection node is obtained according to the connection load of each node in the node pool;
[0114] Determine whether the fifth element of the connected node is false;
[0115] If it is determined that the fifth element of the connected node is false, the fifth element of the newly added node in the node pool is set to false, and the target node selection method is set to the round-robin allocation mode.
[0116] Optionally, the third determining unit 303 is further configured to:
[0117] For the connection node, if it is determined that the connection node is an available node, then try to establish a connection with the connection node;
[0118] Determine whether the number of connection failures of the connection node is greater than a preset number;
[0119] If it is determined that the number of connection failures of the connection node is greater than a preset number, the connection node is determined as an unavailable node.
[0120] Optionally, the third determining unit 303 is further configured to:
[0121] Determine whether the number of connection failures of the connecting node is 0;
[0122] If it is determined that the number of connection failures of the connection node is 0, the number of connection failures is updated to 1, and the step of selecting a target node from the node pool to establish a connection is continued;
[0123] If the number of connection failures of the connection node is determined to be not 0, the connection node is determined to be an unavailable node.
[0124] The load balancing device 30 can be used to perform Figure 2 The method executed in the embodiment shown in FIG. 1 is a method for performing the functions of each functional module of the load balancing device 30. Figure 2 The description of the illustrated embodiment is not repeated in detail.
[0125] In some possible implementations, various aspects of the method provided in the present application may also be implemented in the form of a program part, which includes a program code. When the program part is run on a computer device, the program code is used to enable the computer device to execute the steps of the method according to various exemplary embodiments of the present application described above in this specification. For example, the computer device may execute the following steps: Figure 2 The method performed in the illustrated embodiment.
[0126] It can be understood by those skilled in the art that all or part of the steps of the above method embodiment can be completed by hardware related to program instructions, and the aforementioned program can be stored in a computer-readable storage medium. When the program is executed, the steps of the above method embodiment are executed; and the aforementioned storage medium includes: various media that can store program codes, such as mobile storage devices, read-only memory (ROM), random access memory (RAM), magnetic disk or optical disk. Alternatively, if the above integrated unit of the present invention is implemented in the form of a software function module and sold or used as an independent part, it can also be stored in a computer-readable storage medium. Based on such an understanding, the technical solution of the embodiment of the present invention can be essentially or in other words, the part that contributes to the prior art can be embodied in the form of a software part, which is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the methods described in each embodiment of the present invention. And the aforementioned storage medium includes: various media that can store program codes, such as mobile storage devices, ROM, RAM, magnetic disk or optical disk.
[0127] Although the preferred embodiments of the present application have been described, those skilled in the art may make additional changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications falling within the scope of the present application.
[0128] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the spirit and scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.< / arraylist>
Claims
1. A load balancing method, characterized in that: A JDBC driver applied to a database, the method comprising: In the URL connection string of the database cluster, an IPS attribute is added; wherein the IPS attribute is used to configure the IP addresses of all nodes of the database cluster; In the database cluster, a new node pool is added; wherein the node pool is a global connection control list ipVectors, each node in the node pool is a Vector object, and each node includes five elements: IP address, listening port, number of connection establishments, number of connection failures, and whether it is a newly accessed IP node; When parsing the URL connection string for the first time, the IPS attribute value is decomposed into multiple nodes using commas as separators; Put each node into the node pool in the form of a first preset element, and start a new node monitoring operation; wherein the first preset element form includes the element values of the five elements of the corresponding node; According to the newly added node monitoring operation, obtaining a database cluster node list is performed once at a preset interval, and the database cluster node list is compared with the nodes in the node pool to determine whether there is a newly added node in the database cluster node list; If it is determined that there is a newly added node in the database cluster node list, the newly added node is placed in the node pool in the form of a second preset element; wherein the second preset element form includes element values of each of the five elements of the corresponding node; Determine whether there is an unavailable node in the unavailable node connection table of the database cluster; If it is determined that there is an unavailable node in the unavailable node connection table of the database cluster, determine whether to start the live detection job; If it is determined that the live detection job is started, determining whether each node is an unavailable node according to the number of connection establishments and connection failures of each node in the node pool of the database cluster; For any node in the node pool, if it is determined that any node is an unavailable node, the any node is moved from the node pool to the unavailable node connection table.
2. The method according to claim 1, characterized in that If it is determined that there is an unavailable node in the unavailable node connection table of the database cluster, after determining whether to start the liveness detection job, the method further includes: If it is determined that the live detection operation is started, the target node selection method is determined according to the preset global variables; According to the target node selection method, establishing connections with all nodes in the unavailable node connection table; For any node in the unavailable node connection table, determine whether the connection with the any node is successful; If it is determined that the connection with any of the nodes is successful, any of the nodes is moved from the unavailable node connection list to the node pool in the form of a second preset element; wherein the second preset element form includes the element values of the five elements of the corresponding node.
3. The method according to claim 2, characterized in that The step of determining whether each node is an unavailable node according to the number of connection establishments and the number of connection failures of each node in the node pool of the database cluster includes: If the target node selection mode is a round-robin allocation mode, a connection node is obtained from the node pool in a round-robin manner; If the target node selection mode is a new node joining mode, obtaining a connection node according to the connection load of each node in the node pool; Determine whether the fifth element of the connection node is false; If it is determined that the fifth element of the connection node is false, the fifth element of the newly added node in the node pool is set to false, and the target node selection mode is set to a round-robin allocation mode.
4. The method according to claim 3, characterized in that The step of determining whether each node is an unavailable node according to the number of connection establishments and the number of connection failures of each node in the node pool of the database cluster includes: For the connection node, if it is determined that the connection node is an available node, try to establish a connection with the connection node; Determine whether the number of connection failures of the connection node is greater than a preset number; If it is determined that the number of connection failures of the connection node is greater than a preset number, the connection node is determined as an unavailable node.
5. The method according to claim 4, characterized in that The step of determining the connection node as an unavailable node if it is determined that the number of connection failures of the connection node is greater than a preset number of times comprises: Determine whether the number of connection failures of the connection node is 0; If it is determined that the number of connection failures of the connection node is 0, the value of the number of connection failures is updated to 1, and the step of selecting a target node from the node pool to establish a connection is continued; If it is determined that the number of connection failures of the connection node is not 0, the connection node is determined as an unavailable node.
6. A load balancing device, characterized in that: A JDBC driver applied to a database, the device comprising: A node loading unit, used to add an IPS attribute to the URL connection string of a database cluster; wherein the IPS attribute is used to configure the IP addresses of all nodes of the database cluster; in the database cluster, a node pool is added; wherein the node pool is a global connection control list ipVectors, each node in the node pool is a Vector object, and each node includes five elements: an IP address, a listening port, the number of connection establishment times, the number of connection failures, and whether it is a newly connected IP node; when parsing the URL connection string for the first time, the IPS attribute value is disassembled into multiple nodes with a comma as a separator; each node is placed in the node pool in the form of a first preset element, and a new node monitoring operation is started; wherein the first preset element form includes the element values of the five elements of the corresponding node; according to the new node monitoring operation, the database cluster node list is obtained once at a preset interval, and the database cluster node list is compared with the nodes in the node pool to determine whether there is a new node in the database cluster node list; if it is determined that there is a new node in the database cluster node list, the new node is placed in the node pool in the form of a second preset element; wherein the second preset element form includes the element values of the five elements of the corresponding node; A first determining unit, used to determine whether there is an unavailable node in the unavailable node connection table of the database cluster; A second determining unit, configured to determine whether to start a liveness search operation if it is determined that there is an unavailable node in the unavailable node connection table of the database cluster; A third determining unit is used to determine whether each node is an unavailable node according to the number of connection establishments and connection failures of each node in the node pool of the database cluster if it is determined that the live detection job is started; The node moving unit is used to move any node in the node pool from the node pool to the unavailable node connection table if it is determined that the any node is an unavailable node.
7. An electronic device, characterized in that: The device comprises: A memory for storing program instructions; A processor is used to call the program instructions stored in the memory, and execute any method according to claims 1-5 according to the obtained program instructions.
8. A storage medium, characterized in that: The storage medium stores computer-executable instructions, and the computer-executable instructions are used to enable a computer to execute any one of the methods in claims 1-5.
Citation Information
Patent Citations
Automatic routing and load balancing method and system based on database connection
CN102810116A
Database exception processing method, device and equipment and computer readable storage medium
CN116340045A