FileEntry.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "FileEntry.h"
  36. #include <cassert>
  37. #include <algorithm>
  38. #include "util.h"
  39. #include "URISelector.h"
  40. #include "LogFactory.h"
  41. #include "wallclock.h"
  42. #include "a2algo.h"
  43. #include "uri.h"
  44. namespace aria2 {
  45. FileEntry::FileEntry(const std::string& path,
  46. uint64_t length,
  47. off_t offset,
  48. const std::vector<std::string>& uris):
  49. path_(path), uris_(uris.begin(), uris.end()), length_(length),
  50. offset_(offset),
  51. requested_(true),
  52. uniqueProtocol_(false),
  53. maxConnectionPerServer_(1),
  54. lastFasterReplace_(0),
  55. logger_(LogFactory::getInstance()) {}
  56. FileEntry::FileEntry():
  57. length_(0), offset_(0), requested_(false),
  58. uniqueProtocol_(false),
  59. maxConnectionPerServer_(1),
  60. logger_(LogFactory::getInstance()) {}
  61. FileEntry::~FileEntry() {}
  62. void FileEntry::setupDir()
  63. {
  64. util::mkdirs(File(path_).getDirname());
  65. }
  66. FileEntry& FileEntry::operator=(const FileEntry& entry)
  67. {
  68. if(this != &entry) {
  69. path_ = entry.path_;
  70. length_ = entry.length_;
  71. offset_ = entry.offset_;
  72. requested_ = entry.requested_;
  73. }
  74. return *this;
  75. }
  76. bool FileEntry::operator<(const FileEntry& fileEntry) const
  77. {
  78. return offset_ < fileEntry.offset_;
  79. }
  80. bool FileEntry::exists() const
  81. {
  82. return File(getPath()).exists();
  83. }
  84. off_t FileEntry::gtoloff(off_t goff) const
  85. {
  86. assert(offset_ <= goff);
  87. return goff-offset_;
  88. }
  89. void FileEntry::getUris(std::vector<std::string>& uris) const
  90. {
  91. uris.insert(uris.end(), spentUris_.begin(), spentUris_.end());
  92. uris.insert(uris.end(), uris_.begin(), uris_.end());
  93. }
  94. template<typename InputIterator, typename OutputIterator>
  95. static OutputIterator
  96. enumerateInFlightHosts
  97. (InputIterator first, InputIterator last, OutputIterator out)
  98. {
  99. for(; first != last; ++first) {
  100. uri::UriStruct us;
  101. if(uri::parse(us, (*first)->getUri())) {
  102. *out++ = us.host;
  103. }
  104. }
  105. return out;
  106. }
  107. SharedHandle<Request>
  108. FileEntry::getRequest
  109. (const SharedHandle<URISelector>& selector,
  110. bool uriReuse,
  111. const std::vector<std::pair<size_t, std::string> >& usedHosts,
  112. const std::string& referer,
  113. const std::string& method)
  114. {
  115. SharedHandle<Request> req;
  116. if(requestPool_.empty()) {
  117. std::vector<std::string> inFlightHosts;
  118. enumerateInFlightHosts(inFlightRequests_.begin(), inFlightRequests_.end(),
  119. std::back_inserter(inFlightHosts));
  120. for(int g = 0; g < 2; ++g) {
  121. std::vector<std::string> pending;
  122. std::vector<std::string> ignoreHost;
  123. while(1) {
  124. std::string uri = selector->select(this, usedHosts);
  125. if(uri.empty()) {
  126. break;
  127. }
  128. req.reset(new Request());
  129. if(req->setUri(uri)) {
  130. if(std::count(inFlightHosts.begin(),
  131. inFlightHosts.end(),req->getHost())
  132. >= static_cast<int>(maxConnectionPerServer_)) {
  133. pending.push_back(uri);
  134. ignoreHost.push_back(req->getHost());
  135. req.reset();
  136. continue;
  137. }
  138. req->setReferer(referer);
  139. req->setMethod(method);
  140. spentUris_.push_back(uri);
  141. inFlightRequests_.push_back(req);
  142. break;
  143. } else {
  144. req.reset();
  145. }
  146. }
  147. uris_.insert(uris_.begin(), pending.begin(), pending.end());
  148. if(g == 0 && uriReuse && req.isNull() && uris_.size() == pending.size()) {
  149. // Reuse URIs other than ones in pending
  150. reuseUri(ignoreHost);
  151. } else {
  152. break;
  153. }
  154. }
  155. } else {
  156. req = requestPool_.front();
  157. requestPool_.pop_front();
  158. inFlightRequests_.push_back(req);
  159. if(logger_->debug()) {
  160. logger_->debug("Picked up from pool: %s", req->getUri().c_str());
  161. }
  162. }
  163. return req;
  164. }
  165. SharedHandle<Request>
  166. FileEntry::findFasterRequest(const SharedHandle<Request>& base)
  167. {
  168. const int startupIdleTime = 10;
  169. if(requestPool_.empty() ||
  170. lastFasterReplace_.difference(global::wallclock) < startupIdleTime) {
  171. return SharedHandle<Request>();
  172. }
  173. const SharedHandle<PeerStat>& fastest = requestPool_.front()->getPeerStat();
  174. if(fastest.isNull()) {
  175. return SharedHandle<Request>();
  176. }
  177. const SharedHandle<PeerStat>& basestat = base->getPeerStat();
  178. // TODO hard coded value. See PREF_STARTUP_IDLE_TIME
  179. if(basestat.isNull() ||
  180. (basestat->getDownloadStartTime().
  181. difference(global::wallclock) >= startupIdleTime &&
  182. fastest->getAvgDownloadSpeed()*0.8 > basestat->calculateDownloadSpeed())){
  183. // TODO we should consider that "fastest" is very slow.
  184. SharedHandle<Request> fastestRequest = requestPool_.front();
  185. requestPool_.pop_front();
  186. inFlightRequests_.push_back(fastestRequest);
  187. lastFasterReplace_.reset();
  188. return fastestRequest;
  189. }
  190. return SharedHandle<Request>();
  191. }
  192. namespace {
  193. class RequestFaster {
  194. public:
  195. bool operator()(const SharedHandle<Request>& lhs,
  196. const SharedHandle<Request>& rhs) const
  197. {
  198. if(lhs->getPeerStat().isNull()) {
  199. return false;
  200. }
  201. if(rhs->getPeerStat().isNull()) {
  202. return true;
  203. }
  204. return
  205. lhs->getPeerStat()->getAvgDownloadSpeed() > rhs->getPeerStat()->getAvgDownloadSpeed();
  206. }
  207. };
  208. }
  209. void FileEntry::storePool(const SharedHandle<Request>& request)
  210. {
  211. const SharedHandle<PeerStat>& peerStat = request->getPeerStat();
  212. if(!peerStat.isNull()) {
  213. // We need to calculate average download speed here in order to
  214. // store Request in the right position in the pool.
  215. peerStat->calculateAvgDownloadSpeed();
  216. }
  217. std::deque<SharedHandle<Request> >::iterator i =
  218. std::lower_bound(requestPool_.begin(), requestPool_.end(), request,
  219. RequestFaster());
  220. requestPool_.insert(i, request);
  221. }
  222. void FileEntry::poolRequest(const SharedHandle<Request>& request)
  223. {
  224. removeRequest(request);
  225. if(!request->removalRequested()) {
  226. storePool(request);
  227. }
  228. }
  229. bool FileEntry::removeRequest(const SharedHandle<Request>& request)
  230. {
  231. for(std::deque<SharedHandle<Request> >::iterator i =
  232. inFlightRequests_.begin(), eoi = inFlightRequests_.end();
  233. i != eoi; ++i) {
  234. if((*i).get() == request.get()) {
  235. inFlightRequests_.erase(i);
  236. return true;
  237. }
  238. }
  239. return false;
  240. }
  241. void FileEntry::removeURIWhoseHostnameIs(const std::string& hostname)
  242. {
  243. std::deque<std::string> newURIs;
  244. for(std::deque<std::string>::const_iterator itr = uris_.begin(),
  245. eoi = uris_.end(); itr != eoi; ++itr) {
  246. uri::UriStruct us;
  247. if(!uri::parse(us, *itr)) {
  248. continue;
  249. }
  250. if(us.host != hostname) {
  251. newURIs.push_back(*itr);
  252. }
  253. }
  254. if(logger_->debug()) {
  255. logger_->debug("Removed %d duplicate hostname URIs for path=%s",
  256. uris_.size()-newURIs.size(), getPath().c_str());
  257. }
  258. uris_.swap(newURIs);
  259. }
  260. void FileEntry::removeIdenticalURI(const std::string& uri)
  261. {
  262. uris_.erase(std::remove(uris_.begin(), uris_.end(), uri), uris_.end());
  263. }
  264. void FileEntry::addURIResult(std::string uri, downloadresultcode::RESULT result)
  265. {
  266. uriResults_.push_back(URIResult(uri, result));
  267. }
  268. namespace {
  269. class FindURIResultByResult {
  270. private:
  271. downloadresultcode::RESULT r_;
  272. public:
  273. FindURIResultByResult(downloadresultcode::RESULT r):r_(r) {}
  274. bool operator()(const URIResult& uriResult) const
  275. {
  276. return uriResult.getResult() == r_;
  277. }
  278. };
  279. }
  280. void FileEntry::extractURIResult
  281. (std::deque<URIResult>& res, downloadresultcode::RESULT r)
  282. {
  283. std::deque<URIResult>::iterator i =
  284. std::stable_partition(uriResults_.begin(), uriResults_.end(),
  285. FindURIResultByResult(r));
  286. std::copy(uriResults_.begin(), i, std::back_inserter(res));
  287. uriResults_.erase(uriResults_.begin(), i);
  288. }
  289. void FileEntry::reuseUri(const std::vector<std::string>& ignore)
  290. {
  291. if(logger_->debug()) {
  292. for(std::vector<std::string>::const_iterator i = ignore.begin(),
  293. eoi = ignore.end(); i != eoi; ++i) {
  294. logger_->debug("ignore host=%s", (*i).c_str());
  295. }
  296. }
  297. std::deque<std::string> uris = spentUris_;
  298. std::sort(uris.begin(), uris.end());
  299. uris.erase(std::unique(uris.begin(), uris.end()), uris.end());
  300. std::vector<std::string> errorUris(uriResults_.size());
  301. std::transform(uriResults_.begin(), uriResults_.end(),
  302. errorUris.begin(), std::mem_fun_ref(&URIResult::getURI));
  303. std::sort(errorUris.begin(), errorUris.end());
  304. errorUris.erase(std::unique(errorUris.begin(), errorUris.end()),
  305. errorUris.end());
  306. if(logger_->debug()) {
  307. for(std::vector<std::string>::const_iterator i = errorUris.begin(),
  308. eoi = errorUris.end(); i != eoi; ++i) {
  309. logger_->debug("error URI=%s", (*i).c_str());
  310. }
  311. }
  312. std::vector<std::string> reusableURIs;
  313. std::set_difference(uris.begin(), uris.end(),
  314. errorUris.begin(), errorUris.end(),
  315. std::back_inserter(reusableURIs));
  316. std::vector<std::string>::iterator insertionPoint = reusableURIs.begin();
  317. for(std::vector<std::string>::iterator i = reusableURIs.begin(),
  318. eoi = reusableURIs.end(); i != eoi; ++i) {
  319. uri::UriStruct us;
  320. if(uri::parse(us, *i) &&
  321. std::find(ignore.begin(), ignore.end(), us.host) == ignore.end()) {
  322. if(i != insertionPoint) {
  323. *insertionPoint = *i;
  324. }
  325. ++insertionPoint;
  326. }
  327. }
  328. reusableURIs.erase(insertionPoint, reusableURIs.end());
  329. size_t ininum = reusableURIs.size();
  330. if(logger_->debug()) {
  331. logger_->debug("Found %u reusable URIs", static_cast<unsigned int>(ininum));
  332. for(std::vector<std::string>::const_iterator i = reusableURIs.begin(),
  333. eoi = reusableURIs.end(); i != eoi; ++i) {
  334. logger_->debug("URI=%s", (*i).c_str());
  335. }
  336. }
  337. uris_.insert(uris_.end(), reusableURIs.begin(), reusableURIs.end());
  338. }
  339. void FileEntry::releaseRuntimeResource()
  340. {
  341. requestPool_.clear();
  342. inFlightRequests_.clear();
  343. }
  344. template<typename InputIterator, typename T>
  345. static InputIterator findRequestByUri
  346. (InputIterator first, InputIterator last, const T& uri)
  347. {
  348. for(; first != last; ++first) {
  349. if(!(*first)->removalRequested() && (*first)->getUri() == uri) {
  350. return first;
  351. }
  352. }
  353. return last;
  354. }
  355. bool FileEntry::removeUri(const std::string& uri)
  356. {
  357. std::deque<std::string>::iterator itr =
  358. std::find(spentUris_.begin(), spentUris_.end(), uri);
  359. if(itr == spentUris_.end()) {
  360. itr = std::find(uris_.begin(), uris_.end(), uri);
  361. if(itr == uris_.end()) {
  362. return false;
  363. } else {
  364. uris_.erase(itr);
  365. return true;
  366. }
  367. } else {
  368. spentUris_.erase(itr);
  369. SharedHandle<Request> req;
  370. std::deque<SharedHandle<Request> >::iterator riter =
  371. findRequestByUri(inFlightRequests_.begin(), inFlightRequests_.end(), uri);
  372. if(riter == inFlightRequests_.end()) {
  373. riter = findRequestByUri(requestPool_.begin(), requestPool_.end(), uri);
  374. if(riter == requestPool_.end()) {
  375. return true;
  376. } else {
  377. req = *riter;
  378. requestPool_.erase(riter);
  379. }
  380. } else {
  381. req = *riter;
  382. }
  383. req->requestRemoval();
  384. return true;
  385. }
  386. }
  387. std::string FileEntry::getBasename() const
  388. {
  389. return File(path_).getBasename();
  390. }
  391. std::string FileEntry::getDirname() const
  392. {
  393. return File(path_).getDirname();
  394. }
  395. size_t FileEntry::setUris(const std::vector<std::string>& uris)
  396. {
  397. uris_.clear();
  398. return addUris(uris.begin(), uris.end());
  399. }
  400. bool FileEntry::addUri(const std::string& uri)
  401. {
  402. uri::UriStruct us;
  403. if(uri::parse(us, uri)) {
  404. uris_.push_back(uri);
  405. return true;
  406. } else {
  407. return false;
  408. }
  409. }
  410. bool FileEntry::insertUri(const std::string& uri, size_t pos)
  411. {
  412. uri::UriStruct us;
  413. if(uri::parse(us, uri)) {
  414. pos = std::min(pos, uris_.size());
  415. uris_.insert(uris_.begin()+pos, uri);
  416. return true;
  417. } else {
  418. return false;
  419. }
  420. }
  421. } // namespace aria2