Skip to content

Commit 814bcd9

Browse files
authored
implement kvblock.index metrics config (#57)
Signed-off-by: Maroon Ayoub <[email protected]>
1 parent 7f92230 commit 814bcd9

File tree

7 files changed

+28
-16
lines changed

7 files changed

+28
-16
lines changed

docs/configuration.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ Here's a complete configuration example with all options:
5252
"size": 100000000,
5353
"podCacheSize": 10
5454
},
55-
"enableMetrics": true
55+
"enableMetrics": true,
56+
"metricsLoggingInterval": "1m0s"
5657
},
5758
"tokenizersPoolConfig": {
5859
"workersCount": 8,
@@ -81,6 +82,7 @@ Configures the KV-block index backend. Multiple backends can be configured, but
8182
| `inMemoryConfig` | [InMemoryIndexConfig](#in-memory-index-configuration) | In-memory index configuration | See defaults |
8283
| `redisConfig` | [RedisIndexConfig](#redis-index-configuration) | Redis index configuration | `null` |
8384
| `enableMetrics` | `boolean` | Enable admissions/evictions/hits/misses recording | `false` |
85+
| `metricsLoggingInterval` | `string` (duration) | Interval at which metrics are logged (e.g., `"1m0s"`). If zero or omitted, metrics logging is disabled. Requires `enableMetrics` to be `true`. | `"0s"` |
8486

8587
### In-Memory Index Configuration (`InMemoryIndexConfig`)
8688

examples/kv_cache_index/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ func setupKVCacheIndexer(ctx context.Context) (*kvcache.Indexer, error) {
9494
return nil, err
9595
}
9696

97-
//nolint:contextcheck // NewKVCacheIndexer does not accept context parameter
98-
kvCacheIndexer, err := kvcache.NewKVCacheIndexer(config)
97+
kvCacheIndexer, err := kvcache.NewKVCacheIndexer(ctx, config)
9998
if err != nil {
10099
return nil, err
101100
}

examples/kv_events/offline/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ func main() {
106106
func setupKVCacheIndexer(ctx context.Context) (*kvcache.Indexer, error) {
107107
logger := klog.FromContext(ctx)
108108

109-
//nolint:contextcheck // NewKVCacheIndexer does not accept context parameter
110-
kvCacheIndexer, err := kvcache.NewKVCacheIndexer(getKVCacheIndexerConfig())
109+
kvCacheIndexer, err := kvcache.NewKVCacheIndexer(ctx, getKVCacheIndexerConfig())
111110
if err != nil {
112111
return nil, err
113112
}

examples/kv_events/online/main.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"syscall"
2828
"time"
2929

30-
"github.com/llm-d/llm-d-kv-cache-manager/pkg/kvcache/metrics"
3130
"k8s.io/klog/v2"
3231

3332
"github.com/llm-d/llm-d-kv-cache-manager/pkg/kvcache"
@@ -71,6 +70,9 @@ func getKVCacheIndexerConfig() *kvcache.Config {
7170
config.TokenProcessorConfig.BlockSize = blockSize
7271
}
7372

73+
config.KVBlockIndexConfig.EnableMetrics = true
74+
config.KVBlockIndexConfig.MetricsLoggingInterval = 15 * time.Second
75+
7476
return config
7577
}
7678

@@ -117,10 +119,6 @@ func main() {
117119
eventsPool.Start(ctx)
118120
logger.Info("Events pool started and listening for ZMQ messages")
119121

120-
metrics.Register()
121-
metrics.StartMetricsLogging(ctx, time.Second*10)
122-
logger.Info("Started metrics thread")
123-
124122
// Setup graceful shutdown
125123
sigChan := make(chan os.Signal, 1)
126124
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
@@ -171,8 +169,7 @@ func main() {
171169
func setupKVCacheIndexer(ctx context.Context) (*kvcache.Indexer, error) {
172170
logger := klog.FromContext(ctx)
173171

174-
//nolint:contextcheck // NewKVCacheIndexer does not accept context parameter
175-
kvCacheIndexer, err := kvcache.NewKVCacheIndexer(getKVCacheIndexerConfig())
172+
kvCacheIndexer, err := kvcache.NewKVCacheIndexer(ctx, getKVCacheIndexerConfig())
176173
if err != nil {
177174
return nil, err
178175
}

pkg/kvcache/indexer.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ func NewDefaultConfig() *Config {
5353

5454
// Indexer is a concrete implementation of the KVCacheIndex interface.
5555
type Indexer struct {
56+
config *Config
57+
5658
tokensIndexer prefixstore.Indexer // gets tokens for a prompt
5759
tokensProcessor kvblock.TokenProcessor // turns tokens to kv block keys
5860
kvBlockIndex kvblock.Index // looks up pods for block keys
@@ -62,15 +64,15 @@ type Indexer struct {
6264
}
6365

6466
// NewKVCacheIndexer creates a KVCacheIndex given a Config.
65-
func NewKVCacheIndexer(config *Config) (*Indexer, error) {
67+
func NewKVCacheIndexer(ctx context.Context, config *Config) (*Indexer, error) {
6668
tokensIndexer, err := prefixstore.NewLRUTokenStore(config.PrefixStoreConfig)
6769
if err != nil {
6870
return nil, fmt.Errorf("failed to create prefixstore.Indexer: %w", err)
6971
}
7072

7173
tokensProcessor := kvblock.NewChunkedTokenDatabase(config.TokenProcessorConfig)
7274

73-
kvBlockIndex, err := kvblock.NewIndex(config.KVBlockIndexConfig)
75+
kvBlockIndex, err := kvblock.NewIndex(ctx, config.KVBlockIndexConfig)
7476
if err != nil {
7577
return nil, fmt.Errorf("failed to create RedisKVBlockIndexer: %w", err)
7678
}
@@ -86,6 +88,7 @@ func NewKVCacheIndexer(config *Config) (*Indexer, error) {
8688
}
8789

8890
return &Indexer{
91+
config: config,
8992
tokensIndexer: tokensIndexer,
9093
tokensProcessor: tokensProcessor,
9194
kvBlockIndex: kvBlockIndex,

pkg/kvcache/kvblock/index.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ package kvblock
1919
import (
2020
"context"
2121
"fmt"
22+
"time"
2223

24+
"github.com/llm-d/llm-d-kv-cache-manager/pkg/kvcache/metrics"
2325
"k8s.io/apimachinery/pkg/util/sets"
2426
)
2527

@@ -34,6 +36,10 @@ type IndexConfig struct {
3436
// EnableMetrics toggles whether admissions/evictions/hits/misses are
3537
// recorded.
3638
EnableMetrics bool `json:"enableMetrics"`
39+
// MetricsLoggingInterval defines the interval at which metrics are logged.
40+
// If zero, metrics logging is disabled.
41+
// Requires `EnableMetrics` to be true.
42+
MetricsLoggingInterval time.Duration `json:"metricsLoggingInterval"`
3743
}
3844

3945
// DefaultIndexConfig returns a default configuration for the KV-block index.
@@ -45,7 +51,7 @@ func DefaultIndexConfig() *IndexConfig {
4551
}
4652

4753
// NewIndex creates a new Index instance.
48-
func NewIndex(cfg *IndexConfig) (Index, error) {
54+
func NewIndex(ctx context.Context, cfg *IndexConfig) (Index, error) {
4955
if cfg == nil {
5056
cfg = DefaultIndexConfig()
5157
}
@@ -60,6 +66,7 @@ func NewIndex(cfg *IndexConfig) (Index, error) {
6066
return nil, fmt.Errorf("failed to create in-memory index: %w", err)
6167
}
6268
case cfg.RedisConfig != nil:
69+
//nolint:contextcheck // NewKVCacheIndexer does not accept context parameter
6370
idx, err = NewRedisIndex(cfg.RedisConfig)
6471
if err != nil {
6572
return nil, fmt.Errorf("failed to create Redis index: %w", err)
@@ -71,6 +78,11 @@ func NewIndex(cfg *IndexConfig) (Index, error) {
7178
// wrap in metrics only if enabled
7279
if cfg.EnableMetrics {
7380
idx = NewInstrumentedIndex(idx)
81+
metrics.Register()
82+
if cfg.MetricsLoggingInterval > 0 {
83+
// this is non-blocking
84+
metrics.StartMetricsLogging(ctx, cfg.MetricsLoggingInterval)
85+
}
7486
}
7587

7688
return idx, nil

tests/e2e/redis_mock/e2e_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (s *KVCacheSuite) SetupTest() {
6969

7070
s.Pod1IP = "10.0.0.1"
7171

72-
s.indexer, err = kvcache.NewKVCacheIndexer(s.config)
72+
s.indexer, err = kvcache.NewKVCacheIndexer(s.ctx, s.config)
7373
s.kvBlockIndex = s.indexer.KVBlockIndex()
7474
s.Require().NoError(err)
7575

0 commit comments

Comments
 (0)