olingo-odata2 icon indicating copy to clipboard operation
olingo-odata2 copied to clipboard

Memory Optimization for Batch Request Processing

Open ruchalzi opened this issue 1 year ago • 0 comments

Memory Optimization for Batch Request Processing

Problem Statement The previous implementation of batch request processing in Apache Olingo OData2 stored entire request bodies in memory as byte buffers. With large batch requests, this led to significant memory consumption and could potentially cause OutOfMemoryError in extreme cases.

Solution Introduced a hybrid mechanism that dynamically switches between memory buffering and temporary files:

Small requests (< 64 KB): Remain in memory for optimal performance Large requests (≥ 64 KB): Automatically spilled to temporary files Key Changes

  1. New BatchInputResource Class Introduced a wrapper class for InputStream with size information, enabling unified processing of both in-memory and file-based data.

  2. Modified BatchHelper.BodyBuilder Activation threshold: 64 KB (8 × 8192 bytes) Automatic switching to temporary files when threshold is exceeded Configurable temporary directory via olingo.tmpdir system property

  3. Automatic Resource Cleanup The DeleteOnCloseFileInputStream class automatically deletes temporary files when the stream is closed, ensuring no leftover files in the filesystem.

  4. API Changes Replaced methods:

~~getBody()~~ / ~~getBodyAsBytes()~~ → getBatchInputResource() Benefits ✅ Dramatically reduced heap memory usage for large batch requests ✅ Eliminates risk of OutOfMemoryError with massive batch operations ✅ Performance preserved for small requests (remain in memory) ✅ Automatic lifecycle management of temporary files ✅ Backward compatibility - existing tests pass successfully

Technical Details Temporary file location: Configurable via -Dolingo.tmpdir=/custom/path or defaults to java.io.tmpdir Switching threshold: 65,536 bytes (8 × 8192) File naming pattern: odata*.olingo in temporary directory Modified Files BatchInputResource.java - new class DeleteOnCloseFileInputStream.java - new class BatchChangeSetPart.java - API changes BatchChangeSetPartImpl.java - new API implementation BatchHelper.java - main hybrid buffering logic BatchRequestWriter.java - usage of new API Testing All existing batch-related tests pass without modifications, confirming backward compatibility of the implementation.

ruchalzi avatar Apr 15 '24 12:04 ruchalzi