-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimental support for Avro in native mode.
- Loading branch information
1 parent
1c416eb
commit 5c68f82
Showing
12 changed files
with
413 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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,49 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-avro-parent</artifactId> | ||
<version>999-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>quarkus-avro-deployment</artifactId> | ||
<name>Quarkus - Avro - Deployment</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-core-deployment</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-avro</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-junit5-internal</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<annotationProcessorPaths> | ||
<path> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-extension-processor</artifactId> | ||
<version>${project.version}</version> | ||
</path> | ||
</annotationProcessorPaths> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
43 changes: 43 additions & 0 deletions
43
extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroProcessor.java
This file contains 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,43 @@ | ||
package io.quarkus.avro.deployment; | ||
|
||
import java.util.Collection; | ||
|
||
import org.apache.avro.generic.GenericDatumReader; | ||
import org.apache.avro.specific.AvroGenerated; | ||
import org.jboss.jandex.AnnotationInstance; | ||
import org.jboss.jandex.AnnotationTarget; | ||
import org.jboss.jandex.DotName; | ||
|
||
import io.quarkus.deployment.annotations.BuildProducer; | ||
import io.quarkus.deployment.annotations.BuildStep; | ||
import io.quarkus.deployment.builditem.CombinedIndexBuildItem; | ||
import io.quarkus.deployment.builditem.nativeimage.NativeImageConfigBuildItem; | ||
import io.quarkus.deployment.builditem.nativeimage.NativeImageSystemPropertyBuildItem; | ||
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem; | ||
|
||
public class AvroProcessor { | ||
|
||
@BuildStep | ||
public void build(CombinedIndexBuildItem indexBuildItem, | ||
BuildProducer<ReflectiveClassBuildItem> reflectiveClass, | ||
BuildProducer<NativeImageSystemPropertyBuildItem> sys, | ||
BuildProducer<NativeImageConfigBuildItem> conf) { | ||
|
||
NativeImageConfigBuildItem.Builder builder = NativeImageConfigBuildItem.builder(); | ||
builder.addRuntimeInitializedClass(GenericDatumReader.class.getName()); | ||
|
||
Collection<AnnotationInstance> annotations = indexBuildItem.getIndex() | ||
.getAnnotations(DotName.createSimple(AvroGenerated.class.getName())); | ||
for (AnnotationInstance annotation : annotations) { | ||
if (annotation.target().kind() == AnnotationTarget.Kind.CLASS) { | ||
String className = annotation.target().asClass().name().toString(); | ||
builder.addRuntimeInitializedClass(className); | ||
reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, true, className)); | ||
} | ||
} | ||
|
||
builder.addRuntimeInitializedClass("org.apache.avro.reflect.ReflectData"); | ||
conf.produce(builder.build()); | ||
sys.produce(new NativeImageSystemPropertyBuildItem("avro.disable.unsafe", "true")); | ||
} | ||
} |
This file contains 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,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>quarkus-build-parent</artifactId> | ||
<groupId>io.quarkus</groupId> | ||
<version>999-SNAPSHOT</version> | ||
<relativePath>../../build-parent/pom.xml</relativePath> | ||
</parent> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>quarkus-avro-parent</artifactId> | ||
<name>Quarkus - Avro</name> | ||
<packaging>pom</packaging> | ||
|
||
<modules> | ||
<module>deployment</module> | ||
<module>runtime</module> | ||
</modules> | ||
</project> |
This file contains 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,56 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-avro-parent</artifactId> | ||
<version>999-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>quarkus-avro</artifactId> | ||
<name>Quarkus - Avro - Runtime</name> | ||
<description>Provide support for the Avro data serialization system</description> | ||
<dependencies> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-core</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.avro</groupId> | ||
<artifactId>avro</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.graalvm.nativeimage</groupId> | ||
<artifactId>svm</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<!-- Mark this as a runtime dependency, so to make sure it's included on the final classpath during native-image --> | ||
<plugins> | ||
<plugin> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-bootstrap-maven-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<annotationProcessorPaths> | ||
<path> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-extension-processor</artifactId> | ||
<version>${project.version}</version> | ||
</path> | ||
</annotationProcessorPaths> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
83 changes: 83 additions & 0 deletions
83
extensions/avro/runtime/src/main/java/io/quarkus/avro/graal/AvroSubstitutions.java
This file contains 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,83 @@ | ||
package io.quarkus.avro.graal; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
import org.apache.avro.generic.IndexedRecord; | ||
|
||
import com.oracle.svm.core.annotate.Alias; | ||
import com.oracle.svm.core.annotate.Inject; | ||
import com.oracle.svm.core.annotate.RecomputeFieldValue; | ||
import com.oracle.svm.core.annotate.Substitute; | ||
import com.oracle.svm.core.annotate.TargetClass; | ||
|
||
@TargetClass(className = "org.apache.avro.reflect.ReflectionUtil") | ||
final class Target_org_apache_avro_reflect_ReflectionUtil { | ||
|
||
/** | ||
* Use reflection instead of method handles | ||
*/ | ||
@Substitute | ||
public static <V, R> Function<V, R> getConstructorAsFunction(Class<V> parameterClass, Class<R> clazz) { | ||
try { | ||
Constructor<R> constructor = clazz.getConstructor(parameterClass); | ||
return new Function<V, R>() { | ||
@Override | ||
public R apply(V v) { | ||
try { | ||
return constructor.newInstance(v); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
}; | ||
} catch (Throwable t) { | ||
// if something goes wrong, do not provide a Function instance | ||
return null; | ||
} | ||
} | ||
|
||
} | ||
|
||
@TargetClass(className = "org.apache.avro.reflect.ReflectData") | ||
final class Target_org_apache_avro_reflect_ReflectData { | ||
|
||
@Inject | ||
@RecomputeFieldValue(kind = RecomputeFieldValue.Kind.None) | ||
Map<Class<?>, Target_org_apache_avro_reflect_ReflectData_ClassAccessorData> ACCESSORS; | ||
|
||
@Substitute | ||
private Target_org_apache_avro_reflect_ReflectData_ClassAccessorData getClassAccessorData(Class<?> c) { | ||
if (ACCESSORS == null) { | ||
ACCESSORS = new HashMap<>(); | ||
} | ||
|
||
Map<Class<?>, Target_org_apache_avro_reflect_ReflectData_ClassAccessorData> map = ACCESSORS; | ||
Target_org_apache_avro_reflect_ReflectData_ClassAccessorData o = map.get(c); | ||
if (o == null) { | ||
if (!IndexedRecord.class.isAssignableFrom(c)) { | ||
Target_org_apache_avro_reflect_ReflectData_ClassAccessorData d = new Target_org_apache_avro_reflect_ReflectData_ClassAccessorData( | ||
c); | ||
map.put(c, d); | ||
} | ||
return null; | ||
} | ||
return o; | ||
} | ||
} | ||
|
||
@TargetClass(className = "org.apache.avro.reflect.ReflectData", innerClass = "ClassAccessorData") | ||
final class Target_org_apache_avro_reflect_ReflectData_ClassAccessorData<T> { | ||
// Just provide access to "ReflectData.ClassAccessorData" | ||
|
||
@Alias | ||
public Target_org_apache_avro_reflect_ReflectData_ClassAccessorData(Class<?> c) { | ||
|
||
} | ||
|
||
} | ||
|
||
class AvroSubstitutions { | ||
} |
9 changes: 9 additions & 0 deletions
9
extensions/avro/runtime/src/main/resources/META-INF/quarkus-extension.yaml
This file contains 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,9 @@ | ||
--- | ||
name: "Apache Avro" | ||
metadata: | ||
keywords: | ||
- "avro" | ||
guide: "https://quarkus.io/guides/kafka" | ||
categories: | ||
- "serialization" | ||
status: "experimental" |
This file contains 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 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
Oops, something went wrong.