Skip to content

Commit

Permalink
remove cache lock
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasWaldmann committed Sep 18, 2024
1 parent 7b58adc commit 8456761
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 25 deletions.
6 changes: 2 additions & 4 deletions src/borg/archiver/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,12 @@ def wrapper(self, args, **kwargs):
if "compression" in args:
manifest_.repo_objs.compressor = args.compression.compressor
if secure:
assert_secure(repository, manifest_, self.lock_wait)
assert_secure(repository, manifest_)
if cache:
with Cache(
repository,
manifest_,
progress=getattr(args, "progress", False),
lock_wait=self.lock_wait,
cache_mode=getattr(args, "files_cache_mode", FILES_CACHE_MODE_DISABLED),
iec=getattr(args, "iec", False),
) as cache_:
Expand Down Expand Up @@ -230,15 +229,14 @@ def wrapper(self, args, **kwargs):
manifest_ = Manifest.load(
repository, compatibility, ro_cls=RepoObj if repository.version > 1 else RepoObj1
)
assert_secure(repository, manifest_, self.lock_wait)
assert_secure(repository, manifest_)
if manifest:
kwargs["other_manifest"] = manifest_
if cache:
with Cache(
repository,
manifest_,
progress=False,
lock_wait=self.lock_wait,
cache_mode=getattr(args, "files_cache_mode", FILES_CACHE_MODE_DISABLED),
iec=getattr(args, "iec", False),
) as cache_:
Expand Down
1 change: 0 additions & 1 deletion src/borg/archiver/create_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ def create_inner(archive, cache, fso):
repository,
manifest,
progress=args.progress,
lock_wait=self.lock_wait,
cache_mode=args.files_cache_mode,
iec=args.iec,
archive_name=args.name,
Expand Down
2 changes: 1 addition & 1 deletion src/borg/archiver/list_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def _list_inner(cache):

# Only load the cache if it will be used
if ItemFormatter.format_needs_cache(format):
with Cache(repository, manifest, lock_wait=self.lock_wait) as cache:
with Cache(repository, manifest) as cache:
_list_inner(cache)
else:
_list_inner(cache=None)
Expand Down
2 changes: 1 addition & 1 deletion src/borg/archiver/prune_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def do_prune(self, args, repository, manifest):
keep += prune_split(archives, rule, num, kept_because)

to_delete = set(archives) - set(keep)
with Cache(repository, manifest, lock_wait=self.lock_wait, iec=args.iec) as cache:
with Cache(repository, manifest, iec=args.iec) as cache:
list_logger = logging.getLogger("borg.output.list")
# set up counters for the progress display
to_delete_len = len(to_delete)
Expand Down
25 changes: 7 additions & 18 deletions src/borg/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from .helpers.msgpack import int_to_timestamp, timestamp_to_int
from .item import ChunkListEntry
from .crypto.key import PlaintextKey
from .fslocking import Lock
from .manifest import Manifest
from .platform import SaveFile
from .remote import RemoteRepository
Expand Down Expand Up @@ -152,7 +151,7 @@ def assert_key_type(self, key):
if self.known() and not self.key_matches(key):
raise Cache.EncryptionMethodMismatch()

def assert_secure(self, manifest, key, *, warn_if_unencrypted=True, lock_wait=None):
def assert_secure(self, manifest, key, *, warn_if_unencrypted=True):
# warn_if_unencrypted=False is only used for initializing a new repository.
# Thus, avoiding asking about a repository that's currently initializing.
self.assert_access_unknown(warn_if_unencrypted, manifest, key)
Expand Down Expand Up @@ -192,23 +191,21 @@ def assert_access_unknown(self, warn_if_unencrypted, manifest, key):
raise Cache.CacheInitAbortedError()


def assert_secure(repository, manifest, lock_wait):
def assert_secure(repository, manifest):
sm = SecurityManager(repository)
sm.assert_secure(manifest, manifest.key, lock_wait=lock_wait)
sm.assert_secure(manifest, manifest.key)


def cache_dir(repository, path=None):
return path or os.path.join(get_cache_dir(), repository.id_str)


class CacheConfig:
def __init__(self, repository, path=None, lock_wait=None):
def __init__(self, repository, path=None):
self.repository = repository
self.path = cache_dir(repository, path)
logger.debug("Using %s as cache", self.path)
self.config_path = os.path.join(self.path, "config")
self.lock = None
self.lock_wait = lock_wait

def __enter__(self):
self.open()
Expand All @@ -233,7 +230,6 @@ def create(self):
config.write(fd)

def open(self):
self.lock = Lock(os.path.join(self.path, "lock"), exclusive=True, timeout=self.lock_wait).acquire()
self.load()

def load(self):
Expand Down Expand Up @@ -276,9 +272,7 @@ def save(self, manifest=None):
self._config.write(fd)

def close(self):
if self.lock is not None:
self.lock.release()
self.lock = None
pass

def _check_upgrade(self, config_path):
try:
Expand Down Expand Up @@ -324,8 +318,7 @@ class RepositoryReplay(Error):

@staticmethod
def break_lock(repository, path=None):
path = cache_dir(repository, path)
Lock(os.path.join(path, "lock"), exclusive=True).break_lock()
pass

@staticmethod
def destroy(repository, path=None):
Expand All @@ -344,7 +337,6 @@ def __new__(
sync=True,
warn_if_unencrypted=True,
progress=False,
lock_wait=None,
cache_mode=FILES_CACHE_MODE_DISABLED,
iec=False,
archive_name=None,
Expand All @@ -355,7 +347,6 @@ def __new__(
warn_if_unencrypted=warn_if_unencrypted,
progress=progress,
iec=iec,
lock_wait=lock_wait,
cache_mode=cache_mode,
archive_name=archive_name,
)
Expand Down Expand Up @@ -593,14 +584,12 @@ def __init__(
path=None,
warn_if_unencrypted=True,
progress=False,
lock_wait=None,
cache_mode=FILES_CACHE_MODE_DISABLED,
iec=False,
archive_name=None,
):
"""
:param warn_if_unencrypted: print warning if accessing unknown unencrypted repository
:param lock_wait: timeout for lock acquisition (int [s] or None [wait forever])
:param cache_mode: what shall be compared in the file stat infos vs. cached stat infos comparison
"""
FilesCacheMixin.__init__(self, cache_mode, archive_name)
Expand All @@ -614,7 +603,7 @@ def __init__(

self.path = cache_dir(self.repository, path)
self.security_manager = SecurityManager(self.repository)
self.cache_config = CacheConfig(self.repository, self.path, lock_wait)
self.cache_config = CacheConfig(self.repository, self.path)

# Warn user before sending data to a never seen before unencrypted repository
if not os.path.exists(self.path):
Expand Down

0 comments on commit 8456761

Please sign in to comment.