chore: align slurp config and scaffolding

This commit is contained in:
anthonyrawlins
2025-09-27 21:03:12 +10:00
parent acc4361463
commit 4a77862289
47 changed files with 5133 additions and 4274 deletions

View File

@@ -40,7 +40,7 @@ func (ch *ConsistentHashingImpl) AddNode(nodeID string) error {
for i := 0; i < ch.virtualNodes; i++ {
virtualNodeKey := fmt.Sprintf("%s:%d", nodeID, i)
hash := ch.hashKey(virtualNodeKey)
ch.ring[hash] = nodeID
ch.sortedHashes = append(ch.sortedHashes, hash)
}
@@ -88,7 +88,7 @@ func (ch *ConsistentHashingImpl) GetNode(key string) (string, error) {
}
hash := ch.hashKey(key)
// Find the first node with hash >= key hash
idx := sort.Search(len(ch.sortedHashes), func(i int) bool {
return ch.sortedHashes[i] >= hash
@@ -175,7 +175,7 @@ func (ch *ConsistentHashingImpl) GetNodeDistribution() map[string]float64 {
// Calculate the range each node is responsible for
for i, hash := range ch.sortedHashes {
nodeID := ch.ring[hash]
var rangeSize uint64
if i == len(ch.sortedHashes)-1 {
// Last hash wraps around to first
@@ -230,7 +230,7 @@ func (ch *ConsistentHashingImpl) calculateLoadBalance() float64 {
}
avgVariance := totalVariance / float64(len(distribution))
// Convert to a balance score (higher is better, 1.0 is perfect)
// Use 1/(1+variance) to map variance to [0,1] range
return 1.0 / (1.0 + avgVariance/100.0)
@@ -261,11 +261,11 @@ func (ch *ConsistentHashingImpl) GetMetrics() *ConsistentHashMetrics {
defer ch.mu.RUnlock()
return &ConsistentHashMetrics{
TotalKeys: 0, // Would be maintained by usage tracking
NodeUtilization: ch.GetNodeDistribution(),
RebalanceEvents: 0, // Would be maintained by event tracking
AverageSeekTime: 0.1, // Placeholder - would be measured
LoadBalanceScore: ch.calculateLoadBalance(),
TotalKeys: 0, // Would be maintained by usage tracking
NodeUtilization: ch.GetNodeDistribution(),
RebalanceEvents: 0, // Would be maintained by event tracking
AverageSeekTime: 0.1, // Placeholder - would be measured
LoadBalanceScore: ch.calculateLoadBalance(),
LastRebalanceTime: 0, // Would be maintained by event tracking
}
}
@@ -306,7 +306,7 @@ func (ch *ConsistentHashingImpl) addNodeUnsafe(nodeID string) error {
for i := 0; i < ch.virtualNodes; i++ {
virtualNodeKey := fmt.Sprintf("%s:%d", nodeID, i)
hash := ch.hashKey(virtualNodeKey)
ch.ring[hash] = nodeID
ch.sortedHashes = append(ch.sortedHashes, hash)
}
@@ -333,7 +333,7 @@ func (ch *ConsistentHashingImpl) SetVirtualNodeCount(count int) error {
defer ch.mu.Unlock()
ch.virtualNodes = count
// Rehash with new virtual node count
return ch.Rehash()
}
@@ -364,8 +364,8 @@ func (ch *ConsistentHashingImpl) FindClosestNodes(key string, count int) ([]stri
if hash >= keyHash {
distance = hash - keyHash
} else {
// Wrap around distance
distance = (1<<32 - keyHash) + hash
// Wrap around distance without overflowing 32-bit space
distance = uint32((uint64(1)<<32 - uint64(keyHash)) + uint64(hash))
}
distances = append(distances, struct {
@@ -397,4 +397,4 @@ func (ch *ConsistentHashingImpl) FindClosestNodes(key string, count int) ([]stri
}
return nodes, hashes, nil
}
}