From 49b49c351ab0e5d464d158f2d103333371d9c42a Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 15 May 2011 23:35:06 +0900 Subject: [PATCH] Eliminated few seconds delay when downloads stop or pause. The delay is caused because some Commands are only called in certain interval(called refreshInterval, default, 1000ms). In aria2 download stops when all Commands associated to it are stopped. Since some Commands are called in each 1000ms by default, as mentioned before, we have to wait for them. To fix this issue, we call DownloadEngine::setRefreshInterval(0) when pausing/stopping downloads. DownloadEngine::setRefreshInterval(0) makes refreshInterval 0 in one shot. When all segments are ignored, now DownloadFailureException is thrown. And stop the download immediately. As described earlier, we call DownloadEngine::setRefreshInterval(0) in catch block of DownloadFailureException to eliminate delay. --- src/AbstractCommand.cc | 22 ++++++++++++++++------ src/BtStopDownloadCommand.cc | 2 ++ src/CreateRequestCommand.cc | 5 ++++- src/PeerInteractionCommand.cc | 1 + src/RequestGroup.h | 5 +++++ src/RequestGroupMan.cc | 1 + src/RpcMethodImpl.cc | 2 ++ src/TrackerWatcherCommand.cc | 1 + 8 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/AbstractCommand.cc b/src/AbstractCommand.cc index f9c47358..7d096e19 100644 --- a/src/AbstractCommand.cc +++ b/src/AbstractCommand.cc @@ -211,7 +211,10 @@ bool AbstractCommand::execute() { // no URIs available, so don't retry. if(getSegmentMan()->allSegmentsIgnored()) { A2_LOG_DEBUG("All segments are ignored."); - return true; + // In this case, the error might be already set in + // RequestGroup, so use it here. + throw DOWNLOAD_FAILURE_EXCEPTION2 + ("No URI available.", requestGroup_->getLastErrorCode()); } else { return prepareForRetry(1); } @@ -265,19 +268,19 @@ bool AbstractCommand::execute() { return false; } } catch(DlAbortEx& err) { - if(!req_) { - A2_LOG_DEBUG_EX(EX_EXCEPTION_CAUGHT, err); - } else { + requestGroup_->setLastErrorCode(err.getErrorCode()); + if(req_) { A2_LOG_ERROR_EX(fmt(MSG_DOWNLOAD_ABORTED, getCuid(), req_->getUri().c_str()), DL_ABORT_EX2(fmt("URI=%s", req_->getCurrentUri().c_str()), err)); fileEntry_->addURIResult(req_->getUri(), err.getErrorCode()); - requestGroup_->setLastErrorCode(err.getErrorCode()); if(err.getErrorCode() == error_code::CANNOT_RESUME) { requestGroup_->increaseResumeFailureCount(); } + } else { + A2_LOG_DEBUG_EX(EX_EXCEPTION_CAUGHT, err); } onAbort(); tryReserved(); @@ -316,12 +319,19 @@ bool AbstractCommand::execute() { return prepareForRetry(0); } } catch(DownloadFailureException& err) { - A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, err); requestGroup_->setLastErrorCode(err.getErrorCode()); if(req_) { + A2_LOG_ERROR_EX(fmt(MSG_DOWNLOAD_ABORTED, + getCuid(), + req_->getUri().c_str()), + DL_ABORT_EX2(fmt("URI=%s", req_->getCurrentUri().c_str()), + err)); fileEntry_->addURIResult(req_->getUri(), err.getErrorCode()); + } else { + A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, err); } requestGroup_->setHaltRequested(true); + getDownloadEngine()->setRefreshInterval(0); return true; } } diff --git a/src/BtStopDownloadCommand.cc b/src/BtStopDownloadCommand.cc index 59225080..d488ce1e 100644 --- a/src/BtStopDownloadCommand.cc +++ b/src/BtStopDownloadCommand.cc @@ -41,6 +41,7 @@ #include "wallclock.h" #include "util.h" #include "fmt.h" +#include "DownloadEngine.h" namespace aria2 { @@ -64,6 +65,7 @@ void BtStopDownloadCommand::preProcess() " --bt-stop-timeout option.", util::itos(requestGroup_->getGID()).c_str())); requestGroup_->setForceHaltRequested(true); + getDownloadEngine()->setRefreshInterval(0); enableExit(); } } diff --git a/src/CreateRequestCommand.cc b/src/CreateRequestCommand.cc index f731d91a..9fe997e6 100644 --- a/src/CreateRequestCommand.cc +++ b/src/CreateRequestCommand.cc @@ -94,7 +94,10 @@ bool CreateRequestCommand::executeInternal() if(getSegmentMan()) { getSegmentMan()->ignoreSegmentFor(getFileEntry()); } - throw DL_ABORT_EX("No URI available."); + // In this case, the error might be already set in RequestGroup, + // so use it here. + throw DL_ABORT_EX2("No URI available.", + getRequestGroup()->getLastErrorCode()); } else if(getRequest()->getWakeTime() > global::wallclock) { A2_LOG_DEBUG("This request object is still sleeping."); getFileEntry()->poolRequest(getRequest()); diff --git a/src/PeerInteractionCommand.cc b/src/PeerInteractionCommand.cc index bb8ea4bb..488456ac 100644 --- a/src/PeerInteractionCommand.cc +++ b/src/PeerInteractionCommand.cc @@ -385,6 +385,7 @@ void PeerInteractionCommand::onFailure(const Exception& err) { requestGroup_->setLastErrorCode(err.getErrorCode()); requestGroup_->setHaltRequested(true); + getDownloadEngine()->setRefreshInterval(0); } bool PeerInteractionCommand::exitBeforeExecute() diff --git a/src/RequestGroup.h b/src/RequestGroup.h index 44a283d9..86651e1c 100644 --- a/src/RequestGroup.h +++ b/src/RequestGroup.h @@ -486,6 +486,11 @@ public: lastErrorCode_ = code; } + error_code::Value getLastErrorCode() const + { + return lastErrorCode_; + } + void saveControlFile() const; void removeControlFile() const; diff --git a/src/RequestGroupMan.cc b/src/RequestGroupMan.cc index 2bf6f2a5..4cc61c0d 100644 --- a/src/RequestGroupMan.cc +++ b/src/RequestGroupMan.cc @@ -536,6 +536,7 @@ void RequestGroupMan::fillRequestGroupFromReserver(DownloadEngine* e) } if(count > 0) { e->setNoWait(true); + e->setRefreshInterval(0); A2_LOG_DEBUG(fmt("%d RequestGroup(s) added.", count)); } } diff --git a/src/RpcMethodImpl.cc b/src/RpcMethodImpl.cc index 676e6753..4af1028a 100644 --- a/src/RpcMethodImpl.cc +++ b/src/RpcMethodImpl.cc @@ -395,6 +395,7 @@ SharedHandle removeDownload } else { group->setHaltRequested(true, RequestGroup::USER_REQUEST); } + e->setRefreshInterval(0); } return createGIDResponse(gid); } @@ -452,6 +453,7 @@ SharedHandle pauseDownload group = e->getRequestGroupMan()->findReservedGroup(gid); } if(group && pauseRequestGroup(group, reserved, forcePause)) { + e->setRefreshInterval(0); return createGIDResponse(gid); } else { throw DL_ABORT_EX diff --git a/src/TrackerWatcherCommand.cc b/src/TrackerWatcherCommand.cc index f9263008..d486aa5a 100644 --- a/src/TrackerWatcherCommand.cc +++ b/src/TrackerWatcherCommand.cc @@ -89,6 +89,7 @@ bool TrackerWatcherCommand::execute() { return true; } else { trackerRequestGroup_->setForceHaltRequested(true); + e_->setRefreshInterval(0); e_->addCommand(this); return false; }