Skip to content
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

Fix #9 to add config to register classes #17

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkiverse.fury.deployment;

import java.util.List;
import java.util.Optional;

import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.DotName;
Expand Down Expand Up @@ -46,11 +47,22 @@ void unremovableBeans(

@BuildStep
@Record(ExecutionTime.STATIC_INIT)
public void registerClasses(
public void registerClasses(FuryBuildTimeConfig configs,
FuryBuildItem fury, List<FurySerializerBuildItem> classes, FuryRecorder recorder) {
for (FurySerializerBuildItem item : classes) {
recorder.registerClass(fury.getFury(), item.getClazz(), item.getClassId(), item.getSerializer());
}

if (configs.registerClassNames().isPresent()) {
for (String name : configs.registerClassNames().get().split(",")) {
recorder.registerClassByName(fury.getFury(), name, -1, Optional.empty());
}
}

for (var config : configs.registerClasses().entrySet()) {
recorder.registerClassByName(fury.getFury(), config.getKey(), config.getValue().classId(),
config.getValue().serializer());
}
}

@BuildStep
Expand Down
56 changes: 56 additions & 0 deletions docs/modules/ROOT/pages/includes/quarkus-fury.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,61 @@ endif::add-copy-button-to-env-var[]
|boolean
|`true`

h|[[quarkus-fury_section_quarkus-fury-register-classes]] [.section-name.section-level0]##link:#quarkus-fury_section_quarkus-fury-register-classes[Configurations]##
h|Type
h|Default

a|icon:lock[title=Fixed at build time] [[quarkus-fury_quarkus-fury-register-classes-configuration-name-class-id]] [.property-path]##link:#quarkus-fury_quarkus-fury-register-classes-configuration-name-class-id[`quarkus.fury.register-classes."configuration-name".class-id`]##

[.description]
--
Class id must be greater or equal to 256, and it must be different between classes. The default is -1.


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_FURY_REGISTER_CLASSES__CONFIGURATION_NAME__CLASS_ID+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_FURY_REGISTER_CLASSES__CONFIGURATION_NAME__CLASS_ID+++`
endif::add-copy-button-to-env-var[]
--
|int
|`-1`

a|icon:lock[title=Fixed at build time] [[quarkus-fury_quarkus-fury-register-classes-configuration-name-serializer]] [.property-path]##link:#quarkus-fury_quarkus-fury-register-classes-configuration-name-serializer[`quarkus.fury.register-classes."configuration-name".serializer`]##

[.description]
--
Specify a customized serializer for current class. This should be empty to let Fury create serializer for current class. But if users want to customize serialization for this class, one can provide serializer here.


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_FURY_REGISTER_CLASSES__CONFIGURATION_NAME__SERIALIZER+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_FURY_REGISTER_CLASSES__CONFIGURATION_NAME__SERIALIZER+++`
endif::add-copy-button-to-env-var[]
--
|string
|


a|icon:lock[title=Fixed at build time] [[quarkus-fury_quarkus-fury-register-class-names]] [.property-path]##link:#quarkus-fury_quarkus-fury-register-class-names[`quarkus.fury.register-class-names`]##

[.description]
--
Names of classes to register.


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_FURY_REGISTER_CLASS_NAMES+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_FURY_REGISTER_CLASS_NAMES+++`
endif::add-copy-button-to-env-var[]
--
|string
|

|===

56 changes: 56 additions & 0 deletions docs/modules/ROOT/pages/includes/quarkus-fury_quarkus.fury.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,61 @@ endif::add-copy-button-to-env-var[]
|boolean
|`true`

h|[[quarkus-fury_section_quarkus-fury-register-classes]] [.section-name.section-level0]##link:#quarkus-fury_section_quarkus-fury-register-classes[Configurations]##
h|Type
h|Default

a|icon:lock[title=Fixed at build time] [[quarkus-fury_quarkus-fury-register-classes-configuration-name-class-id]] [.property-path]##link:#quarkus-fury_quarkus-fury-register-classes-configuration-name-class-id[`quarkus.fury.register-classes."configuration-name".class-id`]##

[.description]
--
Class id must be greater or equal to 256, and it must be different between classes. The default is -1.


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_FURY_REGISTER_CLASSES__CONFIGURATION_NAME__CLASS_ID+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_FURY_REGISTER_CLASSES__CONFIGURATION_NAME__CLASS_ID+++`
endif::add-copy-button-to-env-var[]
--
|int
|`-1`

a|icon:lock[title=Fixed at build time] [[quarkus-fury_quarkus-fury-register-classes-configuration-name-serializer]] [.property-path]##link:#quarkus-fury_quarkus-fury-register-classes-configuration-name-serializer[`quarkus.fury.register-classes."configuration-name".serializer`]##

[.description]
--
Specify a customized serializer for current class. This should be empty to let Fury create serializer for current class. But if users want to customize serialization for this class, one can provide serializer here.


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_FURY_REGISTER_CLASSES__CONFIGURATION_NAME__SERIALIZER+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_FURY_REGISTER_CLASSES__CONFIGURATION_NAME__SERIALIZER+++`
endif::add-copy-button-to-env-var[]
--
|string
|


a|icon:lock[title=Fixed at build time] [[quarkus-fury_quarkus-fury-register-class-names]] [.property-path]##link:#quarkus-fury_quarkus-fury-register-class-names[`quarkus.fury.register-class-names`]##

[.description]
--
Names of classes to register.


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_FURY_REGISTER_CLASS_NAMES+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_FURY_REGISTER_CLASS_NAMES+++`
endif::add-copy-button-to-env-var[]
--
|string
|

|===

Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,5 @@
import java.util.List;
import java.util.Map;

import io.quarkiverse.fury.FurySerialization;

@FurySerialization(classId = 300)
public record Foo(int f1, String f2, List<String> f3, Map<String, Long> f4) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
import java.io.Serializable;
import java.util.Objects;

import io.quarkiverse.fury.FurySerialization;

@FurySerialization
public class Struct implements Serializable {
public int f1;
public long f2;
Expand Down
2 changes: 2 additions & 0 deletions integration-tests/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkus.fury.register-class-names=io.quarkiverse.fury.it.Struct
quarkus.fury.register-classes."io.quarkiverse.fury.it.Foo".class-id=300
Copy link
Contributor

@chaokunyang chaokunyang Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you share en exmaple how to configure multiple class with class id and serializer in application.properties?

Does it look like following code? If so, I perfer name configuration key as register-class instead of register-classes

quarkus.fury.register-classes."io.quarkiverse.fury.it.Foo".class-id=300
quarkus.fury.register-classes."io.quarkiverse.fury.it.Foo".serialize=io.quarkiverse.fury.it.FooSerializer

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use register_classes instead of register-classes, will quarkus still parse config correctly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it look like following code? If so, I perfer name configuration key as register-class instead of register-classes

Yeah, it is. I can change it to register-class.

If we use register_classes instead of register-classes, will quarkus still parse config correctly?

No, it will not. I think Quarkus uses microprofile config spec.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, let's change to register-class. And it would be great if we can add a serializer config here for test

Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package io.quarkiverse.fury;

import java.util.Map;
import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigDocMapKey;
import io.quarkus.runtime.annotations.ConfigDocSection;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
Expand All @@ -15,4 +20,16 @@ public interface FuryBuildTimeConfig {
/** Whether to use thread safe fury. The default is true. */
@WithDefault("true")
boolean threadSafe();

/**
* Configurations
chaokunyang marked this conversation as resolved.
Show resolved Hide resolved
*/
@ConfigDocSection
@ConfigDocMapKey("configuration-name")
Map<String, FuryRegisterClassConfig> registerClasses();

/**
* Names of classes to register.
*/
Optional<String> registerClassNames();
}
38 changes: 33 additions & 5 deletions runtime/src/main/java/io/quarkiverse/fury/FuryRecorder.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package io.quarkiverse.fury;

import java.util.Optional;

import org.apache.fury.BaseFury;
import org.apache.fury.Fury;
import org.apache.fury.ThreadSafeFury;
import org.apache.fury.config.FuryBuilder;
import org.apache.fury.resolver.ClassResolver;
import org.apache.fury.serializer.Serializer;
import org.apache.fury.util.Preconditions;
import org.jboss.logging.Logger;

import io.quarkus.arc.runtime.BeanContainer;
import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.annotations.Recorder;

@Recorder
public class FuryRecorder {
private static final Logger LOG = Logger.getLogger(FuryRecorder.class);

public RuntimeValue<BaseFury> createFury(
final FuryBuildTimeConfig config, final BeanContainer beanContainer) {
// create the Fury instance from the config
Expand All @@ -26,6 +31,25 @@ public RuntimeValue<BaseFury> createFury(
return new RuntimeValue<>(fury);
}

public void registerClassByName(final RuntimeValue<BaseFury> furyValue, final String className, final int classId,
final Optional<String> serializerClassName) {
try {
Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
Class<? extends Serializer> serializer = null;
if (serializerClassName.isPresent()) {
serializer = (Class<? extends Serializer>) Thread.currentThread().getContextClassLoader()
.loadClass(serializerClassName.get());
}
registerClass(furyValue, clazz, classId, serializer);
} catch (ClassNotFoundException e) {
LOG.warn("can not find register class: " + className
+ (serializerClassName.isPresent()
? " with serializer: " + serializerClassName.get()
: ""));
throw new RuntimeException(e);
}
}

public void registerClass(
final RuntimeValue<BaseFury> furyValue, final Class<?> clazz,
final int classId, Class<? extends Serializer> serializer) {
Expand All @@ -40,13 +64,17 @@ public void registerClass(
if (fury instanceof ThreadSafeFury) {
ThreadSafeFury threadSafeFury = (ThreadSafeFury) fury;
registeredClass = (threadSafeFury).execute(f -> f.getClassResolver().getRegisteredClass((short) classId));
// Generate serializer bytecode.
threadSafeFury.execute(f -> f.getClassResolver().getSerializerClass(clazz));
if (serializer == null) {
// Generate serializer bytecode.
threadSafeFury.execute(f -> f.getClassResolver().getSerializerClass(clazz));
}
} else {
ClassResolver classResolver = ((Fury) fury).getClassResolver();
registeredClass = classResolver.getRegisteredClass((short) classId);
// Generate serializer bytecode.
classResolver.getSerializerClass(clazz);
if (serializer == null) {
// Generate serializer bytecode.
classResolver.getSerializerClass(clazz);
}
}
Preconditions.checkArgument(
registeredClass == null,
Expand All @@ -56,7 +84,7 @@ public void registerClass(
fury.register(clazz, (short) classId);
} else {
// Generate serializer bytecode.
fury.register(clazz, true);
fury.register(clazz, serializer == null);
}
if (serializer != null) {
fury.registerSerializer(clazz, serializer);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkiverse.fury;

import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigGroup;
import io.smallrye.config.WithDefault;

@ConfigGroup
public interface FuryRegisterClassConfig {
/**
* Class id must be greater or equal to 256, and it must be different between classes.
* The default is -1.
*/
@WithDefault("-1")
Integer classId();

/**
* Specify a customized serializer for current class.
* This should be empty to let Fury create serializer for current class. But if
* users want to customize serialization for this class, one can provide serializer here.
*/
Optional<String> serializer();

}