Documentation
¶
Index ¶
- Constants
- type Config
- type Consistent
- func (c *Consistent) Add(member Member)
- func (c *Consistent) ClosestN(key []byte, count int) ([]Member, error)
- func (c *Consistent) GetAverageLoad() float64
- func (c *Consistent) GetLoadDistribution() map[string]float64
- func (c *Consistent) GetPartitionID(key []byte) int
- func (c *Consistent) GetPartitionOwner(partitionID int) Member
- func (c *Consistent) LocateKey(key []byte) Member
- func (c *Consistent) Members() []Member
- func (c *Consistent) Remove(memberName string)
- type Hasher
- type Member
Constants ¶
const ( // DefaultPartitionCount defines the default number of virtual partitions in the hash ring. // This helps balance the load distribution among members, even with a small number of members. DefaultPartitionCount int = 271 // DefaultReplicationFactor specifies the default number of replicas for each partition. // This ensures redundancy and fault tolerance by assigning partitions to multiple members. DefaultReplicationFactor int = 20 // DefaultLoad defines the default maximum load factor for each member. // A higher value allows members to handle more load before being considered full. DefaultLoad float64 = 1.25 // DefaultPickerWidth determines the default range of candidates considered when picking members. // This can influence the selection logic in advanced configurations. DefaultPickerWidth int = 1 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶ added in v1.2.8
type Config struct { // Hasher is an interface or implementation used for generating hash values. // It is typically used to distribute data evenly across partitions. Hasher Hasher // PartitionCount defines the number of partitions in the system. // This value affects how data is distributed and processed. PartitionCount int // ReplicationFactor specifies the number of replicas for each partition. // It ensures data redundancy and fault tolerance in the system. ReplicationFactor int // Load represents the load balancing factor for the system. // It could be a threshold or weight used for distributing work. Load float64 // PickerWidth determines the width or range of the picker mechanism. // It is typically used to influence how selections are made in certain operations. PickerWidth int }
Config represents the configuration settings for a specific system or application. It includes settings for hashing, partitioning, replication, load balancing, and picker width.
type Consistent ¶
type Consistent struct {
// contains filtered or unexported fields
}
Consistent implements a consistent hashing mechanism with partitioning and load balancing. It is used for distributing data across a dynamic set of members efficiently.
func New ¶ added in v1.2.8
func New(config Config) *Consistent
New initializes and returns a new instance of the Consistent struct. It takes a Config parameter and applies default values for any unset fields.
func (*Consistent) Add ¶
func (c *Consistent) Add(member Member)
Add safely adds a new member to the consistent hash circle. It ensures thread safety and redistributes partitions after adding the member.
func (*Consistent) ClosestN ¶ added in v1.2.8
func (c *Consistent) ClosestN(key []byte, count int) ([]Member, error)
ClosestN calculates the closest N members to a given key in the consistent hash ring. It uses the key to determine the partition ID and then retrieves the closest members. This is useful for identifying members for replication or redundancy.
func (*Consistent) GetAverageLoad ¶ added in v1.2.8
func (c *Consistent) GetAverageLoad() float64
GetAverageLoad calculates and returns the current average load across all members. It is a public method that provides thread-safe access to the load calculation.
func (*Consistent) GetLoadDistribution ¶ added in v1.2.8
func (c *Consistent) GetLoadDistribution() map[string]float64
GetLoadDistribution provides a thread-safe snapshot of the current load distribution across members. It returns a map where the keys are member identifiers and the values are their respective loads.
func (*Consistent) GetPartitionID ¶ added in v1.2.8
func (c *Consistent) GetPartitionID(key []byte) int
GetPartitionID calculates and returns the partition ID for a given key. The partition ID is determined by hashing the key and applying modulo operation with the partition count.
func (*Consistent) GetPartitionOwner ¶ added in v1.2.8
func (c *Consistent) GetPartitionOwner(partitionID int) Member
GetPartitionOwner retrieves the owner of the specified partition in a thread-safe manner. It ensures that the access to shared resources is synchronized.
func (*Consistent) LocateKey ¶ added in v1.2.8
func (c *Consistent) LocateKey(key []byte) Member
LocateKey determines the owner of the partition corresponding to the given key. It calculates the partition ID for the key and retrieves the associated member in a thread-safe manner.
func (*Consistent) Members ¶ added in v1.2.8
func (c *Consistent) Members() []Member
Members returns a slice of all the members currently in the consistent hash ring. It safely retrieves the members using a read lock to prevent data races while accessing the shared `members` map.
func (*Consistent) Remove ¶
func (c *Consistent) Remove(memberName string)
Remove deletes a member from the consistent hash circle and redistributes partitions. If the member does not exist, the method exits early.