Skip to content

Commit

Permalink
Better tracking in downloading objects
Browse files Browse the repository at this point in the history
- remember what was requested from which node
- remember if it was received
- re-request object if we haven't received any new object for more than
  a minute
  • Loading branch information
PeterSurda committed Jan 16, 2017
1 parent 8dfa0fa commit ca6bc99
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 21 deletions.
19 changes: 9 additions & 10 deletions src/class_receiveDataThread.py
Expand Up @@ -217,16 +217,15 @@ def processData(self):
self.data = self.data[payloadLength + protocol.Header.size:] # take this message out and then process the next message

if self.data == '': # if there are no more messages
if Missing().len() > 0 and self.sendDataThreadQueue.qsize() < 10:
for objectHash in Missing().pull(100):
if self.sendDataThreadQueue.full():
break
if objectHash in Inventory():
logger.debug('Inventory already has object listed in inv message.')
Missing().delete(objectHash)
else:
# We don't have the object in our inventory. Let's request it.
self.sendgetdata(objectHash)
for objectHash in Missing().pull(100):
if self.sendDataThreadQueue.full():
break
if objectHash in Inventory():
logger.debug('Inventory already has object listed in inv message.')
Missing().delete(objectHash)
else:
# We don't have the object in our inventory. Let's request it.
self.sendgetdata(objectHash)
self.processData()


Expand Down
53 changes: 42 additions & 11 deletions src/inventory.py
Expand Up @@ -37,7 +37,7 @@ def __setitem__(self, hash, value):
value = self.InventoryItem(*value)
self._inventory[hash] = value
self._streams[value.stream].add(hash)
Missing().delete(hash)
Missing().delete(hash, True)

def __delitem__(self, hash):
raise NotImplementedError
Expand Down Expand Up @@ -92,15 +92,25 @@ def __init__(self):
self.hashes = {}
self.stopped = False
self.frequency = 60
self.pending = {}

def add(self, objectHash):
if self.stopped:
return
with self.lock:
if not objectHash in self.hashes:
if objectHash not in self.hashes:
self.hashes[objectHash] = {'peers':[], 'requested':0}
self.hashes[objectHash]['peers'].append(current_thread().peer)

def addPending(self, objectHash=None):
if self.stopped:
return
if current_thread().peer not in self.pending:
self.pending[current_thread().peer] = {'objects':[], 'requested':0, 'received':0}
if objectHash not in self.pending[current_thread().peer]['objects'] and not objectHash is None:
self.pending[current_thread().peer]['objects'].append(objectHash)
self.pending[current_thread().peer]['requested'] = time.time()

def len(self):
with self.lock:
return len(self.hashes)
Expand All @@ -122,33 +132,54 @@ def pull(self, count=1):
if count < 1:
raise ValueError("Must be at least one")
objectHashes = []
if self.stopped:
return objectHashes
try:
for objectHash in self.hashes.keys():
if len(objectHashes) >= count:
break
if current_thread().peer not in self.pending:
self.addPending()
if (self.pending[current_thread().peer]['requested'] >= time.time() - self.frequency or \
self.pending[current_thread().peer]['received'] >= time.time() - self.frequency) and \
len(self.pending[current_thread().peer]['objects']) >= count:
break
# requested too long ago or not at all
if self.hashes[objectHash]['requested'] < time.time() - self.frequency:
if len(self.hashes[objectHash]['peers']) == 0:
self.removeObjectFromCurrentThread(objectHash)
# already requested from this thread but haven't received yet
if objectHash in self.pending[current_thread().peer]['objects']:
if self.pending[current_thread().peer]['requested'] >= time.time() - self.frequency or self.pending[current_thread().peer]['received'] >= time.time() - self.frequency:
continue
elif current_thread().peer not in self.hashes[objectHash]['peers']:
continue
if current_thread().peer in self.hashes[objectHash]['peers']:
objectHashes.append(objectHash)
self.hashes[objectHash]['requested'] = time.time()
self.removeObjectFromCurrentThread(objectHash)
if len(objectHashes) >= count:
break
objectHashes.append(objectHash)
self.hashes[objectHash]['requested'] = time.time()
self.pending[current_thread().peer]['requested'] = time.time()
self.addPending(objectHash)
except (RuntimeError, KeyError, ValueError):
# the for cycle sometimes breaks if you remove elements
pass
return objectHashes

def delete(self, objectHash):
def delete(self, objectHash, justReceived=False):
with self.lock:
if objectHash in self.hashes:
del self.hashes[objectHash]
if objectHash in self.pending[current_thread().peer]['objects']:
self.pending[current_thread().peer]['objects'].remove(objectHash)
if justReceived:
self.pending[current_thread().peer]['received'] = time.time()

def stop(self):
with self.lock:
self.hashes = {}
self.pending = {}

def threadEnd(self):
with self.lock:
for objectHash in self.hashes:
self.removeObjectFromCurrentThread(objectHash)
try:
del self.pending[current_thread().peer]
except KeyError:
pass

0 comments on commit ca6bc99

Please sign in to comment.