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

AverageLoad() function panics with "divide by zero" when no members are in the hash ring #19

Closed
SQUIDwarrior opened this issue Sep 8, 2021 · 1 comment · Fixed by #25
Assignees
Labels
bug Something isn't working

Comments

@SQUIDwarrior
Copy link

SQUIDwarrior commented Sep 8, 2021

If there are no members in the hashring, calling the AverageLoad() function results in a "divide-by-zero" panic.

Example, this will result in a panic:

package main

import (
	"fmt"

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

type myMember string

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

type hasher struct{}

func (h hasher) Sum64(data []byte) uint64 {
	return xxhash.Sum64(data)
}

func main() {
	cfg := consistent.Config{
		PartitionCount:    7,
		ReplicationFactor: 20,
		Load:              1.25,
		Hasher:            hasher{},
	}
	c := consistent.New(nil, cfg)
        fmt.Printf("average load: %f", c.AverageLoad())
}

The AverageLoad() function should check if the number of members is > 0, and if it is not simply return a fixed value (such as 0 or -1).

@buraksezer buraksezer self-assigned this Sep 15, 2021
@buraksezer buraksezer added the bug Something isn't working label Sep 15, 2021
@Poorunga
Copy link

Poorunga commented Feb 15, 2022

+1

// AverageLoad exposes the current average load.
func (c *Consistent) AverageLoad() float64 {
	avgLoad := float64(c.partitionCount/uint64(len(c.members))) * c.config.Load
	return math.Ceil(avgLoad)
}

len(c.members) may be equal to zero

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants