WO2021151293A1 - Hash ring load balancing method and apparatus, and electronic device and storage medium - Google Patents

Hash ring load balancing method and apparatus, and electronic device and storage medium Download PDF

Info

Publication number
WO2021151293A1
WO2021151293A1 PCT/CN2020/117659 CN2020117659W WO2021151293A1 WO 2021151293 A1 WO2021151293 A1 WO 2021151293A1 CN 2020117659 W CN2020117659 W CN 2020117659W WO 2021151293 A1 WO2021151293 A1 WO 2021151293A1
Authority
WO
WIPO (PCT)
Prior art keywords
virtual service
service node
service nodes
virtual
nodes
Prior art date
Application number
PCT/CN2020/117659
Other languages
French (fr)
Chinese (zh)
Inventor
乐伟
Original Assignee
平安科技(深圳)有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 平安科技(深圳)有限公司 filed Critical 平安科技(深圳)有限公司
Publication of WO2021151293A1 publication Critical patent/WO2021151293A1/en

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5083Techniques for rebalancing the load in a distributed system
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5027Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals
    • G06F9/505Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals considering the load
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L67/00Network arrangements or protocols for supporting network services or applications
    • H04L67/01Protocols
    • H04L67/10Protocols in which an application is distributed across nodes in the network
    • H04L67/1001Protocols in which an application is distributed across nodes in the network for accessing one among a plurality of replicated servers
    • H04L67/1004Server selection for load balancing
    • H04L67/1023Server selection for load balancing based on a hash applied to IP addresses or costs

Definitions

  • This application relates to the field of computer technology, and in particular to a method, device, electronic device, and storage medium for hash ring load balancing.
  • the main reason is that the hash value of the service node cannot be distributed evenly on the hash ring, which causes the request message sent to the service node to not be evenly distributed to each service node.
  • this application provides a hash ring load balancing method, including:
  • a virtual service node is selected from the virtual service nodes corresponding to multiple real service nodes to form a virtual service node group to obtain multiple virtual service node groups, each of the virtual service nodes in the virtual service node group The order of the corresponding real service nodes is the same;
  • the virtual service node queue is evenly distributed on the hash ring.
  • This application also provides a hash ring load balancing device including:
  • the virtual unit is used to virtualize each real service node into a number of equal number of virtual service nodes to obtain the virtual service node corresponding to each real service node;
  • the grouping unit is used to select a virtual service node from the virtual service nodes corresponding to the multiple real service nodes to form a virtual service node group to obtain multiple virtual service node groups, each of the virtual service node groups
  • the order of the virtual service nodes corresponding to the real service nodes in the virtual service node is the same;
  • the connecting unit is used to connect the virtual service nodes in each virtual service node group end to end to obtain a virtual service node queue
  • the distribution unit is configured to evenly distribute the virtual service node queue on the hash ring.
  • the present application also provides an electronic device, including a memory and a processor, the memory stores a computer program, and the processor implements the following steps when executing the computer program:
  • a virtual service node is selected from virtual service nodes corresponding to multiple real service nodes to form a virtual service node group to obtain multiple virtual service node groups, each of the virtual service nodes in the virtual service node group The order of the corresponding real service nodes is the same;
  • the virtual service node queue is evenly distributed on the hash ring.
  • This application also provides a readable storage medium on which a computer program is stored, and when the computer program is executed by a processor, the following steps are implemented:
  • a virtual service node is selected from the virtual service nodes corresponding to multiple real service nodes to form a virtual service node group to obtain multiple virtual service node groups, each of the virtual service nodes in the virtual service node group The order of the corresponding real service nodes is the same;
  • the virtual service node queue is evenly distributed on the hash ring.
  • This application increases the number of nodes on the hash ring by virtualizing each real service node with an equal number of virtual service nodes; the virtual service nodes virtualized corresponding to different real service nodes form a group, In this way, the real service nodes corresponding to the virtual service nodes in each group are spaced; then, each group is connected end to end to obtain a virtual service node queue containing all virtual service nodes, and all virtual service nodes are sorted according to the queue Evenly distributed on the hash ring, so that not only all virtual service nodes can be evenly distributed on the hash ring, but also the virtual service nodes virtualized by each real service node are evenly distributed on the hash ring , So that the load of each service node on the hash ring is more balanced.
  • Figure 1 is a schematic diagram of the current distribution and load of service nodes on the consistent hash ring.
  • FIG. 2 is a schematic diagram of the steps of a method for balancing a load of a hash ring in an embodiment of the present application.
  • Fig. 3 is a schematic diagram of the distribution and load of virtual service nodes on the hash ring in an embodiment of the present application.
  • FIG. 4 is a schematic diagram of the distribution and load of virtual service nodes on the hash ring after capacity expansion based on the distribution shown in FIG. 3.
  • FIG. 5 is a schematic block diagram of the structure of a hash ring load balancing device in an embodiment of the present application.
  • FIG. 6 is a schematic block diagram of the structure of an electronic device in an embodiment of the present application.
  • Fig. 7 is a schematic diagram of a readable storage medium in an embodiment of the present application.
  • the technical solution of the present application can be applied to the field of blockchain and/or big data technology to achieve load balancing.
  • the design goal of the consistent Hash algorithm is to solve the hotspot problem in the Internet, and its original intention is very similar to CARP.
  • the consistent Hash algorithm is a special Hash algorithm. Due to its balanced and durable mapping characteristics, it is widely used in the load balancing field. For example, nginx and memcached both use a consistent Hash algorithm as a cluster load balancing solution. .
  • the consistent hash algorithm is used as a load balancing method, there are still some load imbalance problems. The main reasons are: first, the hash value of the service node cannot be distributed evenly on the hash ring; second, there are too few service nodes, and the hash value of the source request is more likely to be distributed on the same service node.
  • a method for balancing load of a hash ring includes:
  • Step S1 virtualize each real service node into a plurality of equal number of virtual service nodes, and obtain a virtual service node corresponding to each real service node;
  • Step S2 selecting a virtual service node from the virtual service nodes corresponding to the multiple real service nodes to form a virtual service node group to obtain multiple virtual service node groups, each of the virtual service node groups
  • the order of the real service nodes corresponding to the virtual service nodes is the same;
  • Step S3 connecting the virtual service nodes in each virtual service node group end to end to obtain a virtual service node queue
  • Step S4 Distribute the virtual service node queue evenly on the hash ring.
  • each real service node is virtualized into multiple virtual service nodes, and the number of virtual service nodes virtualized by each real service node is equal, so that the number of virtual service nodes corresponding to each real service node is obtained.
  • a virtual service node
  • each real service node virtualizes three virtual service nodes.
  • the real service node Node1 virtualizes three virtual service nodes, V1Node1, V2Node1, and V3Node1.
  • the real service node Node2 virtualizes three virtual service nodes, V1, Node2, V2, Node2, and V3, Node2.
  • the real service node Node3 virtualizes three virtual service nodes, V1 Node3, V2 Node3, and V3 Node3.
  • each virtual service node is distributed on the hash ring instead of the real service node to increase the number of service nodes on the hash ring.
  • each real service node can be understood as a server.
  • a virtual service node is selected from the virtual service nodes corresponding to each real service node, for example:
  • the virtual service node V1 Node3 is selected from the three virtual service nodes to form a virtual service node group V1 Node1, V1 Node2, and V1 Node3.
  • the virtual service node V2Node3 is selected from the three virtual service nodes in, so as to form a virtual service node group V2Node1, V2Node2, and V2Node3.
  • the virtual service node V3Node3 is selected from the three virtual service nodes in, so as to form a virtual service node group V3Node1, V3Node2, and V3Node3.
  • each virtual service node group is a virtual service node corresponding to the real service node Node1
  • the virtual service node corresponding to the real service node Node2 is ranked in the middle
  • the virtual service node corresponding to the real service node Node3 is ranked last.
  • the virtual service nodes in each virtual service node group are connected end to end to obtain a virtual service node queue, for example:
  • the virtual service node After connecting the above three virtual service node groups end to end, the virtual service node queues V1 Node1, V1 Node2, V1 Node3, V2 Node1, V2 Node2, V2 Node3, V3 Node1, V3 Node2, V3 Node3 are obtained.
  • the virtual service node queues obtained through the above steps are evenly distributed on the hash ring.
  • the virtual service nodes in the queue are aligned according to the The sequence in the column is evenly distributed on the hash ring, so that request a, request b, and request c can be more evenly loaded on each real service node.
  • V_Node is the original virtual node
  • V_Node' is the virtual node after capacity expansion.
  • hash%4 is modulo. If another service node is added, hash%5 is modulated, so that only the first 25 The% hash modulus has not changed, and the rest will change. That is, the influence ratio is 75%.
  • the expansion of the cluster will have a much smaller proportion of the load node change, and the hash ring characteristics are retained to the greatest extent.
  • each virtual service node Step S2 in which the order of the virtual service nodes corresponding to the real service nodes in the group is the same includes:
  • Step S21 respectively numbering each of the real service nodes and the virtual service nodes corresponding to each of the real service nodes, wherein the numbering sequence of the virtual service nodes corresponding to each of the real service nodes is the same;
  • step S22 the virtual service nodes with the same number are formed into one virtual service node group, and the virtual service node groups are arranged according to the corresponding number of the real service node in each virtual service node group.
  • each real service node may be numbered first, for example, the three real service nodes are respectively numbered Node1, Node2, and Node3, so as to facilitate the distinction between different real service nodes.
  • the virtual service nodes virtualized by each real service node are numbered, and the numbering sequence of the virtual service nodes corresponding to each real service node is the same.
  • the three virtual service nodes virtualized by one real service node Node are numbered V1, V2, and V3, respectively, and the three virtual service nodes virtualized by each of the remaining real service nodes are numbered V1, V2, and V3, respectively.
  • the virtual service nodes with the same number are formed into a virtual service node group.
  • the virtual service node numbered V1 virtualized by the real service node Node1 is recorded as V1Node1
  • the virtual service node numbered V1 virtualized by the real service node Node2 is recorded as V1 Node2
  • the virtual service node virtualized by the real service node Node 3 The virtual service node numbered V1 is recorded as V1 and Node3 forms a virtual service node group, which is recorded as the first virtual service node group.
  • the virtual service node numbered V2 virtualized by the real service node Node1 is recorded as V2Node1
  • the virtual service node numbered V2 virtualized by the real service node Node2 is recorded as V2 Node2
  • the number virtualized by the real service node Node3 is The virtual service section of V2 is marked as V2 Node3 points to form a virtual service node group, which is recorded as the second virtual service node group.
  • the virtual service node numbered V3 virtualized by the real service node Node1 is recorded as V3Node1
  • the virtual service node numbered V3 virtualized by the real service node Node2 is recorded as V3 Node2
  • the virtual service node numbered by the real service node Node3 is The virtual service node of V3 is recorded as V3 Node3 forms a virtual service node group, which is recorded as the third virtual service node group.
  • the three virtual service nodes V1, Node1, V1, Node2, and V1, Node3 are arranged according to the numbers of the corresponding three real service nodes Node1, Node2, and Node3, namely V1, Node1, V1, Node2, and V1 Node3.
  • the three virtual service nodes V2, Node1, V2, Node2, and V2, Node3 are arranged according to the numbers of the corresponding three real service nodes, Node1, Node2, and Node3, namely V2, Node1, V2, Node2, and V2 Node3, in the third virtual service node group, the three virtual service nodes V3 Node1, V3 Node2, and V3 Node3 are arranged according to the numbers of the corresponding three real service nodes Node1, Node2, and Node3, namely V3, Node1, V3, Node2, and V3Node3.
  • the virtual service nodes in each virtual service node group are sorted, so that the corresponding relationship between the virtual service node and the real service node is more intuitive, which is convenient for analysis and comparison and load verification.
  • the above step S4 of evenly distributing the virtual service node queue on the hash ring includes:
  • Step S41 taking the arc length between two adjacent virtual service nodes on the hash ring as a tolerance, and calculating the value of the equal difference common term based on the common term formula of the arithmetic sequence;
  • Step S42 based on the correspondence between the virtual service node in the virtual service node queue and the equal difference pass item, map the virtual service node queue on the hash ring according to the value of the equal difference pass item.
  • the arc length between two adjacent virtual service nodes on the hash ring is used as the tolerance, and the value of the equal difference common term is calculated based on the common term formula of the arithmetic sequence, for example:
  • a n represents the general term.
  • a 1 represents the first item.
  • n represents the number of items. Let the number of items n be equal to the number of all virtual nodes in the virtual service node queue. d stands for tolerance.
  • the virtual service node queue is mapped on the hash ring according to the value of the equal difference pass item.
  • the correspondence relationship between the virtual service node through the arithmetic term refers to the position on behalf of a n term virtual service node queue n-th hash virtual service node on the ring to construct general term virtual service and a n Correspondence of nodes.
  • step S41 is calculated through the numerical entries contained a n, according to the above correspondence relationship, the virtual service node queue mapping each virtual service node on the ring a n hash values corresponding to the position .
  • V1 Node1, V1 Node2, and V1 Node3 in the virtual service node queue are mapped to positions 1073741823, 2147483647, and 3221225471 on the hash ring, respectively.
  • all virtual service nodes in the queue of all virtual service nodes are mapped on the hash ring, so that all virtual service nodes are evenly distributed on the hash ring.
  • the arc length between two adjacent virtual service nodes on the hash ring is used as the tolerance, and the value of the equal difference common term is calculated based on the common term formula of the arithmetic sequence before step S21 ,
  • the method further includes:
  • Step S21 Obtain the value range of the hash value of the virtual service node, and obtain the perimeter of the hash ring according to the value range;
  • Step S22 Calculate the arc length according to the perimeter of the hash ring and the number of virtual service nodes on the hash ring.
  • the perimeter of the hash ring can be obtained according to the value range of the hash value of the virtual service node.
  • the specific method for obtaining the perimeter of the hash ring may be to use the largest value in the range of the hash value of the virtual service node as the perimeter of the hash ring, so as to facilitate the calculation of the radius of the hash ring according to the perimeter of the hash ring in the next step.
  • the perimeter c of the hash ring is 2 32 -1, and the virtual service node on the hash ring divides the hash ring into n equal parts, so that the value of any two adjacent virtual service nodes on the hash ring can be calculated.
  • the arc length between l (2 32 -1)/n.
  • represents the radian value
  • r represents the radius of the hash
  • m represents the angle of the center of the circle
  • the above step S21 of obtaining the value range of the hash value of the virtual service node, and obtaining the perimeter of the hash ring according to the value range includes:
  • Step S211 Use the maximum value corresponding to the value range as the circumference of the hash ring.
  • the arc length between two adjacent virtual service nodes on the hash ring is used as the tolerance, and after the step S41 of calculating the value of the equal difference common term based on the common term formula of the arithmetic sequence, so The method also includes:
  • step S411 the value of the equal difference pass term is taken as an integer.
  • each value calculated by the equal difference general term is taken as an integer, for example:
  • the number of virtual service nodes in the virtual service node queue is an even number, so that the load is more balanced.
  • the load of each real service node is verified as follows:
  • the real service node Node6, load ratio 0.1235;
  • the real service node Node8 load ratio: 0.13.
  • the hash value of the requested information can be calculated according to the hash algorithm, and then the hash value of the requested information and the hash value of each virtual service node will be requested. Assigned to the closest virtual service node in the clockwise direction.
  • the hash function uses prime numbers (unless 1) to avoid overflow and cause loss of information.
  • the virtual machine uses 2 ⁇ 5-1 by default to improve the performance of computing hash.
  • the hash algorithm is:
  • the hash initialization value is 0.
  • the initial value of ascii is 32.
  • ascii represents the ascii code of the string that needs to be hashed. For example, it needs to be hashed according to the ip.
  • the ascii here is the ascii code of the ip character array. For example, if ip is 114.114.114.114, the calculated value is -1009865594, and then the absolute value needs to be 1009865594.
  • the request information is hashed through the hash algorithm to improve the discrete probability.
  • the request is mapped to the hash ring.
  • the hash value of the request information obtained after hash calculation is 1009865594.
  • the request information is mapped to the 1009865594 location on the hash.
  • the present application distributes the virtual service nodes obtained after virtualization evenly on the hash ring, and the virtual nodes virtualized by each real service node are also evenly distributed on the hash ring, so that each The probability of a data request load on each service node is as uniform as possible.
  • the characteristics of the hash ring can be retained to the greatest extent. After a certain server/service node fails, or a new service node is added for expansion, the impact ratio is greatly reduced, making the server cluster more stable and reliable.
  • a hash ring load balancing device which includes a virtual unit 10, a grouping unit 20, a connection unit 30, and a distribution unit 40.
  • the virtual unit 10 is used to virtualize each real service node into a plurality of virtual service nodes of equal number, and then obtain the virtual service node corresponding to each real service node.
  • the grouping unit 20 is configured to select one virtual service node from the virtual service nodes corresponding to the multiple real service nodes to form a virtual service node group, thereby obtaining multiple virtual service node groups. Wherein, the order of the real service nodes corresponding to the virtual service nodes in each virtual service node group is the same.
  • the connecting unit 30 is used to connect the virtual service nodes in each virtual service node group end to end to obtain a virtual service node queue.
  • the distribution unit 40 is used to evenly distribute the virtual service node queue on the hash ring.
  • the hash ring load balancing device further includes a hash value calculation unit and a distribution unit for calculating request information.
  • the hash value calculation unit of the request information is used to calculate the hash value of the request information.
  • the allocation unit is configured to allocate the request information to the virtual service node closest in a clockwise direction according to the hash value of the request information.
  • the distribution unit is configured to: use the arc length between two adjacent virtual service nodes on the hash ring as a tolerance, and calculate the value of the equal difference common term based on the general term formula of the arithmetic sequence Based on the correspondence between the virtual service node in the virtual service node queue and the equal difference pass item, the virtual service node queue is mapped to the hash ring according to the value of the equal difference pass item.
  • the distribution unit is configured to: obtain the value range of the hash value of the virtual service node, and obtain the perimeter of the hash ring according to the value range;
  • the arc length is calculated from the number of the virtual service nodes on the hash ring.
  • the distribution unit is configured to: use the arc length between two adjacent virtual service nodes on the hash ring as a tolerance, and calculate the value of the equal difference common term based on the general term formula of the arithmetic sequence After that, the value of the equal difference general term is taken as an integer.
  • the grouping unit is configured to: respectively number each real service node and the virtual service node corresponding to each real service node, wherein the virtual service corresponding to each real service node The numbering sequence of the nodes is the same; virtual service nodes with the same number are formed into a virtual service node group, and the virtual service node groups are arranged according to the corresponding real service node number.
  • the example embodiments described here can be implemented by software, or can be implemented by combining software with necessary hardware. Therefore, the technical solution according to the embodiments of the present application can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (which can be a CD-ROM, U disk, mobile hard disk, etc.) or on the network , Including several instructions to make a computing device (which can be a personal computer, a server, a mobile terminal, or a network device, etc.) execute the method according to the embodiment of the present application.
  • a non-volatile storage medium which can be a CD-ROM, U disk, mobile hard disk, etc.
  • Including several instructions to make a computing device which can be a personal computer, a server, a mobile terminal, or a network device, etc.
  • some embodiments of the present application also provide an electronic device for implementing the above method for balancing load of the hash ring.
  • the electronic device 500 shown in FIG. 6 is only an example, and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
  • the electronic device 500 is represented in the form of a general-purpose computing device.
  • the components of the electronic device 500 may include but are not limited to: at least one processing unit 510, at least one storage unit 520, and a bus 530 connecting different system components (including the storage unit 520 and the processing unit 510).
  • the storage unit stores program code, and the program code can be executed by the processing unit 510, so that the processing unit 510 executes the various exemplary methods described in the “Exemplary Method” section of this specification. Steps of implementation.
  • the processing unit 510 may execute steps S1 to S4 as shown in FIG. 2.
  • the storage unit 520 may include a readable medium in the form of a volatile storage unit, such as a random access storage unit (RAM) 5201 and/or a cache storage unit 5202, and may further include a read-only storage unit (ROM) 5203.
  • RAM random access storage unit
  • ROM read-only storage unit
  • the storage unit 520 may also include a program/utility tool 5204 having a set (at least one) program module 5205.
  • program module 5205 includes but is not limited to: an operating system, one or more application programs, other program modules, and program data, Each of these examples or some combination may include the implementation of a network environment.
  • the bus 530 may represent one or more of several types of bus structures, including a storage unit bus or a storage unit controller, a peripheral bus, a graphics acceleration port, a processing unit, or a local area using any bus structure among multiple bus structures. bus.
  • the electronic device 500 may also communicate with one or more external devices 1000 (such as keyboards, pointing devices, Bluetooth devices, etc.), and may also communicate with one or more devices that enable a user to interact with the electronic device 800, and/or communicate with Any device (such as a router, modem, etc.) that enables the electronic device 500 to communicate with one or more other computing devices. Such communication may be performed through an input/output (I/O) interface 540.
  • the electronic device 500 may also communicate with one or more networks (for example, a local area network (LAN), a wide area network (WAN), and/or a public network, such as the Internet) through the network adapter 560.
  • networks for example, a local area network (LAN), a wide area network (WAN), and/or a public network, such as the Internet
  • the network adapter 560 communicates with other modules of the electronic device 500 through the bus 530.
  • other hardware and/or software modules can be used in conjunction with the electronic device 500, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives And data backup storage system, etc.
  • the example embodiments described here can be implemented by software, or can be implemented by combining software with necessary hardware. Therefore, the technical solution according to the embodiments of the present application can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (which can be a CD-ROM, U disk, mobile hard disk, etc.) or on the network , Including several instructions to make a computing device (which can be a personal computer, a server, a terminal device, or a network device, etc.) execute the method according to the embodiment of the present application.
  • a computing device which can be a personal computer, a server, a terminal device, or a network device, etc.
  • the embodiment of the present application also provides a readable storage medium, such as a computer readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, some or all of the steps of the above hash ring load balancing method are realized.
  • the media involved in this application such as computer-readable storage media, may be non-volatile or volatile.
  • a computer-readable storage medium is also provided, on which a program product capable of implementing the above-mentioned hash ring load balancing method of this specification is stored.
  • various aspects of the present application can also be implemented in the form of a program product, which includes program code. When the program product runs on a terminal device, the program code is used to make the The electronic device executes the steps described in the above-mentioned "Exemplary Method" section of this specification according to various exemplary embodiments of the present application.
  • FIG. 7 is a computer-readable storage medium for realizing the above-mentioned hash ring load balancing method according to an exemplary embodiment of the present application.
  • FIG. 7 depicts a program product 600 for implementing the above-mentioned hash ring load balancing method according to an embodiment of the present application, which can adopt a portable compact disk read-only memory (CD-ROM) and include program code, and can be installed in an electronic device, For example, running on a personal computer.
  • CD-ROM portable compact disk read-only memory
  • the program product of this application is not limited to this.
  • the readable storage medium can be any tangible medium that contains or stores a program, and the program can be used by or in combination with an instruction execution system, device, or device.
  • the program product can use any combination of one or more readable media.
  • the readable medium may be a readable signal medium or a readable storage medium.
  • the readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or device, or a combination of any of the above. More specific examples (non-exhaustive list) of readable storage media include: electrical connections with one or more wires, portable disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable Type programmable read only memory (EPROM or flash memory), optical fiber, portable compact disk read only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination of the above.
  • the computer-readable signal medium may include a data signal propagated in baseband or as a part of a carrier wave, and readable program code is carried therein. This propagated data signal can take many forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the foregoing.
  • the readable signal medium may also be any readable medium other than a readable storage medium, and the readable medium may send, propagate, or transmit a program for use by or in combination with the instruction execution system, apparatus, or device.
  • the program code contained on the readable medium can be transmitted by any suitable medium, including but not limited to wireless, wired, optical cable, RF, etc., or any suitable combination of the foregoing.
  • the program code used to perform the operations of the present application can be written in any combination of one or more programming languages.
  • the programming languages include object-oriented programming languages—such as Java, C++, etc., as well as conventional procedural programming languages. Programming language-such as "C" language or similar programming language.
  • the program code can be executed entirely on the user's computing device, partly on the user's device, executed as an independent software package, partly on the user's computing device and partly executed on the remote computing device, or entirely on the remote computing device or server Executed on.
  • the remote computing device can be connected to a user computing device through any kind of network, including a local area network (LAN) or a wide area network (WAN), or it can be connected to an external computing device (for example, using Internet service providers). Business to connect via the Internet).
  • LAN local area network
  • WAN wide area network
  • Internet service providers for example, using Internet service providers.

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Computer Networks & Wireless Communication (AREA)
  • Signal Processing (AREA)
  • Computer And Data Communications (AREA)

