FeedbackURISelector.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "FeedbackURISelector.h"
  36. #include <cassert>
  37. #include <algorithm>
  38. #include "ServerStatMan.h"
  39. #include "ServerStat.h"
  40. #include "A2STR.h"
  41. #include "FileEntry.h"
  42. #include "Logger.h"
  43. #include "LogFactory.h"
  44. #include "a2algo.h"
  45. #include "uri.h"
  46. #include "fmt.h"
  47. namespace aria2 {
  48. FeedbackURISelector::FeedbackURISelector(
  49. const std::shared_ptr<ServerStatMan>& serverStatMan)
  50. : serverStatMan_(serverStatMan)
  51. {
  52. }
  53. FeedbackURISelector::~FeedbackURISelector() = default;
  54. std::string FeedbackURISelector::select(
  55. FileEntry* fileEntry,
  56. const std::vector<std::pair<size_t, std::string>>& usedHosts)
  57. {
  58. if (A2_LOG_DEBUG_ENABLED) {
  59. for (const auto& h : usedHosts) {
  60. A2_LOG_DEBUG(fmt("UsedHost=%lu, %s", static_cast<unsigned long>(h.first),
  61. h.second.c_str()));
  62. }
  63. }
  64. if (fileEntry->getRemainingUris().empty()) {
  65. return A2STR::NIL;
  66. }
  67. // Select URI with usedHosts first. If no URI is selected, then do
  68. // it again without usedHosts.
  69. std::string uri = selectFaster(fileEntry->getRemainingUris(), usedHosts);
  70. if (uri.empty()) {
  71. A2_LOG_DEBUG("No URI returned from selectFaster()");
  72. uri = selectRarer(fileEntry->getRemainingUris(), usedHosts);
  73. }
  74. if (!uri.empty()) {
  75. std::deque<std::string>& uris = fileEntry->getRemainingUris();
  76. uris.erase(std::find(uris.begin(), uris.end(), uri));
  77. }
  78. A2_LOG_DEBUG(fmt("FeedbackURISelector selected %s", uri.c_str()));
  79. return uri;
  80. }
  81. std::string FeedbackURISelector::selectRarer(
  82. const std::deque<std::string>& uris,
  83. const std::vector<std::pair<size_t, std::string>>& usedHosts)
  84. {
  85. // pair of host and URI
  86. std::vector<std::pair<std::string, std::string>> cands;
  87. for (const auto& u : uris) {
  88. uri_split_result us;
  89. if (uri_split(&us, u.c_str()) == -1) {
  90. continue;
  91. }
  92. auto host = uri::getFieldString(us, USR_HOST, u.c_str());
  93. auto protocol = uri::getFieldString(us, USR_SCHEME, u.c_str());
  94. auto ss = serverStatMan_->find(host, protocol);
  95. if (ss && ss->isError()) {
  96. A2_LOG_DEBUG(fmt("Error not considered: %s", u.c_str()));
  97. continue;
  98. }
  99. cands.push_back(std::make_pair(host, u));
  100. }
  101. for (const auto& h : usedHosts) {
  102. for (const auto& c : cands) {
  103. if (h.second == c.first) {
  104. return c.second;
  105. }
  106. }
  107. }
  108. assert(!uris.empty());
  109. return uris.front();
  110. }
  111. std::string FeedbackURISelector::selectFaster(
  112. const std::deque<std::string>& uris,
  113. const std::vector<std::pair<size_t, std::string>>& usedHosts)
  114. {
  115. // Use first 10 good URIs to introduce some randomness.
  116. constexpr size_t NUM_URI = 10;
  117. // Ignore low speed server
  118. constexpr int SPEED_THRESHOLD = 20_k;
  119. std::vector<std::pair<std::shared_ptr<ServerStat>, std::string>> fastCands;
  120. std::vector<std::string> normCands;
  121. for (const auto& u : uris) {
  122. if (fastCands.size() >= NUM_URI) {
  123. break;
  124. }
  125. uri_split_result us;
  126. if (uri_split(&us, u.c_str()) == -1) {
  127. continue;
  128. }
  129. auto host = uri::getFieldString(us, USR_HOST, u.c_str());
  130. if (findSecond(usedHosts.begin(), usedHosts.end(), host) !=
  131. usedHosts.end()) {
  132. A2_LOG_DEBUG(fmt("%s is in usedHosts, not considered", u.c_str()));
  133. continue;
  134. }
  135. auto protocol = uri::getFieldString(us, USR_SCHEME, u.c_str());
  136. auto ss = serverStatMan_->find(host, protocol);
  137. if (!ss) {
  138. normCands.push_back(u);
  139. }
  140. else if (ss->isOK()) {
  141. if (ss->getDownloadSpeed() > SPEED_THRESHOLD) {
  142. fastCands.push_back(std::make_pair(ss, u));
  143. }
  144. else {
  145. normCands.push_back(u);
  146. }
  147. }
  148. }
  149. if (fastCands.empty()) {
  150. if (normCands.empty()) {
  151. return A2STR::NIL;
  152. }
  153. else {
  154. A2_LOG_DEBUG("Selected from normCands");
  155. return normCands.front();
  156. }
  157. }
  158. else {
  159. A2_LOG_DEBUG("Selected from fastCands");
  160. std::sort(fastCands.begin(), fastCands.end(), ServerStatFaster());
  161. return fastCands.front().second;
  162. }
  163. }
  164. } // namespace aria2