From 196fd13629fc0f673b18735d633bdf69a3272590 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Mon, 14 Mar 2011 11:34:32 +0100 Subject: * mirror method: - when downloading data, show the mirror being used --- methods/mirror.cc | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'methods') diff --git a/methods/mirror.cc b/methods/mirror.cc index d092cc657..5a53d3c81 100644 --- a/methods/mirror.cc +++ b/methods/mirror.cc @@ -186,6 +186,10 @@ bool MirrorMethod::TryNextMirror() Queue->Uri.replace(0, mirror->length(), *nextmirror); if (Debug) clog << "TryNextMirror: " << Queue->Uri << endl; + + // inform parent + UsedMirror = *nextmirror; + Log("Switching mirror"); return true; } -- cgit v1.2.3 From 01a695e260429b4ed834a52fb55ba49a8cb42e6a Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Mon, 14 Mar 2011 15:44:08 +0100 Subject: randomize mirror list to ensure more even load --- methods/mirror.cc | 30 ++++++++++++++++++++++++++++++ methods/mirror.h | 1 + 2 files changed, 31 insertions(+) (limited to 'methods') diff --git a/methods/mirror.cc b/methods/mirror.cc index 5a53d3c81..08a603207 100644 --- a/methods/mirror.cc +++ b/methods/mirror.cc @@ -17,6 +17,7 @@ #include #include +#include #include #include #include @@ -147,6 +148,34 @@ bool MirrorMethod::DownloadMirrorFile(string mirror_uri_str) 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 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 * at all mirrors we have for this, this is needed as queue->uri * may point to different mirrors (if TryNextMirror() was run) @@ -313,6 +342,7 @@ bool MirrorMethod::Fetch(FetchItem *Itm) { Clean(_config->FindDir("Dir::State::mirrors")); DownloadMirrorFile(Itm->Uri); + RandomizeMirrorFile(MirrorFile); } if(AllMirrors.empty()) { diff --git a/methods/mirror.h b/methods/mirror.h index 0a3ea6e92..bd807e122 100644 --- a/methods/mirror.h +++ b/methods/mirror.h @@ -34,6 +34,7 @@ class MirrorMethod : public HttpMethod protected: bool DownloadMirrorFile(string uri); + bool RandomizeMirrorFile(string file); string GetMirrorFileName(string uri); bool InitMirrors(); bool TryNextMirror(); -- cgit v1.2.3 From 0190e315a34377c658fa3acb5c658a6b4a52ae1e Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Mon, 14 Mar 2011 16:40:29 +0100 Subject: methods/mirror.cc: init random seed at startup --- methods/mirror.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'methods') diff --git a/methods/mirror.cc b/methods/mirror.cc index 08a603207..ed42cdbfb 100644 --- a/methods/mirror.cc +++ b/methods/mirror.cc @@ -157,7 +157,7 @@ bool MirrorMethod::RandomizeMirrorFile(string mirror_file) // read ifstream in(mirror_file.c_str()); - while ( ! in.eof() ) { + while ( !in.eof() ) { getline(in, line); content.push_back(line); } @@ -403,6 +403,8 @@ int main() { setlocale(LC_ALL, ""); + srand ( time(NULL) ); + MirrorMethod Mth; return Mth.Loop(); -- cgit v1.2.3 From 78c8f3cd7e0d2a61ee564cb010277bccb232b523 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Mon, 14 Mar 2011 18:03:14 +0100 Subject: methods/mirror.cc: randomize only based on hostname --- methods/mirror.cc | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'methods') diff --git a/methods/mirror.cc b/methods/mirror.cc index ed42cdbfb..e499b054b 100644 --- a/methods/mirror.cc +++ b/methods/mirror.cc @@ -16,12 +16,15 @@ #include #include -#include + #include +#include #include + #include #include #include +#include #include using namespace std; @@ -162,7 +165,17 @@ bool MirrorMethod::RandomizeMirrorFile(string mirror_file) content.push_back(line); } - // randomize + // we want the file to be random for each different machine, but also + // "stable" on the same machine. this is to avoid running into out-of-sync + // issues (i.e. Release/Release.gpg different on each mirror) + struct utsname buf; + int seed=1, i; + if(uname(&buf) == 0) { + for(i=0,seed=1; buf.nodename[i] != 0; i++) { + seed = seed * 31 + buf.nodename[i]; + } + } + srand( seed ); random_shuffle(content.begin(), content.end()); // write @@ -403,8 +416,6 @@ int main() { setlocale(LC_ALL, ""); - srand ( time(NULL) ); - MirrorMethod Mth; return Mth.Loop(); -- cgit v1.2.3