Skip to content

Storage - Content Validation Message Encoder Perf Updates#47158

Merged
ibrandes merged 3 commits into
Azure:feature/storage/content-validationfrom
ibrandes:content-validation/messageEncoderPerfChanges
Feb 25, 2026
Merged

Storage - Content Validation Message Encoder Perf Updates#47158
ibrandes merged 3 commits into
Azure:feature/storage/content-validationfrom
ibrandes:content-validation/messageEncoderPerfChanges

Conversation

@ibrandes

@ibrandes ibrandes commented Nov 4, 2025

Copy link
Copy Markdown
Member

📜 Overview

This PR introduces significant updates to the StructuredMessageEncoder to enhance performance and enable reactive streaming of encoded data. The changes reduce memory overhead from 3 times the size of the payload to around the same size of the payload.


🔑 Key Changes

  1. Reactive Encoding API

    • Changed the encode method signature from:
      public ByteBuffer encode(ByteBuffer unencodedBuffer)
      to:
      public Flux<ByteBuffer> encode(ByteBuffer unencodedBuffer)
    • Enables streaming of ByteBuffer segments for better memory efficiency and non-blocking I/O.
  2. Segmented Encoding Logic

    • Refactored encoding flow to emit:
      • Message header
      • Segment headers and content
      • Segment footers
      • Message footer
    • Introduced helper methods like generateMessageHeader() and generateSegmentFooter() for cleaner composition.
  3. Error Handling

    • Updated validation to return Flux.error(...) instead of throwing exceptions directly.
  4. Performance Improvements

    • Removed intermediate ByteArrayOutputStream aggregation in favor of streaming buffers.
    • Reduced unnecessary copying and allocations by emitting buffers as soon as they are ready.

📦 Other Minor Changes

  • StructuredMessageConstants.java: Added constants for default segment size and maximum encoded data length.
  • MessageEncoderTests.java: Updated tests for Flux<ByteBuffer> and added large-scale encoding test (bigEncode) for performance validation.

@github-actions github-actions Bot added the Storage Storage Service (Queues, Blobs, Files) label Nov 4, 2025
@github-actions

github-actions Bot commented Nov 4, 2025

Copy link
Copy Markdown
Contributor

API Change Check

APIView identified API level changes in this PR and created the following API reviews

com.azure:azure-storage-common

@ibrandes
ibrandes marked this pull request as ready for review November 5, 2025 18:53
@github-actions

Copy link
Copy Markdown
Contributor

Hi @ibrandes. Thank you for your interest in helping to improve the Azure SDK experience and for your contribution. We've noticed that there hasn't been recent engagement on this pull request. If this is still an active work stream, please let us know by pushing some changes or leaving a comment. Otherwise, we'll close this out in 7 days.

@github-actions github-actions Bot added the no-recent-activity There has been no recent activity on this issue. label Jan 16, 2026
@ibrandes

Copy link
Copy Markdown
Member Author

comment

@github-actions github-actions Bot removed the no-recent-activity There has been no recent activity on this issue. label Jan 16, 2026

Copilot AI 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.

Pull request overview

This PR updates the storage structured message encoding implementation to expose a reactive Flux<ByteBuffer>-based API and aims to reduce memory overhead by emitting encoded pieces (headers/content/footers) instead of aggregating into a single buffer.

Changes:

  • Updated StructuredMessageEncoder.encode to return Flux<ByteBuffer> and adjusted segment header generation logic.
  • Added new structured message constants (default segment content length and a maximum encoded-data length).
  • Updated encoder unit tests to consume Flux<ByteBuffer> and added a large-scale encoding test.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
sdk/storage/azure-storage-common/src/main/java/com/azure/storage/common/implementation/structuredmessage/StructuredMessageEncoder.java Converts encoding to a reactive Flux<ByteBuffer> API and refactors segment header generation and validation flow.
sdk/storage/azure-storage-common/src/main/java/com/azure/storage/common/implementation/structuredmessage/StructuredMessageConstants.java Adds constants for default segment content length and a maximum encoded-data length.
sdk/storage/azure-storage-common/src/test/java/com/azure/storage/common/implementation/structuredmessage/MessageEncoderTests.java Updates tests to collect bytes from the new Flux<ByteBuffer> API and adds a very large encode test.
Comments suppressed due to low confidence (1)

