Commit 7dd9040c authored by Aarsh Shah's avatar Aarsh Shah

bucket size fncs

parent 552cb4a1
......@@ -196,6 +196,34 @@ func (rt *RoutingTable) peersToValidate() []PeerInfo {
return rt.peersForValidationFnc(peers)
}
// NPeersForCPL returns the number of peers we have for a given Cpl
func (rt *RoutingTable) NPeersForCpl(cpl uint) int {
rt.tabLock.RLock()
defer rt.tabLock.RUnlock()
// it's in the last bucket
if int(cpl) >= len(rt.buckets)-1 {
count := 0
b := rt.buckets[len(rt.buckets)-1]
for _, p := range b.peerIds() {
if CommonPrefixLen(rt.local, ConvertPeerID(p)) == int(cpl) {
count++
}
}
return count
} else {
return rt.buckets[cpl].len()
}
}
// IsBucketFull returns true if the Logical bucket for a given Cpl is full
func (rt *RoutingTable) IsBucketFull(cpl uint) bool {
rt.tabLock.RLock()
defer rt.tabLock.RUnlock()
return rt.NPeersForCpl(cpl) == rt.bucketsize
}
// GetTrackedCplsForRefresh returns the Cpl's we are tracking for refresh.
// Caller is free to modify the returned slice as it is a defensive copy.
func (rt *RoutingTable) GetTrackedCplsForRefresh() []CplRefresh {
......
......@@ -90,6 +90,42 @@ func TestGenRandPeerID(t *testing.T) {
}
}
func TestNPeersForCpl(t *testing.T) {
t.Parallel()
local := test.RandPeerIDFatal(t)
m := pstore.NewMetrics()
rt, err := NewRoutingTable(2, ConvertPeerID(local), time.Hour, m, PeerValidationFnc(PeerAlwaysValidFnc))
require.NoError(t, err)
require.Equal(t, 0, rt.NPeersForCpl(0))
require.Equal(t, 0, rt.NPeersForCpl(1))
// one peer with cpl 1
p, _ := rt.GenRandPeerID(1)
rt.HandlePeerAlive(p)
require.Equal(t, 0, rt.NPeersForCpl(0))
require.Equal(t, 1, rt.NPeersForCpl(1))
require.Equal(t, 0, rt.NPeersForCpl(2))
// one peer with cpl 0
p, _ = rt.GenRandPeerID(0)
rt.HandlePeerAlive(p)
require.Equal(t, 1, rt.NPeersForCpl(0))
require.Equal(t, 1, rt.NPeersForCpl(1))
require.Equal(t, 0, rt.NPeersForCpl(2))
// split the bucket with a peer with cpl 1
p, _ = rt.GenRandPeerID(1)
rt.HandlePeerAlive(p)
require.Equal(t, 1, rt.NPeersForCpl(0))
require.Equal(t, 2, rt.NPeersForCpl(1))
require.Equal(t, 0, rt.NPeersForCpl(2))
p, _ = rt.GenRandPeerID(0)
rt.HandlePeerAlive(p)
require.Equal(t, 2, rt.NPeersForCpl(0))
}
func TestRefreshAndGetTrackedCpls(t *testing.T) {
t.Parallel()
local := test.RandPeerIDFatal(t)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment