merge in the parts of peerutils' peer pruning code that still make sense

This commit is contained in:
Charles Kerr 2007-10-13 13:54:05 +00:00
parent 4b466a8d24
commit 9211c57ad2
4 changed files with 71 additions and 343 deletions

View File

@ -54,10 +54,15 @@ enum
* we're we don't get piece data from a peer in this long */
SNUBBED_SEC = 60,
/* this is arbitrary and, hopefully, temporary until we come up
* with a better idea for managing the connection limits */
/* arbitrary */
MAX_CONNECTED_PEERS_PER_TORRENT = 60,
/* when many peers are available, keep idle ones this long */
MIN_UPLOAD_IDLE_SECS = 60,
/* when few peers are available, keep idle ones this long */
MAX_UPLOAD_IDLE_SECS = 240,
/* how many peers to unchoke per-torrent. */
/* FIXME: make this user-configurable? */
NUM_UNCHOKED_PEERS_PER_TORRENT = 12, /* arbitrary */
@ -1479,53 +1484,69 @@ rechokePulse( void * vtorrent )
/***
****
****
**** Life and Death
****
***/
#define LAISSEZ_FAIRE_PERIOD_SECS 90
static int
shouldPeerBeClosed( const Torrent * t, const tr_peer * peer, int peerCount )
{
const tr_torrent * tor = t->tor;
const time_t now = time( NULL );
const struct peer_atom * atom = getExistingAtom( t, &peer->in_addr );
/* if it's marked for purging, close it */
if( peer->doPurge ) {
tordbg( t, "purging peer %p because its doPurge flag is set", tr_peerIoAddrStr(&atom->addr,atom->port) );
return TRUE;
}
/* if we're both seeds and it's been long enough for a pex exchange, close it */
if( 1 ) {
const int clientIsSeed = tr_cpGetStatus( tor->completion ) != TR_CP_INCOMPLETE;
const int peerIsSeed = atom->flags & ADDED_F_SEED_FLAG;
if( peerIsSeed && clientIsSeed && ( tor->pexDisabled || (now-atom->time>=30) ) ) {
tordbg( t, "purging peer %p because we're both seeds", tr_peerIoAddrStr(&atom->addr,atom->port) );
return TRUE;
}
}
/* disconnect if it's been too long since piece data has been transferred.
* this is on a sliding scale based on number of available peers... */
if( 1 ) {
const int relaxStrictnessIfFewerThanN = (int)((MAX_CONNECTED_PEERS_PER_TORRENT * 0.9) + 0.5);
/* if we have >= relaxIfFewerThan, strictness is 100%.
* if we have zero connections, strictness is 0% */
const double strictness = peerCount >= relaxStrictnessIfFewerThanN
? 1.0
: peerCount / (double)relaxStrictnessIfFewerThanN;
const int lo = MIN_UPLOAD_IDLE_SECS;
const int hi = MAX_UPLOAD_IDLE_SECS;
const int limit = lo + ((hi-lo) * strictness);
const time_t then = peer->pieceDataActivityDate;
const int idleTime = then ? (now-then) : 0;
if( idleTime > limit ) {
tordbg( t, "purging peer %p it's been %d secs since we shared anything",
tr_peerIoAddrStr(&atom->addr,atom->port), idleTime );
return TRUE;
}
}
return FALSE;
}
static tr_peer **
getWeakConnections( Torrent * t, int * setmeSize )
getPeersToClose( Torrent * t, int * setmeSize )
{
int i, insize, outsize;
tr_peer ** peers = (tr_peer**) tr_ptrArrayPeek( t->peers, &insize );
struct tr_peer ** ret = tr_new( tr_peer*, insize );
const int clientIsSeed = tr_cpGetStatus( t->tor->completion ) != TR_CP_INCOMPLETE;
const time_t now = time( NULL );
int i, peerCount, outsize;
tr_peer ** peers = (tr_peer**) tr_ptrArrayPeek( t->peers, &peerCount );
struct tr_peer ** ret = tr_new( tr_peer*, peerCount );
assert( torrentIsLocked( t ) );
for( i=outsize=0; i<insize; ++i )
{
tr_peer * peer = peers[i];
int isWeak;
const struct peer_atom * atom = getExistingAtom( t, &peer->in_addr );
const int peerIsSeed = atom->flags & ADDED_F_SEED_FLAG;
const double throughput = getWeightedThroughput( peer );
assert( atom != NULL );
if( peer->doPurge ) {
tordbg( t, "purging peer %s because it's got doPurge flagged", tr_peerIoAddrStr(&atom->addr,atom->port) );
isWeak = TRUE;
} else if( throughput >= 3 ) {
tordbg( t, "keeping peer %s because it's got a good throughput", tr_peerIoAddrStr(&atom->addr,atom->port) );
isWeak = FALSE;
} else if( peerIsSeed && clientIsSeed ) {
tordbg( t, "%s peer is a seed and so are we", tr_peerIoAddrStr(&atom->addr,atom->port) );
isWeak = t->tor->pexDisabled || (now-atom->time>=30);
} else if( !atom->time || ( ( now - atom->time ) < LAISSEZ_FAIRE_PERIOD_SECS ) ) {
tordbg( t, "%s too new to purge", tr_peerIoAddrStr(&atom->addr,atom->port) );
isWeak = FALSE;
} else {
isWeak = ( now - peer->pieceDataActivityDate ) > 180;
tordbg( t, "%s peer %p", (isWeak?"purging":"keeping"), tr_peerIoAddrStr(&atom->addr,atom->port) );
}
if( isWeak )
ret[outsize++] = peer;
}
for( i=outsize=0; i<peerCount; ++i )
if( shouldPeerBeClosed( t, peers[i], peerCount ) )
ret[outsize++] = peers[i];
*setmeSize = outsize;
return ret;
@ -1581,7 +1602,7 @@ getPeerCandidates( Torrent * t, int * setmeSize )
}
/* if we used this peer recently, give someone else a turn */
if( ( now - atom->time ) < LAISSEZ_FAIRE_PERIOD_SECS ) {
if( ( now - atom->time ) < 60 ) {
tordbg( t, "RECONNECT peer %d (%s) is in its grace period..",
i, tr_peerIoAddrStr(&atom->addr,atom->port) );
continue;
@ -1608,20 +1629,20 @@ reconnectPulse( void * vtorrent )
}
else
{
int i, nCandidates, nWeak, nAdd;
int i, nCandidates, nBad, nAdd;
struct peer_atom ** candidates = getPeerCandidates( t, &nCandidates );
struct tr_peer ** connections = getWeakConnections( t, &nWeak );
struct tr_peer ** connections = getPeersToClose( t, &nBad );
const int peerCount = tr_ptrArraySize( t->peers );
if( nWeak || nCandidates )
tordbg( t, "reconnect pulse for [%s]: %d weak connections, "
if( nBad || nCandidates )
tordbg( t, "reconnect pulse for [%s]: %d bad connections, "
"%d connection candidates, %d atoms, max per pulse is %d",
t->tor->info.name, nWeak, nCandidates,
t->tor->info.name, nBad, nCandidates,
tr_ptrArraySize(t->pool),
(int)MAX_RECONNECTIONS_PER_PULSE );
/* disconnect some peers */
for( i=0; i<nWeak; ++i )
for( i=0; i<nBad; ++i )
removePeer( t, connections[i] );
/* add some new ones */

View File

@ -40,7 +40,7 @@
**/
#define MAX_ALLOWED_SET_COUNT 10 /* number of pieces generated for allow-fast,
threshold for fast-allowing others */
threshold for fast-allowing others */
enum
{

View File

@ -1,295 +0,0 @@
/******************************************************************************
* $Id$
*
* Copyright (c) 2005-2007 Transmission authors and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
static int peerCmp( tr_peer_t * peer1, tr_peer_t * peer2 )
{
/* Wait until we got the peers' ids */
if( peer1->status <= PEER_STATUS_HANDSHAKE ||
peer2->status <= PEER_STATUS_HANDSHAKE )
{
return 1;
}
return memcmp( peer1->id, peer2->id, 20 );
}
static int
checkPeer( tr_peer_t * peer )
{
int ret;
tr_torrent * tor = peer->tor;
const uint64_t now = tr_date( );
const uint64_t idleTime = now - peer->date;
const uint64_t idleSecs = idleTime / 1000u;
uint64_t lo, hi, limit;
int relaxStrictnessIfFewerThanN;
double strictness;
/* when deciding whether or not to keep a peer, judge its responsiveness
on a sliding scale that's based on how many other peers are available */
relaxStrictnessIfFewerThanN =
(int)(((TR_MAX_PEER_COUNT * PERCENT_PEER_WANTED) / 100.0) + 0.5);
/* if we have >= relaxIfFewerThan, strictness is 100%.
if we have zero connections, strictness is 0% */
if( tor->peerCount >= relaxStrictnessIfFewerThanN )
strictness = 1.0;
else
strictness = tor->peerCount / (double)relaxStrictnessIfFewerThanN;
/* test: has it been too long since we were properly connected to them? */
lo = MIN_CON_TIMEOUT;
hi = MAX_CON_TIMEOUT;
limit = lo + ((hi-lo) * strictness);
if( peer->status < PEER_STATUS_CONNECTED && idleTime > limit ) {
peer_dbg( "connection timeout, idled %"PRIu64" seconds", idleSecs );
return TR_ERROR;
}
/* test: have we been waiting on a request for too long? */
lo = MIN_UPLOAD_IDLE;
hi = MAX_UPLOAD_IDLE;
limit = lo + ((hi-lo) * strictness);
if( peer->inRequestCount && idleTime > limit ) {
peer_dbg( "idle uploader timeout, idled %"PRIu64" seconds", idleSecs );
return TR_ERROR;
}
/* test: has it been too long since the peer gave us any response at all? */
lo = MIN_KEEP_ALIVE;
hi = MAX_KEEP_ALIVE;
limit = lo + ((hi-lo) * strictness);
if( idleTime > limit ) {
peer_dbg( "peer timeout, idled %"PRIu64" seconds", idleSecs );
return TR_ERROR;
}
if( PEER_STATUS_CONNECTED == peer->status )
{
/* Send keep-alive every 1 minute and 45 seconds */
if( now > peer->keepAlive + 105000 )
{
sendKeepAlive( peer );
peer->keepAlive = now;
}
/* Resend extended handshake if our public port changed */
if( EXTENDED_HANDSHAKE == peer->extStatus &&
tor->publicPort != peer->advertisedPort )
{
sendExtended( tor, peer, EXTENDED_HANDSHAKE_ID );
}
/* Send peer list */
if( !peer->private && 0 < peer->pexStatus )
{
if( 0 == peer->lastPex )
{
/* randomize time when first pex message is sent */
peer->lastPex = now - 1000 * tr_rand( PEX_INTERVAL );
}
if( now > peer->lastPex + 1000 * PEX_INTERVAL )
{
if( EXTENDED_HANDSHAKE == peer->extStatus )
{
ret = sendExtended( tor, peer, EXTENDED_PEX_ID );
}
else if( peer->azproto )
{
ret = sendAZPex( tor, peer );
}
else
{
assert( 0 );
ret = TR_ERROR;
}
if( ret )
{
return ret;
}
peer->lastPex = now + 1000 *
( PEX_INTERVAL + tr_rand( PEX_INTERVAL / 10 ) );
}
}
}
return TR_OK;
}
static int isPieceInteresting( const tr_torrent * tor,
const tr_peer_t * peer,
int piece )
{
if( tor->info.pieces[piece].dnd ) /* we don't want it */
return 0;
if( tr_cpPieceIsComplete( tor->completion, piece ) ) /* we already have it */
return 0;
if( !tr_bitfieldHas( peer->bitfield, piece ) ) /* peer doesn't have it */
return 0;
if( tr_bitfieldHas( peer->banfield, piece ) ) /* peer is banned for it */
return 0;
return 1;
}
/***********************************************************************
* isInteresting
***********************************************************************
* Returns 1 if 'peer' has at least one piece that we want but
* haven't completed, or 0 otherwise.
**********************************************************************/
static int isInteresting( const tr_torrent * tor, const tr_peer_t * peer )
{
int i;
const tr_bitfield * bitfield = tr_cpPieceBitfield( tor->completion );
if( !peer->bitfield )
{
/* We don't know what this peer has */
return 0;
}
assert( bitfield->len == peer->bitfield->len );
for( i=0; i<tor->info.pieceCount; ++i )
if( isPieceInteresting( tor, peer, i ) )
return 1;
return 0;
}
static void
updateInterest( tr_torrent * tor, tr_peer_t * peer )
{
const int i = !!isInteresting( tor, peer );
if( i != peer->isInteresting )
sendInterest( peer, i );
}
/** utility structure used by getPreferredPieces() and comparePieces() */
typedef struct
{
int piece;
tr_priority_t priority;
int missingBlockCount;
int peerCount;
}
PieceCompareData;
/** utility function used by getPreferredPieces */
int comparePieces (const void * aIn, const void * bIn)
{
const PieceCompareData * a = (const PieceCompareData*) aIn;
const PieceCompareData * b = (const PieceCompareData*) bIn;
/* if one piece has a higher priority, it goes first */
if (a->priority != b->priority)
return a->priority > b->priority ? -1 : 1;
/* otherwise if one has fewer missing blocks, it goes first */
if (a->missingBlockCount != b->missingBlockCount)
return a->missingBlockCount < b->missingBlockCount ? -1 : 1;
/* otherwise if one has fewer peers, it goes first */
if (a->peerCount != b->peerCount)
return a->peerCount < b->peerCount ? -1 : 1;
/* otherwise go with the earlier piece */
return a->piece - b->piece;
}
static int* getPreferredPieces( const tr_torrent * tor,
const tr_peer_t * peer,
int * pieceCount,
int * isEndgame )
{
const tr_info * inf = &tor->info;
int i;
int poolSize = 0;
int endgame = FALSE;
int * pool = tr_new( int, inf->pieceCount );
for( i=0; i<inf->pieceCount; ++i )
if( isPieceInteresting( tor, peer, i ) )
if( tr_cpMissingBlocksForPiece( tor->completion, i ) )
pool[poolSize++] = i;
if( !poolSize ) {
endgame = TRUE;
for( i=0; i<inf->pieceCount; ++i )
if( isPieceInteresting( tor, peer, i ) )
pool[poolSize++] = i;
}
#if 0
fprintf (stderr, "old pool: ");
for (i=0; i<15 && i<poolSize; ++i ) fprintf (stderr, "%d, ", pool[i] );
fprintf (stderr, "\n");
#endif
/* sort the rest from most interesting to least...
but not in endgame, because it asks for pieces in a
scattershot manner anyway and doesn't need them sorted */
if( !endgame && ( poolSize > 1 ) )
{
PieceCompareData * p = tr_new( PieceCompareData, poolSize );
for( i=0; i<poolSize; ++i )
{
int j;
const int piece = pool[i];
p[i].piece = piece;
p[i].priority = inf->pieces[piece].priority;
p[i].missingBlockCount = tr_cpMissingBlocksForPiece( tor->completion, piece );
p[i].peerCount = 0;
for( j=0; j<tor->peerCount; ++j )
if( tr_bitfieldHas( tor->peers[j]->bitfield, piece ) )
++p[i].peerCount;
}
qsort (p, poolSize, sizeof(PieceCompareData), comparePieces);
for( i=0; i<poolSize; ++i )
pool[i] = p[i].piece;
tr_free( p );
}
#if 0
fprintf (stderr, "new pool: ");
for (i=0; i<15 && i<poolSize; ++i ) fprintf (stderr, "%d, ", pool[i] );
fprintf (stderr, "\n");
#endif
*isEndgame = endgame;
*pieceCount = poolSize;
return pool;
}

View File

@ -341,6 +341,8 @@ torrentRealInit( tr_handle * h,
loaded = tr_fastResumeLoad( tor, destination, uncheckedPieces );
assert( tor->destination != NULL );
/* the `paused' flag has highest precedence...
after that, the fastresume setting is used...
if that's not found, default to RUNNING */