Skip to content

Commit ad99004

Browse files
committed
Better names, comments and a small but elegant optimization.
database_backup_path instead of database_backup_file because it is a str|Path. Throttling delay converted to Constant. Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
1 parent 6a7983b commit ad99004

3 files changed

Lines changed: 21 additions & 16 deletions

File tree

drivers/LinstorSR.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from sm_typing import Any, Optional, override, Literal
1818

19-
from constants import CBTLOG_TAG
19+
from constants import CBTLOG_TAG, LINSTOR_AUTO_BACKUP_DELAY
2020

2121
try:
2222
from linstorcowutil import LinstorCowUtil, MultiLinstorCowUtil
@@ -824,15 +824,15 @@ def check_sr(self, sr_uuid) -> None:
824824
# Applied only on the Linstor Controller, for reasons -> listed below.
825825
if not LinstorVolumeManager.is_controller():
826826
return
827-
# Start database invalidation.
827+
# Validate and clean previous backups if necessary.
828828
# -> Needs access to backup files, available only on the Controller.
829829
LinstorVolumeManager.database_invalidation()
830830
# check_sr is launched on *all* hosts, but it turns out that
831831
# we do not want all of them to blindly generate concurrencing backups.
832832
# Hence we must choose one, either one is good, but there must be only one.
833833
# Apply throttling: only backup if last one is >1h old.
834834
# -> Needs access to backup files, available only on the Controller.
835-
if LinstorVolumeManager.database_backup_age() > 3600:
835+
if LinstorVolumeManager.get_database_backup_age() > LINSTOR_AUTO_BACKUP_DELAY:
836836
self.database_backup("auto")
837837

838838
@override

drivers/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@
1111
# Ref counting for VDI's: we need a ref count for LV activation/deactivation
1212
# on the master.
1313
NS_PREFIX_LVM: Final = "lvm-"
14+
15+
LINSTOR_AUTO_BACKUP_DELAY = 3600

drivers/linstorvolumemanager.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,7 +1795,7 @@ def is_controller(cls):
17951795
return cls._is_mounted(DATABASE_PATH)
17961796

17971797
@classmethod
1798-
def database_backup_age(cls):
1798+
def get_database_backup_age(cls):
17991799
"""
18001800
Return the latest backup age in seconds.
18011801
If not called on the Controller, since backups are not available,
@@ -1823,17 +1823,17 @@ def database_invalidation(cls):
18231823
for directory in (DATABASE_BACKUP_DIR_MAIN, DATABASE_BACKUP_DIR_SPARE):
18241824
valid_backup_count = 0
18251825
# Validate file and apply retention
1826-
for database_backup_file, _ in cls._get_sorted_database_backup(directory):
1826+
for database_backup_path, _ in cls._get_sorted_database_backup(directory):
18271827
try:
1828-
cls._check_database_backup(database_backup_file)
1828+
cls._check_database_backup(database_backup_path)
18291829
valid_backup_count += 1
18301830
if valid_backup_count < DATABASE_BACKUP_RETENTION:
18311831
continue
18321832
except LinstorDatabaseBackupError as error:
1833-
util.SMlog(f"[database_backup] Check failed `{error}` [{database_backup_file}]",
1833+
util.SMlog(f"[database_backup] Check failed `{error}` [{database_backup_path}]",
18341834
priority=util.LOG_ERR)
18351835
with contextlib.suppress(OSError):
1836-
os.unlink(database_backup_file)
1836+
os.unlink(database_backup_path)
18371837

18381838
@classmethod
18391839
def get_all_group_names(cls, base_name):
@@ -2713,7 +2713,7 @@ def _get_volume_properties(self, volume_uuid):
27132713
return properties
27142714

27152715
@classmethod
2716-
def _list_database_backup(cls, database_backup_dir):
2716+
def _list_database_backups(cls, database_backup_dir):
27172717
"""
27182718
List all visible backup files in database_backup_dir.
27192719
DATABASE_BACKUP_DIR_MAIN is only available on the Linstor Controller.
@@ -2733,7 +2733,7 @@ def _get_sorted_database_backup(cls, database_backup_dir):
27332733
Return list of backups in database_backup_dir, alongside their creation date.
27342734
Sorted by date from the more recent to the older one.
27352735
"""
2736-
return sorted(cls._list_database_backup(database_backup_dir),
2736+
return sorted(cls._list_database_backups(database_backup_dir),
27372737
reverse=True,
27382738
key=lambda p: p[1])
27392739

@@ -2743,28 +2743,31 @@ def _get_latest_database_backup(cls):
27432743
Return the latest backup in DATABASE_BACKUP_DIR_MAIN, and its creation date.
27442744
Returns (None, timestamp(0)) when none are found.
27452745
None will be found if it is not called on the Linstor Controller.
2746-
(cf _list_database_backup)
2746+
(cf _list_database_backups)
27472747
"""
2748-
return max(cls._list_database_backup(DATABASE_BACKUP_DIR_MAIN),
2748+
return max(cls._list_database_backups(DATABASE_BACKUP_DIR_MAIN),
27492749
default=(None, datetime.fromtimestamp(0)),
27502750
key=lambda p: p[1])
27512751

27522752
@classmethod
2753-
def _check_database_backup(cls, database_backup_file):
2753+
def _check_database_backup(cls, database_backup_path):
27542754
"""
27552755
Make some validation of a database backup zip-file.
27562756
Check its a valid zipfile, and CRC-test its content.
27572757
Check it contains a non-empty linstordb.mv.db file.
27582758
Always raises a LinstorDatabaseBackupError if checks failed.
27592759
"""
27602760
try:
2761-
with zipfile.ZipFile(database_backup_file, mode="r") as archive:
2761+
with zipfile.ZipFile(database_backup_path, mode="r") as archive:
27622762
if archive.testzip() is not None:
27632763
raise LinstorDatabaseBackupError("zip archive CRC failed")
2764-
linstordb = [f for f in archive.filelist if f.filename == "linstordb.mv.db"]
2764+
linstordb = next((
2765+
f
2766+
for f in archive.filelist
2767+
if f.filename == "linstordb.mv.db"
2768+
), None)
27652769
if not linstordb:
27662770
raise LinstorDatabaseBackupError("cannot find linstordb.mv.db")
2767-
linstordb = linstordb[0]
27682771
if linstordb.file_size == 0:
27692772
raise LinstorDatabaseBackupError("linstordb.mv.db is empty")
27702773
except (FileNotFoundError, zipfile.BadZipFile, zipfile.LargeZipFile) as e:

0 commit comments

Comments
 (0)