-
Notifications
You must be signed in to change notification settings - Fork 422
Draft: feat(format): schema evolution for the Java row codec #3714
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
Draft
stevenschlansker
wants to merge
36
commits into
apache:main
Choose a base branch
from
stevenschlansker:row-codec-schema-versions
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.
Draft
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
fa37f00
feat(format): schema evolution for the Java row codec
1595c0a
perf(format): one allocation per encode in evolution-enabled array/ma…
e4db3ac
test(format): add row-format allocation probe
594fd80
feat(format): dispatch nested versioned beans by recursive strict hash
1d6d526
fix(format): route inner-bean version through array/map projection co…
94e5033
test(format): cover producer/consumer flag asymmetry on array and map…
7fbfe09
fix(format): prefer all-current combination when SchemaHistory signat…
bf6225c
test(format): cover @ForyVersion on record components
a50a454
docs(format): clarify wire format and cross-product growth
286a844
docs(format): tighten row-format schema-evolution prose
d573042
fix(format): harden evolution decode and address review feedback
5935ae8
test(format): add JMH schema-evolution benchmark suite
ecb433c
style(format): apply spotless to row schema-evolution sources
d75320b
fix(format): route nested versioned beans by schema identity, enumera…
d1dfc4a
fix(format): harden row decode and trim allocations on the projection…
da1ddb0
fix(format): log array/map codec generation time in the unit it reports
dd913ab
perf(format): benchmark schema-evolution flag overhead on the current…
aba4338
fix(format): gate version-history bean probe behind isBean
bda509e
fix(format): keep inferField overloads contiguous to satisfy checkstyle
2b78362
fix(format): enumerate versioned beans nested inside collection fields
c7faca0
fix(format): drop RECORD_COMPONENT from @ForyVersion target for Java …
496951d
fix(format): reject finite @ForyVersion(until) on a live field
2c79f65
fix(format): stop enumerating map keys in versioned-bean cross-product
56a87f1
refactor(format): carry resolved schema in factory instead of mutatin…
c241f59
perf(format): precompute projection RowFactory once at build time
90d54fd
test(format): add disabled reproducer for map struct-key value-evolut…
67d919f
fix(format): decode map struct keys at current schema during value pr…
4b6f992
fix(format): evolve top-level array/map whose element/value wraps a v…
8f0f9c7
refactor(format): drop unreachable live-field since/until check
318943f
docs(format): note strict-hash dispatch is a 64-bit collision boundary
9113abb
test(format): cover added reference and collection field defaults on …
46f257e
fix(format): support interface beans as map values and discover them …
14484ad
docs(format): point bean-codec missing-registration error at register…
12a54fc
fix(format): throw for non-accessor methods colliding with absent pro…
02ed848
fix(format): warn on large projection cross-product and clarify decod…
b0c5ae6
fix(format): reject @ForyVersion(since) below the first schema version
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
162 changes: 162 additions & 0 deletions
162
benchmarks/java/src/main/java/org/apache/fory/benchmark/SchemaEvolutionSuite.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,162 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.fory.benchmark; | ||
|
|
||
| import java.util.Arrays; | ||
| import org.apache.fory.format.annotation.ForyVersion; | ||
| import org.apache.fory.format.encoder.Encoders; | ||
| import org.apache.fory.format.encoder.RowEncoder; | ||
| import org.apache.fory.logging.Logger; | ||
| import org.apache.fory.logging.LoggerFactory; | ||
| import org.openjdk.jmh.Main; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
|
|
||
| /** | ||
| * Row-codec schema-evolution throughput and allocation. Pair with the JMH gc profiler ({@code -prof | ||
| * gc}) to read {@code gc.alloc.rate.norm} (bytes per op). Two comparisons matter: {@code | ||
| * currentDecode} vs {@code olderDecode} shows that decoding an older payload through a projection | ||
| * codec allocates no more than decoding the current schema, because each projection holds its | ||
| * historical schema's row layout (no per-decode rebuild); and the {@code *NoEvolution} benchmarks | ||
| * vs their evolution-on counterparts show the steady-state cost of enabling {@code | ||
| * withSchemaEvolution()} when reading and writing current-version data. | ||
| */ | ||
| public class SchemaEvolutionSuite { | ||
| private static final Logger LOG = LoggerFactory.getLogger(SchemaEvolutionSuite.class); | ||
|
|
||
| public static class PersonV1 { | ||
| String name; | ||
| int age; | ||
| } | ||
|
|
||
| public static class PersonV2 { | ||
| String name; | ||
| int age; | ||
|
|
||
| @ForyVersion(since = 2) | ||
| String email; | ||
| } | ||
|
|
||
| // Evolution-enabled codecs for the current (V2) schema; the V1 codec only produces a payload | ||
| // whose hash routes the V2 reader onto its projection path. Both standard and compact formats | ||
| // are measured: compact is where a per-projection cached row layout matters, so olderDecode vs | ||
| // currentDecode there is the parity check. | ||
| private static final RowEncoder<PersonV1> v1Codec = | ||
| Encoders.buildBeanCodec(PersonV1.class).withSchemaEvolution().build().get(); | ||
| private static final RowEncoder<PersonV2> v2Codec = | ||
| Encoders.buildBeanCodec(PersonV2.class).withSchemaEvolution().build().get(); | ||
| private static final RowEncoder<PersonV1> v1CompactCodec = | ||
| Encoders.buildBeanCodec(PersonV1.class).compactEncoding().withSchemaEvolution().build().get(); | ||
| private static final RowEncoder<PersonV2> v2CompactCodec = | ||
| Encoders.buildBeanCodec(PersonV2.class).compactEncoding().withSchemaEvolution().build().get(); | ||
|
|
||
| // Evolution-disabled codecs for the same current (V2) schema. Comparing the *NoEvolution | ||
| // benchmarks against their evolution-on counterparts isolates the steady-state cost of the | ||
| // withSchemaEvolution() flag on the common path (reading and writing current-version data): the | ||
| // 8-byte hash slot the evolution wire format adds, plus the hash compare on decode. | ||
| private static final RowEncoder<PersonV2> v2PlainCodec = | ||
| Encoders.buildBeanCodec(PersonV2.class).build().get(); | ||
| private static final RowEncoder<PersonV2> v2PlainCompactCodec = | ||
| Encoders.buildBeanCodec(PersonV2.class).compactEncoding().build().get(); | ||
|
|
||
| private static final PersonV2 person = newPerson(); | ||
| private static final byte[] currentBytes = v2Codec.encode(person); | ||
| private static final byte[] olderBytes = v1Codec.encode(newPersonV1()); | ||
| private static final byte[] currentCompactBytes = v2CompactCodec.encode(person); | ||
| private static final byte[] olderCompactBytes = v1CompactCodec.encode(newPersonV1()); | ||
| private static final byte[] plainBytes = v2PlainCodec.encode(person); | ||
| private static final byte[] plainCompactBytes = v2PlainCompactCodec.encode(person); | ||
|
|
||
| private static PersonV2 newPerson() { | ||
| PersonV2 p = new PersonV2(); | ||
| p.name = "Ada Lovelace"; | ||
| p.age = 36; | ||
| p.email = "ada@example.com"; | ||
| return p; | ||
| } | ||
|
|
||
| private static PersonV1 newPersonV1() { | ||
| PersonV1 p = new PersonV1(); | ||
| p.name = "Ada Lovelace"; | ||
| p.age = 36; | ||
| return p; | ||
| } | ||
|
|
||
| @Benchmark | ||
| public Object encode() { | ||
| return v2Codec.encode(person); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public Object currentDecode() { | ||
| return v2Codec.decode(currentBytes); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public Object olderDecode() { | ||
| return v2Codec.decode(olderBytes); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public Object compactEncode() { | ||
| return v2CompactCodec.encode(person); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public Object compactCurrentDecode() { | ||
| return v2CompactCodec.decode(currentCompactBytes); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public Object compactOlderDecode() { | ||
| return v2CompactCodec.decode(olderCompactBytes); | ||
| } | ||
|
|
||
| // Evolution-off baselines for the current path. Pair each with its evolution-on counterpart | ||
| // (encode/currentDecode and the compact variants) to read the flag's overhead. | ||
| @Benchmark | ||
| public Object encodeNoEvolution() { | ||
| return v2PlainCodec.encode(person); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public Object currentDecodeNoEvolution() { | ||
| return v2PlainCodec.decode(plainBytes); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public Object compactEncodeNoEvolution() { | ||
| return v2PlainCompactCodec.encode(person); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public Object compactCurrentDecodeNoEvolution() { | ||
| return v2PlainCompactCodec.decode(plainCompactBytes); | ||
| } | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| if (args.length == 0) { | ||
| String commandLine = | ||
| "org.apache.fory.*SchemaEvolutionSuite.* -f 3 -wi 3 -i 3 -t 1 -w 2s -r 2s -prof gc -rf csv"; | ||
| args = commandLine.split(" "); | ||
| } | ||
| LOG.info("command line: {}", Arrays.toString(args)); | ||
| Main.main(args); | ||
| } | ||
| } |
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
66 changes: 66 additions & 0 deletions
66
java/fory-format/src/main/java/org/apache/fory/format/annotation/ForySchema.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,66 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.fory.format.annotation; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * Class-level row-codec schema metadata used when the codec builder enables schema evolution. | ||
| * | ||
| * <p>Live fields without a {@link ForyVersion} annotation are treated as present from the first | ||
| * version, so a class can adopt versioning by annotating only the fields added later. | ||
| * | ||
| * <p>{@link #removedFields()} points at a class (conventionally a nested {@code interface}) whose | ||
| * accessor methods describe fields that have been removed from this bean but still appear on the | ||
| * wire in older payloads. Each method's return type is the original Java type of the removed field; | ||
| * each method must carry a {@link ForyVersion} annotation with {@code until} set, since removed | ||
| * fields have a known end-of-life version. | ||
| * | ||
| * <p>Example: | ||
| * | ||
| * <pre>{@code | ||
| * @Data | ||
| * @ForySchema(removedFields = MyBean.History.class) | ||
| * public class MyBean { | ||
| * private String name; | ||
| * | ||
| * interface History { | ||
| * @ForyVersion(until = 3) | ||
| * List<String> tags(); | ||
| * | ||
| * @ForyVersion(since = 2, until = 5) | ||
| * Map<String, Long> counters(); | ||
| * } | ||
| * } | ||
| * }</pre> | ||
| */ | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target(ElementType.TYPE) | ||
| public @interface ForySchema { | ||
| /** | ||
| * A class whose accessor methods describe historically-present-but-now-removed fields. Default | ||
| * {@code void.class} means there are no removed fields. The class is never instantiated; the | ||
| * codec reads its method signatures and annotations. | ||
| */ | ||
| Class<?> removedFields() default void.class; | ||
| } |
50 changes: 50 additions & 0 deletions
50
java/fory-format/src/main/java/org/apache/fory/format/annotation/ForyVersion.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,50 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.fory.format.annotation; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * Declares the version window in which a row-codec field is logically present. The window is | ||
| * inclusive on the left and exclusive on the right, so {@code since=2, until=5} means versions 2, | ||
| * 3, and 4. | ||
| * | ||
| * <p>Only effective when the codec builder is configured with {@code withSchemaEvolution()}; | ||
| * otherwise the annotation is ignored and the field is treated as always present. | ||
| * | ||
| * <p>May be placed on a field, an accessor method, or a record component. Record components are | ||
| * covered by {@code FIELD} and {@code METHOD} rather than {@code ElementType.RECORD_COMPONENT}: the | ||
| * compiler propagates a record-component annotation to the backing field and the accessor method | ||
| * (the targets it declares), and the codec reads the annotation from those elements. {@code | ||
| * RECORD_COMPONENT} is a JDK 16 enum constant and would break this Java 11 module at runtime, so it | ||
| * is intentionally omitted. | ||
| */ | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target({ElementType.FIELD, ElementType.METHOD}) | ||
| public @interface ForyVersion { | ||
| /** First version (inclusive) that contains this field. Defaults to the class base version. */ | ||
| int since() default 1; | ||
|
|
||
| /** First version (exclusive) that no longer contains this field. */ | ||
| int until() default Integer.MAX_VALUE; | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.