FileEntry.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 "Logger.h"
  41. #include "LogFactory.h"
  42. #include "wallclock.h"
  43. #include "a2algo.h"
  44. #include "uri.h"
  45. #include "PeerStat.h"
  46. #include "fmt.h"
  47. namespace aria2 {
  48. FileEntry::FileEntry
  49. (const std::string& path,
  50. uint64_t length,
  51. off_t offset,
  52. const std::vector<std::string>& uris)
  53. : path_(path),
  54. uris_(uris.begin(), uris.end()),
  55. length_(length),
  56. offset_(offset),
  57. requested_(true),
  58. uniqueProtocol_(false),
  59. maxConnectionPerServer_(1),
  60. lastFasterReplace_(0)
  61. {}
  62. FileEntry::FileEntry()
  63. : length_(0),
  64. offset_(0),
  65. requested_(false),
  66. uniqueProtocol_(false),
  67. maxConnectionPerServer_(1)
  68. {}
  69. FileEntry::~FileEntry() {}
  70. FileEntry& FileEntry::operator=(const FileEntry& entry)
  71. {
  72. if(this != &entry) {
  73. path_ = entry.path_;
  74. length_ = entry.length_;
  75. offset_ = entry.offset_;
  76. requested_ = entry.requested_;
  77. }
  78. return *this;
  79. }
  80. bool FileEntry::operator<(const FileEntry& fileEntry) const
  81. {
  82. return offset_ < fileEntry.offset_;
  83. }
  84. bool FileEntry::exists() const
  85. {
  86. return File(getPath()).exists();
  87. }
  88. off_t FileEntry::gtoloff(off_t goff) const
  89. {
  90. assert(offset_ <= goff);
  91. return goff-offset_;
  92. }
  93. void FileEntry::getUris(std::vector<std::string>& uris) const
  94. {
  95. uris.insert(uris.end(), spentUris_.begin(), spentUris_.end());
  96. uris.insert(uris.end(), uris_.begin(), uris_.end());
  97. }
  98. namespace {
  99. template<typename InputIterator, typename OutputIterator>
  100. OutputIterator enumerateInFlightHosts
  101. (InputIterator first, InputIterator last, OutputIterator out)
  102. {
  103. for(; first != last; ++first) {
  104. uri::UriStruct us;
  105. if(uri::parse(us, (*first)->getUri())) {
  106. *out++ = us.host;
  107. }
  108. }
  109. return out;
  110. }
  111. } // namespace
  112. SharedHandle<Request>
  113. FileEntry::getRequest
  114. (const SharedHandle<URISelector>& selector,
  115. bool uriReuse,
  116. const std::vector<std::pair<size_t, std::string> >& usedHosts,
  117. const std::string& referer,
  118. const std::string& method)
  119. {
  120. SharedHandle<Request> req;
  121. if(requestPool_.empty()) {
  122. std::vector<std::string> inFlightHosts;
  123. enumerateInFlightHosts(inFlightRequests_.begin(), inFlightRequests_.end(),
  124. std::back_inserter(inFlightHosts));
  125. for(int g = 0; g < 2; ++g) {
  126. std::vector<std::string> pending;
  127. std::vector<std::string> ignoreHost;
  128. while(1) {
  129. std::string uri = selector->select(this, usedHosts);
  130. if(uri.empty()) {
  131. break;
  132. }
  133. req.reset(new Request());
  134. if(req->setUri(uri)) {
  135. if(std::count(inFlightHosts.begin(),
  136. inFlightHosts.end(),req->getHost())
  137. >= static_cast<int>(maxConnectionPerServer_)) {
  138. pending.push_back(uri);
  139. ignoreHost.push_back(req->getHost());
  140. req.reset();
  141. continue;
  142. }
  143. req->setReferer(referer);
  144. req->setMethod(method);
  145. spentUris_.push_back(uri);
  146. inFlightRequests_.push_back(req);
  147. break;
  148. } else {
  149. req.reset();
  150. }
  151. }
  152. uris_.insert(uris_.begin(), pending.begin(), pending.end());
  153. if(g == 0 && uriReuse && !req && uris_.size() == pending.size()) {
  154. // Reuse URIs other than ones in pending
  155. reuseUri(ignoreHost);
  156. } else {
  157. break;
  158. }
  159. }
  160. } else {
  161. // Skip Request object if it is still
  162. // sleeping(Request::getWakeTime() < global::wallclock). If all
  163. // pooled objects are sleeping, return first one. Caller should
  164. // inspect returned object's getWakeTime().
  165. std::deque<SharedHandle<Request> >::iterator i = requestPool_.begin();
  166. std::deque<SharedHandle<Request> >::iterator eoi = requestPool_.end();
  167. for(; i != eoi; ++i) {
  168. if((*i)->getWakeTime() <= global::wallclock) {
  169. break;
  170. }
  171. }
  172. if(i == eoi) {
  173. i = requestPool_.begin();
  174. }
  175. req = *i;
  176. requestPool_.erase(i);
  177. inFlightRequests_.push_back(req);
  178. A2_LOG_DEBUG(fmt("Picked up from pool: %s", req->getUri().c_str()));
  179. }
  180. return req;
  181. }
  182. SharedHandle<Request>
  183. FileEntry::findFasterRequest(const SharedHandle<Request>& base)
  184. {
  185. const int startupIdleTime = 10;
  186. if(requestPool_.empty() ||
  187. lastFasterReplace_.difference(global::wallclock) < startupIdleTime) {
  188. return SharedHandle<Request>();
  189. }
  190. const SharedHandle<PeerStat>& fastest = requestPool_.front()->getPeerStat();
  191. if(!fastest) {
  192. return SharedHandle<Request>();
  193. }
  194. const SharedHandle<PeerStat>& basestat = base->getPeerStat();
  195. // TODO hard coded value. See PREF_STARTUP_IDLE_TIME
  196. if(!basestat ||
  197. (basestat->getDownloadStartTime().
  198. difference(global::wallclock) >= startupIdleTime &&
  199. fastest->getAvgDownloadSpeed()*0.8 > basestat->calculateDownloadSpeed())){
  200. // TODO we should consider that "fastest" is very slow.
  201. SharedHandle<Request> fastestRequest = requestPool_.front();
  202. requestPool_.pop_front();
  203. inFlightRequests_.push_back(fastestRequest);
  204. lastFasterReplace_.reset();
  205. return fastestRequest;
  206. }
  207. return SharedHandle<Request>();
  208. }
  209. namespace {
  210. class RequestFaster {
  211. public:
  212. bool operator()(const SharedHandle<Request>& lhs,
  213. const SharedHandle<Request>& rhs) const
  214. {
  215. if(!lhs->getPeerStat()) {
  216. return false;
  217. }
  218. if(!rhs->getPeerStat()) {
  219. return true;
  220. }
  221. return
  222. lhs->getPeerStat()->getAvgDownloadSpeed() > rhs->getPeerStat()->getAvgDownloadSpeed();
  223. }
  224. };
  225. } // namespace
  226. void FileEntry::storePool(const SharedHandle<Request>& request)
  227. {
  228. const SharedHandle<PeerStat>& peerStat = request->getPeerStat();
  229. if(peerStat) {
  230. // We need to calculate average download speed here in order to
  231. // store Request in the right position in the pool.
  232. peerStat->calculateAvgDownloadSpeed();
  233. }
  234. std::deque<SharedHandle<Request> >::iterator i =
  235. std::lower_bound(requestPool_.begin(), requestPool_.end(), request,
  236. RequestFaster());
  237. requestPool_.insert(i, request);
  238. }
  239. void FileEntry::poolRequest(const SharedHandle<Request>& request)
  240. {
  241. removeRequest(request);
  242. if(!request->removalRequested()) {
  243. storePool(request);
  244. }
  245. }
  246. bool FileEntry::removeRequest(const SharedHandle<Request>& request)
  247. {
  248. for(std::deque<SharedHandle<Request> >::iterator i =
  249. inFlightRequests_.begin(), eoi = inFlightRequests_.end();
  250. i != eoi; ++i) {
  251. if((*i).get() == request.get()) {
  252. inFlightRequests_.erase(i);
  253. return true;
  254. }
  255. }
  256. return false;
  257. }
  258. void FileEntry::removeURIWhoseHostnameIs(const std::string& hostname)
  259. {
  260. std::deque<std::string> newURIs;
  261. for(std::deque<std::string>::const_iterator itr = uris_.begin(),
  262. eoi = uris_.end(); itr != eoi; ++itr) {
  263. uri::UriStruct us;
  264. if(!uri::parse(us, *itr)) {
  265. continue;
  266. }
  267. if(us.host != hostname) {
  268. newURIs.push_back(*itr);
  269. }
  270. }
  271. A2_LOG_DEBUG(fmt("Removed %lu duplicate hostname URIs for path=%s",
  272. static_cast<unsigned long>(uris_.size()-newURIs.size()),
  273. getPath().c_str()));
  274. uris_.swap(newURIs);
  275. }
  276. void FileEntry::removeIdenticalURI(const std::string& uri)
  277. {
  278. uris_.erase(std::remove(uris_.begin(), uris_.end(), uri), uris_.end());
  279. }
  280. void FileEntry::addURIResult(std::string uri, error_code::Value result)
  281. {
  282. uriResults_.push_back(URIResult(uri, result));
  283. }
  284. namespace {
  285. class FindURIResultByResult {
  286. private:
  287. error_code::Value r_;
  288. public:
  289. FindURIResultByResult(error_code::Value r):r_(r) {}
  290. bool operator()(const URIResult& uriResult) const
  291. {
  292. return uriResult.getResult() == r_;
  293. }
  294. };
  295. } // namespace
  296. void FileEntry::extractURIResult
  297. (std::deque<URIResult>& res, error_code::Value r)
  298. {
  299. std::deque<URIResult>::iterator i =
  300. std::stable_partition(uriResults_.begin(), uriResults_.end(),
  301. FindURIResultByResult(r));
  302. std::copy(uriResults_.begin(), i, std::back_inserter(res));
  303. uriResults_.erase(uriResults_.begin(), i);
  304. }
  305. void FileEntry::reuseUri(const std::vector<std::string>& ignore)
  306. {
  307. if(A2_LOG_DEBUG_ENABLED) {
  308. for(std::vector<std::string>::const_iterator i = ignore.begin(),
  309. eoi = ignore.end(); i != eoi; ++i) {
  310. A2_LOG_DEBUG(fmt("ignore host=%s", (*i).c_str()));
  311. }
  312. }
  313. std::deque<std::string> uris = spentUris_;
  314. std::sort(uris.begin(), uris.end());
  315. uris.erase(std::unique(uris.begin(), uris.end()), uris.end());
  316. std::vector<std::string> errorUris(uriResults_.size());
  317. std::transform(uriResults_.begin(), uriResults_.end(),
  318. errorUris.begin(), std::mem_fun_ref(&URIResult::getURI));
  319. std::sort(errorUris.begin(), errorUris.end());
  320. errorUris.erase(std::unique(errorUris.begin(), errorUris.end()),
  321. errorUris.end());
  322. if(A2_LOG_DEBUG_ENABLED) {
  323. for(std::vector<std::string>::const_iterator i = errorUris.begin(),
  324. eoi = errorUris.end(); i != eoi; ++i) {
  325. A2_LOG_DEBUG(fmt("error URI=%s", (*i).c_str()));
  326. }
  327. }
  328. std::vector<std::string> reusableURIs;
  329. std::set_difference(uris.begin(), uris.end(),
  330. errorUris.begin(), errorUris.end(),
  331. std::back_inserter(reusableURIs));
  332. std::vector<std::string>::iterator insertionPoint = reusableURIs.begin();
  333. for(std::vector<std::string>::iterator i = reusableURIs.begin(),
  334. eoi = reusableURIs.end(); i != eoi; ++i) {
  335. uri::UriStruct us;
  336. if(uri::parse(us, *i) &&
  337. std::find(ignore.begin(), ignore.end(), us.host) == ignore.end()) {
  338. if(i != insertionPoint) {
  339. *insertionPoint = *i;
  340. }
  341. ++insertionPoint;
  342. }
  343. }
  344. reusableURIs.erase(insertionPoint, reusableURIs.end());
  345. size_t ininum = reusableURIs.size();
  346. if(A2_LOG_DEBUG_ENABLED) {
  347. A2_LOG_DEBUG(fmt("Found %u reusable URIs",
  348. static_cast<unsigned int>(ininum)));
  349. for(std::vector<std::string>::const_iterator i = reusableURIs.begin(),
  350. eoi = reusableURIs.end(); i != eoi; ++i) {
  351. A2_LOG_DEBUG(fmt("URI=%s", (*i).c_str()));
  352. }
  353. }
  354. uris_.insert(uris_.end(), reusableURIs.begin(), reusableURIs.end());
  355. }
  356. void FileEntry::releaseRuntimeResource()
  357. {
  358. requestPool_.clear();
  359. inFlightRequests_.clear();
  360. }
  361. namespace {
  362. template<typename InputIterator, typename T>
  363. InputIterator findRequestByUri
  364. (InputIterator first, InputIterator last, const T& uri)
  365. {
  366. for(; first != last; ++first) {
  367. if(!(*first)->removalRequested() && (*first)->getUri() == uri) {
  368. return first;
  369. }
  370. }
  371. return last;
  372. }
  373. } // namespace
  374. bool FileEntry::removeUri(const std::string& uri)
  375. {
  376. std::deque<std::string>::iterator itr =
  377. std::find(spentUris_.begin(), spentUris_.end(), uri);
  378. if(itr == spentUris_.end()) {
  379. itr = std::find(uris_.begin(), uris_.end(), uri);
  380. if(itr == uris_.end()) {
  381. return false;
  382. } else {
  383. uris_.erase(itr);
  384. return true;
  385. }
  386. } else {
  387. spentUris_.erase(itr);
  388. SharedHandle<Request> req;
  389. std::deque<SharedHandle<Request> >::iterator riter =
  390. findRequestByUri(inFlightRequests_.begin(), inFlightRequests_.end(), uri);
  391. if(riter == inFlightRequests_.end()) {
  392. riter = findRequestByUri(requestPool_.begin(), requestPool_.end(), uri);
  393. if(riter == requestPool_.end()) {
  394. return true;
  395. } else {
  396. req = *riter;
  397. requestPool_.erase(riter);
  398. }
  399. } else {
  400. req = *riter;
  401. }
  402. req->requestRemoval();
  403. return true;
  404. }
  405. }
  406. std::string FileEntry::getBasename() const
  407. {
  408. return File(path_).getBasename();
  409. }
  410. std::string FileEntry::getDirname() const
  411. {
  412. return File(path_).getDirname();
  413. }
  414. size_t FileEntry::setUris(const std::vector<std::string>& uris)
  415. {
  416. uris_.clear();
  417. return addUris(uris.begin(), uris.end());
  418. }
  419. bool FileEntry::addUri(const std::string& uri)
  420. {
  421. uri::UriStruct us;
  422. if(uri::parse(us, uri)) {
  423. uris_.push_back(uri);
  424. return true;
  425. } else {
  426. return false;
  427. }
  428. }
  429. bool FileEntry::insertUri(const std::string& uri, size_t pos)
  430. {
  431. uri::UriStruct us;
  432. if(uri::parse(us, uri)) {
  433. pos = std::min(pos, uris_.size());
  434. uris_.insert(uris_.begin()+pos, uri);
  435. return true;
  436. } else {
  437. return false;
  438. }
  439. }
  440. void FileEntry::setPath(const std::string& path)
  441. {
  442. path_ = path;
  443. }
  444. void FileEntry::setContentType(const std::string& contentType)
  445. {
  446. contentType_ = contentType;
  447. }
  448. size_t FileEntry::countInFlightRequest() const
  449. {
  450. return inFlightRequests_.size();
  451. }
  452. size_t FileEntry::countPooledRequest() const
  453. {
  454. return requestPool_.size();
  455. }
  456. void FileEntry::setOriginalName(const std::string& originalName)
  457. {
  458. originalName_ = originalName;
  459. }
  460. bool FileEntry::emptyRequestUri() const
  461. {
  462. return uris_.empty() && inFlightRequests_.empty() && requestPool_.empty();
  463. }
  464. } // namespace aria2