Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: eliminate the deadlock issue mentioned in #23 #24

Merged
merged 1 commit into from Nov 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 27 additions & 22 deletions consistent.go
@@ -1,4 +1,4 @@
// Copyright (c) 2018-2021 Burak Sezer
// Copyright (c) 2018-2022 Burak Sezer
// All rights reserved.
//
// This code is licensed under the MIT License.
Expand Down Expand Up @@ -26,30 +26,30 @@
// https://research.googleblog.com/2017/04/consistent-hashing-with-bounded-loads.html
//
// Example Use:
// cfg := consistent.Config{
// PartitionCount: 71,
// ReplicationFactor: 20,
// Load: 1.25,
// Hasher: hasher{},
// }
//
// // Create a new consistent object
// // You may call this with a list of members
// // instead of adding them one by one.
// c := consistent.New(members, cfg)
// cfg := consistent.Config{
// PartitionCount: 71,
// ReplicationFactor: 20,
// Load: 1.25,
// Hasher: hasher{},
// }
//
// // myMember struct just needs to implement a String method.
// // New/Add/Remove distributes partitions among members using the algorithm
// // defined on Google Research Blog.
// c.Add(myMember)
// // Create a new consistent object
// // You may call this with a list of members
// // instead of adding them one by one.
// c := consistent.New(members, cfg)
//
// key := []byte("my-key")
// // LocateKey hashes the key and calculates partition ID with
// // this modulo operation: MOD(hash result, partition count)
// // The owner of the partition is already calculated by New/Add/Remove.
// // LocateKey just returns the member which's responsible for the key.
// member := c.LocateKey(key)
// // myMember struct just needs to implement a String method.
// // New/Add/Remove distributes partitions among members using the algorithm
// // defined on Google Research Blog.
// c.Add(myMember)
//
// key := []byte("my-key")
// // LocateKey hashes the key and calculates partition ID with
// // this modulo operation: MOD(hash result, partition count)
// // The owner of the partition is already calculated by New/Add/Remove.
// // LocateKey just returns the member which's responsible for the key.
// member := c.LocateKey(key)
package consistent

import (
Expand Down Expand Up @@ -285,6 +285,11 @@ func (c *Consistent) GetPartitionOwner(partID int) Member {
c.mu.RLock()
defer c.mu.RUnlock()

return c.getPartitionOwner(partID)
}

// getPartitionOwner returns the owner of the given partition. It's not thread-safe.
func (c *Consistent) getPartitionOwner(partID int) Member {
member, ok := c.partitions[partID]
if !ok {
return nil
Expand All @@ -309,7 +314,7 @@ func (c *Consistent) getClosestN(partID, count int) ([]Member, error) {
}

var ownerKey uint64
owner := c.GetPartitionOwner(partID)
owner := c.getPartitionOwner(partID)
// Hash and sort all the names.
keys := []uint64{}
kmems := make(map[uint64]*Member)
Expand Down