Skip to content

Commit 040f4c5

Browse files
authored
fix: Redis index parsing bugs and add kvblock unit tests (#80)
Signed-off-by: Kay Yan <[email protected]>
1 parent 09c67f9 commit 040f4c5

File tree

6 files changed

+128
-2
lines changed

6 files changed

+128
-2
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/llm-d/llm-d-kv-cache-manager
33
go 1.24.1
44

55
require (
6+
github.com/alicebob/miniredis/v2 v2.35.0
67
github.com/cespare/xxhash/v2 v2.3.0
78
github.com/daulet/tokenizers v1.22.1
89
github.com/fxamacker/cbor/v2 v2.7.0
@@ -44,6 +45,7 @@ require (
4445
github.com/prometheus/procfs v0.15.1 // indirect
4546
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
4647
github.com/x448/float16 v0.8.4 // indirect
48+
github.com/yuin/gopher-lua v1.1.1 // indirect
4749
golang.org/x/net v0.38.0 // indirect
4850
golang.org/x/oauth2 v0.27.0 // indirect
4951
golang.org/x/sys v0.31.0 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/alicebob/miniredis/v2 v2.35.0 h1:QwLphYqCEAo1eu1TqPRN2jgVMPBweeQcR21jeqDCONI=
2+
github.com/alicebob/miniredis/v2 v2.35.0/go.mod h1:TcL7YfarKPGDAthEtl5NBeHZfeUQj6OXMm/+iu5cLMM=
13
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
24
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
35
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
@@ -111,6 +113,8 @@ github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
111113
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
112114
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
113115
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
116+
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
117+
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
114118
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
115119
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
116120
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

pkg/kvcache/kvblock/common_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright 2025 The llm-d Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package kvblock_test
18+
19+
import (
20+
"testing"
21+
22+
"github.com/llm-d/llm-d-kv-cache-manager/pkg/kvcache/kvblock"
23+
"github.com/stretchr/testify/assert"
24+
"k8s.io/apimachinery/pkg/util/sets"
25+
)
26+
27+
// testAddBasic is a common test helper function for testing basic Add and Lookup functionality.
28+
func testAddBasic(t *testing.T, index kvblock.Index) {
29+
t.Helper()
30+
31+
key := kvblock.Key{ModelName: "12345", ChunkHash: 12345}
32+
entries := []kvblock.PodEntry{
33+
{PodIdentifier: "10.0.0.1", DeviceTier: "gpu"},
34+
{PodIdentifier: "10.0.0.2", DeviceTier: "gpu"},
35+
}
36+
37+
// Add entries
38+
err := index.Add(t.Context(), []kvblock.Key{key}, entries)
39+
assert.NoError(t, err)
40+
41+
// Lookup after add
42+
hitKeys, podsPerKey, err := index.Lookup(t.Context(), []kvblock.Key{key}, sets.Set[string]{})
43+
assert.NoError(t, err)
44+
assert.Len(t, hitKeys, 1)
45+
assert.Equal(t, key, hitKeys[0])
46+
assert.Equal(t, podsPerKey[key], []string{"10.0.0.1", "10.0.0.2"})
47+
}

pkg/kvcache/kvblock/in_memory_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright 2025 The llm-d Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package kvblock_test
18+
19+
import (
20+
"testing"
21+
22+
"github.com/llm-d/llm-d-kv-cache-manager/pkg/kvcache/kvblock"
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
func TestInMemoryAddBasic(t *testing.T) {
27+
// Create index
28+
index, err := kvblock.NewInMemoryIndex(nil)
29+
assert.NoError(t, err)
30+
testAddBasic(t, index)
31+
}

pkg/kvcache/kvblock/redis.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func (r *RedisIndex) Lookup(ctx context.Context, keys []Key,
126126

127127
var filteredPods []string
128128
for _, p := range pods {
129-
ip := strings.SplitN(p, ":", 2)[0]
129+
ip := strings.SplitN(p, "@", 2)[0]
130130
if !filterPods || podIdentifierSet.Has(ip) {
131131
filteredPods = append(filteredPods, ip)
132132
}
@@ -141,7 +141,7 @@ func (r *RedisIndex) Lookup(ctx context.Context, keys []Key,
141141
podsPerKey[key] = filteredPods
142142
}
143143

144-
return keys[:highestHitIdx], podsPerKey, nil
144+
return keys[:highestHitIdx+1], podsPerKey, nil
145145
}
146146

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

pkg/kvcache/kvblock/redis_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright 2025 The llm-d Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package kvblock_test
18+
19+
import (
20+
"testing"
21+
22+
"github.com/alicebob/miniredis/v2"
23+
"github.com/llm-d/llm-d-kv-cache-manager/pkg/kvcache/kvblock"
24+
"github.com/stretchr/testify/assert"
25+
)
26+
27+
func TestRedisAddBasic(t *testing.T) {
28+
// Create index
29+
server, err := miniredis.Run()
30+
if err != nil {
31+
t.Fatalf("failed to start miniredis: %v", err)
32+
}
33+
defer server.Close()
34+
35+
redisConfig := &kvblock.RedisIndexConfig{
36+
Address: server.Addr(),
37+
}
38+
index, err := kvblock.NewRedisIndex(redisConfig)
39+
assert.NoError(t, err)
40+
41+
testAddBasic(t, index)
42+
}

0 commit comments

Comments
 (0)