Skip to content

feat(upload): migrate file parameter upload to Connect-RPC FileParameterUploadService with chunked uploader and async verifier - #898

Merged
kyasbal merged 8 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-wwrysnqzlzkn
Aug 25, 2026

Conversation

@kyasbal

@kyasbal kyasbal commented Aug 25, 2026

Copy link
Copy Markdown
Member

Summary

This PR migrates the file upload mechanism in KHI from the legacy monolithic REST endpoint (POST /api/v3/upload) to a Protobuf / Connect-RPC service (FileParameterUploadService). It introduces a generic chunked upload manager with disk staging, a generic asynchronous task runner (AsyncTaskManager / DoAsyncOrGet) for non-blocking file verification, and a unified frontend chunked upload client with concurrency control.

Background & Motivation

  1. Scalability & Resiliency for Large Files: Uploading multi-gigabyte log bundles or inspection dumps directly in a single HTTP request easily hits network timeouts, proxy payload limits, or memory pressure. Chunked uploading allows resilient, resumable, and concurrent uploads of arbitrary size.
  2. Generic Non-blocking Async Verification: In dry-run inspection queries, verifying large files synchronously would block the HTTP response or cause timeouts. DoAsyncOrGet decouples file integrity verification into asynchronous background jobs with automatic cancellation upon dependency/hash changes, memoizing results for instant polling.
  3. Clean Architecture & Proto-based Service Interface: Unifies the API contract under Protobuf and Connect-RPC, eliminating ad-hoc REST forms and aligning with KHI's modern service architecture.

API Endpoint Changes

Change Protocol / Type HTTP Method Endpoint Path Description
❌ Removed REST (Multipart) POST /api/v3/upload Legacy synchronous single-file multipart upload.
➕ Added Connect-RPC (Unary) POST /api.v1.FileParameterUploadService/StartFileUpload Initializes a chunked upload session and returns suggested chunk size and token.
➕ Added Connect-RPC (Unary) POST /api.v1.FileParameterUploadService/UploadFileChunk Uploads a binary chunk at a specific byte offset.
➕ Added Connect-RPC (Unary) POST /api.v1.FileParameterUploadService/CompleteFileUpload Validates range completeness, commits the staged file, and initiates verification.
➕ Added Connect-RPC (Unary) POST /api.v1.FileParameterUploadService/AbortFileUpload Aborts the upload session and cleans up temporary chunk files.

Key Changes

1. Protobuf (proto/api/v1/file_parameter_upload.proto)

  • Defined FileParameterUploadService with StartFileUpload, UploadFileChunk, CompleteFileUpload, and AbortFileUpload.
  • Generated Go stubs and TypeScript connect-web stubs (make build-proto).

2. Go Backend

  • pkg/common/ttlcleaner:
    • Implemented generic TTLCleaner[K comparable] utility and ExpirableTarget[K] interface for automated TTL-based resource eviction.
  • pkg/server/chunkedupload:
    • Implemented modular chunk session management:
      • range.go: ByteRange struct and ValidateReceivedRanges for byte interval coverage validation.
      • session.go: ChunkSession model and default constants/errors.
      • manager.go: ChunkSessionManager implementing ttlcleaner.ExpirableTarget[string] with thread-safe file chunk writing (WriteAt).
  • pkg/core/inspection/asynctask:
    • Implemented AsyncTaskManager[K comparable, T any] providing DoAsyncOrGet with context cancellation on input key change, background task execution, and result memoization.
  • pkg/server/upload:
    • Refactored UploadFileStore to use AsyncTaskManager for non-blocking asynchronous verification.
    • Implemented FileParameterUploadManager coordinating Connect sessions with UploadFileStore.
  • pkg/server/api/v1 & pkg/core/init/default:
    • Implemented FileParameterUploadServiceServer Connect handler and registered it into the Gin router.
    • Removed legacy REST POST /api/v3/upload route from pkg/server/server.go.
  • pkg/server/importinspection:
    • Refactored ImportSessionManager to delegate chunked storage and range validation to chunkedupload.ChunkSessionManager.

3. Angular Frontend

  • chunked-uploader.ts:
    • Implemented generic executeChunkedUpload helper handling file slicing, concurrent chunk uploading, progress reporting, and cancellation.
  • FileParameterUploadClientService:
    • Implemented Connect-RPC client communicating with FileParameterUploadService.
  • KHIServerFileUploader:
    • Refactored file parameter uploader to use FileParameterUploadClientService.
  • ImportInspectionClientService:
    • Refactored inspection file upload to use shared executeChunkedUpload.
  • BackendAPI & BackendAPIService:
    • Removed deprecated uploadFile method.

Verification

  • Backend Unit Tests: make test-go passed across all packages.
  • Frontend Unit Tests: make test-web passed (743/743 tests).
  • Frontend Build & Storybook: make build-web and make build-storybook passed.
  • Linters & Formatters: make lint-go, make lint-web, make format-go, make format-web, and make pre-commit passed with 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 generic chunked file upload mechanism and background task manager to replace the previous direct file upload endpoint. Specifically, it adds a reusable TTLCleaner for expiring resources, an AsyncTaskManager for coordinating background tasks with memoization and cancellation, and a new Connect-RPC FileParameterUploadService with corresponding frontend client updates. The ImportSessionManager has also been refactored to leverage the new generic ChunkSessionManager. Feedback on these changes includes addressing a potential integer overflow vulnerability in the chunk boundary validation, removing dead code such as the unused done channel in taskFlight and the unreferenced validateReceivedRanges helper, and using a custom CancellationError instead of DOMException in the frontend uploader to handle aborts cleanly.

Comment thread pkg/server/chunkedupload/manager.go Resolved
Comment thread pkg/server/importinspection/session.go Resolved
@kyasbal
kyasbal force-pushed the push-wwrysnqzlzkn branch from d2be440 to d4bbb9c Compare August 25, 2026 03:02
@kyasbal
kyasbal merged commit 2b5b24d into GoogleCloudPlatform:main Aug 25, 2026
23 of 24 checks passed
@kyasbal
kyasbal deleted the push-wwrysnqzlzkn branch August 25, 2026 03:42
@kyasbal kyasbal added the type:refactor Code refactoring without behavioral change label Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type:refactor Code refactoring without behavioral change

1 participant