From 2f5a1eeb104907480d030207d9d65fcf77f492b3 Mon Sep 17 00:00:00 2001 From: jsbxyyx Date: Wed, 31 Jul 2024 15:08:31 +0800 Subject: [PATCH 1/8] mysql metadata --- .../seata/rm/datasource/DataSourceProxy.java | 4 ++ .../SeataDataSourceProxyMetadata.java | 33 +++++++++++ .../MySQLDataSourceProxyMetadata.java | 58 +++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadata.java create mode 100644 rm-datasource/src/main/java/org/apache/seata/rm/datasource/metadata/MySQLDataSourceProxyMetadata.java diff --git a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/DataSourceProxy.java b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/DataSourceProxy.java index e13b0b96dde..b15dd51f72b 100644 --- a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/DataSourceProxy.java +++ b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/DataSourceProxy.java @@ -31,6 +31,7 @@ import org.apache.seata.core.model.BranchType; import org.apache.seata.core.model.Resource; import org.apache.seata.rm.DefaultResourceManager; +import org.apache.seata.rm.datasource.metadata.MySQLDataSourceProxyMetadata; import org.apache.seata.rm.datasource.sql.struct.TableMetaCacheFactory; import org.apache.seata.rm.datasource.undo.UndoLogManager; import org.apache.seata.rm.datasource.undo.UndoLogManagerFactory; @@ -66,6 +67,8 @@ public class DataSourceProxy extends AbstractDataSourceProxy implements Resource private String productVersion; + private SeataDataSourceProxyMetadata dataSourceProxyMetadata; + /** * POLARDB-X 1.X -> TDDL * POLARDB-X 2.X & MySQL 5.6 -> PXC @@ -105,6 +108,7 @@ private void init(DataSource dataSource, String resourceGroupId) { if (JdbcConstants.ORACLE.equals(dbType)) { userName = connection.getMetaData().getUserName(); } else if (JdbcConstants.MYSQL.equals(dbType)) { + dataSourceProxyMetadata = new MySQLDataSourceProxyMetadata().init(dataSource); validMySQLVersion(connection); checkDerivativeProduct(); } diff --git a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadata.java b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadata.java new file mode 100644 index 00000000000..3761c538d34 --- /dev/null +++ b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadata.java @@ -0,0 +1,33 @@ +/* + * 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.seata.rm.datasource; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; + +/** + * The type Data source proxy metadata. + * + */ +public interface SeataDataSourceProxyMetadata { + + SeataDataSourceProxyMetadata init(DataSource dataSource) throws SQLException; + + String getVariableValue(String name); + +} diff --git a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/metadata/MySQLDataSourceProxyMetadata.java b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/metadata/MySQLDataSourceProxyMetadata.java new file mode 100644 index 00000000000..93c16a2c600 --- /dev/null +++ b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/metadata/MySQLDataSourceProxyMetadata.java @@ -0,0 +1,58 @@ +/* + * 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.seata.rm.datasource.metadata; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +import org.apache.seata.common.util.StringUtils; +import org.apache.seata.rm.datasource.SeataDataSourceProxyMetadata; + +/** + * mysql datasource proxy metadata + */ +public class MySQLDataSourceProxyMetadata implements SeataDataSourceProxyMetadata { + + private final Map variables = new HashMap<>(); + + @Override + public SeataDataSourceProxyMetadata init(DataSource dataSource) throws SQLException { + try (Connection connection = dataSource.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SHOW VARIABLES"); + ResultSet rs = preparedStatement.executeQuery()) { + while (rs.next()) { + String name = rs.getString(1); + String value = rs.getString(2); + if (StringUtils.isNotBlank(name)) { + variables.put(name.toLowerCase(), value); + } + } + } + return this; + } + + @Override + public String getVariableValue(String name) { + return variables.get(name); + } + +} From 4345ecb94fb215dbce8a4f9c7860feb139352e50 Mon Sep 17 00:00:00 2001 From: jsbxyyx Date: Fri, 2 Aug 2024 15:19:36 +0800 Subject: [PATCH 2/8] datasource metadata --- .../datasource/SeataDataSourceProxyMetadata.java | 5 ----- .../SeataDataSourceProxyMetadataFactory.java | 1 - .../metadata/DefaultDataSourceProxyMeta.java | 16 ++++++++++++++++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadata.java b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadata.java index 9d891ec0f9e..af0680a5eeb 100644 --- a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadata.java +++ b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadata.java @@ -17,13 +17,8 @@ package org.apache.seata.rm.datasource; import javax.sql.DataSource; -import java.sql.Connection; import java.sql.SQLException; -import org.apache.seata.rm.datasource.metadata.MySQLDataSourceProxyMetadata; -import org.apache.seata.rm.datasource.util.JdbcUtils; -import org.apache.seata.sqlparser.util.JdbcConstants; - /** * The type Data source proxy metadata. * diff --git a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadataFactory.java b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadataFactory.java index e845b02b0bc..ace4222bd59 100644 --- a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadataFactory.java +++ b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadataFactory.java @@ -22,7 +22,6 @@ import org.apache.seata.rm.datasource.metadata.DefaultDataSourceProxyMeta; import org.apache.seata.rm.datasource.metadata.MySQLDataSourceProxyMetadata; -import org.apache.seata.rm.datasource.metadata.OracleDataSourceProxyMetadata; import org.apache.seata.rm.datasource.util.JdbcUtils; import org.apache.seata.sqlparser.util.JdbcConstants; diff --git a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/metadata/DefaultDataSourceProxyMeta.java b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/metadata/DefaultDataSourceProxyMeta.java index 6e396f177ca..ec4a0f438d6 100644 --- a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/metadata/DefaultDataSourceProxyMeta.java +++ b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/metadata/DefaultDataSourceProxyMeta.java @@ -1,3 +1,19 @@ +/* + * 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.seata.rm.datasource.metadata; import javax.sql.DataSource; From cfbab08529ed63a8a730624a5bb983cac4ffed58 Mon Sep 17 00:00:00 2001 From: jsbxyyx Date: Fri, 2 Aug 2024 15:42:27 +0800 Subject: [PATCH 3/8] fix: tests --- .../org/apache/seata/rm/datasource/exec/MultiExecutorTest.java | 2 ++ .../org/apache/seata/rm/datasource/exec/PlainExecutorTest.java | 3 +++ .../seata/rm/datasource/exec/PolarDBXInsertExecutorTest.java | 2 ++ .../seata/rm/datasource/exec/SelectForUpdateExecutorTest.java | 3 +++ .../apache/seata/rm/datasource/exec/UpdateExecutorTest.java | 2 ++ .../seata/rm/datasource/exec/UpdateJoinExecutorTest.java | 2 ++ .../org/apache/seata/rm/datasource/mock/MockDataSource.java | 3 ++- .../apache/seata/rm/datasource/mock/MockMariadbDataSource.java | 3 ++- 8 files changed, 18 insertions(+), 2 deletions(-) diff --git a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/MultiExecutorTest.java b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/MultiExecutorTest.java index ffcd118cd95..b0163ce1545 100644 --- a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/MultiExecutorTest.java +++ b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/MultiExecutorTest.java @@ -22,6 +22,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Properties; import java.util.Set; import java.util.stream.Collectors; @@ -77,6 +78,7 @@ public static void init() throws Throwable { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl("jdbc:mock:xxx"); dataSource.setDriver(mockDriver); + dataSource.setConnectProperties(new Properties()); DataSourceProxy dataSourceProxy = DataSourceProxyTest.getDataSourceProxy(dataSource); diff --git a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/PlainExecutorTest.java b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/PlainExecutorTest.java index 6e2d0f76a70..3e1bceecca4 100644 --- a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/PlainExecutorTest.java +++ b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/PlainExecutorTest.java @@ -19,6 +19,8 @@ import java.sql.SQLException; import java.sql.Types; import java.util.List; +import java.util.Properties; + import com.alibaba.druid.mock.MockStatement; import com.alibaba.druid.mock.MockStatementBase; import com.alibaba.druid.pool.DruidDataSource; @@ -56,6 +58,7 @@ public void init() throws SQLException { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl("jdbc:mock:xxx"); dataSource.setDriver(mockDriver); + dataSource.setConnectProperties(new Properties()); DataSourceProxy dataSourceProxy = DataSourceProxyTest.getDataSourceProxy(dataSource); diff --git a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/PolarDBXInsertExecutorTest.java b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/PolarDBXInsertExecutorTest.java index 1bfe853f421..1bb83a30379 100644 --- a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/PolarDBXInsertExecutorTest.java +++ b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/PolarDBXInsertExecutorTest.java @@ -22,6 +22,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; +import java.util.Properties; import com.alibaba.druid.mock.MockStatement; import com.alibaba.druid.mock.MockStatementBase; @@ -105,6 +106,7 @@ public void init() throws SQLException { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl("jdbc:mock:xxx"); dataSource.setDriver(mockDriver); + dataSource.setConnectProperties(new Properties()); DataSourceProxy newDataSourceProxy = DataSourceProxyTest.getDataSourceProxy(dataSource); try { diff --git a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/SelectForUpdateExecutorTest.java b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/SelectForUpdateExecutorTest.java index ca3dc38fd5a..e19320031a9 100644 --- a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/SelectForUpdateExecutorTest.java +++ b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/SelectForUpdateExecutorTest.java @@ -19,6 +19,8 @@ import java.lang.reflect.Field; import java.sql.Types; import java.util.List; +import java.util.Properties; + import com.alibaba.druid.mock.MockStatement; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.sql.SQLUtils; @@ -70,6 +72,7 @@ public static void init() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl("jdbc:mock:xxx"); dataSource.setDriver(mockDriver); + dataSource.setConnectProperties(new Properties()); DataSourceProxy dataSourceProxy = DataSourceProxyTest.getDataSourceProxy(dataSource); diff --git a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/UpdateExecutorTest.java b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/UpdateExecutorTest.java index 9de02c73654..f3509118930 100644 --- a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/UpdateExecutorTest.java +++ b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/UpdateExecutorTest.java @@ -20,6 +20,7 @@ import java.sql.SQLException; import java.sql.Types; import java.util.List; +import java.util.Properties; import com.alibaba.druid.mock.MockStatement; import com.alibaba.druid.mock.MockStatementBase; @@ -72,6 +73,7 @@ public static void init() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl("jdbc:mock:xxx"); dataSource.setDriver(mockDriver); + dataSource.setConnectProperties(new Properties()); DataSourceProxy dataSourceProxy = DataSourceProxyTest.getDataSourceProxy(dataSource); diff --git a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/UpdateJoinExecutorTest.java b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/UpdateJoinExecutorTest.java index 3a8ce32ce72..4baff651859 100644 --- a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/UpdateJoinExecutorTest.java +++ b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/UpdateJoinExecutorTest.java @@ -20,6 +20,7 @@ import java.sql.SQLException; import java.sql.Types; import java.util.List; +import java.util.Properties; import com.alibaba.druid.mock.MockStatement; import com.alibaba.druid.mock.MockStatementBase; @@ -81,6 +82,7 @@ private StatementProxy mockStatementProxy(List returnValueColumnLabels, DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl("jdbc:mock:xxx"); dataSource.setDriver(mockDriver); + dataSource.setConnectProperties(new Properties()); DataSourceProxy dataSourceProxy = DataSourceProxyTest.getDataSourceProxy(dataSource); try { diff --git a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/mock/MockDataSource.java b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/mock/MockDataSource.java index c9f946446f6..90580b7261e 100644 --- a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/mock/MockDataSource.java +++ b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/mock/MockDataSource.java @@ -21,13 +21,14 @@ import java.sql.Connection; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; +import java.util.Properties; import java.util.logging.Logger; public class MockDataSource implements DataSource { @Override public Connection getConnection() throws SQLException { - return new MockConnection(new MockDriver(), "jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true", null); + return new MockConnection(new MockDriver(), "jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true", new Properties()); } @Override diff --git a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/mock/MockMariadbDataSource.java b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/mock/MockMariadbDataSource.java index cdc28780c6e..15ae9d3d265 100644 --- a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/mock/MockMariadbDataSource.java +++ b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/mock/MockMariadbDataSource.java @@ -18,11 +18,12 @@ import java.sql.Connection; import java.sql.SQLException; +import java.util.Properties; public class MockMariadbDataSource extends MockDataSource { @Override public Connection getConnection() throws SQLException { - return new MockConnection(new MockDriver(), "jdbc:mariadb://127.0.0.1:3306/seata?rewriteBatchedStatements=true", null); + return new MockConnection(new MockDriver(), "jdbc:mariadb://127.0.0.1:3306/seata?rewriteBatchedStatements=true", new Properties()); } } From 8fa48069129b3ce7e5c2ef257586dd6098124ca7 Mon Sep 17 00:00:00 2001 From: jsbxyyx Date: Fri, 2 Aug 2024 15:51:47 +0800 Subject: [PATCH 4/8] fix: tests --- .../apache/seata/rm/datasource/DataSourceProxyTest.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/DataSourceProxyTest.java b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/DataSourceProxyTest.java index 1f4bf9659eb..becf5696b64 100644 --- a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/DataSourceProxyTest.java +++ b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/DataSourceProxyTest.java @@ -16,14 +16,13 @@ */ package org.apache.seata.rm.datasource; +import javax.sql.DataSource; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.SQLException; -import javax.sql.DataSource; +import java.util.Properties; import com.alibaba.druid.pool.DruidDataSource; - -import org.apache.seata.rm.datasource.DataSourceProxy; import org.apache.seata.rm.datasource.mock.MockDataSource; import org.apache.seata.rm.datasource.mock.MockDriver; import org.apache.seata.rm.datasource.sql.struct.TableMetaCacheFactory; @@ -37,7 +36,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; public class DataSourceProxyTest { @@ -64,6 +64,7 @@ public void testNotSupportDb() { dataSource.setDriver(mockDriver); dataSource.setUsername(username); dataSource.setPassword("password"); + dataSource.setConnectProperties(new Properties()); Throwable throwable = Assertions.assertThrows(IllegalStateException.class, () -> new DataSourceProxy(dataSource)); assertThat(throwable).hasMessageContaining("AT mode don't support the dbtype"); From 90b03d7a8d410fdecbd42aa9171333f0198a2761 Mon Sep 17 00:00:00 2001 From: jsbxyyx Date: Fri, 2 Aug 2024 16:13:31 +0800 Subject: [PATCH 5/8] rename --- .../rm/datasource/SeataDataSourceProxyMetadataFactory.java | 4 ++-- ...urceProxyMeta.java => DefaultDataSourceProxyMetadata.java} | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) rename rm-datasource/src/main/java/org/apache/seata/rm/datasource/metadata/{DefaultDataSourceProxyMeta.java => DefaultDataSourceProxyMetadata.java} (93%) diff --git a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadataFactory.java b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadataFactory.java index ace4222bd59..e8e3b424d91 100644 --- a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadataFactory.java +++ b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadataFactory.java @@ -20,7 +20,7 @@ import java.sql.Connection; import java.sql.SQLException; -import org.apache.seata.rm.datasource.metadata.DefaultDataSourceProxyMeta; +import org.apache.seata.rm.datasource.metadata.DefaultDataSourceProxyMetadata; import org.apache.seata.rm.datasource.metadata.MySQLDataSourceProxyMetadata; import org.apache.seata.rm.datasource.util.JdbcUtils; import org.apache.seata.sqlparser.util.JdbcConstants; @@ -35,7 +35,7 @@ public static SeataDataSourceProxyMetadata create(DataSource dataSource) throws if (JdbcConstants.MYSQL.equals(dbType)) { dataSourceProxyMetadata = new MySQLDataSourceProxyMetadata(); } else { - dataSourceProxyMetadata = new DefaultDataSourceProxyMeta(); + dataSourceProxyMetadata = new DefaultDataSourceProxyMetadata(); } } dataSourceProxyMetadata.init(dataSource); diff --git a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/metadata/DefaultDataSourceProxyMeta.java b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/metadata/DefaultDataSourceProxyMetadata.java similarity index 93% rename from rm-datasource/src/main/java/org/apache/seata/rm/datasource/metadata/DefaultDataSourceProxyMeta.java rename to rm-datasource/src/main/java/org/apache/seata/rm/datasource/metadata/DefaultDataSourceProxyMetadata.java index ec4a0f438d6..0548b20a23f 100644 --- a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/metadata/DefaultDataSourceProxyMeta.java +++ b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/metadata/DefaultDataSourceProxyMetadata.java @@ -19,7 +19,7 @@ import javax.sql.DataSource; import java.sql.SQLException; -public class DefaultDataSourceProxyMeta extends AbstractDataSourceProxyMetadata { +public class DefaultDataSourceProxyMetadata extends AbstractDataSourceProxyMetadata { @Override public void init(DataSource dataSource) throws SQLException { From 02d6f4f44dfa1f57a2a5ebc693e735017e5445b6 Mon Sep 17 00:00:00 2001 From: jsbxyyx Date: Sat, 3 Aug 2024 09:44:49 +0800 Subject: [PATCH 6/8] fix: tests --- .../rm/datasource/DataSourceProxyTest.java | 2 + .../rm/datasource/mock/MockDataSource.java | 3 +- .../exec/mysql/MySQLInsertExecutor.java | 49 +++++-------------- 3 files changed, 17 insertions(+), 37 deletions(-) diff --git a/compatible/src/test/java/io/seata/rm/datasource/DataSourceProxyTest.java b/compatible/src/test/java/io/seata/rm/datasource/DataSourceProxyTest.java index e707c4c568f..4a6a02c7920 100644 --- a/compatible/src/test/java/io/seata/rm/datasource/DataSourceProxyTest.java +++ b/compatible/src/test/java/io/seata/rm/datasource/DataSourceProxyTest.java @@ -17,6 +17,7 @@ package io.seata.rm.datasource; import java.sql.Connection; +import java.util.Properties; import javax.sql.DataSource; @@ -61,6 +62,7 @@ public void testNotSupportDb() { dataSource.setDriver(mockDriver); dataSource.setUsername(username); dataSource.setPassword("password"); + dataSource.setConnectProperties(new Properties()); Throwable throwable = Assertions.assertThrows(IllegalStateException.class, () -> new DataSourceProxy(dataSource)); assertThat(throwable).hasMessageContaining("AT mode don't support the dbtype"); diff --git a/compatible/src/test/java/io/seata/rm/datasource/mock/MockDataSource.java b/compatible/src/test/java/io/seata/rm/datasource/mock/MockDataSource.java index 7854c43b0eb..32893fded8c 100644 --- a/compatible/src/test/java/io/seata/rm/datasource/mock/MockDataSource.java +++ b/compatible/src/test/java/io/seata/rm/datasource/mock/MockDataSource.java @@ -20,6 +20,7 @@ import java.sql.Connection; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; +import java.util.Properties; import java.util.logging.Logger; import javax.sql.DataSource; @@ -27,7 +28,7 @@ public class MockDataSource implements DataSource { @Override public Connection getConnection() throws SQLException { - return new MockConnection(new MockDriver(), "jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true", null); + return new MockConnection(new MockDriver(), "jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true", new Properties()); } @Override diff --git a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/exec/mysql/MySQLInsertExecutor.java b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/exec/mysql/MySQLInsertExecutor.java index 923f2b5c416..761cc954537 100644 --- a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/exec/mysql/MySQLInsertExecutor.java +++ b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/exec/mysql/MySQLInsertExecutor.java @@ -26,7 +26,6 @@ import java.util.Map; import java.util.Objects; import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; import org.apache.seata.common.exception.NotSupportYetException; import org.apache.seata.common.exception.ShouldNeverHappenException; @@ -37,8 +36,8 @@ import org.apache.seata.rm.datasource.StatementProxy; import org.apache.seata.rm.datasource.exec.BaseInsertExecutor; import org.apache.seata.rm.datasource.exec.StatementCallback; -import org.apache.seata.sqlparser.struct.ColumnMeta; import org.apache.seata.sqlparser.SQLRecognizer; +import org.apache.seata.sqlparser.struct.ColumnMeta; import org.apache.seata.sqlparser.struct.Defaultable; import org.apache.seata.sqlparser.struct.Null; import org.apache.seata.sqlparser.struct.SqlMethodExpr; @@ -48,7 +47,6 @@ /** * The type My sql insert executor. - * */ @LoadLevel(name = JdbcConstants.MYSQL, scope = Scope.PROTOTYPE) public class MySQLInsertExecutor extends BaseInsertExecutor implements Defaultable { @@ -60,13 +58,6 @@ public class MySQLInsertExecutor extends BaseInsertExecutor implements Defaultab */ public static final String ERR_SQL_STATE = "S1009"; - /** - * The cache of auto increment step of database - * the key is the db's resource id - * the value is the step - */ - public static final Map RESOURCE_ID_STEP_CACHE = new ConcurrentHashMap<>(8); - /** * Instantiates a new Abstract dml base executor. * @@ -80,19 +71,17 @@ public MySQLInsertExecutor(StatementProxy statementProxy, StatementCallback stat } @Override - public Map> getPkValues() throws SQLException { - Map> pkValuesMap = null; + public Map> getPkValues() throws SQLException { + Map> pkValuesMap = null; List pkColumnNameList = getTableMeta().getPrimaryKeyOnlyName(); boolean isContainsPk = containsPK(); //when there is only one pk in the table if (pkColumnNameList.size() == 1) { if (isContainsPk) { pkValuesMap = getPkValuesByColumn(); - } - else if (containsColumns()) { + } else if (containsColumns()) { pkValuesMap = getPkValuesByAuto(); - } - else { + } else { pkValuesMap = getPkValuesByColumn(); } } else { @@ -100,7 +89,7 @@ else if (containsColumns()) { //1,all pk columns are filled value. //2,the auto increment pk column value is null, and other pk value are not null. pkValuesMap = getPkValuesByColumn(); - for (String columnName:pkColumnNameList) { + for (String columnName : pkColumnNameList) { if (!pkValuesMap.containsKey(columnName)) { ColumnMeta pkColumnMeta = getTableMeta().getColumnMeta(columnName); if (Objects.nonNull(pkColumnMeta) && pkColumnMeta.isAutoincrement()) { @@ -181,11 +170,11 @@ public Map> getPkValuesByAuto() throws SQLException { } @Override - public Map> getPkValuesByColumn() throws SQLException { - Map> pkValuesMap = parsePkValuesFromStatement(); + public Map> getPkValuesByColumn() throws SQLException { + Map> pkValuesMap = parsePkValuesFromStatement(); Set keySet = new HashSet<>(pkValuesMap.keySet()); //auto increment - for (String pkKey:keySet) { + for (String pkKey : keySet) { List pkValues = pkValuesMap.get(pkKey); // pk auto generated while single insert primary key is expression if (pkValues.size() == 1 && (pkValues.get(0) instanceof SqlMethodExpr)) { @@ -215,22 +204,10 @@ public List getPkValuesByDefault(String pkKey) throws SQLException { } protected Map> autoGeneratePks(BigDecimal cursor, String autoColumnName, Integer updateCount) throws SQLException { - BigDecimal step = BigDecimal.ONE; - String resourceId = statementProxy.getConnectionProxy().getDataSourceProxy().getResourceId(); - if (RESOURCE_ID_STEP_CACHE.containsKey(resourceId)) { - step = RESOURCE_ID_STEP_CACHE.get(resourceId); - } else { - ResultSet increment = null; - try { - increment = statementProxy.getTargetStatement().executeQuery("SHOW VARIABLES LIKE 'auto_increment_increment'"); - increment.next(); - step = new BigDecimal(increment.getString(2)); - RESOURCE_ID_STEP_CACHE.put(resourceId, step); - } finally { - IOUtil.close(increment); - } - } + String increment = statementProxy.getConnectionProxy().getDataSourceProxy() + .getDataSourceProxyMetadata().getVariableValue("auto_increment_increment"); + BigDecimal step = new BigDecimal(increment); List pkValues = new ArrayList<>(); for (int i = 0; i < updateCount; i++) { @@ -239,7 +216,7 @@ protected Map> autoGeneratePks(BigDecimal cursor, String au } Map> pkValuesMap = new HashMap<>(1, 1.001f); - pkValuesMap.put(autoColumnName,pkValues); + pkValuesMap.put(autoColumnName, pkValues); return pkValuesMap; } From f54d4ad11d5f4ad3246309dbb1aeabfe36ee0a0f Mon Sep 17 00:00:00 2001 From: jsbxyyx Date: Sat, 3 Aug 2024 10:43:57 +0800 Subject: [PATCH 7/8] fix: tests --- .../SeataDataSourceProxyMetadata.java | 14 ++- .../DefaultDataSourceProxyMetadata.java | 7 ++ .../MySQLDataSourceProxyMetadata.java | 5 + .../exec/MySQLInsertExecutorTest.java | 101 +++++++++--------- 4 files changed, 78 insertions(+), 49 deletions(-) diff --git a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadata.java b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadata.java index af0680a5eeb..4decac141a8 100644 --- a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadata.java +++ b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadata.java @@ -18,15 +18,16 @@ import javax.sql.DataSource; import java.sql.SQLException; +import java.util.Map; /** * The type Data source proxy metadata. - * */ public interface SeataDataSourceProxyMetadata { /** * Init datasource metadata + * * @param dataSource the datasource * @throws SQLException sql exception */ @@ -34,13 +35,22 @@ public interface SeataDataSourceProxyMetadata { /** * Get variable value by name + * * @param name the name * @return value */ String getVariableValue(String name); + /** + * Get variables + * + * @return all variable + */ + Map getVariables(); + /** * Get jdbc url + * * @return jdbc url */ String getJdbcUrl(); @@ -54,12 +64,14 @@ public interface SeataDataSourceProxyMetadata { /** * Get database connection username + * * @return username */ String getUserName(); /** * Get kernel version + * * @return kernel version */ String getKernelVersion(); diff --git a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/metadata/DefaultDataSourceProxyMetadata.java b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/metadata/DefaultDataSourceProxyMetadata.java index 0548b20a23f..e44d3218256 100644 --- a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/metadata/DefaultDataSourceProxyMetadata.java +++ b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/metadata/DefaultDataSourceProxyMetadata.java @@ -18,6 +18,8 @@ import javax.sql.DataSource; import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; public class DefaultDataSourceProxyMetadata extends AbstractDataSourceProxyMetadata { @@ -31,6 +33,11 @@ public String getVariableValue(String name) { return null; } + @Override + public Map getVariables() { + return new HashMap<>(); + } + @Override public String getKernelVersion() { return null; diff --git a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/metadata/MySQLDataSourceProxyMetadata.java b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/metadata/MySQLDataSourceProxyMetadata.java index fe73ddfbe74..c883e71f039 100644 --- a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/metadata/MySQLDataSourceProxyMetadata.java +++ b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/metadata/MySQLDataSourceProxyMetadata.java @@ -71,6 +71,11 @@ public String getVariableValue(String name) { return variables.get(name); } + @Override + public Map getVariables() { + return new HashMap<>(variables); + } + @Override public String getKernelVersion() { return kernelVersion; diff --git a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/MySQLInsertExecutorTest.java b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/MySQLInsertExecutorTest.java index eaed8d4abb9..dd517d5c970 100644 --- a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/MySQLInsertExecutorTest.java +++ b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/MySQLInsertExecutorTest.java @@ -36,7 +36,6 @@ import com.alibaba.druid.sql.SQLUtils; import com.alibaba.druid.sql.ast.SQLStatement; import com.google.common.collect.Lists; -import org.apache.seata.rm.datasource.exec.StatementCallback; import org.apache.seata.common.exception.ShouldNeverHappenException; import org.apache.seata.rm.datasource.ConnectionProxy; import org.apache.seata.rm.datasource.DataSourceProxy; @@ -44,25 +43,24 @@ import org.apache.seata.rm.datasource.PreparedStatementProxy; import org.apache.seata.rm.datasource.StatementProxy; import org.apache.seata.rm.datasource.exec.mysql.MySQLInsertExecutor; +import org.apache.seata.rm.datasource.metadata.MySQLDataSourceProxyMetadata; import org.apache.seata.rm.datasource.mock.MockDataSource; import org.apache.seata.rm.datasource.mock.MockDriver; -import org.apache.seata.rm.datasource.mock.MockResultSet; -import org.apache.seata.sqlparser.druid.mysql.MySQLInsertRecognizer; -import org.apache.seata.sqlparser.struct.ColumnMeta; -import org.apache.seata.sqlparser.struct.TableMeta; import org.apache.seata.rm.datasource.sql.struct.TableRecords; import org.apache.seata.sqlparser.SQLInsertRecognizer; +import org.apache.seata.sqlparser.druid.mysql.MySQLInsertRecognizer; +import org.apache.seata.sqlparser.struct.ColumnMeta; import org.apache.seata.sqlparser.struct.Null; import org.apache.seata.sqlparser.struct.SqlDefaultExpr; import org.apache.seata.sqlparser.struct.SqlMethodExpr; import org.apache.seata.sqlparser.struct.SqlSequenceExpr; +import org.apache.seata.sqlparser.struct.TableMeta; import org.apache.seata.sqlparser.util.JdbcConstants; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; - import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; @@ -91,7 +89,7 @@ public class MySQLInsertExecutorTest { protected MySQLInsertExecutor newInsertExecutor; protected final int pkIndex = 0; - protected HashMap pkIndexMap; + protected HashMap pkIndexMap; @BeforeEach public void init() throws SQLException { @@ -104,16 +102,12 @@ public void init() throws SQLException { when(statementProxy.getConnectionProxy()).thenReturn(connectionProxy); when(statementProxy.getTargetStatement()).thenReturn(statementProxy); - MockResultSet resultSet = new MockResultSet(statementProxy); - resultSet.mockResultSet(Arrays.asList("Variable_name", "Value"), new Object[][]{{"auto_increment_increment", "1"}}); - when(statementProxy.getTargetStatement().executeQuery("SHOW VARIABLES LIKE 'auto_increment_increment'")).thenReturn(resultSet); - StatementCallback statementCallback = mock(StatementCallback.class); sqlInsertRecognizer = mock(SQLInsertRecognizer.class); tableMeta = mock(TableMeta.class); insertExecutor = Mockito.spy(new MySQLInsertExecutor(statementProxy, statementCallback, sqlInsertRecognizer)); - pkIndexMap = new HashMap(){ + pkIndexMap = new HashMap() { { put(ID_COLUMN, pkIndex); } @@ -121,21 +115,21 @@ public void init() throws SQLException { // new test init property List returnValueColumnLabels = Lists.newArrayList("id", "user_id", "name", "sex", "update_time"); - Object[][] returnValue = new Object[][] { - new Object[] {1, 1, "will", 1, 0}, + Object[][] returnValue = new Object[][]{ + new Object[]{1, 1, "will", 1, 0}, }; - Object[][] columnMetas = new Object[][] { - new Object[] {"", "", "table_insert_executor_test", "id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 2, "NO", "NO"}, - new Object[] {"", "", "table_insert_executor_test", "user_id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 2, "NO", "NO"}, - new Object[] {"", "", "table_insert_executor_test", "name", Types.VARCHAR, "VARCHAR", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "NO", "NO"}, - new Object[] {"", "", "table_insert_executor_test", "sex", Types.INTEGER, "INTEGER", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "NO", "NO"}, - new Object[] {"", "", "table_insert_executor_test", "update_time", Types.INTEGER, "INTEGER", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "YES", "NO"}, + Object[][] columnMetas = new Object[][]{ + new Object[]{"", "", "table_insert_executor_test", "id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 2, "NO", "NO"}, + new Object[]{"", "", "table_insert_executor_test", "user_id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 2, "NO", "NO"}, + new Object[]{"", "", "table_insert_executor_test", "name", Types.VARCHAR, "VARCHAR", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "NO", "NO"}, + new Object[]{"", "", "table_insert_executor_test", "sex", Types.INTEGER, "INTEGER", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "NO", "NO"}, + new Object[]{"", "", "table_insert_executor_test", "update_time", Types.INTEGER, "INTEGER", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "YES", "NO"}, }; - Object[][] indexMetas = new Object[][] { - new Object[] {"PRIMARY", "id", false, "", 3, 1, "A", 34}, - new Object[] {"PRIMARY", "user_id", false, "", 3, 1, "A", 34}, + Object[][] indexMetas = new Object[][]{ + new Object[]{"PRIMARY", "id", false, "", 3, 1, "A", 34}, + new Object[]{"PRIMARY", "user_id", false, "", 3, 1, "A", 34}, }; - Object[][] onUpdateColumnsReturnValue = new Object[][] { + Object[][] onUpdateColumnsReturnValue = new Object[][]{ new Object[]{0, "update_time", Types.INTEGER, "INTEGER", 64, 10, 0, 0} }; @@ -238,8 +232,8 @@ public void testBeforeAndAfterImageUpperColumn() throws SQLException { @Test public void testAfterImage_ByColumn() throws SQLException { doReturn(true).when(insertExecutor).containsPK(); - Map> pkValuesMap =new HashMap<>(); - pkValuesMap.put("id",Arrays.asList(new Object[]{PK_VALUE})); + Map> pkValuesMap = new HashMap<>(); + pkValuesMap.put("id", Arrays.asList(new Object[]{PK_VALUE})); doReturn(pkValuesMap).when(insertExecutor).getPkValuesByColumn(); TableRecords tableRecords = new TableRecords(); doReturn(tableRecords).when(insertExecutor).buildTableRecords(pkValuesMap); @@ -253,8 +247,8 @@ public void testAfterImage_ByColumn() throws SQLException { public void testAfterImage_ByAuto() throws SQLException { doReturn(false).when(insertExecutor).containsPK(); doReturn(true).when(insertExecutor).containsColumns(); - Map> pkValuesMap =new HashMap<>(); - pkValuesMap.put("id",Arrays.asList(new Object[]{PK_VALUE})); + Map> pkValuesMap = new HashMap<>(); + pkValuesMap.put("id", Arrays.asList(new Object[]{PK_VALUE})); doReturn(pkValuesMap).when(insertExecutor).getPkValuesByAuto(); TableRecords tableRecords = new TableRecords(); doReturn(tableRecords).when(insertExecutor).buildTableRecords(pkValuesMap); @@ -269,8 +263,8 @@ public void testAfterImage_Exception() { Assertions.assertThrows(SQLException.class, () -> { doReturn(false).when(insertExecutor).containsPK(); doReturn(true).when(insertExecutor).containsColumns(); - Map> pkValuesMap =new HashMap<>(); - pkValuesMap.put("id",Arrays.asList(new Object[]{PK_VALUE})); + Map> pkValuesMap = new HashMap<>(); + pkValuesMap.put("id", Arrays.asList(new Object[]{PK_VALUE})); doReturn(pkValuesMap).when(insertExecutor).getPkValuesByAuto(); doReturn(null).when(insertExecutor).buildTableRecords(pkValuesMap); doReturn(tableMeta).when(insertExecutor).getTableMeta(); @@ -301,7 +295,7 @@ public void testGetPkValuesByColumn() throws SQLException { List pkValues = new ArrayList<>(); pkValues.add(PK_VALUE); doReturn(pkIndexMap).when(insertExecutor).getPkIndex(); - Map> pkValuesList = insertExecutor.getPkValuesByColumn(); + Map> pkValuesList = insertExecutor.getPkValuesByColumn(); Assertions.assertIterableEquals(pkValuesList.get(ID_COLUMN), pkValues); } @@ -326,13 +320,17 @@ public void testGetPkValuesByColumn_PkValue_Null() throws SQLException { ColumnMeta cm = new ColumnMeta(); cm.setColumnName(ID_COLUMN); cm.setIsAutoincrement("YES"); - when(tableMeta.getPrimaryKeyMap()).thenReturn(new HashMap(){{put(ID_COLUMN,cm);}}); + when(tableMeta.getPrimaryKeyMap()).thenReturn(new HashMap() {{ + put(ID_COLUMN, cm); + }}); List pkValuesAuto = new ArrayList<>(); pkValuesAuto.add(PK_VALUE); //mock getPkValuesByAuto - doReturn(new HashMap>(){{put(ID_COLUMN,pkValuesAuto);}}).when(insertExecutor).getPkValuesByAuto(); + doReturn(new HashMap>() {{ + put(ID_COLUMN, pkValuesAuto); + }}).when(insertExecutor).getPkValuesByAuto(); doReturn(pkIndexMap).when(insertExecutor).getPkIndex(); - Map> pkValuesList = insertExecutor.getPkValuesByColumn(); + Map> pkValuesList = insertExecutor.getPkValuesByColumn(); //pk value = Null so getPkValuesByAuto verify(insertExecutor).getPkValuesByAuto(); Assertions.assertIterableEquals(pkValuesList.get(ID_COLUMN), pkValuesAuto); @@ -383,7 +381,7 @@ public void testGetPkValuesByAuto_SQLException_WarnLog() throws SQLException { when(statementProxy.getGeneratedKeys()).thenThrow(e); ResultSet genKeys = mock(ResultSet.class); when(statementProxy.getTargetStatement().executeQuery("SELECT LAST_INSERT_ID()")).thenReturn(genKeys); - Map> pkValueMap=insertExecutor.getPkValuesByAuto(); + Map> pkValueMap = insertExecutor.getPkValuesByAuto(); Assertions.assertTrue(pkValueMap.get(ID_COLUMN).isEmpty()); } @@ -401,8 +399,8 @@ public void testGetPkValuesByAuto_GeneratedKeys_NoResult() throws SQLException { when(statementProxy.getGeneratedKeys()).thenReturn(resultSet); when(resultSet.next()).thenReturn(false); when(resultSet.getObject(1)).thenReturn(PK_VALUE); - Map> pkValues = insertExecutor.getPkValuesByAuto(); - Assertions.assertEquals(pkValues.get(ID_COLUMN).size(),0); + Map> pkValues = insertExecutor.getPkValuesByAuto(); + Assertions.assertEquals(pkValues.get(ID_COLUMN).size(), 0); } @Test @@ -421,7 +419,7 @@ public void testGetPkValuesByAuto_GeneratedKeys_HasResult() throws SQLException when(resultSet.getObject(1)).thenReturn(PK_VALUE); List pkValues = new ArrayList<>(); pkValues.add(PK_VALUE); - Map> pkValuesList = insertExecutor.getPkValuesByAuto(); + Map> pkValuesList = insertExecutor.getPkValuesByAuto(); Assertions.assertIterableEquals(pkValuesList.get(ID_COLUMN), pkValues); } @@ -442,7 +440,7 @@ public void testGetPkValuesByAuto_ExecuteQuery_HasResult() throws SQLException { when(resultSet.getObject(1)).thenReturn(PK_VALUE); List pkValues = new ArrayList<>(); pkValues.add(PK_VALUE); - Map> pkValuesList = insertExecutor.getPkValuesByAuto(); + Map> pkValuesList = insertExecutor.getPkValuesByAuto(); Assertions.assertIterableEquals(pkValuesList.get(ID_COLUMN), pkValues); } @@ -456,13 +454,12 @@ public void test_getPkIndex() { @Test - public void test_checkPkValuesForMultiPk() - { - Map> pkValues = new HashMap<>(); + public void test_checkPkValuesForMultiPk() { + Map> pkValues = new HashMap<>(); List pkValues1 = new ArrayList(); List pkValues2 = new ArrayList(); - pkValues.put("id",pkValues1); - pkValues.put("userCode",pkValues2); + pkValues.put("id", pkValues1); + pkValues.put("userCode", pkValues2); //all pk support value pkValues1.add(1); @@ -692,7 +689,15 @@ public void test_checkPkValues() { } @Test - public void test_autoGeneratePks() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + public void test_autoGeneratePks() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException { + MySQLDataSourceProxyMetadata dataSourceProxyMetadata = (MySQLDataSourceProxyMetadata) + insertExecutor.statementProxy.getConnectionProxy().getDataSourceProxy().getDataSourceProxyMetadata(); + Map variables = new HashMap<>(); + variables.put("auto_increment_increment", "1"); + Field variablesField = dataSourceProxyMetadata.getClass().getDeclaredField("variables"); + variablesField.setAccessible(true); + variablesField.set(dataSourceProxyMetadata, variables); + Method method = MySQLInsertExecutor.class.getDeclaredMethod("autoGeneratePks", new Class[]{BigDecimal.class, String.class, Integer.class}); method.setAccessible(true); Object resp = method.invoke(insertExecutor, BigDecimal.ONE, "ID", 3); @@ -716,7 +721,7 @@ private List mockInsertColumns() { } private void mockParameters() { - Map> paramters = new HashMap<>(4); + Map> paramters = new HashMap<>(4); ArrayList arrayList0 = new ArrayList<>(); arrayList0.add(PK_VALUE); ArrayList arrayList1 = new ArrayList<>(); @@ -734,7 +739,7 @@ private void mockParameters() { } private void mockParametersPkWithNull() { - Map> parameters = new HashMap<>(4); + Map> parameters = new HashMap<>(4); ArrayList arrayList0 = new ArrayList<>(); arrayList0.add(Null.get()); ArrayList arrayList1 = new ArrayList<>(); @@ -752,7 +757,7 @@ private void mockParametersPkWithNull() { } private void mockParametersOfOnePk() { - Map> paramters = new HashMap<>(4); + Map> paramters = new HashMap<>(4); ArrayList arrayList1 = new ArrayList<>(); arrayList1.add(PK_VALUE); paramters.put(1, arrayList1); From 11213ab6cd8202fbae5560d5123232ce35acc6b8 Mon Sep 17 00:00:00 2001 From: jsbxyyx Date: Sat, 3 Aug 2024 11:10:35 +0800 Subject: [PATCH 8/8] fix: tests --- .../SeataDataSourceProxyMetadataFactory.java | 7 +++- .../exec/MariadbInsertExecutorTest.java | 10 +----- .../exec/PolarDBXInsertExecutorTest.java | 36 +++++++++---------- 3 files changed, 23 insertions(+), 30 deletions(-) diff --git a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadataFactory.java b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadataFactory.java index e8e3b424d91..f816bc0caa4 100644 --- a/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadataFactory.java +++ b/rm-datasource/src/main/java/org/apache/seata/rm/datasource/SeataDataSourceProxyMetadataFactory.java @@ -25,6 +25,9 @@ import org.apache.seata.rm.datasource.util.JdbcUtils; import org.apache.seata.sqlparser.util.JdbcConstants; +/** + * datasource proxy metadata factory + */ public class SeataDataSourceProxyMetadataFactory { public static SeataDataSourceProxyMetadata create(DataSource dataSource) throws SQLException { @@ -32,7 +35,9 @@ public static SeataDataSourceProxyMetadata create(DataSource dataSource) throws try (Connection connection = dataSource.getConnection()) { String jdbcUrl = connection.getMetaData().getURL(); String dbType = JdbcUtils.getDbType(jdbcUrl); - if (JdbcConstants.MYSQL.equals(dbType)) { + if (JdbcConstants.MYSQL.equals(dbType) + || JdbcConstants.MARIADB.equals(dbType) + || JdbcConstants.POLARDBX.equals(dbType)) { dataSourceProxyMetadata = new MySQLDataSourceProxyMetadata(); } else { dataSourceProxyMetadata = new DefaultDataSourceProxyMetadata(); diff --git a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/MariadbInsertExecutorTest.java b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/MariadbInsertExecutorTest.java index 51b5336af15..3d9522c4a05 100644 --- a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/MariadbInsertExecutorTest.java +++ b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/MariadbInsertExecutorTest.java @@ -19,7 +19,6 @@ import java.lang.reflect.Field; import java.sql.SQLException; import java.sql.Types; -import java.util.Arrays; import java.util.HashMap; import java.util.List; @@ -32,19 +31,16 @@ import org.apache.seata.rm.datasource.DataSourceProxyTest; import org.apache.seata.rm.datasource.PreparedStatementProxy; import org.apache.seata.rm.datasource.StatementProxy; -import org.apache.seata.rm.datasource.exec.StatementCallback; import org.apache.seata.rm.datasource.exec.mysql.MySQLInsertExecutor; import org.apache.seata.rm.datasource.mock.MockDriver; import org.apache.seata.rm.datasource.mock.MockMariadbDataSource; -import org.apache.seata.rm.datasource.mock.MockResultSet; -import org.apache.seata.sqlparser.struct.TableMeta; import org.apache.seata.sqlparser.SQLInsertRecognizer; +import org.apache.seata.sqlparser.struct.TableMeta; import org.apache.seata.sqlparser.util.JdbcConstants; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.mockito.Mockito; - import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -63,10 +59,6 @@ public void init() throws SQLException { when(statementProxy.getConnectionProxy()).thenReturn(connectionProxy); when(statementProxy.getTargetStatement()).thenReturn(statementProxy); - MockResultSet resultSet = new MockResultSet(statementProxy); - resultSet.mockResultSet(Arrays.asList("Variable_name", "Value"), new Object[][]{{"auto_increment_increment", "1"}}); - when(statementProxy.getTargetStatement().executeQuery("SHOW VARIABLES LIKE 'auto_increment_increment'")).thenReturn(resultSet); - StatementCallback statementCallback = mock(StatementCallback.class); sqlInsertRecognizer = mock(SQLInsertRecognizer.class); tableMeta = mock(TableMeta.class); diff --git a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/PolarDBXInsertExecutorTest.java b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/PolarDBXInsertExecutorTest.java index 1bb83a30379..0e0d6a57446 100644 --- a/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/PolarDBXInsertExecutorTest.java +++ b/rm-datasource/src/test/java/org/apache/seata/rm/datasource/exec/PolarDBXInsertExecutorTest.java @@ -19,7 +19,6 @@ import java.lang.reflect.Field; import java.sql.SQLException; import java.sql.Types; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Properties; @@ -33,10 +32,9 @@ import org.apache.seata.rm.datasource.DataSourceProxyTest; import org.apache.seata.rm.datasource.PreparedStatementProxy; import org.apache.seata.rm.datasource.StatementProxy; -import org.apache.seata.rm.datasource.exec.StatementCallback; import org.apache.seata.rm.datasource.exec.polardbx.PolarDBXInsertExecutor; +import org.apache.seata.rm.datasource.metadata.MySQLDataSourceProxyMetadata; import org.apache.seata.rm.datasource.mock.MockDriver; -import org.apache.seata.rm.datasource.mock.MockResultSet; import org.apache.seata.sqlparser.SQLInsertRecognizer; import org.apache.seata.sqlparser.struct.TableMeta; import org.apache.seata.sqlparser.util.JdbcConstants; @@ -48,7 +46,6 @@ /** * Insert executor test for PolarDB-X - * */ public class PolarDBXInsertExecutorTest extends MySQLInsertExecutorTest { @BeforeEach @@ -60,6 +57,9 @@ public void init() throws SQLException { DataSourceProxy dataSourceProxy = mock(DataSourceProxy.class); when(dataSourceProxy.getResourceId()).thenReturn("jdbc:mysql://127.0.0.1:3306/seata"); when(dataSourceProxy.getDbType()).thenReturn(JdbcConstants.POLARDBX); + MySQLDataSourceProxyMetadata dataSourceProxyMetadata = Mockito.mock(MySQLDataSourceProxyMetadata.class); + when(dataSourceProxyMetadata.getVariableValue("auto_increment_increment")).thenReturn("1"); + when(dataSourceProxy.getDataSourceProxyMetadata()).thenReturn(dataSourceProxyMetadata); when(connectionProxy.getDataSourceProxy()).thenReturn(dataSourceProxy); @@ -67,10 +67,6 @@ public void init() throws SQLException { when(statementProxy.getConnectionProxy()).thenReturn(connectionProxy); when(statementProxy.getTargetStatement()).thenReturn(statementProxy); - MockResultSet resultSet = new MockResultSet(statementProxy); - resultSet.mockResultSet(Arrays.asList("Variable_name", "Value"), new Object[][]{{"auto_increment_increment", "1"}}); - when(statementProxy.getTargetStatement().executeQuery("SHOW VARIABLES LIKE 'auto_increment_increment'")).thenReturn(resultSet); - StatementCallback statementCallback = mock(StatementCallback.class); sqlInsertRecognizer = mock(SQLInsertRecognizer.class); tableMeta = mock(TableMeta.class); @@ -84,21 +80,21 @@ public void init() throws SQLException { // new test init property List returnValueColumnLabels = Lists.newArrayList("id", "user_id", "name", "sex", "update_time"); - Object[][] returnValue = new Object[][] { - new Object[] {1, 1, "will", 1, 0}, + Object[][] returnValue = new Object[][]{ + new Object[]{1, 1, "will", 1, 0}, }; - Object[][] columnMetas = new Object[][] { - new Object[] {"", "", "table_insert_executor_test", "id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 2, "NO", "NO"}, - new Object[] {"", "", "table_insert_executor_test", "user_id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 2, "NO", "NO"}, - new Object[] {"", "", "table_insert_executor_test", "name", Types.VARCHAR, "VARCHAR", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "NO", "NO"}, - new Object[] {"", "", "table_insert_executor_test", "sex", Types.INTEGER, "INTEGER", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "NO", "NO"}, - new Object[] {"", "", "table_insert_executor_test", "update_time", Types.INTEGER, "INTEGER", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "YES", "NO"}, + Object[][] columnMetas = new Object[][]{ + new Object[]{"", "", "table_insert_executor_test", "id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 2, "NO", "NO"}, + new Object[]{"", "", "table_insert_executor_test", "user_id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 2, "NO", "NO"}, + new Object[]{"", "", "table_insert_executor_test", "name", Types.VARCHAR, "VARCHAR", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "NO", "NO"}, + new Object[]{"", "", "table_insert_executor_test", "sex", Types.INTEGER, "INTEGER", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "NO", "NO"}, + new Object[]{"", "", "table_insert_executor_test", "update_time", Types.INTEGER, "INTEGER", 64, 0, 10, 0, "", "", 0, 0, 64, 2, "YES", "NO"}, }; - Object[][] indexMetas = new Object[][] { - new Object[] {"PRIMARY", "id", false, "", 3, 1, "A", 34}, - new Object[] {"PRIMARY", "user_id", false, "", 3, 1, "A", 34}, + Object[][] indexMetas = new Object[][]{ + new Object[]{"PRIMARY", "id", false, "", 3, 1, "A", 34}, + new Object[]{"PRIMARY", "user_id", false, "", 3, 1, "A", 34}, }; - Object[][] onUpdateColumnsReturnValue = new Object[][] { + Object[][] onUpdateColumnsReturnValue = new Object[][]{ new Object[]{0, "update_time", Types.INTEGER, "INTEGER", 64, 10, 0, 0} };