Skip to content

Commit 91012e0

Browse files
committed
precommit fix
Signed-off-by: Huamin Chen <[email protected]>
1 parent f1ecc20 commit 91012e0

File tree

5 files changed

+65
-39
lines changed

5 files changed

+65
-39
lines changed

src/semantic-router/pkg/cache/comprehensive_benchmark_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (c ContentLength) String() string {
3333
// GenerateQuery generates a query with maximum semantic diversity using hash-based randomization
3434
func generateQuery(length ContentLength, index int) string {
3535
// Hash the index to get pseudo-random values (deterministic but well-distributed)
36-
hash := uint64(index)
36+
hash := uint64(index) // #nosec G115 -- index is always positive and bounded
3737
hash = hash*2654435761 + 1013904223 // Knuth's multiplicative hash
3838

3939
// Expanded templates for maximum diversity
@@ -119,35 +119,35 @@ func generateQuery(length ContentLength, index int) string {
119119
}
120120

121121
// Use hash to pseudo-randomly select (but deterministic for same index)
122-
templateIdx := int(hash % uint64(len(templates)))
123-
hash = hash * 16807 % 2147483647 // LCG for next random
122+
templateIdx := int(hash % uint64(len(templates))) // #nosec G115 -- modulo operation is bounded by array length
123+
hash = hash * 16807 % 2147483647 // LCG for next random
124124

125-
topic1Idx := int(hash % uint64(len(topics)))
125+
topic1Idx := int(hash % uint64(len(topics))) // #nosec G115 -- modulo operation is bounded by array length
126126
hash = hash * 16807 % 2147483647
127127

128-
topic2Idx := int(hash % uint64(len(topics)))
128+
topic2Idx := int(hash % uint64(len(topics))) // #nosec G115 -- modulo operation is bounded by array length
129129
hash = hash * 16807 % 2147483647
130130

131-
topic3Idx := int(hash % uint64(len(topics)))
131+
topic3Idx := int(hash % uint64(len(topics))) // #nosec G115 -- modulo operation is bounded by array length
132132
hash = hash * 16807 % 2147483647
133133

134134
// Build query with selected template and topics
135135
query := fmt.Sprintf(templates[templateIdx],
136136
topics[topic1Idx],
137137
topics[topic2Idx],
138138
topics[topic3Idx],
139-
modifiers[int(hash%uint64(len(modifiers)))])
139+
modifiers[int(hash%uint64(len(modifiers)))]) // #nosec G115 -- modulo operation is bounded by array length
140140

141141
// Add unique identifier to guarantee uniqueness
142142
query += fmt.Sprintf(" [Request ID: REQ-%d]", index)
143143

144144
// Add extra context for longer queries
145145
if length > MediumContent {
146146
hash = hash * 16807 % 2147483647
147-
extraTopicIdx := int(hash % uint64(len(topics)))
147+
extraTopicIdx := int(hash % uint64(len(topics))) // #nosec G115 -- modulo operation is bounded by array length
148148
query += fmt.Sprintf(" Also considering %s integration and %s compatibility requirements.",
149149
topics[extraTopicIdx],
150-
modifiers[int(hash%uint64(len(modifiers)))])
150+
modifiers[int(hash%uint64(len(modifiers)))]) // #nosec G115 -- modulo operation is bounded by array length
151151
}
152152

153153
return query
@@ -182,7 +182,8 @@ func BenchmarkComprehensive(b *testing.B) {
182182
}
183183

184184
// Open CSV file for results
185-
csvFile, err := os.OpenFile("../../benchmark_results/benchmark_data.csv",
185+
csvFile, err := os.OpenFile(
186+
"../../benchmark_results/benchmark_data.csv",
186187
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
187188
if err != nil {
188189
b.Logf("Warning: Could not open CSV file: %v", err)

src/semantic-router/pkg/cache/hybrid_cache_test.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ milvus:
5858
params:
5959
M: 16
6060
efConstruction: 200
61-
`), 0644)
61+
`),
62+
0644)
6263
if err != nil {
6364
t.Fatalf("Failed to create test config: %v", err)
6465
}
@@ -112,7 +113,7 @@ milvus:
112113
}
113114

