Movatterモバイル変換


[0]ホーム

URL:


lfchring

packagemodule
v0.0.0-...-443ea75Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2018 License:Apache-2.0Imports:6Imported by:0

Details

Repository

github.com/ckatsak/lfchring

Links

README

lfchring

Go Report CardGoDocGoCover

Package lfchring provides await-free consistent hashing ring immutablein-memory data structure, designed for very efficient frequent reading bymultiple readers and less frequent updates by asingle writer.

It features efficient handling of a static number of virtual ring nodes perdistinct ring node, as well as auto-managed data replication information(using a static replication factor).It also allows users to pass the hash function of their choice, furtherimproving its flexibility.

The API is simple, easy to use, and is documented ingodoc.

It has no external dependencies.

Documentation

Overview

Package lfchring provides an efficient lock-free consistent hashing ringdata structure, designed for frequent reading by multiple readers and lessfrequent updates by a single writer.

It features efficient handling of a static number of virtual ring nodes perdistinct ring node, as well as auto-managed data replication information(using a static replication factor), and an easy-to-use interface.

It also offers to the users flexibility to choose their own hash function,and there is no dependency other than the standard library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

typeHashRing

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

HashRing is a lock-free consistent hashing ring entity, designed forfrequent reads by multiple readers and infrequent updates by one singlewriter. In addition, it features efficient support of virtual ring nodes perdistinct node, as well as "auto-managed" data replication among the distinctnodes.

funcNewHashRing

func NewHashRing(hashFunc func([]byte) []byte, replicationFactor, virtualNodeCountint, nodes ...Node) (*HashRing,error)

NewHashRing returns a new HashRing, properly initialized based on the givenparameters, or a non-nil error value if the parameters are invalid.

An arbitrary number of nodes may optionally be inserted to the new ringduring the initialization through parameter `nodes` (hence, NewHashRing is avariadic function).

func (*HashRing)Clone

func (r *HashRing) Clone() *HashRing

Clone allocates, initializes and returns a new ring, which is a deep copy ofthe original.

func (*HashRing)HasVirtualNode

func (r *HashRing) HasVirtualNode(key []byte)bool

HasVirtualNode returns true if the given key corresponds to a virtual nodein the ring, or false otherwise.

Complexity: O( log(V*N) )

func (*HashRing)Insert

func (r *HashRing) Insert(nodes ...Node) ([]*VirtualNode,error)

Insert is a variadic method to insert an arbitrary number of distinct nodes(i.e. all their virtual nodes) to the ring.

In the case that an already existing distinct node is attempted to bere-inserted to the ring, Insert returns a non-nil error value and the ringis left untouched. Otherwise, the ring is modified as expected, and a sliceof the new virtual nodes (not sorted) is returned.

func (*HashRing)NewVirtualNodesIterator

func (r *HashRing) NewVirtualNodesIterator() *VirtualNodesIterator

NewVirtualNodesIterator returns a new VirtualNodesIterator for efficientlyiterating through ring's virtual nodes in (alphanumerical) order.

func (*HashRing)NewVirtualNodesReverseIterator

func (r *HashRing) NewVirtualNodesReverseIterator() *VirtualNodesReverseIterator

NewVirtualNodesReverseIterator returns a new VirtualNodesReverseIterator forefficiently iterating through ring's virtual nodes in reverse(alphanumerical) order.

func (*HashRing)NodesForKey

func (r *HashRing) NodesForKey(key []byte) []Node

NodesForKey returns a slice of Nodes (of length equal to the configuredreplication factor) that are currently responsible for holding the givenkey.

Complexity: O( log(V*N) )

func (*HashRing)NodesForObject

func (r *HashRing) NodesForObject(readerio.Reader) ([]Node,error)

NodesForObject returns a slice of Nodes (of length equal to the configuredreplication factor) that are currently responsible for holding the objectthat can be read from the given io.Reader (hashing is applied first). Itreturns a non-nil error value in the case of a failure while reading fromthe io.Reader.

Complexity: O( Read ) + O( hash ) + O( log(V*N) )

func (*HashRing)Predecessor

func (r *HashRing) Predecessor(key []byte) (*VirtualNode,error)

Predecessor returns the virtual node which is predecessor to the one thatthe given key would be assigned to. It returns a non-nil error if the ringis empty.

Complexity: O( log(V*N) )

func (*HashRing)PredecessorNode

func (r *HashRing) PredecessorNode(key []byte) (*VirtualNode,error)

PredecessorNode returns the virtual node which is the first predecessor tothe one that the given key would be assigned to, but also belongs to adifferent distinct node than the latter. It returns a non-nil error if thering either is empty or consists of a single distinct node.

Complexity: Worst case O(V*N) but should be O( log(V*N) ) on average.

func (*HashRing)Remove

func (r *HashRing) Remove(nodes ...Node) ([]*VirtualNode,error)

