Skip to content

Commit e926c26

Browse files
committed
internal/manifest: add heap maintaining virtual backing rewrite candidates
Add a rewriteCandidatesHeap which tracks the table backing with the lowest estimated amount of referenced data.
1 parent 2b27823 commit e926c26

File tree

9 files changed

+314
-54
lines changed

9 files changed

+314
-54
lines changed

internal/manifest/table_metadata.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,11 @@ type TableBacking struct {
322322
DiskFileNum base.DiskFileNum
323323
Size uint64
324324

325+
// ReferencedBlobValueSizeTotal is the the sum of the length of uncompressed
326+
// values in all blob files for which there exists a reference in the backing
327+
// table.
328+
ReferencedBlobValueSizeTotal uint64
329+
325330
// Reference count for the backing file, used to determine when a backing file
326331
// is obsolete and can be removed.
327332
//
@@ -474,9 +479,16 @@ func (m *TableMetadata) InitPhysicalBacking() {
474479
if m.TableBacking != nil {
475480
panic("backing already initialized")
476481
}
482+
483+
var blobValueSizeTotal uint64
484+
for i := range m.BlobReferences {
485+
blobValueSizeTotal += m.BlobReferences[i].BackingValueSize
486+
}
487+
477488
m.TableBacking = &TableBacking{
478-
DiskFileNum: base.PhysicalTableDiskFileNum(m.TableNum),
479-
Size: m.Size,
489+
DiskFileNum: base.PhysicalTableDiskFileNum(m.TableNum),
490+
Size: m.Size,
491+
ReferencedBlobValueSizeTotal: blobValueSizeTotal,
480492
}
481493
}
482494

internal/manifest/table_metadata_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func TestTableMetadataSize(t *testing.T) {
198198
structSize, tableMetadataSize)
199199
}
200200

