Compare commits
1 Commits
2bf699909b
...
e77a3a9868
| Author | SHA1 | Date | |
|---|---|---|---|
|
e77a3a9868
|
@@ -5,12 +5,10 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
"sync"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConsistentHashRing implements a consistent hash ring for shard distribution
|
// ConsistentHashRing implements a consistent hash ring for shard distribution
|
||||||
type ConsistentHashRing struct {
|
type ConsistentHashRing struct {
|
||||||
mu sync.RWMutex
|
|
||||||
ring map[uint32]string // hash -> node ID
|
ring map[uint32]string // hash -> node ID
|
||||||
sortedHashes []uint32 // sorted hash keys
|
sortedHashes []uint32 // sorted hash keys
|
||||||
nodes map[string]bool // active nodes
|
nodes map[string]bool // active nodes
|
||||||
@@ -37,9 +35,6 @@ func NewConsistentHashRingWithConfig(config HashRingConfig) *ConsistentHashRing
|
|||||||
|
|
||||||
// AddNode adds a node to the hash ring
|
// AddNode adds a node to the hash ring
|
||||||
func (chr *ConsistentHashRing) AddNode(nodeID string) {
|
func (chr *ConsistentHashRing) AddNode(nodeID string) {
|
||||||
chr.mu.Lock()
|
|
||||||
defer chr.mu.Unlock()
|
|
||||||
|
|
||||||
if chr.nodes[nodeID] {
|
if chr.nodes[nodeID] {
|
||||||
return // Node already exists
|
return // Node already exists
|
||||||
}
|
}
|
||||||
@@ -61,9 +56,6 @@ func (chr *ConsistentHashRing) AddNode(nodeID string) {
|
|||||||
|
|
||||||
// RemoveNode removes a node from the hash ring
|
// RemoveNode removes a node from the hash ring
|
||||||
func (chr *ConsistentHashRing) RemoveNode(nodeID string) {
|
func (chr *ConsistentHashRing) RemoveNode(nodeID string) {
|
||||||
chr.mu.Lock()
|
|
||||||
defer chr.mu.Unlock()
|
|
||||||
|
|
||||||
if !chr.nodes[nodeID] {
|
if !chr.nodes[nodeID] {
|
||||||
return // Node doesn't exist
|
return // Node doesn't exist
|
||||||
}
|
}
|
||||||
@@ -84,9 +76,6 @@ func (chr *ConsistentHashRing) RemoveNode(nodeID string) {
|
|||||||
|
|
||||||
// GetNode returns the node responsible for a given key
|
// GetNode returns the node responsible for a given key
|
||||||
func (chr *ConsistentHashRing) GetNode(key string) string {
|
func (chr *ConsistentHashRing) GetNode(key string) string {
|
||||||
chr.mu.RLock()
|
|
||||||
defer chr.mu.RUnlock()
|
|
||||||
|
|
||||||
if len(chr.sortedHashes) == 0 {
|
if len(chr.sortedHashes) == 0 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@@ -114,9 +103,6 @@ func (chr *ConsistentHashRing) hash(key string) uint32 {
|
|||||||
|
|
||||||
// GetNodes returns all active nodes in the ring
|
// GetNodes returns all active nodes in the ring
|
||||||
func (chr *ConsistentHashRing) GetNodes() []string {
|
func (chr *ConsistentHashRing) GetNodes() []string {
|
||||||
chr.mu.RLock()
|
|
||||||
defer chr.mu.RUnlock()
|
|
||||||
|
|
||||||
nodes := make([]string, 0, len(chr.nodes))
|
nodes := make([]string, 0, len(chr.nodes))
|
||||||
for nodeID := range chr.nodes {
|
for nodeID := range chr.nodes {
|
||||||
nodes = append(nodes, nodeID)
|
nodes = append(nodes, nodeID)
|
||||||
@@ -126,9 +112,6 @@ func (chr *ConsistentHashRing) GetNodes() []string {
|
|||||||
|
|
||||||
// IsEmpty returns true if the ring has no nodes
|
// IsEmpty returns true if the ring has no nodes
|
||||||
func (chr *ConsistentHashRing) IsEmpty() bool {
|
func (chr *ConsistentHashRing) IsEmpty() bool {
|
||||||
chr.mu.RLock()
|
|
||||||
defer chr.mu.RUnlock()
|
|
||||||
|
|
||||||
return len(chr.nodes) == 0
|
return len(chr.nodes) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user