From 3825f433b6dc6ac658619b7d9bf9d622a3c18c4f Mon Sep 17 00:00:00 2001 From: Jingsong Date: Wed, 16 Oct 2024 13:35:20 +0800 Subject: [PATCH] [core] Add FACTORIES cache in FactoryUtil --- .../apache/paimon/factories/FactoryUtil.java | 20 ++++++++++++++----- .../org.apache.paimon.factories.Factory | 20 ------------------- 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/paimon-common/src/main/java/org/apache/paimon/factories/FactoryUtil.java b/paimon-common/src/main/java/org/apache/paimon/factories/FactoryUtil.java index 25fe559663fa..1213168e16ae 100644 --- a/paimon-common/src/main/java/org/apache/paimon/factories/FactoryUtil.java +++ b/paimon-common/src/main/java/org/apache/paimon/factories/FactoryUtil.java @@ -18,6 +18,9 @@ package org.apache.paimon.factories; +import org.apache.paimon.shade.caffeine2.com.github.benmanes.caffeine.cache.Cache; +import org.apache.paimon.shade.caffeine2.com.github.benmanes.caffeine.cache.Caffeine; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,13 +32,17 @@ /** Utility for working with {@link Factory}s. */ public class FactoryUtil { + private static final Logger LOG = LoggerFactory.getLogger(FactoryUtil.class); + private static final Cache> FACTORIES = + Caffeine.newBuilder().softValues().maximumSize(100).executor(Runnable::run).build(); + /** Discovers a factory using the given factory base class and identifier. */ @SuppressWarnings("unchecked") public static T discoverFactory( ClassLoader classLoader, Class factoryClass, String identifier) { - final List factories = discoverFactories(classLoader); + final List factories = getFactories(classLoader); final List foundFactories = factories.stream() @@ -85,7 +92,7 @@ public static T discoverFactory( public static List discoverIdentifiers( ClassLoader classLoader, Class factoryClass) { - final List factories = discoverFactories(classLoader); + final List factories = getFactories(classLoader); return factories.stream() .filter(f -> factoryClass.isAssignableFrom(f.getClass())) @@ -93,6 +100,10 @@ public static List discoverIdentifiers( .collect(Collectors.toList()); } + private static List getFactories(ClassLoader classLoader) { + return FACTORIES.get(classLoader, FactoryUtil::discoverFactories); + } + private static List discoverFactories(ClassLoader classLoader) { final Iterator serviceLoaderIterator = ServiceLoader.load(Factory.class, classLoader).iterator(); @@ -110,9 +121,8 @@ private static List discoverFactories(ClassLoader classLoader) { } catch (Throwable t) { if (t instanceof NoClassDefFoundError) { LOG.debug( - "NoClassDefFoundError when loading a " - + Factory.class.getCanonicalName() - + ". This is expected when trying to load factory but no implementation is loaded.", + "NoClassDefFoundError when loading a {}. This is expected when trying to load factory but no implementation is loaded.", + Factory.class.getCanonicalName(), t); } else { throw new RuntimeException( diff --git a/paimon-core/src/test/resources/META-INF/services/org.apache.paimon.factories.Factory b/paimon-core/src/test/resources/META-INF/services/org.apache.paimon.factories.Factory index 3b85790c7364..f3e74bb8925d 100644 --- a/paimon-core/src/test/resources/META-INF/services/org.apache.paimon.factories.Factory +++ b/paimon-core/src/test/resources/META-INF/services/org.apache.paimon.factories.Factory @@ -13,24 +13,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -org.apache.paimon.mergetree.compact.aggregate.factory.FieldBoolAndAggFactory -org.apache.paimon.mergetree.compact.aggregate.factory.FieldBoolOrAggFactory -org.apache.paimon.mergetree.compact.aggregate.factory.FieldCollectAggFactory -org.apache.paimon.mergetree.compact.aggregate.factory.FieldFirstNonNullValueAggFactory -org.apache.paimon.mergetree.compact.aggregate.factory.FieldFirstNonNullValueAggLegacyFactory -org.apache.paimon.mergetree.compact.aggregate.factory.FieldFirstValueAggFactory -org.apache.paimon.mergetree.compact.aggregate.factory.FieldHllSketchAggFactory -org.apache.paimon.mergetree.compact.aggregate.factory.FieldLastNonNullValueAggFactory -org.apache.paimon.mergetree.compact.aggregate.factory.FieldLastValueAggFactory -org.apache.paimon.mergetree.compact.aggregate.factory.FieldListaggAggFactory -org.apache.paimon.mergetree.compact.aggregate.factory.FieldMaxAggFactory -org.apache.paimon.mergetree.compact.aggregate.factory.FieldMergeMapAggFactory -org.apache.paimon.mergetree.compact.aggregate.factory.FieldMinAggFactory -org.apache.paimon.mergetree.compact.aggregate.factory.FieldNestedUpdateAggFactory -org.apache.paimon.mergetree.compact.aggregate.factory.FieldPrimaryKeyAggFactory -org.apache.paimon.mergetree.compact.aggregate.factory.FieldProductAggFactory -org.apache.paimon.mergetree.compact.aggregate.factory.FieldRoaringBitmap32AggFactory -org.apache.paimon.mergetree.compact.aggregate.factory.FieldRoaringBitmap64AggFactory -org.apache.paimon.mergetree.compact.aggregate.factory.FieldSumAggFactory -org.apache.paimon.mergetree.compact.aggregate.factory.FieldThetaSketchAggFactory org.apache.paimon.mergetree.compact.aggregate.TestCostomAggFactory \ No newline at end of file