storj/satellite/nodeselection/rand.go
Márton Elek 0b02a48a10
satellite/nodeselection: SelectBySubnet should use placement filters for all nodes
Current node selection logic (in case of using SelectBySubnet):

 1. selects one subnet randomly
 2. selects one node randomly from the subnet
 3. applies the placement NodeFilters to the node and ignore it, if doesn't match

This logic is wrong:

 1. Imagine that we have a subnet with two DE and one GB nodes.
 2. We would like to select DE nodes
 2. In case of GB node is selected (randomly) in step2, step3 will ignore the subnet, even if there are good (DE) nodes in there.

Change-Id: I7673f52c89b46e0cc7b20a9b74137dc689d6c17e
2023-08-04 10:48:15 +02:00

43 lines
891 B
Go

// Copyright (C) 2023 Storj Labs, Inc.
// See LICENSE for copying information.
package nodeselection
import mathrand "math/rand"
// RandomOrder as an iterator of a pseudo-random permutation set.
type RandomOrder struct {
count uint64
at uint64
prime uint64
len uint64
}
// NewRandomOrder creates new iterator, returns number between [0,n) in pseudo-random order.
func NewRandomOrder(n int) RandomOrder {
if n == 0 {
return RandomOrder{
count: 0,
}
}
return RandomOrder{
count: uint64(n),
at: uint64(mathrand.Intn(n)),
prime: primes[mathrand.Intn(len(primes))],
len: uint64(n),
}
}
// Next generates the next number.
func (r *RandomOrder) Next() bool {
if r.count == 0 {
return false
}
r.at = (r.at + r.prime) % r.len
r.count--
return true
}
// At returns the current number in the permutations.
func (r *RandomOrder) At() uint64 { return r.at }