Skip to content

Commit

Permalink
Disk utilization naming adjusted (#1565)
Browse files Browse the repository at this point in the history
  • Loading branch information
Raalsky authored Nov 13, 2023
1 parent 0877fc7 commit e1e090e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- Rework disk utilization check ([#1549](https://github.com/neptune-ai/neptune-client/pull/1549))
- Introduce error handlers for disk utilization ([#1559](https://github.com/neptune-ai/neptune-client/pull/1559))
- Added support for `neptune[experimental]` extras ([#1560](https://github.com/neptune-ai/neptune-client/pull/1560))
- Disk utilization environment variables renamed ([#1565](https://github.com/neptune-ai/neptune-client/pull/1565))


## neptune 1.8.2
Expand Down
8 changes: 4 additions & 4 deletions src/neptune/envs.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"NEPTUNE_FETCH_TABLE_STEP_SIZE",
"NEPTUNE_SYNC_AFTER_STOP_TIMEOUT",
"NEPTUNE_REQUEST_TIMEOUT",
"NEPTUNE_MAX_DISK_UTILIZATION",
"NEPTUNE_NON_RAISING_ON_DISK_ISSUE",
"NEPTUNE_MAX_DISK_USAGE",
"NEPTUNE_RAISE_ERROR_ON_DISK_USAGE_EXCEEDED",
"NEPTUNE_ENABLE_DEFAULT_ASYNC_LAG_CALLBACK",
"NEPTUNE_ENABLE_DEFAULT_ASYNC_NO_PROGRESS_CALLBACK",
"NEPTUNE_DISABLE_PARENT_DIR_DELETION",
Expand Down Expand Up @@ -69,9 +69,9 @@

NEPTUNE_ENABLE_DEFAULT_ASYNC_NO_PROGRESS_CALLBACK = "NEPTUNE_ENABLE_DEFAULT_ASYNC_NO_PROGRESS_CALLBACK"

NEPTUNE_MAX_DISK_UTILIZATION = "NEPTUNE_MAX_DISK_UTILIZATION"
NEPTUNE_MAX_DISK_USAGE = "NEPTUNE_MAX_DISK_USAGE"

NEPTUNE_NON_RAISING_ON_DISK_ISSUE = "NEPTUNE_NON_RAISING_ON_DISK_ISSUE"
NEPTUNE_RAISE_ERROR_ON_DISK_USAGE_EXCEEDED = "NEPTUNE_RAISE_ERROR_ON_DISK_USAGE_EXCEEDED"

NEPTUNE_DISABLE_PARENT_DIR_DELETION = "NEPTUNE_DISABLE_PARENT_DIR_DELETION"

Expand Down
12 changes: 6 additions & 6 deletions src/neptune/internal/utils/disk_utilization.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
)
from neptune.constants import NEPTUNE_DATA_DIRECTORY
from neptune.envs import (
NEPTUNE_MAX_DISK_UTILIZATION,
NEPTUNE_NON_RAISING_ON_DISK_ISSUE,
NEPTUNE_MAX_DISK_USAGE,
NEPTUNE_RAISE_ERROR_ON_DISK_USAGE_EXCEEDED,
)
from neptune.exceptions import NeptuneMaxDiskUtilizationExceeded

Expand All @@ -58,7 +58,7 @@ def get_disk_utilization_percent(path: Optional[str] = None) -> Optional[float]:


def get_max_disk_utilization_from_env() -> Optional[float]:
env_limit_disk_utilization = os.getenv(NEPTUNE_MAX_DISK_UTILIZATION)
env_limit_disk_utilization = os.getenv(NEPTUNE_MAX_DISK_USAGE)

if env_limit_disk_utilization is None:
return None
Expand All @@ -71,7 +71,7 @@ def get_max_disk_utilization_from_env() -> Optional[float]:
return limit_disk_utilization
except (ValueError, TypeError):
warn_once(
f"Provided invalid value of '{NEPTUNE_MAX_DISK_UTILIZATION}': '{env_limit_disk_utilization}'. "
f"Provided invalid value of '{NEPTUNE_MAX_DISK_USAGE}': '{env_limit_disk_utilization}'. "
"Check of disk utilization will not be applied.",
exception=NeptuneWarning,
)
Expand Down Expand Up @@ -165,10 +165,10 @@ def handle_limit_exceeded(self, current_utilization: float) -> None:


def ensure_disk_not_overutilize(func: Callable[..., None]) -> Callable[..., None]:
non_raising_on_disk_issue = os.getenv(NEPTUNE_NON_RAISING_ON_DISK_ISSUE, "false").lower() in ("true", "t", "1")
raising_on_disk_issue = os.getenv(NEPTUNE_RAISE_ERROR_ON_DISK_USAGE_EXCEEDED, "True").lower() in ("true", "t", "1")
max_disk_utilization = get_max_disk_utilization_from_env()

error_handler = NonRaisingErrorHandler if non_raising_on_disk_issue else RaisingErrorHandler
error_handler = RaisingErrorHandler if raising_on_disk_issue else NonRaisingErrorHandler

@wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> None:
Expand Down
39 changes: 20 additions & 19 deletions tests/unit/neptune/new/internal/utils/test_disk_utilization.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
)

from neptune.envs import (
NEPTUNE_MAX_DISK_UTILIZATION,
NEPTUNE_NON_RAISING_ON_DISK_ISSUE,
NEPTUNE_MAX_DISK_USAGE,
NEPTUNE_RAISE_ERROR_ON_DISK_USAGE_EXCEEDED,
)
from neptune.exceptions import NeptuneMaxDiskUtilizationExceeded
from neptune.internal.utils.disk_utilization import (
Expand All @@ -41,23 +41,23 @@


class TestDiskUtilization(unittest.TestCase):
@patch.dict(os.environ, {NEPTUNE_NON_RAISING_ON_DISK_ISSUE: "True"})
@patch.dict(os.environ, {NEPTUNE_RAISE_ERROR_ON_DISK_USAGE_EXCEEDED: "True"})
def test_handle_invalid_env_values(self):
for value in ["True", "101", "-1"]:
with patch.dict(os.environ, {NEPTUNE_MAX_DISK_UTILIZATION: value}, clear=True):
with patch.dict(os.environ, {NEPTUNE_MAX_DISK_USAGE: value}, clear=True):
mocked_func = MagicMock()
with warnings.catch_warnings(record=True) as warns:
wrapped_func = ensure_disk_not_overutilize(mocked_func)
wrapped_func()

assert len(warns) == 1
assert f"invalid value of '{NEPTUNE_MAX_DISK_UTILIZATION}': '{value}" in str(warns[-1].message)
assert f"invalid value of '{NEPTUNE_MAX_DISK_USAGE}': '{value}" in str(warns[-1].message)
mocked_func.assert_called_once()

# Catching OSError that's base error for all OS and IO errors. More info here: https://peps.python.org/pep-3151
# Additionally, catching specific psutil's base error - psutil.Error.
# More info about psutil.Error here: https://psutil.readthedocs.io/en/latest/index.html#psutil.Error
@patch.dict(os.environ, {NEPTUNE_NON_RAISING_ON_DISK_ISSUE: "True"})
@patch.dict(os.environ, {NEPTUNE_RAISE_ERROR_ON_DISK_USAGE_EXCEEDED: "False"})
def test_suppressing_of_func_errors(self):
disk_errors = [
OSError(),
Expand Down Expand Up @@ -85,8 +85,8 @@ def test_suppressing_of_func_errors(self):
wrapped_func()
mocked_func.assert_called_once()

@patch.dict(os.environ, {NEPTUNE_NON_RAISING_ON_DISK_ISSUE: "True"})
@patch.dict(os.environ, {NEPTUNE_MAX_DISK_UTILIZATION: "60"})
@patch.dict(os.environ, {NEPTUNE_RAISE_ERROR_ON_DISK_USAGE_EXCEEDED: "True"})
@patch.dict(os.environ, {NEPTUNE_MAX_DISK_USAGE: "60"})
@patch("psutil.disk_usage")
def test_suppressing_of_checking_utilization_errors(self, disk_usage_mock):
checking_errors = [
Expand All @@ -103,20 +103,21 @@ def test_suppressing_of_checking_utilization_errors(self, disk_usage_mock):
wrapped_func() # asserting is not required as expecting that any error will be caught
mocked_func.assert_called_once()

@patch.dict(os.environ, {NEPTUNE_NON_RAISING_ON_DISK_ISSUE: "True"})
@patch.dict(os.environ, {NEPTUNE_MAX_DISK_UTILIZATION: "100"})
@patch.dict(os.environ, {NEPTUNE_RAISE_ERROR_ON_DISK_USAGE_EXCEEDED: "True"})
@patch.dict(os.environ, {NEPTUNE_MAX_DISK_USAGE: "100"})
@patch("psutil.disk_usage")
def test_not_called_with_usage_100_percent(self, disk_usage_mock):
disk_usage_mock.return_value.percent = 100
mocked_func = MagicMock()
wrapped_func = ensure_disk_not_overutilize(mocked_func)

wrapped_func()
with pytest.raises(NeptuneMaxDiskUtilizationExceeded):
wrapped_func()

mocked_func.assert_not_called()

@patch.dict(os.environ, {NEPTUNE_NON_RAISING_ON_DISK_ISSUE: "True"})
@patch.dict(os.environ, {NEPTUNE_MAX_DISK_UTILIZATION: "100"})
@patch.dict(os.environ, {NEPTUNE_RAISE_ERROR_ON_DISK_USAGE_EXCEEDED: "True"})
@patch.dict(os.environ, {NEPTUNE_MAX_DISK_USAGE: "100"})
@patch("psutil.disk_usage")
def test_called_when_usage_less_than_limit(self, disk_usage_mock):
disk_usage_mock.return_value.percent = 99
Expand All @@ -127,8 +128,8 @@ def test_called_when_usage_less_than_limit(self, disk_usage_mock):

mocked_func.assert_called_once()

@patch.dict(os.environ, {NEPTUNE_NON_RAISING_ON_DISK_ISSUE: "False"})
@patch.dict(os.environ, {NEPTUNE_MAX_DISK_UTILIZATION: "60"})
@patch.dict(os.environ, {NEPTUNE_RAISE_ERROR_ON_DISK_USAGE_EXCEEDED: "True"})
@patch.dict(os.environ, {NEPTUNE_MAX_DISK_USAGE: "60"})
@patch("psutil.disk_usage")
def test_not_called_when_(self, disk_usage_mock):
disk_usage_mock.return_value.percent = 99
Expand All @@ -143,17 +144,17 @@ def test_not_called_when_(self, disk_usage_mock):
class TestDiskErrorHandler(unittest.TestCase):
@patch("neptune.internal.utils.disk_utilization.RaisingErrorHandler")
@patch("neptune.internal.utils.disk_utilization.NonRaisingErrorHandler")
@patch.dict(os.environ, {NEPTUNE_NON_RAISING_ON_DISK_ISSUE: "False"})
def test_raising_handler_used_if_env_var_false(self, mock_non_raising_handler, mock_raising_handler):
@patch.dict(os.environ, {NEPTUNE_RAISE_ERROR_ON_DISK_USAGE_EXCEEDED: "True"})
def test_raising_handler_used_if_env_var_true(self, mock_non_raising_handler, mock_raising_handler):
decorated = ensure_disk_not_overutilize(MagicMock())
decorated()
mock_raising_handler.assert_called_once()
mock_non_raising_handler.assert_not_called()

@patch("neptune.internal.utils.disk_utilization.RaisingErrorHandler")
@patch("neptune.internal.utils.disk_utilization.NonRaisingErrorHandler")
@patch.dict(os.environ, {NEPTUNE_NON_RAISING_ON_DISK_ISSUE: "True"})
def test_non_raising_handler_used_if_env_var_true(self, mock_non_raising_handler, mock_raising_handler):
@patch.dict(os.environ, {NEPTUNE_RAISE_ERROR_ON_DISK_USAGE_EXCEEDED: "False"})
def test_non_raising_handler_used_if_env_var_false(self, mock_non_raising_handler, mock_raising_handler):
decorated = ensure_disk_not_overutilize(MagicMock())
decorated()
mock_non_raising_handler.assert_called_once()
Expand Down

0 comments on commit e1e090e

Please sign in to comment.