Abstract

A hash ring load balancing method and apparatus, and an electronic device and a storage medium. The method comprises: virtualizing a preset number of virtual service nodes for each real service node, so as to increase the number of nodes on a hash ring; forming virtual service nodes, with the same serial number, virtualized for different real service nodes into a group, and performing sequencing according to the corresponding serial numbers of the real service nodes, such that the real service nodes corresponding to the virtual service nodes in each group are spaced; and then, connecting each group of virtual service nodes to form a full virtual service node queue according to the sequence of the serial numbers of the virtual service nodes, and uniformly distributing, according to the queue, all the virtual service nodes on the hash ring, such that not only are all the virtual service nodes uniformly distributed on the hash ring, but the various virtual service nodes virtualized for each real service node are also uniformly distributed on the hash ring.

Description

hash环均衡负载方法、装置、电子设备和存储介质Hash ring balancing load method, device, electronic equipment and storage medium
本申请要求于2020年8月27日提交中国专利局、申请号为202010879098.8,发明名称为“hash环均衡负载方法、装置、电子设备和存储介质”的中国专利申请的优先权,其全部内容通过引用结合在本申请中。This application claims the priority of a Chinese patent application filed with the Chinese Patent Office on August 27, 2020, the application number is 202010879098.8, and the invention title is "hash ring balancing load method, device, electronic equipment, and storage medium". The entire content is approved The reference is incorporated in this application.
技术领域Technical field
本申请涉及计算机技术领域,特别涉及一种hash环均衡负载方法、装置、电子设备和存储介质。This application relates to the field of computer technology, and in particular to a method, device, electronic device, and storage medium for hash ring load balancing.
背景技术Background technique
发明人发现,目前使用一致性hash作为负载均衡时,存在负载不均衡的问题。主要原因是服务节点的hash值不能离散均匀的分布到hash环上,进而导致发送给服务节点的请求消息不能均匀地分发到各服务节点。The inventor found that the problem of unbalanced load exists when a consistent hash is currently used as load balancing. The main reason is that the hash value of the service node cannot be distributed evenly on the hash ring, which causes the request message sent to the service node to not be evenly distributed to each service node.
发明内容Summary of the invention
为解决以上负载不均匀的问题,本申请提供一种hash环均衡负载方法,包括:In order to solve the above uneven load problem, this application provides a hash ring load balancing method, including:
将每个真实服务节点虚拟出多个相等数量的虚拟服务节点,得到每个真实服务节点对应的虚拟服务节点;Virtualize each real service node into a number of equal number of virtual service nodes, and obtain the virtual service node corresponding to each real service node;
分别从多个真实服务节点对应的虚拟服务节点中选择一个虚拟服务节点组成一个虚拟服务节点群组,以得到多个虚拟服务节点群组,每个所述虚拟服务节点群组中的虚拟服务节点对应的真实服务节点的顺序相同;A virtual service node is selected from the virtual service nodes corresponding to multiple real service nodes to form a virtual service node group to obtain multiple virtual service node groups, each of the virtual service nodes in the virtual service node group The order of the corresponding real service nodes is the same;
将各虚拟服务节点群组中的虚拟服务节点首尾相连,得到虚拟服务节点队列;Connect the virtual service nodes in each virtual service node group end to end to obtain a virtual service node queue;
将所述虚拟服务节点队列均匀分布在hash环上。The virtual service node queue is evenly distributed on the hash ring.
本申请还提供一种hash环均衡负载装置包括:This application also provides a hash ring load balancing device including:
虚拟单元,用于将每个真实服务节点虚拟出多个相等数量的虚拟服务节点,得到每个真实服务节点对应的虚拟服务节点;The virtual unit is used to virtualize each real service node into a number of equal number of virtual service nodes to obtain the virtual service node corresponding to each real service node;
分组单元,用于分别从多个真实服务节点对应的虚拟服务节点中选择一个虚拟服务节点组成一个虚拟服务节点群组,以得到多个虚拟服务节点群组,每个所述虚拟服务节点群组中的虚拟服务节点对应的真实服务节点的顺序相同;The grouping unit is used to select a virtual service node from the virtual service nodes corresponding to the multiple real service nodes to form a virtual service node group to obtain multiple virtual service node groups, each of the virtual service node groups The order of the virtual service nodes corresponding to the real service nodes in the virtual service node is the same;
连接单元,用于将各虚拟服务节点群组中的虚拟服务节点首尾相连,得到虚拟服务节点队列;The connecting unit is used to connect the virtual service nodes in each virtual service node group end to end to obtain a virtual service node queue;
分布单元,用于将所述虚拟服务节点队列均匀分布在hash环上。The distribution unit is configured to evenly distribute the virtual service node queue on the hash ring.
本申请还提供一种电子设备,包括存储器和处理器,所述存储器中存储有计算机程序,所述处理器执行所述计算机程序时实现以下步骤:The present application also provides an electronic device, including a memory and a processor, the memory stores a computer program, and the processor implements the following steps when executing the computer program:
将每个真实服务节点虚拟出多个相等数量的虚拟服务节点,得到每个真实服务节点对应的虚拟服务节点;Virtualize each real service node into a number of equal number of virtual service nodes, and obtain the virtual service node corresponding to each real service node;
分别从多个真实服务节点对应的虚拟服务节点中选择一个虚拟服务节点组成一个虚拟服务节点群组,以得到多个虚拟服务节点群组,每个所述虚拟服务节点群组中的虚拟服务节点对应的真实服务节点的顺序相同;A virtual service node is selected from virtual service nodes corresponding to multiple real service nodes to form a virtual service node group to obtain multiple virtual service node groups, each of the virtual service nodes in the virtual service node group The order of the corresponding real service nodes is the same;
将各虚拟服务节点群组中的虚拟服务节点首尾相连,得到虚拟服务节点队列;Connect the virtual service nodes in each virtual service node group end to end to obtain a virtual service node queue;
将所述虚拟服务节点队列均匀分布在hash环上。The virtual service node queue is evenly distributed on the hash ring.
本申请还提供一种可读存储介质,其上存储有计算机程序,所述计算机程序被处理器执行时实现以下步骤:This application also provides a readable storage medium on which a computer program is stored, and when the computer program is executed by a processor, the following steps are implemented:
将每个真实服务节点虚拟出多个相等数量的虚拟服务节点,得到每个真实服务节点对应的虚拟服务节点;Virtualize each real service node into a number of equal number of virtual service nodes, and obtain the virtual service node corresponding to each real service node;
分别从多个真实服务节点对应的虚拟服务节点中选择一个虚拟服务节点组成一个虚拟服务节点群组,以得到多个虚拟服务节点群组,每个所述虚拟服务节点群组中的虚拟服务 节点对应的真实服务节点的顺序相同;A virtual service node is selected from the virtual service nodes corresponding to multiple real service nodes to form a virtual service node group to obtain multiple virtual service node groups, each of the virtual service nodes in the virtual service node group The order of the corresponding real service nodes is the same;
将各虚拟服务节点群组中的虚拟服务节点首尾相连,得到虚拟服务节点队列;Connect the virtual service nodes in each virtual service node group end to end to obtain a virtual service node queue;
将所述虚拟服务节点队列均匀分布在hash环上。The virtual service node queue is evenly distributed on the hash ring.
本申请通过将每个真实服务节点均虚拟出多个相等数量的虚拟服务节点,从而来增加hash环上的节点数量;将对应于不同真实服务节点所虚拟出的虚拟服务节点组成一个群组,从而使每个群组中的虚拟服务节点对应的真实服务节点都是间隔的;然后,将各群组首尾相连,得到包含全部虚拟服务节点的虚拟服务节点队列,并且按照队列将全部虚拟服务节点均匀分布在hash环上,从而不仅能够使全部的虚拟服务节点在hash环上是均匀分布的,而且还能够使每个真实服务节点所虚拟出的各虚拟服务节点在hash环上也是均匀分布的,从而使hash环上各服务节点的负载更为均衡。This application increases the number of nodes on the hash ring by virtualizing each real service node with an equal number of virtual service nodes; the virtual service nodes virtualized corresponding to different real service nodes form a group, In this way, the real service nodes corresponding to the virtual service nodes in each group are spaced; then, each group is connected end to end to obtain a virtual service node queue containing all virtual service nodes, and all virtual service nodes are sorted according to the queue Evenly distributed on the hash ring, so that not only all virtual service nodes can be evenly distributed on the hash ring, but also the virtual service nodes virtualized by each real service node are evenly distributed on the hash ring , So that the load of each service node on the hash ring is more balanced.
附图说明Description of the drawings
图1是目前使用的一致性hash环上服务节点分布和负载情况示意图。Figure 1 is a schematic diagram of the current distribution and load of service nodes on the consistent hash ring.
图2是本申请一实施例中hash环均衡负载方法的步骤示意图。FIG. 2 is a schematic diagram of the steps of a method for balancing a load of a hash ring in an embodiment of the present application.
图3是本申请一实施例中虚拟服务节点在hash环上的分布和负载情况示意图。Fig. 3 is a schematic diagram of the distribution and load of virtual service nodes on the hash ring in an embodiment of the present application.
图4是在图3所示的分布基础上进行扩容后的虚拟服务节点在hash环上的分布和负载情况示意图。FIG. 4 is a schematic diagram of the distribution and load of virtual service nodes on the hash ring after capacity expansion based on the distribution shown in FIG. 3.
图5是本申请一实施例中hash环均衡负载装置结构示意框图。FIG. 5 is a schematic block diagram of the structure of a hash ring load balancing device in an embodiment of the present application.
图6是本申请一实施例中电子设备的结构示意框图。FIG. 6 is a schematic block diagram of the structure of an electronic device in an embodiment of the present application.
图7是本申请一实施例中可读存储介质示意图。Fig. 7 is a schematic diagram of a readable storage medium in an embodiment of the present application.
具体实施方式Detailed ways
下面将结合本申请实施例中的附图,对本申请实施例中的技术方案进行清楚、完整地描述,显然,所描述的实施例仅是本申请一部分实施例,而不是全部的实施例。基于本申请中的实施例,本领域普通技术人员在没有做出创造性劳动前提下所获得的所有其他实施例,都属于本申请保护的范围。The following will clearly and completely describe the technical solutions in the embodiments of the present application with reference to the drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in this application, all other embodiments obtained by those of ordinary skill in the art without creative work shall fall within the protection scope of this application.
在本申请的描述中,需要理解的是,术语“第一”、“第二”仅用于描述目的,而不能理解为指示或暗示相对重要性或者隐含指明所指示的技术特征的数量。由此,限定有“第一”、“第二”的特征可以明示或者隐含地包括一个或者更多个该特征。在本申请的描述中,除非另有说明,“多个”的含义是两个或两个以上。In the description of this application, it should be understood that the terms “first” and “second” are only used for description purposes, and cannot be understood as indicating or implying relative importance or implicitly indicating the number of indicated technical features. Thus, the features defined with "first" and "second" may explicitly or implicitly include one or more of these features. In the description of this application, unless otherwise specified, "plurality" means two or more.
此外,所描述的特征、结构或特性可以以任何合适的方式结合在一个或更多实施例中。在下面的描述中,提供许多具体细节从而给出对本申请的实施例的充分理解。然而,本领域技术人员将意识到,可以实践本申请的技术方案而没有特定细节中的一个或更多,或者可以采用其它的方法、组元、装置、步骤等。在其它情况下,不详细示出或描述公知方法、装置、实现或者操作以避免模糊本申请的各方面。In addition, the described features, structures, or characteristics may be combined in one or more embodiments in any suitable manner. In the following description, many specific details are provided to give a sufficient understanding of the embodiments of the present application. However, those skilled in the art will realize that the technical solutions of the present application can be practiced without one or more of the specific details, or other methods, components, devices, steps, etc. can be used. In other cases, well-known methods, devices, implementations or operations are not shown or described in detail in order to avoid obscuring various aspects of the present application.
附图中所示的方框图仅仅是功能实体,不一定必须与物理上独立的实体相对应。即,可以采用软件形式来实现这些功能实体,或在一个或多个硬件模块或集成电路中实现这些功能实体,或在不同网络和/或处理器装置和/或微控制器装置中实现这些功能实体。The block diagrams shown in the drawings are merely functional entities, and do not necessarily correspond to physically independent entities. That is, these functional entities can be implemented in the form of software, or implemented in one or more hardware modules or integrated circuits, or implemented in different networks and/or processor devices and/or microcontroller devices. entity.
附图中所示的流程图仅是示例性说明,不是必须包括所有的内容和操作/步骤,也不是必须按所描述的顺序执行。例如,有的操作/步骤还可以分解,而有的操作/步骤可以合并或部分合并,因此实际执行的顺序有可能根据实际情况改变。The flowchart shown in the drawings is only an exemplary description, and does not necessarily include all contents and operations/steps, nor does it have to be performed in the described order. For example, some operations/steps can be decomposed, and some operations/steps can be combined or partially combined, so the actual execution order may be changed according to actual conditions.
本申请的技术方案可应用于区块链和/或大数据技术领域,以实现负载均衡。The technical solution of the present application can be applied to the field of blockchain and/or big data technology to achieve load balancing.
一致性Hash算法设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似。一致性Hash算法是一种特殊的Hash算法,由于其均衡性、持久性的映射特点,被广泛的应用于负载均衡领域,如nginx和memcached都采用了一致性Hash算法来作为集群负载均衡的方案。目前使用一致性hash算法作为负载均衡方法时,仍旧存在一些负载不 均衡的问题。主要原因是:一、服务节点hash值不能离散均匀的分布到hash环上;二、服务节点过少,源请求的hash值,分布在同一服务节点概率较大。The design goal of the consistent Hash algorithm is to solve the hotspot problem in the Internet, and its original intention is very similar to CARP. The consistent Hash algorithm is a special Hash algorithm. Due to its balanced and durable mapping characteristics, it is widely used in the load balancing field. For example, nginx and memcached both use a consistent Hash algorithm as a cluster load balancing solution. . At present, when the consistent hash algorithm is used as a load balancing method, there are still some load imbalance problems. The main reasons are: first, the hash value of the service node cannot be distributed evenly on the hash ring; second, there are too few service nodes, and the hash value of the source request is more likely to be distributed on the same service node.
参照图1,在图1所示的hash环中,有Node1、Node2、Node3共三个服务节点,在进行负载均衡时,可以使用FNV1_64_HASH算出服务节点hash值;a、b、c分别为三个请求对应的请求,也通过FNV1_64_HASH算出对应hash值,然后a顺时针找到最近的Node服务节点,由图1可以看出,由于Node3与Node2的区间更大,请求更大的概率会负载到Node1节点上,c、a两个请求都会负载在Node1节点上,b请求负载到Node2节点,Node3节点没有请求。可以发现,此种一致性hash的算法设计,会导致负载不均衡的情况。Referring to Figure 1, in the hash ring shown in Figure 1, there are three service nodes Node1, Node2, and Node3. When performing load balancing, you can use FNV1_64_HASH to calculate the service node hash value; a, b, and c are three respectively Request the corresponding request, also calculate the corresponding hash value through FNV1_64_HASH, and then a clockwise to find the nearest Node service node, as can be seen from Figure 1, because the interval between Node3 and Node2 is larger, the greater the probability of the request will be loaded to Node1 node Above, both requests c and a will be loaded on the Node1 node, the request b will be loaded on the Node2 node, and there is no request from the Node3 node. It can be found that this kind of consistent hash algorithm design will lead to unbalanced load.
参照图2,本申请一些实施例中提供一种hash环均衡负载方法,方法包括:2, in some embodiments of the present application, a method for balancing load of a hash ring is provided, and the method includes:
步骤S1,将每个真实服务节点虚拟出多个相等数量的虚拟服务节点,得到每个真实服务节点对应的虚拟服务节点;Step S1, virtualize each real service node into a plurality of equal number of virtual service nodes, and obtain a virtual service node corresponding to each real service node;
步骤S2,分别从多个真实服务节点对应的虚拟服务节点中选择一个虚拟服务节点组成一个虚拟服务节点群组,以得到多个虚拟服务节点群组,每个所述虚拟服务节点群组中的虚拟服务节点对应的真实服务节点的顺序相同;Step S2, selecting a virtual service node from the virtual service nodes corresponding to the multiple real service nodes to form a virtual service node group to obtain multiple virtual service node groups, each of the virtual service node groups The order of the real service nodes corresponding to the virtual service nodes is the same;
步骤S3,将各虚拟服务节点群组中的虚拟服务节点首尾相连,得到虚拟服务节点队列;Step S3, connecting the virtual service nodes in each virtual service node group end to end to obtain a virtual service node queue;
步骤S4,将所述虚拟服务节点队列均匀分布在hash环上。Step S4: Distribute the virtual service node queue evenly on the hash ring.
如上述步骤S1所述,将每个真实服务节点都虚拟出多个虚拟服务节点,每个真实服务节点虚拟出的虚拟服务节点的个数是相等的,从而得到每个真实服务节点对应的多个虚拟服务节点。As described in step S1 above, each real service node is virtualized into multiple virtual service nodes, and the number of virtual service nodes virtualized by each real service node is equal, so that the number of virtual service nodes corresponding to each real service node is obtained. A virtual service node.
例如,总共有Node1、Node2和Node3三个真实服务节点。每个真实服务节点均虚拟出三个虚拟服务节点。如,真实服务节点Node1虚拟出V1Node1、V2Node1和V3Node1三个虚拟服务节点。真实服务节点Node2虚拟出V1 Node2、V2 Node2和V3 Node2三个虚拟服务节点。真实服务节点Node3虚拟出V1 Node3、V2 Node3和V3 Node3三个虚拟服务节点。For example, there are a total of three real service nodes, Node1, Node2, and Node3. Each real service node virtualizes three virtual service nodes. For example, the real service node Node1 virtualizes three virtual service nodes, V1Node1, V2Node1, and V3Node1. The real service node Node2 virtualizes three virtual service nodes, V1, Node2, V2, Node2, and V3, Node2. The real service node Node3 virtualizes three virtual service nodes, V1 Node3, V2 Node3, and V3 Node3.
从而,将各虚拟服务节点代替真实服务节点分布在hash环上,以提高hash环上服务节点的数量。其中,每个真实服务节点可以理解成一个服务器。Therefore, each virtual service node is distributed on the hash ring instead of the real service node to increase the number of service nodes on the hash ring. Among them, each real service node can be understood as a server.
如上述步骤S2所述,分别从每个真实服务节点对应的虚拟服务节点中选择一个虚拟服务节点组成,例如:As described in step S2 above, a virtual service node is selected from the virtual service nodes corresponding to each real service node, for example:
从真实服务节点Node1所虚拟出的三个虚拟服务节点中选取虚拟服务节点V1 Node1,从真实服务节点Node2所虚拟出的三个虚拟服务节点中选取虚拟服务节点V1 Node2,从真实服务节点Node3所虚拟出的三个虚拟服务节点中选取虚拟服务节点V1 Node3,从而组成一个虚拟服务节点群组V1 Node1、V1 Node2和V1 Node3。Select the virtual service node V1 Node1 from the three virtual service nodes virtualized by the real service node Node1, select the virtual service node V1 Node2 from the three virtual service nodes virtualized by the real service node Node2, and select the virtual service node V1 Node2 from the real service node Node3. The virtual service node V1 Node3 is selected from the three virtual service nodes to form a virtual service node group V1 Node1, V1 Node2, and V1 Node3.
从真实服务节点Node1所虚拟出的三个虚拟服务节点中选取虚拟服务节点V2Node1,从真实服务节点Node2所虚拟出的三个虚拟服务节点中选取虚拟服务节点V2Node2,从真实服务节点Node3所虚拟出的三个虚拟服务节点中选取虚拟服务节点V2Node3,从而组成一个虚拟服务节点群组V2Node1、V2Node2和V2Node3。Select the virtual service node V2Node1 from the three virtual service nodes virtualized by the real service node Node1, select the virtual service node V2Node2 from the three virtual service nodes virtualized by the real service node Node2, and virtualize it from the real service node Node3 The virtual service node V2Node3 is selected from the three virtual service nodes in, so as to form a virtual service node group V2Node1, V2Node2, and V2Node3.
从真实服务节点Node1所虚拟出的三个虚拟服务节点中选取虚拟服务节点V3Node1,从真实服务节点Node2所虚拟出的三个虚拟服务节点中选取虚拟服务节点V3Node2,从真实服务节点Node3所虚拟出的三个虚拟服务节点中选取虚拟服务节点V3Node3,从而组成一个虚拟服务节点群组V3Node1、V3Node2和V3Node3。Select the virtual service node V3Node1 from the three virtual service nodes virtualized by the real service node Node1, select the virtual service node V3Node2 from the three virtual service nodes virtualized by the real service node Node2, and virtualize it from the real service node Node3 The virtual service node V3Node3 is selected from the three virtual service nodes in, so as to form a virtual service node group V3Node1, V3Node2, and V3Node3.
从而,得到三个虚拟服务节点群组。三个虚拟服务节点群组中的虚拟服务节点对应的真实服务节点的顺序相同,如上述三个虚拟服务节点群组中,每个虚拟服务节点群组都是对应真实服务节点Node1的虚拟服务节点排在最前,对应真实服务节点Node2的虚拟服务节点排在中间,对应真实服务节点Node3的虚拟服务节点排在最后。Thus, three virtual service node groups are obtained. The order of the virtual service nodes corresponding to the real service nodes in the three virtual service node groups is the same. For example, in the above three virtual service node groups, each virtual service node group is a virtual service node corresponding to the real service node Node1 At the top, the virtual service node corresponding to the real service node Node2 is ranked in the middle, and the virtual service node corresponding to the real service node Node3 is ranked last.
如上述步骤S3所述,将各虚拟服务节点群组中的虚拟服务节点首尾相连,从而得到虚拟服务节点队列,例如:As described in step S3 above, the virtual service nodes in each virtual service node group are connected end to end to obtain a virtual service node queue, for example:
将上述三个虚拟服务节点群组首尾相连后,得到虚拟服务节点队列V1 Node1、V1 Node2、V1 Node3、V2 Node1、V2 Node2、V2 Node3、V3 Node1、V3 Node2、V3 Node3。After connecting the above three virtual service node groups end to end, the virtual service node queues V1 Node1, V1 Node2, V1 Node3, V2 Node1, V2 Node2, V2 Node3, V3 Node1, V3 Node2, V3 Node3 are obtained.
如上述步骤S4所述,将经过以上步骤所得到的虚拟服务节点队列均匀地分布在hash环上。如图3所示,得到虚拟服务节点队列V1 Node1、V1 Node2、V1 Node3、V2 Node1、V2 Node2、V2 Node3、V3 Node1、V3 Node2、V3 Node3后,将对列中的各虚拟服务节点按照对列中的前后顺序均匀地分布在hash环上,进而请求a、请求b和请求c可以更为均衡地负载于每个真实服务节点上。As described in the above step S4, the virtual service node queues obtained through the above steps are evenly distributed on the hash ring. As shown in Figure 3, after the virtual service node queues V1, Node1, V1, Node2, V1, Node3, V2, Node1, V2, Node2, V2, Node3, V3, Node1, V3, Node2, V3, Node3 are obtained, the virtual service nodes in the queue are aligned according to the The sequence in the column is evenly distributed on the hash ring, so that request a, request b, and request c can be more evenly loaded on each real service node.
通过以上各步骤,可以确保hash环上相邻的两个虚拟服务节点对应的是不同的真实服务节点,能够使全部的虚拟服务节点在hash环上是均匀分布的,而且每个真实服务节点所虚拟出的各虚拟服务节点在hash环上也是均匀分布的。Through the above steps, it can be ensured that the two adjacent virtual service nodes on the hash ring correspond to different real service nodes, so that all virtual service nodes are evenly distributed on the hash ring, and each real service node is The virtual service nodes that are virtualized are also evenly distributed on the hash ring.
通过以上方案,不仅能使hash环上各服务节点的负载更为均衡,而且能够最大程度保留hash环的特性。Through the above scheme, not only can the load of each service node on the hash ring be more balanced, but also the characteristics of the hash ring can be retained to the greatest extent.
例如,新增一个节点Node4,加载在Node3和Node1的虚拟节点中间,这样即扩容节点,同时大部分的负载集群都没有变,只会影响Node3和Node1之间的部分数据负载在新加入的Node4虚拟节点上。For example, add a new node Node4 and load it between the virtual nodes of Node3 and Node1, so that the node will be expanded. At the same time, most of the load clusters will not change, which will only affect part of the data load between Node3 and Node1 in the newly added Node4. On the virtual node.
影响比例公式:(V_Node'-V_Node)/(2×V_Node);Influence ratio formula: (V_Node'-V_Node)/(2×V_Node);
其中,V_Node为原始虚拟节点;V_Node'为扩容后虚拟节点。Among them, V_Node is the original virtual node; V_Node' is the virtual node after capacity expansion.
如图4所示,按照以上方案,原始虚拟节点为九个,若增加一个真实服务节点Node4扩容后,也即增加了V1 Node4、V2 Node4和V3 Node4三个虚拟服务节点,虚拟服务节点一共有十二个,影响比例为:16.6667%。As shown in Figure 4, according to the above scheme, there are nine original virtual nodes. If a real service node Node4 is added to expand the capacity, three virtual service nodes V1 Node4, V2 Node4, and V3 Node4 will be added. The total number of virtual service nodes is Twelve, the impact ratio is 16.667%.
如果用传统hash取模来负载的话,当新增一个节点后,比如有四个节点,hash%4即为取模,如再加一个服务节点,则取模hash%5,这样只有前面的25%的hash模没有变,后面的都会发生变动。也即,影响比例为75%。If you use traditional hash modulus to load, when a new node is added, for example, there are four nodes, hash%4 is modulo. If another service node is added, hash%5 is modulated, so that only the first 25 The% hash modulus has not changed, and the rest will change. That is, the influence ratio is 75%.
所以相比传统的hash取模负载,对集群进行扩容,影响的负载节点变化比例小很多,最大程度保留了hash环特性。Therefore, compared with the traditional hash modulus load, the expansion of the cluster will have a much smaller proportion of the load node change, and the hash ring characteristics are retained to the greatest extent.
本申请一些实施例中,上述将分别从多个真实服务节点对应的虚拟服务节点中选择一个虚拟服务节点组成一个虚拟服务节点群组,以得到多个虚拟服务节点群组,每个虚拟服务节点群组中的虚拟服务节点对应的真实服务节点的顺序相同的步骤S2,包括:In some embodiments of the present application, the above will select a virtual service node from the virtual service nodes corresponding to multiple real service nodes to form a virtual service node group, so as to obtain multiple virtual service node groups, and each virtual service node Step S2 in which the order of the virtual service nodes corresponding to the real service nodes in the group is the same, includes:
步骤S21,对每个所述真实服务节点以及每个所述真实服务节点对应的虚拟服务节点分别进行编号,其中,每个所述真实服务节点对应的虚拟服务节点的编号顺序相同;Step S21, respectively numbering each of the real service nodes and the virtual service nodes corresponding to each of the real service nodes, wherein the numbering sequence of the virtual service nodes corresponding to each of the real service nodes is the same;
步骤S22,将编号相同的虚拟服务节点组成一个所述虚拟服务节点群组,并在每个所述虚拟服务节点群组中按照对应的真实服务节点的编号进行排列。In step S22, the virtual service nodes with the same number are formed into one virtual service node group, and the virtual service node groups are arranged according to the corresponding number of the real service node in each virtual service node group.
如上述步骤S21所述,可以先对每个真实服务节点进行编号,如三个真实服务节点分别编号为Node1、Node2和Node3,从而便于区分不同的真实服务节点。As described in step S21 above, each real service node may be numbered first, for example, the three real service nodes are respectively numbered Node1, Node2, and Node3, so as to facilitate the distinction between different real service nodes.
然后,对每个真实服务节点所虚拟出来的虚拟服务节点进行编号,每个所述真实服务节点对应的虚拟服务节点的编号顺序相同。Then, the virtual service nodes virtualized by each real service node are numbered, and the numbering sequence of the virtual service nodes corresponding to each real service node is the same.
例如,其中一个真实服务节点Node虚拟出的三个虚拟服务节点的编号分别为V1、V2和V3,则其余每个真实服务节点虚拟出的三个虚拟服务节点均分别编号为V1、V2和V3。For example, the three virtual service nodes virtualized by one real service node Node are numbered V1, V2, and V3, respectively, and the three virtual service nodes virtualized by each of the remaining real service nodes are numbered V1, V2, and V3, respectively .
如上述步骤S22所述,将编号相同的虚拟服务节点组成一个虚拟服务节点群组。As described in step S22 above, the virtual service nodes with the same number are formed into a virtual service node group.
例如,将真实服务节点Node1所虚拟出的编号为V1的虚拟服务节点记为V1Node1、真实服务节点Node2所虚拟出的编号为V1的虚拟服务节点记为V1 Node2以及真实服务节点Node3所虚拟出的编号为V1的虚拟服务节点记为V1 Node3组成一个虚拟服务节点群组, 记为第一虚拟服务节点群组。For example, the virtual service node numbered V1 virtualized by the real service node Node1 is recorded as V1Node1, and the virtual service node numbered V1 virtualized by the real service node Node2 is recorded as V1 Node2 and the virtual service node virtualized by the real service node Node3 The virtual service node numbered V1 is recorded as V1 and Node3 forms a virtual service node group, which is recorded as the first virtual service node group.
将真实服务节点Node1所虚拟出的编号为V2的虚拟服务节点记为V2Node1、真实服务节点Node2所虚拟出的编号为V2的虚拟服务节点记为V2 Node2以及真实服务节点Node3所虚拟出的编号为V2的虚拟服务节记为V2 Node3点组成一个虚拟服务节点群组,记为第二虚拟服务节点群组。The virtual service node numbered V2 virtualized by the real service node Node1 is recorded as V2Node1, the virtual service node numbered V2 virtualized by the real service node Node2 is recorded as V2 Node2, and the number virtualized by the real service node Node3 is The virtual service section of V2 is marked as V2 Node3 points to form a virtual service node group, which is recorded as the second virtual service node group.
将真实服务节点Node1所虚拟出的编号为V3的虚拟服务节点记为V3Node1、真实服务节点Node2所虚拟出的编号为V3的虚拟服务节点记为V3 Node2以及真实服务节点Node3所虚拟出的编号为V3的虚拟服务节点记为V3 Node3组成一个虚拟服务节点群组,记为第三虚拟服务节点群组。The virtual service node numbered V3 virtualized by the real service node Node1 is recorded as V3Node1, the virtual service node numbered V3 virtualized by the real service node Node2 is recorded as V3 Node2 and the virtual service node numbered by the real service node Node3 is The virtual service node of V3 is recorded as V3 Node3 forms a virtual service node group, which is recorded as the third virtual service node group.
从而得到三个虚拟服务节点群组,然后,在三个虚拟服务节点群组中均按照对应的真实服务节点的编号进行排列。In this way, three virtual service node groups are obtained, and then the three virtual service node groups are arranged according to the numbers of the corresponding real service nodes.
例如,在第一虚拟服务节点群组中三个虚拟服务节点V1 Node1、V1 Node2和V1 Node3分别按照对应的三个真实服务节点Node1、Node2和Node3的编号进行排列即为V1 Node1、V1 Node2和V1 Node3。For example, in the first virtual service node group, the three virtual service nodes V1, Node1, V1, Node2, and V1, Node3 are arranged according to the numbers of the corresponding three real service nodes Node1, Node2, and Node3, namely V1, Node1, V1, Node2, and V1 Node3.
同理,在第二虚拟服务节点群组中三个虚拟服务节点V2 Node1、V2Node2和V2 Node3分别按照对应的三个真实服务节点Node1、Node2和Node3的编号进行排列即为V2 Node1、V2 Node2和V2 Node3,在第三虚拟服务节点群组中三个虚拟服务节点V3 Node1、V3 Node2和V3Node3分别按照对应的三个真实服务节点Node1、Node2和Node3的编号进行排列即为V3 Node1、V3 Node2和V3Node3。Similarly, in the second virtual service node group, the three virtual service nodes V2, Node1, V2, Node2, and V2, Node3 are arranged according to the numbers of the corresponding three real service nodes, Node1, Node2, and Node3, namely V2, Node1, V2, Node2, and V2 Node3, in the third virtual service node group, the three virtual service nodes V3 Node1, V3 Node2, and V3 Node3 are arranged according to the numbers of the corresponding three real service nodes Node1, Node2, and Node3, namely V3, Node1, V3, Node2, and V3Node3.
通过以上方法对各虚拟服务节点群组中的虚拟服务节点进行排序,使虚拟服务节点与真实服务节点的对应关系更为直观,便于分析对比和负载验证。Through the above method, the virtual service nodes in each virtual service node group are sorted, so that the corresponding relationship between the virtual service node and the real service node is more intuitive, which is convenient for analysis and comparison and load verification.
本申请一些实施例中,上述将所述虚拟服务节点队列均匀分布在hash环上的步骤S4包括:In some embodiments of the present application, the above step S4 of evenly distributing the virtual service node queue on the hash ring includes:
步骤S41,将所述hash环上相邻的两个虚拟服务节点之间的弧长作为公差,并基于等差数列的通项公式计算等差通项的数值;Step S41, taking the arc length between two adjacent virtual service nodes on the hash ring as a tolerance, and calculating the value of the equal difference common term based on the common term formula of the arithmetic sequence;
步骤S42,基于所述虚拟服务节点队列中的虚拟服务节点与等差通项之间的对应关系,根据所述等差通项的数值将所述虚拟服务节点队列映射于所述hash环上。Step S42, based on the correspondence between the virtual service node in the virtual service node queue and the equal difference pass item, map the virtual service node queue on the hash ring according to the value of the equal difference pass item.
如上述步骤S41所述,将hash环上相邻的两个虚拟服务节点之间的弧长作为公差,并基于等差数列的通项公式计算等差通项的数值,例如:As described in step S41 above, the arc length between two adjacent virtual service nodes on the hash ring is used as the tolerance, and the value of the equal difference common term is calculated based on the common term formula of the arithmetic sequence, for example:
等差数列的通项公式为The general formula of the arithmetic sequence is
a n=a 1+(n-1)×d a n =a 1 +(n-1)×d
其中,a n代表通项。通项a n代表虚拟服务节点队列中第n个虚拟服务节点在hash环上的位置。a 1代表首项。n代表项数。令项数n等于虚拟服务节点队列中所有虚拟节点的数量。d代表公差。 Among them, a n represents the general term. A n general term representative of the position of the virtual queue service node n th hash virtual service node on the ring. a 1 represents the first item. n represents the number of items. Let the number of items n be equal to the number of all virtual nodes in the virtual service node queue. d stands for tolerance.
假设hash环上相邻的两个虚拟服务节点之间的弧长为l,则将公差d代替为弧长为l后,得到Assuming that the arc length between two adjacent virtual service nodes on the hash ring is l, the tolerance d is replaced by the arc length l to obtain
a n=a 1+(n-1)×l a n =a 1 +(n-1)×l
根据此公式,结合弧长为l和项数n,就可以计算出该等差通项中每项的值。According to this formula, combining the arc length of l and the number of terms n, the value of each term in the equidistant general term can be calculated.
如上述步骤S42所述,基于虚拟服务节点队列中的虚拟服务节点与等差通项之间的对应关系,根据等差通项的数值将虚拟服务节点队列映射于hash环上。其中,虚拟服务节点与等差通项之间的对应关系是指通项a n代表虚拟服务节点队列中第n个虚拟服务节点在hash环上的位置,从而构建的通项a n与虚拟服务节点的对应关系。 As described in the above step S42, based on the correspondence between the virtual service nodes in the virtual service node queue and the equal difference pass item, the virtual service node queue is mapped on the hash ring according to the value of the equal difference pass item. Wherein the correspondence relationship between the virtual service node through the arithmetic term refers to the position on behalf of a n term virtual service node queue n-th hash virtual service node on the ring to construct general term virtual service and a n Correspondence of nodes.
例如,a 1代表虚拟服务节点队列中第1个虚拟服务节点在hash环上的位置,a 1对应虚拟服务节点队列中第1个虚拟服务节点;a 2代表虚拟服务节点队列中第2个虚拟服务节点 在hash环上的位置,a 2对应虚拟服务节点队列中第2个虚拟服务节点,依次类推,通项a n对应第n个虚拟服务节点。 For example, a 1 represents the position of the first virtual service node in the virtual service node queue on the hash ring, a 1 corresponds to the first virtual service node in the virtual service node queue; a 2 represents the second virtual service node in the virtual service node queue hash location service node on the ring, the second virtual queue service node corresponding to a 2 virtual service node, and so on, through a n item corresponding to the n-th virtual service node.
从而,通过上述步骤S41计算出通项a n所包含的各项的数值后,按照上述对应关系,将虚拟服务节点队列中各虚拟服务节点映射在hash环上a n各项数值对应的位置上。 Whereby, after the above step S41 is calculated through the numerical entries contained a n, according to the above correspondence relationship, the virtual service node queue mapping each virtual service node on the ring a n hash values corresponding to the position .
例如,计算得a 1=1073741823,a 2=2147483647,a 3=3221225471,则将虚拟服务节点队列中的V1 Node1、V1 Node2、V1 Node3分别映射在hash环上的1073741823、2147483647和3221225471位置处。依次类推,将全部虚拟服务节点队列中的全部虚拟服务节点映射在hash环上,从而使全部虚拟服务节点均匀分布于hash环上。 For example, if a 1 =1073741823, a 2 =2147483647, and a 3 =3221225471 are calculated, then V1 Node1, V1 Node2, and V1 Node3 in the virtual service node queue are mapped to positions 1073741823, 2147483647, and 3221225471 on the hash ring, respectively. By analogy, all virtual service nodes in the queue of all virtual service nodes are mapped on the hash ring, so that all virtual service nodes are evenly distributed on the hash ring.
本申请一些实施例中,上述将所述hash环上相邻的两个虚拟服务节点之间的弧长作为公差,并基于等差数列的通项公式计算等差通项的数值的步骤S21之前,所述方法还包括:In some embodiments of the present application, the arc length between two adjacent virtual service nodes on the hash ring is used as the tolerance, and the value of the equal difference common term is calculated based on the common term formula of the arithmetic sequence before step S21 , The method further includes:
步骤S21,获取虚拟服务节点的hash值的取值范围,并根据所述取值范围获得所述hash环的周长;Step S21: Obtain the value range of the hash value of the virtual service node, and obtain the perimeter of the hash ring according to the value range;
步骤S22,根据所述hash环的周长和所述hash环上所述虚拟服务节点的数量计算出所述弧长。Step S22: Calculate the arc length according to the perimeter of the hash ring and the number of virtual service nodes on the hash ring.
如上述步骤S21和上述步骤S22所述,hash环的周长可根据虚拟服务节点的hash值的取值范围获得。具体获得hash环的周长的方法可以是将虚拟服务节点的hash值的范围中的最大的值作为hash环的周长,从而便于下一步根据hash环周长计算hash环的半径。As described in the above step S21 and the above step S22, the perimeter of the hash ring can be obtained according to the value range of the hash value of the virtual service node. The specific method for obtaining the perimeter of the hash ring may be to use the largest value in the range of the hash value of the virtual service node as the perimeter of the hash ring, so as to facilitate the calculation of the radius of the hash ring according to the perimeter of the hash ring in the next step.
例如,通过步骤S21获得hash环的周长c为2 32-1,hash环上的虚拟服务节点将hash环进行了n等分,从而可以算出hash环上任意相邻的两个虚拟服务节点之间的弧长l=(2 32-1)/n。 For example, through step S21, the perimeter c of the hash ring is 2 32 -1, and the virtual service node on the hash ring divides the hash ring into n equal parts, so that the value of any two adjacent virtual service nodes on the hash ring can be calculated. The arc length between l=(2 32 -1)/n.
在以上实施例中,公差d=l,进而得到d=(2 32-1)/n。 In the above embodiment, the tolerance d=1, and then d=(2 32 -1)/n is obtained.
在一些实施例中,也可以根据弧长公式l=a×r和弧度与角度的换算公式1°=π/180°得In some embodiments, it can also be obtained according to the arc length formula l=a×r and the conversion formula of radians and angles 1°=π/180°
l=m×π×r/180l=m×π×r/180
其中,α代表弧度值,r代表hash的半径,m代表圆心角度数;Among them, α represents the radian value, r represents the radius of the hash, and m represents the angle of the center of the circle;
令弧长等于公差值,即长l=d,得,d=m×π×r/180;Let the arc length be equal to the tolerance value, that is, the length l=d, so d=m×π×r/180;
由于hash环是被虚拟服务节点n等分的,所以每段圆弧所对应的圆心角m=360/n;Since the hash ring is equally divided by the virtual service node n, the central angle corresponding to each arc is m=360/n;
进而得到,
Figure PCTCN2020117659-appb-000001
And then get,
Figure PCTCN2020117659-appb-000001
由hash环周长c=2 32-1,结合圆的周长公式c=2×π×r,得到 From the perimeter of the hash ring c=2 32 -1, combined with the perimeter formula of the circle c=2×π×r, we get
2 32-1=2×π×r 2 32 -1=2×π×r
进而得到r=(2 32-1)/2π,代入上述公式同样可以得到 Then we get r=(2 32 -1)/2π, and substituting into the above formula can also get
d=(2 32-1)/n。 d=(2 32 -1)/n.
本申请一些实施例中,上述获取虚拟服务节点的hash值的取值范围,并根据所述取值范围获得所述hash环的周长的步骤S21包括:In some embodiments of the present application, the above step S21 of obtaining the value range of the hash value of the virtual service node, and obtaining the perimeter of the hash ring according to the value range includes:
步骤S211,将所述取值范围对应的最大值作为所述hash环的周长。Step S211: Use the maximum value corresponding to the value range as the circumference of the hash ring.
例如,若虚拟服务节点的hash的取值范围为0~2 32-1,则虚拟服务节点的hash的最大值为2 32-1,进而可将hash环的周长可记为c=232-1。将hash环的周长c代入以上实施例中,便可以用于计算等差数列的公差。 For example, if the value of the hash of the virtual service node ranges from 0 to 2 32 -1, the maximum value of the hash of the virtual service node is 2 32 -1, and the perimeter of the hash ring can be recorded as c=232- 1. Substituting the perimeter c of the hash ring into the above embodiment can be used to calculate the tolerance of the arithmetic sequence.
本申请一些实施例中,上述将hash环上相邻的两个虚拟服务节点之间的弧长作为公差,并基于等差数列的通项公式计算等差通项的数值的步骤S41之后,所述方法还包括:In some embodiments of the present application, the arc length between two adjacent virtual service nodes on the hash ring is used as the tolerance, and after the step S41 of calculating the value of the equal difference common term based on the common term formula of the arithmetic sequence, so The method also includes:
步骤S411,将所述等差通项的数值取为整数。In step S411, the value of the equal difference pass term is taken as an integer.
在步骤S411中,将等差通项计算出来的各项数值均取为整数,例如:In step S411, each value calculated by the equal difference general term is taken as an integer, for example:
a 1=1×(2 32-1)/4=1073741823,a 2=2×(2 32-1)/4=2147483647 a 1 =1×(2 32 -1)/4=1073741823, a 2 =2×(2 32 -1)/4=2147483647
a 3=3×(2 32-1)/4=3221225471,a 4=4×(2 32-1)/4=4294967295,便于统计和计算。 a 3 =3×(2 32 -1)/4=3221225471, a 4 =4×(2 32 -1)/4=4294967295, which is convenient for statistics and calculations.
本申请一些实施例中,虚拟服务节点队列中,虚拟服务节点的个数为偶数,从而使负载更为均衡。通过设置不同的真实服务节点和虚拟服务节点,对各真实服务节点的负载进行验证如下:In some embodiments of the present application, the number of virtual service nodes in the virtual service node queue is an even number, so that the load is more balanced. By setting up different real service nodes and virtual service nodes, the load of each real service node is verified as follows:
设置方案一:真实服务节点有四个,每个真实服务节点虚拟出两个虚拟服务节点。通过计算机验证,负载结果如下:Setting plan 1: There are four real service nodes, and each real service node virtualizes two virtual service nodes. Through computer verification, the load results are as follows:
真实服务节点Node1,负载比列:0.2527;The real service node Node1, load ratio: 0.2527;
真实服务节点Node2,负载比列:0.2486;The real service node Node2, load ratio: 0.2486;
真实服务节点Node3,负载比列:0.2441;The real service node Node3, load ratio: 0.2441;
真实服务节点Node4,负载比列:0.2546。The real service node Node4, load ratio: 0.2546.
设置方案二:真实服务节点有四个,每个真实服务节点虚拟出五个虚拟服务节点。通过计算机验证,负载结果如下:Setting plan 2: There are four real service nodes, and each real service node virtualizes five virtual service nodes. Through computer verification, the load results are as follows:
真实服务节点Node1,负载比列:0.2907;The real service node Node1, load ratio: 0.2907;
真实服务节点Node2,负载比列:0.3029;The real service node Node2, load ratio: 0.3029;
真实服务节点Node3,负载比列:0.2001;The real service node Node3, load ratio: 0.2001;
真实服务节点Node4,负载比列:0.2063。The real service node Node4, load ratio: 0.2063.
对比设置方案一与设置方案二的负载结果可知,虚拟节点为偶数时,各真实服务节点负载更均衡。Comparing the load results of setting plan 1 and setting plan 2, it can be seen that when the virtual node is an even number, the load of each real service node is more balanced.
设置方案三:真实服务节点有四个,每个真实服务节点虚拟出六个虚拟服务节点。通过计算机验证,负载结果如下:Setting scheme 3: There are four real service nodes, and each real service node virtualizes six virtual service nodes. Through computer verification, the load results are as follows:
真实服务节点Node1,负载比列:0.2506;The real service node Node1, load ratio: 0.2506;
真实服务节点Node2,负载比列:0.2505;The real service node Node2, load ratio: 0.2505;
真实服务节点Node3,负载比列:0.2508;The real service node Node3, load ratio: 0.2508;
真实服务节点Node4,负载比列:0.2481。The real service node Node4, load ratio: 0.2481.
对比设置方案二与设置方案三的负载结果可知,虚拟节点更多时,各真实服务节点负载更均衡。Comparing the load results of setting plan 2 and setting plan 3, it can be seen that when there are more virtual nodes, the load of each real service node is more balanced.
设置方案四:真实服务节点扩容到八个,每个真实服务节点虚拟出六个虚拟服务节点。通过计算机验证,负载结果如下:Setting plan 4: Expand the capacity of real service nodes to eight, and each real service node virtualizes six virtual service nodes. Through computer verification, the load results are as follows:
真实服务节点Node1,负载比列:0.1258;The real service node Node1, load ratio: 0.1258;
真实服务节点Node2,负载比列:0.1229;The real service node Node2, load ratio: 0.1229;
真实服务节点Node3,负载比列:0.1244;The real service node Node3, load ratio: 0.1244;
真实服务节点Node4,负载比列:0.1223;The real service node Node4, load ratio: 0.1223;
真实服务节点Node5,负载比列:0.1232;The real service node Node5, load ratio: 0.1232;
真实服务节点Node6,负载比列:0.1235;The real service node Node6, load ratio: 0.1235;
真实服务节点Node7,负载比列:0.1279;The real service node Node7, load ratio: 0.1279;
真实服务节点Node8,负载比列:0.13。The real service node Node8, load ratio: 0.13.
对比设置方案三与设置方案四的负载结果可知,按照本申请方案扩容节点后,负载依然比较均衡。Comparing the load results of setting plan 3 and setting plan 4, it can be seen that the load is still relatively balanced after the node is expanded according to the plan of this application.
通过以上各实施例,将虚拟服务节点均匀分布在hash环上后,就可以根据hash算法,计算请求信息的hash值,然后根据请求信息的hash值以及各虚拟服务节点的hash值将,请求信息分配给顺时针方向最接近的虚拟服务节点。Through the above embodiments, after the virtual service nodes are evenly distributed on the hash ring, the hash value of the requested information can be calculated according to the hash algorithm, and then the hash value of the requested information and the hash value of each virtual service node will be requested. Assigned to the closest virtual service node in the clockwise direction.
例如,hash函数选用质数(除非1),从而避免溢出的情况,导致信息丢失。兼顾到虚拟机的性能,虚拟机默认使用2<<5-1,提高计算hash的性能。综合以上,hash的算法为:For example, the hash function uses prime numbers (unless 1) to avoid overflow and cause loss of information. Taking into account the performance of the virtual machine, the virtual machine uses 2<<5-1 by default to improve the performance of computing hash. In summary, the hash algorithm is:
hash=31*hash+ascii;hash=31*hash+ascii;
其中,hash初始化值为0。ascii初始化值为32。ascii表示需要进行hash的字符串的ascii码,比如需要根据ip进行hash这里的ascii就是ip字符数组ascii码。例如,ip为114.114.114.114,计算出值为-1009865594,然后需要取绝对值为1009865594。Among them, the hash initialization value is 0. The initial value of ascii is 32. ascii represents the ascii code of the string that needs to be hashed. For example, it needs to be hashed according to the ip. The ascii here is the ascii code of the ip character array. For example, if ip is 114.114.114.114, the calculated value is -1009865594, and then the absolute value needs to be 1009865594.
本步骤中,通过hash算法对请求信息进行hash计算,从而提高离散概率。In this step, the request information is hashed through the hash algorithm to improve the discrete probability.
然后,根据计算出的请求信息的hash值,将请求映射到hash环上。例如,通过hash计算后得到的请求信息的hash值为1009865594。则将请求信息映射到hash上的1009865594位置处。Then, according to the calculated hash value of the request information, the request is mapped to the hash ring. For example, the hash value of the request information obtained after hash calculation is 1009865594. Then the request information is mapped to the 1009865594 location on the hash.
可见,请求信息的位置1009865594距离虚拟服务节点V1Node1的位置(a 1=1073741823)比较近,按照顺时针则该请求信息负载在虚拟服务节点V1Node1对应的真实服务节点Node1上。 It can be seen that the location 1009865594 of the requested information is relatively close to the location of the virtual service node V1Node1 (a 1 =1073741823). According to the clockwise direction, the request information is loaded on the real service node Node1 corresponding to the virtual service node V1Node1.
综合以上实施例,本申请通过将虚化后所得到的虚拟服务节点都均匀分布在hash环上,而且每个真实服务节点所虚化出的虚拟节点也均匀分布在hash环上,从而使每一个数据请求负载在各服务节点上的概率尽可能地均匀。通过本方案还能够最大程度地保留hash环的特性,某个服务器/服务节点失效,或者添加新的服务节点进行扩容后,影响比大大降低,使服务器集群更为稳定可靠。Based on the above embodiments, the present application distributes the virtual service nodes obtained after virtualization evenly on the hash ring, and the virtual nodes virtualized by each real service node are also evenly distributed on the hash ring, so that each The probability of a data request load on each service node is as uniform as possible. Through this solution, the characteristics of the hash ring can be retained to the greatest extent. After a certain server/service node fails, or a new service node is added for expansion, the impact ratio is greatly reduced, making the server cluster more stable and reliable.
参照图5,本申请一些实施例中,还提供一种hash环均衡负载装置,包括虚拟单元10、分组单元20、连接单元30和分布单元40。虚拟单元10用于将每个真实服务节点虚拟出多个相等数量的虚拟服务节点,进而得到每个真实服务节点对应的虚拟服务节点。分组单元20用于分别从多个真实服务节点对应的虚拟服务节点中选择一个虚拟服务节点组成一个虚拟服务节点群组,进而得到多个虚拟服务节点群组。其中,每个虚拟服务节点群组中的虚拟服务节点对应的真实服务节点的顺序相同。连接单元30用于将各虚拟服务节点群组中的虚拟服务节点首尾相连,从而得到虚拟服务节点队列。分布单元40用于将虚拟服务节点队列均匀分布在hash环上。Referring to FIG. 5, in some embodiments of the present application, a hash ring load balancing device is also provided, which includes a virtual unit 10, a grouping unit 20, a connection unit 30, and a distribution unit 40. The virtual unit 10 is used to virtualize each real service node into a plurality of virtual service nodes of equal number, and then obtain the virtual service node corresponding to each real service node. The grouping unit 20 is configured to select one virtual service node from the virtual service nodes corresponding to the multiple real service nodes to form a virtual service node group, thereby obtaining multiple virtual service node groups. Wherein, the order of the real service nodes corresponding to the virtual service nodes in each virtual service node group is the same. The connecting unit 30 is used to connect the virtual service nodes in each virtual service node group end to end to obtain a virtual service node queue. The distribution unit 40 is used to evenly distribute the virtual service node queue on the hash ring.
可选地,所述hash环均衡负载装置还包括计算请求信息的hash值计算单元和分配单元。所述请求信息的hash值计算单元用于计算请求信息的hash值。所述分配单元用于根据所述请求信息的hash值,将所述请求信息分配给顺时针方向最近的所述虚拟服务节点。Optionally, the hash ring load balancing device further includes a hash value calculation unit and a distribution unit for calculating request information. The hash value calculation unit of the request information is used to calculate the hash value of the request information. The allocation unit is configured to allocate the request information to the virtual service node closest in a clockwise direction according to the hash value of the request information.
可选地,所述分布单元被配置为:将所述hash环上相邻的两个虚拟服务节点之间的弧长作为公差,并基于等差数列的通项公式计算等差通项的数值;基于所述虚拟服务节点队列中的虚拟服务节点与等差通项之间的对应关系,根据所述等差通项的数值将所述虚拟服务节点队列映射于所述hash环上。Optionally, the distribution unit is configured to: use the arc length between two adjacent virtual service nodes on the hash ring as a tolerance, and calculate the value of the equal difference common term based on the general term formula of the arithmetic sequence Based on the correspondence between the virtual service node in the virtual service node queue and the equal difference pass item, the virtual service node queue is mapped to the hash ring according to the value of the equal difference pass item.
可选地,所述分布单元被配置为:获取虚拟服务节点的hash值的取值范围,并根据所述取值范围获得所述hash环的周长;根据所述hash环的周长和所述hash环上所述虚拟服务节点的数量计算出所述弧长。Optionally, the distribution unit is configured to: obtain the value range of the hash value of the virtual service node, and obtain the perimeter of the hash ring according to the value range; The arc length is calculated from the number of the virtual service nodes on the hash ring.
可选地,所述分布单元被配置为:将所述hash环上相邻的两个虚拟服务节点之间的弧长作为公差,并基于等差数列的通项公式计算等差通项的数值之后,将所述等差通项的数值取为整数。Optionally, the distribution unit is configured to: use the arc length between two adjacent virtual service nodes on the hash ring as a tolerance, and calculate the value of the equal difference common term based on the general term formula of the arithmetic sequence After that, the value of the equal difference general term is taken as an integer.
可选地,所述分组单元被配置为:对每个所述真实服务节点以及每个所述真实服务节点对应的虚拟服务节点分别进行编号,其中,每个所述真实服务节点对应的虚拟服务节点的编号顺序相同;将编号相同的虚拟服务节点组成一个虚拟服务节点群组,并在每个所述虚拟服务节点群组中按照对应的真实服务节点的编号进行排列。Optionally, the grouping unit is configured to: respectively number each real service node and the virtual service node corresponding to each real service node, wherein the virtual service corresponding to each real service node The numbering sequence of the nodes is the same; virtual service nodes with the same number are formed into a virtual service node group, and the virtual service node groups are arranged according to the corresponding real service node number.
上述装置中各个模块的功能和作用的实现过程具体详见上述hash环均衡负载方法中对应步骤的实现过程,在此不再赘述。For the implementation process of the functions and roles of each module in the above-mentioned device, refer to the implementation process of corresponding steps in the above-mentioned hash ring load balancing method for details, which will not be repeated here.
应当注意,尽管在上文详细描述中提及了用于若干模块或者单元,但是这种划分并非强制性的。实际上,根据本申请公开的实施方式,上文描述的两个或更多模块或者单元的 特征和功能可以在一个模块或者单元中具体化。反之,上文描述的一个模块或者单元的特征和功能可以进一步划分为由多个模块或者单元来具体化。It should be noted that although in the above detailed description it is mentioned for several modules or units, this division is not mandatory. In fact, according to the embodiments disclosed in the present application, the features and functions of two or more modules or units described above can be embodied in one module or unit. Conversely, the features and functions of a module or unit described above can be further divided into multiple modules or units to be embodied.
此外,尽管在附图中以特定顺序描述了本申请中方法的各个步骤,但是,这并非要求或者暗示必须按照该特定顺序来执行这些步骤,或是必须执行全部所示的步骤才能实现期望的结果。附加的或备选的,可以省略某些步骤,将多个步骤合并为一个步骤执行,以及/或者将一个步骤分解为多个步骤执行等。In addition, although the various steps of the method in the present application are described in a specific order in the drawings, this does not require or imply that these steps must be performed in the specific order, or that all the steps shown must be performed to achieve the desired result. Additionally or alternatively, some steps may be omitted, multiple steps may be combined into one step for execution, and/or one step may be decomposed into multiple steps for execution, etc.
通过以上的实施方式的描述,本领域的技术人员易于理解,这里描述的示例实施方式可以通过软件实现,也可以通过软件结合必要的硬件的方式来实现。因此,根据本申请实施方式的技术方案可以以软件产品的形式体现出来,该软件产品可以存储在一个非易失性存储介质(可以是CD-ROM,U盘,移动硬盘等)中或网络上,包括若干指令以使得一台计算设备(可以是个人计算机、服务器、移动终端、或者网络设备等)执行根据本申请实施方式的方法。Through the description of the above embodiments, those skilled in the art can easily understand that the example embodiments described here can be implemented by software, or can be implemented by combining software with necessary hardware. Therefore, the technical solution according to the embodiments of the present application can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (which can be a CD-ROM, U disk, mobile hard disk, etc.) or on the network , Including several instructions to make a computing device (which can be a personal computer, a server, a mobile terminal, or a network device, etc.) execute the method according to the embodiment of the present application.
参照图6,本申请一些实施例中还提供一种用于实现上述hash环均衡负载方法的电子设备。图6显示的电子设备500仅仅是一个示例,不应对本申请实施例的功能和使用范围带来任何限制。Referring to FIG. 6, some embodiments of the present application also provide an electronic device for implementing the above method for balancing load of the hash ring. The electronic device 500 shown in FIG. 6 is only an example, and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
电子设备500以通用计算设备的形式表现。电子设备500的组件可以包括但不限于:至少一个处理单元510、至少一个存储单元520、连接不同系统组件(包括存储单元520和处理单元510)的总线530。The electronic device 500 is represented in the form of a general-purpose computing device. The components of the electronic device 500 may include but are not limited to: at least one processing unit 510, at least one storage unit 520, and a bus 530 connecting different system components (including the storage unit 520 and the processing unit 510).
其中,所述存储单元存储有程序代码,所述程序代码可以被所述处理单元510执行,使得所述处理单元510执行本说明书上述“示例性方法”部分中描述的根据本申请各种示例性实施方式的步骤。例如,所述处理单元510可以执行如图2中所示的步骤S1至步骤S4。Wherein, the storage unit stores program code, and the program code can be executed by the processing unit 510, so that the processing unit 510 executes the various exemplary methods described in the “Exemplary Method” section of this specification. Steps of implementation. For example, the processing unit 510 may execute steps S1 to S4 as shown in FIG. 2.
存储单元520可以包括易失性存储单元形式的可读介质,例如随机存取存储单元(RAM)5201和/或高速缓存存储单元5202,还可以进一步包括只读存储单元(ROM)5203。The storage unit 520 may include a readable medium in the form of a volatile storage unit, such as a random access storage unit (RAM) 5201 and/or a cache storage unit 5202, and may further include a read-only storage unit (ROM) 5203.
存储单元520还可以包括具有一组(至少一个)程序模块5205的程序/实用工具5204,这样的程序模块5205包括但不限于:操作系统、一个或者多个应用程序、其它程序模块以及程序数据,这些示例中的每一个或某种组合中可能包括网络环境的实现。The storage unit 520 may also include a program/utility tool 5204 having a set (at least one) program module 5205. Such program module 5205 includes but is not limited to: an operating system, one or more application programs, other program modules, and program data, Each of these examples or some combination may include the implementation of a network environment.
总线530可以为表示几类总线结构中的一种或多种,包括存储单元总线或者存储单元控制器、外围总线、图形加速端口、处理单元或者使用多种总线结构中的任意总线结构的局域总线。The bus 530 may represent one or more of several types of bus structures, including a storage unit bus or a storage unit controller, a peripheral bus, a graphics acceleration port, a processing unit, or a local area using any bus structure among multiple bus structures. bus.
电子设备500也可以与一个或多个外部设备1000(例如键盘、指向设备、蓝牙设备等)通信,还可与一个或者多个使得用户能与该电子设备800交互的设备通信,和/或与使得该电子设备500能与一个或多个其它计算设备进行通信的任何设备(例如路由器、调制解调器等等)通信。这种通信可以通过输入/输出(I/O)接口540进行。并且,电子设备500还可以通过网络适配器560与一个或者多个网络(例如局域网(LAN),广域网(WAN)和/或公共网络,例如因特网)通信。如图所示,网络适配器560通过总线530与电子设备500的其它模块通信。应当明白,尽管图中未示出,可以结合电子设备500使用其它硬件和/或软件模块,包括但不限于:微代码、设备驱动器、冗余处理单元、外部磁盘驱动阵列、RAID系统、磁带驱动器以及数据备份存储系统等。The electronic device 500 may also communicate with one or more external devices 1000 (such as keyboards, pointing devices, Bluetooth devices, etc.), and may also communicate with one or more devices that enable a user to interact with the electronic device 800, and/or communicate with Any device (such as a router, modem, etc.) that enables the electronic device 500 to communicate with one or more other computing devices. Such communication may be performed through an input/output (I/O) interface 540. In addition, the electronic device 500 may also communicate with one or more networks (for example, a local area network (LAN), a wide area network (WAN), and/or a public network, such as the Internet) through the network adapter 560. As shown in the figure, the network adapter 560 communicates with other modules of the electronic device 500 through the bus 530. It should be understood that although not shown in the figure, other hardware and/or software modules can be used in conjunction with the electronic device 500, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives And data backup storage system, etc.
通过以上的实施方式的描述,本领域的技术人员易于理解,这里描述的示例实施方式可以通过软件实现,也可以通过软件结合必要的硬件的方式来实现。因此,根据本申请实施方式的技术方案可以以软件产品的形式体现出来,该软件产品可以存储在一个非易失性存储介质(可以是CD-ROM,U盘,移动硬盘等)中或网络上,包括若干指令以使得一台计算设备(可以是个人计算机、服务器、终端装置、或者网络设备等)执行根据本申请实施方式的方法。Through the description of the above embodiments, those skilled in the art can easily understand that the example embodiments described here can be implemented by software, or can be implemented by combining software with necessary hardware. Therefore, the technical solution according to the embodiments of the present application can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (which can be a CD-ROM, U disk, mobile hard disk, etc.) or on the network , Including several instructions to make a computing device (which can be a personal computer, a server, a terminal device, or a network device, etc.) execute the method according to the embodiment of the present application.
本申请实施例还提供了一种可读存储介质如计算机可读存储介质,其上存储有计算机程序,该计算机程序被处理器执行时实现上述hash环均衡负载方法的部分或全部步骤。可选的,本申请涉及的介质如计算机可读存储介质等可以是非易失性的,也可以是易失性的。例如,参照图7,在本申请的示例性实施例中,还提供了一种计算机可读存储介质,其上存储有能够实现本说明书上述hash环均衡负载方法的程序产品。在一些可能的实施方式中,本申请的各个方面还可以实现为一种程序产品的形式,其包括程序代码,当所述程序产品在终端设备上运行时,所述程序代码用于使所述电子设备执行本说明书上述“示例性方法”部分中描述的根据本申请各种示例性实施方式的步骤。The embodiment of the present application also provides a readable storage medium, such as a computer readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, some or all of the steps of the above hash ring load balancing method are realized. Optionally, the media involved in this application, such as computer-readable storage media, may be non-volatile or volatile. For example, referring to FIG. 7, in an exemplary embodiment of the present application, a computer-readable storage medium is also provided, on which a program product capable of implementing the above-mentioned hash ring load balancing method of this specification is stored. In some possible implementation manners, various aspects of the present application can also be implemented in the form of a program product, which includes program code. When the program product runs on a terminal device, the program code is used to make the The electronic device executes the steps described in the above-mentioned "Exemplary Method" section of this specification according to various exemplary embodiments of the present application.
图7是本申请一示例性实施例示出的一种用于实现上述hash环均衡负载方法的计算机可读存储介质。图7描述了根据本申请的实施方式的用于实现上述hash环均衡负载方法的程序产品600,其可以采用便携式紧凑盘只读存储器(CD-ROM)并包括程序代码,并可以在电子设备,例如个人电脑上运行。然而,本申请的程序产品不限于此,在本文件中,可读存储介质可以是任何包含或存储程序的有形介质,该程序可以被指令执行系统、装置或者器件使用或者与其结合使用。Fig. 7 is a computer-readable storage medium for realizing the above-mentioned hash ring load balancing method according to an exemplary embodiment of the present application. FIG. 7 depicts a program product 600 for implementing the above-mentioned hash ring load balancing method according to an embodiment of the present application, which can adopt a portable compact disk read-only memory (CD-ROM) and include program code, and can be installed in an electronic device, For example, running on a personal computer. However, the program product of this application is not limited to this. In this document, the readable storage medium can be any tangible medium that contains or stores a program, and the program can be used by or in combination with an instruction execution system, device, or device.
所述程序产品可以采用一个或多个可读介质的任意组合。可读介质可以是可读信号介质或者可读存储介质。可读存储介质例如可以为但不限于电、磁、光、电磁、红外线、或半导体的系统、装置或器件,或者任意以上的组合。可读存储介质的更具体的例子(非穷举的列表)包括:具有一个或多个导线的电连接、便携式盘、硬盘、随机存取存储器(RAM)、只读存储器(ROM)、可擦式可编程只读存储器(EPROM或闪存)、光纤、便携式紧凑盘只读存储器(CD-ROM)、光存储器件、磁存储器件、或者上述的任意合适的组合。The program product can use any combination of one or more readable media. The readable medium may be a readable signal medium or a readable storage medium. The readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or device, or a combination of any of the above. More specific examples (non-exhaustive list) of readable storage media include: electrical connections with one or more wires, portable disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable Type programmable read only memory (EPROM or flash memory), optical fiber, portable compact disk read only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination of the above.
计算机可读信号介质可以包括在基带中或者作为载波一部分传播的数据信号,其中承载了可读程序代码。这种传播的数据信号可以采用多种形式,包括但不限于电磁信号、光信号或上述的任意合适的组合。可读信号介质还可以是可读存储介质以外的任何可读介质,该可读介质可以发送、传播或者传输用于由指令执行系统、装置或者器件使用或者与其结合使用的程序。The computer-readable signal medium may include a data signal propagated in baseband or as a part of a carrier wave, and readable program code is carried therein. This propagated data signal can take many forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the foregoing. The readable signal medium may also be any readable medium other than a readable storage medium, and the readable medium may send, propagate, or transmit a program for use by or in combination with the instruction execution system, apparatus, or device.
可读介质上包含的程序代码可以用任何适当的介质传输,包括但不限于无线、有线、光缆、RF等等,或者上述的任意合适的组合。The program code contained on the readable medium can be transmitted by any suitable medium, including but not limited to wireless, wired, optical cable, RF, etc., or any suitable combination of the foregoing.
可以以一种或多种程序设计语言的任意组合来编写用于执行本申请操作的程序代码,所述程序设计语言包括面向对象的程序设计语言—诸如Java、C++等,还包括常规的过程式程序设计语言—诸如“C”语言或类似的程序设计语言。程序代码可以完全地在用户计算设备上执行、部分地在用户设备上执行、作为一个独立的软件包执行、部分在用户计算设备上部分在远程计算设备上执行、或者完全在远程计算设备或服务器上执行。在涉及远程计算设备的情形中,远程计算设备可以通过任意种类的网络,包括局域网(LAN)或广域网(WAN),连接到用户计算设备,或者,可以连接到外部计算设备(例如利用因特网服务提供商来通过因特网连接)。The program code used to perform the operations of the present application can be written in any combination of one or more programming languages. The programming languages include object-oriented programming languages—such as Java, C++, etc., as well as conventional procedural programming languages. Programming language-such as "C" language or similar programming language. The program code can be executed entirely on the user's computing device, partly on the user's device, executed as an independent software package, partly on the user's computing device and partly executed on the remote computing device, or entirely on the remote computing device or server Executed on. In the case of a remote computing device, the remote computing device can be connected to a user computing device through any kind of network, including a local area network (LAN) or a wide area network (WAN), or it can be connected to an external computing device (for example, using Internet service providers). Business to connect via the Internet).
此外,上述附图仅是根据本申请示例性实施例的方法所包括的处理的示意性说明,而不是限制目的。易于理解,上述附图所示的处理并不表明或限制这些处理的时间顺序。另外,也易于理解,这些处理可以是在多个模块中同步或异步执行的。In addition, the above-mentioned drawings are merely schematic illustrations of the processing included in the method according to the exemplary embodiments of the present application, and are not intended for limitation. It is easy to understand that the processing shown in the above drawings does not indicate or limit the time sequence of these processings. In addition, it is easy to understand that these processes can be executed synchronously or asynchronously in multiple modules.
虽然已参照几个典型实施方式描述了本申请,但应当理解,所用的术语是说明和示例性、而非限制性的术语。由于本申请能够以多种形式具体实施而不脱离本申请的精神或实质,所以应当理解,上述实施方式不限于任何前述的细节,而应在随附权利要求所限定的精神和范围内广泛地解释,因此落入权利要求或其等效范围内的全部变化和改型都应为随附权利要求所涵盖。Although this application has been described with reference to several exemplary embodiments, it should be understood that the terms used are illustrative and exemplary rather than restrictive. Since this application can be implemented in various forms without departing from the spirit or essence of the application, it should be understood that the above-mentioned implementations are not limited to any of the foregoing details, but should be broadly within the spirit and scope defined by the appended claims. Interpretation, therefore, all changes and modifications falling within the scope of the claims or their equivalents shall be covered by the appended claims.

