Skip to content

Commit 0e46743

Browse files
authored
YDB: minor improvements in table metadata caching layer (#345)
1 parent 65e9df5 commit 0e46743

File tree

10 files changed

+74
-51
lines changed

10 files changed

+74
-51
lines changed

app/config/server.pb.go

Lines changed: 17 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/config/server.proto

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,10 @@ message TYdbConfig {
360360
// TRistretto contains configuration for Ristretto cache.
361361
// See https://pkg.go.dev/github.com/dgraph-io/ristretto/v2#Config for details.
362362
message TRistretto {
363-
int64 num_counters = 1;
364-
int64 max_cost = 2;
365-
int64 buffer_items = 3;
363+
// The maximum number of keys to store in the cache.
364+
int64 max_keys = 1;
365+
// The maximum size of the cache in bytes (approximate limit)
366+
int64 max_size_bytes = 2;
366367
}
367368

368369
oneof storage {

app/server/config/config.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ func fillServerConfigDefaults(c *config.TServerConfig) {
246246
fillYdbConfigDefaults(c.Datasources.Logging.Ydb)
247247
}
248248

249+
//nolint:gocyclo
249250
func fillYdbConfigDefaults(c *config.TYdbConfig) {
250251
if c.OpenConnectionTimeout == "" {
251252
c.OpenConnectionTimeout = "5s"
@@ -289,6 +290,18 @@ func fillYdbConfigDefaults(c *config.TYdbConfig) {
289290
if c.Mode == config.TYdbConfig_MODE_QUERY_SERVICE_NATIVE && c.ResourcePool == "" {
290291
c.ResourcePool = "default"
291292
}
293+
294+
if c.TableMetadataCache != nil && c.TableMetadataCache.GetRistretto() != nil {
295+
ristretto := c.TableMetadataCache.GetRistretto()
296+
297+
if ristretto.MaxKeys == 0 {
298+
ristretto.MaxKeys = 10000
299+
}
300+
301+
if ristretto.MaxSizeBytes == 0 {
302+
ristretto.MaxSizeBytes = 64 * 1024 * 1024
303+
}
304+
}
292305
}
293306

294307
func validateServerConfig(c *config.TServerConfig) error {
@@ -573,16 +586,12 @@ func validateYdbConfig(c *config.TYdbConfig) error {
573586

574587
switch storageCfg := cacheCfg.Storage.(type) {
575588
case *config.TYdbConfig_TTableMetadataCache_Ristretto:
576-
if storageCfg.Ristretto.BufferItems <= 0 {
577-
return fmt.Errorf("invalid `buffer_items` value: %v", storageCfg.Ristretto.BufferItems)
578-
}
579-
580-
if storageCfg.Ristretto.MaxCost <= 0 {
581-
return fmt.Errorf("invalid `max_cost` value: %v", storageCfg.Ristretto.MaxCost)
589+
if storageCfg.Ristretto.MaxSizeBytes <= 0 {
590+
return fmt.Errorf("invalid `max_size_bytes` value: %v", storageCfg.Ristretto.MaxSizeBytes)
582591
}
583592

584-
if storageCfg.Ristretto.NumCounters <= 0 {
585-
return fmt.Errorf("invalid `num_counters` value: %v", storageCfg.Ristretto.NumCounters)
593+
if storageCfg.Ristretto.MaxKeys <= 0 {
594+
return fmt.Errorf("invalid `max_keys` value: %v", storageCfg.Ristretto.MaxKeys)
586595
}
587596
default:
588597
return fmt.Errorf("unknown storage: %v", storageCfg)

app/server/datasource/rdbms/ydb/schema_provider.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ func (f *schemaProvider) GetSchema(
3535
logger.Debug("obtaining table metadata from YDB")
3636

3737
// Try to get cached value - this helps us avoid creating a connection
38-
cachedValue, exists := f.tableMetadataCache.Get(request.DataSourceInstance, request.Table)
39-
if exists && cachedValue != nil && cachedValue.Schema != nil {
38+
cachedValue, cachedValueExists := f.tableMetadataCache.Get(logger, request.DataSourceInstance, request.Table)
39+
if cachedValueExists && cachedValue != nil && cachedValue.Schema != nil {
4040
logger.Debug("obtained table metadata from cache")
4141

4242
return cachedValue.Schema, nil
@@ -110,7 +110,7 @@ func (f *schemaProvider) GetSchema(
110110
StoreType: table_metadata_cache.EStoreType(desc.StoreType),
111111
}
112112

113-
ok := f.tableMetadataCache.Put(request.DataSourceInstance, request.Table, value)
113+
ok := f.tableMetadataCache.Put(logger, request.DataSourceInstance, request.Table, value)
114114
if !ok {
115115
logger.Warn("failed to cache table metadata")
116116
} else {

app/server/datasource/rdbms/ydb/split_provider.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func (sp SplitProvider) ListSplits(
3838

3939
// Try to get cached value - this may help us to save a connection
4040
cachedValue, cachedValueExists := sp.tableMetadataCache.Get(
41+
logger,
4142
params.Select.DataSourceInstance,
4243
params.Select.GetFrom().GetTable(),
4344
)
@@ -96,6 +97,21 @@ func (sp SplitProvider) ListSplits(
9697
if err != nil {
9798
return fmt.Errorf("get table store type: %w", err)
9899
}
100+
101+
// and than put it back
102+
ok := sp.tableMetadataCache.Put(
103+
logger,
104+
slct.GetDataSourceInstance(),
105+
slct.GetFrom().GetTable(),
106+
&table_metadata_cache.TValue{
107+
StoreType: storeType,
108+
},
109+
)
110+
if !ok {
111+
logger.Warn("failed to cache table metadata")
112+
} else {
113+
logger.Debug("cached table metadata")
114+
}
99115
}
100116

101117
switch storeType {

app/server/datasource/rdbms/ydb/table_metadata_cache/interface.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package table_metadata_cache
22

33
import (
4+
"go.uber.org/zap"
5+
46
api_common "github.com/ydb-platform/fq-connector-go/api/common"
57
)
68

@@ -16,7 +18,7 @@ type Metrics struct {
1618
}
1719

1820
type Cache interface {
19-
Put(dsi *api_common.TGenericDataSourceInstance, tableName string, value *TValue) bool
20-
Get(dsi *api_common.TGenericDataSourceInstance, tableName string) (*TValue, bool)
21+
Put(logger *zap.Logger, dsi *api_common.TGenericDataSourceInstance, tableName string, value *TValue) bool
22+
Get(logger *zap.Logger, dsi *api_common.TGenericDataSourceInstance, tableName string) (*TValue, bool)
2123
Metrics() *Metrics
2224
}

app/server/datasource/rdbms/ydb/table_metadata_cache/noop.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package table_metadata_cache
22

33
import (
4+
"go.uber.org/zap"
5+
46
api_common "github.com/ydb-platform/fq-connector-go/api/common"
57
)
68

@@ -9,11 +11,11 @@ var _ Cache = (*noopCache)(nil)
911
type noopCache struct {
1012
}
1113

12-
func (noopCache) Put(_ *api_common.TGenericDataSourceInstance, _ string, _ *TValue) bool {
14+
func (noopCache) Put(_ *zap.Logger, _ *api_common.TGenericDataSourceInstance, _ string, _ *TValue) bool {
1315
return true
1416
}
1517

16-
func (noopCache) Get(_ *api_common.TGenericDataSourceInstance, _ string) (*TValue, bool) {
18+
func (noopCache) Get(_ *zap.Logger, _ *api_common.TGenericDataSourceInstance, _ string) (*TValue, bool) {
1719
return nil, false
1820
}
1921

app/server/datasource/rdbms/ydb/table_metadata_cache/ristretto.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
"github.com/dgraph-io/ristretto/v2"
8+
"go.uber.org/zap"
89
"google.golang.org/protobuf/proto"
910

1011
api_common "github.com/ydb-platform/fq-connector-go/api/common"
@@ -23,7 +24,7 @@ type ristrettoCache struct {
2324
ttl time.Duration
2425
}
2526

26-
func (r *ristrettoCache) Put(dsi *api_common.TGenericDataSourceInstance, tableName string, value *TValue) bool {
27+
func (r *ristrettoCache) Put(_ *zap.Logger, dsi *api_common.TGenericDataSourceInstance, tableName string, value *TValue) bool {
2728
key := serializeKey(dsi, tableName)
2829

2930
// Serialize TValue to bytes
@@ -35,7 +36,7 @@ func (r *ristrettoCache) Put(dsi *api_common.TGenericDataSourceInstance, tableNa
3536
return r.cache.SetWithTTL(key, data, int64(len(data)), r.ttl)
3637
}
3738

38-
func (r *ristrettoCache) Get(dsi *api_common.TGenericDataSourceInstance, tableName string) (*TValue, bool) {
39+
func (r *ristrettoCache) Get(_ *zap.Logger, dsi *api_common.TGenericDataSourceInstance, tableName string) (*TValue, bool) {
3940
key := serializeKey(dsi, tableName)
4041

4142
data, found := r.cache.Get(key)
@@ -77,9 +78,9 @@ func (r *ristrettoCache) Metrics() *Metrics {
7778

7879
func newRistrettoCache(cfg *config.TYdbConfig_TTableMetadataCache) (*ristrettoCache, error) {
7980
cache, err := ristretto.NewCache(&ristretto.Config[string, []byte]{
80-
NumCounters: cfg.GetRistretto().NumCounters,
81-
MaxCost: cfg.GetRistretto().MaxCost,
82-
BufferItems: cfg.GetRistretto().BufferItems,
81+
NumCounters: cfg.GetRistretto().MaxKeys,
82+
MaxCost: cfg.GetRistretto().MaxSizeBytes,
83+
BufferItems: 64, // reasonable default
8384
Metrics: true,
8485
})
8586

app/validate/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func validateHelmConfigurationFile(cmd *cobra.Command, _ []string) error {
9797
return fmt.Errorf("new config from YAML data: %v", err)
9898
}
9999

100+
// Print protobuf config
100101
prettyJSON, err := common.ProtobufToJSON(cfg, true, " ")
101102
if err != nil {
102103
return fmt.Errorf("marshal config to JSON: %v", err)

tests/suite/suite.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ func (b *Base[_, _]) SetupSuite() {
8585
Ttl: YDBTableMetadataCacheTTL.String(),
8686
Storage: &config.TYdbConfig_TTableMetadataCache_Ristretto{
8787
Ristretto: &config.TYdbConfig_TTableMetadataCache_TRistretto{
88-
NumCounters: 1000,
89-
MaxCost: 10 * 1024 * 1024, // 10 MB
90-
BufferItems: 64,
88+
MaxKeys: 1000,
89+
MaxSizeBytes: 10 * 1024 * 1024, // 10 MB
9190
},
9291
},
9392
},

0 commit comments

Comments
 (0)