t-reftable-stack: add test for non-default compaction factor

In a recent codebase update (commit ae8e378430, merge branch 'ps/reftable-write-options', 2024/05/13) the geometric factor used in auto-compaction of reftable tables was made configurable. Add a test to verify the functionality introduced by this update. Mentored-by: Patrick Steinhardt <ps@pks.im> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Chandra Pratap committed Sep 8, 2024 at 09:36 UTC e87952443a90fae9690db6c8dbf05d9ba97541ba
1 file changed +37 -4
t/unit-tests/t-reftable-stack.c
+37 -4
@@ -831,12 +831,12 @@ static void t_empty_add(void)
831 reftable_stack_destroy(st2);
832 }
833
834 -static int fastlog2(uint64_t sz)
834 +static int fastlogN(uint64_t sz, uint64_t N)
835 {
836 int l = 0;
837 if (sz == 0)
838 return 0;
839 - for (; sz; sz /= 2)
839 + for (; sz; sz /= N)
840 l++;
841 return l - 1;
842 }
@@ -869,11 +869,43 @@ static void t_reftable_stack_auto_compaction(void)
869
870 err = reftable_stack_auto_compact(st);
871 check(!err);
872 - check(i < 3 || st->merged->readers_len < 2 * fastlog2(i));
872 + check(i < 2 || st->merged->readers_len < 2 * fastlogN(i, 2));
873 }
874
875 check_int(reftable_stack_compaction_stats(st)->entries_written, <,
876 - (uint64_t)(N * fastlog2(N)));
876 + (uint64_t)(N * fastlogN(N, 2)));
877 +
878 + reftable_stack_destroy(st);
879 + clear_dir(dir);
880 +}
881 +
882 +static void t_reftable_stack_auto_compaction_factor(void)
883 +{
884 + struct reftable_write_options opts = {
885 + .auto_compaction_factor = 5,
886 + };
887 + struct reftable_stack *st = NULL;
888 + char *dir = get_tmp_dir(__LINE__);
889 + int err;
890 + size_t N = 100;
891 +
892 + err = reftable_new_stack(&st, dir, &opts);
893 + check(!err);
894 +
895 + for (size_t i = 0; i < N; i++) {
896 + char name[20];
897 + struct reftable_ref_record ref = {
898 + .refname = name,
899 + .update_index = reftable_stack_next_update_index(st),
900 + .value_type = REFTABLE_REF_VAL1,
901 + };
902 + xsnprintf(name, sizeof(name), "branch%04"PRIuMAX, (uintmax_t)i);
903 +
904 + err = reftable_stack_add(st, &write_test_ref, &ref);
905 + check(!err);
906 +
907 + check(i < 5 || st->merged->readers_len < 5 * fastlogN(i, 5));
908 + }
909
910 reftable_stack_destroy(st);
911 clear_dir(dir);
@@ -1186,6 +1218,7 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
1218 TEST(t_reftable_stack_add_one(), "add a single ref record to stack");
1219 TEST(t_reftable_stack_add_performs_auto_compaction(), "addition to stack triggers auto-compaction");
1220 TEST(t_reftable_stack_auto_compaction(), "stack must form geometric sequence after compaction");
1221 + TEST(t_reftable_stack_auto_compaction_factor(), "auto-compaction with non-default geometric factor");
1222 TEST(t_reftable_stack_auto_compaction_fails_gracefully(), "failure on auto-compaction");
1223 TEST(t_reftable_stack_auto_compaction_with_locked_tables(), "auto compaction with locked tables");
1224 TEST(t_reftable_stack_compaction_concurrent(), "compaction with concurrent stack");