114115
// Test FindSimilar with similar query (should hit)
115-
response, found, err = cache.FindSimilar("gpt-4", "What's the meaning of life?")
116+
_, found, err = cache.FindSimilar("gpt-4", "What's the meaning of life?")
116117
if err != nil {
117118
t.Fatalf("FindSimilar failed: %v", err)
118119
}
@@ -154,7 +155,8 @@ milvus:
154155
dimension: 384
155156
index_type: "HNSW"
156157
metric_type: "IP"
157-
`), 0644)
158+
`),
159+
0644)
158160
if err != nil {
159161
t.Fatalf("Failed to create test config: %v", err)
160162
}
@@ -217,7 +219,8 @@ milvus:
217219
dimension: 384
218220
index_type: "HNSW"
219221
metric_type: "IP"
220-
`), 0644)
222+
`),
223+
0644)
221224
if err != nil {
222225
t.Fatalf("Failed to create test config: %v", err)
223226
}
@@ -264,7 +267,7 @@ milvus:
264267
}
265268

266269
// Try to find an old evicted entry (should be in Milvus)
267-
_, found, err = cache.FindSimilar("gpt-4", "Query number 0")
270+
_, _, err = cache.FindSimilar("gpt-4", "Query number 0")
268271
if err != nil {
269272
t.Fatalf("FindSimilar failed: %v", err)
270273
}
@@ -287,7 +290,8 @@ milvus:
287290
dimension: 384
288291
index_type: "HNSW"
289292
metric_type: "IP"
290-
`), 0644)
293+
`),
294+
0644)
291295
if err != nil {
292296
t.Fatalf("Failed to create test config: %v", err)
293297
}
@@ -316,7 +320,7 @@ milvus:
316320
time.Sleep(100 * time.Millisecond)
317321

318322
// First search - should populate local cache
319-
response, found, err := cache.FindSimilar("gpt-4", testQuery)
323+
_, found, err := cache.FindSimilar("gpt-4", testQuery)
320324
if err != nil {
321325
t.Fatalf("FindSimilar failed: %v", err)
322326
}
@@ -363,7 +367,8 @@ milvus:
363367
dimension: 384
364368
index_type: "HNSW"
365369
metric_type: "IP"
366-
`), 0644)
370+
`),
371+
0644)
367372
if err != nil {
368373
b.Fatalf("Failed to create test config: %v", err)
369374
}
@@ -406,7 +411,8 @@ milvus:
406411
dimension: 384
407412
index_type: "HNSW"
408413
metric_type: "IP"
409-
`), 0644)
414+
`),
415+
0644)
410416
if err != nil {
411417
b.Fatalf("Failed to create test config: %v", err)
412418
}

