improved random chunk strategy, to limit cluster effects on files partially downloaded in streaming

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3764 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2010-11-08 22:09:43 +00:00
parent 60d750f799
commit f2406d4e57
2 changed files with 21 additions and 18 deletions

View File

@ -192,19 +192,7 @@ bool ChunkMap::getDataChunk(const std::string& peer_id,uint32_t size_hint,ftChun
{ {
// 1 - select an available chunk id for this peer. // 1 - select an available chunk id for this peer.
// //
uint32_t c ; uint32_t c = getAvailableChunk(peer_id,source_chunk_map_needed) ;
switch(_strategy)
{
case FileChunksInfo::CHUNK_STRATEGY_STREAMING: c = getAvailableChunk(0,peer_id,source_chunk_map_needed) ; // very bold!!
break ;
case FileChunksInfo::CHUNK_STRATEGY_RANDOM: c = getAvailableChunk(rand()%_map.size(),peer_id,source_chunk_map_needed) ;
break ;
default:
std::cerr << "!!! ChunkMap::getDataChunk: error!: unknown strategy" << std::endl ;
return false ;
}
if(c >= _map.size()) if(c >= _map.size())
return false ; return false ;
@ -347,7 +335,7 @@ uint32_t ChunkMap::sizeOfChunk(uint32_t cid) const
return _chunk_size ; return _chunk_size ;
} }
uint32_t ChunkMap::getAvailableChunk(uint32_t start_location,const std::string& peer_id,bool& map_is_too_old) uint32_t ChunkMap::getAvailableChunk(const std::string& peer_id,bool& map_is_too_old)
{ {
// Quite simple strategy: Check for 1st availabe chunk for this peer starting from the given start location. // Quite simple strategy: Check for 1st availabe chunk for this peer starting from the given start location.
// //
@ -404,16 +392,31 @@ uint32_t ChunkMap::getAvailableChunk(uint32_t start_location,const std::string&
else else
map_is_too_old = false ;// the map is not too old map_is_too_old = false ;// the map is not too old
uint32_t start_location = (_strategy == FileChunksInfo::CHUNK_STRATEGY_STREAMING)?0:(rand()%_map.size()) ;
for(unsigned int i=0;i<_map.size();++i) for(unsigned int i=0;i<_map.size();++i)
{ {
uint32_t j = (start_location+i)%(int)_map.size() ; // index of the chunk uint32_t j = (start_location+i)%(int)_map.size() ; // index of the chunk
if(_map[j] == FileChunksInfo::CHUNK_OUTSTANDING && (peer_chunks->is_full || peer_chunks->cmap[j])) if(_map[j] == FileChunksInfo::CHUNK_OUTSTANDING && (peer_chunks->is_full || peer_chunks->cmap[j]))
{ {
#ifdef DEBUG_FTCHUNK if(_strategy == FileChunksInfo::CHUNK_STRATEGY_STREAMING)
std::cerr << "ChunkMap::getAvailableChunk: returning chunk " << j << " for peer " << peer_id << std::endl;
#endif
return j ; return j ;
else
{
// reserve a series of available chunks
//
uint32_t k=0 ;
for(;k+j<_map.size() && (_map[k+j] == FileChunksInfo::CHUNK_OUTSTANDING && (peer_chunks->is_full || peer_chunks->cmap[k+j]));++k) ;
#ifdef DEBUG_FTCHUNK
std::cerr << "ChunkMap::getAvailableChunk: returning chunk " << j+k << " for peer " << peer_id << std::endl;
#endif
// ...and select one randomly
//
return (rand()%k) + j ;
}
} }
} }

View File

@ -170,9 +170,9 @@ class ChunkMap
/// handles what size the last chunk has. /// handles what size the last chunk has.
uint32_t sizeOfChunk(uint32_t chunk_number) const ; uint32_t sizeOfChunk(uint32_t chunk_number) const ;
/// Returns the first chunk available starting from start_location for this peer_id. /// Returns a chunk available for this peer_id, depending on the chunk strategy.
// //
uint32_t getAvailableChunk(uint32_t start_location,const std::string& peer_id,bool& chunk_map_too_old) ; uint32_t getAvailableChunk(const std::string& peer_id,bool& chunk_map_too_old) ;
private: private:
uint64_t _file_size ; //! total size of the file in bytes. uint64_t _file_size ; //! total size of the file in bytes.