chash

package
v0.0.0-...-e20221d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 17, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

README

一致性哈希算法是一种用于解决分布式系统中数据分布的算法。具体来说,一致性哈希算法将节点和数据映射到一个固定的区间上,通过对节点和数据的哈希值进行计算,将数据分配到对应的节点上。

一致性哈希算法的基本思路是将节点和哈希值映射到一个闭合区间(环)上,将数据的哈希值也映射到环上,让数据尽可能平均地分布在节点上。具体实现分为以下几个步骤:

  1. 对节点进行 Hash 转换,将节点映射到 0~2^32-1 的环上。
  2. 对数据使用同样的哈希函数进行转换,将数据映射到同样的环上。
  3. 将数据映射到的环上,在顺时针方向上找到第一个遇到的节点,将数据分配到该节点上。

通过这种方法,可以确保在节点增删、数据分配等情况下,数据仍然能够比较均匀地分布在各个节点上,同时节点也能够快速地处理来自数据的请求。此外,一致性哈希算法在节点增删时,影响到的数据节点只有一小部分,不必像传统哈希算法一样需要重新分配所有数据,减少了算法的复杂度和开销。

一致性哈希算法在分布式缓存、负载均衡、分布式文件系统等领域都有着广泛的应用。通过使用一致性哈希算法,可以提高分布式系统的可用性、可扩展性和负载均衡效果,同时也能够减少因节点增删导致的数据迁移。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Consistent

type Consistent interface {
	AddNode(node string)
	RemoveNode(node string)
	GetNode(key string) string
}

type ConsistentHash

type ConsistentHash struct {
	// contains filtered or unexported fields
}

func NewConsistentHash

func NewConsistentHash(opts ...ConsistentHashOption) *ConsistentHash

func (*ConsistentHash) AddNode

func (c *ConsistentHash) AddNode(node string)

func (*ConsistentHash) GetNode

func (c *ConsistentHash) GetNode(key string) string

func (*ConsistentHash) RemoveNode

func (c *ConsistentHash) RemoveNode(node string)

type ConsistentHashOption

type ConsistentHashOption func(c *ConsistentHash)

func WithHash

func WithHash(h HashFunc) ConsistentHashOption

func WithReplica

func WithReplica(replicas int) ConsistentHashOption

type HashFunc

type HashFunc func(data []byte) uint32

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL