Module resty.dns.balancer.consistent_hashing

Consistent-Hashing balancer

This balancer implements a consistent-hashing algorithm based on the Ketama algorithm.

This load balancer is designed to make sure that every time a load balancer object is built, it is built the same, no matter the order the process is done.

NOTE: This documentation only described the altered user methods/properties, see the user properties from the balancer_base for a complete overview.

Info:

  • Copyright: 2020 Kong Inc. All rights reserved.
  • License: Apache 2.0
  • Author: Vinicius Mignot

Functions

_get_continuum () for testing only
addHost (hostname, port, weight) Adds a host to the balancer.
afterHostUpdate (host) Actually adds the addresses to the continuum.
getPeer (cacheOnly, handle, valueToHash) Gets an IP/port/hostname combo for the value to hash This function will hash the valueToHash param and use it as an index in the continuum.
new (opts) Creates a new balancer.


Functions

_get_continuum ()
for testing only
addHost (hostname, port, weight)
Adds a host to the balancer. This function checks if there is enough points to add more hosts and then call the base class's addHost(). see addHost() from the balancer_base for more details.

Parameters:

  • hostname
  • port
  • weight
afterHostUpdate (host)
Actually adds the addresses to the continuum. This function should not be called directly, as it will called by addHost() after adding the new host. This function makes sure the continuum will be built identically every time, no matter the order the hosts are added.

Parameters:

  • host
getPeer (cacheOnly, handle, valueToHash)
Gets an IP/port/hostname combo for the value to hash This function will hash the valueToHash param and use it as an index in the continuum. It will return the address that is at the hashed value or the first one found going counter-clockwise in the continuum.

Parameters:

  • cacheOnly If truthy, no dns lookups will be done, only cache.
  • handle the handle returned by a previous call to getPeer. This will retain some state over retries. See also setAddressStatus.
  • valueToHash value for consistent hashing. Please note that this value will be hashed, so no need to hash it prior to calling this function.

Returns:

    ip + port + hostheader + handle, or nil+error
new (opts)

Creates a new balancer.

The balancer is based on a wheel (continuum) with a number of points between MINCONTINUUMSIZE and MAXCONTINUUMSIZE points. Key points will be assigned to addresses based on their IP and port. The number of points each address will be assigned is proportional to their weight.

The options table has the following fields, additional to the ones from the balancer_base:

  • hosts (optional) containing hostnames, ports, and weights. If omitted, ports and weights default respectively to 80 and 10. The list will be sorted before being added, so the order of entry is deterministic.
  • wheelSize (optional) for total number of positions in the continuum. If omitted DEFAULT_CONTINUUM_SIZE is used. It is important to have enough indices to fit all addresses entries, keep in mind that each address will use 160 entries in the continuum (more or less, proportional to its weight, but the total points will always be 160 * addresses). Consider the maximum number of targets expected, as new hosts can be dynamically added, and DNS renewals might yield larger record sets. The wheelSize cannot be altered, the object has to built again to change this value. On a similar note, making it too big will have a performance impact to get peers from the continuum, as the values will be too dispersed among them.

Parameters:

  • opts table with options

Returns:

    new balancer object or nil+error
generated by LDoc 1.4.6 Last updated 2021-07-06 11:55:24