Claims (20)

  1. 一种hash环均衡负载方法,其中,包括:A method for load balancing with a hash ring, which includes:
    将每个真实服务节点虚拟出多个相等数量的虚拟服务节点,得到每个真实服务节点对应的虚拟服务节点;Virtualize each real service node into a number of equal number of virtual service nodes, and obtain the virtual service node corresponding to each real service node;
    分别从多个真实服务节点对应的多个虚拟服务节点中选择一个虚拟服务节点组成一个虚拟服务节点群组,以得到多个虚拟服务节点群组,每个所述虚拟服务节点群组中的虚拟服务节点对应的真实服务节点的顺序相同;A virtual service node is selected from multiple virtual service nodes corresponding to multiple real service nodes to form a virtual service node group to obtain multiple virtual service node groups, and the virtual service node group in each virtual service node group The order of the real service nodes corresponding to the service nodes is the same;
    将各虚拟服务节点群组中的虚拟服务节点首尾相连,得到虚拟服务节点队列;Connect the virtual service nodes in each virtual service node group end to end to obtain a virtual service node queue;
    将所述虚拟服务节点队列均匀分布在hash环上。The virtual service node queue is evenly distributed on the hash ring.
  2. 根据权利要求1所述的hash环均衡负载方法,其中,将所述虚拟服务节点队列均匀分布在hash环上,包括:The hash ring load balancing method according to claim 1, wherein evenly distributing the virtual service node queue on the hash ring comprises:
    将所述hash环上相邻的两个虚拟服务节点之间的弧长作为公差,并基于等差数列的通项公式计算等差通项的数值;Taking the arc length between two adjacent virtual service nodes on the hash ring as a tolerance, and calculating the value of the equal difference common term based on the common term formula of the arithmetic sequence;
    基于所述虚拟服务节点队列中的虚拟服务节点与等差通项之间的对应关系,根据所述等差通项的数值将所述虚拟服务节点队列映射于所述hash环上。Based on the corresponding relationship between the virtual service node in the virtual service node queue and the equal difference pass item, the virtual service node queue is mapped on the hash ring according to the value of the equal difference pass item.
  3. 根据权利要求2所述的hash环均衡负载方法,其中,将所述hash环上相邻的两个虚拟服务节点之间的弧长作为公差,并基于等差数列的通项公式计算等差通项的数值之前,所述方法还包括:The hash ring load balancing method according to claim 2, wherein the arc length between two adjacent virtual service nodes on the hash ring is used as a tolerance, and the equal difference is calculated based on the general term formula of the arithmetic sequence Before the value of the item, the method also includes:
    获取所述虚拟服务节点的hash值的取值范围,并根据所述取值范围获得所述hash环的周长;Obtaining a value range of the hash value of the virtual service node, and obtaining the perimeter of the hash ring according to the value range;
    根据所述hash环的周长和所述hash环上所述虚拟服务节点的数量计算出所述弧长。The arc length is calculated according to the perimeter of the hash ring and the number of virtual service nodes on the hash ring.
  4. 根据权利要求2所述的hash环均衡负载方法,其中,将所述hash环上相邻的两个虚拟服务节点之间的弧长作为公差,并基于等差数列的通项公式计算等差通项的数值之后,所述方法还包括:The hash ring load balancing method according to claim 2, wherein the arc length between two adjacent virtual service nodes on the hash ring is used as a tolerance, and the equal difference is calculated based on the general term formula of the arithmetic sequence After the value of the item, the method further includes:
    将所述等差通项的数值取为整数。The numerical value of the equal difference general term is taken as an integer.
  5. 根据权利要求1所述的hash环均衡负载方法,其中,分别从多个真实服务节点对应的虚拟服务节点中选择一个虚拟服务节点组成一个虚拟服务节点群组,以得到多个虚拟服务节点群组,每个所述虚拟服务节点群组中的虚拟服务节点对应的真实服务节点的顺序相同,包括:The hash ring load balancing method according to claim 1, wherein a virtual service node is selected from virtual service nodes corresponding to multiple real service nodes to form a virtual service node group to obtain multiple virtual service node groups , The order of the real service nodes corresponding to the virtual service nodes in each virtual service node group is the same, including:
    对每个所述真实服务节点以及每个所述真实服务节点对应的虚拟服务节点分别进行编号,其中,每个所述真实服务节点对应的虚拟服务节点的编号顺序相同;Respectively number each of the real service nodes and the virtual service nodes corresponding to each of the real service nodes, wherein the numbering sequence of the virtual service nodes corresponding to each of the real service nodes is the same;
    将编号相同的虚拟服务节点组成一个所述虚拟服务节点群组,并在每个所述虚拟服务节点群组中按照对应的真实服务节点的编号进行排列。The virtual service nodes with the same number are formed into one virtual service node group, and the virtual service node groups are arranged according to the number of the corresponding real service node.
  6. 根据权利要求1所述的hash环均衡负载方法,其中,将所述虚拟服务节点队列均匀分布在hash环上之后,所述方法还包括:The hash ring load balancing method according to claim 1, wherein after the virtual service node queue is evenly distributed on the hash ring, the method further comprises:
    计算请求信息的hash值;Calculate the hash value of the requested information;
    根据所述请求信息的hash值,将所述请求信息分配给顺时针方向最近的所述虚拟服务节点。According to the hash value of the request information, the request information is allocated to the virtual service node closest in a clockwise direction.
  7. 根据权利要求1所述的hash环均衡负载方法,其中,The hash ring load balancing method according to claim 1, wherein:
    所述虚拟服务节点队列中的所述虚拟服务节点的个数为偶数。The number of the virtual service nodes in the virtual service node queue is an even number.
  8. 一种hash环均衡负载装置,其中,包括:A hash ring load balancing device, which includes:
    虚拟单元,用于将每个真实服务节点虚拟出多个相等数量的虚拟服务节点,得到每个真实服务节点对应的虚拟服务节点;The virtual unit is used to virtualize each real service node into a number of equal number of virtual service nodes to obtain the virtual service node corresponding to each real service node;
    分组单元,用于分别从多个所述真实服务节点对应的所述虚拟服务节点中选择一个所 述虚拟服务节点组成一个虚拟服务节点群组,以得到多个所述虚拟服务节点群组,每个所述虚拟服务节点群组中的虚拟服务节点对应的真实服务节点的顺序相同;The grouping unit is configured to select one of the virtual service nodes corresponding to the multiple real service nodes to form a virtual service node group to obtain multiple virtual service node groups, each The order of the real service nodes corresponding to the virtual service nodes in the virtual service node groups is the same;
    连接单元,用于将各虚拟服务节点群组中的虚拟服务节点首尾相连,得到虚拟服务节点队列;The connecting unit is used to connect the virtual service nodes in each virtual service node group end to end to obtain a virtual service node queue;
    分布单元,用于将所述虚拟服务节点队列均匀分布在hash环上。The distribution unit is configured to evenly distribute the virtual service node queue on the hash ring.
  9. 一种电子设备,包括存储器和处理器,所述存储器中存储有计算机程序,其中,所述处理器执行所述计算机程序时实现以下步骤:An electronic device includes a memory and a processor, and a computer program is stored in the memory, wherein the processor implements the following steps when executing the computer program:
    将每个真实服务节点虚拟出多个相等数量的虚拟服务节点,得到每个真实服务节点对应的虚拟服务节点;Virtualize each real service node into a number of equal number of virtual service nodes, and obtain the virtual service node corresponding to each real service node;
    分别从多个真实服务节点对应的多个虚拟服务节点中选择一个虚拟服务节点组成一个虚拟服务节点群组,以得到多个虚拟服务节点群组,每个所述虚拟服务节点群组中的虚拟服务节点对应的真实服务节点的顺序相同;A virtual service node is selected from multiple virtual service nodes corresponding to multiple real service nodes to form a virtual service node group to obtain multiple virtual service node groups, and the virtual service node group in each virtual service node group The order of the real service nodes corresponding to the service nodes is the same;
    将各虚拟服务节点群组中的虚拟服务节点首尾相连,得到虚拟服务节点队列;Connect the virtual service nodes in each virtual service node group end to end to obtain a virtual service node queue;
    将所述虚拟服务节点队列均匀分布在hash环上。The virtual service node queue is evenly distributed on the hash ring.
  10. 根据权利要求9所述的电子设备,其中,将所述虚拟服务节点队列均匀分布在hash环上时,具体实现以下步骤:The electronic device according to claim 9, wherein when the virtual service node queue is evenly distributed on the hash ring, the following steps are specifically implemented:
    将所述hash环上相邻的两个虚拟服务节点之间的弧长作为公差,并基于等差数列的通项公式计算等差通项的数值;Taking the arc length between two adjacent virtual service nodes on the hash ring as a tolerance, and calculating the value of the equal difference common term based on the common term formula of the arithmetic sequence;
    基于所述虚拟服务节点队列中的虚拟服务节点与等差通项之间的对应关系,根据所述等差通项的数值将所述虚拟服务节点队列映射于所述hash环上。Based on the corresponding relationship between the virtual service node in the virtual service node queue and the equal difference pass item, the virtual service node queue is mapped on the hash ring according to the value of the equal difference pass item.
  11. 根据权利要求10所述的电子设备,其中,将所述hash环上相邻的两个虚拟服务节点之间的弧长作为公差,并基于等差数列的通项公式计算等差通项的数值之前,所述处理器还用于执行所述计算机程序以实现以下步骤:The electronic device according to claim 10, wherein the arc length between two adjacent virtual service nodes on the hash ring is used as a tolerance, and the value of the equal difference common term is calculated based on the general term formula of the arithmetic sequence Previously, the processor was also used to execute the computer program to implement the following steps:
    获取所述虚拟服务节点的hash值的取值范围,并根据所述取值范围获得所述hash环的周长;Obtaining a value range of the hash value of the virtual service node, and obtaining the perimeter of the hash ring according to the value range;
    根据所述hash环的周长和所述hash环上所述虚拟服务节点的数量计算出所述弧长。The arc length is calculated according to the perimeter of the hash ring and the number of virtual service nodes on the hash ring.
  12. 根据权利要求9所述的电子设备,其中,分别从多个真实服务节点对应的虚拟服务节点中选择一个虚拟服务节点组成一个虚拟服务节点群组,以得到多个虚拟服务节点群组,每个所述虚拟服务节点群组中的虚拟服务节点对应的真实服务节点的顺序相同时,具体实现以下步骤:The electronic device according to claim 9, wherein one virtual service node is selected from the virtual service nodes corresponding to the multiple real service nodes to form a virtual service node group to obtain multiple virtual service node groups, each When the order of the real service nodes corresponding to the virtual service nodes in the virtual service node group is the same, the following steps are specifically implemented:
    对每个所述真实服务节点以及每个所述真实服务节点对应的虚拟服务节点分别进行编号,其中,每个所述真实服务节点对应的虚拟服务节点的编号顺序相同;Respectively number each of the real service nodes and the virtual service nodes corresponding to each of the real service nodes, wherein the numbering sequence of the virtual service nodes corresponding to each of the real service nodes is the same;
    将编号相同的虚拟服务节点组成一个所述虚拟服务节点群组,并在每个所述虚拟服务节点群组中按照对应的真实服务节点的编号进行排列。The virtual service nodes with the same number are formed into one virtual service node group, and the virtual service node groups are arranged according to the number of the corresponding real service node.
  13. 根据权利要求9所述的电子设备,其中,将所述虚拟服务节点队列均匀分布在hash环上之后,所述处理器还用于执行所述计算机程序以实现以下步骤:The electronic device according to claim 9, wherein after the virtual service node queue is evenly distributed on the hash ring, the processor is further configured to execute the computer program to implement the following steps:
    计算请求信息的hash值;Calculate the hash value of the requested information;
    根据所述请求信息的hash值,将所述请求信息分配给顺时针方向最近的所述虚拟服务节点。According to the hash value of the request information, the request information is allocated to the virtual service node closest in a clockwise direction.
  14. 根据权利要求9所述的电子设备,其中,The electronic device according to claim 9, wherein:
    所述虚拟服务节点队列中的所述虚拟服务节点的个数为偶数。The number of the virtual service nodes in the virtual service node queue is an even number.
  15. 一种可读存储介质,其上存储有计算机程序,其中,所述计算机程序被处理器执行时实现以下步骤:A readable storage medium having a computer program stored thereon, wherein the computer program implements the following steps when being executed by a processor:
    将每个真实服务节点虚拟出多个相等数量的虚拟服务节点,得到每个真实服务节点对 应的虚拟服务节点;Virtualize each real service node into multiple equal number of virtual service nodes, and obtain the virtual service node corresponding to each real service node;
    分别从多个真实服务节点对应的多个虚拟服务节点中选择一个虚拟服务节点组成一个虚拟服务节点群组,以得到多个虚拟服务节点群组,每个所述虚拟服务节点群组中的虚拟服务节点对应的真实服务节点的顺序相同;A virtual service node is selected from multiple virtual service nodes corresponding to multiple real service nodes to form a virtual service node group to obtain multiple virtual service node groups, and the virtual service node group in each virtual service node group The order of the real service nodes corresponding to the service nodes is the same;
    将各虚拟服务节点群组中的虚拟服务节点首尾相连,得到虚拟服务节点队列;Connect the virtual service nodes in each virtual service node group end to end to obtain a virtual service node queue;
    将所述虚拟服务节点队列均匀分布在hash环上。The virtual service node queue is evenly distributed on the hash ring.
  16. 根据权利要求15所述的可读存储介质,其中,将所述虚拟服务节点队列均匀分布在hash环上时,具体实现:The readable storage medium according to claim 15, wherein when the virtual service node queue is evenly distributed on a hash ring, the specific implementation is as follows:
    将所述hash环上相邻的两个虚拟服务节点之间的弧长作为公差,并基于等差数列的通项公式计算等差通项的数值;Taking the arc length between two adjacent virtual service nodes on the hash ring as a tolerance, and calculating the value of the equal difference common term based on the common term formula of the arithmetic sequence;
    基于所述虚拟服务节点队列中的虚拟服务节点与等差通项之间的对应关系,根据所述等差通项的数值将所述虚拟服务节点队列映射于所述hash环上。Based on the corresponding relationship between the virtual service node in the virtual service node queue and the equal difference pass item, the virtual service node queue is mapped on the hash ring according to the value of the equal difference pass item.
  17. 根据权利要求16所述的可读存储介质,其中,将所述hash环上相邻的两个虚拟服务节点之间的弧长作为公差,并基于等差数列的通项公式计算等差通项的数值之前,所述计算机程序被处理器执行时还实现以下步骤:The readable storage medium according to claim 16, wherein the arc length between two adjacent virtual service nodes on the hash ring is used as a tolerance, and the equal difference common term is calculated based on the general term formula of the arithmetic sequence Before the value of, the computer program also implements the following steps when being executed by the processor:
    获取所述虚拟服务节点的hash值的取值范围,并根据所述取值范围获得所述hash环的周长;Obtaining a value range of the hash value of the virtual service node, and obtaining the perimeter of the hash ring according to the value range;
    根据所述hash环的周长和所述hash环上所述虚拟服务节点的数量计算出所述弧长。The arc length is calculated according to the perimeter of the hash ring and the number of virtual service nodes on the hash ring.
  18. 根据权利要求15所述的可读存储介质,其中,分别从多个真实服务节点对应的虚拟服务节点中选择一个虚拟服务节点组成一个虚拟服务节点群组,以得到多个虚拟服务节点群组,每个所述虚拟服务节点群组中的虚拟服务节点对应的真实服务节点的顺序相同时,具体实现:15. The readable storage medium according to claim 15, wherein a virtual service node is selected from the virtual service nodes corresponding to the multiple real service nodes to form a virtual service node group to obtain multiple virtual service node groups, When the sequence of the real service nodes corresponding to the virtual service nodes in each virtual service node group is the same, the specific implementation is as follows:
    对每个所述真实服务节点以及每个所述真实服务节点对应的虚拟服务节点分别进行编号,其中,每个所述真实服务节点对应的虚拟服务节点的编号顺序相同;Respectively number each of the real service nodes and the virtual service nodes corresponding to each of the real service nodes, wherein the numbering sequence of the virtual service nodes corresponding to each of the real service nodes is the same;
    将编号相同的虚拟服务节点组成一个所述虚拟服务节点群组,并在每个所述虚拟服务节点群组中按照对应的真实服务节点的编号进行排列。The virtual service nodes with the same number are formed into one virtual service node group, and the virtual service node groups are arranged according to the number of the corresponding real service node.
  19. 根据权利要求15所述的可读存储介质,其中,将所述虚拟服务节点队列均匀分布在hash环上之后,所述计算机程序被处理器执行时还实现以下步骤:The readable storage medium according to claim 15, wherein after the virtual service node queue is evenly distributed on the hash ring, the following steps are further implemented when the computer program is executed by the processor:
    计算请求信息的hash值;Calculate the hash value of the requested information;
    根据所述请求信息的hash值,将所述请求信息分配给顺时针方向最近的所述虚拟服务节点。According to the hash value of the request information, the request information is allocated to the virtual service node closest in a clockwise direction.
  20. 根据权利要求15所述的可读存储介质,其中,The readable storage medium according to claim 15, wherein:
    所述虚拟服务节点队列中的所述虚拟服务节点的个数为偶数。The number of the virtual service nodes in the virtual service node queue is an even number.