sdk/storage/azure-storage-common/src/main/java/com/azure/storage/common/implementation/structuredmessage/StructuredMessageEncoder.java:183

  • This implementation still buffers the entire encoded output into an in-memory List<ByteBuffer> before returning Flux.fromIterable(buffers). That means it isn’t actually streaming (and doesn’t benefit from backpressure), which contradicts the PR description’s claim of reactive streaming and reduced memory overhead. Consider emitting incrementally using Flux.generate/Flux.create (or Flux.push) so buffers can be consumed as they’re produced.
        return Flux.defer(() -> {
            List<ByteBuffer> buffers = new ArrayList<>();

            // if we are at the beginning of the message, encode message header
            if (currentContentOffset == 0) {
                buffers.add(ByteBuffer.wrap(generateMessageHeader()));
            }

            while (unencodedBuffer.hasRemaining()) {
                // if we are at the beginning of a segment's content, encode segment header
                if (currentSegmentOffset == 0) {
                    incrementCurrentSegment();
                    buffers.add(ByteBuffer.wrap(generateSegmentHeader()));
                }

                buffers.add(encodeSegmentContent(unencodedBuffer));

                // if we are at the end of a segment's content, encode segment footer
                if (currentSegmentOffset == getSegmentContentLength()) {
                    byte[] footer = generateSegmentFooter();
                    if (footer.length > 0) {
                        buffers.add(ByteBuffer.wrap(footer));
                    }
                    currentSegmentOffset = 0;
                }
            }

            // if all content has been encoded, encode message footer
            if (currentContentOffset == contentLength) {
                byte[] footer = generateMessageFooter();
                if (footer.length > 0) {
                    buffers.add(ByteBuffer.wrap(footer));
                }
            }

            return Flux.fromIterable(buffers);
        });

