-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathin_memory.go
More file actions
306 lines (258 loc) · 10.3 KB
/
in_memory.go
File metadata and controls
306 lines (258 loc) · 10.3 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
/*
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"
"strings"
"sync"
lru "github.com/hashicorp/golang-lru/v2"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/controller-runtime/pkg/log"
"github.com/llm-d/llm-d-kv-cache/pkg/utils"
"github.com/llm-d/llm-d-kv-cache/pkg/utils/logging"
)
const (
defaultInMemoryIndexSize = 1e8 // TODO: change to memory-size based configuration
defaultPodsPerKey = 10 // number of pods per key
)
// InMemoryIndexConfig holds the configuration for the InMemoryIndex.
type InMemoryIndexConfig struct {
// Size is the maximum number of keys that can be stored in the index.
Size int `json:"size"`
// PodCacheSize is the maximum number of pod entries per key.
PodCacheSize int `json:"podCacheSize"`
}
// DefaultInMemoryIndexConfig returns a default configuration for the InMemoryIndex.
func DefaultInMemoryIndexConfig() *InMemoryIndexConfig {
return &InMemoryIndexConfig{
Size: defaultInMemoryIndexSize,
PodCacheSize: defaultPodsPerKey,
}
}
// NewInMemoryIndex creates a new InMemoryIndex instance.
func NewInMemoryIndex(cfg *InMemoryIndexConfig) (*InMemoryIndex, error) {
if cfg == nil {
cfg = DefaultInMemoryIndexConfig()
}
cache, err := lru.New[BlockHash, *PodCache](cfg.Size)
if err != nil {
return nil, fmt.Errorf("failed to initialize in-memory index: %w", err)
}
engineToRequestKeys, err := lru.New[BlockHash, BlockHash](cfg.Size)
if err != nil {
return nil, fmt.Errorf("failed to initialize in-memory engine key map: %w", err)
}
return &InMemoryIndex{
data: cache,
engineToRequestKeys: engineToRequestKeys,
podCacheSize: cfg.PodCacheSize,
}, nil
}
// InMemoryIndex is an in-memory implementation of the Index interface.
type InMemoryIndex struct {
// data holds the mapping of requestKeys to sets of pod identifiers.
data *lru.Cache[BlockHash, *PodCache]
// engineToRequestKeys holds the mapping of engineKeys to requestKeys.
engineToRequestKeys *lru.Cache[BlockHash, BlockHash]
// podCacheSize is the maximum number of pod entries per key.
podCacheSize int
}
var _ Index = &InMemoryIndex{}
// PodCache represents a cache for pod entries.
type PodCache struct {
// cache is an LRU cache that maps PodEntry to their last access time.
// thread-safe.
cache *lru.Cache[PodEntry, struct{}]
// mu protects the cache from concurrent access during check-and-set operations.
mu sync.Mutex
// removed indicates this PodCache has been evicted from the parent map.
// Checked by Add after acquiring mu to avoid writing into an orphaned cache.
removed bool
}
// Lookup receives a list of requestKeys and a set of pod identifiers,
// and retrieves the filtered pods associated with those keys.
// The filtering is done based on the pod identifiers provided.
// If the podIdentifierSet is empty, all pods are returned.
//
// It returns:
// 1. A map where the keys are those in (1) and the values are pod-identifiers.
// 2. An error if any occurred during the operation.
func (m *InMemoryIndex) Lookup(ctx context.Context, requestKeys []BlockHash,
podIdentifierSet sets.Set[string],
) (map[BlockHash][]PodEntry, error) {
if len(requestKeys) == 0 {
return nil, fmt.Errorf("no requestKeys provided for lookup")
}
traceLogger := log.FromContext(ctx).V(logging.TRACE).WithName("kvblock.InMemoryIndex.Lookup")
podsPerKey := make(map[BlockHash][]PodEntry)
highestHitIdx := 0
for idx, requestKey := range requestKeys {
if pods, found := m.data.Get(requestKey); found { //nolint:nestif // TODO: can this be optimized?
if pods == nil || pods.cache.Len() == 0 {
traceLogger.Info("no pods found for key, cutting search", "key", requestKey)
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
podsPerKey[requestKey] = pods.cache.Keys()
} else {
// Filter pods based on the provided pod identifiers
for _, pod := range pods.cache.Keys() {
if podIdentifierSet.Has(pod.PodIdentifier) {
podsPerKey[requestKey] = append(podsPerKey[requestKey], pod)
}
}
}
} else {
traceLogger.Info("key not found in index", "key", requestKey)
}
}
traceLogger.Info("lookup completed", "highest-hit-index", highestHitIdx,
"pods-per-key", podsPerKeyPrintHelper(podsPerKey))
return podsPerKey, nil
}
// Add adds a set of engineKeys/requestKeys 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 *InMemoryIndex) Add(ctx context.Context, engineKeys, requestKeys []BlockHash, entries []PodEntry) error {
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.InMemoryIndex.Add")
for i, requestKey := range requestKeys {
// 1. Store engineKey -> requestKey mapping (only if engineKeys provided)
if engineKeys != nil {
m.engineToRequestKeys.Add(engineKeys[i], requestKey)
}
// 2. Store requestKey -> PodCache mapping with retry on stale cache.
// A retry is needed only when a concurrent Evict marks the PodCache as
// removed between getOrCreatePodCache and Lock. The window is tiny, so
// this loop almost never iterates more than once.
for {
podCache := m.getOrCreatePodCache(requestKey)
podCache.mu.Lock()
if podCache.removed {
podCache.mu.Unlock()
continue // retry — this cache was evicted
}
for _, entry := range entries {
podCache.cache.Add(entry, struct{}{})
}
podCache.mu.Unlock()
traceLogger.Info("added pods to key", "requestKey", requestKey, "pods", entries)
break
}
}
return 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 *InMemoryIndex) Evict(ctx context.Context, key BlockHash, keyType KeyType, entries []PodEntry) error {
if len(entries) == 0 {
return fmt.Errorf("no entries provided for eviction from index")
}
traceLogger := log.FromContext(ctx).V(logging.TRACE).WithName("kvblock.InMemoryIndex.Evict")
var requestKey BlockHash
hasEngineKeyMapping := false
switch keyType {
case EngineKey:
rk, found := m.engineToRequestKeys.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)
}
podCache, found := m.data.Get(requestKey)
if !found || podCache == nil {
if hasEngineKeyMapping {
traceLogger.Info("requestKey not found in index, cleaning up engineKey", "requestKey", requestKey, "engineKey", key)
m.engineToRequestKeys.Remove(key)
} else {
traceLogger.Info("key not found in index, nothing to evict", "key", key)
}
return nil
}
podCache.mu.Lock()
prevLen := podCache.cache.Len()
for _, entry := range entries {
podCache.cache.Remove(entry)
}
// Only mark as removed if this Evict actually emptied the cache.
// If the cache was already empty (prevLen == 0), a concurrent Add may have
// just created it — marking it removed would cause Add to spin.
if podCache.cache.Len() == 0 && prevLen > 0 {
podCache.removed = true
// Use Peek + pointer equality to avoid removing a replacement PodCache
// that a concurrent Add may have inserted.
if cur, ok := m.data.Peek(requestKey); ok && cur == podCache {
m.data.Remove(requestKey)
}
if hasEngineKeyMapping {
m.engineToRequestKeys.Remove(key)
}
traceLogger.Info("removed requestKey from index as no pods remain", "requestKey", requestKey, "key", key)
}
podCache.mu.Unlock()
traceLogger.Info("evicted pods from key", "requestKey", requestKey, "key", key, "keyType", keyType, "pods", entries)
return nil
}
// GetRequestKey returns the requestKey associated with the given engineKey.
// Returns an error if the engineKey mapping is missing (e.g., already evicted).
// No external lock needed — lru.Cache is internally thread-safe.
func (m *InMemoryIndex) GetRequestKey(ctx context.Context, engineKey BlockHash) (BlockHash, error) {
requestKey, found := m.engineToRequestKeys.Get(engineKey)
if !found {
return EmptyBlockHash, fmt.Errorf("engine key not found: %s", engineKey.String())
}
return requestKey, nil
}
// getOrCreatePodCache returns the existing PodCache for requestKey,
// or creates and inserts a new one if none exists.
func (m *InMemoryIndex) getOrCreatePodCache(requestKey BlockHash) *PodCache {
if podCache, found := m.data.Get(requestKey); found {
return podCache
}
cache, _ := lru.New[PodEntry, struct{}](m.podCacheSize) //nolint:errcheck // size is always > 0
newPodCache := &PodCache{cache: cache}
// Try to add atomically; if another goroutine beat us, use theirs.
if contains, _ := m.data.ContainsOrAdd(requestKey, newPodCache); contains {
if existing, ok := m.data.Get(requestKey); ok {
return existing
}
// Key was evicted between ContainsOrAdd and Get — use ours.
m.data.Add(requestKey, newPodCache)
}
return newPodCache
}
// podsPerKeyPrintHelper formats a map of keys to pod names for printing.
func podsPerKeyPrintHelper(ks map[BlockHash][]PodEntry) string {
var b strings.Builder
for k, v := range ks {
fmt.Fprintf(&b, "%s: %v\n", k.String(), utils.SliceMap(v, func(pod PodEntry) string {
return pod.String()
}))
}
return b.String()
}