/** * 增加判断支持未配置分片的表走默认的dataNode * * @param schemaConfig * @param tableName * @return */ public static boolean isNoSharding(SchemaConfig schemaConfig, String tableName) { if (schemaConfig.isNoSharding()) { return true; } if (schemaConfig.getDataNode() != null && !schemaConfig.getTables().containsKey(tableName)) { return true; } return false; }
/** * 多表路由 * * @param schema * @param ctx * @param tables * @param rrs * @param isSelect * @return * @throws SQLNonTransientException */ public static RouteResultset tryRouteForTables( SchemaConfig schema, DruidShardingParseInfo ctx, RouteCalculateUnit routeUnit, RouteResultset rrs, boolean isSelect, LayerCachePool cachePool) throws SQLNonTransientException { List<String> tables = ctx.getTables(); if (schema.isNoSharding() || (tables.size() >= 1 && isNoSharding(schema, tables.get(0)))) { return routeToSingleNode(rrs, schema.getDataNode(), ctx.getSql()); } // 只有一个表的 if (tables.size() == 1) { return RouterUtil.tryRouteForOneTable( schema, ctx, routeUnit, tables.get(0), rrs, isSelect, cachePool); } Set<String> retNodesSet = new HashSet<String>(); // 每个表对应的路由映射 Map<String, Set<String>> tablesRouteMap = new HashMap<String, Set<String>>(); // 分库解析信息不为空 Map<String, Map<String, Set<ColumnRoutePair>>> tablesAndConditions = routeUnit.getTablesAndConditions(); if (tablesAndConditions != null && tablesAndConditions.size() > 0) { // 为分库表找路由 RouterUtil.findRouteWithcConditionsForTables( schema, rrs, tablesAndConditions, tablesRouteMap, ctx.getSql(), cachePool, isSelect); if (rrs.isFinishedRoute()) { return rrs; } } // 为全局表和单库表找路由 for (String tableName : tables) { TableConfig tableConfig = schema.getTables().get(tableName.toUpperCase()); 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()) { // 全局表 if (tablesRouteMap.get(tableName) == null) { tablesRouteMap.put(tableName, new HashSet<String>()); } tablesRouteMap.get(tableName).addAll(tableConfig.getDataNodes()); } else if (tablesRouteMap.get(tableName) == null) { // 余下的表都是单库表 tablesRouteMap.put(tableName, new HashSet<String>()); tablesRouteMap.get(tableName).addAll(tableConfig.getDataNodes()); } } boolean isFirstAdd = true; for (Map.Entry<String, Set<String>> entry : tablesRouteMap.entrySet()) { if (entry.getValue() == null || entry.getValue().size() == 0) { throw new SQLNonTransientException("parent key can't find any valid datanode "); } else { if (isFirstAdd) { retNodesSet.addAll(entry.getValue()); isFirstAdd = false; } else { retNodesSet.retainAll(entry.getValue()); if (retNodesSet.size() == 0) { // 两个表的路由无交集 String errMsg = "invalid route in sql, multi tables found but datanode has no intersection " + " sql:" + ctx.getSql(); LOGGER.warn(errMsg); throw new SQLNonTransientException(errMsg); } } } } if (retNodesSet != null && retNodesSet.size() > 0) { if (retNodesSet.size() > 1 && isAllGlobalTable(ctx, schema)) { // mulit routes ,not cache route result if (isSelect) { rrs.setCacheAble(false); routeToSingleNode(rrs, retNodesSet.iterator().next(), ctx.getSql()); } else { // delete 删除全局表的记录 routeToMultiNode(isSelect, rrs, retNodesSet, ctx.getSql(), true); } } else { routeToMultiNode(isSelect, rrs, retNodesSet, ctx.getSql()); } } return rrs; }