2019-01-22 18:49:43 +01:00
|
|
|
import asyncio
|
|
|
|
import typing
|
|
|
|
import logging
|
2022-01-24 14:54:19 +01:00
|
|
|
|
2022-01-24 15:20:48 +01:00
|
|
|
from prometheus_client import Counter, Gauge
|
2022-01-24 14:54:19 +01:00
|
|
|
|
2019-01-22 18:49:43 +01:00
|
|
|
if typing.TYPE_CHECKING:
|
2019-06-21 02:55:47 +02:00
|
|
|
from lbry.dht.node import Node
|
|
|
|
from lbry.extras.daemon.storage import SQLiteStorage
|
2019-01-22 18:49:43 +01:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class BlobAnnouncer:
|
2022-01-24 14:54:19 +01:00
|
|
|
announcements_sent_metric = Counter(
|
|
|
|
"announcements_sent", "Number of announcements sent and their respective status.", namespace="dht_node",
|
|
|
|
labelnames=("peers", "error"),
|
|
|
|
)
|
2022-01-24 15:20:48 +01:00
|
|
|
announcement_queue_size_metric = Gauge(
|
|
|
|
"announcement_queue_size", "Number of hashes waiting to be announced.", namespace="dht_node",
|
|
|
|
labelnames=("scope",)
|
|
|
|
)
|
2022-01-24 14:54:19 +01:00
|
|
|
|
2019-08-02 19:14:41 +02:00
|
|
|
def __init__(self, loop: asyncio.AbstractEventLoop, node: 'Node', storage: 'SQLiteStorage'):
|
2019-01-22 18:49:43 +01:00
|
|
|
self.loop = loop
|
|
|
|
self.node = node
|
|
|
|
self.storage = storage
|
|
|
|
self.announce_task: asyncio.Task = None
|
|
|
|
self.announce_queue: typing.List[str] = []
|
2022-02-18 22:47:01 +01:00
|
|
|
self._done = asyncio.Event()
|
2022-02-15 04:55:36 +01:00
|
|
|
self.announced = set()
|
2019-01-22 18:49:43 +01:00
|
|
|
|
2022-02-15 04:55:36 +01:00
|
|
|
async def _run_consumer(self):
|
|
|
|
while self.announce_queue:
|
|
|
|
try:
|
|
|
|
blob_hash = self.announce_queue.pop()
|
|
|
|
peers = len(await self.node.announce_blob(blob_hash))
|
|
|
|
self.announcements_sent_metric.labels(peers=peers, error=False).inc()
|
|
|
|
if peers > 4:
|
|
|
|
self.announced.add(blob_hash)
|
|
|
|
else:
|
|
|
|
log.debug("failed to announce %s, could only find %d peers, retrying soon.", blob_hash[:8], peers)
|
|
|
|
except Exception as err:
|
|
|
|
self.announcements_sent_metric.labels(peers=0, error=True).inc()
|
|
|
|
log.warning("error announcing %s: %s", blob_hash[:8], str(err))
|
2019-01-22 18:49:43 +01:00
|
|
|
|
2019-05-07 06:33:15 +02:00
|
|
|
async def _announce(self, batch_size: typing.Optional[int] = 10):
|
|
|
|
while batch_size:
|
|
|
|
if not self.node.joined.is_set():
|
|
|
|
await self.node.joined.wait()
|
2021-08-21 05:33:21 +02:00
|
|
|
await asyncio.sleep(60)
|
2019-05-12 05:52:46 +02:00
|
|
|
if not self.node.protocol.routing_table.get_peers():
|
|
|
|
log.warning("No peers in DHT, announce round skipped")
|
|
|
|
continue
|
2019-05-07 06:33:15 +02:00
|
|
|
self.announce_queue.extend(await self.storage.get_blobs_to_announce())
|
2022-01-24 15:20:48 +01:00
|
|
|
self.announcement_queue_size_metric.labels(scope="global").set(len(self.announce_queue))
|
2019-05-07 06:33:15 +02:00
|
|
|
log.debug("announcer task wake up, %d blobs to announce", len(self.announce_queue))
|
2020-01-03 05:31:28 +01:00
|
|
|
while len(self.announce_queue) > 0:
|
2019-05-07 06:56:13 +02:00
|
|
|
log.info("%i blobs to announce", len(self.announce_queue))
|
2021-08-21 05:33:21 +02:00
|
|
|
await asyncio.gather(*[self._run_consumer() for _ in range(batch_size)])
|
2022-02-15 04:55:36 +01:00
|
|
|
announced = list(filter(None, self.announced))
|
2019-05-07 06:33:15 +02:00
|
|
|
if announced:
|
|
|
|
await self.storage.update_last_announced_blobs(announced)
|
|
|
|
log.info("announced %i blobs", len(announced))
|
2022-02-15 04:55:36 +01:00
|
|
|
self.announced.clear()
|
2022-02-18 22:47:01 +01:00
|
|
|
self._done.set()
|
|
|
|
self._done.clear()
|
2019-01-22 18:49:43 +01:00
|
|
|
|
|
|
|
def start(self, batch_size: typing.Optional[int] = 10):
|
2019-05-07 06:33:15 +02:00
|
|
|
assert not self.announce_task or self.announce_task.done(), "already running"
|
|
|
|
self.announce_task = self.loop.create_task(self._announce(batch_size))
|
2019-01-22 18:49:43 +01:00
|
|
|
|
|
|
|
def stop(self):
|
2019-05-07 06:33:15 +02:00
|
|
|
if self.announce_task and not self.announce_task.done():
|
|
|
|
self.announce_task.cancel()
|
2022-02-18 22:47:01 +01:00
|
|
|
|
|
|
|
def wait(self):
|
|
|
|
return self._done.wait()
|