[FLINK-38449][core] Reject fractional MemorySize values with explicit error#28478
Open
Jackeyzhe wants to merge 3 commits into
Open
[FLINK-38449][core] Reject fractional MemorySize values with explicit error#28478Jackeyzhe wants to merge 3 commits into
Jackeyzhe wants to merge 3 commits into
Conversation
spuru9
reviewed
Jun 18, 2026
| private static final List<MemoryUnit> ORDERED_UNITS = | ||
| Arrays.asList(BYTES, KILO_BYTES, MEGA_BYTES, GIGA_BYTES, TERA_BYTES); | ||
|
|
||
| private static final Pattern MEMORY_SIZE_FORMAT = Pattern.compile("\\d+\\s*[a-zA-Z]*"); |
Contributor
There was a problem hiding this comment.
In the current implementation the unit can be passed as any gebbrish value. Some sort of check on unit if present would help I believe, can you take a look at the stale PR on this https://github.com/apache/flink/pull/27056/changes
Contributor
Author
There was a problem hiding this comment.
Thanks for the suggestion. I updated the patch to validate the unit part as well, and then adjusted it to build the accepted unit pattern from MemoryUnit.values() instead of duplicating the unit aliases in the regex.
I also strengthened the invalid-unit test with MemorySize.parseBytes("16 gjah").
Verified with:
./mvnw -pl flink-core-api -Dtest=MemorySizeTest test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is the purpose of the change
Currently the memory size configuration in Flink accepts numeric values with or without an explicit unit (e.g.
100,100 bytes,1k,1g,1t), but the error message returned for malformed values is confusing. When a user passes a fractional value such as1.5gor.5g, the parser first extracts the leading1and then fails on the remainder.5g(which is not a valid unit). The error surfaced to the user isNumberFormatExceptionfromLong.parseLong, which does not make the actual problem — fractional input — obvious.This change adds an explicit format check at the top of
MemorySize.parseBytesthat rejects inputs that do not match the supported shape (an integer, optionally followed by whitespace and a unit). The new error message names the actual problem ("Fractional or malformed values are not supported") and points the user to the expected format ("an integer optionally followed by a unit").This closes #FLINK-38449 .
Brief change log
Pattern-based format check at the top ofMemorySize.parseBytes(inflink-core-api/src/main/java/org/apache/flink/configuration/MemorySize.java). The regex accepts an integer followed by optional whitespace and a non-space unit suffix; inputs that do not match the supported shape are rejected with a newIllegalArgumentExceptionwhose message names the actual problem.flink-core-api/src/test/java/org/apache/flink/configuration/MemorySizeTest.javacovering the issue's headline examples (.5gand1.5g) and positive controls (100and100 bytes) to confirm that back-compat for no-unit and unit-suffixed inputs is preserved.Verifying this change
This change added tests and can be verified as follows:
./mvnw -pl flink-core-api -Dtest=MemorySizeTest testand confirm that the new fractional-input assertions pass, the existing100and100 bytespositive cases still pass, and the pre-existing1.5g-style assertion (if any) still passes../mvnw -pl flink-core-api testto confirm the broader test suite for the module still passes (no other test in the module should depend on fractional MemorySize input being accepted, but the full module test is the safer check).Does this pull request potentially affect one of the following parts:
@Public(Evolving): no (MemorySizeis annotated@Public; the change is to an internal validation path of an existing public method, not to the public method's contract — input that was previously accepted with a confusing error is still accepted with a clearer error, and input that was previously rejected with a confusing error is now rejected with a clearer error)Patternmatch, run only at configuration-parse time, not on the hot path)Documentation
Was generative AI tooling used to co-author this PR?