-
Notifications
You must be signed in to change notification settings - Fork 422
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
11
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
11 commits
Select commit
Hold shift + click to select a range
034c5c1
feat(format): schema evolution for the Java row codec
e860743
feat(format): dispatch nested versioned beans by recursive strict hash
c89e760
fix(format): enumerate versioned beans nested inside collection fields
5a3c987
fix(format): decode map struct keys at current schema during value pr…
56c2a3a
fix(format): evolve top-level array/map whose element/value wraps a v…
f21bb0b
fix(format): support interface beans as map values, discovered when n…
f48944a
feat(format): evolve map keys via a combined key/value schema hash
fd11e9f
feat(format): evolve versioned map keys nested in a row field
1a6c11d
fix(format): evolve every distinct bean reachable through a top-level…
3d2f8f1
fix(format): delimit struct children in row-codec strict schema hash
5978a5d
perf(format): generate row-codec schema projections lazily on first d…
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
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> | ||
| * @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; | ||
| } |
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.