From ee725789b089809e72b4c7b7bcdcbd6d72dec4e3 Mon Sep 17 00:00:00 2001 From: wangweicugw <38103831+wangweicugw@users.noreply.github.com> Date: Fri, 28 Jul 2023 15:02:09 +0800 Subject: [PATCH] Adjusting SplitTableUtil (#100) --- .../jd/jdbc/engine/table/TableDMLEngine.java | 2 +- .../jdbc/engine/table/TableInsertEngine.java | 2 +- .../jdbc/engine/table/TableRouteEngine.java | 2 +- .../com/jd/jdbc/tindexes/SplitTableUtil.java | 22 ++++++++---- .../com/jd/jdbc/table/SplitTableUtilTest.java | 34 +++++++++++++------ .../table/TableAutoGeneratedKeysTest.java | 2 +- 6 files changed, 43 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/jd/jdbc/engine/table/TableDMLEngine.java b/src/main/java/com/jd/jdbc/engine/table/TableDMLEngine.java index 341e219..7510262 100644 --- a/src/main/java/com/jd/jdbc/engine/table/TableDMLEngine.java +++ b/src/main/java/com/jd/jdbc/engine/table/TableDMLEngine.java @@ -79,7 +79,7 @@ protected TableEngine.TableDestinationResponse getResolvedTablesEqual(final Map< for (LogicTable ltb : this.logicTables) { ActualTable actualTable = ltb.map(value); if (actualTable == null) { - throw new SQLException("cannot calculate split table, logic table: " + ltb.getLogicTable()); + throw new SQLException("cannot calculate split table, logic table: " + ltb.getLogicTable() + "; shardingColumnValue: " + value); } actualTables.add(actualTable); } diff --git a/src/main/java/com/jd/jdbc/engine/table/TableInsertEngine.java b/src/main/java/com/jd/jdbc/engine/table/TableInsertEngine.java index e02075b..ad675eb 100644 --- a/src/main/java/com/jd/jdbc/engine/table/TableInsertEngine.java +++ b/src/main/java/com/jd/jdbc/engine/table/TableInsertEngine.java @@ -234,7 +234,7 @@ private void buildActualTables(Map bindVariableMap, List> tableIndexesMap = getTableIndexesMap(configPath); if (tableIndexesMap == null || tableIndexesMap.isEmpty()) { - throw new RuntimeException("cat not find split-table config through configPath=" + configPath); + logger.warn("cannot find split-table config through configPath=" + configPath); + return null; } String lowerCaseKeyspace = keyspace.toLowerCase(); String lowerCaseLogicTable = logicTableName.toLowerCase(); if (!tableIndexesMap.containsKey(lowerCaseKeyspace)) { - throw new RuntimeException("cat not find keyspace in split-table config, target keyspace=" + keyspace); + logger.warn("cannot find keyspace in split-table config, target keyspace=" + keyspace); + return null; } if (!tableIndexesMap.get(lowerCaseKeyspace).containsKey(lowerCaseLogicTable)) { - throw new RuntimeException("cat not find logicTable in split-table config, target keyspace=" + keyspace + " ,target logicTable=" + logicTableName); + logger.warn("cannot find logicTable in split-table config, target keyspace=" + keyspace + " ,target logicTable=" + logicTableName); + return null; } return tableIndexesMap.get(lowerCaseKeyspace).get(lowerCaseLogicTable); } private static ActualTable getActualTable(String configPath, String keyspace, String logicTableName, Object value) { - LogicTable logicTable = getLogicTable(configPath, keyspace, logicTableName); VtValue vtValue; try { vtValue = VtValue.toVtValue(value); } catch (SQLException e) { throw new RuntimeException(e); } + LogicTable logicTable = getLogicTable(configPath, keyspace, logicTableName); + if (logicTable == null) { + throw new RuntimeException("get split-table config fail"); + } final ActualTable actualTable = logicTable.map(vtValue); if (actualTable == null) { - throw new RuntimeException("cannot calculate split table, logic table: " + logicTable.getLogicTable() + ",current value: " + vtValue); + throw new RuntimeException("cannot calculate split table, logic table: " + logicTable.getLogicTable() + "; shardingColumnValue: " + vtValue); } return actualTable; } diff --git a/src/test/java/com/jd/jdbc/table/SplitTableUtilTest.java b/src/test/java/com/jd/jdbc/table/SplitTableUtilTest.java index 3d29392..9514d63 100644 --- a/src/test/java/com/jd/jdbc/table/SplitTableUtilTest.java +++ b/src/test/java/com/jd/jdbc/table/SplitTableUtilTest.java @@ -16,9 +16,9 @@ package com.jd.jdbc.table; -import com.google.common.collect.Lists; +import com.jd.jdbc.sqlparser.utils.StringUtils; import com.jd.jdbc.tindexes.SplitTableUtil; -import java.util.List; +import org.apache.commons.lang3.RandomUtils; import org.junit.Assert; import org.junit.Test; @@ -26,17 +26,17 @@ public class SplitTableUtilTest { @Test public void getActualTableNames() { - List ids = Lists.newArrayList(713, 714, 715, 716, 717, 718); - for (Integer id : ids) { - String actualTableName = SplitTableUtil.getActualTableName("vtdriver-split-table.yml", "commerce", "table_engine_test", id); - System.out.println(String.format("id=%s,actualTableName=%s", id, actualTableName)); + int start = RandomUtils.nextInt(); + for (int i = start; i < start + 10; i++) { + String actualTableName = SplitTableUtil.getActualTableName("vtdriver-split-table.yml", "commerce", "table_engine_test", i); + Assert.assertTrue("actualTableName should not empty", StringUtils.isNotEmpty(actualTableName)); } } @Test - public void testGetActualTableName() { - String actualTableName = SplitTableUtil.getActualTableName("commerce", "table_engine_test", 111); - System.out.println(actualTableName); + public void getActualTableName() { + String actualTableName = SplitTableUtil.getActualTableName("commerce", "table_engine_test", RandomUtils.nextInt()); + Assert.assertTrue("actualTableName should not empty", StringUtils.isNotEmpty(actualTableName)); } @Test @@ -47,7 +47,19 @@ public void getShardingColumnName() { @Test public void getShardingColumnName2() { - String shardingColumnName = SplitTableUtil.getShardingColumnName("vtdriver-split-table.yml", "commerce", "table_engine_test"); - Assert.assertTrue("getShardingColumnName error", "f_key".equalsIgnoreCase(shardingColumnName)); + String shardingColumnName = SplitTableUtil.getShardingColumnName("commerce", "table_engine_test3"); + Assert.assertNull(shardingColumnName); + } + + @Test + public void getShardingColumnName3() { + String shardingColumnName = SplitTableUtil.getShardingColumnName("commerce2", "table_engine_test"); + Assert.assertNull(shardingColumnName); + } + + @Test + public void getShardingColumnName4() { + String shardingColumnName = SplitTableUtil.getShardingColumnName("commerce3", "table_engine_test3"); + Assert.assertNull(shardingColumnName); } } \ No newline at end of file diff --git a/src/test/java/com/jd/jdbc/table/TableAutoGeneratedKeysTest.java b/src/test/java/com/jd/jdbc/table/TableAutoGeneratedKeysTest.java index 605a035..78eb244 100644 --- a/src/test/java/com/jd/jdbc/table/TableAutoGeneratedKeysTest.java +++ b/src/test/java/com/jd/jdbc/table/TableAutoGeneratedKeysTest.java @@ -91,7 +91,7 @@ public void test23SetNull() throws Exception { stmt.setInt(1, 100); stmt.setNull(2, 0); thrown.expect(SQLException.class); - thrown.expectMessage("cannot calculate split table, logic table: table_auto"); + thrown.expectMessage("cannot calculate split table, logic table: table_auto; shardingColumnValue: null"); stmt.executeUpdate(); } }