PCT/CN2020/117659 2020-08-27 2020-09-25 Hash ring load balancing method and apparatus, and electronic device and storage medium WO2021151293A1 (en)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN202010879098.8 2020-08-27
CN202010879098.8A CN112015552A (en) 2020-08-27 2020-08-27 Hash ring load balancing method and device, electronic equipment and storage medium

Publications (1)

Publication Number Publication Date
WO2021151293A1 true WO2021151293A1 (en) 2021-08-05

Family

ID=73503823

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2020/117659 WO2021151293A1 (en) 2020-08-27 2020-09-25 Hash ring load balancing method and apparatus, and electronic device and storage medium

Country Status (2)

Country Link
CN (1) CN112015552A (en)
WO (1) WO2021151293A1 (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114095565A (en) * 2021-11-18 2022-02-25 税友信息技术有限公司 Service discovery method, device, equipment and medium

Families Citing this family (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113923218B (en) * 2021-10-09 2023-07-21 天翼物联科技有限公司 Distributed deployment method, device, equipment and medium for coding and decoding plug-in

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN104158755A (en) * 2014-07-30 2014-11-19 华为技术有限公司 Method, device and system used for transmitting messages
CN104754001A (en) * 2013-12-30 2015-07-01 方正宽带网络服务股份有限公司 Cloud storage system and data storage method
CN105956068A (en) * 2016-04-27 2016-09-21 湖南蚁坊软件有限公司 Webpage URL repetition elimination method based on distributed database
CN109617989A (en) * 2018-12-28 2019-04-12 浙江省公众信息产业有限公司 For loading method, apparatus, system and the computer-readable medium of distribution
US20190121559A1 (en) * 2016-10-08 2019-04-25 Tencent Technology (Shenzhen) Company Limited Data processing method and apparatus and storage medium

Family Cites Families (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN104104611B (en) * 2014-07-10 2017-12-12 浪潮(北京)电子信息产业有限公司 A kind of method and device for realizing cluster load balance scheduling
CN106559448A (en) * 2015-09-28 2017-04-05 北京国双科技有限公司 Server load balancing method and apparatus
CN107154957A (en) * 2016-12-29 2017-09-12 贵州电网有限责任公司铜仁供电局 Distributed storage control method based on virtual ring load-balancing algorithm
CN106850459B (en) * 2017-02-27 2020-02-04 苏州浪潮智能科技有限公司 Method and device for realizing load balance of virtual network

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN104754001A (en) * 2013-12-30 2015-07-01 方正宽带网络服务股份有限公司 Cloud storage system and data storage method
CN104158755A (en) * 2014-07-30 2014-11-19 华为技术有限公司 Method, device and system used for transmitting messages
CN105956068A (en) * 2016-04-27 2016-09-21 湖南蚁坊软件有限公司 Webpage URL repetition elimination method based on distributed database
US20190121559A1 (en) * 2016-10-08 2019-04-25 Tencent Technology (Shenzhen) Company Limited Data processing method and apparatus and storage medium
CN109617989A (en) * 2018-12-28 2019-04-12 浙江省公众信息产业有限公司 For loading method, apparatus, system and the computer-readable medium of distribution

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114095565A (en) * 2021-11-18 2022-02-25 税友信息技术有限公司 Service discovery method, device, equipment and medium
CN114095565B (en) * 2021-11-18 2023-08-29 税友信息技术有限公司 Service discovery method, device, equipment and medium

Also Published As

Publication number Publication date
CN112015552A (en) 2020-12-01

Similar Documents

Publication Publication Date Title
US10996879B2 (en) Locality-based load balancing of input-output paths
JP5175483B2 (en) Storage apparatus and control method thereof
US7802251B2 (en) System for resource allocation to an active virtual machine using switch and controller to associate resource groups
US9052824B2 (en) Content addressable stores based on sibling groups
US9043547B2 (en) Virtualized data storage in a network computing environment
WO2021151293A1 (en) Hash ring load balancing method and apparatus, and electronic device and storage medium
US10826990B2 (en) Clustered storage system configured for bandwidth efficient processing of writes at sizes below a native page size
US10248460B2 (en) Storage management computer
JP2010537297A (en) Network adapter resource allocation between logical partitions
US11140220B1 (en) Consistent hashing using the power of k choices in server placement
JP2021089704A (en) Method, apparatus, electronic device, readable storage medium, and computer program for data query
WO2022111313A1 (en) Request processing method and micro-service system
WO2023072048A1 (en) Network storage method, storage system, data processing unit, and computer system
CN111913670A (en) Load balancing processing method and device, electronic equipment and storage medium
CN108920613A (en) A kind of metadata management method, system and equipment and storage medium
US11281384B2 (en) Method, device and computer program product for managing memory of dedicated processing resource
JP2015532734A (en) Management system for managing physical storage system, method for determining resource migration destination of physical storage system, and storage medium
US11012364B2 (en) Aggregated link flow control system
US10956125B2 (en) Data shuffling with hierarchical tuple spaces
US9262083B2 (en) Determining efficiency of a virtual array in a virtualized storage system
JP2023539212A (en) Storage level load balancing
US11151048B2 (en) Host-based read performance optimization of a content addressable storage system
WO2023029485A1 (en) Data processing method and apparatus, computer device, and computer-readable storage medium
US11310309B1 (en) Arc jump: per-key selection of an alternative server when implemented bounded loads
US11093147B2 (en) Asymmetric logical unit access path distribution system

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 20916372

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 20916372

Country of ref document: EP

Kind code of ref document: A1