Remove deletes a member from the consistent hash circle and redistributes partitions. If the member does not exist, the method exits early.
(memberName string)
| 311 | // Remove deletes a member from the consistent hash circle and redistributes partitions. |
| 312 | // If the member does not exist, the method exits early. |
| 313 | func (c *Consistent) Remove(memberName string) { |
| 314 | // Acquire a write lock to ensure thread-safe access. |
| 315 | c.mu.Lock() |
| 316 | defer c.mu.Unlock() |
| 317 | |
| 318 | // Check if the member exists in the hash ring. If not, exit early. |
| 319 | if _, exists := c.members[memberName]; !exists { |
| 320 | return |
| 321 | } |
| 322 | |
| 323 | // Remove all replicas of the member from the hash ring and sorted set. |
| 324 | for replicaIndex := 0; replicaIndex < c.config.ReplicationFactor; replicaIndex++ { |
| 325 | // Generate the unique key for each replica of the member. |
| 326 | replicaKey := []byte(fmt.Sprintf("%s%d", memberName, replicaIndex)) |
| 327 | hashValue := c.hasher(replicaKey) |
| 328 | |
| 329 | // Remove the hash value from the hash ring. |
| 330 | delete(c.ring, hashValue) |
| 331 | |
| 332 | // Remove the hash value from the sorted set. |
| 333 | c.removeFromSortedSet(hashValue) |
| 334 | } |
| 335 | |
| 336 | // Remove the member from the members map. |
| 337 | delete(c.members, memberName) |
| 338 | |
| 339 | // If no members remain, reset the partition table and exit. |
| 340 | if len(c.members) == 0 { |
| 341 | c.partitions = make(map[int]*Member) |
| 342 | return |
| 343 | } |
| 344 | |
| 345 | // Redistribute partitions among the remaining members. |
| 346 | c.distributePartitions() |
| 347 | } |
| 348 | |
| 349 | // GetLoadDistribution provides a thread-safe snapshot of the current load distribution across members. |
| 350 | // It returns a map where the keys are member identifiers and the values are their respective loads. |
no test coverage detected