Skip to content

Refactor locking into repository package#4709

Merged
MichaelEischer merged 10 commits intorestic:masterfrom
MichaelEischer:refactor-locking
Mar 28, 2024
Merged

Refactor locking into repository package#4709
MichaelEischer merged 10 commits intorestic:masterfrom
MichaelEischer:refactor-locking

Conversation

@MichaelEischer
Copy link
Copy Markdown
Member

@MichaelEischer MichaelEischer commented Feb 24, 2024

What does this PR change? What problem does it solve?

The code to lock a repository was living in cmd/restic. However, the allowed read or write access to a repository depends on the type of lock held by the current process. Therefore, locking must be centrally managed by the repository to guarantee integrity of the repository.

The PR replaces the lockRepo and lockRepoExclusive functions with openWithReadLock, openWithAppendLock and openWithExclusiveLock. The code paths for openWithReadLock and openWithAppendLock are nearly identical, but the difference is in semantics: a read lock can be disabled by specifying --no-lock, whereas for a write lock the relevant question is whether the command is run in --dry-run mode or not.

The unified lock handling leads to a few minor cosmetic changes and three actual changes in behavior:

  • tag no longer accepts the --no-lock flag. As it replaces files in the repository, this always requires an exclusive lock.
  • debug examine now returns an error if both --extract-pack and --no-lock are given.
  • ls erroneously did not lock the repository. The old behavior is now available using ls --no-lock.

The remaining changes are a mix of fewer global variables and test cleanups to correctly lock the repository. The latter are the result of an experiment to actually enforce the access requested together with the lock (i.e., a read lock blocks all modifications). However, that code is still too horrible to show anyone ^^ .

TODOs

  • Cleanup repository.Lock function interface

Was the change previously discussed in an issue or on the forum?

Part of the roadmap for restic 0.17

Checklist

  • I have read the contribution guidelines.
  • I have enabled maintainer edits.
  • I have added tests for all code changes.
  • I have added documentation for relevant changes (in the manual).
  • There's a new file in changelog/unreleased/ that describes the changes for our users (see template).
  • I have run gofmt on the code in all commits.
  • All commit messages are formatted in the same style as the other commits in the repo.
  • I'm done! This pull request is ready for review.

…Lock

The new functions much better convey the intent behind the lock
request. This allows cleanly integrating noLock (for read) and dryRun
(write/exclusive) handling.

There are only minor changes to existing behavior with two exceptions:
- `tag` no longer accepts the `--no-lock` flag. As it replaces files in
  the repository, this always requires an exclusive lock.
- `debug examine` now returns an error if both `--extract-pack` and
  `--no-lock` are given.
Tests should use a helper from internal/repository/testing.go to
construct a Repository object.
As `ls` reads data from the repository, it must acquire a read lock
unless `--no-lock` was specified. The old behavior is equivalent to `ls
--no-lock`.
@MichaelEischer MichaelEischer marked this pull request as ready for review March 28, 2024 22:51
Copy link
Copy Markdown
Member Author

@MichaelEischer MichaelEischer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@MichaelEischer MichaelEischer merged commit 510f6f0 into restic:master Mar 28, 2024
@MichaelEischer MichaelEischer deleted the refactor-locking branch March 28, 2024 22:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant