Skip to content

feat(upload): implement concurrent async upload pipeline with WebSock… - #334

Merged
aniebietafia merged 1 commit into
mainfrom
perf/upload_streaming
Jun 28, 2026
Merged

aniebietafia merged 1 commit into
mainfrom
perf/upload_streaming

Conversation

@aniebietafia

@aniebietafia aniebietafia commented Jun 28, 2026 •

Copy link
Copy Markdown
Contributor

…ets and Resilience4j

  • Refactored synchronous, sequential document uploads to a concurrent, non-blocking pipeline using CompletableFuture and Spring @async.
  • Integrated STOMP WebSockets with Redis-backed, one-time ticket authentication for real-time progress updates.
  • Added Resilience4j circuit breakers, retries, and bulkhead policies to S3 upload and OCR provider integrations (Google Vision, Tesseract).
  • Configured immediate file-by-file Kafka message publishing to start OCR processing as soon as individual files land in storage.
  • Added a dedicated thread-pool executor for the upload pipeline and Actuator metrics for circuit breakers.

Summary by CodeRabbit

  • New Features

    • Added real-time progress updates for upload and extraction workflows.
    • Introduced a WebSocket connection flow with short-lived access tickets.
    • Upload and OCR requests now return an immediate accepted response with a collection ID and progress channel.
  • Bug Fixes

    • Improved handling for large uploads with upfront file-size and storage checks.
    • Added more reliable processing with retries, circuit breaking, and upload backpressure.
    • Expanded access for WebSocket endpoints in the app’s security settings.

…ets and Resilience4j

- Refactored synchronous, sequential document uploads to a concurrent, non-blocking pipeline using CompletableFuture and Spring @async.
- Integrated STOMP WebSockets with Redis-backed, one-time ticket authentication for real-time progress updates.
- Added Resilience4j circuit breakers, retries, and bulkhead policies to S3 upload and OCR provider integrations (Google Vision, Tesseract).
- Configured immediate file-by-file Kafka message publishing to start OCR processing as soon as individual files land in storage.
- Added a dedicated thread-pool executor for the upload pipeline and Actuator metrics for circuit breakers.

Signed-off-by: aniebietafia <aniebietafia87@gmail.com>
@coderabbitai

coderabbitai Bot commented Jun 28, 2026 •

Copy link
Copy Markdown

Review Change Stack

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Free

Run ID: aabe6726-d50a-406c-a54f-3326542e3a63

📥 Commits

Reviewing files that changed from the base of the PR and between 9a56f4d and 5d0ffe8.

📒 Files selected for processing (22)
  • pom.xml
  • src/main/java/com/xtraction/api/brokers/kafka/config/KafkaTopicConfig.java
  • src/main/java/com/xtraction/api/config/AsyncConfig.java
  • src/main/java/com/xtraction/api/config/CircuitBreakerEventListener.java
  • src/main/java/com/xtraction/api/config/ResilienceConfig.java
  • src/main/java/com/xtraction/api/config/SecurityConfig.java
  • src/main/java/com/xtraction/api/config/WebSocketConfig.java
  • src/main/java/com/xtraction/api/documents/controller/DocumentController.java
  • src/main/java/com/xtraction/api/documents/dto/response/StreamingUploadResponse.java
  • src/main/java/com/xtraction/api/documents/service/StreamingUploadPipeline.java
  • src/main/java/com/xtraction/api/ocrprocessing/controller/OcrDocumentController.java
  • src/main/java/com/xtraction/api/ocrprocessing/dto/response/ExtractionProgressEvent.java
  • src/main/java/com/xtraction/api/ocrprocessing/events/OcrEventMapper.java
  • src/main/java/com/xtraction/api/ocrprocessing/events/OcrMessageListener.java
  • src/main/java/com/xtraction/api/ocrprocessing/events/OcrRequestedEvent.java
  • src/main/java/com/xtraction/api/ocrprocessing/service/ExtractionProgressService.java
  • src/main/java/com/xtraction/api/ocrprocessing/service/OcrProcessingService.java
  • src/main/java/com/xtraction/api/ocrprocessing/service/ProcessOcr.java
  • src/main/java/com/xtraction/api/ocrprocessing/utils/FileStorageService.java
  • src/main/java/com/xtraction/api/websocket/WebSocketAuthInterceptor.java
  • src/main/java/com/xtraction/api/websocket/WebSocketTicketController.java
  • src/main/java/com/xtraction/api/websocket/WebSocketTicketService.java

📝 Walkthrough

Walkthrough

Introduces an async streaming upload pipeline that returns HTTP 202 immediately and pushes real-time extraction progress over STOMP/WebSocket. Adds ticket-based WebSocket authentication via Redis, configures Resilience4j circuit breakers, retries, and bulkheads for S3 and OCR providers, and wires progress notifications into both the upload pipeline and Kafka OCR message listener.

Changes

Async Streaming Upload Pipeline with WebSocket Progress and Resilience4j

