Documentation
¶
Index ¶
- Variables
- type Config
- type ConsistentHashing
- func (c *ConsistentHashing) Add(ctx context.Context, host string) error
- func (c *ConsistentHashing) DecreaseLoad(ctx context.Context, host string) error
- func (c *ConsistentHashing) Get(ctx context.Context, key string) (string, error)
- func (c *ConsistentHashing) GetLeast(ctx context.Context, key string) (string, error)
- func (c *ConsistentHashing) GetLoads() map[string]int64
- func (c *ConsistentHashing) Hash(key string) (uint64, error)
- func (c *ConsistentHashing) Hosts() []string
- func (c *ConsistentHashing) IncreaseLoad(ctx context.Context, host string) error
- func (c *ConsistentHashing) LoadOk(host string) bool
- func (c *ConsistentHashing) MaxLoad() int64
- func (c *ConsistentHashing) Remove(ctx context.Context, host string) error
- func (c *ConsistentHashing) Search(key uint64) (int, error)
- func (c *ConsistentHashing) UpdateLoad(ctx context.Context, host string, load int64) error
- type Host
Constants ¶
This section is empty.
Variables ¶
var ( ErrNoHost = errors.New("no host added") ErrHostNotFound = errors.New("host not found") )
Custom errors
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
ReplicationFactor int // no of virtual_nodes per host
LoadFactor float64 // max load factor before redistribution
HashFunction func() hash.Hash64 // for the time being lets keep the hash function simple
}
Consistent Hashing config parameters
type ConsistentHashing ¶
type ConsistentHashing struct {
// contains filtered or unexported fields
}
CH with bounded loads
func (*ConsistentHashing) Add ¶
func (c *ConsistentHashing) Add(ctx context.Context, host string) error
Add adds a new host to the consistent hashing ring, including its virtual nodes, and updates the internal data structures accordingly. It returns an error if the operation fails.
func (*ConsistentHashing) DecreaseLoad ¶
func (c *ConsistentHashing) DecreaseLoad(ctx context.Context, host string) error
DecreaseLoad decreases the Load for a specific host.
func (*ConsistentHashing) Get ¶
Get retrieves the host that should handle the given key in the consistent hashing ring. It returns the host name and nil error if successful. If no hosts are added, it returns ErrNoHost. If there's an error generating the hash value or searching for it, it returns an appropriate error. If the host associated with the hash value is not found, it returns ErrHostNotFound.
func (*ConsistentHashing) GetLeast ¶
GetLeast retrieves the host that should handle the given key in the consistent hashing ring with the least current load. It returns the host name and nil error if successful. If no hosts are added, it returns ErrNoHost. If there's an error generating the hash value or searching for it, it returns an appropriate error. If no host with acceptable load is found, it falls back to returning the initially found host. If no suitable host is found at all, it returns ErrHostNotFound. Bounded Loads: Research Paper: https://research.googleblog.com/2017/04/consistent-hashing-with-bounded-loads.html
func (*ConsistentHashing) GetLoads ¶
func (c *ConsistentHashing) GetLoads() map[string]int64
GetLoads returns the current load for all hosts
func (*ConsistentHashing) Hash ¶
func (c *ConsistentHashing) Hash(key string) (uint64, error)
hash generates a 64-bit hash value for a given key using the configured hash function. It returns the computed hash value and an error, if any occurred during the hashing process.
func (*ConsistentHashing) Hosts ¶
func (c *ConsistentHashing) Hosts() []string
Hosts returns the list of current hosts
func (*ConsistentHashing) IncreaseLoad ¶
func (c *ConsistentHashing) IncreaseLoad(ctx context.Context, host string) error
IncreaseLoad increments the load for a specific host.
func (*ConsistentHashing) LoadOk ¶
func (c *ConsistentHashing) LoadOk(host string) bool
LoadOk checks if the host's current load is below the maximum allowed load. It returns true if the host's load is acceptable, otherwise false.
func (*ConsistentHashing) MaxLoad ¶
func (c *ConsistentHashing) MaxLoad() int64
MaxLoad calculates and returns the maximum allowed load per host based on the current total load across all hosts and the configured load factor.
func (*ConsistentHashing) Remove ¶
func (c *ConsistentHashing) Remove(ctx context.Context, host string) error
Remove removes a host from the hash ring
func (*ConsistentHashing) Search ¶
func (c *ConsistentHashing) Search(key uint64) (int, error)
Search finds the closest index in the sorted set where the given hash key should be placed. It uses binary search to efficiently locate the index. For example, if c.sortedSet = [10, 20, 30, 40, 50] and key = 25, sort.Search determines that key should be inserted after 20 and before 30, returning index 2. The modulo operation (index % len(c.sortedSet)) ensures correct placement within the ring structure
func (*ConsistentHashing) UpdateLoad ¶
UpdateLoad updates the load for a specific host