From 4bd9f3bd681f6d8628ce3c41e6d677ff7a506725 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Wed, 23 May 2018 16:48:47 -0400 Subject: [PATCH] remove popular hash tracking, simplify DHTPeerFinder --- lbrynet/core/BlobAvailability.py | 15 ----------- lbrynet/dht/node.py | 3 --- lbrynet/dht/peerfinder.py | 46 +++++++------------------------- 3 files changed, 10 insertions(+), 54 deletions(-) diff --git a/lbrynet/core/BlobAvailability.py b/lbrynet/core/BlobAvailability.py index 5ce8b95ea..cc9d446d1 100644 --- a/lbrynet/core/BlobAvailability.py +++ b/lbrynet/core/BlobAvailability.py @@ -23,18 +23,14 @@ class BlobAvailabilityTracker(object): self._blob_manager = blob_manager self._peer_finder = peer_finder self._dht_node = dht_node - self._check_popular = LoopingCall(self._update_most_popular) self._check_mine = LoopingCall(self._update_mine) def start(self): log.info("Starting blob availability tracker.") - self._check_popular.start(600) self._check_mine.start(600) def stop(self): log.info("Stopping blob availability tracker.") - if self._check_popular.running: - self._check_popular.stop() if self._check_mine.running: self._check_mine.stop() @@ -68,17 +64,6 @@ class BlobAvailabilityTracker(object): d.addCallback(lambda peers: _save_peer_info(blob, peers)) return d - def _get_most_popular(self): - dl = [] - for (hash, _) in self._dht_node.get_most_popular_hashes(10): - encoded = hash.encode('hex') - dl.append(self._update_peers_for_blob(encoded)) - return defer.DeferredList(dl) - - def _update_most_popular(self): - d = self._get_most_popular() - d.addCallback(lambda _: self._set_mean_peers()) - def _update_mine(self): def _get_peers(blobs): dl = [] diff --git a/lbrynet/dht/node.py b/lbrynet/dht/node.py index 7c48e8c31..804ba85ef 100644 --- a/lbrynet/dht/node.py +++ b/lbrynet/dht/node.py @@ -261,9 +261,6 @@ class Node(object): expanded_peers.append((host, port, peer_node_id)) defer.returnValue(expanded_peers) - def get_most_popular_hashes(self, num_to_return): - return self.hash_watcher.most_popular_hashes(num_to_return) - @defer.inlineCallbacks def iterativeAnnounceHaveBlob(self, blob_hash, value): known_nodes = {} diff --git a/lbrynet/dht/peerfinder.py b/lbrynet/dht/peerfinder.py index afbbddd6b..4074807ea 100644 --- a/lbrynet/dht/peerfinder.py +++ b/lbrynet/dht/peerfinder.py @@ -4,7 +4,6 @@ import logging from zope.interface import implements from twisted.internet import defer from lbrynet.interfaces import IPeerFinder -from lbrynet.core.utils import short_hash log = logging.getLogger(__name__) @@ -13,18 +12,9 @@ log = logging.getLogger(__name__) class DummyPeerFinder(object): """This class finds peers which have announced to the DHT that they have certain blobs""" - def run_manage_loop(self): - pass - - def stop(self): - pass - - def find_peers_for_blob(self, blob_hash): + def find_peers_for_blob(self, blob_hash, timeout=None, filter_self=True): return defer.succeed([]) - def get_most_popular_hashes(self, num_to_return): - return [] - class DHTPeerFinder(DummyPeerFinder): """This class finds peers which have announced to the DHT that they have certain blobs""" @@ -39,11 +29,8 @@ class DHTPeerFinder(DummyPeerFinder): self.peer_manager = peer_manager self.peers = [] - def stop(self): - pass - @defer.inlineCallbacks - def find_peers_for_blob(self, blob_hash, timeout=None, filter_self=False): + def find_peers_for_blob(self, blob_hash, timeout=None, filter_self=True): """ Find peers for blob in the DHT blob_hash (str): blob hash to look for @@ -54,32 +41,19 @@ class DHTPeerFinder(DummyPeerFinder): Returns: list of peers for the blob """ - def _trigger_timeout(): - if not finished_deferred.called: - log.debug("Peer search for %s timed out", short_hash(blob_hash)) - finished_deferred.cancel() - bin_hash = binascii.unhexlify(blob_hash) - finished_deferred = self.dht_node.getPeersForBlob(bin_hash) - - if timeout is not None: - self.dht_node.reactor_callLater(timeout, _trigger_timeout) - + finished_deferred = self.dht_node.iterativeFindValue(bin_hash) + if timeout: + finished_deferred.addTimeout(timeout, self.dht_node.clock) try: peer_list = yield finished_deferred - except defer.CancelledError: + except defer.TimeoutError: peer_list = [] peers = set(peer_list) - good_peers = [] - for host, port in peers: + results = [] + for node_id, host, port in peers: if filter_self and (host, port) == (self.dht_node.externalIP, self.dht_node.peerPort): continue - peer = self.peer_manager.get_peer(host, port) - if peer.is_available() is True: - good_peers.append(peer) - - defer.returnValue(good_peers) - - def get_most_popular_hashes(self, num_to_return): - return self.dht_node.get_most_popular_hashes(num_to_return) + results.append(self.peer_manager.get_peer(host, port)) + defer.returnValue(results)