Skip to content

Commit f663d34

Browse files
pks-tgitster
authored andcommitted
reftable: make the compaction factor configurable
When auto-compacting, the reftable library packs references such that the sizes of the tables form a geometric sequence. The factor for this geometric sequence is hardcoded to 2 right now. We're about to expose this as a config option though, so let's expose the factor via write options. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent afbdbfa commit f663d34

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

reftable/constants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ license that can be found in the LICENSE file or at
1717

1818
#define MAX_RESTARTS ((1 << 16) - 1)
1919
#define DEFAULT_BLOCK_SIZE 4096
20+
#define DEFAULT_GEOMETRIC_FACTOR 2
2021

2122
#endif

reftable/reftable-writer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ struct reftable_write_options {
4545

4646
/* boolean: Prevent auto-compaction of tables. */
4747
unsigned disable_auto_compact : 1;
48+
49+
/*
50+
* Geometric sequence factor used by auto-compaction to decide which
51+
* tables to compact. Defaults to 2 if unset.
52+
*/
53+
uint8_t auto_compaction_factor;
4854
};
4955

5056
/* reftable_block_stats holds statistics for a single block type */

reftable/stack.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ license that can be found in the LICENSE file or at
1010

1111
#include "../write-or-die.h"
1212
#include "system.h"
13+
#include "constants.h"
1314
#include "merged.h"
1415
#include "reader.h"
1516
#include "reftable-error.h"
@@ -1212,12 +1213,16 @@ static int segment_size(struct segment *s)
12121213
return s->end - s->start;
12131214
}
12141215

1215-
struct segment suggest_compaction_segment(uint64_t *sizes, size_t n)
1216+
struct segment suggest_compaction_segment(uint64_t *sizes, size_t n,
1217+
uint8_t factor)
12161218
{
12171219
struct segment seg = { 0 };
12181220
uint64_t bytes;
12191221
size_t i;
12201222

1223+
if (!factor)
1224+
factor = DEFAULT_GEOMETRIC_FACTOR;
1225+
12211226
/*
12221227
* If there are no tables or only a single one then we don't have to
12231228
* compact anything. The sequence is geometric by definition already.
@@ -1249,7 +1254,7 @@ struct segment suggest_compaction_segment(uint64_t *sizes, size_t n)
12491254
* 64, 32, 16, 8, 4, 3, 1
12501255
*/
12511256
for (i = n - 1; i > 0; i--) {
1252-
if (sizes[i - 1] < sizes[i] * 2) {
1257+
if (sizes[i - 1] < sizes[i] * factor) {
12531258
seg.end = i + 1;
12541259
bytes = sizes[i];
12551260
break;
@@ -1275,7 +1280,7 @@ struct segment suggest_compaction_segment(uint64_t *sizes, size_t n)
12751280
uint64_t curr = bytes;
12761281
bytes += sizes[i - 1];
12771282

1278-
if (sizes[i - 1] < curr * 2) {
1283+
if (sizes[i - 1] < curr * factor) {
12791284
seg.start = i - 1;
12801285
seg.bytes = bytes;
12811286
}
@@ -1301,7 +1306,8 @@ int reftable_stack_auto_compact(struct reftable_stack *st)
13011306
{
13021307
uint64_t *sizes = stack_table_sizes_for_compaction(st);
13031308
struct segment seg =
1304-
suggest_compaction_segment(sizes, st->merged->stack_len);
1309+
suggest_compaction_segment(sizes, st->merged->stack_len,
1310+
st->opts.auto_compaction_factor);
13051311
reftable_free(sizes);
13061312
if (segment_size(&seg) > 0)
13071313
return stack_compact_range_stats(st, seg.start, seg.end - 1,

reftable/stack.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct segment {
3535
uint64_t bytes;
3636
};
3737

38-
struct segment suggest_compaction_segment(uint64_t *sizes, size_t n);
38+
struct segment suggest_compaction_segment(uint64_t *sizes, size_t n,
39+
uint8_t factor);
3940

4041
#endif

reftable/stack_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ static void test_suggest_compaction_segment(void)
729729
{
730730
uint64_t sizes[] = { 512, 64, 17, 16, 9, 9, 9, 16, 2, 16 };
731731
struct segment min =
732-
suggest_compaction_segment(sizes, ARRAY_SIZE(sizes));
732+
suggest_compaction_segment(sizes, ARRAY_SIZE(sizes), 2);
733733
EXPECT(min.start == 1);
734734
EXPECT(min.end == 10);
735735
}
@@ -738,7 +738,7 @@ static void test_suggest_compaction_segment_nothing(void)
738738
{
739739
uint64_t sizes[] = { 64, 32, 16, 8, 4, 2 };
740740
struct segment result =
741-
suggest_compaction_segment(sizes, ARRAY_SIZE(sizes));
741+
suggest_compaction_segment(sizes, ARRAY_SIZE(sizes), 2);
742742
EXPECT(result.start == result.end);
743743
}
744744

0 commit comments

Comments
 (0)