hrw

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2023 License: MIT Imports: 6 Imported by: 3

README

Golang HRW implementation

Build Status codecov Report GitHub release

Rendezvous or highest random weight (HRW) hashing is an algorithm that allows clients to achieve distributed agreement on a set of k options out of a possible set of n options. A typical application is when clients need to agree on which sites (or proxies) objects are assigned to. When k is 1, it subsumes the goals of consistent hashing, using an entirely different method.

Install

go get github.com/nspcc-dev/hrw

Benchmark:

BenchmarkSort_fnv_10-8                                           4812801               240.9 ns/op           216 B/op          4 allocs/op
BenchmarkSort_fnv_100-8                                           434767              2600 ns/op            1848 B/op          4 allocs/op
BenchmarkSort_fnv_1000-8                                           20428             66116 ns/op           16440 B/op          4 allocs/op
BenchmarkSortByIndex_fnv_10-8                                    2505410               486.5 ns/op           352 B/op          7 allocs/op
BenchmarkSortByIndex_fnv_100-8                                    254556              4697 ns/op            1984 B/op          7 allocs/op
BenchmarkSortByIndex_fnv_1000-8                                    13581             88334 ns/op           16576 B/op          7 allocs/op
BenchmarkSortByValue_fnv_10-8                                    1761030               682.1 ns/op           592 B/op         18 allocs/op
BenchmarkSortByValue_fnv_100-8                                    258838              4675 ns/op            4480 B/op        108 allocs/op
BenchmarkSortByValue_fnv_1000-8                                    27027             44649 ns/op           40768 B/op       1008 allocs/op
BenchmarkSortHashersByValue_Reflection_fnv_10-8                  1013560              1249 ns/op             768 B/op         29 allocs/op
BenchmarkSortHashersByValue_Reflection_fnv_100-8                  106029             11414 ns/op            6096 B/op        209 allocs/op
BenchmarkSortHashersByValue_Reflection_fnv_1000-8                  10000            108977 ns/op           56784 B/op       2009 allocs/op
BenchmarkSortHashersByValue_Typed_fnv_10-8                       1577814               700.3 ns/op           584 B/op         17 allocs/op
BenchmarkSortHashersByValue_Typed_fnv_100-8                       215938              5024 ns/op            4472 B/op        107 allocs/op
BenchmarkSortHashersByValue_Typed_fnv_1000-8                       24447             46889 ns/op           40760 B/op       1007 allocs/op

BenchmarkSortByWeight_fnv_10-8                                   2924833               370.6 ns/op           448 B/op          8 allocs/op
BenchmarkSortByWeight_fnv_100-8                                   816069              1516 ns/op            2896 B/op          8 allocs/op
BenchmarkSortByWeight_fnv_1000-8                                   80391             17478 ns/op           24784 B/op          8 allocs/op
BenchmarkSortByWeightIndex_fnv_10-8                              1945612               550.3 ns/op           368 B/op          7 allocs/op
BenchmarkSortByWeightIndex_fnv_100-8                              140473              8084 ns/op            2000 B/op          7 allocs/op
BenchmarkSortByWeightIndex_fnv_1000-8                               5518            200949 ns/op           16592 B/op          7 allocs/op
BenchmarkSortByWeightValue_fnv_10-8                              1305580               909.8 ns/op           608 B/op         18 allocs/op
BenchmarkSortByWeightValue_fnv_100-8                              165410              6796 ns/op            4496 B/op        108 allocs/op
BenchmarkSortByWeightValue_fnv_1000-8                              17922             78555 ns/op           40784 B/op       1008 allocs/op
BenchmarkSortHashersByWeightValueReflection_fnv_10-8              454976              2229 ns/op             784 B/op         29 allocs/op
BenchmarkSortHashersByWeightValueReflection_fnv_100-8              76264             15332 ns/op            6112 B/op        209 allocs/op
BenchmarkSortHashersByWeightValueReflection_fnv_1000-8             80288             13192 ns/op            6112 B/op        209 allocs/op
BenchmarkSortHashersByWeightValueTyped_fnv_10-8                  1433113               901.4 ns/op           600 B/op         17 allocs/op
BenchmarkSortHashersByWeightValueTyped_fnv_100-8                  188626              5896 ns/op            4488 B/op        107 allocs/op
BenchmarkSortHashersByWeightValueTyped_fnv_1000-8                 178131              6518 ns/op            4488 B/op        107 allocs/op