Remove is a variadic method to remove an arbitrary number of distinct nodes(i.e. all their virtual nodes) from the ring.

If any of the distinct nodes' virtual nodes cannot be found in the ring, anon-nil error value is returned and the ring is left untouched; otherwisethe ring is modified as expected, and a slice of the removed virtual nodes(not sorted) is returned.

func (*HashRing)Size

func (r *HashRing) Size()int

Size returns the number of *distinct* nodes in the ring, in its currentstate.

func (*HashRing)String

func (r *HashRing) String()string

String returns the slice of virtual nodes of the current state of the ring,along with their replica owners, as a "print-friendly" string.

func (*HashRing)Successor

func (r *HashRing) Successor(key []byte) (*VirtualNode,error)

Successor returns the virtual node which is successor to the one that thegiven key would be assigned to. It returns a non-nil error if the ring isempty.

Complexity: O( log(V*N) )

func (*HashRing)SuccessorNode

func (r *HashRing) SuccessorNode(key []byte) (*VirtualNode,error)

SuccessorNode returns the virtual node which is the first successor to theone that the given key would be assigned to, but also belongs to a differentdistinct node than the latter. It returns a non-nil error if the ring eitheris empty or consists of a single distinct node.

Complexity: Worst case O(V*N) but should be O( log(V*N) ) on average.

func (*HashRing)VirtualNodeForKey

func (r *HashRing) VirtualNodeForKey(key []byte) *VirtualNode

VirtualNodeForKey returns the virtual node in the ring that the given keywould be assigned to.

Complexity: O( log(V*N) )

func (*HashRing)VirtualNodes

func (r *HashRing) VirtualNodes(stop <-chan struct{}) <-chan *VirtualNode

VirtualNodes allows iteration over all virtual nodes in the ring, byreturning a channel for the caller to read the virtual nodes from.

The stop channel parameter, if used with care, may help avoiding memoryleaks when quitting the iteration early.

BUG: If there is a chance for the returned channel not to be drained (i.e.to quit the iteration early), it is highly recommended to use aVirtualNodesIterator instead, which API, although maybe less comfortable,makes sure there will be no memory leaks (specifically, goroutine leaks) insuch cases.

func (*HashRing)VirtualNodesReversed

func (r *HashRing) VirtualNodesReversed(stop <-chan struct{}) <-chan *VirtualNode

VirtualNodesReversed allows iteration over all virtual nodes in the ring inreverse order, by returning a channel for the caller to read the virtualnodes from.

The stop channel parameter, if used with care, may help avoiding memoryleaks when quitting the iteration early.

BUG: If there is a chance for the returned channel not to be drained (i.e.to quit the iteration early), it is highly recommended to use aVirtualNodesReverseIterator instead, which API, although maybe lesscomfortable, makes sure there will be no memory leaks (specifically,goroutine leaks) in such cases.

typeNode

type Nodestring

Node represents a single distinct node in the ring.

typeVirtualNode

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

VirtualNode represents a single virtual node in the ring.

func (*VirtualNode)Name

func (vn *VirtualNode) Name() []byte

Name returns the "name" of the virtual node as it appears on the ring (i.e.as a key in the key space that the ring operates on).

func (*VirtualNode)Node

func (vn *VirtualNode) Node()Node

Node returns the distinct node that the virtual node represents (or "belongsto").

func (*VirtualNode)String

func (vn *VirtualNode) String()string

String returns a representation of the VirtualNode in a print-friendlyformat.

typeVirtualNodesIterator

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

VirtualNodesIterator is an iterator for efficiently iterating through allvirtual nodes in the ring in (alphanumerical) order.

func (*VirtualNodesIterator)HasNext

func (iter *VirtualNodesIterator) HasNext()bool

HasNext returns true if there is at least one more virtual node in the ringto iterate over, and false if there is none.

The user of VirtualNodesIterator should always check the result of HasNextbefore calling Next to avoid panicking.

func (*VirtualNodesIterator)Next

func (iter *VirtualNodesIterator) Next() *VirtualNode

Next returns the next virtual node of the iteration.

The user of VirtualNodesIterator should always check the result of HasNextbefore calling Next to avoid panicking.

typeVirtualNodesReverseIterator

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

VirtualNodesReverseIterator is an iterator for efficiently iterating throughall virtual nodes in the ring in reverse (alphanumerical) order.

func (*VirtualNodesReverseIterator)HasNext

func (iter *VirtualNodesReverseIterator) HasNext()bool

HasNext returns true if there is at least one more virtual node in the ringto iterate over, and false if there is none.

The user of VirtualNodesReverseIterator should always check the result ofHasNext before calling Next to avoid panicking.

func (*VirtualNodesReverseIterator)Next

Next returns the next virtual node of the (reverse) iteration.

The user of VirtualNodesReverseIterator should always check the result ofHasNext before calling Next to avoid panicking.

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp