hash

package
v0.4.6 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const TopWeight = 100

TopWeight is the maximum weight for a node in the consistent hashing ring. Higher weights indicate higher priority for the node.

Variables

This section is empty.

Functions

func Hash

func Hash(data []byte) uint64

Hash returns the hash value of data.

Types

type Consistent

type Consistent interface {
	// Add inserts a node into the consistent hashing ring with the
	// default number of replicas and weight.
	Add(node string)

	// SyncNodes updates the consistent hashing ring with the current
	// state of the gossip cluster.
	SyncNodes(g *gossip.Gossip)

	// AddWithReplicas inserts a node into the consistent hashing ring
	// with the specified number of replicas and default weight.
	AddWithReplicas(node string, replicas int)

	// AddWithWeight inserts a node into the consistent hashing ring
	// with the specified weight and default number of replicas.
	AddWithWeight(node string, weight int)

	// Get finds the responsible node for the given key in the consistent
	// hashing ring. If a node is found, it returns the node and true;
	// otherwise, it returns an empty string and false.
	Get(v string) (string, bool)

	// Remove deletes a node from the consistent hashing ring.
	Remove(node string)

	// AddKey adds the given key to the consistent hashing ring and
	// returns true if the key was successfully added; otherwise, it
	// returns false.
	AddKey(key string) bool
}

Consistent is an interface that represents a consistent hashing ring with support for node weights and replicas. Implementations of this interface should provide methods for adding, updating, and removing nodes, as well as retrieving the responsible node for a given key.

type ConsistentHash

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

ConsistentHash is a struct that implements a consistent hashing algorithm with support for node replicas. It contains the following fields:

  • hashFunc: a Func type that represents the hash function to use for keys.
  • replicas: the number of replicas for each node in the consistent hashing ring.
  • keys: a slice of 64-bit unsigned integers representing the sorted hash keys.
  • ring: a map that associates hash keys with their corresponding node names.
  • nodes: a map that stores the nodes' names as keys with empty struct values (used for quick membership tests).
  • lock: a sync.RWMutex that provides synchronization for concurrent access.

func NewConsistentHash

func NewConsistentHash(replicas int, nodes []string, fn Func) *ConsistentHash

NewConsistentHash creates a new ConsistentHash instance with the specified number of replicas, initial node list, and hash function. If the number of replicas is less than minReplicas, it will be set to minReplicas. If the hash function is not provided (nil), the default Hash function will be used. This function returns a pointer to the newly created ConsistentHash instance.

func (*ConsistentHash) Add

func (h *ConsistentHash) Add(node string)

Add inserts a node into the ConsistentHash instance with the default number of replicas. It calls AddWithReplicas, passing the node and the number of replicas stored in the ConsistentHash instance.

func (*ConsistentHash) AddKey

func (h *ConsistentHash) AddKey(key string) bool

AddKey inserts a key into the ConsistentHash instance, associating it with the appropriate node. It calculates the hash of the key, finds the responsible node using the Get method, and adds the hash and node to the keys slice and the ring. It then sorts the keys slice and returns true.

func (*ConsistentHash) AddWithReplicas

func (h *ConsistentHash) AddWithReplicas(node string, replicas int)

AddWithReplicas inserts a node into the ConsistentHash instance with the specified number of replicas. If the number of replicas is greater than the maximum allowed by the instance, it will be capped. It also updates the keys and ring with the hash values for the node's replicas and sorts the keys slice.

func (*ConsistentHash) AddWithWeight

func (h *ConsistentHash) AddWithWeight(node string, weight int)

AddWithWeight inserts a node into the ConsistentHash instance with a specified weight. The number of replicas is calculated based on the weight and the maximum allowed replicas. If the resulting number of replicas is less than 1, it will be set to 1. It calls AddWithReplicas with the calculated number of replicas.

func (*ConsistentHash) Get

func (h *ConsistentHash) Get(v string) (string, bool)

Get retrieves the node that is responsible for the specified key from the ConsistentHash instance. It calculates the hash of the key, finds the index of the smallest hash in the keys slice that is greater than or equal to the key hash, and returns the corresponding node from the ring. If the ring is empty, it returns an empty string and false.

func (*ConsistentHash) Remove

func (h *ConsistentHash) Remove(node string)

Remove deletes a node from the ConsistentHash instance. It removes the node's replicas from the keys slice and the ring. It also removes the node from the nodes map.

func (*ConsistentHash) SyncNodes

func (h *ConsistentHash) SyncNodes(g *gossip.Gossip)

SyncNodes updates the ConsistentHash instance with the current state of the gossip cluster. It adds new nodes from the gossip cluster with the default weight and removes nodes that are no longer in the cluster.

type Func

type Func func(data []byte) uint64

Func is a function type that takes a byte slice as input and returns a 64-bit unsigned integer hash value.

Jump to

Keyboard shortcuts

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