Skip to content

Commit

Permalink
lib/repo: Add repo locking mechanism
Browse files Browse the repository at this point in the history
Currently ostree has no method of guarding against concurrent pruning.
When there are multiple repo writers, it's possible to have a pull or
commit race against a prune and end up with missing objects.

This adds a file based repo locking mechanism. The intention is to take
a shared lock when writing objects and an exclusive lock when deleting
them. In order to make use of the locking throughout the library in a
fine grained fashion, the lock acts recursively with a stack of lock
states. If the lock becomes exclusive, it will stay in that state until
the stack is unwound past the initial exclusive push. The file locking
is similar to GLnxLockFile in that it uses open file descriptor locks
but falls back to flock when needed.

The lock also attempts to be thread safe by storing the lock state in
thread local storage with GPrivate. This means that each thread will
have an independent lock for each repository it opens. There are some
drawbacks to that, but it seemed impossible to manage the lock state
coherently in the face of multithreaded access.

The API is a push/pop interface in accordance with the recursive nature
of the locking. The push interface uses an enum that's translated to
LOCK_SH or LOCK_EX as needed. Both interfaces use an internal timeout
field to decide whether to manage the lock in a blocking or non-blocking
fashion. The intention is to allow ostree applications as well as
administrators to control this timeout. For now, the default is a 30
second timeout.

Note that the timeout is handled synchronously in thread since the lock
is maintained in thread local storage. I.e., the thread that acquires
the lock needs to be the same thread that runs the operation. There may
be a way to offer an asynchronous version, but it's not clear exactly
how that would work since it would likely involve a separate thread that
invokes a callback when the locking operation completes.

https://bugzilla.gnome.org/show_bug.cgi?id=759442

Closes: #1343
Approved by: cgwalters
  • Loading branch information
dbnicholson authored and rh-atomic-bot committed Dec 5, 2017
1 parent e48262c commit 4e78ddd
Show file tree
Hide file tree
Showing 5 changed files with 514 additions and 0 deletions.
3 changes: 3 additions & 0 deletions apidoc/ostree-experimental-sections.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ ostree_repo_finder_override_get_type

<SECTION>
<FILE>ostree-misc-experimental</FILE>
OstreeRepoLockType
ostree_repo_lock_push
ostree_repo_lock_pop
ostree_repo_get_collection_id
ostree_repo_set_collection_id
ostree_validate_collection_id
Expand Down
2 changes: 2 additions & 0 deletions src/libostree/libostree-experimental.sym
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,6 @@ LIBOSTREE_2017.14_EXPERIMENTAL {
global:
ostree_remote_get_type;
ostree_remote_get_url;
ostree_repo_lock_pop;
ostree_repo_lock_push;
} LIBOSTREE_2017.13_EXPERIMENTAL;
20 changes: 20 additions & 0 deletions src/libostree/ostree-repo-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ G_BEGIN_DECLS
#define _OSTREE_MAX_OUTSTANDING_FETCHER_REQUESTS 8
#define _OSTREE_MAX_OUTSTANDING_DELTAPART_REQUESTS 2

#define _OSTREE_DEFAULT_LOCK_TIMEOUT_SECONDS 30

/* In most cases, writing to disk should be much faster than
* fetching from the network, so we shouldn't actually hit
* this. But if using pipelining and e.g. pulling over LAN
Expand Down Expand Up @@ -157,6 +159,7 @@ struct OstreeRepo {
guint64 tmp_expiry_seconds;
gchar *collection_id;
gboolean add_remotes_config_dir; /* Add new remotes in remotes.d dir */
gint lock_timeout_seconds;

OstreeRepo *parent_repo;
};
Expand Down Expand Up @@ -436,6 +439,23 @@ _ostree_repo_get_remote_inherited (OstreeRepo *self,

#ifndef OSTREE_ENABLE_EXPERIMENTAL_API

/* All the locking APIs below are duplicated in ostree-repo.h. Remove the ones
* here once it's no longer experimental.
*/

typedef enum {
OSTREE_REPO_LOCK_SHARED,
OSTREE_REPO_LOCK_EXCLUSIVE
} OstreeRepoLockType;

gboolean ostree_repo_lock_push (OstreeRepo *self,
OstreeRepoLockType lock_type,
GCancellable *cancellable,
GError **error);
gboolean ostree_repo_lock_pop (OstreeRepo *self,
GCancellable *cancellable,
GError **error);

const gchar * ostree_repo_get_collection_id (OstreeRepo *self);
gboolean ostree_repo_set_collection_id (OstreeRepo *self,
const gchar *collection_id,
Expand Down
Loading

0 comments on commit 4e78ddd

Please sign in to comment.