diff --git a/ChangeLog b/ChangeLog index 9b641085..e606be7a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2010-08-26 Tatsuhiro Tsujikawa + + Enter end game mode as soon as all pieces are assigned to peer to + avoid substantial slow down at the very last stage of download. + * src/DefaultBtInteractive.cc + * src/DefaultPieceStorage.cc + * src/DefaultPieceStorage.h + * src/PieceStorage.h + * src/UnknownLengthPieceStorage.h + * test/MockPieceStorage.h + 2010-08-25 Tatsuhiro Tsujikawa Updated IPv6 DHT usage. diff --git a/src/DefaultBtInteractive.cc b/src/DefaultBtInteractive.cc index 783072c5..95c2cccb 100644 --- a/src/DefaultBtInteractive.cc +++ b/src/DefaultBtInteractive.cc @@ -323,7 +323,8 @@ size_t DefaultBtInteractive::receiveMessages() { break; } } - if(countOldOutstandingRequest >= maxOutstandingRequest_ && + if(!pieceStorage_->isEndGame() && + countOldOutstandingRequest >= maxOutstandingRequest_ && dispatcher_->countOutstandingRequest() == 0){ maxOutstandingRequest_ = std::min((size_t)UB_MAX_OUTSTANDING_REQUEST, @@ -394,6 +395,12 @@ void DefaultBtInteractive::fillPiece(size_t maxMissingBlock) { } void DefaultBtInteractive::addRequests() { + if(!pieceStorage_->isEndGame() && !pieceStorage_->hasMissingUnusedPiece()) { + pieceStorage_->enterEndGame(); + } + if(pieceStorage_->isEndGame()) { + maxOutstandingRequest_ = 2; + } fillPiece(maxOutstandingRequest_); size_t reqNumToCreate = maxOutstandingRequest_ <= dispatcher_->countOutstandingRequest() ? @@ -402,6 +409,7 @@ void DefaultBtInteractive::addRequests() { std::vector > requests; requests.reserve(reqNumToCreate); if(pieceStorage_->isEndGame()) { + logger_->debug("ENDGAME"); btRequestFactory_->createRequestMessagesOnEndGame(requests,reqNumToCreate); } else { btRequestFactory_->createRequestMessages(requests, reqNumToCreate); diff --git a/src/DefaultPieceStorage.cc b/src/DefaultPieceStorage.cc index 5801ead6..bf1f0a77 100644 --- a/src/DefaultPieceStorage.cc +++ b/src/DefaultPieceStorage.cc @@ -71,6 +71,7 @@ DefaultPieceStorage::DefaultPieceStorage bitfieldMan_(new BitfieldMan(downloadContext->getPieceLength(), downloadContext->getTotalLength())), diskWriterFactory_(new DefaultDiskWriterFactory()), + endGame_(false), endGamePieceNum_(END_GAME_PIECE_NUM), logger_(LogFactory::getInstance()), option_(option), @@ -82,11 +83,6 @@ DefaultPieceStorage::~DefaultPieceStorage() { delete bitfieldMan_; } -bool DefaultPieceStorage::isEndGame() -{ - return bitfieldMan_->countMissingBlock() <= endGamePieceNum_; -} - bool DefaultPieceStorage::getMissingPieceIndex(size_t& index, const unsigned char* bitfield, size_t length) diff --git a/src/DefaultPieceStorage.h b/src/DefaultPieceStorage.h index 879f13c7..3846d1a2 100644 --- a/src/DefaultPieceStorage.h +++ b/src/DefaultPieceStorage.h @@ -78,6 +78,7 @@ private: SharedHandle diskWriterFactory_; std::deque > usedPieces_; + bool endGame_; size_t endGamePieceNum_; Logger* logger_; const Option* option_; @@ -187,8 +188,16 @@ public: virtual bool isSelectiveDownloadingMode(); - virtual bool isEndGame(); + virtual bool isEndGame() + { + return endGame_; + } + virtual void enterEndGame() + { + endGame_ = true; + } + virtual SharedHandle getDiskAdaptor(); virtual size_t getPieceLength(size_t index); diff --git a/src/PieceStorage.h b/src/PieceStorage.h index 02167432..efaf01e2 100644 --- a/src/PieceStorage.h +++ b/src/PieceStorage.h @@ -189,6 +189,9 @@ public: virtual bool isEndGame() = 0; + virtual void enterEndGame() = 0; + + // TODO We can remove this. virtual void setEndGamePieceNum(size_t num) = 0; virtual SharedHandle getDiskAdaptor() = 0; diff --git a/src/UnknownLengthPieceStorage.h b/src/UnknownLengthPieceStorage.h index 01bd94e6..5b89391a 100644 --- a/src/UnknownLengthPieceStorage.h +++ b/src/UnknownLengthPieceStorage.h @@ -211,6 +211,8 @@ public: return false; } + virtual void enterEndGame() {} + virtual void setEndGamePieceNum(size_t num) {} virtual SharedHandle getDiskAdaptor(); diff --git a/test/MockPieceStorage.h b/test/MockPieceStorage.h index 6d1a50cf..82947dbd 100644 --- a/test/MockPieceStorage.h +++ b/test/MockPieceStorage.h @@ -190,8 +190,8 @@ public: virtual void setEndGamePieceNum(size_t num) {} - void setEndGame(bool flag) { - this->endGame = flag; + virtual void enterEndGame() { + this->endGame = true; } virtual SharedHandle getDiskAdaptor() {