Skip to content

Commit c9a3b2a

Browse files
committed
db,base: move SpanPolicy and related structs to base pkg
Make this available to the valsep pkg.
1 parent 2d1c267 commit c9a3b2a

File tree

5 files changed

+91
-92
lines changed

5 files changed

+91
-92
lines changed

compaction.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3469,7 +3469,7 @@ func (d *DB) compactAndWrite(
34693469
runner := compact.NewRunner(runnerCfg, iter)
34703470

34713471
var spanPolicyValid bool
3472-
var spanPolicy SpanPolicy
3472+
var spanPolicy base.SpanPolicy
34733473
// If spanPolicyValid is true and spanPolicyEndKey is empty, then spanPolicy
34743474
// applies for the rest of the keyspace.
34753475
var spanPolicyEndKey []byte

compaction_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1521,7 +1521,7 @@ func runCompactionTest(
15211521
Start: []byte(keys[0]),
15221522
End: []byte(keys[1]),
15231523
}
1524-
policy := SpanPolicy{}
1524+
policy := base.SpanPolicy{}
15251525
args = args[1:]
15261526
for _, arg := range args {
15271527
parts := strings.Split(arg, "=")

data_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,7 +1787,7 @@ func parseDBOptionsArgs(opts *Options, args []datadriven.CmdArg) error {
17871787
Start: []byte(cmdArg.Vals[0]),
17881788
End: []byte(cmdArg.Vals[1]),
17891789
}
1790-
policy := SpanPolicy{
1790+
policy := base.SpanPolicy{
17911791
ValueStoragePolicy: ValueStorageLowReadLatency,
17921792
}
17931793
spanPolicies = append(spanPolicies, SpanAndPolicy{
@@ -1802,7 +1802,7 @@ func parseDBOptionsArgs(opts *Options, args []datadriven.CmdArg) error {
18021802
Start: []byte(cmdArg.Vals[0]),
18031803
End: []byte(cmdArg.Vals[1]),
18041804
}
1805-
policy := SpanPolicy{ValueStoragePolicy: ValueStorageLatencyTolerant}
1805+
policy := base.SpanPolicy{ValueStoragePolicy: ValueStorageLatencyTolerant}
18061806
spanPolicies = append(spanPolicies, SpanAndPolicy{
18071807
KeyRange: span,
18081808
Policy: policy,

internal/base/internal.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,3 +777,80 @@ const (
777777
ColdTier
778778
NumStorageTiers
779779
)
780+
781+
// SpanPolicy contains policies that can vary by key range.
782+
type SpanPolicy struct {
783+
// Prefer a faster compression algorithm for the keys in this span.
784+
//
785+
// This is useful for keys that are frequently read or written but which don't
786+
// amount to a significant amount of space.
787+
PreferFastCompression bool
788+
789+
// ValueStoragePolicy is a hint used to determine where to store the values
790+
// for KVs.
791+
ValueStoragePolicy ValueStoragePolicyAdjustment
792+
}
793+
794+
// String returns a string representation of the SpanPolicy.
795+
func (p SpanPolicy) String() string {
796+
var sb strings.Builder
797+
if p.PreferFastCompression {
798+
sb.WriteString("fast-compression,")
799+
}
800+
if p.ValueStoragePolicy.DisableSeparationBySuffix {
801+
sb.WriteString("disable-value-separation-by-suffix,")
802+
}
803+
if p.ValueStoragePolicy.DisableBlobSeparation {
804+
sb.WriteString("no-blob-value-separation,")
805+
}
806+
if p.ValueStoragePolicy.OverrideBlobSeparationMinimumSize > 0 {
807+
sb.WriteString("override-value-separation-min-size,")
808+
}
809+
if p.ValueStoragePolicy.MinimumMVCCGarbageSize > 0 {
810+
sb.WriteString("minimum-mvcc-garbage-size")
811+
}
812+
return strings.TrimSuffix(sb.String(), ",")
813+
}
814+
815+
// ValueStoragePolicyAdjustment is used to determine where to store the values for
816+
// KVs, overriding global policies. Values can be configured to be stored in-place,
817+
// in value blocks, or in blob files.
818+
type ValueStoragePolicyAdjustment struct {
819+
// DisableSeparationBySuffix disables discriminating KVs depending on
820+
// suffix.
821+
//
822+
// Among a set of keys with the same prefix, Pebble's default heuristics
823+
// optimize access to the KV with the smallest suffix. This is useful for MVCC
824+
// keys (where the smallest suffix is the latest version), but should be
825+
// disabled for keys where the suffix does not correspond to a version.
826+
// See sstable.IsLikelyMVCCGarbage for the exact criteria we use to
827+
// determine whether a value is likely MVCC garbage.
828+
//
829+
// If separation by suffix is enabled, KVs with older suffix values will be
830+
// written according to the following rules:
831+
// - If the value is empty, no separation is performed.
832+
// - If blob separation is enabled the value will be separated into a blob
833+
// file even if its size is smaller than the minimum value size.
834+
// - If blob separation is disabled, the value will be written to a value
835+
// block within the sstable.
836+
DisableSeparationBySuffix bool
837+
838+
// DisableBlobSeparation disables separating values into blob files.
839+
DisableBlobSeparation bool
840+
841+
// OverrideBlobSeparationMinimumSize overrides the minimum size required
842+
// for value separation into a blob file. Note that value separation must
843+
// be enabled globally for this to take effect.
844+
OverrideBlobSeparationMinimumSize int
845+
846+
// MinimumMVCCGarbageSize, when non-zero, imposes a new minimum size required
847+
// for value separation into a blob file only if the value is likely MVCC
848+
// garbage. Note that value separation must be enabled globally for this to
849+
// take effect.
850+
MinimumMVCCGarbageSize int
851+
}
852+
853+
func (vsp *ValueStoragePolicyAdjustment) ContainsOverrides() bool {
854+
return vsp.OverrideBlobSeparationMinimumSize > 0 || vsp.DisableSeparationBySuffix ||
855+
vsp.MinimumMVCCGarbageSize > 0
856+
}

options.go

Lines changed: 10 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,94 +1281,16 @@ type ValueSeparationPolicy struct {
12811281
GarbageRatioHighPriority float64
12821282
}
12831283

1284-
// SpanPolicy contains policies that can vary by key range. The zero value is
1285-
// the default value.
1286-
type SpanPolicy struct {
1287-
// Prefer a faster compression algorithm for the keys in this span.
1288-
//
1289-
// This is useful for keys that are frequently read or written but which don't
1290-
// amount to a significant amount of space.
1291-
PreferFastCompression bool
1292-
1293-
// ValueStoragePolicy is a hint used to determine where to store the values
1294-
// for KVs.
1295-
ValueStoragePolicy ValueStoragePolicyAdjustment
1296-
}
1297-
1298-
// String returns a string representation of the SpanPolicy.
1299-
func (p SpanPolicy) String() string {
1300-
var sb strings.Builder
1301-
if p.PreferFastCompression {
1302-
sb.WriteString("fast-compression,")
1303-
}
1304-
if p.ValueStoragePolicy.DisableSeparationBySuffix {
1305-
sb.WriteString("disable-value-separation-by-suffix,")
1306-
}
1307-
if p.ValueStoragePolicy.DisableBlobSeparation {
1308-
sb.WriteString("no-blob-value-separation,")
1309-
}
1310-
if p.ValueStoragePolicy.OverrideBlobSeparationMinimumSize > 0 {
1311-
sb.WriteString("override-value-separation-min-size,")
1312-
}
1313-
if p.ValueStoragePolicy.MinimumMVCCGarbageSize > 0 {
1314-
sb.WriteString("minimum-mvcc-garbage-size")
1315-
}
1316-
return strings.TrimSuffix(sb.String(), ",")
1317-
}
1318-
1319-
// ValueStoragePolicyAdjustment is used to determine where to store the values for
1320-
// KVs, overriding global policies. Values can be configured to be stored in-place,
1321-
// in value blocks, or in blob files.
1322-
type ValueStoragePolicyAdjustment struct {
1323-
// DisableSeparationBySuffix disables discriminating KVs depending on
1324-
// suffix.
1325-
//
1326-
// Among a set of keys with the same prefix, Pebble's default heuristics
1327-
// optimize access to the KV with the smallest suffix. This is useful for MVCC
1328-
// keys (where the smallest suffix is the latest version), but should be
1329-
// disabled for keys where the suffix does not correspond to a version.
1330-
// See sstable.IsLikelyMVCCGarbage for the exact criteria we use to
1331-
// determine whether a value is likely MVCC garbage.
1332-
//
1333-
// If separation by suffix is enabled, KVs with older suffix values will be
1334-
// written according to the following rules:
1335-
// - If the value is empty, no separation is performed.
1336-
// - If blob separation is enabled the value will be separated into a blob
1337-
// file even if its size is smaller than the minimum value size.
1338-
// - If blob separation is disabled, the value will be written to a value
1339-
// block within the sstable.
1340-
DisableSeparationBySuffix bool
1341-
1342-
// DisableBlobSeparation disables separating values into blob files.
1343-
DisableBlobSeparation bool
1344-
1345-
// OverrideBlobSeparationMinimumSize overrides the minimum size required
1346-
// for value separation into a blob file. Note that value separation must
1347-
// be enabled globally for this to take effect.
1348-
OverrideBlobSeparationMinimumSize int
1349-
1350-
// MinimumMVCCGarbageSize, when non-zero, imposes a new minimum size required
1351-
// for value separation into a blob file only if the value is likely MVCC
1352-
// garbage. Note that value separation must be enabled globally for this to
1353-
// take effect.
1354-
MinimumMVCCGarbageSize int
1355-
}
1356-
1357-
func (vsp *ValueStoragePolicyAdjustment) ContainsOverrides() bool {
1358-
return vsp.OverrideBlobSeparationMinimumSize > 0 || vsp.DisableSeparationBySuffix ||
1359-
vsp.MinimumMVCCGarbageSize > 0
1360-
}
1361-
13621284
// ValueStorageLatencyTolerant is the suggested ValueStoragePolicyAdjustment
13631285
// to use for key ranges that can tolerate higher value retrieval
13641286
// latency.
1365-
var ValueStorageLatencyTolerant = ValueStoragePolicyAdjustment{
1287+
var ValueStorageLatencyTolerant = base.ValueStoragePolicyAdjustment{
13661288
OverrideBlobSeparationMinimumSize: 10,
13671289
}
13681290

13691291
// ValueStorageLowReadLatency is the suggested ValueStoragePolicyAdjustment
13701292
// to use for key ranges that require low value retrieval latency.
1371-
var ValueStorageLowReadLatency = ValueStoragePolicyAdjustment{
1293+
var ValueStorageLowReadLatency = base.ValueStoragePolicyAdjustment{
13721294
DisableBlobSeparation: true,
13731295
DisableSeparationBySuffix: true,
13741296
}
@@ -1384,12 +1306,12 @@ var ValueStorageLowReadLatency = ValueStoragePolicyAdjustment{
13841306
//
13851307
// The end key can be empty, in which case the policy is valid for the entire
13861308
// keyspace after startKey.
1387-
type SpanPolicyFunc func(startKey []byte) (policy SpanPolicy, endKey []byte, err error)
1309+
type SpanPolicyFunc func(startKey []byte) (policy base.SpanPolicy, endKey []byte, err error)
13881310

13891311
// SpanAndPolicy defines a key range and the policy to apply to it.
13901312
type SpanAndPolicy struct {
13911313
KeyRange KeyRange
1392-
Policy SpanPolicy
1314+
Policy base.SpanPolicy
13931315
}
13941316

13951317
// MakeStaticSpanPolicyFunc returns a SpanPolicyFunc that applies a given policy
@@ -1406,28 +1328,28 @@ func MakeStaticSpanPolicyFunc(cmp base.Compare, inputPolicies ...SpanAndPolicy)
14061328
uniqueKeys = slices.CompactFunc(uniqueKeys, func(a, b []byte) bool { return cmp(a, b) == 0 })
14071329

14081330
// Create a list of policies.
1409-
policies := make([]SpanPolicy, len(uniqueKeys)-1)
1331+
policies := make([]base.SpanPolicy, len(uniqueKeys)-1)
14101332
for _, p := range inputPolicies {
14111333
idx, _ := slices.BinarySearchFunc(uniqueKeys, p.KeyRange.Start, cmp)
14121334
policies[idx] = p.Policy
14131335
}
14141336

1415-
return func(startKey []byte) (_ SpanPolicy, endKey []byte, _ error) {
1337+
return func(startKey []byte) (_ base.SpanPolicy, endKey []byte, _ error) {
14161338
// Find the policy that applies to the start key.
14171339
idx, eq := slices.BinarySearchFunc(uniqueKeys, startKey, cmp)
14181340
switch idx {
14191341
case len(uniqueKeys):
14201342
// The start key is after the last policy.
1421-
return SpanPolicy{}, nil, nil
1343+
return base.SpanPolicy{}, nil, nil
14221344
case len(uniqueKeys) - 1:
14231345
if eq {
14241346
// The start key is exactly the start of the last policy.
1425-
return SpanPolicy{}, nil, nil
1347+
return base.SpanPolicy{}, nil, nil
14261348
}
14271349
case 0:
14281350
if !eq {
14291351
// The start key is before the first policy.
1430-
return SpanPolicy{}, uniqueKeys[0], nil
1352+
return base.SpanPolicy{}, uniqueKeys[0], nil
14311353
}
14321354
}
14331355
if eq {
@@ -1696,7 +1618,7 @@ func (o *Options) EnsureDefaults() {
16961618
o.Experimental.MultiLevelCompactionHeuristic = OptionWriteAmpHeuristic
16971619
}
16981620
if o.Experimental.SpanPolicyFunc == nil {
1699-
o.Experimental.SpanPolicyFunc = func(startKey []byte) (SpanPolicy, []byte, error) { return SpanPolicy{}, nil, nil }
1621+
o.Experimental.SpanPolicyFunc = func(startKey []byte) (base.SpanPolicy, []byte, error) { return base.SpanPolicy{}, nil, nil }
17001622
}
17011623
if o.Experimental.VirtualTableRewriteUnreferencedFraction == nil {
17021624
o.Experimental.VirtualTableRewriteUnreferencedFraction = func() float64 { return defaultVirtualTableUnreferencedFraction }

0 commit comments

Comments
 (0)