2019-01-22 18:52:32 +01:00
|
|
|
import asyncio
|
|
|
|
import typing
|
|
|
|
import logging
|
2019-06-21 02:55:47 +02:00
|
|
|
from lbry.utils import cache_concurrent
|
|
|
|
from lbry.blob_exchange.client import request_blob
|
2021-07-20 15:50:24 +02:00
|
|
|
from lbry.dht.node import get_kademlia_peers_from_hosts
|
2019-01-22 18:52:32 +01:00
|
|
|
if typing.TYPE_CHECKING:
|
2019-06-21 02:55:47 +02:00
|
|
|
from lbry.conf import Config
|
|
|
|
from lbry.dht.node import Node
|
|
|
|
from lbry.dht.peer import KademliaPeer
|
|
|
|
from lbry.blob.blob_manager import BlobManager
|
|
|
|
from lbry.blob.blob_file import AbstractBlob
|
2019-08-16 21:52:02 +02:00
|
|
|
from lbry.blob_exchange.client import BlobExchangeClientProtocol
|
2019-01-22 18:52:32 +01:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-01-30 20:57:09 +01:00
|
|
|
class BlobDownloader:
|
2019-04-30 22:56:29 +02:00
|
|
|
BAN_FACTOR = 2.0 # fixme: when connection manager gets implemented, move it out from here
|
2019-03-28 19:51:55 +01:00
|
|
|
|
2019-08-02 19:14:41 +02:00
|
|
|
def __init__(self, loop: asyncio.AbstractEventLoop, config: 'Config', blob_manager: 'BlobManager',
|
2019-01-30 20:57:09 +01:00
|
|
|
peer_queue: asyncio.Queue):
|
2019-01-22 18:52:32 +01:00
|
|
|
self.loop = loop
|
2019-01-30 20:57:09 +01:00
|
|
|
self.config = config
|
2019-01-22 18:52:32 +01:00
|
|
|
self.blob_manager = blob_manager
|
2019-01-30 20:57:09 +01:00
|
|
|
self.peer_queue = peer_queue
|
2019-01-31 19:05:00 +01:00
|
|
|
self.active_connections: typing.Dict['KademliaPeer', asyncio.Task] = {} # active request_blob calls
|
2019-02-18 21:11:07 +01:00
|
|
|
self.ignored: typing.Dict['KademliaPeer', int] = {}
|
2019-01-31 18:28:59 +01:00
|
|
|
self.scores: typing.Dict['KademliaPeer', int] = {}
|
2019-04-30 22:56:29 +02:00
|
|
|
self.failures: typing.Dict['KademliaPeer', int] = {}
|
2019-11-26 20:53:14 +01:00
|
|
|
self.connection_failures: typing.Set['KademliaPeer'] = set()
|
2019-08-16 21:52:02 +02:00
|
|
|
self.connections: typing.Dict['KademliaPeer', 'BlobExchangeClientProtocol'] = {}
|
2019-05-06 21:04:38 +02:00
|
|
|
self.is_running = asyncio.Event(loop=self.loop)
|
2019-01-22 18:52:32 +01:00
|
|
|
|
2019-03-31 01:17:42 +01:00
|
|
|
def should_race_continue(self, blob: 'AbstractBlob'):
|
2019-07-25 03:32:20 +02:00
|
|
|
max_probes = self.config.max_connections_per_download * (1 if self.connections else 10)
|
|
|
|
if len(self.active_connections) >= max_probes:
|
2019-02-08 05:28:03 +01:00
|
|
|
return False
|
2019-03-31 01:17:42 +01:00
|
|
|
return not (blob.get_is_verified() or not blob.is_writeable())
|
2019-02-08 05:28:03 +01:00
|
|
|
|
2019-07-25 00:59:12 +02:00
|
|
|
async def request_blob_from_peer(self, blob: 'AbstractBlob', peer: 'KademliaPeer', connection_id: int = 0,
|
|
|
|
just_probe: bool = False):
|
2019-02-04 22:03:08 +01:00
|
|
|
if blob.get_is_verified():
|
|
|
|
return
|
2019-02-08 07:05:53 +01:00
|
|
|
start = self.loop.time()
|
2019-08-16 21:52:02 +02:00
|
|
|
bytes_received, protocol = await request_blob(
|
2019-07-25 00:59:12 +02:00
|
|
|
self.loop, blob if not just_probe else None, peer.address, peer.tcp_port, self.config.peer_connect_timeout,
|
2019-08-16 21:52:02 +02:00
|
|
|
self.config.blob_download_timeout, connected_protocol=self.connections.get(peer),
|
|
|
|
connection_id=connection_id, connection_manager=self.blob_manager.connection_manager
|
2019-02-04 22:03:08 +01:00
|
|
|
)
|
2019-08-16 21:52:02 +02:00
|
|
|
if not bytes_received and not protocol and peer not in self.connection_failures:
|
2019-11-26 20:53:14 +01:00
|
|
|
self.connection_failures.add(peer)
|
2019-08-16 21:52:02 +02:00
|
|
|
if not protocol and peer not in self.ignored:
|
2019-02-18 21:11:07 +01:00
|
|
|
self.ignored[peer] = self.loop.time()
|
2019-02-04 22:03:08 +01:00
|
|
|
log.debug("drop peer %s:%i", peer.address, peer.tcp_port)
|
2019-04-30 22:56:29 +02:00
|
|
|
self.failures[peer] = self.failures.get(peer, 0) + 1
|
2019-02-08 06:27:58 +01:00
|
|
|
if peer in self.connections:
|
|
|
|
del self.connections[peer]
|
2019-08-16 21:52:02 +02:00
|
|
|
elif protocol:
|
2019-02-04 22:03:08 +01:00
|
|
|
log.debug("keep peer %s:%i", peer.address, peer.tcp_port)
|
2019-05-01 07:55:51 +02:00
|
|
|
self.failures[peer] = 0
|
2019-08-16 21:52:02 +02:00
|
|
|
self.connections[peer] = protocol
|
2019-05-02 21:41:48 +02:00
|
|
|
elapsed = self.loop.time() - start
|
2019-05-13 19:24:13 +02:00
|
|
|
self.scores[peer] = bytes_received / elapsed if bytes_received and elapsed else 1
|
2019-01-22 18:52:32 +01:00
|
|
|
|
2019-04-30 22:56:29 +02:00
|
|
|
async def new_peer_or_finished(self):
|
2019-05-01 07:55:51 +02:00
|
|
|
active_tasks = list(self.active_connections.values()) + [asyncio.sleep(1)]
|
2019-04-30 22:56:29 +02:00
|
|
|
await asyncio.wait(active_tasks, loop=self.loop, return_when='FIRST_COMPLETED')
|
2019-01-22 18:52:32 +01:00
|
|
|
|
2019-02-08 00:11:28 +01:00
|
|
|
def cleanup_active(self):
|
2019-05-13 19:24:13 +02:00
|
|
|
if not self.active_connections and not self.connections:
|
2019-05-13 18:14:08 +02:00
|
|
|
self.clearbanned()
|
2019-02-08 00:11:28 +01:00
|
|
|
to_remove = [peer for (peer, task) in self.active_connections.items() if task.done()]
|
|
|
|
for peer in to_remove:
|
|
|
|
del self.active_connections[peer]
|
|
|
|
|
2019-02-18 21:11:07 +01:00
|
|
|
def clearbanned(self):
|
|
|
|
now = self.loop.time()
|
2019-10-02 18:38:56 +02:00
|
|
|
self.ignored = {
|
|
|
|
peer: when for (peer, when) in self.ignored.items()
|
2019-05-01 23:04:45 +02:00
|
|
|
if (now - when) < min(30.0, (self.failures.get(peer, 0) ** self.BAN_FACTOR))
|
2019-10-02 18:38:56 +02:00
|
|
|
}
|
2019-02-18 21:11:07 +01:00
|
|
|
|
2019-03-31 03:05:46 +02:00
|
|
|
@cache_concurrent
|
2019-05-06 02:22:10 +02:00
|
|
|
async def download_blob(self, blob_hash: str, length: typing.Optional[int] = None,
|
|
|
|
connection_id: int = 0) -> 'AbstractBlob':
|
2019-01-30 20:57:09 +01:00
|
|
|
blob = self.blob_manager.get_blob(blob_hash, length)
|
|
|
|
if blob.get_is_verified():
|
|
|
|
return blob
|
2019-05-06 21:04:38 +02:00
|
|
|
self.is_running.set()
|
2019-01-22 18:52:32 +01:00
|
|
|
try:
|
2019-05-06 21:04:38 +02:00
|
|
|
while not blob.get_is_verified() and self.is_running.is_set():
|
2019-11-26 20:53:14 +01:00
|
|
|
batch: typing.Set['KademliaPeer'] = set(self.connections.keys())
|
2019-01-30 20:57:09 +01:00
|
|
|
while not self.peer_queue.empty():
|
2019-05-01 23:04:45 +02:00
|
|
|
batch.update(self.peer_queue.get_nowait())
|
2019-02-08 00:11:28 +01:00
|
|
|
log.debug(
|
2019-11-26 20:53:14 +01:00
|
|
|
"%s running, %d peers, %d ignored, %d active, %s connections", blob_hash[:6],
|
2019-07-25 03:32:20 +02:00
|
|
|
len(batch), len(self.ignored), len(self.active_connections), len(self.connections)
|
2019-02-08 00:11:28 +01:00
|
|
|
)
|
2019-05-01 23:04:45 +02:00
|
|
|
for peer in sorted(batch, key=lambda peer: self.scores.get(peer, 0), reverse=True):
|
2019-11-26 20:53:14 +01:00
|
|
|
if peer in self.ignored:
|
|
|
|
continue
|
2022-02-09 01:41:52 +01:00
|
|
|
if peer in self.active_connections or not self.should_race_continue(blob):
|
2019-07-25 03:32:20 +02:00
|
|
|
continue
|
|
|
|
log.debug("request %s from %s:%i", blob_hash[:8], peer.address, peer.tcp_port)
|
2019-11-26 20:53:14 +01:00
|
|
|
t = self.loop.create_task(self.request_blob_from_peer(blob, peer, connection_id))
|
2019-07-25 03:32:20 +02:00
|
|
|
self.active_connections[peer] = t
|
2022-02-09 01:41:52 +01:00
|
|
|
self.peer_queue.put_nowait(list(batch))
|
2019-04-30 22:56:29 +02:00
|
|
|
await self.new_peer_or_finished()
|
2019-02-08 05:04:38 +01:00
|
|
|
self.cleanup_active()
|
2019-02-04 22:09:50 +01:00
|
|
|
log.debug("downloaded %s", blob_hash[:8])
|
2019-01-30 20:57:09 +01:00
|
|
|
return blob
|
2019-02-22 04:03:40 +01:00
|
|
|
finally:
|
2019-03-31 01:17:42 +01:00
|
|
|
blob.close()
|
2019-11-26 20:53:14 +01:00
|
|
|
if self.loop.is_running():
|
|
|
|
self.loop.call_soon(self.cleanup_active)
|
2019-01-30 20:57:09 +01:00
|
|
|
|
2019-02-08 06:27:58 +01:00
|
|
|
def close(self):
|
2019-08-05 15:24:23 +02:00
|
|
|
self.connection_failures.clear()
|
2019-02-18 21:11:07 +01:00
|
|
|
self.scores.clear()
|
|
|
|
self.ignored.clear()
|
2019-05-06 21:04:38 +02:00
|
|
|
self.is_running.clear()
|
2019-08-16 21:52:02 +02:00
|
|
|
for protocol in self.connections.values():
|
|
|
|
protocol.close()
|
2019-02-08 06:27:58 +01:00
|
|
|
|
2019-01-30 20:57:09 +01:00
|
|
|
|
2021-07-20 15:50:24 +02:00
|
|
|
async def download_blob(loop, config: 'Config', blob_manager: 'BlobManager', dht_node: 'Node',
|
2019-03-31 01:17:42 +01:00
|
|
|
blob_hash: str) -> 'AbstractBlob':
|
2019-01-31 18:28:59 +01:00
|
|
|
search_queue = asyncio.Queue(loop=loop, maxsize=config.max_connections_per_download)
|
2019-01-30 20:57:09 +01:00
|
|
|
search_queue.put_nowait(blob_hash)
|
2021-07-20 15:50:24 +02:00
|
|
|
peer_queue, accumulate_task = dht_node.accumulate_peers(search_queue)
|
|
|
|
fixed_peers = None if not config.fixed_peers else await get_kademlia_peers_from_hosts(config.fixed_peers)
|
|
|
|
if fixed_peers:
|
|
|
|
loop.call_later(config.fixed_peer_delay, peer_queue.put_nowait, fixed_peers)
|
2019-01-30 20:57:09 +01:00
|
|
|
downloader = BlobDownloader(loop, config, blob_manager, peer_queue)
|
|
|
|
try:
|
|
|
|
return await downloader.download_blob(blob_hash)
|
|
|
|
finally:
|
|
|
|
if accumulate_task and not accumulate_task.done():
|
|
|
|
accumulate_task.cancel()
|
2019-02-08 06:27:58 +01:00
|
|
|
downloader.close()
|