Fix in rarestFirst implemenation.

If there are more than one rarest blocks, pick randomly between them (was deterministic before)
This commit is contained in:
Mosharaf Chowdhury 2011-02-10 20:37:44 -08:00
parent 520bbdc7e3
commit ca2895ebb0

View file

@ -726,19 +726,30 @@ extends Broadcast[T] with Logging {
}
}
// Find the block with the minimum copies that this peer does not have
// Find the minimum
var minVal = Integer.MAX_VALUE
var minIndex = -1
for (i <- 0 until totalBlocks) {
if (needBlocksBitVector.get (i) &&
numCopiesPerBlock(i) > 0 &&
numCopiesPerBlock(i) < minVal) {
if (numCopiesPerBlock(i) > 0 && numCopiesPerBlock(i) < minVal) {
minVal = numCopiesPerBlock(i)
minIndex = i
}
}
return minIndex
// Find the blocks with the least copies that this peer does not have
var minBlocksIndices = ListBuffer[Int] ()
for (i <- 0 until totalBlocks) {
if (needBlocksBitVector.get (i) && numCopiesPerBlock(i) == minVal) {
minBlocksIndices += i
}
}
// Now select a random index from minBlocksIndices
if (minBlocksIndices.size == 0) {
return -1
} else {
// Pick uniformly the i'th index
var i = BitTorrentBroadcast.ranGen.nextInt (minBlocksIndices.size)
return minBlocksIndices(i)
}
}
}