-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[fix](fe) Fix Paimon JDBC driver registration for JNI scans #61513
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
Open
xylaaaaa
wants to merge
4
commits into
apache:master
Choose a base branch
from
xylaaaaa:fix-paimon-jdbc-jni-driver-registration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7396bb2
[fix](fe) Fix Paimon JDBC driver registration for JNI scans
xylaaaaa ae13439
[test](regression) Cover all Paimon JDBC system tables
xylaaaaa a0ed403
[fix](fe) Address Paimon JDBC review feedback
xylaaaaa fac0362
[fix](be) Format Paimon reader conditionals
xylaaaaa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...va-extensions/java-common/src/main/java/org/apache/doris/common/jdbc/JdbcDriverUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package org.apache.doris.common.jdbc; | ||
|
|
||
| import org.apache.doris.common.classloader.JniScannerClassLoader; | ||
|
|
||
| import java.net.MalformedURLException; | ||
| import java.net.URL; | ||
| import java.net.URLClassLoader; | ||
| import java.sql.Connection; | ||
| import java.sql.Driver; | ||
| import java.sql.DriverManager; | ||
| import java.sql.DriverPropertyInfo; | ||
| import java.sql.SQLFeatureNotSupportedException; | ||
| import java.util.Set; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| public final class JdbcDriverUtils { | ||
| private static final ConcurrentHashMap<URL, ClassLoader> DRIVER_CLASS_LOADER_CACHE = new ConcurrentHashMap<>(); | ||
| private static final Set<String> REGISTERED_DRIVER_KEYS = ConcurrentHashMap.newKeySet(); | ||
|
|
||
| private JdbcDriverUtils() { | ||
| } | ||
|
|
||
| public static void registerDriver(String driverUrl, String driverClassName, ClassLoader classLoader) { | ||
| try { | ||
| URL url = new URL(driverUrl); | ||
| String driverKey = driverUrl + "#" + driverClassName; | ||
| if (!REGISTERED_DRIVER_KEYS.add(driverKey)) { | ||
| return; | ||
| } | ||
| try { | ||
| ClassLoader driverClassLoader = prepareDriverClassLoader(url, classLoader); | ||
| Class<?> loadedDriverClass = Class.forName(driverClassName, true, driverClassLoader); | ||
| Driver driver = (Driver) loadedDriverClass.getDeclaredConstructor().newInstance(); | ||
| DriverManager.registerDriver(new DriverShim(driver)); | ||
| } catch (Exception e) { | ||
| REGISTERED_DRIVER_KEYS.remove(driverKey); | ||
| throw new RuntimeException("Failed to register JDBC driver: " + driverClassName, e); | ||
| } | ||
| } catch (MalformedURLException e) { | ||
| throw new IllegalArgumentException("Invalid JDBC driver URL: " + driverUrl, e); | ||
| } | ||
| } | ||
|
|
||
| private static ClassLoader prepareDriverClassLoader(URL driverUrl, ClassLoader classLoader) { | ||
| if (classLoader instanceof JniScannerClassLoader) { | ||
| JniScannerClassLoader scannerClassLoader = (JniScannerClassLoader) classLoader; | ||
| scannerClassLoader.addURLIfAbsent(driverUrl); | ||
| return scannerClassLoader; | ||
| } | ||
| return DRIVER_CLASS_LOADER_CACHE.computeIfAbsent(driverUrl, | ||
| url -> URLClassLoader.newInstance(new URL[] {url}, classLoader)); | ||
| } | ||
|
|
||
| private static final class DriverShim implements Driver { | ||
| private final Driver delegate; | ||
|
|
||
| private DriverShim(Driver delegate) { | ||
| this.delegate = delegate; | ||
| } | ||
|
|
||
| @Override | ||
| public Connection connect(String url, java.util.Properties info) throws java.sql.SQLException { | ||
| return delegate.connect(url, info); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean acceptsURL(String url) throws java.sql.SQLException { | ||
| return delegate.acceptsURL(url); | ||
| } | ||
|
|
||
| @Override | ||
| public DriverPropertyInfo[] getPropertyInfo(String url, java.util.Properties info) | ||
| throws java.sql.SQLException { | ||
| return delegate.getPropertyInfo(url, info); | ||
| } | ||
|
|
||
| @Override | ||
| public int getMajorVersion() { | ||
| return delegate.getMajorVersion(); | ||
| } | ||
|
|
||
| @Override | ||
| public int getMinorVersion() { | ||
| return delegate.getMinorVersion(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean jdbcCompliant() { | ||
| return delegate.jdbcCompliant(); | ||
| } | ||
|
|
||
| @Override | ||
| public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException { | ||
| return delegate.getParentLogger(); | ||
| } | ||
| } | ||
| } | ||
59 changes: 59 additions & 0 deletions
59
...xtensions/paimon-scanner/src/main/java/org/apache/doris/paimon/PaimonJdbcDriverUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package org.apache.doris.paimon; | ||
|
|
||
| import org.apache.doris.common.jdbc.JdbcDriverUtils; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| final class PaimonJdbcDriverUtils { | ||
| static final String PAIMON_JDBC_DRIVER_URL = "paimon.jdbc.driver_url"; | ||
| static final String PAIMON_JDBC_DRIVER_CLASS = "paimon.jdbc.driver_class"; | ||
| static final String JDBC_DRIVER_URL = "jdbc.driver_url"; | ||
| static final String JDBC_DRIVER_CLASS = "jdbc.driver_class"; | ||
|
|
||
| private PaimonJdbcDriverUtils() { | ||
| } | ||
|
|
||
| static void registerDriverIfNeeded(Map<String, String> params, ClassLoader parentClassLoader) { | ||
| String driverUrl = firstNonBlank(params.get(PAIMON_JDBC_DRIVER_URL), params.get(JDBC_DRIVER_URL)); | ||
| if (driverUrl == null) { | ||
| return; | ||
| } | ||
| String driverClassName = firstNonBlank(params.get(PAIMON_JDBC_DRIVER_CLASS), params.get(JDBC_DRIVER_CLASS)); | ||
| if (driverClassName == null) { | ||
| throw new IllegalArgumentException("paimon.jdbc.driver_class or jdbc.driver_class is required when " | ||
| + "paimon.jdbc.driver_url or jdbc.driver_url is specified"); | ||
| } | ||
| registerDriver(driverUrl, driverClassName, parentClassLoader); | ||
| } | ||
|
|
||
| static void registerDriver(String driverUrl, String driverClassName, ClassLoader parentClassLoader) { | ||
| JdbcDriverUtils.registerDriver(driverUrl, driverClassName, parentClassLoader); | ||
| } | ||
|
|
||
| private static String firstNonBlank(String first, String second) { | ||
| if (first != null && !first.trim().isEmpty()) { | ||
| return first; | ||
| } | ||
| if (second != null && !second.trim().isEmpty()) { | ||
| return second; | ||
| } | ||
| return null; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
...sions/paimon-scanner/src/test/java/org/apache/doris/paimon/PaimonJdbcDriverUtilsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package org.apache.doris.paimon; | ||
|
|
||
| import org.apache.doris.common.classloader.JniScannerClassLoader; | ||
|
|
||
| import org.junit.After; | ||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.sql.Driver; | ||
| import java.sql.DriverManager; | ||
| import java.sql.DriverPropertyInfo; | ||
| import java.sql.SQLFeatureNotSupportedException; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Properties; | ||
| import java.util.jar.JarEntry; | ||
| import java.util.jar.JarOutputStream; | ||
| import java.util.logging.Logger; | ||
|
|
||
| public class PaimonJdbcDriverUtilsTest { | ||
| private final List<Driver> registeredDrivers = new ArrayList<>(); | ||
| private final List<Path> tempJars = new ArrayList<>(); | ||
|
|
||
| @After | ||
| public void tearDown() throws Exception { | ||
| for (Driver driver : registeredDrivers) { | ||
| DriverManager.deregisterDriver(driver); | ||
| } | ||
| registeredDrivers.clear(); | ||
| for (Path tempJar : tempJars) { | ||
| Files.deleteIfExists(tempJar); | ||
| } | ||
| tempJars.clear(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRegisterDriverIfNeeded() throws Exception { | ||
| Path driverJar = createDriverJar(); | ||
| Map<String, String> params = new HashMap<>(); | ||
| params.put(PaimonJdbcDriverUtils.PAIMON_JDBC_DRIVER_URL, driverJar.toUri().toURL().toString()); | ||
| params.put(PaimonJdbcDriverUtils.PAIMON_JDBC_DRIVER_CLASS, DummyJdbcDriver.class.getName()); | ||
|
|
||
| JniScannerClassLoader scannerClassLoader = | ||
| new JniScannerClassLoader("paimon-test", List.of(), ClassLoader.getPlatformClassLoader()); | ||
| PaimonJdbcDriverUtils.registerDriverIfNeeded(params, scannerClassLoader); | ||
|
|
||
| Driver driver = DriverManager.getDriver("jdbc:dummy:test"); | ||
| registeredDrivers.add(driver); | ||
| Assert.assertTrue(driver.acceptsURL("jdbc:dummy:test")); | ||
|
Comment on lines
+62
to
+72
|
||
| } | ||
|
|
||
| @Test | ||
| public void testRegisterDriverIfNeededRequiresDriverClass() { | ||
| Map<String, String> params = new HashMap<>(); | ||
| params.put(PaimonJdbcDriverUtils.PAIMON_JDBC_DRIVER_URL, "file:///tmp/postgresql-42.5.0.jar"); | ||
|
|
||
| IllegalArgumentException exception = Assert.assertThrows(IllegalArgumentException.class, | ||
| () -> PaimonJdbcDriverUtils.registerDriverIfNeeded(params, getClass().getClassLoader())); | ||
| Assert.assertTrue(exception.getMessage().contains("driver_class")); | ||
| } | ||
|
|
||
| private Path createDriverJar() throws IOException { | ||
| Path jarPath = Files.createTempFile("paimon-jdbc-driver", ".jar"); | ||
| tempJars.add(jarPath); | ||
| String resourceName = DummyJdbcDriver.class.getName().replace('.', '/') + ".class"; | ||
| try (JarOutputStream jarOutputStream = new JarOutputStream(Files.newOutputStream(jarPath)); | ||
| InputStream inputStream = DummyJdbcDriver.class.getClassLoader().getResourceAsStream(resourceName)) { | ||
| Assert.assertNotNull(inputStream); | ||
| jarOutputStream.putNextEntry(new JarEntry(resourceName)); | ||
| byte[] buffer = new byte[4096]; | ||
| int bytesRead; | ||
| while ((bytesRead = inputStream.read(buffer)) >= 0) { | ||
| jarOutputStream.write(buffer, 0, bytesRead); | ||
| } | ||
| jarOutputStream.closeEntry(); | ||
| } | ||
| return jarPath; | ||
| } | ||
|
Comment on lines
+85
to
+101
|
||
|
|
||
| public static class DummyJdbcDriver implements Driver { | ||
| @Override | ||
| public java.sql.Connection connect(String url, Properties info) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean acceptsURL(String url) { | ||
| return url != null && url.startsWith("jdbc:dummy:"); | ||
| } | ||
|
|
||
| @Override | ||
| public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) { | ||
| return new DriverPropertyInfo[0]; | ||
| } | ||
|
|
||
| @Override | ||
| public int getMajorVersion() { | ||
| return 1; | ||
| } | ||
|
|
||
| @Override | ||
| public int getMinorVersion() { | ||
| return 0; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean jdbcCompliant() { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public Logger getParentLogger() throws SQLFeatureNotSupportedException { | ||
| throw new SQLFeatureNotSupportedException("not supported"); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code duplication: This
DriverShimclass is the third identical copy in the codebase — the other two are:PaimonJdbcMetaStoreProperties.DriverShim(FE side, line 222)IcebergJdbcMetaStoreProperties.DriverShim(FE side, line 191)Since
JdbcDriverUtilswas explicitly created as a shared utility class, consider also consolidating the FE-side copies to reduce maintenance burden. (The FE copies live in a different classloader context, but theDriverShimclass itself is trivially shareable — it's a pure delegation wrapper with no classloader-specific behavior.)This is a minor improvement suggestion, not a blocking issue.