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

jshao pushed a commit to branch branch-0.6
in repository https://gitbox.apache.org/repos/asf/gravitino.git


The following commit(s) were added to refs/heads/branch-0.6 by this push:
     new 72018aa97 [3844]fix(catalog-mysql) Add the version checking for the 
MYSQL JDBC driver (#4510)
72018aa97 is described below

commit 72018aa97ace1600e8d66a2a63dd4a2a7abbeffe
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Wed Aug 14 16:02:49 2024 +0800

    [3844]fix(catalog-mysql) Add the version checking for the MYSQL JDBC driver 
(#4510)
    
    ### What changes were proposed in this pull request?
    
    Add the version checking for the MYSQL JDBC driver, if failed throw an
    exception
    
    ### Why are the changes needed?
    
    Fix: #3844
    
    ### Does this PR introduce _any_ user-facing change?
    
    NO
    
    ### How was this patch tested?
    
    UT and manually test
    
    Co-authored-by: Yuhui <[email protected]>
---
 .../catalog/jdbc/JdbcCatalogOperations.java        | 48 ++++++++++++++++++++++
 .../MySQLProtocolCompatibleCatalogOperations.java  | 14 +++++++
 .../catalog/mysql/operation/TestMysql.java         |  2 +-
 .../operation/TestMysqlCatalogOperations.java      | 37 +++++++++++++++++
 4 files changed, 100 insertions(+), 1 deletion(-)

diff --git 
a/catalogs/catalog-jdbc-common/src/main/java/org/apache/gravitino/catalog/jdbc/JdbcCatalogOperations.java
 
b/catalogs/catalog-jdbc-common/src/main/java/org/apache/gravitino/catalog/jdbc/JdbcCatalogOperations.java
index 4d2747a66..cbbbd469e 100644
--- 
a/catalogs/catalog-jdbc-common/src/main/java/org/apache/gravitino/catalog/jdbc/JdbcCatalogOperations.java
+++ 
b/catalogs/catalog-jdbc-common/src/main/java/org/apache/gravitino/catalog/jdbc/JdbcCatalogOperations.java
@@ -22,6 +22,8 @@ import static 
org.apache.gravitino.connector.BaseCatalog.CATALOG_BYPASS_PREFIX;
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Maps;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
 import java.sql.Driver;
 import java.sql.DriverManager;
 import java.sql.SQLException;
@@ -54,6 +56,7 @@ import org.apache.gravitino.connector.CatalogInfo;
 import org.apache.gravitino.connector.CatalogOperations;
 import org.apache.gravitino.connector.HasPropertyMetadata;
 import org.apache.gravitino.connector.SupportsSchemas;
+import org.apache.gravitino.exceptions.GravitinoRuntimeException;
 import org.apache.gravitino.exceptions.NoSuchCatalogException;
 import org.apache.gravitino.exceptions.NoSuchSchemaException;
 import org.apache.gravitino.exceptions.NoSuchTableException;
@@ -98,6 +101,20 @@ public class JdbcCatalogOperations implements 
CatalogOperations, SupportsSchemas
 
   private final JdbcColumnDefaultValueConverter columnDefaultValueConverter;
 
+  public static class JDBCDriverInfo {
+    public String name;
+    public String version;
+    public int majorVersion;
+    public int minorVersion;
+
+    public JDBCDriverInfo(String driverName, String version, int majorVersion, 
int minorVersion) {
+      this.name = driverName;
+      this.version = version;
+      this.majorVersion = majorVersion;
+      this.minorVersion = minorVersion;
+    }
+  }
+
   /**
    * Constructs a new instance of JdbcCatalogOperations.
    *
@@ -148,6 +165,8 @@ public class JdbcCatalogOperations implements 
CatalogOperations, SupportsSchemas
 
     JdbcConfig jdbcConfig = new JdbcConfig(resultConf);
     this.dataSource = DataSourceUtils.createDataSource(jdbcConfig);
+
+    checkJDBCDriverVersion();
     this.databaseOperation.initialize(dataSource, exceptionConverter, 
resultConf);
     this.tableOperation.initialize(
         dataSource, exceptionConverter, jdbcTypeConverter, 
columnDefaultValueConverter, resultConf);
@@ -486,6 +505,35 @@ public class JdbcCatalogOperations implements 
CatalogOperations, SupportsSchemas
     return loadTable(NameIdentifier.of(tableIdent.namespace(), 
renameTable.getNewName()));
   }
 
+  /**
+   * Get the JDBC driver name and version
+   *
+   * @return Returns the JDBC driver info
+   */
+  public JDBCDriverInfo getDiverInfo() {
+    try (Connection conn = dataSource.getConnection()) {
+      DatabaseMetaData metaData = conn.getMetaData();
+      return new JDBCDriverInfo(
+          metaData.getDriverName(),
+          metaData.getDriverVersion(),
+          metaData.getDriverMajorVersion(),
+          metaData.getDriverMinorVersion());
+    } catch (final SQLException se) {
+      throw new GravitinoRuntimeException(
+          se, "Failed to get JDBC driver information %s: ", se.getMessage());
+    }
+  }
+
+  /**
+   * Check the version of JDBC driver can supported.
+   *
+   * @return Returns the result of checking the jdbc driver version. If 
success return true,
+   *     otherwise throw a RuntimeException
+   */
+  public boolean checkJDBCDriverVersion() {
+    return true;
+  }
+
   private Table internalAlterTable(NameIdentifier tableIdent, TableChange... 
changes)
       throws NoSuchTableException, IllegalArgumentException {
     String databaseName = 
NameIdentifier.of(tableIdent.namespace().levels()).name();
diff --git 
a/catalogs/catalog-jdbc-common/src/main/java/org/apache/gravitino/catalog/jdbc/MySQLProtocolCompatibleCatalogOperations.java
 
b/catalogs/catalog-jdbc-common/src/main/java/org/apache/gravitino/catalog/jdbc/MySQLProtocolCompatibleCatalogOperations.java
index 4253301e5..35f3a659c 100644
--- 
a/catalogs/catalog-jdbc-common/src/main/java/org/apache/gravitino/catalog/jdbc/MySQLProtocolCompatibleCatalogOperations.java
+++ 
b/catalogs/catalog-jdbc-common/src/main/java/org/apache/gravitino/catalog/jdbc/MySQLProtocolCompatibleCatalogOperations.java
@@ -33,6 +33,8 @@ public class MySQLProtocolCompatibleCatalogOperations extends 
JdbcCatalogOperati
   private static final Logger LOG =
       LoggerFactory.getLogger(MySQLProtocolCompatibleCatalogOperations.class);
 
+  private static final int MYSQL_JDBC_DRIVER_MINIMAL_SUPPORT_VERSION = 8;
+
   public MySQLProtocolCompatibleCatalogOperations(
       JdbcExceptionConverter exceptionConverter,
       JdbcTypeConverter jdbcTypeConverter,
@@ -47,6 +49,18 @@ public class MySQLProtocolCompatibleCatalogOperations 
extends JdbcCatalogOperati
         columnDefaultValueConverter);
   }
 
+  @Override
+  public boolean checkJDBCDriverVersion() {
+    JDBCDriverInfo driverInfo = getDiverInfo();
+    if (driverInfo.majorVersion < MYSQL_JDBC_DRIVER_MINIMAL_SUPPORT_VERSION) {
+      throw new RuntimeException(
+          String.format(
+              "Mysql catalog does not support the jdbc driver version %s, 
minimal required version is 8.0",
+              driverInfo.version));
+    }
+    return true;
+  }
+
   @Override
   public void close() {
     super.close();
diff --git 
a/catalogs/catalog-jdbc-mysql/src/test/java/org/apache/gravitino/catalog/mysql/operation/TestMysql.java
 
b/catalogs/catalog-jdbc-mysql/src/test/java/org/apache/gravitino/catalog/mysql/operation/TestMysql.java
index 687ef1073..417678e79 100644
--- 
a/catalogs/catalog-jdbc-mysql/src/test/java/org/apache/gravitino/catalog/mysql/operation/TestMysql.java
+++ 
b/catalogs/catalog-jdbc-mysql/src/test/java/org/apache/gravitino/catalog/mysql/operation/TestMysql.java
@@ -58,7 +58,7 @@ public class TestMysql extends TestJdbc {
         Collections.emptyMap());
   }
 
-  private static Map<String, String> getMySQLCatalogProperties() throws 
SQLException {
+  protected static Map<String, String> getMySQLCatalogProperties() throws 
SQLException {
     Map<String, String> catalogProperties = Maps.newHashMap();
 
     MySQLContainer mySQLContainer = containerSuite.getMySQLContainer();
diff --git 
a/catalogs/catalog-jdbc-mysql/src/test/java/org/apache/gravitino/catalog/mysql/operation/TestMysqlCatalogOperations.java
 
b/catalogs/catalog-jdbc-mysql/src/test/java/org/apache/gravitino/catalog/mysql/operation/TestMysqlCatalogOperations.java
new file mode 100644
index 000000000..f428c39a8
--- /dev/null
+++ 
b/catalogs/catalog-jdbc-mysql/src/test/java/org/apache/gravitino/catalog/mysql/operation/TestMysqlCatalogOperations.java
@@ -0,0 +1,37 @@
+/*
+ * 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.gravitino.catalog.mysql.operation;
+
+import java.sql.SQLException;
+import 
org.apache.gravitino.catalog.jdbc.MySQLProtocolCompatibleCatalogOperations;
+import org.apache.gravitino.catalog.mysql.MysqlCatalog;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+
+@Tag("gravitino-docker-test")
+public class TestMysqlCatalogOperations extends TestMysql {
+
+  @Test
+  public void testCheckJDBCDriver() throws SQLException {
+    MySQLProtocolCompatibleCatalogOperations catalogOperations =
+        new MySQLProtocolCompatibleCatalogOperations(
+            null, null, DATABASE_OPERATIONS, TABLE_OPERATIONS, null);
+    catalogOperations.initialize(getMySQLCatalogProperties(), null, new 
MysqlCatalog());
+  }
+}

Reply via email to