Example

package main

import (
	"fmt"

	"git.frostfs.info/TrueCloudLab/hrw"
)

func main() {
	// given a set of servers
	servers := []string{
		"one.example.com",
		"two.example.com",
		"three.example.com",
		"four.example.com",
		"five.example.com",
		"six.example.com",
	}

	// HRW can consistently select a uniformly-distributed set of servers for
	// any given key
	var (
		key = []byte("/examples/object-key")
		h   = hrw.Hash(key)
	)

	hrw.SortSliceByValue(servers, h)
	for id := range servers {
		fmt.Printf("trying GET %s%s\n", servers[id], key)
	}

	// Output:
	// trying GET three.example.com/examples/object-key
	// trying GET two.example.com/examples/object-key
	// trying GET five.example.com/examples/object-key
	// trying GET six.example.com/examples/object-key
	// trying GET one.example.com/examples/object-key
	// trying GET four.example.com/examples/object-key
}

Documentation

Overview

Package hrw implements Rendezvous hashing. http://en.wikipedia.org/wiki/Rendezvous_hashing.

Example
// given a set of servers
servers := []string{
	"one.example.com",
	"two.example.com",
	"three.example.com",
	"four.example.com",
	"five.example.com",
	"six.example.com",
}

// HRW can consistently select a uniformly-distributed set of servers for
// any given key
var (
	key = []byte("/examples/object-key")
	h   = Hash(key)
)

SortSliceByValue(servers, h)
for id := range servers {
	fmt.Printf("trying GET %s%s\n", servers[id], key)
}
Output:

trying GET three.example.com/examples/object-key
trying GET two.example.com/examples/object-key
trying GET five.example.com/examples/object-key
trying GET six.example.com/examples/object-key
trying GET one.example.com/examples/object-key
trying GET four.example.com/examples/object-key

Index

Examples

Constants

View Source
const (
	NormalizedMaxWeight = 1.0
	NormalizedMinWeight = 0.0
)

Boundaries of valid normalized weights

Variables

This section is empty.

Functions

func Hash

func Hash(key []byte) uint64

Hash uses murmur3 hash to return uint64

func Sort

func Sort(nodes []uint64, hash uint64) []uint64

Sort receive nodes and hash, and sort it by distance

func SortByWeight

func SortByWeight(nodes []uint64, weights []float64, hash uint64) []uint64

SortByWeight receive nodes, weights and hash, and sort it by distance * weight

func SortHasherSliceByValue

func SortHasherSliceByValue[T Hasher](slice []T, hash uint64)

SortHasherSliceByValue receives []Hasher and hash to sort by value-distance.

func SortHasherSliceByWeightValue

func SortHasherSliceByWeightValue[T Hasher](slice []T, weights []float64, hash uint64)

SortHasherSliceByWeightValue receives []Hasher, weights and hash to sort by value-distance * weights.

func SortSliceByIndex

func SortSliceByIndex(slice interface{}, hash uint64)

SortSliceByIndex received []T and hash to sort by index-distance

func SortSliceByValue

func SortSliceByValue(slice interface{}, hash uint64)

SortSliceByValue received []T and hash to sort by value-distance

func SortSliceByWeightIndex

func SortSliceByWeightIndex(slice interface{}, weights []float64, hash uint64)

SortSliceByWeightIndex received []T, weights and hash to sort by index-distance * weights

func SortSliceByWeightValue

func SortSliceByWeightValue(slice interface{}, weights []float64, hash uint64)

SortSliceByWeightValue received []T, weights and hash to sort by value-distance * weights

func StringHash added in v1.2.1

func StringHash(key string) uint64

StringHash uses murmur3 hash to return uint64

func ValidateWeights

func ValidateWeights(weights []float64) error

ValidateWeights checks if weights are normalized between 0.0 and 1.0

Types

type Hasher

type Hasher interface{ Hash() uint64 }

Hasher interface used by SortSliceByValue

Jump to

Keyboard shortcuts

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