aboutsummaryrefslogtreecommitdiffstats
path: root/reftable
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2024-09-30 16:16:14 -0700
committerJunio C Hamano <gitster@pobox.com>2024-09-30 16:16:14 -0700
commitab68c70a8b90626be24d95a27f6495ab6e7e57a5 (patch)
tree72a3caa79d15bc26df1bc1b0259d3522db892bc2 /reftable
parent3857aae53f3633b7de63ad640737c657387ae0c6 (diff)
parent6241ce217046cc77d306bda06ad80ac1973b4932 (diff)
downloadgit-ab68c70a8b90626be24d95a27f6495ab6e7e57a5.tar.gz
Merge branch 'ps/reftable-concurrent-writes'
Give timeout to the locking code to write to reftable. * ps/reftable-concurrent-writes: refs/reftable: reload locked stack when preparing transaction reftable/stack: allow locking of outdated stacks refs/reftable: introduce "reftable.lockTimeout"
Diffstat (limited to 'reftable')
-rw-r--r--reftable/reftable-stack.h13
-rw-r--r--reftable/reftable-writer.h11
-rw-r--r--reftable/stack.c38
3 files changed, 47 insertions, 15 deletions
diff --git a/reftable/reftable-stack.h b/reftable/reftable-stack.h
index f4f8cabc7f..6370fe45dd 100644
--- a/reftable/reftable-stack.h
+++ b/reftable/reftable-stack.h
@@ -37,12 +37,21 @@ uint64_t reftable_stack_next_update_index(struct reftable_stack *st);
/* holds a transaction to add tables at the top of a stack. */
struct reftable_addition;
+enum {
+ /*
+ * Reload the stack when the stack is out-of-date after locking it.
+ */
+ REFTABLE_STACK_NEW_ADDITION_RELOAD = (1 << 0),
+};
+
/*
* returns a new transaction to add reftables to the given stack. As a side
- * effect, the ref database is locked.
+ * effect, the ref database is locked. Accepts REFTABLE_STACK_NEW_ADDITION_*
+ * flags.
*/
int reftable_stack_new_addition(struct reftable_addition **dest,
- struct reftable_stack *st);
+ struct reftable_stack *st,
+ unsigned int flags);
/* Adds a reftable to transaction. */
int reftable_addition_add(struct reftable_addition *add,
diff --git a/reftable/reftable-writer.h b/reftable/reftable-writer.h
index 189b1f4144..f5e25cfda1 100644
--- a/reftable/reftable-writer.h
+++ b/reftable/reftable-writer.h
@@ -51,6 +51,17 @@ struct reftable_write_options {
* tables to compact. Defaults to 2 if unset.
*/
uint8_t auto_compaction_factor;
+
+ /*
+ * The number of milliseconds to wait when trying to lock "tables.list".
+ * Note that this does not apply to locking individual tables, as these
+ * should only ever be locked when already holding the "tables.list"
+ * lock.
+ *
+ * Passing 0 will fail immediately when the file is locked, passing a
+ * negative value will cause us to block indefinitely.
+ */
+ long lock_timeout_ms;
};
/* reftable_block_stats holds statistics for a single block type */
diff --git a/reftable/stack.c b/reftable/stack.c
index ce0a35216b..84cf37a2ad 100644
--- a/reftable/stack.c
+++ b/reftable/stack.c
@@ -596,15 +596,18 @@ struct reftable_addition {
#define REFTABLE_ADDITION_INIT {0}
static int reftable_stack_init_addition(struct reftable_addition *add,
- struct reftable_stack *st)
+ struct reftable_stack *st,
+ unsigned int flags)
{
struct strbuf lock_file_name = STRBUF_INIT;
int err;
add->stack = st;
- err = hold_lock_file_for_update(&add->tables_list_lock, st->list_file,
- LOCK_NO_DEREF);
+ err = hold_lock_file_for_update_timeout(&add->tables_list_lock,
+ st->list_file,
+ LOCK_NO_DEREF,
+ st->opts.lock_timeout_ms);
if (err < 0) {
if (errno == EEXIST) {
err = REFTABLE_LOCK_ERROR;
@@ -624,6 +627,11 @@ static int reftable_stack_init_addition(struct reftable_addition *add,
err = stack_uptodate(st);
if (err < 0)
goto done;
+ if (err > 0 && flags & REFTABLE_STACK_NEW_ADDITION_RELOAD) {
+ err = reftable_stack_reload_maybe_reuse(add->stack, 1);
+ if (err)
+ goto done;
+ }
if (err > 0) {
err = REFTABLE_OUTDATED_ERROR;
goto done;
@@ -631,9 +639,8 @@ static int reftable_stack_init_addition(struct reftable_addition *add,
add->next_update_index = reftable_stack_next_update_index(st);
done:
- if (err) {
+ if (err)
reftable_addition_close(add);
- }
strbuf_release(&lock_file_name);
return err;
}
@@ -737,13 +744,14 @@ done:
}
int reftable_stack_new_addition(struct reftable_addition **dest,
- struct reftable_stack *st)
+ struct reftable_stack *st,
+ unsigned int flags)
{
int err = 0;
struct reftable_addition empty = REFTABLE_ADDITION_INIT;
REFTABLE_CALLOC_ARRAY(*dest, 1);
**dest = empty;
- err = reftable_stack_init_addition(*dest, st);
+ err = reftable_stack_init_addition(*dest, st, flags);
if (err) {
reftable_free(*dest);
*dest = NULL;
@@ -757,7 +765,7 @@ static int stack_try_add(struct reftable_stack *st,
void *arg)
{
struct reftable_addition add = REFTABLE_ADDITION_INIT;
- int err = reftable_stack_init_addition(&add, st);
+ int err = reftable_stack_init_addition(&add, st, 0);
if (err < 0)
goto done;
@@ -1056,8 +1064,10 @@ static int stack_compact_range(struct reftable_stack *st,
* Hold the lock so that we can read "tables.list" and lock all tables
* which are part of the user-specified range.
*/
- err = hold_lock_file_for_update(&tables_list_lock, st->list_file,
- LOCK_NO_DEREF);
+ err = hold_lock_file_for_update_timeout(&tables_list_lock,
+ st->list_file,
+ LOCK_NO_DEREF,
+ st->opts.lock_timeout_ms);
if (err < 0) {
if (errno == EEXIST)
err = REFTABLE_LOCK_ERROR;
@@ -1156,8 +1166,10 @@ static int stack_compact_range(struct reftable_stack *st,
* "tables.list". We'll then replace the compacted range of tables with
* the new table.
*/
- err = hold_lock_file_for_update(&tables_list_lock, st->list_file,
- LOCK_NO_DEREF);
+ err = hold_lock_file_for_update_timeout(&tables_list_lock,
+ st->list_file,
+ LOCK_NO_DEREF,
+ st->opts.lock_timeout_ms);
if (err < 0) {
if (errno == EEXIST)
err = REFTABLE_LOCK_ERROR;
@@ -1602,7 +1614,7 @@ static int reftable_stack_clean_locked(struct reftable_stack *st)
int reftable_stack_clean(struct reftable_stack *st)
{
struct reftable_addition *add = NULL;
- int err = reftable_stack_new_addition(&add, st);
+ int err = reftable_stack_new_addition(&add, st, 0);
if (err < 0) {
goto done;
}