Skip to content

Commit

Permalink
refactor: add default values to the configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
buraksezer committed Nov 9, 2022
1 parent 7eb5636 commit 4516339
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
41 changes: 26 additions & 15 deletions consistent.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

// Package consistent provides a consistent hashing function with bounded loads.
// For more information about the underlying algorithm, please take a look at
// https://research.googleblog.com/2017/04/consistent-hashing-with-bounded-loads.html
// Package consistent provides a consistent hashing function with bounded loads. This implementation also adds
// partitioning logic on top of the original algorithm. For more information about the underlying algorithm,
// please take a look at https://research.googleblog.com/2017/04/consistent-hashing-with-bounded-loads.html
//
// Example Use:
//
Expand Down Expand Up @@ -65,15 +65,16 @@ import (
"sync"
)

var (
// ErrInsufficientMemberCount represents an error which means there are not enough members to complete the task.
ErrInsufficientMemberCount = errors.New("insufficient member count")

// ErrMemberNotFound represents an error which means requested member could not be found in consistent hash ring.
ErrMemberNotFound = errors.New("member could not be found in ring")
const (
DefaultPartitionCount int = 271
DefaultReplicationFactor int = 20
DefaultLoad float64 = 1.25
)

// Hasher is responsible for generating unsigned, 64 bit hash of provided byte slice.
// ErrInsufficientMemberCount represents an error which means there are not enough members to complete the task.
var ErrInsufficientMemberCount = errors.New("insufficient member count")

// Hasher is responsible for generating unsigned, 64-bit hash of provided byte slice.
// Hasher should minimize collisions (generating same hash for different byte slice)
// and while performance is also important fast functions are preferable (i.e.
// you can use FarmHash family).
Expand All @@ -88,7 +89,7 @@ type Member interface {

// Config represents a structure to control consistent package.
type Config struct {
// Hasher is responsible for generating unsigned, 64 bit hash of provided byte slice.
// Hasher is responsible for generating unsigned, 64-bit hash of provided byte slice.
Hasher Hasher

// Keys are distributed among partitions. Prime numbers are good to
Expand Down Expand Up @@ -120,16 +121,26 @@ type Consistent struct {

// New creates and returns a new Consistent object.
func New(members []Member, config Config) *Consistent {
if config.Hasher == nil {
panic("Hasher cannot be nil")
}
if config.PartitionCount == 0 {
config.PartitionCount = DefaultPartitionCount
}
if config.ReplicationFactor == 0 {
config.ReplicationFactor = DefaultReplicationFactor
}
if config.Load == 0 {
config.Load = DefaultLoad
}

c := &Consistent{
config: config,
members: make(map[string]*Member),
partitionCount: uint64(config.PartitionCount),
ring: make(map[uint64]*Member),
}
if config.Hasher == nil {
panic("Hasher cannot be nil")
}
// TODO: Check configuration here

c.hasher = config.Hasher
for _, member := range members {
c.add(member)
Expand Down
10 changes: 5 additions & 5 deletions consistent_test.go
Original file line number Diff line number Diff line change
@@ -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 @@ -76,7 +76,7 @@ func TestConsistentAdd(t *testing.T) {
}

func TestConsistentRemove(t *testing.T) {
members := []Member{}
var members []Member
for i := 0; i < 8; i++ {
member := testMember(fmt.Sprintf("node%d.olric", i))
members = append(members, member)
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestConsistentLocateKey(t *testing.T) {
}

func TestConsistentInsufficientMemberCount(t *testing.T) {
members := []Member{}
var members []Member
for i := 0; i < 8; i++ {
member := testMember(fmt.Sprintf("node%d.olric", i))
members = append(members, member)
Expand All @@ -159,7 +159,7 @@ func TestConsistentInsufficientMemberCount(t *testing.T) {
}

func TestConsistentClosestMembers(t *testing.T) {
members := []Member{}
var members []Member
for i := 0; i < 8; i++ {
member := testMember(fmt.Sprintf("node%d.olric", i))
members = append(members, member)
Expand Down Expand Up @@ -215,6 +215,6 @@ func BenchmarkGetClosestN(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
key := []byte("key" + strconv.Itoa(i))
c.GetClosestN(key, 3)
_, _ = c.GetClosestN(key, 3)
}
}

0 comments on commit 4516339

Please sign in to comment.