Skip to content

Commit 7b121d9

Browse files
h-shafferaversecat
authored andcommitted
small btree balancing
While we are filling blocks the final block may not have enough items to properly fill that block. Here we add a check that stops filling the block if we have less than the minimum amount of items. Signed-off-by: Hunter Shaffer <hunter.shaffer@versity.com>
1 parent 200b286 commit 7b121d9

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

utils/src/parallel_restore.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,8 @@ static spr_err_t build_btree_block(struct scoutfs_parallel_restore_writer *wri,
10301030
unsigned long val_align;
10311031
unsigned long bytes;
10321032
unsigned long nr;
1033+
unsigned long min_items;
1034+
long item_bytes_after_block;
10331035
void *val_buf;
10341036
spr_err_t err;
10351037
u8 height;
@@ -1083,10 +1085,16 @@ static spr_err_t build_btree_block(struct scoutfs_parallel_restore_writer *wri,
10831085
for_each_bti_safe(&btb->items[level].root, bti, tmp) {
10841086
val_align = round_up(bti->val_len, SCOUTFS_BTREE_VALUE_ALIGN);
10851087
bytes = sizeof(struct scoutfs_btree_item) + val_align;
1088+
item_bytes_after_block = (le64_to_cpu(btb->total_items) * bytes) - le16_to_cpu(bt->mid_free_len);
1089+
min_items = (SCOUTFS_BLOCK_LG_SIZE - sizeof(struct scoutfs_btree_block)) / 4;
10861090

10871091
if (le16_to_cpu(bt->mid_free_len) < bytes)
10881092
break;
10891093

1094+
/* stop when there are not enough items to fill the next block */
1095+
if (item_bytes_after_block > 0 && item_bytes_after_block < min_items)
1096+
break;
1097+
10901098
item->node.height = UNLINKED_AVL_HEIGHT;
10911099
item->key = bti->key;
10921100
item->seq = cpu_to_le64(1);

0 commit comments

Comments
 (0)