2016-09-23 09:02:17 +02:00
|
|
|
import logging
|
2016-12-14 23:14:37 +01:00
|
|
|
import random
|
|
|
|
import time
|
2016-09-23 09:02:17 +02:00
|
|
|
|
|
|
|
from twisted.internet import defer
|
|
|
|
from twisted.internet.task import LoopingCall
|
2016-10-06 04:58:34 +02:00
|
|
|
from decimal import Decimal
|
2016-09-23 09:02:17 +02:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-09-27 19:52:44 +02:00
|
|
|
class BlobAvailabilityTracker(object):
|
2016-09-23 09:02:17 +02:00
|
|
|
"""
|
2016-09-27 19:52:44 +02:00
|
|
|
Class to track peer counts for known blobs, and to discover new popular blobs
|
2016-09-23 09:02:17 +02:00
|
|
|
|
|
|
|
Attributes:
|
|
|
|
availability (dict): dictionary of peers for known blobs
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, blob_manager, peer_finder, dht_node):
|
|
|
|
self.availability = {}
|
2016-10-20 07:35:18 +02:00
|
|
|
self._last_mean_availability = Decimal(0.0)
|
2016-09-23 09:02:17 +02:00
|
|
|
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):
|
2016-11-03 20:42:45 +01:00
|
|
|
log.info("Starting %s", self)
|
2016-09-23 09:02:17 +02:00
|
|
|
self._check_popular.start(30)
|
2016-12-14 23:14:37 +01:00
|
|
|
self._check_mine.start(600)
|
2016-09-23 09:02:17 +02:00
|
|
|
|
|
|
|
def stop(self):
|
2016-11-03 20:42:45 +01:00
|
|
|
log.info("Stopping %s", self)
|
2016-09-23 09:02:17 +02:00
|
|
|
if self._check_popular.running:
|
|
|
|
self._check_popular.stop()
|
|
|
|
if self._check_mine.running:
|
|
|
|
self._check_mine.stop()
|
|
|
|
|
2016-09-30 19:28:01 +02:00
|
|
|
def get_blob_availability(self, blob):
|
|
|
|
def _get_peer_count(peers):
|
2016-09-30 20:26:13 +02:00
|
|
|
have_blob = sum(1 for peer in peers if peer.is_available())
|
2016-09-30 19:28:01 +02:00
|
|
|
return {blob: have_blob}
|
|
|
|
|
|
|
|
d = self._peer_finder.find_peers_for_blob(blob)
|
|
|
|
d.addCallback(_get_peer_count)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def get_availability_for_blobs(self, blobs):
|
2016-09-30 20:26:13 +02:00
|
|
|
dl = [self.get_blob_availability(blob) for blob in blobs if blob]
|
2016-09-30 19:28:01 +02:00
|
|
|
d = defer.DeferredList(dl)
|
2016-10-13 19:35:55 +02:00
|
|
|
d.addCallback(lambda results: [val for success, val in results if success])
|
2016-09-30 19:28:01 +02:00
|
|
|
return d
|
|
|
|
|
2016-10-20 07:35:18 +02:00
|
|
|
@property
|
|
|
|
def last_mean_availability(self):
|
|
|
|
return max(Decimal(0.01), self._last_mean_availability)
|
|
|
|
|
|
|
|
|
2016-09-23 09:02:17 +02:00
|
|
|
def _update_peers_for_blob(self, blob):
|
|
|
|
def _save_peer_info(blob_hash, peers):
|
|
|
|
v = {blob_hash: peers}
|
|
|
|
self.availability.update(v)
|
|
|
|
return v
|
|
|
|
|
|
|
|
d = self._peer_finder.find_peers_for_blob(blob)
|
|
|
|
d.addCallback(lambda r: [[c.host, c.port, c.is_available()] for c in r])
|
|
|
|
d.addCallback(lambda peers: _save_peer_info(blob, peers))
|
|
|
|
return d
|
|
|
|
|
2016-10-13 19:35:55 +02:00
|
|
|
def _get_most_popular(self):
|
|
|
|
dl = []
|
|
|
|
for (hash, _) in self._dht_node.get_most_popular_hashes(100):
|
|
|
|
encoded = hash.encode('hex')
|
|
|
|
dl.append(self._update_peers_for_blob(encoded))
|
|
|
|
return defer.DeferredList(dl)
|
2016-10-01 07:09:27 +02:00
|
|
|
|
2016-10-13 19:35:55 +02:00
|
|
|
def _update_most_popular(self):
|
|
|
|
d = self._get_most_popular()
|
2016-12-14 23:14:37 +01:00
|
|
|
d.addCallback(lambda _: self._set_mean_peers())
|
|
|
|
|
2016-09-23 09:02:17 +02:00
|
|
|
|
|
|
|
def _update_mine(self):
|
|
|
|
def _get_peers(blobs):
|
|
|
|
dl = []
|
|
|
|
for hash in blobs:
|
|
|
|
dl.append(self._update_peers_for_blob(hash))
|
|
|
|
return defer.DeferredList(dl)
|
2016-10-01 07:09:27 +02:00
|
|
|
|
2016-12-14 23:14:37 +01:00
|
|
|
def sample(blobs):
|
|
|
|
return random.sample(blobs, 100)
|
|
|
|
|
|
|
|
start = time.time()
|
|
|
|
log.debug('==> Updating the peers for my blobs')
|
2016-09-23 09:02:17 +02:00
|
|
|
d = self._blob_manager.get_all_verified_blobs()
|
2016-12-14 23:14:37 +01:00
|
|
|
# as far as I can tell, this only is used to set _last_mean_availability
|
|
|
|
# which... seems like a very expensive operation for such little payoff.
|
|
|
|
# so taking a sample should get about the same effect as querying the entire
|
|
|
|
# list of blobs
|
|
|
|
d.addCallback(sample)
|
2016-09-23 09:02:17 +02:00
|
|
|
d.addCallback(_get_peers)
|
2016-12-14 23:14:37 +01:00
|
|
|
d.addCallback(lambda _: self._set_mean_peers())
|
|
|
|
d.addCallback(lambda _: log.debug('<== Done updating peers for my blobs. Took %s seconds',
|
|
|
|
time.time() - start))
|
|
|
|
# although unused, need to return or else the looping call
|
|
|
|
# could overrun on a previous call
|
|
|
|
return d
|
2016-09-23 09:02:17 +02:00
|
|
|
|
2016-12-14 23:14:37 +01:00
|
|
|
def _set_mean_peers(self):
|
2016-09-23 09:02:17 +02:00
|
|
|
num_peers = [len(self.availability[blob]) for blob in self.availability]
|
2016-10-06 04:58:34 +02:00
|
|
|
mean = Decimal(sum(num_peers)) / Decimal(max(1, len(num_peers)))
|
2016-10-20 07:35:18 +02:00
|
|
|
self._last_mean_availability = mean
|