Use the mirror as a peer when there are few peers for a file.
authorCameron Dale <camrdale@gmail.com>
Mon, 14 Apr 2008 03:18:17 +0000 (20:18 -0700)
committerCameron Dale <camrdale@gmail.com>
Mon, 14 Apr 2008 03:18:17 +0000 (20:18 -0700)
apt-p2p.conf
apt_p2p/HTTPDownloader.py
apt_p2p/PeerManager.py
apt_p2p/apt_p2p_conf.py
debian/apt-p2p.conf.sgml
test.py

index 249de43f24d8851a75528d46607ca6ab8422b96b..6121547c516d9399e33915707d7ab0b8f817e53a 100644 (file)
@@ -22,6 +22,12 @@ PORT = 9977
 # Set this to 0 to not limit the upload bandwidth.
 UPLOAD_LIMIT = 0
 
+# The minimum number of peers before the mirror is not used.
+# If there are fewer peers than this for a file, the mirror will also be
+# used to speed up the download. Set to 0 to never use the mirror if
+# there are peers.
+MIN_DOWNLOAD_PEERS = 3
+
 # Directory to store the downloaded files in
 CACHE_DIR = /var/cache/apt-p2p
     
index 15ee5640696bc777b76b6a1ae4f7c900fcf9de2a..057b5a2befdc6783d1c2e295d8c6b9024c782231 100644 (file)
@@ -31,6 +31,7 @@ class Peer(ClientFactory):
     def __init__(self, host, port=80):
         self.host = host
         self.port = port
+        self.mirror = False
         self.rank = 0.5
         self.busy = False
         self.pipeline = False
@@ -47,7 +48,7 @@ class Peer(ClientFactory):
         self._responseTimes = []
     
     def __repr__(self):
-        return "(%s, %d, %0.5f)" % (self.host, self.port, self.rank)
+        return "(%r, %r, %r)" % (self.host, self.port, self.rank)
         
     #{ Manage the request queue
     def connect(self):
index e19c7b40dc0e5b691f2eea00d01928dab3590e37..e9d1ecb590702de992bec32c14f3803fa0d6b6b9 100644 (file)
@@ -17,6 +17,7 @@ from HTTPDownloader import Peer
 from util import uncompact
 from Hash import PIECE_SIZE
 from apt_p2p_Khashmir.bencode import bdecode
+from apt_p2p_conf import config
 
 class GrowingFileStream(stream.FileStream):
     """Modified to stream data from a file as it becomes available.
@@ -261,6 +262,7 @@ class FileDownload:
         self.compact_peers = compact_peers
         
         self.path = '/~/' + quote_plus(hash.expected())
+        self.mirror_path = None
         self.pieces = None
         self.started = False
         
@@ -486,6 +488,16 @@ class FileDownload:
         assert self.pieces, "You must initialize the piece hashes first"
         self.peerlist = [self.peers[site]['peer'] for site in self.peers]
         
+        # Use the mirror if there are few peers
+        if len(self.peerlist) < config.getint('DEFAULT', 'MIN_DOWNLOAD_PEERS'):
+            parsed = urlparse(self.mirror)
+            if parsed[0] == "http":
+                site = splitHostPort(parsed[0], parsed[1])
+                self.mirror_path = urlunparse(('', '') + parsed[2:])
+                peer = self.manager.getPeer(site)
+                peer.mirror = True
+                self.peerlist.append(peer)
+        
         # Special case if there's only one good peer left
 #        if len(self.peerlist) == 1:
 #            log.msg('Downloading from peer %r' % (self.peerlist[0], ))
@@ -518,7 +530,10 @@ class FileDownload:
                 log.msg('Sending a request for piece %d to peer %r' % (piece, peer))
                 
                 self.outstanding += 1
-                df = peer.getRange(self.path, piece*PIECE_SIZE, (piece+1)*PIECE_SIZE - 1)
+                if peer.mirror:
+                    df = peer.getRange(self.mirror_path, piece*PIECE_SIZE, (piece+1)*PIECE_SIZE - 1)
+                else:
+                    df = peer.getRange(self.path, piece*PIECE_SIZE, (piece+1)*PIECE_SIZE - 1)
                 reactor.callLater(0, df.addCallbacks,
                                   *(self._getPiece, self._getError),
                                   **{'callbackArgs': (piece, peer),
@@ -635,6 +650,7 @@ class PeerManager:
             site = splitHostPort(parsed[0], parsed[1])
             path = urlunparse(('', '') + parsed[2:])
             peer = self.getPeer(site)
+            peer.mirror = True
             return peer.get(path, method, modtime)
 #        elif len(peers) == 1:
 #            site = uncompact(peers[0]['c'])
index fe56d7c9c456f7460ae517c38c116e7ff154021c..69f6a9c088bbe6641fbeaf9ebda4dabd6cf96750 100644 (file)
@@ -44,6 +44,12 @@ DEFAULTS = {
     # Set this to 0 to not limit the upload bandwidth.
     'UPLOAD_LIMIT': '0',
 
+    # The minimum number of peers before the mirror is not used.
+    # If there are fewer peers than this for a file, the mirror will also be
+    # used to speed up the download. Set to 0 to never use the mirror if
+    # there are peers.
+    'MIN_DOWNLOAD_PEERS': '3',
+
     # Directory to store the downloaded files in
     'CACHE_DIR': home + '/.apt-p2p/cache',
     
index ce9913744d0d0268a516078e3e09dd62cb224e95..722de014a4f19b674bb337153eaea566d397b34e 100644 (file)
                (Default is 0)</para>
            </listitem>
          </varlistentry>
+         <varlistentry>
+           <term><option>MIN_DOWNLOAD_PEERS = <replaceable>number</replaceable></option></term>
+            <listitem>
+             <para>The minimum <replaceable>number</replaceable> of peers before the mirror is not used.
+               If there are fewer peers than this for a file, the mirror will also be
+               used to speed up the download. Set to 0 to never use the mirror if
+               there are peers.                
+               (Default is 3)</para>
+           </listitem>
+         </varlistentry>
          <varlistentry>
            <term><option>CACHE_DIR = <replaceable>directory</replaceable></option></term>
             <listitem>
diff --git a/test.py b/test.py
index acbeeae50c808ae7d47144aae2276e4ffe447752..3ccaa14b3947a22d915757c09a7202966f861a16 100755 (executable)
--- a/test.py
+++ b/test.py
@@ -343,6 +343,12 @@ PORT = %(PORT)s
 # Set this to 0 to not limit the upload bandwidth.
 UPLOAD_LIMIT = 100
 
+# The minimum number of peers before the mirror is not used.
+# If there are fewer peers than this for a file, the mirror will also be
+# used to speed up the download. Set to 0 to never use the mirror if
+# there are peers.
+MIN_DOWNLOAD_PEERS = 3
+
 # Directory to store the downloaded files in
 CACHE_DIR = %(CACHE_DIR)s