maghash

package module
v0.0.0-...-857ed74 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2018 License: MIT Imports: 7 Imported by: 0

README

Maghash

Maglev hashing algorithm implementation in Golang for load balance backend selection. Paper

Interface
// MagHash is an interface providing Maglev Hashing functions
type MagHash interface {
	// Add backends to Maglev hashing, those added items will be considered
	// as available backends. This will internally RW lock backend related
	// data structures. It will trigger a permutation and lookup table re-calculation
	AddBackends(backends []string) (err error)

	// Remove backends from backend list, we don't need recalculate permutation
	// but a lookup table populate is required.
	RemoveBackends(backends []string)

	// Get the current number of backends.
	BackendsNum() (count int)

	// Get the m value for hash calculation. M must be a prime number.
	M() (m int)

	// Get the backend lookup result for given flow.
	GetBackend(flow string) (backend string)

	// Get the lookup table (list of backends)
	LookupTable() (backends []string)
}
Documentation:

Godoc

Example
maghash.go:

package main

import (
	"github.com/ksang/maghash"
	"log"
	"time"
)

func main() {
	// 0 means use the default M value: 65537
	mh, err := maghash.NewMagHash(0)
	if err != nil {
		log.Fatal(err)
	}

	backends := []string{
		"1.1.1.1",
		"2.2.2.2",
		"3.3.3.3",
		"4.4.4.4",
		"5.5.5.5",
	}

	flows := []string{
		"10.0.0.1:80|10.0.0.2:8080|tcp",
		"10.0.0.1:80|10.0.0.2:8081|tcp",
		"10.0.1.1:80|10.0.1.2:65535|udp",
	}

	// Add backend servers to Maglev Hashing
	if err := mh.AddBackends(backends); err != nil {
		log.Fatal(err)
	}

	// The lookup table calculation is Asyncronize, so need to wait
	time.Sleep(time.Second)
	for _, f := range flows {
		log.Println("Backend selected:", mh.GetBackend(f))
	}

}
Output
$ go run maghash.go
2016/07/24 13:36:32 Backend selected: 4.4.4.4
2016/07/24 13:36:32 Backend selected: 1.1.1.1
2016/07/24 13:36:32 Backend selected: 3.3.3.3

Documentation

Overview

Package maghash implements Google's load balance solution - Maglev's consistent hashing algorithm. ref:

http://static.googleusercontent.com/media/research.google.com/zh-CN//pubs/archive/44824.pdf

Index

Constants

This section is empty.

Variables

View Source
var (
	//ErrInvalidIndex indicates invalid index
	ErrInvalidIndex = errors.New("invalid index")
	//ErrInvalidPrime indicates invalid prime number
	ErrInvalidPrime = errors.New("invalid prime number")
)

Functions

This section is empty.

Types

type MagHash

type MagHash interface {
	// Add backends to Maglev hashing, those added items will be considered
	// as available backends. This will internally RW lock backend related
	// data structures. It will trigger a permutation and lookup table re-calculation
	AddBackends(backends []string) (err error)

	// Remove backends from backend list, we don't need recalculate permutation
	// but a lookup table populate is required.
	RemoveBackends(backends []string)

	// Get the current number of backends.
	BackendsNum() (count int)

	// Get the m value for hash calculation.
	M() (m int)

	// Get the backend lookup result for given flow.
	GetBackend(flow string) (backend string)

	// Get the lookup table (list of backends)
	LookupTable() (backends []string)
}

MagHash is an interface providing Maglev Hashing functions

func NewMagHash

func NewMagHash(m int) (mh MagHash, err error)

NewMagHash creates Maghash with M, M must larger than backend number. A greater M value increasing backend selection equalization while decreasing performance.

Jump to

Keyboard shortcuts

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