201-
const tableBackingSize = 152
201+
const tableBackingSize = 160
202202
if structSize := unsafe.Sizeof(TableBacking{}); structSize != tableBackingSize {
203203
t.Errorf("TableBacking struct size (%d bytes) is not expected size (%d bytes)",
204204
structSize, tableBackingSize)
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Test rewrite candidates heap with blob value sizes.
2+
3+
# Add backings with values in blob files.
4+
add n=1 size=100 blobValueSize=50
5+
----
6+
1 virtual backings, total size 100:
7+
000001: size=100 refBlobValueSize=50 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
8+
unused virtual backings: 000001
9+
10+
add n=2 size=200 blobValueSize=100
11+
----
12+
2 virtual backings, total size 300:
13+
000001: size=100 refBlobValueSize=50 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
14+
000002: size=200 refBlobValueSize=100 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
15+
unused virtual backings: 000001 000002
16+
17+
add n=3 size=150 blobValueSize=75
18+
----
19+
3 virtual backings, total size 450:
20+
000001: size=100 refBlobValueSize=50 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
21+
000002: size=200 refBlobValueSize=100 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
22+
000003: size=150 refBlobValueSize=75 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
23+
unused virtual backings: 000001 000002 000003
24+
25+
add n=4 size=300 blobValueSize=150
26+
----
27+
4 virtual backings, total size 750:
28+
000001: size=100 refBlobValueSize=50 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
29+
000002: size=200 refBlobValueSize=100 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
30+
000003: size=150 refBlobValueSize=75 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
31+
000004: size=300 refBlobValueSize=150 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
32+
unused virtual backings: 000001 000002 000003 000004
33+
34+
add n=5 size=250 blobValueSize=125
35+
----
36+
5 virtual backings, total size 1000:
37+
000001: size=100 refBlobValueSize=50 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
38+
000002: size=200 refBlobValueSize=100 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
39+
000003: size=150 refBlobValueSize=75 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
40+
000004: size=300 refBlobValueSize=150 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
41+
000005: size=250 refBlobValueSize=125 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
42+
unused virtual backings: 000001 000002 000003 000004 000005
43+
44+
# Add tables to create rewrite candidates with varying percentages.
45+
add-table n=1 size=50 table=1
46+
----
47+
5 virtual backings, total size 1000:
48+
000001: size=100 refBlobValueSize=50 useCount=1 protectionCount=0 virtualizedSize=50 tables: [000001]
49+
000002: size=200 refBlobValueSize=100 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
50+
000003: size=150 refBlobValueSize=75 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
51+
000004: size=300 refBlobValueSize=150 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
52+
000005: size=250 refBlobValueSize=125 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
53+
rewrite candidates heap: 000001(33.3%)
54+
unused virtual backings: 000002 000003 000004 000005
55+
56+
add-table n=2 size=10 table=2
57+
----
58+
5 virtual backings, total size 1000:
59+
000001: size=100 refBlobValueSize=50 useCount=1 protectionCount=0 virtualizedSize=50 tables: [000001]
60+
000002: size=200 refBlobValueSize=100 useCount=1 protectionCount=0 virtualizedSize=10 tables: [000002]
61+
000003: size=150 refBlobValueSize=75 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
62+
000004: size=300 refBlobValueSize=150 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
63+
000005: size=250 refBlobValueSize=125 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
64+
rewrite candidates heap: 000002(3.3%) 000001(33.3%)
65+
unused virtual backings: 000003 000004 000005
66+
67+
add-table n=3 size=90 table=3
68+
----
69+
5 virtual backings, total size 1000:
70+
000001: size=100 refBlobValueSize=50 useCount=1 protectionCount=0 virtualizedSize=50 tables: [000001]
71+
000002: size=200 refBlobValueSize=100 useCount=1 protectionCount=0 virtualizedSize=10 tables: [000002]
72+
000003: size=150 refBlobValueSize=75 useCount=1 protectionCount=0 virtualizedSize=90 tables: [000003]
73+
000004: size=300 refBlobValueSize=150 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
74+
000005: size=250 refBlobValueSize=125 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
75+
rewrite candidates heap: 000002(3.3%) 000001(33.3%) 000003(40.0%)
76+
unused virtual backings: 000004 000005
77+
78+
add-table n=4 size=45 table=4
79+
----
80+
5 virtual backings, total size 1000:
81+
000001: size=100 refBlobValueSize=50 useCount=1 protectionCount=0 virtualizedSize=50 tables: [000001]
82+
000002: size=200 refBlobValueSize=100 useCount=1 protectionCount=0 virtualizedSize=10 tables: [000002]
83+
000003: size=150 refBlobValueSize=75 useCount=1 protectionCount=0 virtualizedSize=90 tables: [000003]
84+
000004: size=300 refBlobValueSize=150 useCount=1 protectionCount=0 virtualizedSize=45 tables: [000004]
85+
000005: size=250 refBlobValueSize=125 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
86+
rewrite candidates heap: 000002(3.3%) 000004(10.0%) 000003(40.0%) 000001(33.3%)
87+
unused virtual backings: 000005
88+
89+
add-table n=5 size=100 table=5
90+
----
91+
5 virtual backings, total size 1000:
92+
000001: size=100 refBlobValueSize=50 useCount=1 protectionCount=0 virtualizedSize=50 tables: [000001]
93+
000002: size=200 refBlobValueSize=100 useCount=1 protectionCount=0 virtualizedSize=10 tables: [000002]
94+
000003: size=150 refBlobValueSize=75 useCount=1 protectionCount=0 virtualizedSize=90 tables: [000003]
95+
000004: size=300 refBlobValueSize=150 useCount=1 protectionCount=0 virtualizedSize=45 tables: [000004]
96+
000005: size=250 refBlobValueSize=125 useCount=1 protectionCount=0 virtualizedSize=100 tables: [000005]
97+
rewrite candidates heap: 000002(3.3%) 000004(10.0%) 000003(40.0%) 000001(33.3%) 000005(26.7%)
98+
99+
add-table n=2 size=80 table=6
100+
----
101+
5 virtual backings, total size 1000:
102+
000001: size=100 refBlobValueSize=50 useCount=1 protectionCount=0 virtualizedSize=50 tables: [000001]
103+
000002: size=200 refBlobValueSize=100 useCount=2 protectionCount=0 virtualizedSize=90 tables: [000002 000006]
104+
000003: size=150 refBlobValueSize=75 useCount=1 protectionCount=0 virtualizedSize=90 tables: [000003]
105+
000004: size=300 refBlobValueSize=150 useCount=1 protectionCount=0 virtualizedSize=45 tables: [000004]
106+
000005: size=250 refBlobValueSize=125 useCount=1 protectionCount=0 virtualizedSize=100 tables: [000005]
107+
rewrite candidates heap: 000004(10.0%) 000005(26.7%) 000003(40.0%) 000001(33.3%) 000002(30.0%)
108+
109+
# Remove some tables to demonstrate heap updates.
110+
remove-table n=5 table=5
111+
----
112+
5 virtual backings, total size 1000:
113+
000001: size=100 refBlobValueSize=50 useCount=1 protectionCount=0 virtualizedSize=50 tables: [000001]
114+
000002: size=200 refBlobValueSize=100 useCount=2 protectionCount=0 virtualizedSize=90 tables: [000002 000006]
115+
000003: size=150 refBlobValueSize=75 useCount=1 protectionCount=0 virtualizedSize=90 tables: [000003]
116+
000004: size=300 refBlobValueSize=150 useCount=1 protectionCount=0 virtualizedSize=45 tables: [000004]
117+
000005: size=250 refBlobValueSize=125 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
118+
rewrite candidates heap: 000004(10.0%) 000002(30.0%) 000003(40.0%) 000001(33.3%)
119+
unused virtual backings: 000005
120+
121+
# Remove backing 4's table. Should remove from heap since virtualizedSize=0.
122+
remove-table n=4 table=4
123+
----
124+
5 virtual backings, total size 1000:
125+
000001: size=100 refBlobValueSize=50 useCount=1 protectionCount=0 virtualizedSize=50 tables: [000001]
126+
000002: size=200 refBlobValueSize=100 useCount=2 protectionCount=0 virtualizedSize=90 tables: [000002 000006]
127+
000003: size=150 refBlobValueSize=75 useCount=1 protectionCount=0 virtualizedSize=90 tables: [000003]
128+
000004: size=300 refBlobValueSize=150 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
129+
000005: size=250 refBlobValueSize=125 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
130+
rewrite candidates heap: 000002(30.0%) 000001(33.3%) 000003(40.0%)
131+
unused virtual backings: 000004 000005

internal/manifest/testdata/virtual_backings/virtual_backings

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
add n=1 size=100
33
----
44
1 virtual backings, total size 100:
5-
000001: size=100 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
5+
000001: size=100 refBlobValueSize=0 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
66
unused virtual backings: 000001
77

88
add n=2 size=200
99
----
1010
2 virtual backings, total size 300:
11-
000001: size=100 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
12-
000002: size=200 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
11+
000001: size=100 refBlobValueSize=0 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
12+
000002: size=200 refBlobValueSize=0 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
1313
unused virtual backings: 000001 000002
1414

1515
add n=2 size=200
@@ -19,8 +19,9 @@ pebble: trying to add an existing file backing
1919
add-table n=1 size=10 table=1
2020
----
2121
2 virtual backings, total size 300:
22-
000001: size=100 useCount=1 protectionCount=0 virtualizedSize=10 tables: [000001]
23-
000002: size=200 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
22+
000001: size=100 refBlobValueSize=0 useCount=1 protectionCount=0 virtualizedSize=10 tables: [000001]
23+
000002: size=200 refBlobValueSize=0 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
24+
rewrite candidates heap: 000001(10.0%)
2425
unused virtual backings: 000002
2526

2627
remove-table n=2 table=1
@@ -31,15 +32,17 @@ table 000001 does not use backing 000002
3132
add-table n=1 size=10 table=3
3233
----
3334
2 virtual backings, total size 300:
34-
000001: size=100 useCount=2 protectionCount=0 virtualizedSize=20 tables: [000001 000003]
35-
000002: size=200 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
35+
000001: size=100 refBlobValueSize=0 useCount=2 protectionCount=0 virtualizedSize=20 tables: [000001 000003]
36+
000002: size=200 refBlobValueSize=0 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
37+
rewrite candidates heap: 000001(20.0%)
3638
unused virtual backings: 000002
3739

3840
add-table n=1 size=10 table=5
3941
----
4042
2 virtual backings, total size 300:
41-
000001: size=100 useCount=3 protectionCount=0 virtualizedSize=30 tables: [000001 000003 000005]
42-
000002: size=200 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
43+
000001: size=100 refBlobValueSize=0 useCount=3 protectionCount=0 virtualizedSize=30 tables: [000001 000003 000005]
44+
000002: size=200 refBlobValueSize=0 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
45+
rewrite candidates heap: 000001(30.0%)
4346
unused virtual backings: 000002
4447

4548
add-table n=1 size=10 table=5
@@ -54,44 +57,46 @@ backing 000001 still in use (useCount=3 protectionCount=0)
5457
remove-table n=1 table=1
5558
----
5659
2 virtual backings, total size 300:
57-
000001: size=100 useCount=2 protectionCount=0 virtualizedSize=20 tables: [000003 000005]
58-
000002: size=200 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
60+
000001: size=100 refBlobValueSize=0 useCount=2 protectionCount=0 virtualizedSize=20 tables: [000003 000005]
61+
000002: size=200 refBlobValueSize=0 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
62+
rewrite candidates heap: 000001(20.0%)
5963
unused virtual backings: 000002
6064

6165
remove-table n=1 table=3
6266
----
6367
2 virtual backings, total size 300:
64-
000001: size=100 useCount=1 protectionCount=0 virtualizedSize=10 tables: [000005]
65-
000002: size=200 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
68+
000001: size=100 refBlobValueSize=0 useCount=1 protectionCount=0 virtualizedSize=10 tables: [000005]
69+
000002: size=200 refBlobValueSize=0 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
70+
rewrite candidates heap: 000001(10.0%)
6671
unused virtual backings: 000002
6772

6873
remove-table n=1 table=5
6974
----
7075
2 virtual backings, total size 300:
71-
000001: size=100 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
72-
000002: size=200 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
76+
000001: size=100 refBlobValueSize=0 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
77+
000002: size=200 refBlobValueSize=0 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
7378
unused virtual backings: 000001 000002
7479

7580
remove n=1
7681
----
7782
1 virtual backings, total size 200:
78-
000002: size=200 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
83+
000002: size=200 refBlobValueSize=0 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
7984
unused virtual backings: 000002
8085

8186
protect n=2
8287
----
8388
1 virtual backings, total size 200:
84-
000002: size=200 useCount=0 protectionCount=1 virtualizedSize=0 tables: []
89+
000002: size=200 refBlobValueSize=0 useCount=0 protectionCount=1 virtualizedSize=0 tables: []
8590

8691
protect n=2
8792
----
8893
1 virtual backings, total size 200:
89-
000002: size=200 useCount=0 protectionCount=2 virtualizedSize=0 tables: []
94+
000002: size=200 refBlobValueSize=0 useCount=0 protectionCount=2 virtualizedSize=0 tables: []
9095

9196
unprotect n=2
9297
----
9398
1 virtual backings, total size 200:
94-
000002: size=200 useCount=0 protectionCount=1 virtualizedSize=0 tables: []
99+
000002: size=200 refBlobValueSize=0 useCount=0 protectionCount=1 virtualizedSize=0 tables: []
95100

96101
remove n=2
97102
----
@@ -100,36 +105,40 @@ backing 000002 still in use (useCount=0 protectionCount=1)
100105
unprotect n=2
101106
----
102107
1 virtual backings, total size 200:
103-
000002: size=200 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
108+
000002: size=200 refBlobValueSize=0 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
104109
unused virtual backings: 000002
105110

106111
add-table n=2 size=10 table=2
107112
----
108113
1 virtual backings, total size 200:
109-
000002: size=200 useCount=1 protectionCount=0 virtualizedSize=10 tables: [000002]
114+
000002: size=200 refBlobValueSize=0 useCount=1 protectionCount=0 virtualizedSize=10 tables: [000002]
115+
rewrite candidates heap: 000002(5.0%)
110116

111117
add-table n=2 size=10 table=4
112118
----
113119
1 virtual backings, total size 200:
114-
000002: size=200 useCount=2 protectionCount=0 virtualizedSize=20 tables: [000002 000004]
120+
000002: size=200 refBlobValueSize=0 useCount=2 protectionCount=0 virtualizedSize=20 tables: [000002 000004]
121+
rewrite candidates heap: 000002(10.0%)
115122

116123
protect n=2
117124
----
118125
1 virtual backings, total size 200:
119-
000002: size=200 useCount=2 protectionCount=1 virtualizedSize=20 tables: [000002 000004]
126+
000002: size=200 refBlobValueSize=0 useCount=2 protectionCount=1 virtualizedSize=20 tables: [000002 000004]
127+
rewrite candidates heap: 000002(10.0%)
120128

121129
remove-table n=2 size=10 table=4
122130
----
123131
1 virtual backings, total size 200:
124-
000002: size=200 useCount=1 protectionCount=1 virtualizedSize=10 tables: [000002]
132+
000002: size=200 refBlobValueSize=0 useCount=1 protectionCount=1 virtualizedSize=10 tables: [000002]
133+
rewrite candidates heap: 000002(5.0%)
125134

126135
remove-table n=2 size=10 table=2
127136
----
128137
1 virtual backings, total size 200:
129-
000002: size=200 useCount=0 protectionCount=1 virtualizedSize=0 tables: []
138+
000002: size=200 refBlobValueSize=0 useCount=0 protectionCount=1 virtualizedSize=0 tables: []
130139

131140
unprotect n=2
132141
----
133142
1 virtual backings, total size 200:
134-
000002: size=200 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
143+
000002: size=200 refBlobValueSize=0 useCount=0 protectionCount=0 virtualizedSize=0 tables: []
135144
unused virtual backings: 000002

internal/manifest/version_edit.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,15 @@ func (b *BulkVersionEdit) Accumulate(ve *VersionEdit) error {
12401240
if backing == nil {
12411241
return errors.Errorf("TableBacking for virtual sstable must not be nil")
12421242
}
1243+
// Currently, we give virtual tables all the blob references of their
1244+
// backing table. To populate the total referenced blob value size, we
1245+
// sum up the backing value sizes from one of the virtual table's
1246+
// blob references.
1247+
if backing.ReferencedBlobValueSizeTotal == 0 {
1248+
for _, br := range nf.Meta.BlobReferences {
1249+
backing.ReferencedBlobValueSizeTotal += br.BackingValueSize
1250+
}
1251+
}
12431252
nf.Meta.AttachVirtualBacking(backing)
12441253
} else if nf.Meta.TableBacking == nil {
12451254
return errors.Errorf("Added file L%d.%s's has no TableBacking", nf.Level, nf.Meta.TableNum)

internal/manifest/version_edit_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestVERoundTripAndAccumulate(t *testing.T) {
6060
SyntheticPrefixAndSuffix: sstable.MakeSyntheticPrefixAndSuffix([]byte("after"), []byte("foo")),
6161
BlobReferences: []BlobReference{
6262
{FileID: 900, ValueSize: 1024, BackingValueSize: 1024},
63-
{FileID: 910, ValueSize: 1024, BackingValueSize: 8090},
63+
{FileID: 910, ValueSize: 8090, BackingValueSize: 8090},
6464
},
6565
}).ExtendPointKeyBounds(
6666
cmp,

0 commit comments

Comments
 (0)