2017-01-10 21:15:35 +01:00
|
|
|
import collections
|
2017-05-27 19:03:27 +02:00
|
|
|
from importlib import import_module
|
2017-01-19 19:48:12 +01:00
|
|
|
from threading import current_thread, enumerate as threadingEnumerate, RLock
|
2017-03-19 22:08:00 +01:00
|
|
|
import Queue
|
2017-01-10 21:15:35 +01:00
|
|
|
import time
|
2017-05-27 19:03:27 +02:00
|
|
|
import sys
|
2017-01-10 21:15:35 +01:00
|
|
|
|
2017-05-27 19:03:27 +02:00
|
|
|
from bmconfigparser import BMConfigParser
|
2017-01-10 21:15:35 +01:00
|
|
|
from helper_sql import *
|
|
|
|
from singleton import Singleton
|
|
|
|
|
2017-05-27 19:03:27 +02:00
|
|
|
# TODO make this dynamic, and watch out for frozen, like with messagetypes
|
|
|
|
import storage.sqlite
|
|
|
|
import storage.filesystem
|
2017-01-10 21:15:35 +01:00
|
|
|
|
|
|
|
@Singleton
|
2017-05-27 19:03:27 +02:00
|
|
|
class Inventory():
|
2017-01-10 21:15:35 +01:00
|
|
|
def __init__(self):
|
2017-05-27 19:03:27 +02:00
|
|
|
#super(self.__class__, self).__init__()
|
|
|
|
self._moduleName = BMConfigParser().safeGet("inventory", "storage")
|
|
|
|
#import_module("." + self._moduleName, "storage")
|
|
|
|
#import_module("storage." + self._moduleName)
|
|
|
|
self._className = "storage." + self._moduleName + "." + self._moduleName.title() + "Inventory"
|
|
|
|
self._inventoryClass = eval(self._className)
|
|
|
|
self._realInventory = self._inventoryClass()
|
|
|
|
|
|
|
|
# cheap inheritance copied from asyncore
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
try:
|
|
|
|
realRet = getattr(self._realInventory, attr)
|
|
|
|
except AttributeError:
|
|
|
|
raise AttributeError("%s instance has no attribute '%s'" %(self.__class__.__name__, attr))
|
|
|
|
else:
|
|
|
|
return realRet
|
2017-01-15 19:21:24 +01:00
|
|
|
|
|
|
|
|
2017-03-19 22:08:00 +01:00
|
|
|
class PendingDownloadQueue(Queue.Queue):
|
2017-01-19 19:48:12 +01:00
|
|
|
# keep a track of objects that have been advertised to us but we haven't downloaded them yet
|
2017-03-20 01:22:37 +01:00
|
|
|
maxWait = 300
|
|
|
|
|
2017-03-19 22:08:00 +01:00
|
|
|
def __init__(self, maxsize=0):
|
|
|
|
Queue.Queue.__init__(self, maxsize)
|
2017-01-15 22:21:19 +01:00
|
|
|
self.stopped = False
|
2017-03-20 01:22:37 +01:00
|
|
|
self.pending = {}
|
|
|
|
self.lock = RLock()
|
2017-03-19 22:08:00 +01:00
|
|
|
|
2017-03-20 01:22:37 +01:00
|
|
|
def task_done(self, hashId):
|
2017-03-19 22:08:00 +01:00
|
|
|
Queue.Queue.task_done(self)
|
2017-03-20 01:22:37 +01:00
|
|
|
try:
|
|
|
|
with self.lock:
|
|
|
|
del self.pending[hashId]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2017-03-19 22:08:00 +01:00
|
|
|
|
|
|
|
def get(self, block=True, timeout=None):
|
|
|
|
retval = Queue.Queue.get(self, block, timeout)
|
|
|
|
# no exception was raised
|
|
|
|
if not self.stopped:
|
2017-03-20 01:22:37 +01:00
|
|
|
with self.lock:
|
|
|
|
self.pending[retval] = time.time()
|
2017-03-19 22:08:00 +01:00
|
|
|
return retval
|
|
|
|
|
2017-03-20 01:22:37 +01:00
|
|
|
def clear(self):
|
|
|
|
with self.lock:
|
|
|
|
newPending = {}
|
|
|
|
for hashId in self.pending:
|
|
|
|
if self.pending[hashId] + PendingDownloadQueue.maxWait > time.time():
|
|
|
|
newPending[hashId] = self.pending[hashId]
|
|
|
|
self.pending = newPending
|
|
|
|
|
2017-03-19 22:08:00 +01:00
|
|
|
@staticmethod
|
|
|
|
def totalSize():
|
|
|
|
size = 0
|
|
|
|
for thread in threadingEnumerate():
|
|
|
|
if thread.isAlive() and hasattr(thread, 'downloadQueue'):
|
2017-03-20 01:22:37 +01:00
|
|
|
size += thread.downloadQueue.qsize() + len(thread.downloadQueue.pending)
|
2017-03-19 22:08:00 +01:00
|
|
|
return size
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def stop():
|
|
|
|
for thread in threadingEnumerate():
|
|
|
|
if thread.isAlive() and hasattr(thread, 'downloadQueue'):
|
|
|
|
thread.downloadQueue.stopped = True
|
2017-03-20 01:22:37 +01:00
|
|
|
with thread.downloadQueue.lock:
|
|
|
|
thread.downloadQueue.pending = {}
|
2017-01-19 19:48:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
class PendingUploadDeadlineException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@Singleton
|
|
|
|
class PendingUpload(object):
|
|
|
|
# keep a track of objects that we have created but haven't distributed yet
|
|
|
|
def __init__(self):
|
|
|
|
super(self.__class__, self).__init__()
|
|
|
|
self.lock = RLock()
|
|
|
|
self.hashes = {}
|
|
|
|
# end by this time in any case
|
|
|
|
self.deadline = 0
|
|
|
|
self.maxLen = 0
|
2017-03-01 10:05:08 +01:00
|
|
|
# during shutdown, wait up to 20 seconds to finish uploading
|
|
|
|
self.shutdownWait = 20
|
|
|
|
# forget tracking objects after 60 seconds
|
|
|
|
self.objectWait = 60
|
|
|
|
# wait 10 seconds between clears
|
|
|
|
self.clearDelay = 10
|
|
|
|
self.lastCleared = time.time()
|
2017-01-19 19:48:12 +01:00
|
|
|
|
|
|
|
def add(self, objectHash = None):
|
|
|
|
with self.lock:
|
|
|
|
# add a new object into existing thread lists
|
|
|
|
if objectHash:
|
|
|
|
if objectHash not in self.hashes:
|
2017-03-01 10:05:08 +01:00
|
|
|
self.hashes[objectHash] = {'created': time.time(), 'sendCount': 0, 'peers': []}
|
2017-01-19 19:48:12 +01:00
|
|
|
for thread in threadingEnumerate():
|
|
|
|
if thread.isAlive() and hasattr(thread, 'peer') and \
|
2017-03-01 10:05:08 +01:00
|
|
|
thread.peer not in self.hashes[objectHash]['peers']:
|
|
|
|
self.hashes[objectHash]['peers'].append(thread.peer)
|
2017-01-19 19:48:12 +01:00
|
|
|
# add all objects into the current thread
|
|
|
|
else:
|
2017-01-19 20:04:45 +01:00
|
|
|
for objectHash in self.hashes:
|
2017-03-01 10:05:08 +01:00
|
|
|
if current_thread().peer not in self.hashes[objectHash]['peers']:
|
|
|
|
self.hashes[objectHash]['peers'].append(current_thread().peer)
|
2017-01-19 19:48:12 +01:00
|
|
|
|
|
|
|
def len(self):
|
2017-03-01 10:05:08 +01:00
|
|
|
self.clearHashes()
|
2017-01-19 19:48:12 +01:00
|
|
|
with self.lock:
|
2017-03-01 10:05:08 +01:00
|
|
|
return sum(1
|
|
|
|
for x in self.hashes if (self.hashes[x]['created'] + self.objectWait < time.time() or
|
|
|
|
self.hashes[x]['sendCount'] == 0))
|
2017-01-19 19:48:12 +01:00
|
|
|
|
|
|
|
def _progress(self):
|
|
|
|
with self.lock:
|
2017-03-01 10:05:08 +01:00
|
|
|
return float(sum(len(self.hashes[x]['peers'])
|
|
|
|
for x in self.hashes if (self.hashes[x]['created'] + self.objectWait < time.time()) or
|
|
|
|
self.hashes[x]['sendCount'] == 0))
|
2017-01-19 19:48:12 +01:00
|
|
|
|
2017-03-01 10:05:08 +01:00
|
|
|
def progress(self, raiseDeadline=True):
|
2017-01-19 19:48:12 +01:00
|
|
|
if self.maxLen < self._progress():
|
|
|
|
self.maxLen = self._progress()
|
|
|
|
if self.deadline < time.time():
|
2017-03-01 10:05:08 +01:00
|
|
|
if self.deadline > 0 and raiseDeadline:
|
2017-01-19 19:48:12 +01:00
|
|
|
raise PendingUploadDeadlineException
|
|
|
|
self.deadline = time.time() + 20
|
|
|
|
try:
|
|
|
|
return 1.0 - self._progress() / self.maxLen
|
|
|
|
except ZeroDivisionError:
|
|
|
|
return 1.0
|
|
|
|
|
2017-03-01 10:05:08 +01:00
|
|
|
def clearHashes(self, objectHash=None):
|
|
|
|
if objectHash is None:
|
|
|
|
if self.lastCleared > time.time() - self.clearDelay:
|
|
|
|
return
|
|
|
|
objects = self.hashes.keys()
|
|
|
|
else:
|
|
|
|
objects = objectHash,
|
|
|
|
with self.lock:
|
|
|
|
for i in objects:
|
|
|
|
try:
|
|
|
|
if self.hashes[i]['sendCount'] > 0 and (
|
|
|
|
len(self.hashes[i]['peers']) == 0 or
|
|
|
|
self.hashes[i]['created'] + self.objectWait < time.time()):
|
|
|
|
del self.hashes[i]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
self.lastCleared = time.time()
|
|
|
|
|
|
|
|
def delete(self, objectHash=None):
|
2017-01-19 19:48:12 +01:00
|
|
|
if not hasattr(current_thread(), 'peer'):
|
|
|
|
return
|
2017-03-01 10:05:08 +01:00
|
|
|
if objectHash is None:
|
|
|
|
return
|
2017-01-19 19:48:12 +01:00
|
|
|
with self.lock:
|
2017-03-01 10:05:08 +01:00
|
|
|
try:
|
|
|
|
if objectHash in self.hashes and current_thread().peer in self.hashes[objectHash]['peers']:
|
|
|
|
self.hashes[objectHash]['sendCount'] += 1
|
|
|
|
self.hashes[objectHash]['peers'].remove(current_thread().peer)
|
|
|
|
except KeyError:
|
2017-03-02 15:02:51 +01:00
|
|
|
pass
|
2017-03-01 10:05:08 +01:00
|
|
|
self.clearHashes(objectHash)
|
2017-01-19 19:48:12 +01:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
with self.lock:
|
|
|
|
self.hashes = {}
|
|
|
|
|
|
|
|
def threadEnd(self):
|
2017-03-01 10:05:08 +01:00
|
|
|
with self.lock:
|
|
|
|
for objectHash in self.hashes:
|
|
|
|
try:
|
|
|
|
if current_thread().peer in self.hashes[objectHash]['peers']:
|
|
|
|
self.hashes[objectHash]['peers'].remove(current_thread().peer)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
self.clearHashes()
|