Преглед изворни кода

randomize mirror list to ensure more even load

Michael Vogt пре 15 година
родитељ
комит
01a695e260
3 измењених фајлова са 32 додато и 0 уклоњено
  1. 1 0
      debian/changelog
  2. 30 0
      methods/mirror.cc
  3. 1 0
      methods/mirror.h

+ 1 - 0
debian/changelog

@@ -13,6 +13,7 @@ apt (0.8.13) unstable; urgency=low
       user to insert the CD on each apt-get update
       user to insert the CD on each apt-get update
   * mirror method:
   * mirror method:
     - when downloading data, show the mirror being used
     - when downloading data, show the mirror being used
+    - randomize mirror list to ensure more even load
 
 
  -- Michael Vogt <michael.vogt@ubuntu.com>  Thu, 10 Mar 2011 15:56:54 +0100
  -- Michael Vogt <michael.vogt@ubuntu.com>  Thu, 10 Mar 2011 15:56:54 +0100
 
 

+ 30 - 0
methods/mirror.cc

@@ -17,6 +17,7 @@
 #include <apt-pkg/sourcelist.h>
 #include <apt-pkg/sourcelist.h>
 
 
 #include <fstream>
 #include <fstream>
+#include <algorithm>
 #include <iostream>
 #include <iostream>
 #include <stdarg.h>
 #include <stdarg.h>
 #include <sys/stat.h>
 #include <sys/stat.h>
@@ -147,6 +148,34 @@ bool MirrorMethod::DownloadMirrorFile(string mirror_uri_str)
    return res;
    return res;
 }
 }
 
 
+// Randomizes the lines in the mirror file, this is used so that
+// we spread the load on the mirrors evenly
+bool MirrorMethod::RandomizeMirrorFile(string mirror_file)
+{
+   vector<string> content;
+   string line;
+
+   // read 
+   ifstream in(mirror_file.c_str());
+   while ( ! in.eof() ) {
+      getline(in, line);
+      content.push_back(line);
+   }
+   
+   // randomize
+   random_shuffle(content.begin(), content.end());
+
+   // write
+   ofstream out(mirror_file.c_str());
+   while ( !content.empty()) {
+      line = content.back();
+      content.pop_back();
+      out << line << "\n";
+   }
+
+   return true;
+}
+
 /* convert a the Queue->Uri back to the mirror base uri and look
 /* convert a the Queue->Uri back to the mirror base uri and look
  * at all mirrors we have for this, this is needed as queue->uri
  * at all mirrors we have for this, this is needed as queue->uri
  * may point to different mirrors (if TryNextMirror() was run)
  * may point to different mirrors (if TryNextMirror() was run)
@@ -313,6 +342,7 @@ bool MirrorMethod::Fetch(FetchItem *Itm)
    {
    {
       Clean(_config->FindDir("Dir::State::mirrors"));
       Clean(_config->FindDir("Dir::State::mirrors"));
       DownloadMirrorFile(Itm->Uri);
       DownloadMirrorFile(Itm->Uri);
+      RandomizeMirrorFile(MirrorFile);
    }
    }
 
 
    if(AllMirrors.empty()) {
    if(AllMirrors.empty()) {

+ 1 - 0
methods/mirror.h

@@ -34,6 +34,7 @@ class MirrorMethod : public HttpMethod
 
 
  protected:
  protected:
    bool DownloadMirrorFile(string uri);
    bool DownloadMirrorFile(string uri);
+   bool RandomizeMirrorFile(string file);
    string GetMirrorFileName(string uri);
    string GetMirrorFileName(string uri);
    bool InitMirrors();
    bool InitMirrors();
    bool TryNextMirror();
    bool TryNextMirror();