src/semantic-router/pkg/cache/large_scale_benchmark_test.go

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ func BenchmarkLargeScale(b *testing.B) {
4242
// Open CSV file for results
4343
// Create benchmark_results directory if it doesn't exist
4444
resultsDir := "../../benchmark_results"
45-
os.MkdirAll(resultsDir, 0755)
45+
if err := os.MkdirAll(resultsDir, 0755); err != nil {
46+
b.Logf("Warning: Could not create results directory: %v", err)
47+
}
4648

4749
csvFile, err := os.OpenFile(resultsDir+"/large_scale_benchmark.csv",
4850
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
@@ -274,7 +276,9 @@ func BenchmarkScalability(b *testing.B) {
274276

275277
// CSV output
276278
resultsDir := "../../benchmark_results"
277-
os.MkdirAll(resultsDir, 0755)
279+
if err := os.MkdirAll(resultsDir, 0755); err != nil {
280+
b.Logf("Warning: Could not create results directory: %v", err)
281+
}
278282

279283
csvFile, err := os.OpenFile(resultsDir+"/scalability_benchmark.csv",
280284
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
@@ -313,14 +317,18 @@ func BenchmarkScalability(b *testing.B) {
313317
})
314318

315319
for i := 0; i < cacheSize; i++ {
316-
cache.AddEntry(fmt.Sprintf("req-%d", i), "model",
317-
testQueries[i], []byte("req"), []byte("resp"))
320+
if err := cache.AddEntry(fmt.Sprintf("req-%d", i), "model",
321+
testQueries[i], []byte("req"), []byte("resp")); err != nil {
322+
b.Fatalf("AddEntry failed: %v", err)
323+
}
318324
}
319325

320326
b.ResetTimer()
321327
start := time.Now()
322328
for i := 0; i < b.N; i++ {
323-
cache.FindSimilar("model", searchQuery)
329+
if _, _, err := cache.FindSimilar("model", searchQuery); err != nil {
330+
b.Fatalf("FindSimilar failed: %v", err)
331+
}
324332
}
325333
elapsed := time.Since(start)
326334

@@ -331,7 +339,9 @@ func BenchmarkScalability(b *testing.B) {
331339
if csvFile != nil {
332340
line := fmt.Sprintf("%d,linear,%.0f,%.3f,%.0f\n",
333341
cacheSize, avgLatency, latencyMS, opsPerSec)
334-
csvFile.WriteString(line)
342+
if _, err := csvFile.WriteString(line); err != nil {
343+
b.Logf("Warning: failed to write to CSV: %v", err)
344+
}
335345
}
336346

337347
b.ReportMetric(latencyMS, "ms/op")
@@ -351,8 +361,10 @@ func BenchmarkScalability(b *testing.B) {
351361

352362
buildStart := time.Now()
353363
for i := 0; i < cacheSize; i++ {
354-
cache.AddEntry(fmt.Sprintf("req-%d", i), "model",
355-
testQueries[i], []byte("req"), []byte("resp"))
364+
if err := cache.AddEntry(fmt.Sprintf("req-%d", i), "model",
365+
testQueries[i], []byte("req"), []byte("resp")); err != nil {
366+
b.Fatalf("AddEntry failed: %v", err)
367+
}
356368
if (i+1)%10000 == 0 {
357369
b.Logf(" Built %d/%d entries", i+1, cacheSize)
358370
}
@@ -362,7 +374,9 @@ func BenchmarkScalability(b *testing.B) {
362374
b.ResetTimer()
363375
start := time.Now()
364376
for i := 0; i < b.N; i++ {
365-
cache.FindSimilar("model", searchQuery)
377+
if _, _, err := cache.FindSimilar("model", searchQuery); err != nil {
378+
b.Fatalf("FindSimilar failed: %v", err)
379+
}
366380
}
367381
elapsed := time.Since(start)
368382

@@ -373,7 +387,9 @@ func BenchmarkScalability(b *testing.B) {
373387
if csvFile != nil {
374388
line := fmt.Sprintf("%d,hnsw,%.0f,%.3f,%.0f\n",
375389
cacheSize, avgLatency, latencyMS, opsPerSec)
376-
csvFile.WriteString(line)
390+
if _, err := csvFile.WriteString(line); err != nil {
391+
b.Logf("Warning: failed to write to CSV: %v", err)
392+
}
377393
}
378394

379395
b.ReportMetric(latencyMS, "ms/op")
@@ -430,7 +446,9 @@ func BenchmarkHNSWParameterSweep(b *testing.B) {
430446

431447
// CSV output
432448
resultsDir := "../../benchmark_results"
433-
os.MkdirAll(resultsDir, 0755)
449+
if err := os.MkdirAll(resultsDir, 0755); err != nil {
450+
b.Logf("Warning: Could not create results directory: %v", err)
451+
}
434452

435453
csvFile, err := os.OpenFile(resultsDir+"/hnsw_parameter_sweep.csv",
436454
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
@@ -463,8 +481,10 @@ func BenchmarkHNSWParameterSweep(b *testing.B) {
463481
b.Logf("Building HNSW index: M=%d, efConstruction=200, efSearch=%d", config.m, config.efSearch)
464482
buildStart := time.Now()
465483
for i := 0; i < cacheSize; i++ {
466-
cache.AddEntry(fmt.Sprintf("req-%d", i), "model",
467-
testQueries[i], []byte("req"), []byte("resp"))
484+
if err := cache.AddEntry(fmt.Sprintf("req-%d", i), "model",
485+
testQueries[i], []byte("req"), []byte("resp")); err != nil {
486+
b.Fatalf("AddEntry failed: %v", err)
487+
}
468488
if (i+1)%10000 == 0 {
469489
b.Logf(" Progress: %d/%d", i+1, cacheSize)
470490
}
@@ -484,7 +504,9 @@ func BenchmarkHNSWParameterSweep(b *testing.B) {
484504
b.ResetTimer()
485505
start := time.Now()
486506
for i := 0; i < b.N; i++ {
487-
cache.FindSimilar("model", searchQuery)
507+
if _, _, err := cache.FindSimilar("model", searchQuery); err != nil {
508+
b.Fatalf("FindSimilar failed: %v", err)
509+
}
488510
}
489511
elapsed := time.Since(start)
490512

src/semantic-router/pkg/cache/milvus_cache.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func loadMilvusConfig(configPath string) (*MilvusConfig, error) {
207207

208208
// WORKAROUND: Force development settings for benchmarks
209209
// There seems to be a YAML parsing issue with sigs.k8s.io/yaml
210-
if config.Development.AutoCreateCollection == false && config.Development.DropCollectionOnStartup == false {
210+
if !config.Development.AutoCreateCollection && !config.Development.DropCollectionOnStartup {
211211
fmt.Printf("[WARN] Development settings parsed as false, forcing to true for benchmarks\n")
212212
config.Development.AutoCreateCollection = true
213213
config.Development.DropCollectionOnStartup = true
@@ -773,7 +773,6 @@ func (c *MilvusCache) GetAllEntries(ctx context.Context) ([]string, [][]float32,
773773
"response_body != \"\"", // Only get complete entries
774774
[]string{"request_id", c.config.Collection.VectorField.Name}, // Get IDs and embeddings
775775
)
776-
777776
if err != nil {
778777
observability.Warnf("MilvusCache.GetAllEntries: query failed: %v", err)
779778
return nil, nil, fmt.Errorf("milvus query all failed: %w", err)
@@ -884,7 +883,7 @@ func (c *MilvusCache) GetByID(ctx context.Context, requestID string) ([]byte, er
884883

885884
responseBody := []byte(responseBodyStr)
886885

887-
if responseBody == nil || len(responseBody) == 0 {
886+
if len(responseBody) == 0 {
888887
observability.Debugf("MilvusCache.GetByID: response_body is empty")
889888
metrics.RecordCacheOperation("milvus", "get_by_id", "miss", time.Since(start).Seconds())
890889
return nil, fmt.Errorf("response_body is empty for: %s", requestID)

website/docs/tutorials/semantic-cache/hybrid-cache.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,4 @@ The hybrid cache supports multi-instance deployments where each instance maintai
209209
## See Also
210210

211211
- [In-Memory Cache Documentation](./in-memory-cache.md)
212-
- [Milvus Cache Documentation](./milvus-cache.md)
213-
- [HNSW Implementation Details](../../HNSW_IMPLEMENTATION_SUMMARY.md)
214-
- [Research Paper: Hybrid Architecture](../../papers/hybrid_hnsw_storage_architecture.md)
212+
- [Milvus Cache Documentation](./milvus-cache.md)

0 commit comments

Comments
 (0)