Skip to content

A corner case will cause panic. #13

@ShiKaiWi

Description

@ShiKaiWi

This demo will panic:

package main

import (
	"github.com/buraksezer/consistent"
	"github.com/cespare/xxhash"
)

type hasher struct{}

type myMember string

func (m myMember) String() string {
	return string(m)
}

func (h hasher) Sum64(data []byte) uint64 {
	// you should use a proper hash function for uniformity.
	return xxhash.Sum64(data)
}

func main() {
	cfg := consistent.Config{
		PartitionCount:    1,
		ReplicationFactor: 1,
		Load:              1,
		Hasher:            hasher{},
	}
	c := consistent.New(nil, cfg)

	node1 := myMember("node1")
	c.Add(node1)
}

Here is the error message:

panic: not enough room to distribute partitions

goroutine 1 [running]:
github.com/buraksezer/consistent.(*Consistent).distributeWithLoad(0x450060, 0x0, 0x0, 0x43e2e0, 0x43e2c0, 0x34c96acd)
	/tmp/gopath552882815/pkg/mod/github.com/buraksezer/consistent@v0.0.0-20191006190839-693edf70fd72/consistent.go:165 +0x3a0
github.com/buraksezer/consistent.(*Consistent).distributePartitions(0x450060, 0x1604d0)
	/tmp/gopath552882815/pkg/mod/github.com/buraksezer/consistent@v0.0.0-20191006190839-693edf70fd72/consistent.go:196 +0xc0
github.com/buraksezer/consistent.(*Consistent).Add(0x450060, 0x1604d0, 0x40c150, 0x2b5f)
	/tmp/gopath552882815/pkg/mod/github.com/buraksezer/consistent@v0.0.0-20191006190839-693edf70fd72/consistent.go:227 +0x180
main.main()
	/tmp/sandbox195891616/prog.go:30 +0xe0

So this is expected behavior or a bug?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions