Skip to content

Commit

Permalink
Adjusting SplitTableUtil (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangweicugw authored Jul 28, 2023
1 parent b0b4507 commit ee72578
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/jd/jdbc/engine/table/TableDMLEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ private void buildActualTables(Map<String, BindVariable> bindVariableMap, List<A
bindVariableMap.put(name, SqlTypes.valueBindVariable(rowResolvedValue));
ActualTable actualTable = this.table.map(rowResolvedValue);
if (actualTable == null) {
throw new SQLException("cannot calculate split table, logic table: " + table.getLogicTable());
throw new SQLException("cannot calculate split table, logic table: " + table.getLogicTable() + "; shardingColumnValue: " + rowResolvedValue);
}
Query.Value index = Query.Value.newBuilder().setValue(ByteString.copyFrom(String.valueOf(rowNum).getBytes())).build();
if (actualTableMap.containsKey(actualTable)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private TableEngine.TableDestinationResponse paramsSelectEqual(final Map<String,
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);
}
Expand Down
22 changes: 16 additions & 6 deletions src/main/java/com/jd/jdbc/tindexes/SplitTableUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public static String getActualTableName(final String configPath, final String ke

public static String getShardingColumnName(final String configPath, final String keyspace, final String logicTableName) {
LogicTable logicTable = getLogicTable(configPath, keyspace, logicTableName);
if (logicTable == null) {
return null;
}
return logicTable.getTindexCol().getColumnName();
}

Expand All @@ -72,34 +75,41 @@ public static String getShardingColumnName(final String keyspace, final String l

private static LogicTable getLogicTable(String configPath, String keyspace, String logicTableName) {
if (StringUtils.isEmpty(keyspace) || StringUtils.isEmpty(logicTableName)) {
throw new RuntimeException("keyspace or logicTableName should not empty");
logger.warn("keyspace or logicTableName should not empty");
return null;
}
Map<String, Map<String, LogicTable>> 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;
}
Expand Down
34 changes: 23 additions & 11 deletions src/test/java/com/jd/jdbc/table/SplitTableUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@

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;

public class SplitTableUtilTest {

@Test
public void getActualTableNames() {
List<Integer> 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
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down

0 comments on commit ee72578

Please sign in to comment.