Skip to content

feat(workbench): persistent background Trigram indexing and search optimization - #905

Merged
kyasbal merged 7 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-sxorrsrrkvkm
Aug 26, 2026
Merged

kyasbal merged 7 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-sxorrsrrkvkm

Conversation

@kyasbal

@kyasbal kyasbal commented Aug 26, 2026

Copy link
Copy Markdown
Member

Summary

This PR implements persistent background Trigram indexing and search optimization for Kubernetes History Inspector (KHI), enabling sub-second full-text and field-specific regex/literal log searching across large inspection datasets.

Background & Motivation

When filtering logs on large inspection files (e.g. hundreds of thousands of logs), evaluating CEL log queries (such as body("image", "nginx") or body("spec.containers.image", "nginx")) required resolving and parsing every log's AST or full YAML body text on the fly. This incurred high latency (several seconds per query).

Key Changes

1. TrigramIndex Core & Binary Serialization (pkg/server/workbench/cel/trigram.go)

  • Implemented TrigramIndex using Roaring Bitmaps (*roaring.Bitmap) mapped to 3-gram character substrings.
  • Supports streaming incremental construction and RunOptimize() compaction.
  • Implemented compact binary serialization (KHITRIX format v2) with 7-byte magic prefix, zlib-compressed roaring bitmaps, and CRC32 checksums.

2. Field-Path Trigram Query Optimization (pkg/server/workbench/cel/)

  • Implemented PathToTrigramQuery(pathKey string) which parses dot-delimited field paths and formats keys using khifilev6model.FormatKeyName(seg) + ":".
  • Handles standard identifiers (image:), 2-letter fields (ip:), and quoted keys ("@type":) to constrain candidates before resolving log AST nodes.
  • Implemented FindCandidateLogsWithField combining field path queries and regex pattern queries.

3. Background Index Manager (pkg/server/workbench/index_manager.go)

  • Implemented InspectionIndexManager to manage asynchronous TrigramIndex generation and caching in the .khi inspection directory (search.index).
  • Features a concurrent worker pool (worker.NewPool), progress tracking, and atomic file replacement (.tmp -> rename).
  • Detects and automatically invalidates stale indices if log count or size mismatches.

4. Server Lifecycle Integration (pkg/server/)

  • Automatically triggers background index building upon inspection completion and import.
  • Automatically purges index cache on inspection deletion.

5. Workbench API Streaming Synchronization (pkg/server/api/v1/workbench.go)

  • Updated WorkbenchService.OpenWorkbench Connect-RPC handler to stream index building progress to the frontend while reading .khi log chunks.
  • Ensures the active Workbench instance receives the indexed TrigramIndex upon completion for immediate acceleration of subsequent CEL evaluations.

Verification

  • Backend Tests: make test-go passed (100%).
  • Frontend Tests: make test-web passed (775 tests).
  • Linters: make lint-go (0 issues).
  • Pre-commit: make pre-commit passed (0 issues).

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a persistent disk and memory caching mechanism for search indexes (TrigramIndex) via a new InspectionIndexManager, integrating it across the server, import, and workbench services to support asynchronous indexing. The code review highlights several critical issues, including a potential runtime panic in broadcast due to concurrent channel closure, stale cache issues when re-importing or re-running inspections, a goroutine leak in SubscribeIndexProgress, a thundering herd risk during concurrent disk loads, and a potential nil pointer dereference in TrigramIndex.WriteTo.

Comment thread pkg/server/workbench/index_manager.go Resolved
Comment thread pkg/server/api/v1/import_inspection.go Resolved
Comment thread pkg/server/server.go Resolved
Comment thread pkg/server/workbench/index_manager.go Resolved
Comment thread pkg/server/workbench/index_manager.go Resolved
Comment thread pkg/server/workbench/index_manager.go Resolved
Comment thread pkg/server/workbench/cel/trigram.go Resolved
Comment thread pkg/server/workbench/index.go Resolved
Comment thread pkg/server/workbench/manager.go Resolved
Comment thread pkg/server/workbench/manager_test.go Resolved
Comment thread pkg/server/server.go Resolved
@kyasbal kyasbal added the type:feature New feature or request label Aug 26, 2026
@kyasbal
kyasbal merged commit 50b5aae into GoogleCloudPlatform:main Aug 26, 2026
16 checks passed
@kyasbal
kyasbal deleted the push-sxorrsrrkvkm branch August 26, 2026 03:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type:feature New feature or request

1 participant