Skip to content

Commit

Permalink
Clear Avro cache on reload
Browse files Browse the repository at this point in the history
Fixes #18776
  • Loading branch information
stuartwdouglas committed Jul 28, 2021
1 parent 83a5b04 commit 60b3f6e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,27 @@
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.DotName;

import io.quarkus.avro.runtime.AvroRecorder;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
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
@Record(ExecutionTime.STATIC_INIT)
void clearCaches(AvroRecorder recorder, LaunchModeBuildItem launchModeBuildItem) {
if (launchModeBuildItem.getLaunchMode().isDevOrTest()) {
recorder.clearStaticCaches();
}
}

@BuildStep
public void build(CombinedIndexBuildItem indexBuildItem,
BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.quarkus.avro.runtime;

import java.lang.reflect.Field;
import java.util.Map;

import org.apache.avro.specific.SpecificData;
import org.jboss.logging.Logger;

import io.quarkus.runtime.annotations.Recorder;

@Recorder
public class AvroRecorder {

private static final Logger log = Logger.getLogger(AvroRecorder.class);

public void clearStaticCaches() {
try {
Field instanceField = SpecificData.class.getDeclaredField("INSTANCE");
instanceField.setAccessible(true);
SpecificData data = (SpecificData) instanceField.get(null);
Field classCache = SpecificData.class.getDeclaredField("classCache");
classCache.setAccessible(true);
Map<String, Class> classCacheMap = (Map<String, Class>) classCache.get(data);
classCacheMap.clear();
} catch (Throwable t) {
log.error("Failed to clear Avro cache", t);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkus.avro.graal;
package io.quarkus.avro.runtime.graal;

import java.lang.reflect.Constructor;
import java.util.HashMap;
Expand Down

0 comments on commit 60b3f6e

Please sign in to comment.