HashRing
in package
implements
DistributorInterface, HashGeneratorInterface
This class implements an hashring-based distributor that uses the same algorithm of memcache to distribute keys in a cluster using client-side sharding.
Tags
Interfaces, Classes and Traits
- DistributorInterface
- A distributor implements the logic to automatically distribute keys among several nodes for client-side sharding.
- HashGeneratorInterface
- An hash generator implements the logic used to calculate the hash of a key to distribute operations among Redis nodes.
Table of Contents
- DEFAULT_REPLICAS = 128
- DEFAULT_WEIGHT = 100
- $nodeHashCallback : mixed
- $nodes : mixed
- $replicas : mixed
- $ring : mixed
- $ringKeys : mixed
- $ringKeysCount : mixed
- __construct() : mixed
- add() : mixed
- Adds a node to the ring with an optional weight.
- get() : mixed
- Returns a node from the distributor mapping to the specified value.
- getByHash() : mixed
- Returns a node from the distributor using the computed hash of a key.
- getBySlot() : mixed|null
- Returns a node from the distributor using its assigned slot ID.
- getHashGenerator() : HashGeneratorInterface
- Returns the underlying hash generator instance.
- getSlot() : mixed
- Returns the corresponding slot of a node from the distributor using the computed hash of a key.
- hash() : int
- Generates an hash from a string to be used for distribution.
- remove() : mixed
- Removes a node from the distributor.
- addNodeToRing() : mixed
- Implements the logic needed to add a node to the hashring.
- getNodeHash() : mixed
- {@inheritdoc}
- wrapAroundStrategy() : int
- Implements a strategy to deal with wrap-around errors during binary searches.
- computeTotalWeight() : int
- Calculates the total weight of all the nodes in the distributor.
- initialize() : mixed
- Initializes the distributor.
- isInitialized() : bool
- Returns the initialization status of the distributor.
- reset() : mixed
- Resets the distributor.
Constants
DEFAULT_REPLICAS
public
mixed
DEFAULT_REPLICAS
= 128
DEFAULT_WEIGHT
public
mixed
DEFAULT_WEIGHT
= 100
Properties
$nodeHashCallback
private
mixed
$nodeHashCallback
$nodes
private
mixed
$nodes
= array()
$replicas
private
mixed
$replicas
$ring
private
mixed
$ring
$ringKeys
private
mixed
$ringKeys
$ringKeysCount
private
mixed
$ringKeysCount
Methods
__construct()
public
__construct([int $replicas = self::DEFAULT_REPLICAS ][, mixed $nodeHashCallback = null ]) : mixed
Parameters
- $replicas : int = self::DEFAULT_REPLICAS
-
Number of replicas in the ring.
- $nodeHashCallback : mixed = null
-
Callback returning a string used to calculate the hash of nodes.
Return values
mixed —add()
Adds a node to the ring with an optional weight.
public
add(mixed $node[, int $weight = null ]) : mixed
Parameters
- $node : mixed
-
Node object.
- $weight : int = null
-
Weight for the node.
Return values
mixed —get()
Returns a node from the distributor mapping to the specified value.
public
get(mixed $value) : mixed
Parameters
- $value : mixed
Return values
mixed —getByHash()
Returns a node from the distributor using the computed hash of a key.
public
getByHash(mixed $hash) : mixed
Parameters
- $hash : mixed
Return values
mixed —getBySlot()
Returns a node from the distributor using its assigned slot ID.
public
getBySlot(mixed $slot) : mixed|null
Parameters
- $slot : mixed
Return values
mixed|null —getHashGenerator()
Returns the underlying hash generator instance.
public
getHashGenerator() : HashGeneratorInterface
Return values
HashGeneratorInterface —getSlot()
Returns the corresponding slot of a node from the distributor using the computed hash of a key.
public
getSlot(mixed $hash) : mixed
Parameters
- $hash : mixed
Return values
mixed —hash()
Generates an hash from a string to be used for distribution.
public
hash(mixed $value) : int
Parameters
- $value : mixed
-
String value.
Return values
int —remove()
Removes a node from the distributor.
public
remove(mixed $node) : mixed
Parameters
- $node : mixed
-
Node object.
Return values
mixed —addNodeToRing()
Implements the logic needed to add a node to the hashring.
protected
addNodeToRing(array<string|int, mixed> &$ring, mixed $node, int $totalNodes, int $replicas, float $weightRatio) : mixed
Parameters
- $ring : array<string|int, mixed>
-
Source hashring.
- $node : mixed
-
Node object to be added.
- $totalNodes : int
-
Total number of nodes.
- $replicas : int
-
Number of replicas in the ring.
- $weightRatio : float
-
Weight ratio for the node.
Return values
mixed —getNodeHash()
{@inheritdoc}
protected
getNodeHash(mixed $nodeObject) : mixed
Parameters
- $nodeObject : mixed
Return values
mixed —wrapAroundStrategy()
Implements a strategy to deal with wrap-around errors during binary searches.
protected
wrapAroundStrategy(int $upper, int $lower, int $ringKeysCount) : int
Parameters
- $upper : int
- $lower : int
- $ringKeysCount : int
Return values
int —computeTotalWeight()
Calculates the total weight of all the nodes in the distributor.
private
computeTotalWeight() : int
Return values
int —initialize()
Initializes the distributor.
private
initialize() : mixed
Return values
mixed —isInitialized()
Returns the initialization status of the distributor.
private
isInitialized() : bool
Return values
bool —reset()
Resets the distributor.
private
reset() : mixed