Commit

aae63be1d8391401e0ad1663ed159bcd03326b0a

reftable/stack: remove `REFTABLE_STACK_NEW_ADDITION_RELOAD`

In 80e7342ea8 (reftable/stack: allow locking of outdated stacks,
2024-09-24), the `REFTABLE_STACK_NEW_ADDITION_RELOAD` was introduced so
that callers of `reftable_stack_init_addition()` can also reload the
stack if there was a concurrent update made before the lock was
obtained.

Then 16684b6fae (refs/reftable: always reload stacks when creating
lock, 2025-08-12) updated all of the remaining call-sites to propagate
this flag to ensure that we always reload the stack whenever there was a
concurrent update.

As all calls to `reftable_stack_init_addition()` inevitably propagate
the flag, it is safe to remove the flag and its associated code and make
the reloading of the stack the default flow. This makes it easier to
follow the flow and simplifies the logic.

The only exceptions are:

  1. Unit tests, where we explicitly do not propagate the flag. These
     tests are now modified with the new status quo.

  2. `reftable_stack_clean()`, which was propagating 0 to
     `reftable_stack_new_addition()` but was then manually reloading the
     stack after. Here the new flow will achieve the same, while also
     allowing us to remove the manual reload.

This also makes two checks for 'REFTABLE_OUTDATED_ERROR' redundant, so
remove them also.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak <karthik.188@gmail.com> · 2026-08-24 09:30 UTC · committed by Junio C Hamano