@ibrandes
ibrandes merged commit d0e63ae into Azure:feature/storage/content-validation Feb 25, 2026
20 checks passed
@ibrandes
ibrandes deleted the content-validation/messageEncoderPerfChanges branch February 25, 2026 23:17
ibrandes added a commit that referenced this pull request Jun 23, 2026
* Crc polynomial (#43752)

* Message encoder (#43803)

* wip

* basic message encoder logic working

* removing print statements and making slightly more readable

* fixing several bugs

* adding more tests

* adding comments for building purposes and to branch off for service testing

* wip

* wip

* working encode that takes in bytebuffer

* removing redundant 'this' and making small readability changes

* fixing flag bug, adding more comments, adjusting incorrect test case

* adding structuredmessage package in implemenation and addressing other comments

* refactoring

* adding more tests, adjusting test encoder, adding more validation

* re-ordering empty buffer return

* adding correct comments

* addressing comments and fixing ci issues

* trying to resolve spotbugs issue

* formatting

* Extracting non-decoder changes from the decoder PR (#45546)

* creating class

* replacing encoder constants with new class constants

* adding fromValue to StructuredMessageFlags

* updating module info to contain more packages

* style

* undoing module info change

* Storage Content Validation - Encoder Performance Improvements (#47531)

* adjusting encoder logic

* adjusting tests to work with encoder

* addressing copilot comments

* adding more documentation

* Storage - Content Validation Public Interface (#48074)

* public facing interface wip

* adjusting output stream constructors

* removing redundant apis

* fixing pageblob api

* bloboutputstream adjustment

* wip

* wip

* importing things correctly

* formatting

* throwing error with client logger

* fixing revapi visibility increased error

* overload adjustments

* adding final to options bags and hopefully resolving revapi error

* more edits

* more edits

* more edits

* spotless

* spotless

* more edits

* removing javadoc again

* Storage - Content Validation Message Encoder Perf Updates (#47158)

* all perf adjustments

* addressing copilot comments

* Consolidate content validation types under contentvalidation package (#48272)

* Storage - Content Validation Encoder Pipeline Implementation (#48354)

* adding perf to message encoder tests

* changing package name

* changing package name

* moving checksum algorithm to new package

* wip

* moving storageschecksumalgorithm out of impl so it can actually be used

* removing unused file

* bruh

* small cleanups

* implementation for other APIs

* test updates

* undoing silly agent changes

* async tests

* perf tests for all APIs

* new path for crc64s without accessible array

* md5 compatibility tests

* progress reporter tests

* changing upload file test to stream file

* consolidating conflicting transactional checksum logic

* addressing copilot comments

* changing policy to compute crc64 header in a non-blocking manner

* fixing crc64 policy

* addressing copilot comments

* deleting perf tests

* more tests

* addressing more copilot comments

* adding recordings and marking multi part tests as live only due to the random block ids

* removing unused tests

* updating assets

* addressing github comments

* addressing comments

* renaming missed setters

* idk

* mode resolver cleanup

* adjusting mode resolver tests

* un-deprecating compute md5

* addressing API view comments

* addressing comments

* swapping test behavior for new md5 compatibility util

* wip

* adding validation to prevent progress listener being used with content val

* adding comment about flux.generate in encoder class

* adding recordings

* removing unused imports

* Storage - Content Validation Decoder Implementation (#47016)

* adding the StructuredMessageDecoder

* adding the pipeline policy changes

* smart retry changes

* fixing smart retry impl

* smart retry changes

* smart retry changes

* smart retry changes

* smart retry changes

* adding content validation tests

* code refactoring

* fixing errors i introduced :(

* addressing review comments

* code refactoring based on latest review comments

* code refactoring based on latest review comments

* simplifying retry mechanism

* removing dead code

* addressing Kyle's review comments

* addressing latest review comments

* refactoring based on latest review comments

* refactoring based on latest review comments

* refactoring based on latest review comments

* refactoring based on latest review comments from kyle

* expanding test coverage

* removing unused imports

* recordings

* adding documentation to decoder classes

* small fixes and failure path tests

* addressing context comment

* analyze error

* removing close override from decodedresponse

* line removal

---------

Co-authored-by: Isabelle <ibrandes@microsoft.com>

* Storage Content Validation - Defer MD5 + CRC64 Val Conflict to Service (#48999)

* impl and tests

* cleanup

* updating assets

* strengthening sync assertations

* Storage Content Validation - adding failure tests for content validation decoder (#49073)

* adding failure tests for content validation decoder

* adding Decoder Random Byte Failure Case Test

* rearrange code to make it a little more human readable

* fix linting

---------

Co-authored-by: browndav <browndav@microsoft.com>

* Storage Content Validation - Audit DecodedResponse (#49147)

* Audit DecodedResponse: tighten override surface, pin UTF-8, add unit tests

* Reduce DecodedResponse to minimal override surface

Strip overrides and tests that aren't required for transparent HttpResponse behavior, leaving exactly the compiler-required abstract methods plus one discretionary override that fixes a proven base-class bug.

DecodedResponse.java:
- Remove close() override. Current consumers fully drain the body Flux, so Reactor's onComplete signals release the underlying transport; the explicit close-forwarding was defensive against a try-with-resources / status-only pattern that no caller in the codebase actually uses.
- Collapse no-arg getBodyAsString() to delegate to the charset overload, pinning UTF-8 in one place instead of duplicating the lambda.
- Final override surface (8 methods): the 7 abstract methods on HttpResponse in azure-core 1.57.1 (compiler-required) plus getBodyAsBinaryData, which is concrete in the base but seeds BinaryData with the wire Content-Length header, making BinaryData.getLength() return the encoded size (frames + CRC trailers) instead of the decoded payload size.

DecodedResponseTests.java:
- Remove closeDelegatesToWrappedResponse and closeDoesNotSubscribeToDecodedBody (no override left to validate).
- Drop the close-counting assertion from the writeBodyTo test and rename it to inheritedWriteBodyToWritesDecodedBytes; it now uses MockHttpResponse directly.
- Strengthen getBodyAsBinaryDataReportsDecodedSizeNotContentLength: assert data.getLength() equals the decoded size (post-consumption). Without the override this returns 71 instead of 7, empirically reproduced.
- Drop now-unused trackingResponse helper, AtomicInteger import.

Tests: 10 of 10 passing. Each test maps 1:1 to one override or one inherited integration; no test is redundant.

* some code cleanup

* Address Copilot PR #49147 review comments

DecodedResponse.java (comment r3220562536):
- Restore HttpResponse#getBodyAsString() base contract by switching from unconditional UTF-8 to CoreUtils.bomAwareToString(bytes, contentType), matching BufferedHttpResponse's idiom. The previous UTF-8 pin silently dropped BOM detection and Content-Type charset honoring, which callers viewing this as a generic HttpResponse expect.

DecodedResponseTests.java (comment r3220562553):
- Add inheritedGetBodyAsBinaryDataReturnsDecodedBytes: exercises the inherited HttpResponse#getBodyAsBinaryData() and asserts the bytes match the decoded payload. Uses a divergent Content-Length header to make the wire vs decoded distinction explicit and guard against header-forwarding regressions.
- Add getBodyAsStringHonorsCharsetFromContentTypeHeader: proves the bom-aware fix actually honors a charset declared in Content-Type (would fail if the previous UTF-8 pinning were still in place).
- Add getBodyAsStringDetectsUtf8BomAndStripsIt: pins the BOM-detection arm of CoreUtils.bomAwareToString.
- Update getBodyAsStringDefaultsToUtf8WhenNoCharsetSpecified docstring to describe the new bom-aware fallback semantics rather than the old unconditional UTF-8 behavior.

Tests: 12 of 12 passing.

* some code cleanup

* analyze error

---------

Co-authored-by: Isabelle <ibrandes@microsoft.com>

* Storage Content Validation - Assert Request and Response Headers for Decoder Tests (#49075)

* add checks for response headers

* fix linting

* refactor async, change messageDownloadRequestHeaders signatures

* fix methods with wrong method names

* refactor sync

* remove unnecessary helper in BlobTestBase

* reuse hasStructuredMessageDownloadRequestHeaders with List<HttpHeaders>

* remove unused imports

* apply copilot suggestions

* fix linting

* inline local variables

* remove try-finally, replace with @tempdir

* add checks for response headers

* add recording for downloadStreamWithResponseContentValidationRange

* readd recording

* rerecord recordings

* Storage Content Validation - Verify Download Progress Listener Support (#49157)

* tests and blob client changes

* addressing copilot comments

* updating assets

* Storage Content Validation - Fuzzy Upload Tests (#49031)

* tests and assets

* addressing copilot comments

* making things actually fuzzy

* cleanup

* cleanup

* addressing copilot comments

* recordings

* resolving conflicts

* updating assets

* Storage Content Validation - adding fuzzy tests for content validation decoder (#49058)

* adding fuzzy tests for content validation decoder

* adding recordings

* adding few fuzzy tests

* randomizing fuzzy test payloads

* randomizing fuzzy test payloads

* fixing code review comments

* fix spotless formatting violation in BlobContentValidationAsyncDownloadTests

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Local Merge <merge@local.tmp>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>

* Storage - Content Validation Decoder Dynamic Segment Size Tests and Large GET Behavior Notes (#49172)

* tests for dynamic segment size encoding and decoding

* cleanup

* cleanup

* Apply suggestions from code review

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* style

* cant change pr name while ci is running i guess. no-op change

* cleaning up test file

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Storage - Decoder Perf Improvements (#49205)

* refactoring

* removing unused code

* reverting unecessary changes

* reverting unecessary changes

* reverting unecessary changes

* not pre-allocating entire byte array of segment size

* renaming variable

* adding back unecessary removals

* renaming variable

* addressing copilot comments

* Storage - Content validation content-length override (#49226)

* content length override changes

* content length override changes

* removing stuff

* addressing review comments

* addressing review comments

* addressing review comments

* resolving merge conflicts

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fixing spotbugs

* fixing spellchecks

---------

Co-authored-by: Local Merge <merge@local.tmp>
Co-authored-by: Isabelle <141270045+ibrandes@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Storage - Content Validation Decoder Stress Tests (#49255)

* adding stress tests for content validation decoder

* fix TelemetryHelper, add tru to registerOberservers()

* Storage - Fix Flaky Stress Tests (#48359)

* removed enableDeterministic

* change .delete() to .deleteIfExists()

* remove Sinks.EmitFailureHandler.FAIL_FAST from CrcInputStream

- read functions had FAIL_FAST which would throw an error when  the stream had reached then end and we wanted to read from the stream again. So we removed from  both reads.
- refactor code so that the exit criteria is a tthe beginning
- refactor the emitContentInfo for dry

* prevent crashes on reattempted close on stream

- changed emitValue to tryEmitValue
- remove Sinks.EmitFailureHandler.FAIL_FAST so that multiple closes does not cause an error to be thrown

* fix telemetry so that it doesnt swallow errors

* roll back two deps because they were causing failures in the containers

- opentelemetry-runtime-telemetry-java8 from 2.24.0-alpha -> 2.15.0-alpha
- opentelemetry-logback-appender-1.0 from 2.24.0-alpha -> 2.15.0-alpha

* rollback azure-client-sdk-parent linting extensions from 1.0.0-beta.2 t0 beta.1

* revert linting extensions to beta2

* remove comments with old code

* add logging for errors

* remove catches for double close issue and okay status

* recursively delete files then delete the directory

* change to sync deletes, refactor for easier reading

* restructing share clean up so super calls only once

* incorporate copilot suggestions

* incorporate copilot suggestions

* incorporate copilot suggestions

* incorporate copilot suggestions

* fix all deletes to make sync and wrap in try-catch

* fix tests so that super.globalCleanupAsync() is only called once

* change telemetry to loggin only returns final state instead of failed retries when ultimately successful

* undo versio downgrade for linting-extensions

* Fixing spacing in error messages

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* refactor datalake delete all so that it is easier to read

* refactor runAsync in ShareScenarioBase so retry failures dont show as failures upon success

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* changes to cvdownload content, lazyload versus eagerloading

* update to TelemetryHelper based on previous cherry picked stress tests fixes

* Fix storage stress fault injector certificate trust

Export the fault injector certificate as PEM and wait for it before importing it into the Java truststore so storage stress tests fail fast instead of hitting PKIX errors.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* bump opentelemetry version

* add cv tests to App.class

* add script to delete resource groups

* update scenarios matrix with tests for cv

* fixing stress tests

* stageblocksmall scenario adjustment

* wip

* avoid depending on raw short baseName being available globally

* dep pinning

* reversion

* adding back

* wip

* storageseekablebytechannel change

* bare minimum changes

* removing perf core change

* removing redundant test file and non-blob package changes

* removing redundant test file and non-blob package changes

* renaming files and hardcoding crc64 option

* removing unused test

* removing unused imports

---------

Co-authored-by: browndav <browndav@microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Local Merge <merge@local.tmp>
Co-authored-by: Isabelle <ibrandes@microsoft.com>

* Storage - Content Validation Encoder Stress Tests (#48979)

* Add encoder stress tests

* enabling fault injector

* restoring commitblocklist

* renaming files and hardcoding crc64 option

* adding scenarios back to app.java

---------

Co-authored-by: Rabab Ibrahim <ibrahimr@microsoft.com>
Co-authored-by: Isabelle <141270045+ibrandes@users.noreply.github.com>
Co-authored-by: Isabelle <ibrandes@microsoft.com>
Co-authored-by: browndav <browndav@microsoft.com>
Co-authored-by: Local Merge <merge@local.tmp>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Storage Storage Service (Queues, Blobs, Files)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants