Assignment 3. Implementing Augmented B+tree - wiki
2021024057 ๊น๋ณ์ค
1. Design
1.1. Overall Architecture
ํด๋น ๊ณผ์ ์์๋ Disk-based B+tree๋ฅผ ๊ตฌํํ์๋ค. ๋ชจ๋ ๋
ธ๋๋ 4KB ํฌ๊ธฐ์ page ๋จ์๋ก ๋์คํฌ์ ์ ์ฅ๋๋ฉฐ, Header Page, Free Page, Internal Page, Leaf Page์ ๋ค ๊ฐ์ง ํ์
์ ๊ฐ์ง๋ค.
- In-Memory Logic vs On-Disk Structure: ๋ฉ๋ชจ๋ฆฌ ์์ ํฌ์ธํฐ ๋์ ํ์ผ ๋ด์
offset์ ์ฌ์ฉํ์ฌ ํ์ด์ง ๊ฐ์ ์ฐ๊ฒฐ(Link)์ ๊ตฌํํ์๋ค.open_tableํธ์ถ ์fd(File Descriptor)๋ฅผ ์ ์ญ์ผ๋ก ๊ด๋ฆฌํ๋ฉฐ,pread/pwritesystem call์ ์ฌ์ฉํ์ฌ ํ์ด์ง ๋จ์์ I/O๋ฅผ ์ํํ๋ค.
1.2. Data Structures
- Page Structure:
4096 Bytes๊ณ ์ ํฌ๊ธฐ. Header(128B)์ Body(Record/Internal Entry)๋ก ๊ตฌ์ฑ๋๋ค. - Leaf Page: Key์
120Bํฌ๊ธฐ์ Value๋ฅผ ์ ์ฅํ๋record๋ฐฐ์ด์ ๊ฐ์ง๋ค. Order(Branching Factor)๋ 32์ด๋ค. - Internal Page: Key์ ์์ ํ์ด์ง์ Offset์ ์ ์ฅํ๋
inter_record๋ฐฐ์ด์ ๊ฐ์ง๋ค. Order๋ 249์ด๋ค. - Bitmap for Logical Deletion (
bptree2):bptree2์์๋ ๋ฌผ๋ฆฌ์ ์ญ์ ๋์ ๋ ผ๋ฆฌ์ ์ญ์ ๋ฅผ ์ง์ํ๊ธฐ ์ํด,page๊ตฌ์กฐ์ฒด์reserved[104]์์ญ์ Bitmap์ผ๋ก ํ์ฉํ์๋ค.reserved[i] == 1์ธ ๊ฒฝ์ฐ ํด๋น ์ธ๋ฑ์ค์ ๋ ์ฝ๋๋ ์ญ์ ๋ ๊ฒ์ผ๋ก ๊ฐ์ฃผํ๋ค.
2. Implement
2.1. Normal B+ tree (bptree1)
๊ธฐ๋ณธ์ ์ธ B+ tree์ ์ฝ์ , ์ญ์ , ํ์ ์ฐ์ฐ์ ๊ตฌํํ์๋ค.
Insertion & Splitting: Leaf Page๊ฐ ๊ฐ๋ ์ฐฌ ์ํ(
LEAF_MAX = 31)์์ ์ฝ์ ์insert_into_leaf_after_splitting์ด ํธ์ถ๋๋ค. ์์ ๋ฐฐ์ด์ ์์ฑํ์ฌ ๊ธฐ์กด ๋ ์ฝ๋์ ์ ๋ ์ฝ๋๋ฅผ ์ ๋ ฌํ ํ, ์ค๊ฐ ์ง์ (cut)์ ๊ธฐ์ค์ผ๋ก ๋ ๊ฐ์ ํ์ด์ง๋ก ๋ถํ ํ๋ค. ์ดํ ์ ํ์ด์ง์ ์ฒซ ๋ฒ์งธ Key๋ฅผ ๋ถ๋ชจ๋ก ์ฌ๋ฆฌ๋(insert_into_parent) ์ฌ๊ท์ logic์ ๊ตฌํํ์๋ค.Deletion & Merging/Redistribution:
db_delete๋ ๋ ์ฝ๋๋ฅผ ๋ฌผ๋ฆฌ์ ์ผ๋ก ์ ๊ฑฐ(memmove)ํ๋ค. ์ญ์ ํ Key์ ๊ฐ์๊ฐ ์ต์ ์กฐ๊ฑด ๋ฏธ๋ง(Underflow)์ด ๋๋ฉดget_neighbor_index๋ฅผ ํตํด ํ์ ๋ ธ๋๋ฅผ ํ์ํ๋ค.- Redistribution: ํ์ ๋ ธ๋์ ์ฌ์ ๊ฐ ์๋ค๋ฉด Key๋ฅผ ํ๋ ๋น๋ ค์ ๊ท ํ์ ๋ง์ถ๋ค.
- Coalesce (Merge): ํ์ ๋
ธ๋๋ ์ฌ์ ๊ฐ ์๋ค๋ฉด ๋ ๋
ธ๋๋ฅผ ๋ณํฉํ๊ณ , ๋ถ๋ชจ ๋
ธ๋์์ ํด๋น ํฌ์ธํฐ๋ฅผ ์ ๊ฑฐ(
delete_entry)ํ๋ ๊ณผ์ ์ ์ฌ๊ท์ ์ผ๋ก ์ํํ๋ค.
2.2. Logical Deletion Applied B+ tree (bptree2)
bptree1์ code๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ๋, ์ญ์ ์ ์ฑ
๊ณผ ๊ตฌ์กฐ๋ฅผ ๋ณ๊ฒฝํ์๋ค.
Logical Deletion (
db_delete)- ๋ฌผ๋ฆฌ์ ์ธ ๋ฐ์ดํฐ ์ด๋์ด๋ ๋ณํฉ(Merge) ๊ณผ์ ์ ์ ๊ฑฐํ์๋ค.
- ๋์ ํด๋น ๋ ์ฝ๋์ ์ธ๋ฑ์ค
i์ ๋ํดpage->reserved[i] = 1๋ก ๋งํนํ๊ณ ํ์ด์ง๋ฅผ ๋์คํฌ์ ์ ์ฅํ๋ค. - ์ด๋ก ์ธํด ์ญ์ ์ฐ์ฐ์ ๋น์ฉ์ด ๋ก ํฌ๊ฒ ๊ฐ์ํ์๋ค.
Revival on Insertion (
db_insert)- ์ด๋ฏธ ์กด์ฌํ๋ Key์ ๋ํ ์ฝ์
์์ฒญ์ด ๋ค์ด์์ ๋, ํด๋น Key๊ฐ ๋
ผ๋ฆฌ์ ์ผ๋ก ์ญ์ ๋ ์ํ(
reserved == 1)๋ผ๋ฉด ์๋ก์ด ๋ ์ฝ๋๋ฅผ ์ถ๊ฐํ๋ ๋์ ๊ธฐ์กด ์์น์ Value๋ฅผ ๊ฐฑ์ ํ๊ณreserved = 0์ผ๋ก ๋ณ๊ฒฝํ์ฌ ๋ฐ์ดํฐ๋ฅผ '๋ถํ'์ํจ๋ค.
- ์ด๋ฏธ ์กด์ฌํ๋ Key์ ๋ํ ์ฝ์
์์ฒญ์ด ๋ค์ด์์ ๋, ํด๋น Key๊ฐ ๋
ผ๋ฆฌ์ ์ผ๋ก ์ญ์ ๋ ์ํ(
Visibility Check (
db_find)- ํ์ ์ Key๊ฐ ์กด์ฌํ๋๋ผ๋
reserved == 1์ด๋ผ๋ฉดNULL์ ๋ฐํํ์ฌ ์ฌ์ฉ์์๊ฒ๋ ์ญ์ ๋ ๊ฒ์ฒ๋ผ ๋ณด์ด๋๋ก ๊ตฌํํ์๋ค.
- ํ์ ์ Key๊ฐ ์กด์ฌํ๋๋ผ๋
3. Result
3.1. Test Environment
- OS: Ubuntu 24.04 LTS
- Compiler: gcc
- Tools: Make
3.2. bptree1 Execution
- Insert & Find: Key
100,50,150์ ์์๋๋ก ์ฝ์ ํ ์กฐํ ์ ์ ๋ ฌ๋์ด ์ถ๋ ฅ๋จ์ ํ์ธํ์๋ค. - Split: 32๊ฐ ์ด์์ ๋ ์ฝ๋ ์ฝ์ ์ Leaf Page๊ฐ ๋ถํ ๋๊ณ , ์๋ก์ด Root๊ฐ ์์ฑ๋์ด ํธ๋ฆฌ์ ๋์ด๊ฐ ์ฆ๊ฐํจ์ ํ์ธํ์๋ค.
- Delete (Merge): ๋๋ ์ญ์ ์ํ ์ Page Merge๊ฐ ๋ฐ์ํ๊ณ , Root๊ฐ ๋ค์ Leaf๋ก ๋ด๋ ค์ค๊ฑฐ๋ ๋ณ๊ฒฝ๋๋ ๊ณผ์ ์ ํ์ธํ์๋ค.
kmbzn@p31:~/workspace/Assignment3_2021024057/bptree1$ rm -f *.db
kmbzn@p31:~/workspace/Assignment3_2021024057/bptree1$ ./main
i 1 A
i 2 B
i 3 C
i 4 D
i 5 E
i 6 F
i 7 G
i 8 H
i 9 I
i 10 J
i 11 K
i 12 L
i 13 M
i 14 N
i 15 O
i 16 P
i 17 Q
i 18 R
i 19 S
i 20 T
i 21 U
i 22 V
i 23 W
i 24 X
i 25 Y
i 26 Z
i 27 AA
i 28 BB
i 29 CC
i 30 DD
i 31 EE
i 32 FF
f 1
Key: 1, Value: A
f 32
Key: 32, Value: FF
d 1
d 2
d 3
d 4
d 5
d 6
d 7
d 8
d 9
d 10
d 11
d 12
d 13
d 14
d 15
d 16
d 17
f 17
Not Exists
f 18
Key: 18, Value: R
q
3.3. bptree2 Execution (Logical Deletion)
- Logical Delete:
d 100์ํ ํf 100์ "Not Exists" ์ถ๋ ฅ ํ์ธํ์๋ค. ํ์ง๋ง ๋ฌผ๋ฆฌ์ file ํฌ๊ธฐ๋ ์ค์ด๋ค์ง ์๋๋ค. - Revival: ์ญ์ ๋ Key 100์ ๋ํด
i 100 world์ํ ์, ์๋ก์ด ๊ณต๊ฐ์ ํ ๋นํ์ง ์๊ณ ๊ธฐ์กด ์ฌ๋กฏ์ ์ฌํ์ฉํ์ฌ ๊ฐ์ด ๊ฐฑ์ ๋จ์ ํ์ธํ์๋ค. - Reorganization:
q๋ฅผ ๋๋ฌ ์ข ๋ฃ ์db_reorganize๊ฐ ํธ์ถ๋์ด, ์ญ์ ๋ ๋ฐ์ดํฐ๊ฐ ์ ๋ฆฌ๋ ์ํ๋ก DB ํ์ผ์ด ๊ฐฑ์ ๋จ์ ํ์ธํ์๋ค.
kmbzn@p31:~/workspace/Assignment3_2021024057/bptree2$ rm -f *.db
kmbzn@p31:~/workspace/Assignment3_2021024057/bptree2$ ./main
i 100 hello
d 100
f 100
Not Exists
i 100 world
f 100
Key: 100, Value: world
q
4. TroubleShooting
4.1. get_neighbor_index Segmentation Fault
- ๋ฌธ์ :
bptree1์ ์ญ์ ์ฐ์ฐ ์ค, Internal Page๊ฐ ๋ณํฉ๋์ด ๋น ํ์ด์ง(num_of_keys == 0)๊ฐ ๋ ์ํ์์ ๋ถ๋ชจ๋ฅผ ํ์ํ ๋parent->b_f[0]์ ์ ๊ทผํ์ฌ Segmentation Fault๊ฐ ๋ฐ์ํ์๋ค. - ์์ธ:
num_of_keys๊ฐ 0์ธ ๊ฒฝ์ฐb_f๋ฐฐ์ด์ด ์ ํจํ์ง ์์์๋ ์ ๊ทผ์ ์๋ํ์๋ค. - ํด๊ฒฐ:
parent->num_of_keys > 0์กฐ๊ฑด์ ์ถ๊ฐํ์ฌ, ํค๊ฐ ์์ ๋๋งb_f[0]๋ฅผ ํ์ธํ๋๋ก ๋ก์ง์ ์์ ํ์ฌ ํด๊ฒฐํ์๋ค.
4.2. db_reorganize Trace Trap Error
- ๋ฌธ์ :
bptree2์์ ์ฌ๊ตฌ์ฑ(Reorganize) ์คํ ์ ํ๋ก๊ทธ๋จ์ด ๋น์ ์ ์ข ๋ฃ(Trace Trap)๋์๋ค. - ์์ธ:
open_tableํจ์ ๋ด๋ถ์์ ์ ์ญ ๋ณ์DB_PATH๋ฅผmemset์ผ๋ก ์ด๊ธฐํํ๋๋ฐdb_reorganize์์open_table(DB_PATH)ํํ๋ก ์๊ธฐ ์์ ์ argument๋ก ๋๊ธฐ๋ฉด์ ํฌ์ธํฐ ์ฐธ์กฐ ์ค๋ฅ๊ฐ ๋ฐ์ํ์๋ค. - ํด๊ฒฐ:
db_reorganizeํจ์ ์ด์ ์์DB_PATH๋ฅผ ๋ก์ปฌ ๋ณ์original_path์strncpy๋ก ๋ณต์ฌํด๋ ๋ค ์ด ๋ณต์ฌ๋ณธ์ ์ฌ์ฉํ์ฌopen_table์ ํธ์ถํ๋๋ก ์์ ํ์๋ค.
5. Consideration on db_reorganize
5.1. ๊ตฌํ ๋ชฉํ
db_reorganize๋ ๋
ผ๋ฆฌ์ ์ผ๋ก ์ญ์ ๋ ๋ ์ฝ๋(Fragmentation)๋ฅผ ์ ๊ฑฐํ๊ณ , B+ tree๋ฅผ ๋ฌผ๋ฆฌ์ ์ผ๋ก ์ฌ๊ตฌ์ฑํ์ฌ ๊ฒ์ ์ฑ๋ฅ๊ณผ ๊ณต๊ฐ ํจ์จ์ฑ์ ์ต์ ํํ๋ ๊ฒ์ ๋ชฉํ๋ก ํ๋ค.
5.2. ์ฑํํ ์๊ณ ๋ฆฌ์ฆ
๊ธฐ์กด ํ์ผ ๋ด์์ ๋น ๊ณต๊ฐ์ ์ฐพ์ ๋ฐ์ดํฐ๋ฅผ ์ด๋์ํค๋ ๋ฐฉ์(In-place compaction) ๋์ , ์๋ก์ด DB ํ์ผ์ ์์ฑํ์ฌ ์ ํจํ ๋ฐ์ดํฐ๋ง migrate์ํค๋ ๋ฐฉ์์ ์ฌ์ฉํ๊ธฐ๋ก ๊ฒฐ์ ํ์๋ค.
- Rename: ํ์ฌ ์ฌ์ฉ ์ค์ธ DB ํ์ผ์
backup.db๋ก ์ด๋ฆ์ ๋ณ๊ฒฝํ๋ค. - Create New: ์๋ ์ด๋ฆ์ผ๋ก ์๋ก์ด ๋น DB ํ์ผ์ ์์ฑํ๋ค (
open_table). - Migrate:
backup.db๋ฅผ Read-only๋ก ์ด๊ณ , Root๋ถํฐ ์์ํ์ฌ ๋ชจ๋ Leaf Page๋ฅผ ์ํํ๋ค.- Leaf Page ๋ด์์
reserved == 0์ธ(์ญ์ ๋์ง ์์) ์ ํจํ ๋ ์ฝ๋๋ง ์ถ์ถํ๋ค. - ์ถ์ถ๋ ๋ ์ฝ๋๋ฅผ ์ DB์
db_insertํ๋ค.
- Cleanup: ์์
์๋ฃ ํ
backup.db๋ฅผ ์ญ์ (unlink)ํ๋ค.
5.3. ์ฑ๋ฅ์์ ์ด์
- ์ด ๋ฐฉ์์ด ๊ฐ๋ ์ฅ์ ๋ค
- Sequential I/O: ๊ธฐ์กด ํธ๋ฆฌ๋ฅผ ์์ฐจ์ ์ผ๋ก ์ฝ๊ณ (Sequential Read), ์ ํธ๋ฆฌ์ ์์ฐจ์ ์ผ๋ก ์(Append-only Write)์ผ๋ก์จ ๋์คํฌ ํค๋ ์ด๋์ ์ต์ํํ ์ ์๋ค.
- Fragmentation ์ ๊ฑฐ: ์๋ก ์์ฑ๋ ํธ๋ฆฌ๋ ์ค๊ฐ์ ๋น ๊ณต๊ฐ ์์ด ๊ฝ ์ฑ์์ง๋ฏ๋ก(Compacted), ๋์คํฌ ๊ณต๊ฐ ๋ญ๋น๊ฐ 0์ ์๋ ดํ๋ค๊ณ ๋ณผ ์ ์๋ค.
- ์์ ์ฑ: ์์
๋์ค ์คํจํ๋๋ผ๋ ์๋ณธ(
backup.db)์ด ๋ณด์กด๋ ์ํ์ด๋ฏ๋ก ๋ณต๊ตฌ๊ฐ ์ฉ์ดํ๋ค.

