dht

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2021 License: Apache-2.0 Imports: 5 Imported by: 0

README

DHT

一致性哈希
创建和使用
import (
	"github.com/8treenet/venus/dht"
)
func TestNormal(t *testing.T) {
	//创建一致性哈希和范围1-100的节点
	hash := dht.New().Range(1, 100)
	t.Log(hash.FindNode(55)) //查找节点 55

	//创建一致性哈希和列表节点
	hash = dht.New().List("hostname1", "hostname2", "hostname3")
	t.Log(hash.FindNode("hostname1")) //查找节点 hostname1

	//获取全部节点
	for _, v := range hash.GetNodes() {
		//打印节点
		t.Log(v.Value(), v.CRC32())
	}
}
$ 55
$ hostname1
$ hostname1 3918110341
$ hostname2 1887489855
$ hostname3 126353321
查找数据的节点
import (
	"github.com/8treenet/venus/dht"
)
func TestSearch(t *testing.T) {
	//方式1 创建一致性哈希和范围1-100的节点
	hash := dht.New().Range(1, 100)
	//输入数据 查找节点
	node := hash.Search("freedom")
	t.Log(node.Value(), node.CRC32())

	//方式2 创建一致性哈希和列表节点
	hash = dht.New().List("hostname1", "hostname2", "hostname3")
	//输入数据 查找节点
	node = hash.Search("group-1001")
	t.Log(node.Value(), node.CRC32())
}
$ 74 4033496702
$ hostname3 126353321
节点伸缩后的数据分布
import (
	"fmt"
	"math/rand"
	"testing"
	"github.com/8treenet/venus/dht"
)
func TestRebalance(t *testing.T) {
	//创建一致性哈希和范围1-100的节点
	hash := dht.New().Range(1, 100)

	//伪造50000行数据
	rows := []string{}
	for i := 0; i < 50000; i++ {
		rows = append(rows, fmt.Sprintf("freedom:%d", rand.Intn(99999999999)))
	}

	//为每行数据分配crc32
	rowNodeMap := map[string]uint32{}
	for i := 0; i < len(rows); i++ {
		node := hash.Search(rows[i]) //查找节点
		rowNodeMap[rows[i]] = node.CRC32()
	}

	//测试伸缩节点后crc32的分布
	diffCount := 0
	hash.AddNode(dht.NewNode(101)) //增加节点101
	//hash.RemoveNode(dht.NewNode(88)) //删除节点88
	for i := 0; i < len(rows); i++ {
		node := hash.Search(rows[i]) //重新查找节点
		if rowNodeMap[rows[i]] != node.CRC32() {
			diffCount++ //重新分布总数递增
		}
	}

	t.Log(diffCount) //打印重新分布的总数
}
$ 1374

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConsistentHashing

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

ConsistentHashing .

func (*ConsistentHashing) AddNode

func (hash *ConsistentHashing) AddNode(node *Node) error

AddNode .

func (*ConsistentHashing) FindNode

func (hash *ConsistentHashing) FindNode(data interface{}) (result *Node)

FindNode .

func (*ConsistentHashing) GetNodes

func (hash *ConsistentHashing) GetNodes() (result []*Node)

GetNodes .

func (*ConsistentHashing) RemoveNode

func (hash *ConsistentHashing) RemoveNode(node *Node)

RemoveNode .

func (*ConsistentHashing) Search

func (hash *ConsistentHashing) Search(data interface{}) *Node

Search .

type DHT

type DHT interface {
	Range(begin, end int) *ConsistentHashing
	List(data ...interface{}) *ConsistentHashing
}

DHT Create Consistent Hashing Object

func New

func New() DHT

New .

type Node

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

Node .

func NewNode

func NewNode(data interface{}) (result *Node)

NewNode .

func (*Node) CRC32

func (node *Node) CRC32() uint32

CRC32 .

func (*Node) GetProperty

func (node *Node) GetProperty(key string) (result interface{}, ok bool)

GetProperty .

func (*Node) SetProperty

func (node *Node) SetProperty(key string, value interface{})

SetProperty .

func (*Node) String

func (node *Node) String() string

String .

func (*Node) Value

func (node *Node) Value() interface{}

Value .

Jump to

Keyboard shortcuts

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