Browse files
4 files changed, +49 −92
modified refs/reftable-backend.c
@@ -1003,8 +1003,7 @@ static int prepare_transaction_update(struct write_transaction_table_arg **out,
1003 1003 struct reftable_addition *addition;
1004 1004
1005 1005 ret = reftable_stack_new_addition(&addition, be->stack,
1006 - &reftable_be_write_options(refs)->opts,
1007 - REFTABLE_STACK_NEW_ADDITION_RELOAD);
1006 + &reftable_be_write_options(refs)->opts);
1008 1007 if (ret) {
1009 1008 if (ret == REFTABLE_LOCK_ERROR)
1010 1009 strbuf_addstr(err, "cannot lock references");
@@ -2010,8 +2009,7 @@ static int reftable_be_rename_ref(struct ref_store *ref_store,
2010 2009 if (ret)
2011 2010 goto done;
2012 2011 ret = reftable_stack_add(arg.be->stack, &write_copy_table, &arg,
2013 - &reftable_be_write_options(refs)->opts,
2014 - REFTABLE_STACK_NEW_ADDITION_RELOAD);
2012 + &reftable_be_write_options(refs)->opts);
2015 2013
2016 2014 done:
2017 2015 assert(ret != REFTABLE_API_ERROR);
@@ -2041,8 +2039,7 @@ static int reftable_be_copy_ref(struct ref_store *ref_store,
2041 2039 if (ret)
2042 2040 goto done;
2043 2041 ret = reftable_stack_add(arg.be->stack, &write_copy_table, &arg,
2044 - &reftable_be_write_options(refs)->opts,
2045 - REFTABLE_STACK_NEW_ADDITION_RELOAD);
2042 + &reftable_be_write_options(refs)->opts);
2046 2043
2047 2044 done:
2048 2045 assert(ret != REFTABLE_API_ERROR);
@@ -2424,8 +2421,7 @@ static int reftable_be_create_reflog(struct ref_store *ref_store,
2424 2421 arg.stack = be->stack;
2425 2422
2426 2423 ret = reftable_stack_add(be->stack, &write_reflog_existence_table, &arg,
2427 - &reftable_be_write_options(refs)->opts,
2428 - REFTABLE_STACK_NEW_ADDITION_RELOAD);
2424 + &reftable_be_write_options(refs)->opts);
2429 2425
2430 2426 done:
2431 2427 return ret;
@@ -2499,8 +2495,7 @@ static int reftable_be_delete_reflog(struct ref_store *ref_store,
2499 2495 arg.stack = be->stack;
2500 2496
2501 2497 ret = reftable_stack_add(be->stack, &write_reflog_delete_table, &arg,
2502 - &reftable_be_write_options(refs)->opts,
2503 - REFTABLE_STACK_NEW_ADDITION_RELOAD);
2498 + &reftable_be_write_options(refs)->opts);
2504 2499
2505 2500 assert(ret != REFTABLE_API_ERROR);
2506 2501 return ret;
@@ -2622,8 +2617,7 @@ static int reftable_be_reflog_expire(struct ref_store *ref_store,
2622 2617 goto done;
2623 2618
2624 2619 ret = reftable_stack_new_addition(&add, be->stack,
2625 - &reftable_be_write_options(refs)->opts,
2626 - REFTABLE_STACK_NEW_ADDITION_RELOAD);
2620 + &reftable_be_write_options(refs)->opts);
2627 2621 if (ret < 0)
2628 2622 goto done;
2629 2623
modified reftable/reftable-stack.h
@@ -58,22 +58,13 @@ uint64_t reftable_stack_next_update_index(struct reftable_stack *st);
58 58 /* holds a transaction to add tables at the top of a stack. */
59 59 struct reftable_addition;
60 60
61 - enum {
62 - /*
63 - * Reload the stack when the stack is out-of-date after locking it.
64 - */
65 - REFTABLE_STACK_NEW_ADDITION_RELOAD = (1 << 0),
66 - };
67 -
68 61 /*
69 62 * returns a new transaction to add reftables to the given stack. As a side
70 - * effect, the ref database is locked. Accepts REFTABLE_STACK_NEW_ADDITION_*
71 - * flags.
63 + * effect, the ref database is locked.
72 64 */
73 65 int reftable_stack_new_addition(struct reftable_addition **dest,
74 66 struct reftable_stack *st,
75 - const struct reftable_write_options *opts,
76 - unsigned int flags);
67 + const struct reftable_write_options *opts);
77 68
78 69 /* Adds a reftable to transaction. */
79 70 int reftable_addition_add(struct reftable_addition *add,
@@ -93,14 +84,12 @@ void reftable_addition_destroy(struct reftable_addition *add);
93 84 /*
94 85 * Add a new table to the stack. The write_table function must call
95 86 * reftable_writer_set_limits, add refs and return an error value.
96 - * The flags are passed through to `reftable_stack_new_addition()`.
97 87 */
98 88 int reftable_stack_add(struct reftable_stack *st,
99 89 int (*write_table)(struct reftable_writer *wr,
100 90 void *write_arg),
101 91 void *write_arg,
102 - const struct reftable_write_options *opts,
103 - unsigned flags);
92 + const struct reftable_write_options *opts);
104 93
105 94 struct reftable_iterator;
106 95
modified reftable/stack.c
@@ -659,8 +659,7 @@ static void reftable_addition_close(struct reftable_addition *add)
659 659
660 660 static int reftable_stack_init_addition(struct reftable_addition *add,
661 661 struct reftable_stack *st,
662 - const struct reftable_write_options *opts,
663 - unsigned int flags)
662 + const struct reftable_write_options *opts)
664 663 {
665 664 struct reftable_buf lock_file_name = REFTABLE_BUF_INIT;
666 665 int err;
@@ -686,15 +685,11 @@ static int reftable_stack_init_addition(struct reftable_addition *add,
686 685 err = stack_uptodate(st);
687 686 if (err < 0)
688 687 goto done;
689 - if (err > 0 && flags & REFTABLE_STACK_NEW_ADDITION_RELOAD) {
688 + if (err > 0) {
690 689 err = reftable_stack_reload_maybe_reuse(add->stack, 1);
691 690 if (err)
692 691 goto done;
693 692 }
694 - if (err > 0) {
695 - err = REFTABLE_OUTDATED_ERROR;
696 - goto done;
697 - }
698 693
699 694 add->next_update_index = reftable_stack_next_update_index(st);
700 695 done:
@@ -708,13 +703,12 @@ static int stack_try_add(struct reftable_stack *st,
708 703 int (*write_table)(struct reftable_writer *wr,
709 704 void *arg),
710 705 void *arg,
711 - const struct reftable_write_options *opts,
712 - unsigned flags)
706 + const struct reftable_write_options *opts)
713 707 {
714 708 struct reftable_addition add;
715 709 int err;
716 710
717 - err = reftable_stack_init_addition(&add, st, opts, flags);
711 + err = reftable_stack_init_addition(&add, st, opts);
718 712 if (err < 0)
719 713 goto done;
720 714
@@ -731,17 +725,10 @@ done:
731 725 int reftable_stack_add(struct reftable_stack *st,
732 726 int (*write)(struct reftable_writer *wr, void *arg),
733 727 void *arg,
734 - const struct reftable_write_options *opts,
735 - unsigned flags)
728 + const struct reftable_write_options *opts)
736 729 {
737 - int err = stack_try_add(st, write, arg, opts, flags);
730 + int err = stack_try_add(st, write, arg, opts);
738 731 if (err < 0) {
739 - if (err == REFTABLE_OUTDATED_ERROR) {
740 - /* Ignore error return, we want to propagate
741 - REFTABLE_OUTDATED_ERROR.
742 - */
743 - reftable_stack_reload(st);
744 - }
745 732 return err;
746 733 }
747 734
@@ -843,8 +830,7 @@ done:
843 830
844 831 int reftable_stack_new_addition(struct reftable_addition **dest,
845 832 struct reftable_stack *st,
846 - const struct reftable_write_options *opts,
847 - unsigned int flags)
833 + const struct reftable_write_options *opts)
848 834 {
849 835 int err;
850 836
@@ -852,7 +838,7 @@ int reftable_stack_new_addition(struct reftable_addition **dest,
852 838 if (!*dest)
853 839 return REFTABLE_OUT_OF_MEMORY_ERROR;
854 840
855 - err = reftable_stack_init_addition(*dest, st, opts, flags);
841 + err = reftable_stack_init_addition(*dest, st, opts);
856 842 if (err) {
857 843 reftable_free(*dest);
858 844 *dest = NULL;
@@ -1840,12 +1826,7 @@ static int reftable_stack_clean_locked(struct reftable_stack *st)
1840 1826 int reftable_stack_clean(struct reftable_stack *st)
1841 1827 {
1842 1828 struct reftable_addition *add = NULL;
1843 - int err = reftable_stack_new_addition(&add, st, NULL, 0);
1844 - if (err < 0) {
1845 - goto done;
1846 - }
1847 -
1848 - err = reftable_stack_reload(st);
1829 + int err = reftable_stack_new_addition(&add, st, NULL);
1849 1830 if (err < 0) {
1850 1831 goto done;
1851 1832 }
modified t/unit-tests/u-reftable-stack.c
@@ -127,7 +127,7 @@ static void write_n_ref_tables(struct reftable_stack *st,
127 127 cl_reftable_set_hash(ref.value.val1, i, REFTABLE_HASH_SHA1);
128 128
129 129 cl_assert_equal_i(reftable_stack_add(st,
130 - &write_test_ref, &ref, &opts, 0), 0);
130 + &write_test_ref, &ref, &opts), 0);
131 131 }
132 132 }
133 133
@@ -168,7 +168,7 @@ void test_reftable_stack__add_one(void)
168 168 err = reftable_new_stack(&st, dir, NULL);
169 169 cl_assert(!err);
170 170
171 - err = reftable_stack_add(st, write_test_ref, &ref, &opts, 0);
171 + err = reftable_stack_add(st, write_test_ref, &ref, &opts);
172 172 cl_assert(!err);
173 173
174 174 err = reftable_stack_read_ref(st, ref.refname, &dest);
@@ -231,12 +231,9 @@ void test_reftable_stack__uptodate(void)
231 231 cl_assert_equal_i(reftable_new_stack(&st1, dir, NULL), 0);
232 232 cl_assert_equal_i(reftable_new_stack(&st2, dir, NULL), 0);
233 233 cl_assert_equal_i(reftable_stack_add(st1, write_test_ref,
234 - &ref1, NULL, 0), 0);
234 + &ref1, NULL), 0);
235 235 cl_assert_equal_i(reftable_stack_add(st2, write_test_ref,
236 - &ref2, NULL, 0), REFTABLE_OUTDATED_ERROR);
237 - cl_assert_equal_i(reftable_stack_reload(st2), 0);
238 - cl_assert_equal_i(reftable_stack_add(st2, write_test_ref,
239 - &ref2, NULL, 0), 0);
236 + &ref2, NULL), 0);
240 237 reftable_stack_destroy(st1);
241 238 reftable_stack_destroy(st2);
242 239 clear_dir(dir);
@@ -260,7 +257,7 @@ void test_reftable_stack__transaction_api(void)
260 257
261 258 reftable_addition_destroy(add);
262 259
263 - cl_assert_equal_i(reftable_stack_new_addition(&add, st, NULL, 0), 0);
260 + cl_assert_equal_i(reftable_stack_new_addition(&add, st, NULL), 0);
264 261 cl_assert_equal_i(reftable_addition_add(add, write_test_ref,
265 262 &ref), 0);
266 263 cl_assert_equal_i(reftable_addition_commit(add), 0);
@@ -301,21 +298,17 @@ void test_reftable_stack__transaction_with_reload(void)
301 298
302 299 cl_assert_equal_i(reftable_new_stack(&st1, dir, NULL), 0);
303 300 cl_assert_equal_i(reftable_new_stack(&st2, dir, NULL), 0);
304 - cl_assert_equal_i(reftable_stack_new_addition(&add, st1, NULL, 0), 0);
301 + cl_assert_equal_i(reftable_stack_new_addition(&add, st1, NULL), 0);
305 302 cl_assert_equal_i(reftable_addition_add(add, write_test_ref,
306 303 &refs[0]), 0);
307 304 cl_assert_equal_i(reftable_addition_commit(add), 0);
308 305 reftable_addition_destroy(add);
309 306
310 307 /*
311 - * The second stack is now outdated, which we should notice. We do not
312 - * create the addition and lock the stack by default, but allow the
313 - * reload to happen when REFTABLE_STACK_NEW_ADDITION_RELOAD is set.
308 + * The second stack is now outdated, but it should automatically reload it
309 + * with the newer updates.
314 310 */
315 - cl_assert_equal_i(reftable_stack_new_addition(&add, st2, NULL, 0),
316 - REFTABLE_OUTDATED_ERROR);
317 - cl_assert_equal_i(reftable_stack_new_addition(&add, st2, NULL,
318 - REFTABLE_STACK_NEW_ADDITION_RELOAD), 0);
311 + cl_assert_equal_i(reftable_stack_new_addition(&add, st2, NULL), 0);
319 312 cl_assert_equal_i(reftable_addition_add(add, write_test_ref,
320 313 &refs[1]), 0);
321 314 cl_assert_equal_i(reftable_addition_commit(add), 0);
@@ -363,7 +356,7 @@ void test_reftable_stack__transaction_api_performs_auto_compaction(void)
363 356 * better control over when exactly auto compaction runs.
364 357 */
365 358 cl_assert_equal_i(reftable_stack_new_addition(&add,
366 - st, &write_opts, 0), 0);
359 + st, &write_opts), 0);
367 360 cl_assert_equal_i(reftable_addition_add(add,
368 361 write_test_ref, &ref), 0);
369 362 cl_assert_equal_i(reftable_addition_commit(add), 0);
@@ -400,7 +393,7 @@ void test_reftable_stack__auto_compaction_fails_gracefully(void)
400 393
401 394 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0);
402 395 cl_assert_equal_i(reftable_stack_add(st, write_test_ref,
403 - &ref, NULL, 0), 0);
396 + &ref, NULL), 0);
404 397 cl_assert_equal_i(st->merged->tables_len, 1);
405 398 cl_assert_equal_i(st->stats.attempts, 0);
406 399 cl_assert_equal_i(st->stats.failures, 0);
@@ -418,7 +411,7 @@ void test_reftable_stack__auto_compaction_fails_gracefully(void)
418 411 write_file_buf(table_path.buf, "", 0);
419 412
420 413 ref.update_index = 2;
421 - err = reftable_stack_add(st, write_test_ref, &ref, NULL, 0);
414 + err = reftable_stack_add(st, write_test_ref, &ref, NULL);
422 415 cl_assert(!err);
423 416 cl_assert_equal_i(st->merged->tables_len, 2);
424 417 cl_assert_equal_i(st->stats.attempts, 1);
@@ -453,9 +446,9 @@ void test_reftable_stack__update_index_check(void)
453 446
454 447 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0);
455 448 cl_assert_equal_i(reftable_stack_add(st, write_test_ref,
456 - &ref1, NULL, 0), 0);
449 + &ref1, NULL), 0);
457 450 cl_assert_equal_i(reftable_stack_add(st, write_test_ref,
458 - &ref2, NULL, 0), REFTABLE_API_ERROR);
451 + &ref2, NULL), REFTABLE_API_ERROR);
459 452 reftable_stack_destroy(st);
460 453 clear_dir(dir);
461 454 }
@@ -469,7 +462,7 @@ void test_reftable_stack__lock_failure(void)
469 462 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0);
470 463 for (i = -1; i != REFTABLE_EMPTY_TABLE_ERROR; i--)
471 464 cl_assert_equal_i(reftable_stack_add(st, write_error,
472 - &i, NULL, 0), i);
465 + &i, NULL), i);
473 466
474 467 reftable_stack_destroy(st);
475 468 clear_dir(dir);
@@ -513,7 +506,7 @@ void test_reftable_stack__add(void)
513 506
514 507 for (i = 0; i < N; i++)
515 508 cl_assert_equal_i(reftable_stack_add(st, write_test_ref,
516 - &refs[i], &opts, 0), 0);
509 + &refs[i], &opts), 0);
517 510
518 511 for (i = 0; i < N; i++) {
519 512 struct write_log_arg arg = {
@@ -521,7 +514,7 @@ void test_reftable_stack__add(void)
521 514 .update_index = reftable_stack_next_update_index(st),
522 515 };
523 516 cl_assert_equal_i(reftable_stack_add(st, write_test_log,
524 - &arg, &opts, 0), 0);
517 + &arg, &opts), 0);
525 518 }
526 519
527 520 cl_assert_equal_i(reftable_stack_compact_all(st, &opts, NULL), 0);
@@ -604,7 +597,7 @@ void test_reftable_stack__iterator(void)
604 597
605 598 for (i = 0; i < N; i++)
606 599 cl_assert_equal_i(reftable_stack_add(st, write_test_ref,
607 - &refs[i], NULL, 0), 0);
600 + &refs[i], NULL), 0);
608 601
609 602 for (i = 0; i < N; i++) {
610 603 struct write_log_arg arg = {
@@ -613,7 +606,7 @@ void test_reftable_stack__iterator(void)
613 606 };
614 607
615 608 cl_assert_equal_i(reftable_stack_add(st, write_test_log,
616 - &arg, NULL, 0), 0);
609 + &arg, NULL), 0);
617 610 }
618 611
619 612 reftable_stack_init_ref_iterator(st, &it);
@@ -685,11 +678,11 @@ void test_reftable_stack__log_normalize(void)
685 678
686 679 input.value.update.message = (char *) "one\ntwo";
687 680 cl_assert_equal_i(reftable_stack_add(st, write_test_log,
688 - &arg, NULL, 0), REFTABLE_API_ERROR);
681 + &arg, NULL), REFTABLE_API_ERROR);
689 682
690 683 input.value.update.message = (char *) "one";
691 684 cl_assert_equal_i(reftable_stack_add(st, write_test_log,
692 - &arg, NULL, 0), 0);
685 + &arg, NULL), 0);
693 686 cl_assert_equal_i(reftable_stack_read_log(st, input.refname,
694 687 &dest), 0);
695 688 cl_assert_equal_s(dest.value.update.message, "one\n");
@@ -697,7 +690,7 @@ void test_reftable_stack__log_normalize(void)
697 690 input.value.update.message = (char *) "two\n";
698 691 arg.update_index = 2;
699 692 cl_assert_equal_i(reftable_stack_add(st, write_test_log,
700 - &arg, NULL, 0), 0);
693 + &arg, NULL), 0);
701 694 cl_assert_equal_i(reftable_stack_read_log(st, input.refname,
702 695 &dest), 0);
703 696 cl_assert_equal_s(dest.value.update.message, "two\n");
@@ -747,7 +740,7 @@ void test_reftable_stack__tombstone(void)
747 740 }
748 741 for (i = 0; i < N; i++)
749 742 cl_assert_equal_i(reftable_stack_add(st, write_test_ref,
750 - &refs[i], NULL, 0), 0);
743 + &refs[i], NULL), 0);
751 744
752 745 for (i = 0; i < N; i++) {
753 746 struct write_log_arg arg = {
@@ -755,7 +748,7 @@ void test_reftable_stack__tombstone(void)
755 748 .update_index = reftable_stack_next_update_index(st),
756 749 };
757 750 cl_assert_equal_i(reftable_stack_add(st, write_test_log,
758 - &arg, NULL, 0), 0);
751 + &arg, NULL), 0);
759 752 }
760 753
761 754 cl_assert_equal_i(reftable_stack_read_ref(st, "branch",
@@ -801,7 +794,7 @@ void test_reftable_stack__hash_id(void)
801 794
802 795 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0);
803 796 cl_assert_equal_i(reftable_stack_add(st, write_test_ref,
804 - &ref, NULL, 0), 0);
797 + &ref, NULL), 0);
805 798
806 799 /* can't read it with the wrong hash ID. */
807 800 cl_assert_equal_i(reftable_new_stack(&st32, dir,
@@ -869,7 +862,7 @@ void test_reftable_stack__reflog_expire(void)
869 862 .update_index = reftable_stack_next_update_index(st),
870 863 };
871 864 cl_assert_equal_i(reftable_stack_add(st, write_test_log,
872 - &arg, NULL, 0), 0);
865 + &arg, NULL), 0);
873 866 }
874 867
875 868 cl_assert_equal_i(reftable_stack_compact_all(st, NULL, NULL), 0);
@@ -908,7 +901,7 @@ void test_reftable_stack__empty_add(void)
908 901
909 902 cl_assert_equal_i(reftable_new_stack(&st, dir, NULL), 0);
910 903 cl_assert_equal_i(reftable_stack_add(st, write_nothing,
911 - NULL, NULL, 0), 0);
904 + NULL, NULL), 0);
912 905 cl_assert_equal_i(reftable_new_stack(&st2, dir, NULL), 0);
913 906 clear_dir(dir);
914 907 reftable_stack_destroy(st);
@@ -947,7 +940,7 @@ void test_reftable_stack__auto_compaction(void)
947 940 };
948 941 snprintf(name, sizeof(name), "branch%04"PRIuMAX, (uintmax_t)i);
949 942
950 - err = reftable_stack_add(st, write_test_ref, &ref, &opts, 0);
943 + err = reftable_stack_add(st, write_test_ref, &ref, &opts);
951 944 cl_assert(!err);
952 945
953 946 err = reftable_stack_auto_compact(st, &opts);
@@ -983,7 +976,7 @@ void test_reftable_stack__auto_compaction_factor(void)
983 976 };
984 977 xsnprintf(name, sizeof(name), "branch%04"PRIuMAX, (uintmax_t)i);
985 978
986 - err = reftable_stack_add(st, &write_test_ref, &ref, &opts, 0);
979 + err = reftable_stack_add(st, &write_test_ref, &ref, &opts);
987 980 cl_assert(!err);
988 981
989 982 cl_assert(i < 5 || st->merged->tables_len < 5 * fastlogN(i, 5));
@@ -1064,7 +1057,7 @@ void test_reftable_stack__add_performs_auto_compaction(void)
1064 1057 ref.refname = buf;
1065 1058
1066 1059 cl_assert_equal_i(reftable_stack_add(st, write_test_ref,
1067 - &ref, &write_opts, 0), 0);
1060 + &ref, &write_opts), 0);
1068 1061
1069 1062 /*
1070 1063 * The stack length should grow continuously for all runs where
@@ -1303,7 +1296,7 @@ void test_reftable_stack__invalid_limit_updates(void)
1303 1296
1304 1297 reftable_addition_destroy(add);
1305 1298
1306 - cl_assert_equal_i(reftable_stack_new_addition(&add, st, &opts, 0), 0);
1299 + cl_assert_equal_i(reftable_stack_new_addition(&add, st, &opts), 0);
1307 1300
1308 1301 /*
1309 1302 * write_limits_after_ref also updates the update indexes after adding