use of io.mycat.sqlengine.mpp.ColumnRoutePair in project Mycat-Server by MyCATApache.
the class RouterUtil method ruleByJoinValueCalculate.
/**
* @return dataNodeIndex -> [partitionKeysValueTuple+]
*/
public static Set<String> ruleByJoinValueCalculate(RouteResultset rrs, TableConfig tc, Set<ColumnRoutePair> colRoutePairSet) throws SQLNonTransientException {
String joinValue = "";
if (colRoutePairSet.size() > 1) {
LOGGER.warn("joinKey can't have multi Value");
} else {
Iterator<ColumnRoutePair> it = colRoutePairSet.iterator();
ColumnRoutePair joinCol = it.next();
joinValue = joinCol.colValue;
}
Set<String> retNodeSet = new LinkedHashSet<String>();
Set<String> nodeSet;
if (tc.isSecondLevel() && tc.getParentTC().getPartitionColumn().equals(tc.getParentKey())) {
// using
// parent
// rule to
// find
// datanode
nodeSet = ruleCalculate(tc.getParentTC(), colRoutePairSet, rrs.getDataNodeSlotMap());
if (nodeSet.isEmpty()) {
throw new SQLNonTransientException("parent key can't find valid datanode ,expect 1 but found: " + nodeSet.size());
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("found partion node (using parent partion rule directly) for child table to insert " + nodeSet + " sql :" + rrs.getStatement());
}
retNodeSet.addAll(nodeSet);
// }
return retNodeSet;
} else {
retNodeSet.addAll(tc.getParentTC().getDataNodes());
}
return retNodeSet;
}
use of io.mycat.sqlengine.mpp.ColumnRoutePair in project Mycat_plus by coderczp.
the class RouterUtil method ruleByJoinValueCalculate.
/**
* @return dataNodeIndex -> [partitionKeysValueTuple+]
*/
public static Set<String> ruleByJoinValueCalculate(RouteResultset rrs, TableConfig tc, Set<ColumnRoutePair> colRoutePairSet) throws SQLNonTransientException {
String joinValue = "";
if (colRoutePairSet.size() > 1) {
LOGGER.warn("joinKey can't have multi Value");
} else {
Iterator<ColumnRoutePair> it = colRoutePairSet.iterator();
ColumnRoutePair joinCol = it.next();
joinValue = joinCol.colValue;
}
Set<String> retNodeSet = new LinkedHashSet<String>();
Set<String> nodeSet;
if (tc.isSecondLevel() && tc.getParentTC().getPartitionColumn().equals(tc.getParentKey())) {
// using
// parent
// rule to
// find
// datanode
nodeSet = ruleCalculate(tc.getParentTC(), colRoutePairSet, rrs.getDataNodeSlotMap());
if (nodeSet.isEmpty()) {
throw new SQLNonTransientException("parent key can't find valid datanode ,expect 1 but found: " + nodeSet.size());
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("found partion node (using parent partion rule directly) for child table to insert " + nodeSet + " sql :" + rrs.getStatement());
}
retNodeSet.addAll(nodeSet);
// }
return retNodeSet;
} else {
retNodeSet.addAll(tc.getParentTC().getDataNodes());
}
return retNodeSet;
}
use of io.mycat.sqlengine.mpp.ColumnRoutePair in project Mycat_plus by coderczp.
the class RouterUtil method ruleCalculate.
/**
* @return dataNodeIndex -> [partitionKeysValueTuple+]
*/
public static Set<String> ruleCalculate(TableConfig tc, Set<ColumnRoutePair> colRoutePairSet, Map<String, Integer> dataNodeSlotMap) {
Set<String> routeNodeSet = new LinkedHashSet<String>();
String col = tc.getRule().getColumn();
RuleConfig rule = tc.getRule();
AbstractPartitionAlgorithm algorithm = rule.getRuleAlgorithm();
for (ColumnRoutePair colPair : colRoutePairSet) {
if (colPair.colValue != null) {
Integer nodeIndx = algorithm.calculate(colPair.colValue);
if (nodeIndx == null) {
throw new IllegalArgumentException("can't find datanode for sharding column:" + col + " val:" + colPair.colValue);
} else {
String dataNode = tc.getDataNodes().get(nodeIndx);
routeNodeSet.add(dataNode);
if (algorithm instanceof SlotFunction) {
dataNodeSlotMap.put(dataNode, ((SlotFunction) algorithm).slotValue());
}
colPair.setNodeId(nodeIndx);
}
} else if (colPair.rangeValue != null) {
Integer[] nodeRange = algorithm.calculateRange(String.valueOf(colPair.rangeValue.beginValue), String.valueOf(colPair.rangeValue.endValue));
if (nodeRange != null) {
/**
* 不能确认 colPair的 nodeid是否会有其它影响
*/
if (nodeRange.length == 0) {
routeNodeSet.addAll(tc.getDataNodes());
} else {
ArrayList<String> dataNodes = tc.getDataNodes();
String dataNode = null;
for (Integer nodeId : nodeRange) {
dataNode = dataNodes.get(nodeId);
if (algorithm instanceof SlotFunction) {
dataNodeSlotMap.put(dataNode, ((SlotFunction) algorithm).slotValue());
}
routeNodeSet.add(dataNode);
}
}
}
}
}
return routeNodeSet;
}
use of io.mycat.sqlengine.mpp.ColumnRoutePair in project Mycat_plus by coderczp.
the class RouterUtil method routeToDistTableNode.
private static RouteResultset routeToDistTableNode(String tableName, SchemaConfig schema, RouteResultset rrs, String orgSql, Map<String, Map<String, Set<ColumnRoutePair>>> tablesAndConditions, LayerCachePool cachePool, boolean isSelect) throws SQLNonTransientException {
TableConfig tableConfig = schema.getTables().get(tableName);
if (tableConfig == null) {
String msg = "can't find table define in schema " + tableName + " schema:" + schema.getName();
LOGGER.warn(msg);
throw new SQLNonTransientException(msg);
}
if (tableConfig.isGlobalTable()) {
String msg = "can't suport district table " + tableName + " schema:" + schema.getName() + " for global table ";
LOGGER.warn(msg);
throw new SQLNonTransientException(msg);
}
String partionCol = tableConfig.getPartitionColumn();
// String primaryKey = tableConfig.getPrimaryKey();
boolean isLoadData = false;
Set<String> tablesRouteSet = new HashSet<String>();
List<String> dataNodes = tableConfig.getDataNodes();
if (dataNodes.size() > 1) {
String msg = "can't suport district table " + tableName + " schema:" + schema.getName() + " for mutiple dataNode " + dataNodes;
LOGGER.warn(msg);
throw new SQLNonTransientException(msg);
}
String dataNode = dataNodes.get(0);
// 主键查找缓存暂时不实现
if (tablesAndConditions.isEmpty()) {
List<String> subTables = tableConfig.getDistTables();
tablesRouteSet.addAll(subTables);
}
for (Map.Entry<String, Map<String, Set<ColumnRoutePair>>> entry : tablesAndConditions.entrySet()) {
boolean isFoundPartitionValue = partionCol != null && entry.getValue().get(partionCol) != null;
Map<String, Set<ColumnRoutePair>> columnsMap = entry.getValue();
Set<ColumnRoutePair> partitionValue = columnsMap.get(partionCol);
if (partitionValue == null || partitionValue.size() == 0) {
tablesRouteSet.addAll(tableConfig.getDistTables());
} else {
for (ColumnRoutePair pair : partitionValue) {
AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
if (pair.colValue != null) {
Integer tableIndex = algorithm.calculate(pair.colValue);
if (tableIndex == null) {
String msg = "can't find any valid datanode :" + tableConfig.getName() + " -> " + tableConfig.getPartitionColumn() + " -> " + pair.colValue;
LOGGER.warn(msg);
throw new SQLNonTransientException(msg);
}
String subTable = tableConfig.getDistTables().get(tableIndex);
if (subTable != null) {
tablesRouteSet.add(subTable);
if (algorithm instanceof SlotFunction) {
rrs.getDataNodeSlotMap().put(subTable, ((SlotFunction) algorithm).slotValue());
}
}
}
if (pair.rangeValue != null) {
Integer[] tableIndexs = algorithm.calculateRange(pair.rangeValue.beginValue.toString(), pair.rangeValue.endValue.toString());
for (Integer idx : tableIndexs) {
String subTable = tableConfig.getDistTables().get(idx);
if (subTable != null) {
tablesRouteSet.add(subTable);
if (algorithm instanceof SlotFunction) {
rrs.getDataNodeSlotMap().put(subTable, ((SlotFunction) algorithm).slotValue());
}
}
}
}
}
}
}
Object[] subTables = tablesRouteSet.toArray();
RouteResultsetNode[] nodes = new RouteResultsetNode[subTables.length];
Map<String, Integer> dataNodeSlotMap = rrs.getDataNodeSlotMap();
for (int i = 0; i < nodes.length; i++) {
String table = String.valueOf(subTables[i]);
String changeSql = orgSql;
// rrs.getStatement()
nodes[i] = new RouteResultsetNode(dataNode, rrs.getSqlType(), changeSql);
nodes[i].setSubTableName(table);
nodes[i].setSource(rrs);
if (rrs.getDataNodeSlotMap().containsKey(dataNode)) {
nodes[i].setSlot(rrs.getDataNodeSlotMap().get(dataNode));
}
if (rrs.getCanRunInReadDB() != null) {
nodes[i].setCanRunInReadDB(rrs.getCanRunInReadDB());
}
if (dataNodeSlotMap.containsKey(table)) {
nodes[i].setSlot(dataNodeSlotMap.get(table));
}
if (rrs.getRunOnSlave() != null) {
nodes[0].setRunOnSlave(rrs.getRunOnSlave());
}
}
rrs.setNodes(nodes);
rrs.setSubTables(tablesRouteSet);
rrs.setFinishedRoute(true);
return rrs;
}
use of io.mycat.sqlengine.mpp.ColumnRoutePair in project Mycat_plus by coderczp.
the class RouterUtil method routeByERParentKey.
/**
* 根据 ER分片规则获取路由集合
*
* @param stmt 执行的语句
* @param rrs 数据路由集合
* @param tc 表实体
* @param joinKeyVal 连接属性
* @return RouteResultset(数据路由集合) *
* @throws SQLNonTransientException,IllegalShardingColumnValueException
* @author mycat
*/
public static RouteResultset routeByERParentKey(ServerConnection sc, SchemaConfig schema, int sqlType, String stmt, RouteResultset rrs, TableConfig tc, String joinKeyVal) throws SQLNonTransientException {
// table's partition key
if (tc.isSecondLevel() && // 判断是否为二级子表(父表不再有父表)
tc.getParentTC().getPartitionColumn().equals(tc.getParentKey())) {
// using
// parent
// rule to
// find
// datanode
Set<ColumnRoutePair> parentColVal = new HashSet<ColumnRoutePair>(1);
ColumnRoutePair pair = new ColumnRoutePair(joinKeyVal);
parentColVal.add(pair);
Set<String> dataNodeSet = ruleCalculate(tc.getParentTC(), parentColVal, rrs.getDataNodeSlotMap());
if (dataNodeSet.isEmpty() || dataNodeSet.size() > 1) {
throw new SQLNonTransientException("parent key can't find valid datanode ,expect 1 but found: " + dataNodeSet.size());
}
String dn = dataNodeSet.iterator().next();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("found partion node (using parent partion rule directly) for child table to insert " + dn + " sql :" + stmt);
}
return RouterUtil.routeToSingleNode(rrs, dn, stmt);
}
return null;
}
Aggregations