-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathcost_aware_memory.go
More file actions
392 lines (330 loc) · 12.6 KB
/
cost_aware_memory.go
File metadata and controls
392 lines (330 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
Copyright 2025 The llm-d Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package kvblock
import (
"context"
"fmt"
"sync"
"sync/atomic"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/controller-runtime/pkg/log"
"github.com/dgraph-io/ristretto/v2"
"github.com/dustin/go-humanize"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/llm-d/llm-d-kv-cache/pkg/utils/logging"
)
const (
defaultNumCounters = 1e8 // 100M keys
defaultBufferItems = 64 // default buffer size for ristretto
)
// CostAwareMemoryIndexConfig holds the configuration for the CostAwareMemoryIndex.
type CostAwareMemoryIndexConfig struct {
// Size is the maximum memory size that can be used by the index.
// Supports human-readable formats like "2GiB", "500MiB", "1GB", etc.
Size string `json:"size,omitempty"`
}
func DefaultCostAwareMemoryIndexConfig() *CostAwareMemoryIndexConfig {
return &CostAwareMemoryIndexConfig{
Size: "2GiB", // 2GiB default size
}
}
// NewCostAwareMemoryIndex creates a new CostAwareMemoryIndex instance.
func NewCostAwareMemoryIndex(cfg *CostAwareMemoryIndexConfig) (*CostAwareMemoryIndex, error) {
if cfg == nil {
cfg = DefaultCostAwareMemoryIndexConfig()
}
// Parse the size string to get byte value using go-humanize
sizeBytes, err := humanize.ParseBytes(cfg.Size)
if err != nil {
return nil, fmt.Errorf("failed to initialize cost aware index: %w", err)
}
cache, err := ristretto.NewCache(&ristretto.Config[string, *CostPodCache]{
NumCounters: defaultNumCounters, // number of keys to track.
MaxCost: int64(sizeBytes), // #nosec G115 , maximum cost of cache
BufferItems: defaultBufferItems, // number of keys per Get buffer.
})
if err != nil {
return nil, fmt.Errorf("failed to initialize cost aware index: %w", err)
}
requestKeys, err := lru.New[BlockHash, BlockHash](defaultNumCounters)
if err != nil {
return nil, fmt.Errorf("failed to initialize in-memory engine key map: %w", err)
}
return &CostAwareMemoryIndex{
data: cache,
requestKeys: requestKeys,
}, nil
}
// CostAwareMemoryIndex implements the Index interface using Ristretto cache for cost-aware memory management.
// The two caches below are kept in sync:
// - data: requestKey -> pod cache (cost-bound by Ristretto MaxCost)
// - requestKeys: engineKey -> requestKey (LRU to cap mapping size)
//
// Add always writes both maps; Evict removes pods and, when empty, removes
// both the requestKey entry and its engineKey mapping to avoid dangling keys.
type CostAwareMemoryIndex struct {
// data holds the mapping of request keys to sets of pod identifiers.
data *ristretto.Cache[string, *CostPodCache]
// requestKeys holds the mapping of engine keys to request keys.
requestKeys *lru.Cache[BlockHash, BlockHash]
// mu protects concurrent access to the index operations
mu sync.RWMutex
}
func (m *CostAwareMemoryIndex) MaxCost() int64 {
return m.data.MaxCost()
}
// CostPodCache wraps a sync.Map of PodEntry and provides cost calculation for memory usage estimation.
type CostPodCache struct {
cache sync.Map // map[string]*PodEntry (key: "podID@tier")
// size tracks the number of entries in cache for O(1) Len().
size atomic.Int64
}
// Add adds or updates a PodEntry in the cache, merging StoredGroups if the entry exists.
func (c *CostPodCache) Add(entry PodEntry) {
cacheKey := podCacheKey(entry.PodIdentifier, entry.DeviceTier, entry.Speculative)
// Try to load existing entry
if existingVal, loaded := c.cache.Load(cacheKey); loaded {
if existingEntry, ok := existingVal.(*PodEntry); ok {
// Merge StoredGroups
existingEntry.StoredGroups = mergeGroupsUnique(existingEntry.StoredGroups, entry.StoredGroups)
// Store updated entry
c.cache.Store(cacheKey, existingEntry)
}
} else {
// Create new entry
newEntry := &PodEntry{
PodIdentifier: entry.PodIdentifier,
DeviceTier: entry.DeviceTier,
Speculative: entry.Speculative,
StoredGroups: mergeGroupsUnique(nil, entry.StoredGroups),
}
c.cache.Store(cacheKey, newEntry)
c.size.Add(1)
}
}
// Delete removes a PodEntry from the cache entirely.
func (c *CostPodCache) Delete(entry PodEntry) {
cacheKey := podCacheKey(entry.PodIdentifier, entry.DeviceTier, entry.Speculative)
if _, loaded := c.cache.LoadAndDelete(cacheKey); loaded {
c.size.Add(-1)
}
}
// RemoveGroups removes specified groups from a PodEntry's StoredGroups.
// If no groups remain, the entry is deleted.
func (c *CostPodCache) RemoveGroups(entry PodEntry) bool {
cacheKey := podCacheKey(entry.PodIdentifier, entry.DeviceTier, entry.Speculative)
existingVal, loaded := c.cache.Load(cacheKey)
if !loaded {
return false
}
existingEntry, ok := existingVal.(*PodEntry)
if !ok {
return false
}
// Remove specified groups
updatedGroups := removeGroups(existingEntry.StoredGroups, entry.StoredGroups)
if len(updatedGroups) == 0 {
// No groups left, delete the entry
c.cache.Delete(cacheKey)
c.size.Add(-1)
return true
}
// Update with remaining groups
existingEntry.StoredGroups = updatedGroups
c.cache.Store(cacheKey, existingEntry)
return false
}
// Len returns the number of entries in the cache.
func (c *CostPodCache) Len() int {
return int(c.size.Load())
}
// CalculateByteSize estimates memory usage for ristretto cost calculation.
// This is an approximation used for cache eviction decisions.
func (c *CostPodCache) CalculateByteSize(keyStr string) int64 {
var totalBytes int64
var entryCount int64
// Key string memory usage
totalBytes += int64(len(keyStr))
// CostPodCache struct overhead (sync.Map overhead)
totalBytes += 64 // approximate sync.Map overhead
// Count entries and calculate their size
c.cache.Range(func(key, value interface{}) bool {
// key is now a string, value is *PodEntry
keyStr, okKey := key.(string)
entry, okEntry := value.(*PodEntry)
if !okKey || !okEntry {
return true
}
entryCount++
totalBytes += int64(len(keyStr)) // cache key string
totalBytes += int64(len(entry.PodIdentifier)) // PodIdentifier string content
totalBytes += int64(len(entry.DeviceTier)) // DeviceTier string content
totalBytes += int64(len(entry.StoredGroups) * 8) // StoredGroups slice (8 bytes per int)
totalBytes += 32 // string headers (16 bytes each for 2 strings)
totalBytes += 24 // slice header for StoredGroups
totalBytes += 8 // pointer to PodEntry
totalBytes += 8 // struct padding/alignment
return true
})
// sync.Map overhead estimation
if entryCount > 0 {
// Map overhead: assuming 24 bytes per entry (key+value+metadata in sync.Map)
totalBytes += entryCount * 24
}
return totalBytes
}
var _ Index = &CostAwareMemoryIndex{}
// Add adds a set of keys and their associated pod entries to the index backend.
// If engineKeys is nil, only requestKey -> PodEntry mappings are created (no engineKey -> requestKey mapping).
// This is used for speculative entries where engine keys are not yet known.
func (m *CostAwareMemoryIndex) Add(ctx context.Context, engineKeys, requestKeys []BlockHash, entries []PodEntry) error {
m.mu.Lock()
defer m.mu.Unlock()
if len(requestKeys) == 0 || len(entries) == 0 {
return fmt.Errorf("no keys or entries provided for adding to index")
}
if engineKeys != nil && len(engineKeys) != len(requestKeys) {
return fmt.Errorf("mismatch between engine keys and request keys length")
}
traceLogger := log.FromContext(ctx).V(logging.TRACE).WithName("kvblock.CostAwareMemoryIndex.Add")
for i, requestKey := range requestKeys {
// Store engineKey -> requestKey mapping (only if engineKeys provided)
if engineKeys != nil {
m.requestKeys.Add(engineKeys[i], requestKey)
}
keyStr := requestKey.String()
podCache, found := m.data.Get(keyStr)
if !found {
podCache = &CostPodCache{}
}
for _, entry := range entries {
podCache.Add(entry)
}
// Calculate the actual cost for this cache entry
cost := podCache.CalculateByteSize(keyStr)
m.data.Set(keyStr, podCache, cost)
traceLogger.Info("added pods to key", "requestKey", requestKey, "pods", entries, "cost-bytes", cost)
}
m.data.Wait()
return nil
}
func (m *CostAwareMemoryIndex) Lookup(ctx context.Context, requestKeys []BlockHash,
podIdentifierSet sets.Set[string],
) (map[BlockHash][]PodEntry, error) {
m.mu.RLock()
defer m.mu.RUnlock()
if len(requestKeys) == 0 {
return nil, fmt.Errorf("no keys provided for lookup")
}
traceLogger := log.FromContext(ctx).V(logging.TRACE).WithName("kvblock.CostAwareMemoryIndex.Lookup")
podsPerKey := make(map[BlockHash][]PodEntry)
highestHitIdx := 0
for idx, key := range requestKeys {
keyStr := key.String()
if pods, found := m.data.Get(keyStr); found { //nolint:nestif // TODO: can this be optimized?
if pods == nil || pods.Len() == 0 {
traceLogger.Info("no pods found for key, cutting search", "key", key)
return podsPerKey, nil // early stop since prefix-chain breaks here
}
highestHitIdx = idx
if podIdentifierSet.Len() == 0 {
// If no pod identifiers are provided, return all pods
pods.cache.Range(func(k, value interface{}) bool {
if pod, ok := value.(*PodEntry); ok {
podsPerKey[key] = append(podsPerKey[key], *pod)
}
return true
})
} else {
// Filter pods based on the provided pod identifiers
pods.cache.Range(func(k, value interface{}) bool {
if pod, ok := value.(*PodEntry); ok {
if podIdentifierSet.Has(pod.PodIdentifier) {
podsPerKey[key] = append(podsPerKey[key], *pod)
}
}
return true
})
}
} else {
traceLogger.Info("key not found in index", "key", key)
}
}
traceLogger.Info("lookup completed", "highest-hit-index", highestHitIdx,
"pods-per-key", podsPerKeyPrintHelper(podsPerKey))
return podsPerKey, nil
}
// Evict removes a key and its associated pod entries from the index backend.
// keyType indicates whether the key is an EngineKey (requires engine→request lookup)
// or a RequestKey (used directly for speculative entries without engineKey mapping).
func (m *CostAwareMemoryIndex) Evict(ctx context.Context, key BlockHash, keyType KeyType, entries []PodEntry) error {
m.mu.Lock()
defer m.mu.Unlock()
if len(entries) == 0 {
return fmt.Errorf("no entries provided for eviction from index")
}
traceLogger := log.FromContext(ctx).V(logging.TRACE).WithName("kvblock.CostAwareMemoryIndex.Evict")
var requestKey BlockHash
hasEngineKeyMapping := false
switch keyType {
case EngineKey:
rk, found := m.requestKeys.Get(key)
if !found {
traceLogger.Info("engineKey not found in mapping, nothing to evict", "engineKey", key)
return nil
}
requestKey = rk
hasEngineKeyMapping = true
case RequestKey:
requestKey = key
default:
return fmt.Errorf("unknown key type: %d", keyType)
}
keyStr := requestKey.String()
podCache, found := m.data.Get(keyStr)
if !found || podCache == nil {
if hasEngineKeyMapping {
traceLogger.Info("requestKey not found in index, cleaning up engineKey", "requestKey", requestKey, "engineKey", key)
m.requestKeys.Remove(key)
} else {
traceLogger.Info("key not found in index, nothing to evict", "key", key)
}
return nil
}
podCacheLenBefore := podCache.Len()
for _, entry := range entries {
// Remove groups from the entry; if no groups remain, the entry is deleted
podCache.RemoveGroups(entry)
}
if podCache.Len() == 0 {
m.data.Del(keyStr)
if hasEngineKeyMapping {
m.requestKeys.Remove(key)
}
traceLogger.Info("removed requestKey from index as no pods remain", "requestKey", requestKey, "key", key)
} else if podCacheLenBefore != podCache.Len() {
m.data.Set(keyStr, podCache, podCache.CalculateByteSize(keyStr))
traceLogger.Info("evicted pods from key", "requestKey", requestKey, "key", key, "keyType", keyType, "pods", entries)
}
m.data.Wait()
return nil
}
// GetRequestKey returns the requestKey associated with the given engineKey.
// Returns an error if the engineKey is not mapped (e.g., evicted earlier).
func (m *CostAwareMemoryIndex) GetRequestKey(ctx context.Context, engineKey BlockHash) (BlockHash, error) {
requestKey, found := m.requestKeys.Get(engineKey)
if !found {
return EmptyBlockHash, fmt.Errorf("engine key not found: %s", engineKey.String())
}
return requestKey, nil
}