This is an automated email from the ASF dual-hosted git repository.

lzljs3620320 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git


The following commit(s) were added to refs/heads/master by this push:
     new 3825f433b [core] Add FACTORIES cache in FactoryUtil
3825f433b is described below

commit 3825f433b6dc6ac658619b7d9bf9d622a3c18c4f
Author: Jingsong <[email protected]>
AuthorDate: Wed Oct 16 13:35:20 2024 +0800

    [core] Add FACTORIES cache in FactoryUtil
---
 .../org/apache/paimon/factories/FactoryUtil.java     | 20 +++++++++++++++-----
 .../services/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 25fe55966..1213168e1 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 @@ import java.util.stream.Collectors;
 
 /** Utility for working with {@link Factory}s. */
 public class FactoryUtil {
+
     private static final Logger LOG = 
LoggerFactory.getLogger(FactoryUtil.class);
 
+    private static final Cache<ClassLoader, List<Factory>> 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 extends Factory> T discoverFactory(
             ClassLoader classLoader, Class<T> factoryClass, String identifier) 
{
-        final List<Factory> factories = discoverFactories(classLoader);
+        final List<Factory> factories = getFactories(classLoader);
 
         final List<Factory> foundFactories =
                 factories.stream()
@@ -85,7 +92,7 @@ public class FactoryUtil {
 
     public static <T extends Factory> List<String> discoverIdentifiers(
             ClassLoader classLoader, Class<T> factoryClass) {
-        final List<Factory> factories = discoverFactories(classLoader);
+        final List<Factory> factories = getFactories(classLoader);
 
         return factories.stream()
                 .filter(f -> factoryClass.isAssignableFrom(f.getClass()))
@@ -93,6 +100,10 @@ public class FactoryUtil {
                 .collect(Collectors.toList());
     }
 
+    private static List<Factory> getFactories(ClassLoader classLoader) {
+        return FACTORIES.get(classLoader, FactoryUtil::discoverFactories);
+    }
+
     private static List<Factory> discoverFactories(ClassLoader classLoader) {
         final Iterator<Factory> serviceLoaderIterator =
                 ServiceLoader.load(Factory.class, classLoader).iterator();
@@ -110,9 +121,8 @@ public class FactoryUtil {
             } 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 3b85790c7..f3e74bb89 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

Reply via email to