-
Notifications
You must be signed in to change notification settings - Fork 7
bugfix: Avro reflection cache #30
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
74e7b90
apply hypertrace codestyle
laxmanchekka 3ada8df
Merge branch 'main' into avro-reflection-cache
laxmanchekka 019a692
avro builder reflection cache utility added
laxmanchekka e8bac6a
avro builder reflection cache utility used in util class
laxmanchekka 51ca9f0
Merge branch 'main' into avro-reflection-cache
laxman-traceable 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
70 changes: 70 additions & 0 deletions
70
data-model/src/main/java/org/hypertrace/core/datamodel/shared/AvroBuilderCache.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,70 @@ | ||
| package org.hypertrace.core.datamodel.shared; | ||
|
|
||
| import java.lang.reflect.Constructor; | ||
| import java.lang.reflect.Method; | ||
| import java.util.Arrays; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import javax.annotation.Nonnull; | ||
| import org.apache.avro.specific.SpecificRecord; | ||
| import org.apache.avro.specific.SpecificRecordBuilderBase; | ||
| import org.apache.commons.lang3.tuple.Pair; | ||
|
|
||
| /** | ||
| * Utility class to instantiate a new avro builder in a fast and efficient way. Default Avro | ||
| * implementation has a perf issue due to uncached reflection based implementation. For more | ||
| * details, https://issues.apache.org/jira/browse/AVRO-3048 | ||
| * | ||
| * <p>This avro perf bug is fixed in unreleased avro version (0.11.x). We can get rid of this | ||
| * utility class once we migrate to Avro 0.11.x or above | ||
| */ | ||
| @SuppressWarnings({"rawtypes", "unchecked"}) | ||
| public class AvroBuilderCache { | ||
| private static final Map< | ||
| Class<SpecificRecordBuilderBase>, Pair<Method, SpecificRecordBuilderBase>> | ||
| AVRO_BUILDER_CACHE = new ConcurrentHashMap<>(); | ||
|
|
||
| public static <T extends SpecificRecordBuilderBase> T fastNewBuilder( | ||
| Class<T> specificAvroBuilderClass) { | ||
| try { | ||
| Pair<Method, SpecificRecordBuilderBase> pair = | ||
| AVRO_BUILDER_CACHE.computeIfAbsent( | ||
| (Class<SpecificRecordBuilderBase>) specificAvroBuilderClass, | ||
| AvroBuilderCache::newBuilderFor); | ||
| return (T) pair.getLeft().invoke(null, pair.getRight()); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException( | ||
| "Unable to instantiate new builder for: " + specificAvroBuilderClass.getName(), e); | ||
| } | ||
| } | ||
|
|
||
| private static Pair<Method, SpecificRecordBuilderBase> newBuilderFor( | ||
| @Nonnull Class<SpecificRecordBuilderBase> specificAvroBuilderClass) { | ||
| try { | ||
| Class<SpecificRecord> specificAvroClass = | ||
| (Class<SpecificRecord>) specificAvroBuilderClass.getEnclosingClass(); | ||
| Method newBuilderMethod = findBuilderMethod(specificAvroClass); | ||
|
|
||
| final Constructor declaredConstructor = specificAvroBuilderClass.getDeclaredConstructor(); | ||
| declaredConstructor.setAccessible(true); | ||
| SpecificRecordBuilderBase builderModel = | ||
| (SpecificRecordBuilderBase) declaredConstructor.newInstance(); | ||
|
|
||
| return Pair.of(newBuilderMethod, builderModel); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException( | ||
| "Unable to instantiate the model builder: " + specificAvroBuilderClass.getName(), e); | ||
| } | ||
| } | ||
|
|
||
| private static Method findBuilderMethod(Class specificAvroClass) { | ||
| return Arrays.stream(specificAvroClass.getDeclaredMethods()) | ||
| .filter(method -> method.getName().equals("newBuilder")) | ||
| .filter(method -> method.getParameterTypes().length == 1) | ||
| .filter( | ||
| method -> | ||
| SpecificRecordBuilderBase.class.isAssignableFrom(method.getParameterTypes()[0])) | ||
| .findFirst() | ||
| .orElseThrow(IllegalArgumentException::new); | ||
| } | ||
| } | ||
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
8 changes: 5 additions & 3 deletions
8
data-model/src/main/java/org/hypertrace/core/datamodel/shared/trace/MetricValueCreator.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 |
|---|---|---|
| @@ -1,18 +1,20 @@ | ||
| package org.hypertrace.core.datamodel.shared.trace; | ||
|
|
||
| import static org.hypertrace.core.datamodel.shared.AvroBuilderCache.fastNewBuilder; | ||
|
|
||
| import org.hypertrace.core.datamodel.MetricValue; | ||
|
|
||
| public class MetricValueCreator { | ||
|
|
||
| public static MetricValue create(double value) { | ||
| return MetricValue.newBuilder().setValue(value).build(); | ||
| return fastNewBuilder(MetricValue.Builder.class).setValue(value).build(); | ||
| } | ||
|
|
||
| public static MetricValue create(long value) { | ||
| return MetricValue.newBuilder().setValue((double) value).build(); | ||
| return fastNewBuilder(MetricValue.Builder.class).setValue((double) value).build(); | ||
| } | ||
|
|
||
| public static MetricValue create(int value) { | ||
| return MetricValue.newBuilder().setValue((double) value).build(); | ||
| return fastNewBuilder(MetricValue.Builder.class).setValue((double) value).build(); | ||
| } | ||
| } |
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
16 changes: 16 additions & 0 deletions
16
data-model/src/test/java/org/hypertrace/core/datamodel/shared/AvroBuilderCacheTest.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,16 @@ | ||
| package org.hypertrace.core.datamodel.shared; | ||
|
|
||
| import org.hypertrace.core.datamodel.Metrics; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class AvroBuilderCacheTest { | ||
|
|
||
| @Test | ||
| public void testGetBuilderFromCache() { | ||
| Metrics.Builder builder1 = AvroBuilderCache.fastNewBuilder(Metrics.Builder.class); | ||
| Metrics.Builder builder2 = AvroBuilderCache.fastNewBuilder(Metrics.Builder.class); | ||
| Assertions.assertNotSame( | ||
| builder1, builder2, "Builders can't be same. Every builder has to be a new instance"); | ||
| } | ||
| } |
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.
Do we have to pass the instance
declaredConstructor.newInstance()? Is this class instance, right? So, it will be used by the builder of creating an object, right?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.
We are creating a default builder instance once per avro class and using it as blue print to create other builder instances to reduce the reflection overhead.