chring

package module
v0.0.0-...-8efb767 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2017 License: MIT Imports: 20 Imported by: 0

README

chring

chring is an implementation of a consistent hash ring. It is currently a naive implementation that does not allow for virtual nodes. I plan on adding that soon.

Usage

Create a new hash ring with ring := chring.New().

You add nodes with ring.Add(NodeName). I suggest you use an IP address for a node's name.

Now, you can get a consistent node destination when you ring.Get(key), where key is any value that you want to route upon, such as a user's ID.

If a node goes down, you can ring.Remove(NodeName).

Data Visualization

You can visualize your hash ring and its node locations with chring.ServeRing(ring, ":5000"). Check it out live with cd example/ring; go run main.go and load http://localhost:5000. Neat!

Ring manager

The RingManager is a double ring implementation that allows you to manage nodes and keys separately and you can see its usage in example/ringmanager. You can run its visualization just like with the ring example. The current implementation should allow you to back the ring manager's keys with a kv store such as redis. I plan on putting an example of this together. Currently, only keys would be able to be stored outside of memory. This probably should be expanded to store nodes outside of memory too, but currently this would be an exercise for the developer when using the RingManager.

Pending Development
  • on visualization and in code for node manager, be able to get weights of nodes (know x% of keys in node N)
  • allow for virtal nodes
  • provide example of using a kv store like redis
  • allow placing of nodes at a given hash id so you can manually balance nodes
  • provide a clear path for rebalancing when you add or remove a node by providing a list of nodes that require migrations of data
Inspiration

See https://github.com/sent-hil/consistenthash

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Debug = false

Debug if true, prints verbose logging

View Source
var ErrNotFound = errors.New("node not found")

Functions

func DefaultHasher

func DefaultHasher(id string) uint32

DefaultHasher uses crc32

func FindInGOPATH

func FindInGOPATH(filename string) string

FindInGOPATH searches through all GOPATHS and attempts to find the given file this is useful here because we want to find chring files but we can't know the relative import path as the importer could be a subpackage

func ServeRing

func ServeRing(r *Ring, addr string)

ServeRing presents a web view into your consistent hash ring

func ServeRingManager

func ServeRingManager(rm *RingManager, addr string)

ServeRingManager presents a web view into your consistent hash ring manager

Types

type Ring

type Ring struct {
	sync.Mutex
	Nodes  nodes
	Hasher func(id string) uint32
}

Ring is a consistent hash ring. Use New() to create a ring. You may change out the hasher function to change key balancing if needed.

func NewRing

func NewRing() *Ring

New creates a new consistent hash ring with a default hashing algo

func (*Ring) Add

func (r *Ring) Add(id string)

Add inserts a new node into the hash ring

func (*Ring) Get

func (r *Ring) Get(key string) string

Get retrievs the closest node in the hash ring for the given key

func (*Ring) Remove

func (r *Ring) Remove(id string) error

type RingManager

type RingManager struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewRingManager

func NewRingManager() *RingManager

func (*RingManager) AddKey

func (rm *RingManager) AddKey(key string) error

func (*RingManager) AddNode

func (rm *RingManager) AddNode(nodeID string) error

func (*RingManager) GetKeys

func (rm *RingManager) GetKeys(nodeID string) (nodes, error)

func (*RingManager) GetNodes

func (rm *RingManager) GetNodes() []string

func (*RingManager) RemoveKey

func (rm *RingManager) RemoveKey(key string) error

func (*RingManager) RemoveNode

func (rm *RingManager) RemoveNode(nodeID string) error

func (*RingManager) SetKeyFetcher

func (rm *RingManager) SetKeyFetcher(fn func(nodeRing, dataRing *Ring, id string) (nodes, error))

SetKeyFetcher allows a user to override the default in memory ring store

func (*RingManager) SetKeyStorer

func (rm *RingManager) SetKeyStorer(fn func(key string) error)

SetKeyStorer allows a user to override the default in memory key store

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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