This is an automated email from the ASF dual-hosted git repository.
jiangmaolin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 52ce5a28122 Add database environment checker for PG/OG/H2 (#32149)
52ce5a28122 is described below
commit 52ce5a281223a1472c2b81d4a7a43a99916abe4e
Author: Raigor <[email protected]>
AuthorDate: Wed Jul 17 17:49:14 2024 +0800
Add database environment checker for PG/OG/H2 (#32149)
---
.../CheckDatabaseEnvironmentFailedException.java | 2 +-
.../MissingRequiredPrivilegeException.java | 2 +-
...tion.java => MissingRequiredUserException.java} | 12 ++--
.../UnexpectedVariableValueException.java | 2 +-
.../h2/checker/H2DatabaseEnvironmentChecker.java} | 25 ++++---
....core.checker.DialectDatabaseEnvironmentChecker | 18 +++++
.../OpenGaussDatabaseEnvironmentChecker.java | 76 ++++++++++++++++++++++
....core.checker.DialectDatabaseEnvironmentChecker | 18 +++++
.../PostgreSQLDatabaseEnvironmentChecker.java | 76 ++++++++++++++++++++++
....core.checker.DialectDatabaseEnvironmentChecker | 18 +++++
10 files changed, 230 insertions(+), 19 deletions(-)
diff --git
a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/exception/CheckDatabaseEnvironmentFailedException.java
b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/exception/CheckDatabaseEnvironmentFailedException.java
index f3c85318036..40d2f4de3ed 100644
---
a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/exception/CheckDatabaseEnvironmentFailedException.java
+++
b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/exception/CheckDatabaseEnvironmentFailedException.java
@@ -30,6 +30,6 @@ public final class CheckDatabaseEnvironmentFailedException
extends MetaDataSQLEx
private static final long serialVersionUID = 3913140870320566898L;
public CheckDatabaseEnvironmentFailedException(final SQLException cause) {
- super(XOpenSQLState.CONNECTION_EXCEPTION, 6, "Check database
environment failed.", cause);
+ super(XOpenSQLState.CONNECTION_EXCEPTION, 5, "Check database
environment failed.", cause);
}
}
diff --git
a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/exception/MissingRequiredPrivilegeException.java
b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/exception/MissingRequiredPrivilegeException.java
index 994b10c55ec..5600f116b84 100644
---
a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/exception/MissingRequiredPrivilegeException.java
+++
b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/exception/MissingRequiredPrivilegeException.java
@@ -30,6 +30,6 @@ public final class MissingRequiredPrivilegeException extends
MetaDataSQLExceptio
private static final long serialVersionUID = 3755362278200749857L;
public MissingRequiredPrivilegeException(final Collection<String>
privileges) {
- super(XOpenSQLState.PRIVILEGE_NOT_GRANTED, 5, "Missing required
privilege(s) `%s`.", privileges);
+ super(XOpenSQLState.PRIVILEGE_NOT_GRANTED, 6, "Missing required
privilege(s) `%s`.", privileges);
}
}
diff --git
a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/exception/MissingRequiredPrivilegeException.java
b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/exception/MissingRequiredUserException.java
similarity index 72%
copy from
infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/exception/MissingRequiredPrivilegeException.java
copy to
infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/exception/MissingRequiredUserException.java
index 994b10c55ec..f611d8bc31a 100644
---
a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/exception/MissingRequiredPrivilegeException.java
+++
b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/exception/MissingRequiredUserException.java
@@ -20,16 +20,14 @@ package
org.apache.shardingsphere.infra.database.core.exception;
import
org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.XOpenSQLState;
import
org.apache.shardingsphere.infra.exception.core.external.sql.type.kernel.category.MetaDataSQLException;
-import java.util.Collection;
-
/**
- * Missing required privilege exception.
+ * Missing required user exception.
*/
-public final class MissingRequiredPrivilegeException extends
MetaDataSQLException {
+public final class MissingRequiredUserException extends MetaDataSQLException {
- private static final long serialVersionUID = 3755362278200749857L;
+ private static final long serialVersionUID = -656859547059598488L;
- public MissingRequiredPrivilegeException(final Collection<String>
privileges) {
- super(XOpenSQLState.PRIVILEGE_NOT_GRANTED, 5, "Missing required
privilege(s) `%s`.", privileges);
+ public MissingRequiredUserException(final String username) {
+ super(XOpenSQLState.NOT_FOUND, 7, "User '%s' does exist.", username);
}
}
diff --git
a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/exception/UnexpectedVariableValueException.java
b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/exception/UnexpectedVariableValueException.java
index fe3cbdffb56..b60f0a8881b 100644
---
a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/exception/UnexpectedVariableValueException.java
+++
b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/exception/UnexpectedVariableValueException.java
@@ -28,6 +28,6 @@ public final class UnexpectedVariableValueException extends
MetaDataSQLException
private static final long serialVersionUID = -8481467708967951766L;
public UnexpectedVariableValueException(final String variableName, final
String expectedValue, final String actualValue) {
- super(XOpenSQLState.GENERAL_ERROR, 7, "Unexpected variable value of
'%s', required '%s', now is '%s'.", variableName, expectedValue, actualValue);
+ super(XOpenSQLState.GENERAL_ERROR, 8, "Unexpected variable value of
'%s', required '%s', now is '%s'.", variableName, expectedValue, actualValue);
}
}
diff --git
a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/exception/CheckDatabaseEnvironmentFailedException.java
b/infra/database/type/h2/src/main/java/org/apache/shardingsphere/infra/database/h2/checker/H2DatabaseEnvironmentChecker.java
similarity index 54%
copy from
infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/exception/CheckDatabaseEnvironmentFailedException.java
copy to
infra/database/type/h2/src/main/java/org/apache/shardingsphere/infra/database/h2/checker/H2DatabaseEnvironmentChecker.java
index f3c85318036..2b05baa77d7 100644
---
a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/exception/CheckDatabaseEnvironmentFailedException.java
+++
b/infra/database/type/h2/src/main/java/org/apache/shardingsphere/infra/database/h2/checker/H2DatabaseEnvironmentChecker.java
@@ -15,21 +15,28 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.database.core.exception;
+package org.apache.shardingsphere.infra.database.h2.checker;
-import
org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.XOpenSQLState;
-import
org.apache.shardingsphere.infra.exception.core.external.sql.type.kernel.category.MetaDataSQLException;
+import
org.apache.shardingsphere.infra.database.core.checker.DialectDatabaseEnvironmentChecker;
+import
org.apache.shardingsphere.infra.database.core.checker.PrivilegeCheckType;
-import java.sql.SQLException;
+import javax.sql.DataSource;
/**
- * Check database environment failed exception.
+ * Database environment checker for H2.
*/
-public final class CheckDatabaseEnvironmentFailedException extends
MetaDataSQLException {
+public final class H2DatabaseEnvironmentChecker implements
DialectDatabaseEnvironmentChecker {
- private static final long serialVersionUID = 3913140870320566898L;
+ @Override
+ public void checkPrivilege(final DataSource dataSource, final
PrivilegeCheckType privilegeCheckType) {
+ }
+
+ @Override
+ public void checkVariable(final DataSource dataSource) {
+ }
- public CheckDatabaseEnvironmentFailedException(final SQLException cause) {
- super(XOpenSQLState.CONNECTION_EXCEPTION, 6, "Check database
environment failed.", cause);
+ @Override
+ public String getDatabaseType() {
+ return "H2";
}
}
diff --git
a/infra/database/type/h2/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.checker.DialectDatabaseEnvironmentChecker
b/infra/database/type/h2/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.checker.DialectDatabaseEnvironmentChecker
new file mode 100644
index 00000000000..c405f0ce24d
--- /dev/null
+++
b/infra/database/type/h2/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.checker.DialectDatabaseEnvironmentChecker
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.infra.database.h2.checker.H2DatabaseEnvironmentChecker
diff --git
a/infra/database/type/opengauss/src/main/java/org/apache/shardingsphere/infra/database/opengauss/checker/OpenGaussDatabaseEnvironmentChecker.java
b/infra/database/type/opengauss/src/main/java/org/apache/shardingsphere/infra/database/opengauss/checker/OpenGaussDatabaseEnvironmentChecker.java
new file mode 100644
index 00000000000..c50cb1a6ee4
--- /dev/null
+++
b/infra/database/type/opengauss/src/main/java/org/apache/shardingsphere/infra/database/opengauss/checker/OpenGaussDatabaseEnvironmentChecker.java
@@ -0,0 +1,76 @@
+/*
+ * 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.shardingsphere.infra.database.opengauss.checker;
+
+import lombok.extern.slf4j.Slf4j;
+import
org.apache.shardingsphere.infra.database.core.checker.DialectDatabaseEnvironmentChecker;
+import
org.apache.shardingsphere.infra.database.core.checker.PrivilegeCheckType;
+import
org.apache.shardingsphere.infra.database.core.exception.CheckDatabaseEnvironmentFailedException;
+import
org.apache.shardingsphere.infra.database.core.exception.MissingRequiredPrivilegeException;
+import
org.apache.shardingsphere.infra.database.core.exception.MissingRequiredUserException;
+import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Collections;
+
+/**
+ * Database environment checker for openGauss.
+ */
+@Slf4j
+public final class OpenGaussDatabaseEnvironmentChecker implements
DialectDatabaseEnvironmentChecker {
+
+ private static final String SHOW_GRANTS_SQL = "SELECT * FROM pg_roles
WHERE rolname = ?";
+
+ @Override
+ public void checkPrivilege(final DataSource dataSource, final
PrivilegeCheckType privilegeCheckType) {
+ if (PrivilegeCheckType.PIPELINE != privilegeCheckType) {
+ return;
+ }
+ try (
+ Connection connection = dataSource.getConnection();
+ PreparedStatement preparedStatement =
connection.prepareStatement(SHOW_GRANTS_SQL)) {
+ DatabaseMetaData metaData = connection.getMetaData();
+ preparedStatement.setString(1, metaData.getUserName());
+ try (ResultSet resultSet = preparedStatement.executeQuery()) {
+ String username = metaData.getUserName();
+ ShardingSpherePreconditions.checkState(resultSet.next(), () ->
new MissingRequiredUserException(username));
+ String isSuperRole = resultSet.getString("rolsuper");
+ String isReplicationRole =
resultSet.getString("rolreplication");
+ String isSystemAdminRole =
resultSet.getString("rolsystemadmin");
+
ShardingSpherePreconditions.checkState("t".equalsIgnoreCase(isSuperRole) ||
"t".equalsIgnoreCase(isReplicationRole) ||
"t".equalsIgnoreCase(isSystemAdminRole),
+ () -> new
MissingRequiredPrivilegeException(Collections.singleton("REPLICATION")));
+ }
+ } catch (final SQLException ex) {
+ throw new CheckDatabaseEnvironmentFailedException(ex);
+ }
+ }
+
+ @Override
+ public void checkVariable(final DataSource dataSource) {
+ }
+
+ @Override
+ public String getDatabaseType() {
+ return "openGauss";
+ }
+}
diff --git
a/infra/database/type/opengauss/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.checker.DialectDatabaseEnvironmentChecker
b/infra/database/type/opengauss/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.checker.DialectDatabaseEnvironmentChecker
new file mode 100644
index 00000000000..4412cff08c0
--- /dev/null
+++
b/infra/database/type/opengauss/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.checker.DialectDatabaseEnvironmentChecker
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.infra.database.opengauss.checker.OpenGaussDatabaseEnvironmentChecker
diff --git
a/infra/database/type/postgresql/src/main/java/org/apache/shardingsphere/infra/database/postgresql/checker/PostgreSQLDatabaseEnvironmentChecker.java
b/infra/database/type/postgresql/src/main/java/org/apache/shardingsphere/infra/database/postgresql/checker/PostgreSQLDatabaseEnvironmentChecker.java
new file mode 100644
index 00000000000..324fbd7e0bf
--- /dev/null
+++
b/infra/database/type/postgresql/src/main/java/org/apache/shardingsphere/infra/database/postgresql/checker/PostgreSQLDatabaseEnvironmentChecker.java
@@ -0,0 +1,76 @@
+/*
+ * 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.shardingsphere.infra.database.postgresql.checker;
+
+import lombok.extern.slf4j.Slf4j;
+import
org.apache.shardingsphere.infra.database.core.checker.DialectDatabaseEnvironmentChecker;
+import
org.apache.shardingsphere.infra.database.core.checker.PrivilegeCheckType;
+import
org.apache.shardingsphere.infra.database.core.exception.CheckDatabaseEnvironmentFailedException;
+import
org.apache.shardingsphere.infra.database.core.exception.MissingRequiredPrivilegeException;
+import
org.apache.shardingsphere.infra.database.core.exception.MissingRequiredUserException;
+import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Collections;
+
+/**
+ * Database environment checker for PostgreSQL.
+ */
+@Slf4j
+public final class PostgreSQLDatabaseEnvironmentChecker implements
DialectDatabaseEnvironmentChecker {
+
+ private static final String SHOW_GRANTS_SQL = "SELECT * FROM pg_roles
WHERE rolname = ?";
+
+ @Override
+ public void checkPrivilege(final DataSource dataSource, final
PrivilegeCheckType privilegeCheckType) {
+ if (PrivilegeCheckType.PIPELINE != privilegeCheckType) {
+ return;
+ }
+ try (
+ Connection connection = dataSource.getConnection();
+ PreparedStatement preparedStatement =
connection.prepareStatement(SHOW_GRANTS_SQL)) {
+ DatabaseMetaData metaData = connection.getMetaData();
+ preparedStatement.setString(1, metaData.getUserName());
+ try (ResultSet resultSet = preparedStatement.executeQuery()) {
+ String username = metaData.getUserName();
+ ShardingSpherePreconditions.checkState(resultSet.next(), () ->
new MissingRequiredUserException(username));
+ String isSuperRole = resultSet.getString("rolsuper");
+ String isReplicationRole =
resultSet.getString("rolreplication");
+ log.info("checkPrivilege: isSuperRole: {}, isReplicationRole:
{}", isSuperRole, isReplicationRole);
+
ShardingSpherePreconditions.checkState("t".equalsIgnoreCase(isSuperRole) ||
"t".equalsIgnoreCase(isReplicationRole),
+ () -> new
MissingRequiredPrivilegeException(Collections.singleton("REPLICATION")));
+ }
+ } catch (final SQLException ex) {
+ throw new CheckDatabaseEnvironmentFailedException(ex);
+ }
+ }
+
+ @Override
+ public void checkVariable(final DataSource dataSource) {
+ }
+
+ @Override
+ public String getDatabaseType() {
+ return "PostgreSQL";
+ }
+}
diff --git
a/infra/database/type/postgresql/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.checker.DialectDatabaseEnvironmentChecker
b/infra/database/type/postgresql/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.checker.DialectDatabaseEnvironmentChecker
new file mode 100644
index 00000000000..a67dec86c20
--- /dev/null
+++
b/infra/database/type/postgresql/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.database.core.checker.DialectDatabaseEnvironmentChecker
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.infra.database.postgresql.checker.PostgreSQLDatabaseEnvironmentChecker