Improve rarest-first behavior by not favoring a partial piece

held by multiple peers when requesting from a seed
This commit is contained in:
zzz
2012-05-30 15:21:37 +00:00
parent c6fcdf967c
commit 8453c34bfc
2 changed files with 24 additions and 3 deletions

View File

@@ -1146,10 +1146,18 @@ class PeerCoordinator implements PeerListener
PartialPiece pp = iter.next(); PartialPiece pp = iter.next();
int savedPiece = pp.getPiece(); int savedPiece = pp.getPiece();
if (havePieces.get(savedPiece)) { if (havePieces.get(savedPiece)) {
iter.remove();
// this is just a double-check, it should be in there // this is just a double-check, it should be in there
boolean skipped = false;
for(Piece piece : wantedPieces) { for(Piece piece : wantedPieces) {
if (piece.getId() == savedPiece) { if (piece.getId() == savedPiece) {
if (peer.isCompleted() && piece.getPeerCount() > 1) {
// Try to preserve rarest-first when we have only one seeder
// by not preferring a partial piece that others have too
// from a seeder
skipped = true;
break;
}
iter.remove();
piece.setRequested(peer, true); piece.setRequested(peer, true);
if (_log.shouldLog(Log.INFO)) { if (_log.shouldLog(Log.INFO)) {
_log.info("Restoring orphaned partial piece " + pp + _log.info("Restoring orphaned partial piece " + pp +
@@ -1158,8 +1166,12 @@ class PeerCoordinator implements PeerListener
return pp; return pp;
} }
} }
if (_log.shouldLog(Log.WARN)) if (_log.shouldLog(Log.WARN)) {
_log.warn("Partial piece " + pp + " NOT in wantedPieces??"); if (skipped)
_log.warn("Partial piece " + pp + " with multiple peers skipped for seeder");
else
_log.warn("Partial piece " + pp + " NOT in wantedPieces??");
}
} }
} }
if (_log.shouldLog(Log.WARN) && !partialPieces.isEmpty()) if (_log.shouldLog(Log.WARN) && !partialPieces.isEmpty())

View File

@@ -57,6 +57,15 @@ class Piece implements Comparable {
/** caller must synchronize */ /** caller must synchronize */
public boolean removePeer(Peer peer) { return this.peers.remove(peer.getPeerID()); } public boolean removePeer(Peer peer) { return this.peers.remove(peer.getPeerID()); }
/**
* How many peers have this piece?
* Caller must synchronize
* @since 0.9.1
*/
public int getPeerCount() {
return this.peers.size();
}
/** caller must synchronize */ /** caller must synchronize */
public boolean isRequested() { public boolean isRequested() {
return this.requests != null && !this.requests.isEmpty(); return this.requests != null && !this.requests.isEmpty();