Skip to content

Commit 6c81480

Browse files
committed
sstable: remove GetScaledProperties
We no longer use this "bulk" scaling of properties.
1 parent 8875a68 commit 6c81480

File tree

10 files changed

+185
-220
lines changed

10 files changed

+185
-220
lines changed

data_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"time"
2020
"unicode"
2121

22+
"github.com/cockroachdb/crlib/crhumanize"
2223
"github.com/cockroachdb/crlib/crstrings"
2324
"github.com/cockroachdb/datadriven"
2425
"github.com/cockroachdb/errors"
@@ -1378,22 +1379,22 @@ func runSSTablePropertiesCmd(t *testing.T, td *datadriven.TestData, d *DB) strin
13781379
}
13791380
defer r.Close()
13801381

1381-
loadedProps, err := r.ReadPropertiesBlock(context.Background(), nil /* buffer pool */)
1382+
props, err := r.ReadPropertiesBlock(context.Background(), nil /* buffer pool */)
13821383
if err != nil {
13831384
return err.Error()
13841385
}
1385-
props := loadedProps.String()
1386-
env := sstable.ReadEnv{}
1386+
var buf bytes.Buffer
1387+
13871388
if m != nil && m.Virtual {
1388-
env.Virtual = m.VirtualParams
1389-
scaledProps := loadedProps.GetScaledProperties(m.TableBacking.Size, m.Size)
1390-
props = scaledProps.String()
1389+
fmt.Fprintf(&buf, "virtual table (estimated size %s / backing size %s)\n",
1390+
crhumanize.Bytes(m.Size), crhumanize.Bytes(m.TableBacking.Size),
1391+
)
13911392
}
13921393
if len(td.Input) == 0 {
1393-
return props
1394+
buf.WriteString(props.String())
1395+
return buf.String()
13941396
}
1395-
var buf bytes.Buffer
1396-
propsSlice := strings.Split(props, "\n")
1397+
propsSlice := strings.Split(props.String(), "\n")
13971398
for _, requestedProp := range strings.Split(td.Input, "\n") {
13981399
fmt.Fprintf(&buf, "%s:\n", requestedProp)
13991400
for _, prop := range propsSlice {

db.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,6 +2278,9 @@ type SSTableInfo struct {
22782278
// BackingSSTNum is the disk file number associated with the backing sstable.
22792279
// If Virtual is false, BackingSSTNum == PhysicalTableDiskFileNum(TableNum).
22802280
BackingSSTNum base.DiskFileNum
2281+
// BackingSize is the size of the backing sstable. This is the same with
2282+
// TableInfo.Size when the table is not virtual.
2283+
BackingSize uint64
22812284
// BackingType is the type of storage backing this sstable.
22822285
BackingType BackingType
22832286
// Locator is the remote.Locator backing this sstable, if the backing type is
@@ -2345,14 +2348,11 @@ func (d *DB) SSTables(opts ...SSTablesOption) ([][]SSTableInfo, error) {
23452348
if err != nil {
23462349
return nil, err
23472350
}
2348-
if m.Virtual {
2349-
commonProps := p.GetScaledProperties(m.TableBacking.Size, m.Size)
2350-
p = &sstable.Properties{CommonProperties: commonProps}
2351-
}
23522351
destTables[j].Properties = p
23532352
}
23542353
destTables[j].Virtual = m.Virtual
23552354
destTables[j].BackingSSTNum = m.TableBacking.DiskFileNum
2355+
destTables[j].BackingSize = m.TableBacking.Size
23562356
objMeta, err := d.objProvider.Lookup(base.FileTypeTable, m.TableBacking.DiskFileNum)
23572357
if err != nil {
23582358
return nil, err

db_test.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,7 +1438,11 @@ func TestVirtualSSTables(t *testing.T) {
14381438
// Get the original properties.
14391439
tableInfos, err := d.SSTables(WithProperties())
14401440
require.NoError(t, err)
1441-
originalProps := tableInfos[0][0].Properties
1441+
for _, levelTables := range tableInfos {
1442+
for _, info := range levelTables {
1443+
require.NotNil(t, info.Properties)
1444+
}
1445+
}
14421446

14431447
// Excise to create virtual ssts.
14441448
exciseSpan := KeyRange{
@@ -1450,20 +1454,14 @@ func TestVirtualSSTables(t *testing.T) {
14501454

14511455
tableInfos, err = d.SSTables(WithProperties())
14521456
require.NoError(t, err)
1453-
1454-
// Find the virtual sstables and ensure that some of its common properties
1455-
// have been scaled down correctly.
1457+
sawVirtual := false
14561458
for _, levelTables := range tableInfos {
14571459
for _, info := range levelTables {
1458-
if info.Virtual {
1459-
virtualProps := info.Properties
1460-
require.NotNil(t, virtualProps)
1461-
require.Less(t, virtualProps.NumEntries, originalProps.NumEntries)
1462-
require.Less(t, virtualProps.RawKeySize, originalProps.RawKeySize)
1463-
require.Less(t, virtualProps.RawValueSize, originalProps.RawValueSize)
1464-
}
1460+
sawVirtual = sawVirtual || info.Virtual
1461+
require.NotNil(t, info.Properties)
14651462
}
14661463
}
1464+
require.True(t, sawVirtual)
14671465
}
14681466

14691467
type testTracer struct {

internal/manifest/table_metadata.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,9 @@ func (ikr *InternalKeyBounds) SetLargest(ik InternalKey) {
12391239
type TableInfo struct {
12401240
// FileNum is the internal DB identifier for the table.
12411241
FileNum base.FileNum
1242-
// Size is the size of the file in bytes.
1242+
// Size is the size of the table in bytes. If the table is physical, this is
1243+
// the same with the backing size. If the table is virtual, this is an
1244+
// estimation of how much data the virtual table references.
12431245
Size uint64
12441246
// Smallest is the smallest internal key in the table.
12451247
Smallest InternalKey

internal/manifest/table_metadata_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,49 @@ func TestTableMetadata_ParseRoundTrip(t *testing.T) {
138138
}
139139
}
140140

141+
func TestTableMetadata_ScaleStatistic(t *testing.T) {
142+
var meta TableMetadata
143+
meta.TableBacking = &TableBacking{}
144+
meta.Virtual = true
145+
146+
meta.Size = 50
147+
meta.TableBacking.Size = 100
148+
require.Equal(t, uint64(0), meta.ScaleStatistic(0))
149+
require.Equal(t, uint64(1), meta.ScaleStatistic(1))
150+
require.Equal(t, uint64(1), meta.ScaleStatistic(2))
151+
require.Equal(t, uint64(2), meta.ScaleStatistic(3))
152+
require.Equal(t, uint64(50_000), meta.ScaleStatistic(100_000))
153+
154+
t.Run("invalid sizes", func(t *testing.T) {
155+
meta.Size = 0
156+
meta.TableBacking.Size = 100
157+
require.Equal(t, uint64(0), meta.ScaleStatistic(0))
158+
require.Equal(t, uint64(1), meta.ScaleStatistic(1))
159+
require.Equal(t, uint64(1), meta.ScaleStatistic(10))
160+
161+
meta.Size = 1000
162+
meta.TableBacking.Size = 1000
163+
require.Equal(t, uint64(0), meta.ScaleStatistic(0))
164+
require.Equal(t, uint64(1), meta.ScaleStatistic(1))
165+
require.Equal(t, uint64(10), meta.ScaleStatistic(10))
166+
})
167+
168+
t.Run("overflow", func(t *testing.T) {
169+
meta.Size = 1 << 60
170+
meta.TableBacking.Size = 1 << 60
171+
require.Equal(t, uint64(0), meta.ScaleStatistic(0))
172+
require.Equal(t, uint64(1), meta.ScaleStatistic(1))
173+
require.Equal(t, uint64(12345), meta.ScaleStatistic(12345))
174+
require.Equal(t, uint64(1<<40), meta.ScaleStatistic(1<<40))
175+
require.Equal(t, uint64(1<<50), meta.ScaleStatistic(1<<50))
176+
177+
meta.Size = 1 << 40
178+
meta.TableBacking.Size = 1 << 50
179+
require.Equal(t, uint64(1<<30), meta.ScaleStatistic(1<<40))
180+
require.Equal(t, uint64(1<<40), meta.ScaleStatistic(1<<50))
181+
})
182+
}
183+
141184
// TestTableMetadataSize tests the expected size of our TableMetadata and
142185
// TableBacking structs.
143186
//

sstable/properties.go

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"slices"
1212
"unsafe"
1313

14-
"github.com/cockroachdb/crlib/crmath"
1514
"github.com/cockroachdb/pebble/internal/invariants"
1615
"github.com/cockroachdb/pebble/sstable/colblk"
1716
"github.com/cockroachdb/pebble/sstable/rowblk"
@@ -25,9 +24,6 @@ const propertiesBlockRestartInterval = math.MaxInt32
2524
// can be used by code which doesn't care to make the distinction between physical
2625
// and virtual sstables properties.
2726
//
28-
// For virtual sstables, fields are constructed through extrapolation upon virtual
29-
// reader construction.
30-
//
3127
// NB: The values of these properties can affect correctness. For example,
3228
// if NumRangeKeySets == 0, but the sstable actually contains range keys, then
3329
// the iterators will behave incorrectly.
@@ -91,47 +87,6 @@ func (c *CommonProperties) NumPointDeletions() uint64 {
9187
return invariants.SafeSub(c.NumDeletions, c.NumRangeDeletions)
9288
}
9389

94-
// GetScaledProperties returns an estimation of the common properties for a
95-
// virtual table that addresses only <size> bytes out of the entire <backingSize>.
96-
func (c *CommonProperties) GetScaledProperties(backingSize, size uint64) CommonProperties {
97-
// Make sure the sizes are sane, just in case.
98-
size = max(size, 1)
99-
backingSize = max(backingSize, size)
100-
101-
scale := func(a uint64) uint64 {
102-
return crmath.ScaleUint64(a, size, backingSize)
103-
}
104-
// It's important that no non-zero fields (like NumDeletions, NumRangeKeySets)
105-
// become zero (or vice-versa).
106-
if invariants.Enabled && (scale(1) != 1 || scale(0) != 0) {
107-
panic("bad scale()")
108-
}
109-
110-
scaled := *c
111-
scaled.RawKeySize = scale(c.RawKeySize)
112-
scaled.RawValueSize = scale(c.RawValueSize)
113-
scaled.NumEntries = scale(c.NumEntries)
114-
scaled.NumDataBlocks = scale(c.NumDataBlocks)
115-
scaled.NumTombstoneDenseBlocks = scale(c.NumTombstoneDenseBlocks)
116-
117-
scaled.NumRangeDeletions = scale(c.NumRangeDeletions)
118-
scaled.NumSizedDeletions = scale(c.NumSizedDeletions)
119-
// We cannot directly scale NumDeletions, because it is supposed to be the sum
120-
// of various types of deletions. See #4670.
121-
numOtherDeletions := scale(invariants.SafeSub(c.NumDeletions, c.NumRangeDeletions) + c.NumSizedDeletions)
122-
scaled.NumDeletions = numOtherDeletions + scaled.NumRangeDeletions + scaled.NumSizedDeletions
123-
124-
scaled.NumRangeKeyDels = scale(c.NumRangeKeyDels)
125-
scaled.NumRangeKeySets = scale(c.NumRangeKeySets)
126-
127-
scaled.ValueBlocksSize = scale(c.ValueBlocksSize)
128-
129-
scaled.RawPointTombstoneKeySize = scale(c.RawPointTombstoneKeySize)
130-
scaled.RawPointTombstoneValueSize = scale(c.RawPointTombstoneValueSize)
131-
132-
return scaled
133-
}
134-
13590
// Properties holds the sstable property values. The properties are
13691
// automatically populated during sstable creation and load from the properties
13792
// meta block when an sstable is opened.

sstable/properties_test.go

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -133,42 +133,6 @@ func TestPropertiesSave(t *testing.T) {
133133
}
134134
}
135135

136-
func TestScalePropertiesBadSizes(t *testing.T) {
137-
// Verify that GetScaledProperties works even if the given sizes are not
138-
// valid.
139-
p := CommonProperties{
140-
NumDeletions: 0,
141-
NumEntries: 1,
142-
NumDataBlocks: 10,
143-
}
144-
scaled := p.GetScaledProperties(100, 0)
145-
require.Equal(t, uint64(0), scaled.NumDeletions)
146-
require.Equal(t, uint64(1), scaled.NumEntries)
147-
require.Equal(t, uint64(1), scaled.NumDataBlocks)
148-
149-
scaled = p.GetScaledProperties(100, 1000)
150-
require.Equal(t, uint64(0), scaled.NumDeletions)
151-
require.Equal(t, uint64(1), scaled.NumEntries)
152-
require.Equal(t, uint64(10), scaled.NumDataBlocks)
153-
}
154-
155-
func TestScalePropertiesOverflow(t *testing.T) {
156-
// Verify that GetScaledProperties works even if the given sizes are not
157-
// valid.
158-
p := CommonProperties{
159-
RawKeySize: 1 << 40,
160-
RawValueSize: 1 << 50,
161-
}
162-
scaled := p.GetScaledProperties(1<<60, 1<<60)
163-
require.Equal(t, p, scaled)
164-
165-
scaled = p.GetScaledProperties(1<<60, 1<<50)
166-
require.Equal(t, scaled, CommonProperties{
167-
RawKeySize: 1 << 30,
168-
RawValueSize: 1 << 40,
169-
})
170-
}
171-
172136
func BenchmarkPropertiesLoad(b *testing.B) {
173137
var w rowblk.Writer
174138
w.RestartInterval = propertiesBlockRestartInterval

sstable/reader_test.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"testing"
2323
"time"
2424

25+
"github.com/cockroachdb/crlib/crhumanize"
2526
"github.com/cockroachdb/crlib/testutils/leaktest"
2627
"github.com/cockroachdb/datadriven"
2728
"github.com/cockroachdb/errors"
@@ -138,18 +139,11 @@ func runVirtualReaderTest(t *testing.T, path string, blockSize, indexBlockSize i
138139
}
139140
}()
140141

141-
formatVirtualReader := func(r *Reader, showProps bool, env ReadEnv, backingSize, tableSize uint64) string {
142+
formatVirtualReader := func(r *Reader, showSize bool, env ReadEnv, backingSize, tableSize uint64) string {
142143
var b bytes.Buffer
143144
fmt.Fprintf(&b, "bounds: [%s-%s]\n", env.Virtual.Lower, env.Virtual.Upper)
144-
if showProps {
145-
fmt.Fprintf(&b, "filenum: %s\n", env.Virtual.FileNum.String())
146-
fmt.Fprintf(&b, "props:\n")
147-
props, err := r.ReadPropertiesBlock(context.Background(), nil)
148-
require.NoError(t, err)
149-
p := props.GetScaledProperties(backingSize, tableSize)
150-
for _, line := range strings.Split(strings.TrimSpace(p.String()), "\n") {
151-
fmt.Fprintf(&b, " %s\n", line)
152-
}
145+
if showSize {
146+
fmt.Fprintf(&b, "size: %s / backing size: %s\n", crhumanize.Bytes(tableSize), crhumanize.Bytes(backingSize))
153147
}
154148
return b.String()
155149
}
@@ -198,7 +192,7 @@ func runVirtualReaderTest(t *testing.T, path string, blockSize, indexBlockSize i
198192
params.Lower = base.ParseInternalKey(lowerStr)
199193
params.Upper = base.ParseInternalKey(upperStr)
200194

201-
showProps := td.HasArg("show-props")
195+
showSize := td.HasArg("show-size")
202196

203197
var syntheticPrefix []byte
204198
if td.HasArg("prefix") {
@@ -225,7 +219,7 @@ func runVirtualReaderTest(t *testing.T, path string, blockSize, indexBlockSize i
225219
if err != nil {
226220
return err.Error()
227221
}
228-
return formatVirtualReader(r, showProps, env, wMeta.Size, tableSize)
222+
return formatVirtualReader(r, showSize, env, wMeta.Size, tableSize)
229223

230224
case "compaction-iter":
231225
// Creates a compaction iterator from the virtual reader, and then

0 commit comments

Comments
 (0)