-
Notifications
You must be signed in to change notification settings - Fork 118
[controller][schema] Coerce legacy numeric defaults during store migration #2802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xunyin8
wants to merge
6
commits into
linkedin:main
Choose a base branch
from
xunyin8:StoreMigrationRejectedDueToLegacySchema
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8d7223c
[controller][schema] Coerce legacy numeric defaults during store migr…
xunyin8 05bca13
Fix spotbugs issue
xunyin8 fe09cad
[schema] Add branch-coverage tests for AvroSchemaParseUtils
xunyin8 124be12
Fix spotbugs ES_COMPARING_STRINGS_WITH_EQ in AvroSchemaParseUtils tests
xunyin8 e3ff730
[schema] Pin DoubleNode-on-float strict-parse behavior
xunyin8 9c88232
[controller] Preserve original strict failure during migration coercion
xunyin8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
177 changes: 177 additions & 0 deletions
177
...nice-client-common/src/test/java/com/linkedin/venice/schema/TestAvroSchemaParseUtils.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| package com.linkedin.venice.schema; | ||
|
|
||
| import com.linkedin.venice.exceptions.VeniceException; | ||
| import org.apache.avro.Schema; | ||
| import org.testng.Assert; | ||
| import org.testng.annotations.Test; | ||
|
|
||
|
|
||
| /** | ||
| * Branch-coverage tests for {@link AvroSchemaParseUtils}. | ||
| * | ||
| * Lives in {@code venice-client-common} (alongside the class under test) so coverage tooling on | ||
| * this module sees the exercised branches. There is a similar test file in {@code venice-push-job} | ||
| * that covers a few overlapping cases but doesn't count toward this module's diff coverage. | ||
| */ | ||
| public class TestAvroSchemaParseUtils { | ||
| // STRICT-clean record schema. | ||
| private static final String CLEAN_RECORD_SCHEMA = | ||
| "{\"type\":\"record\",\"name\":\"Clean\",\"fields\":[" + "{\"name\":\"v\",\"type\":\"string\"}]}"; | ||
|
|
||
| // Legacy: int default on a float field — fails STRICT (numeric tier), accepted by LOOSE_NUMERICS. | ||
| private static final String LEGACY_INT_ON_FLOAT = "{\"type\":\"record\",\"name\":\"Scores\",\"fields\":[" | ||
| + "{\"name\":\"score\",\"type\":\"float\",\"default\":0}]}"; | ||
|
|
||
| // Union default whose JSON tier matches the second branch, not the first — STRICT failure outside | ||
| // the numeric-default tier. LOOSE_NUMERICS must still reject this. | ||
| private static final String UNION_DEFAULT_NOT_FIRST_BRANCH = "{\"type\":\"record\",\"name\":\"BadUnion\",\"fields\":[" | ||
| + "{\"name\":\"f\",\"type\":[\"int\",\"null\"],\"default\":null}]}"; | ||
|
|
||
| // Schema mixing all four numeric tiers with mismatched defaults — exercises every case in the | ||
| // coerceNumber switch. | ||
| private static final String ALL_NUMERIC_TIERS = "{\"type\":\"record\",\"name\":\"AllTiers\",\"fields\":[" | ||
| + "{\"name\":\"f\",\"type\":\"float\",\"default\":0}," + "{\"name\":\"d\",\"type\":\"double\",\"default\":1}," | ||
| + "{\"name\":\"l\",\"type\":\"long\",\"default\":2.0}," + "{\"name\":\"i\",\"type\":\"int\",\"default\":3.0}]}"; | ||
|
|
||
| // ----- parseSchemaFromJSONStrictValidation ---------------------------------------------------- | ||
|
|
||
| @Test | ||
| public void strictAcceptsCleanSchema() { | ||
| Schema s = AvroSchemaParseUtils.parseSchemaFromJSONStrictValidation(CLEAN_RECORD_SCHEMA); | ||
| Assert.assertEquals(s.getType(), Schema.Type.RECORD); | ||
| } | ||
|
|
||
| @Test | ||
| public void strictRejectsLegacyIntOnFloat() { | ||
| Assert.assertThrows( | ||
| Exception.class, | ||
| () -> AvroSchemaParseUtils.parseSchemaFromJSONStrictValidation(LEGACY_INT_ON_FLOAT)); | ||
| } | ||
|
|
||
| // ----- parseSchemaFromJSONLooseNumericValidation ---------------------------------------------- | ||
|
|
||
| @Test | ||
| public void looseNumericAcceptsLegacyIntOnFloat() { | ||
| Schema s = AvroSchemaParseUtils.parseSchemaFromJSONLooseNumericValidation(LEGACY_INT_ON_FLOAT); | ||
| Assert.assertNotNull(s); | ||
| } | ||
|
|
||
| @Test | ||
| public void looseNumericStillRejectsNonNumericStrictViolations() { | ||
| // The union-default-not-first-branch case is outside the numeric-default tier this preset | ||
| // relaxes — must still throw. | ||
| Assert.assertThrows( | ||
| Exception.class, | ||
| () -> AvroSchemaParseUtils.parseSchemaFromJSONLooseNumericValidation(UNION_DEFAULT_NOT_FIRST_BRANCH)); | ||
| } | ||
|
|
||
| // ----- parseSchemaFromJSONLooseValidation ----------------------------------------------------- | ||
|
|
||
| @Test | ||
| public void looseAcceptsLegacyIntOnFloat() { | ||
| Schema s = AvroSchemaParseUtils.parseSchemaFromJSONLooseValidation(LEGACY_INT_ON_FLOAT); | ||
| Assert.assertNotNull(s); | ||
| } | ||
|
|
||
| // ----- parseSchemaFromJSON(str, extendedSchemaValidityCheckEnabled) --------------------------- | ||
|
|
||
| @Test | ||
| public void parseSchemaFromJSONStrictPassesThroughForCleanSchema() { | ||
| // Clean schema → strict parser succeeds, no fallback triggered. | ||
| Schema s = AvroSchemaParseUtils.parseSchemaFromJSON(CLEAN_RECORD_SCHEMA, true); | ||
| Assert.assertEquals(s.getType(), Schema.Type.RECORD); | ||
| } | ||
|
|
||
| @Test(expectedExceptions = VeniceException.class) | ||
| public void parseSchemaFromJSONThrowsWhenExtendedValidationOnAndStrictFails() { | ||
| AvroSchemaParseUtils.parseSchemaFromJSON(LEGACY_INT_ON_FLOAT, true); | ||
| } | ||
|
|
||
| @Test | ||
| public void parseSchemaFromJSONFallsBackToLooseWhenExtendedValidationOff() { | ||
| // Strict fails, but extendedSchemaValidityCheckEnabled=false so we fall back to LOOSE and | ||
| // return a parsed schema rather than throwing. | ||
| Schema s = AvroSchemaParseUtils.parseSchemaFromJSON(LEGACY_INT_ON_FLOAT, false); | ||
| Assert.assertNotNull(s); | ||
| } | ||
|
|
||
| // ----- coerceNumericDefaultsToFieldType ------------------------------------------------------- | ||
|
|
||
| @Test | ||
| public void doubleDefaultOnFloatFieldRoundTripsCleanly() { | ||
| // Jackson parses any JSON decimal literal (e.g. {@code 0.0}) as a DoubleNode regardless of the | ||
| // declared field type. The {@code "float"} branch of {@code coerceNumber} short-circuits on | ||
| // {@code isDouble()} — i.e. it does NOT rewrite DoubleNode-on-float to FloatNode. This test | ||
| // pins down that empirically: avro-util1's STRICT parser accepts DoubleNode-on-float, so the | ||
| // walker's short-circuit produces a strict-clean output. If avro-util1 ever tightens the float | ||
| // numeric-tier check to be symmetric (rejecting double-typed defaults on float fields), this | ||
| // test will fail and the {@code "float"} branch will need to coerce DoubleNode -> FloatNode. | ||
| String doubleOnFloat = "{\"type\":\"record\",\"name\":\"Scores\",\"fields\":[" | ||
| + "{\"name\":\"score\",\"type\":\"float\",\"default\":0.0}]}"; | ||
| String coerced = AvroSchemaParseUtils.coerceNumericDefaultsToFieldType(doubleOnFloat); | ||
| AvroSchemaParseUtils.parseSchemaFromJSONStrictValidation(coerced); | ||
| } | ||
|
|
||
| @Test | ||
| public void coerceRewritesIntDefaultOnFloatField() { | ||
| String coerced = AvroSchemaParseUtils.coerceNumericDefaultsToFieldType(LEGACY_INT_ON_FLOAT); | ||
| Assert.assertNotEquals(coerced, LEGACY_INT_ON_FLOAT, "Walker must rewrite the legacy default"); | ||
| // Round-trip: the rewritten output must pass STRICT. | ||
| AvroSchemaParseUtils.parseSchemaFromJSONStrictValidation(coerced); | ||
| } | ||
|
|
||
| @Test | ||
| public void coerceHandlesAllFourNumericTiers() { | ||
| // Walks each branch of coerceNumber's switch (float / double / int / long with mismatched JSON tier). | ||
| String coerced = AvroSchemaParseUtils.coerceNumericDefaultsToFieldType(ALL_NUMERIC_TIERS); | ||
| AvroSchemaParseUtils.parseSchemaFromJSONStrictValidation(coerced); | ||
| } | ||
|
|
||
| @Test | ||
| public void coerceReturnsInputUnchangedWhenCleanSchema() { | ||
| String coerced = AvroSchemaParseUtils.coerceNumericDefaultsToFieldType(CLEAN_RECORD_SCHEMA); | ||
| Assert.assertEquals(coerced, CLEAN_RECORD_SCHEMA, "Clean schema must short-circuit the walker"); | ||
| } | ||
|
|
||
| @Test | ||
| public void coerceLeavesNonNumericStrictViolationsAlone() { | ||
| // The walker is a numeric-default fixer only. A non-numeric STRICT violation must still trip | ||
| // strict parse on the walker's output. | ||
| String coerced = AvroSchemaParseUtils.coerceNumericDefaultsToFieldType(UNION_DEFAULT_NOT_FIRST_BRANCH); | ||
| Assert.assertThrows(Exception.class, () -> AvroSchemaParseUtils.parseSchemaFromJSONStrictValidation(coerced)); | ||
| } | ||
|
|
||
| @Test | ||
| public void coerceRecursesIntoNestedRecord() { | ||
| // Nested record with a legacy numeric default inside — exercises the recursion path through | ||
| // a non-array, non-primitive {@code type} value. | ||
| String nested = "{\"type\":\"record\",\"name\":\"Outer\",\"fields\":[" | ||
| + "{\"name\":\"inner\",\"type\":{\"type\":\"record\",\"name\":\"Inner\",\"fields\":[" | ||
| + "{\"name\":\"score\",\"type\":\"float\",\"default\":0}]}}]}"; | ||
| String coerced = AvroSchemaParseUtils.coerceNumericDefaultsToFieldType(nested); | ||
| Assert.assertNotEquals(coerced, nested); | ||
| AvroSchemaParseUtils.parseSchemaFromJSONStrictValidation(coerced); | ||
| } | ||
|
|
||
| @Test | ||
| public void coerceLeavesNonNumericDefaultsUnchanged() { | ||
| // String-typed field with a string default — not a coercion target. | ||
| String coerced = AvroSchemaParseUtils.coerceNumericDefaultsToFieldType(CLEAN_RECORD_SCHEMA); | ||
| Assert.assertEquals(coerced, CLEAN_RECORD_SCHEMA); | ||
| } | ||
|
|
||
| @Test | ||
| public void coerceLeavesNonTextualTypeUnchanged() { | ||
| // When the {@code type} is itself an object (e.g. a nested record definition) rather than a | ||
| // textual primitive name, the field-spec check skips and only recursion fires. | ||
| String schema = "{\"type\":\"record\",\"name\":\"Outer\",\"fields\":[" | ||
| + "{\"name\":\"items\",\"type\":{\"type\":\"array\",\"items\":\"string\"}}]}"; | ||
| String coerced = AvroSchemaParseUtils.coerceNumericDefaultsToFieldType(schema); | ||
| Assert.assertEquals(coerced, schema); | ||
| } | ||
|
|
||
| @Test(expectedExceptions = VeniceException.class) | ||
| public void coerceWrapsInvalidJsonAsVeniceException() { | ||
| AvroSchemaParseUtils.coerceNumericDefaultsToFieldType("{not valid json"); | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jackson always parses a JSON float literal like 0.0 into a DoubleNode, never a FloatNode. so the value.isDouble() short-circuit on the "float" branch means a legacy schema written as {"type":"float","default":0.0} would slip through without being rewritten.
The existing tests cover IntNode-on-float (e.g. "default":0) but not DoubleNode-on-float, so I can't tell from the suite alone whether avroutil1's strict parser tolerates that combo or not. Could you please add a quick test for {"type":"float","default":0.0} running through coerceNumericDefaultsToFieldType followed by parseSchemaFromJSONStrictValidation?