Skip to content

Commit 3bcc158

Browse files
authored
[Breaking] Remove redundant keys return from Index.Lookup interface (#84)
* fix Lookup in index interface * fix GetPodScores * fix InMemoryIndex Lookup * fix instrumentedIndex Lookup * update RedisIndex.Lookup * Migrate common test to new interface * lint redis index
1 parent 640d308 commit 3bcc158

File tree

6 files changed

+25
-31
lines changed

6 files changed

+25
-31
lines changed

pkg/kvcache/indexer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,15 @@ func (k *Indexer) GetPodScores(ctx context.Context, prompt, modelName string,
133133
traceLogger.Info("found tokens", "tokens", tokens, "block-keys", blockKeys)
134134

135135
// 3. query kvblock indexer for pods
136-
strBlockKeys, keyToPods, err := k.kvBlockIndex.Lookup(ctx, blockKeys, sets.New(podIdentifiers...))
136+
keyToPods, err := k.kvBlockIndex.Lookup(ctx, blockKeys, sets.New(podIdentifiers...))
137137
if err != nil {
138138
return nil, fmt.Errorf("failed to query kvblock indexer: %w", err)
139139
}
140140
traceLogger.Info("found block keys", "block-keys", blockKeys,
141141
"pods", podsPerKeyPrintHelper(keyToPods))
142142

143143
// 4. score pods
144-
podScores, err := k.kvBlockScorer.Score(strBlockKeys, keyToPods)
144+
podScores, err := k.kvBlockScorer.Score(blockKeys, keyToPods)
145145
if err != nil {
146146
return nil, fmt.Errorf("failed to query kvblock scorer: %w", err)
147147
}

pkg/kvcache/kvblock/common_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ func testAddBasic(t *testing.T, index kvblock.Index) {
3939
assert.NoError(t, err)
4040

4141
// Lookup after add
42-
hitKeys, podsPerKey, err := index.Lookup(t.Context(), []kvblock.Key{key}, sets.Set[string]{})
42+
podsPerKey, err := index.Lookup(t.Context(), []kvblock.Key{key}, sets.Set[string]{})
4343
assert.NoError(t, err)
44-
assert.Len(t, hitKeys, 1)
45-
assert.Equal(t, key, hitKeys[0])
44+
assert.Len(t, podsPerKey, 1)
45+
assert.Contains(t, podsPerKey, key)
4646
assert.Equal(t, podsPerKey[key], []string{"10.0.0.1", "10.0.0.2"})
4747
}

pkg/kvcache/kvblock/in_memory.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,13 @@ type PodCache struct {
8989
// If the podIdentifierSet is empty, all pods are returned.
9090
//
9191
// It returns:
92-
// 1. A slice of the hit keys.
93-
// 2. A map where the keys are those in (1) and the values are pod-identifiers.
94-
// 3. An error if any occurred during the operation.
92+
// 1. A map where the keys are those in (1) and the values are pod-identifiers.
93+
// 2. An error if any occurred during the operation.
9594
func (m *InMemoryIndex) Lookup(ctx context.Context, keys []Key,
9695
podIdentifierSet sets.Set[string],
97-
) ([]Key, map[Key][]string, error) {
96+
) (map[Key][]string, error) {
9897
if len(keys) == 0 {
99-
return nil, nil, fmt.Errorf("no keys provided for lookup")
98+
return nil, fmt.Errorf("no keys provided for lookup")
10099
}
101100

102101
traceLogger := klog.FromContext(ctx).V(logging.TRACE).WithName("kvblock.InMemoryIndex.Lookup")
@@ -108,7 +107,7 @@ func (m *InMemoryIndex) Lookup(ctx context.Context, keys []Key,
108107
if pods, found := m.data.Get(key); found { //nolint:nestif // TODO: can this be optimized?
109108
if pods == nil || pods.cache.Len() == 0 {
110109
traceLogger.Info("no pods found for key, cutting search", "key", key)
111-
return keys[:idx], podsPerKey, nil // early stop since prefix-chain breaks here
110+
return podsPerKey, nil // early stop since prefix-chain breaks here
112111
}
113112

114113
highestHitIdx = idx
@@ -135,7 +134,7 @@ func (m *InMemoryIndex) Lookup(ctx context.Context, keys []Key,
135134
traceLogger.Info("lookup completed", "highest-hit-index", highestHitIdx,
136135
"pods-per-key", podsPerKeyPrintHelper(podsPerKey))
137136

138-
return keys[:highestHitIdx+1], podsPerKey, nil
137+
return podsPerKey, nil
139138
}
140139

141140
// Add adds a set of keys and their associated pod entries to the index backend.

pkg/kvcache/kvblock/index.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,9 @@ type Index interface {
107107
// If the podIdentifierSet is empty, all pods are returned.
108108
//
109109
// It returns:
110-
// 1. A slice of the hit keys.
111-
// 2. A map where the keys are those in (1) and the values are pod-identifiers.
112-
// 3. An error if any occurred during the operation.
113-
Lookup(ctx context.Context, keys []Key, podIdentifierSet sets.Set[string]) ([]Key, map[Key][]string, error)
110+
// 1. A map where the keys are those in (1) and the values are pod-identifiers.
111+
// 2. An error if any occurred during the operation.
112+
Lookup(ctx context.Context, keys []Key, podIdentifierSet sets.Set[string]) (map[Key][]string, error)
114113
// Add adds a set of keys and their associated pod entries to the index backend.
115114
Add(ctx context.Context, keys []Key, entries []PodEntry) error
116115
// Evict removes a key and its associated pod entries from the index backend.

pkg/kvcache/kvblock/instrumented_index.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@ func (m *instrumentedIndex) Lookup(
3434
ctx context.Context,
3535
keys []Key,
3636
podIdentifierSet sets.Set[string],
37-
) ([]Key, map[Key][]string, error) {
37+
) (map[Key][]string, error) {
3838
timer := prometheus.NewTimer(metrics.LookupLatency)
3939
defer timer.ObserveDuration()
4040

4141
metrics.LookupRequests.Inc()
4242

43-
hitKeys, pods, err := m.next.Lookup(ctx, keys, podIdentifierSet)
44-
metrics.LookupHits.Add(float64(len(hitKeys)))
43+
pods, err := m.next.Lookup(ctx, keys, podIdentifierSet)
4544

46-
return hitKeys, pods, err
45+
return pods, err
4746
}

pkg/kvcache/kvblock/redis.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,13 @@ var _ Index = &RedisIndex{}
8080
// If the podIdentifierSet is empty, all pods are returned.
8181
//
8282
// It returns:
83-
// 1. A slice of the hit keys.
84-
// 2. A map where the keys are those in (1) and the values are pod-identifiers.
85-
// 3. An error if any occurred during the operation.
83+
// 1. A map where the keys are those in (1) and the values are pod-identifiers.
84+
// 2. An error if any occurred during the operation.
8685
func (r *RedisIndex) Lookup(ctx context.Context, keys []Key,
8786
podIdentifierSet sets.Set[string],
88-
) ([]Key, map[Key][]string, error) {
87+
) (map[Key][]string, error) {
8988
if len(keys) == 0 {
90-
return nil, nil, nil
89+
return make(map[Key][]string), nil
9190
}
9291

9392
logger := klog.FromContext(ctx).WithName("kvblock.RedisIndex.Lookup")
@@ -105,11 +104,10 @@ func (r *RedisIndex) Lookup(ctx context.Context, keys []Key,
105104

106105
_, execErr := pipe.Exec(ctx)
107106
if execErr != nil {
108-
return nil, nil, fmt.Errorf("redis pipeline execution failed: %w", execErr)
107+
return nil, fmt.Errorf("redis pipeline execution failed: %w", execErr)
109108
}
110109

111110
filterPods := len(podIdentifierSet) > 0 // predicate for filtering
112-
highestHitIdx := 0
113111

114112
for idx, cmd := range results {
115113
key := keys[idx]
@@ -121,7 +119,7 @@ func (r *RedisIndex) Lookup(ctx context.Context, keys []Key,
121119
logger.Error(cmdErr, "failed to get pods for key", "key", key)
122120
}
123121

124-
return keys[:idx], podsPerKey, nil // early stop since prefix-chain breaks here
122+
return podsPerKey, nil // early stop since prefix-chain breaks here
125123
}
126124

127125
var filteredPods []string
@@ -134,14 +132,13 @@ func (r *RedisIndex) Lookup(ctx context.Context, keys []Key,
134132

135133
if len(filteredPods) == 0 {
136134
logger.Info("no pods found for key, cutting search", "key", key)
137-
return keys[:idx], podsPerKey, nil // early stop since prefix-chain breaks here
135+
return podsPerKey, nil // early stop since prefix-chain breaks here
138136
}
139137

140-
highestHitIdx = idx
141138
podsPerKey[key] = filteredPods
142139
}
143140

144-
return keys[:highestHitIdx+1], podsPerKey, nil
141+
return podsPerKey, nil
145142
}
146143

147144
// Add adds a set of keys and their associated pod entries to the index backend.

0 commit comments

Comments
 (0)