Skip to content

Commit 21806f5

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 dfb3b77 commit 21806f5

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
@@ -1789,7 +1789,7 @@ def is_controller(cls):
17891789
return cls._is_mounted(DATABASE_PATH)
17901790

17911791
@classmethod
1792-
def database_backup_age(cls):
1792+
def get_database_backup_age(cls):
17931793
"""
17941794
Return the latest backup age in seconds.
17951795
If not called on the Controller, since backups are not available,
@@ -1817,17 +1817,17 @@ def database_invalidation(cls):
18171817
for directory in (DATABASE_BACKUP_DIR_MAIN, DATABASE_BACKUP_DIR_SPARE):
18181818
valid_backup_count = 0
18191819
# Validate file and apply retention
1820-
for database_backup_file, _ in cls._get_sorted_database_backup(directory):
1820+
for database_backup_path, _ in cls._get_sorted_database_backup(directory):
18211821
try:
1822-
cls._check_database_backup(database_backup_file)
1822+
cls._check_database_backup(database_backup_path)
18231823
valid_backup_count += 1
18241824
if valid_backup_count < DATABASE_BACKUP_RETENTION:
18251825
continue
18261826
except LinstorDatabaseBackupError as error:
1827-
util.SMlog(f"[database_backup] Check failed `{error}` [{database_backup_file}]",
1827+
util.SMlog(f"[database_backup] Check failed `{error}` [{database_backup_path}]",
18281828
priority=util.LOG_ERR)
18291829
with contextlib.suppress(OSError):
1830-
os.unlink(database_backup_file)
1830+
os.unlink(database_backup_path)
18311831

18321832
@classmethod
18331833
def get_all_group_names(cls, base_name):
@@ -2707,7 +2707,7 @@ def _get_volume_properties(self, volume_uuid):
27072707
return properties
27082708

27092709
@classmethod
2710-
def _list_database_backup(cls, database_backup_dir):
2710+
def _list_database_backups(cls, database_backup_dir):
27112711
"""
27122712
List all visible backup files in database_backup_dir.
27132713
DATABASE_BACKUP_DIR_MAIN is only available on the Linstor Controller.
@@ -2727,7 +2727,7 @@ def _get_sorted_database_backup(cls, database_backup_dir):
27272727
Return list of backups in database_backup_dir, alongside their creation date.
27282728
Sorted by date from the more recent to the older one.
27292729
"""
2730-
return sorted(cls._list_database_backup(database_backup_dir),
2730+
return sorted(cls._list_database_backups(database_backup_dir),
27312731
reverse=True,
27322732
key=lambda p: p[1])
27332733

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

27462746
@classmethod
2747-
def _check_database_backup(cls, database_backup_file):
2747+
def _check_database_backup(cls, database_backup_path):
27482748
"""
27492749
Make some validation of a database backup zip-file.
27502750
Check its a valid zipfile, and CRC-test its content.
27512751
Check it contains a non-empty linstordb.mv.db file.
27522752
Always raises a LinstorDatabaseBackupError if checks failed.
27532753
"""
27542754
try:
2755-
with zipfile.ZipFile(database_backup_file, mode="r") as archive:
2755+
with zipfile.ZipFile(database_backup_path, mode="r") as archive:
27562756
if archive.testzip() is not None:
27572757
raise LinstorDatabaseBackupError("zip archive CRC failed")
2758-
linstordb = [f for f in archive.filelist if f.filename == "linstordb.mv.db"]
2758+
linstordb = next((
2759+
f
2760+
for f in archive.filelist
2761+
if f.filename == "linstordb.mv.db"
2762+
), None)
27592763
if not linstordb:
27602764
raise LinstorDatabaseBackupError("cannot find linstordb.mv.db")
2761-
linstordb = linstordb[0]
27622765
if linstordb.file_size == 0:
27632766
raise LinstorDatabaseBackupError("linstordb.mv.db is empty")
27642767
except (FileNotFoundError, zipfile.BadZipFile, zipfile.LargeZipFile) as e:

0 commit comments

Comments
 (0)