Skip to content

Commit

Permalink
FAB-1723 Fix peers filtering for gossip push/pull
Browse files Browse the repository at this point in the history
File gossip/filter/filter.go

Method SelectPeer - in case of small k, log(membership) for example,
and many small channels in most cases it will return empty list -
as first step it choose k peers from membership and only after that
filter them, as result it will remain with zero peers

Correct way to implement it is filter peers first and only after that
choose k peers from filtered list.

Change-Id: I1577df37cc8441adf7e96a0e847dc901afd60ea8
Signed-off-by: Gennady Laventman <gennady@il.ibm.com>
  • Loading branch information
gennadylaventman committed Jan 18, 2017
1 parent 48d8df9 commit 121cf99
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions gossip/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,27 @@ func CombineRoutingFilters(filters ...RoutingFilter) RoutingFilter {

// SelectPeers returns a slice of peers that match a list of routing filters
func SelectPeers(k int, peerPool []discovery.NetworkMember, filters ...RoutingFilter) []*comm.RemotePeer {
var filteredPeers []*comm.RemotePeer
for _, peer := range peerPool {
if CombineRoutingFilters(filters ...)(peer) {
filteredPeers = append(filteredPeers, &comm.RemotePeer{PKIID: peer.PKIid, Endpoint: peer.Endpoint})
}
}

var indices []int
if len(peerPool) <= k {
indices = make([]int, len(peerPool))
for i := 0; i < len(peerPool); i++ {
if len(filteredPeers) <= k {
indices = make([]int, len(filteredPeers))
for i := 0; i < len(filteredPeers); i++ {
indices[i] = i
}
} else {
indices = util.GetRandomIndices(k, len(peerPool)-1)
indices = util.GetRandomIndices(k, len(filteredPeers)-1)
}

var remotePeers []*comm.RemotePeer
for _, index := range indices {
peer := peerPool[index]
if CombineRoutingFilters(filters ...)(peer) {
remotePeers = append(remotePeers, &comm.RemotePeer{PKIID: peer.PKIid, Endpoint: peer.Endpoint})
}

remotePeers = append(remotePeers, filteredPeers[index])
}

return remotePeers
}

0 comments on commit 121cf99

Please sign in to comment.