addMemberToRing adds a member to the consistent hash ring and updates the sorted set of hashes.
(member Member)
| 258 | |
| 259 | // addMemberToRing adds a member to the consistent hash ring and updates the sorted set of hashes. |
| 260 | func (c *Consistent) addMemberToRing(member Member) { |
| 261 | // Add replication factor entries for the member in the hash ring. |
| 262 | for replicaIndex := 0; replicaIndex < c.config.ReplicationFactor; replicaIndex++ { |
| 263 | // Generate a unique key for each replica of the member. |
| 264 | replicaKey := []byte(fmt.Sprintf("%s%d", member.String(), replicaIndex)) |
| 265 | hashValue := c.hasher(replicaKey) |
| 266 | |
| 267 | // Add the hash value to the ring and associate it with the member. |
| 268 | c.ring[hashValue] = &member |
| 269 | |
| 270 | // Append the hash value to the sorted set of hashes. |
| 271 | c.sortedSet = append(c.sortedSet, hashValue) |
| 272 | } |
| 273 | |
| 274 | // Sort the hash values to maintain the ring's order. |
| 275 | sort.Slice(c.sortedSet, func(i, j int) bool { |
| 276 | return c.sortedSet[i] < c.sortedSet[j] |
| 277 | }) |
| 278 | |
| 279 | // Add the member to the members map. |
| 280 | c.members[member.String()] = &member |
| 281 | } |
| 282 | |
| 283 | // Add safely adds a new member to the consistent hash circle. |
| 284 | // It ensures thread safety and redistributes partitions after adding the member. |