Skip to content

Commit 80e7342

Browse files
pks-tgitster
authored andcommitted
reftable/stack: allow locking of outdated stacks
In `reftable_stack_new_addition()` we first lock the stack and then check whether it is still up-to-date. If it is not we return an error to the caller indicating that the stack is outdated. This is overly restrictive in our ref transaction interface though: we lock the stack right before we start to verify the transaction, so we do not really care whether it is outdated or not. What we really want is that the stack is up-to-date after it has been locked so that we can verify queued updates against its current state while we know that it is locked for concurrent modification. Introduce a new flag `REFTABLE_STACK_NEW_ADDITION_RELOAD` that alters the behaviour of `reftable_stack_init_addition()` in this case: when we notice that it is out-of-date we reload it instead of returning an error to the caller. This logic will be wired up in the reftable backend in the next commit. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bc39b6a commit 80e7342

File tree

4 files changed

+91
-13
lines changed

4 files changed

+91
-13
lines changed

refs/reftable-backend.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ static int prepare_transaction_update(struct write_transaction_table_arg **out,
770770
if (ret)
771771
return ret;
772772

773-
ret = reftable_stack_new_addition(&addition, stack);
773+
ret = reftable_stack_new_addition(&addition, stack, 0);
774774
if (ret) {
775775
if (ret == REFTABLE_LOCK_ERROR)
776776
strbuf_addstr(err, "cannot lock references");
@@ -2207,7 +2207,7 @@ static int reftable_be_reflog_expire(struct ref_store *ref_store,
22072207
if (ret < 0)
22082208
goto done;
22092209

2210-
ret = reftable_stack_new_addition(&add, stack);
2210+
ret = reftable_stack_new_addition(&add, stack, 0);
22112211
if (ret < 0)
22122212
goto done;
22132213

reftable/reftable-stack.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,21 @@ uint64_t reftable_stack_next_update_index(struct reftable_stack *st);
3737
/* holds a transaction to add tables at the top of a stack. */
3838
struct reftable_addition;
3939

40+
enum {
41+
/*
42+
* Reload the stack when the stack is out-of-date after locking it.
43+
*/
44+
REFTABLE_STACK_NEW_ADDITION_RELOAD = (1 << 0),
45+
};
46+
4047
/*
4148
* returns a new transaction to add reftables to the given stack. As a side
42-
* effect, the ref database is locked.
49+
* effect, the ref database is locked. Accepts REFTABLE_STACK_NEW_ADDITION_*
50+
* flags.
4351
*/
4452
int reftable_stack_new_addition(struct reftable_addition **dest,
45-
struct reftable_stack *st);
53+
struct reftable_stack *st,
54+
unsigned int flags);
4655

4756
/* Adds a reftable to transaction. */
4857
int reftable_addition_add(struct reftable_addition *add,

reftable/stack.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,8 @@ struct reftable_addition {
596596
#define REFTABLE_ADDITION_INIT {0}
597597

598598
static int reftable_stack_init_addition(struct reftable_addition *add,
599-
struct reftable_stack *st)
599+
struct reftable_stack *st,
600+
unsigned int flags)
600601
{
601602
struct strbuf lock_file_name = STRBUF_INIT;
602603
int err;
@@ -626,16 +627,20 @@ static int reftable_stack_init_addition(struct reftable_addition *add,
626627
err = stack_uptodate(st);
627628
if (err < 0)
628629
goto done;
630+
if (err > 0 && flags & REFTABLE_STACK_NEW_ADDITION_RELOAD) {
631+
err = reftable_stack_reload_maybe_reuse(add->stack, 1);
632+
if (err)
633+
goto done;
634+
}
629635
if (err > 0) {
630636
err = REFTABLE_OUTDATED_ERROR;
631637
goto done;
632638
}
633639

634640
add->next_update_index = reftable_stack_next_update_index(st);
635641
done:
636-
if (err) {
642+
if (err)
637643
reftable_addition_close(add);
638-
}
639644
strbuf_release(&lock_file_name);
640645
return err;
641646
}
@@ -739,13 +744,14 @@ int reftable_addition_commit(struct reftable_addition *add)
739744
}
740745

741746
int reftable_stack_new_addition(struct reftable_addition **dest,
742-
struct reftable_stack *st)
747+
struct reftable_stack *st,
748+
unsigned int flags)
743749
{
744750
int err = 0;
745751
struct reftable_addition empty = REFTABLE_ADDITION_INIT;
746752
REFTABLE_CALLOC_ARRAY(*dest, 1);
747753
**dest = empty;
748-
err = reftable_stack_init_addition(*dest, st);
754+
err = reftable_stack_init_addition(*dest, st, flags);
749755
if (err) {
750756
reftable_free(*dest);
751757
*dest = NULL;
@@ -759,7 +765,7 @@ static int stack_try_add(struct reftable_stack *st,
759765
void *arg)
760766
{
761767
struct reftable_addition add = REFTABLE_ADDITION_INIT;
762-
int err = reftable_stack_init_addition(&add, st);
768+
int err = reftable_stack_init_addition(&add, st, 0);
763769
if (err < 0)
764770
goto done;
765771

@@ -1608,7 +1614,7 @@ static int reftable_stack_clean_locked(struct reftable_stack *st)
16081614
int reftable_stack_clean(struct reftable_stack *st)
16091615
{
16101616
struct reftable_addition *add = NULL;
1611-
int err = reftable_stack_new_addition(&add, st);
1617+
int err = reftable_stack_new_addition(&add, st, 0);
16121618
if (err < 0) {
16131619
goto done;
16141620
}

t/unit-tests/t-reftable-stack.c

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ static void t_reftable_stack_transaction_api(void)
271271

272272
reftable_addition_destroy(add);
273273

274-
err = reftable_stack_new_addition(&add, st);
274+
err = reftable_stack_new_addition(&add, st, 0);
275275
check(!err);
276276

277277
err = reftable_addition_add(add, write_test_ref, &ref);
@@ -292,6 +292,68 @@ static void t_reftable_stack_transaction_api(void)
292292
clear_dir(dir);
293293
}
294294

295+
static void t_reftable_stack_transaction_with_reload(void)
296+
{
297+
char *dir = get_tmp_dir(__LINE__);
298+
struct reftable_stack *st1 = NULL, *st2 = NULL;
299+
int err;
300+
struct reftable_addition *add = NULL;
301+
struct reftable_ref_record refs[2] = {
302+
{
303+
.refname = (char *) "refs/heads/a",
304+
.update_index = 1,
305+
.value_type = REFTABLE_REF_VAL1,
306+
.value.val1 = { '1' },
307+
},
308+
{
309+
.refname = (char *) "refs/heads/b",
310+
.update_index = 2,
311+
.value_type = REFTABLE_REF_VAL1,
312+
.value.val1 = { '1' },
313+
},
314+
};
315+
struct reftable_ref_record ref = { 0 };
316+
317+
err = reftable_new_stack(&st1, dir, NULL);
318+
check(!err);
319+
err = reftable_new_stack(&st2, dir, NULL);
320+
check(!err);
321+
322+
err = reftable_stack_new_addition(&add, st1, 0);
323+
check(!err);
324+
err = reftable_addition_add(add, write_test_ref, &refs[0]);
325+
check(!err);
326+
err = reftable_addition_commit(add);
327+
check(!err);
328+
reftable_addition_destroy(add);
329+
330+
/*
331+
* The second stack is now outdated, which we should notice. We do not
332+
* create the addition and lock the stack by default, but allow the
333+
* reload to happen when REFTABLE_STACK_NEW_ADDITION_RELOAD is set.
334+
*/
335+
err = reftable_stack_new_addition(&add, st2, 0);
336+
check_int(err, ==, REFTABLE_OUTDATED_ERROR);
337+
err = reftable_stack_new_addition(&add, st2, REFTABLE_STACK_NEW_ADDITION_RELOAD);
338+
check(!err);
339+
err = reftable_addition_add(add, write_test_ref, &refs[1]);
340+
check(!err);
341+
err = reftable_addition_commit(add);
342+
check(!err);
343+
reftable_addition_destroy(add);
344+
345+
for (size_t i = 0; i < ARRAY_SIZE(refs); i++) {
346+
err = reftable_stack_read_ref(st2, refs[i].refname, &ref);
347+
check(!err);
348+
check(reftable_ref_record_equal(&refs[i], &ref, GIT_SHA1_RAWSZ));
349+
}
350+
351+
reftable_ref_record_release(&ref);
352+
reftable_stack_destroy(st1);
353+
reftable_stack_destroy(st2);
354+
clear_dir(dir);
355+
}
356+
295357
static void t_reftable_stack_transaction_api_performs_auto_compaction(void)
296358
{
297359
char *dir = get_tmp_dir(__LINE__);
@@ -322,7 +384,7 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void)
322384
*/
323385
st->opts.disable_auto_compact = i != n;
324386

325-
err = reftable_stack_new_addition(&add, st);
387+
err = reftable_stack_new_addition(&add, st, 0);
326388
check(!err);
327389

328390
err = reftable_addition_add(add, write_test_ref, &ref);
@@ -1314,6 +1376,7 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
13141376
TEST(t_reftable_stack_reload_with_missing_table(), "stack iteration with garbage tables");
13151377
TEST(t_reftable_stack_tombstone(), "'tombstone' refs in stack");
13161378
TEST(t_reftable_stack_transaction_api(), "update transaction to stack");
1379+
TEST(t_reftable_stack_transaction_with_reload(), "transaction with reload");
13171380
TEST(t_reftable_stack_transaction_api_performs_auto_compaction(), "update transaction triggers auto-compaction");
13181381
TEST(t_reftable_stack_update_index_check(), "update transactions with equal update indices");
13191382
TEST(t_reftable_stack_uptodate(), "stack must be reloaded before ref update");

0 commit comments

Comments
 (0)