Skip to content

Commit

Permalink
cache space stats from running components so status is instant
Browse files Browse the repository at this point in the history
  • Loading branch information
shyba committed Nov 3, 2021
1 parent d3fd9d4 commit e1112db
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
9 changes: 6 additions & 3 deletions lbry/blob/disk_space_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def __init__(self, config, db, blob_manager, cleaning_interval=30 * 60, analytic
self.running = False
self.task = None
self.analytics = analytics
self._used_space_bytes = None

async def get_free_space_mb(self, is_network_blob=False):
limit_mb = self.config.network_storage_limit if is_network_blob else self.config.blob_storage_limit
Expand All @@ -22,10 +23,12 @@ async def get_free_space_mb(self, is_network_blob=False):
return max(0, limit_mb - space_used_mb)

async def get_space_used_bytes(self):
return await self.db.get_stored_blob_disk_usage()
self._used_space_bytes = await self.db.get_stored_blob_disk_usage()
return self._used_space_bytes

async def get_space_used_mb(self):
space_used_bytes = await self.get_space_used_bytes()
async def get_space_used_mb(self, cached=True):
cached = cached and self._used_space_bytes is not None
space_used_bytes = self._used_space_bytes if cached else await self.get_space_used_bytes()
return {key: int(value/1024.0/1024.0) for key, value in space_used_bytes.items()}

async def clean(self):
Expand Down
8 changes: 5 additions & 3 deletions lbry/extras/daemon/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ def __init__(self, component_manager):
self.blob_manager: typing.Optional[BlobManager] = None
self.background_downloader: typing.Optional[BackgroundDownloader] = None
self.dht_node: typing.Optional[Node] = None
self.space_available: typing.Optional[int] = None

@property
def is_busy(self):
Expand All @@ -404,12 +405,13 @@ def component(self) -> 'BackgroundDownloaderComponent':

async def get_status(self):
return {'running': self.task is not None and not self.task.done(),
'available_free_space_mb': await self.space_manager.get_free_space_mb(True),
'available_free_space_mb': self.space_available,
'ongoing_download': self.is_busy}

async def loop(self):
while True:
if not self.is_busy and await self.space_manager.get_free_space_mb(True) > 10:
self.space_available = await self.space_manager.get_free_space_mb(True)
if not self.is_busy and self.space_available > 10:
blob_hash = next((key.hex() for key in self.dht_node.stored_blob_hashes if
key.hex() not in self.blob_manager.completed_blob_hashes), None)
if blob_hash:
Expand Down Expand Up @@ -447,7 +449,7 @@ def component(self) -> typing.Optional[DiskSpaceManager]:

async def get_status(self):
if self.disk_space_manager:
space_used = await self.disk_space_manager.get_space_used_mb()
space_used = await self.disk_space_manager.get_space_used_mb(cached=True)
return {
'total_used_mb': space_used['total'],
'published_blobs_storage_used_mb': space_used['private_storage'],
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/datanetwork/test_file_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ async def test_file_management(self):
await self.daemon.storage.update_blob_ownership(sd_hash1, False)
await self.daemon.storage.update_blob_ownership(sd_hash3, False)
await self.daemon.storage.update_blob_ownership(sd_hash4, False)
await self.blob_clean() # just to refresh caches, has no effect

self.assertEqual(7, (await self.status())['disk_space']['content_blobs_storage_used_mb'])
self.assertEqual(10, (await self.status())['disk_space']['total_used_mb'])
Expand All @@ -563,6 +564,7 @@ async def test_file_management(self):
await self.blob_clean()

self.assertEqual(10, (await self.status())['disk_space']['total_used_mb'])
self.assertEqual(7, (await self.status())['disk_space']['content_blobs_storage_used_mb'])
self.assertEqual(3, (await self.status())['disk_space']['published_blobs_storage_used_mb'])
self.assertEqual(blobs1 | blobs2 | blobs3 | blobs4, set(await self.blob_list()))

Expand Down

0 comments on commit e1112db

Please sign in to comment.