Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[MINOR] improvement(common): cache Credential ServiceLoader lookup in CredentialFactory (#12423)

YangJie1347c18ecba9
diff --git a/common/src/main/java/org/apache/gravitino/credential/CredentialFactory.java b/common/src/main/java/org/apache/gravitino/credential/CredentialFactory.java
index 0ba46301b9..9698132956 100644
--- a/common/src/main/java/org/apache/gravitino/credential/CredentialFactory.java
+++ b/common/src/main/java/org/apache/gravitino/credential/CredentialFactory.java
@@ -19,15 +19,41 @@
package org.apache.gravitino.credential;
-import com.google.common.collect.Iterables;
-import com.google.common.collect.Streams;
-import java.util.List;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Locale;
import java.util.Map;
import java.util.ServiceLoader;
-import java.util.stream.Collectors;
/** Create a specific credential according to the credential information. */
public class CredentialFactory {
+
+ /**
+ * Holder for the lazily-initialized, immutable mapping from credential type to its implementation
+ * class. {@link Credential} is a fixed, built-in SPI (all implementations ship in the {@code api}
+ * and {@code common} modules on the same class loader as this factory; custom credentials are
+ * added through {@link CredentialProvider}, not by registering new {@link Credential} services),
+ * so the {@link ServiceLoader} result is stable for the lifetime of the JVM and is safe to scan
+ * once and cache instead of on every {@link #create} call.
+ */
+ private static class CredentialClassesHolder {
+ private static final Map<String, Class<? extends Credential>> CREDENTIAL_CLASSES =
+ loadCredentialClasses();
+
+ private static Map<String, Class<? extends Credential>> loadCredentialClasses() {
+ Map<String, Class<? extends Credential>> classes = new HashMap<>();
+ for (Credential credential : ServiceLoader.load(Credential.class)) {
+ String type = credential.credentialType().toLowerCase(Locale.ROOT);
+ Class<? extends Credential> existing = classes.put(type, credential.getClass());
+ if (existing != null) {
+ throw new RuntimeException(
+ "Multiple credentials found for: " + credential.credentialType());
+ }
+ }
+ return Collections.unmodifiableMap(classes);
+ }
+ }
+
/**
* Creates a {@link Credential} instance based on the provided credential type, information, and
* expiration time.
@@ -52,18 +78,11 @@ public class CredentialFactory {
}
private static Class<? extends Credential> lookupCredential(String credentialType) {
- ServiceLoader<Credential> serviceLoader = ServiceLoader.load(Credential.class);
- List<Class<? extends Credential>> credentials =
- Streams.stream(serviceLoader.iterator())
- .filter(credential -> credentialType.equalsIgnoreCase(credential.credentialType()))
- .map(Credential::getClass)
- .collect(Collectors.toList());
- if (credentials.isEmpty()) {
+ Class<? extends Credential> credentialClz =
+ CredentialClassesHolder.CREDENTIAL_CLASSES.get(credentialType.toLowerCase(Locale.ROOT));
+ if (credentialClz == null) {
throw new RuntimeException("No credential found for: " + credentialType);
- } else if (credentials.size() > 1) {
- throw new RuntimeException("Multiple credential found for: " + credentialType);
- } else {
- return Iterables.getOnlyElement(credentials);
}
+ return credentialClz;
}
}
diff --git a/common/src/test/java/org/apache/gravitino/credential/TestCredentialFactory.java b/common/src/test/java/org/apache/gravitino/credential/TestCredentialFactory.java
index 259949ae2b..eaa7402c2a 100644
--- a/common/src/test/java/org/apache/gravitino/credential/TestCredentialFactory.java
+++ b/common/src/test/java/org/apache/gravitino/credential/TestCredentialFactory.java
@@ -236,4 +236,40 @@ public class TestCredentialFactory {
Assertions.assertEquals(jdbcPassword, jdbcCredential.jdbcPassword());
Assertions.assertEquals(expireTime, jdbcCredential.expireTimeInMs());
}
+
+ @Test
+ void testUnknownCredentialType() {
+ RuntimeException e =
+ Assertions.assertThrows(
+ RuntimeException.class,
+ () -> CredentialFactory.create("no-such-credential", ImmutableMap.of(), 0));
+ Assertions.assertEquals("No credential found for: no-such-credential", e.getMessage());
+ }
+
+ @Test
+ void testUnknownCredentialTypeMessagePreservesOriginalCasing() {
+ // Lookup is case-insensitive, but the "not found" message must echo the type as the caller
+ // supplied it, not the lower-cased lookup key.
+ RuntimeException e =
+ Assertions.assertThrows(
+ RuntimeException.class,
+ () -> CredentialFactory.create("No-Such-Credential", ImmutableMap.of(), 0));
+ Assertions.assertEquals("No credential found for: No-Such-Credential", e.getMessage());
+ }
+
+ @Test
+ void testCredentialTypeLookupIsCaseInsensitive() {
+ // The factory looks up the credential class by type case-insensitively; a differently-cased
+ // type must resolve to the same implementation.
+ Credential credential =
+ CredentialFactory.create(
+ JdbcCredential.JDBC_CREDENTIAL_TYPE.toUpperCase(java.util.Locale.ROOT),
+ ImmutableMap.of(
+ JdbcCredential.GRAVITINO_JDBC_USER,
+ "test-user",
+ JdbcCredential.GRAVITINO_JDBC_PASSWORD,
+ "test-password"),
+ 0);
+ Assertions.assertInstanceOf(JdbcCredential.class, credential);
+ }
}