Skip to content

Commit

Permalink
feat: add check for free storage space when enabling cache
Browse files Browse the repository at this point in the history
PR Closed: #1081
  • Loading branch information
Lee-000 committed Nov 1, 2021
1 parent ace8001 commit ef8ac0b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
13 changes: 10 additions & 3 deletions tensorbay/client/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import logging
import os
import shutil
import tempfile
from typing import TYPE_CHECKING, Any, Dict, Generator, Iterable, Iterator, Optional, Tuple, Union

Expand All @@ -32,6 +33,7 @@
from tensorbay.client.diff import DataDiff, DatasetDiff, SegmentDiff
from tensorbay.client.lazy import PagingList
from tensorbay.client.log import (
CACHE_SPACE_WARNING,
UPLOAD_SEGMENT_RESUME_TEMPLATE_CLI,
UPLOAD_SEGMENT_RESUME_TEMPLATE_SDK,
)
Expand Down Expand Up @@ -229,15 +231,20 @@ def enable_cache(self, cache_path: str = "") -> None:
)
else:
self._cache_path = os.path.join(tempfile.gettempdir(), "tensorbay", self.dataset_id)

total_size = self.get_total_size()
print(
"To use cache, "
"please make sure there is free storage space larger than the dataset size.\n"
"To cache the entire dataset, "
f"please make sure there is free storage space larger than {total_size} bytes.\n"
"Note that cache will not work for datasets under draft status.\n\n"
f'The cache will be stored under "{self._cache_path}".\n'
"You can remove all the files after using."
)

os.makedirs(self._cache_path, exist_ok=True)
_, _, free = shutil.disk_usage(self._cache_path)
if free < total_size:
logger.warning(CACHE_SPACE_WARNING, free, total_size)

def update_notes(
self,
*,
Expand Down
9 changes: 9 additions & 0 deletions tensorbay/client/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@
*****************************************************************************
"""

CACHE_SPACE_WARNING = """
*****************************************************************************
%d bytes left on device, less than the dataset size %d bytes.
Please be aware that there is not enough space to cache the entire dataset.
*****************************************************************************
"""


class RequestLogging:
"""This class used to lazy load request to logging.
Expand Down

0 comments on commit ef8ac0b

Please sign in to comment.