Verified Commit 7a879786 authored by meskio's avatar meskio 🏔️
Browse files

Give resources if they are in the hashring

If GetMany is called asking for more resources than there are in the
hashring let's return the resources that we have instead of giving an
error. So in situations of having few resources the distributors keep
working instead of failing.

Closes: #162
parent 6f504c40
Loading
Loading
Loading
Loading
+8 −5
Original line number Diff line number Diff line
@@ -307,16 +307,19 @@ func (h *Hashring) GetExact(k Hashkey) (Resource, error) {

// GetMany behaves like Get with the exception that it attempts to return the
// given number of elements.  If the number of desired elements exceeds the
// number of elements in the hashring, an error is returned.
func (h *Hashring) GetMany(k Hashkey, num int) ([]Resource, error) {
// number of elements in the hashring all the resources in the hashring will
// be returned.
func (h *Hashring) GetMany(k Hashkey, num int) (resources []Resource, err error) {
	h.RLock()
	defer h.RUnlock()

	if num > h.Len() {
		return nil, errors.New("requested more elements than hashring has")
	if h.Len() == 0 {
		return nil, errors.New("Hashring is empty")
	}
	if num >= h.Len() {
		num = h.Len()
	}

	var resources []Resource
	i, err := h.getIndex(k)
	if err != nil && i == -1 {
		return nil, err
+2 −2
Original line number Diff line number Diff line
@@ -129,8 +129,8 @@ func TestGetMany(t *testing.T) {
	h.Add(d1)
	h.Add(d2)
	h.Add(d3)
	if _, err := h.GetMany(0, 4); err == nil {
		t.Error("requesting more elements than present should result in error")
	if elems, _ := h.GetMany(0, 4); len(elems) != 3 {
		t.Error("requesting more elements than present should return all the elements in the hashring")
	}

	numElems := 3