Layer / File(s) Summary
Dependencies and infrastructure config
pom.xml, config/AsyncConfig.java, brokers/kafka/config/KafkaTopicConfig.java, config/SecurityConfig.java
Adds Maven dependencies for WebSocket, Resilience4j, and AOP; registers uploadPipelineExecutor thread pool bean; creates xtraction-extraction-progress Kafka topic with 3-day retention; permits /ws/** in security filter chain.
Resilience4j configuration and observability
config/ResilienceConfig.java, config/CircuitBreakerEventListener.java
Defines CircuitBreakerRegistry (s3Upload, ocrGoogleVision, ocrTesseract), RetryRegistry (s3Upload, ocrProvider), and BulkheadRegistry (ocrProcessing) beans. Adds post-construct event listener logging state transitions and threshold exceedances.
WebSocket ticket auth and STOMP configuration
websocket/WebSocketTicketService.java, websocket/WebSocketTicketController.java, websocket/WebSocketAuthInterceptor.java, config/WebSocketConfig.java
WebSocketTicketService issues UUID tickets stored in Redis with 30s TTL and validates/consumes them one-time. WebSocketTicketController exposes POST /api/v1/ws/ticket. WebSocketAuthInterceptor validates tickets on STOMP CONNECT frames. WebSocketConfig wires the STOMP broker, SockJS endpoint, and interceptor.
ExtractionProgressEvent DTO and ExtractionProgressService
ocrprocessing/dto/response/ExtractionProgressEvent.java, ocrprocessing/service/ExtractionProgressService.java
Defines ExtractionProgressEvent with nested ProgressEventType enum covering full upload/extraction lifecycle. ExtractionProgressService delivers events via SimpMessagingTemplate.convertAndSendToUser with scaled progress values (0–50% upload, 55–95% extraction, 100% pipeline complete).
StreamingUploadPipeline and response DTO
documents/dto/response/StreamingUploadResponse.java, documents/service/StreamingUploadPipeline.java
StreamingUploadResponse DTO carries collectionId and wsSubscriptionTopic for the 202 response. StreamingUploadPipeline creates a DocumentCollection in PROCESSING state, concurrently uploads files via CompletableFuture, emits per-file progress, optionally publishes OcrRequestedEvent, then transactionally finalizes collection status and storage allocation.
DocumentController and OcrDocumentController async refactor
documents/controller/DocumentController.java, ocrprocessing/controller/OcrDocumentController.java
Both /upload and /extract-all endpoints now validate file size and storage synchronously, generate collectionId upfront, delegate to StreamingUploadPipeline.processUploadAsync, and return 202 ACCEPTED with StreamingUploadResponse.
OCR service resilience: circuit breakers, bulkhead, and S3 retry
ocrprocessing/service/OcrProcessingService.java, ocrprocessing/utils/FileStorageService.java, ocrprocessing/events/OcrRequestedEvent.java, ocrprocessing/events/OcrEventMapper.java
OcrProcessingService routes all provider calls through executeWithCircuitBreaker (selects by provider type) and adds @Bulkhead on processOcr. FileStorageService gains @CircuitBreaker+@Retry with handleUploadFallback. OcrRequestedEvent adds userId; OcrEventMapper ignores it in MapStruct mappings.
OcrMessageListener and ProcessOcr progress wiring
ocrprocessing/events/OcrMessageListener.java, ocrprocessing/service/ProcessOcr.java
OcrMessageListener sends sendExtractionCompleted/sendExtractionFailed after Kafka message handling. ProcessOcr extracts userId/fileName earlier for progress notifications on started/completed/failed paths, and uses the renamed sanitizer field throughout.

Sequence Diagram

sequenceDiagram
  participant Client
  participant WebSocketTicketController
  participant WebSocketTicketService
  participant Redis
  participant DocumentController
  participant StreamingUploadPipeline
  participant ExtractionProgressService
  participant OcrMessageListener

  rect rgba(70, 130, 180, 0.5)
    note over Client,Redis: Ticket issuance
    Client->>WebSocketTicketController: POST /api/v1/ws/ticket
    WebSocketTicketController->>WebSocketTicketService: issueTicket(userId)
    WebSocketTicketService->>Redis: SET ws:ticket:{uuid} EX 30
    WebSocketTicketController-->>Client: {ticket, wsUrl}
  end

  rect rgba(60, 179, 113, 0.5)
    note over Client,StreamingUploadPipeline: Async upload
    Client->>DocumentController: POST /upload (files)
    DocumentController->>StreamingUploadPipeline: processUploadAsync(files, collectionId, autoExtract=false)
    DocumentController-->>Client: 202 {collectionId, wsSubscriptionTopic}
    StreamingUploadPipeline->>ExtractionProgressService: sendUploadStarted
    loop each file
      StreamingUploadPipeline->>ExtractionProgressService: sendFileUploaded / sendFileUploadFailed
    end
    StreamingUploadPipeline->>ExtractionProgressService: sendPipelineCompleted
  end

  rect rgba(210, 105, 30, 0.5)
    note over OcrMessageListener,ExtractionProgressService: OCR extraction progress
    OcrMessageListener->>ExtractionProgressService: sendExtractionCompleted / sendExtractionFailed
    ExtractionProgressService-->>Client: STOMP /user/queue/extraction-progress
  end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Poem

🐇 Hoppity-hop, no more waiting in line,
Files fly async, a 202 is fine!
A ticket for WebSockets, Redis keeps the key,
Circuit breakers guard OCR with glee.
Progress events stream, the pipeline's complete —
This rabbit's proud of such a streaming feat! 🎉


Note

🎁 Summarized by CodeRabbit Free

Your organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://app.coderabbit.ai/login.

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown
Contributor

⚠️ CodeQL security scan failed. Please check the workflow logs.

Integer startPage,
Integer endPage,
List<Integer> pages,
User user,
@aniebietafia
aniebietafia merged commit 4352b99 into main Jun 28, 2026
12 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant