/** * valid the consistency between table's node size and rule's node size * * @param tableConf * @return -1 if table datanode size < rule function partition size * 0 if table datanode size == rule function partition size * 1 if table datanode size > rule function partition size */ public final int suitableFor(TableConfig tableConf) { int nPartition = getPartitionNum(); if (nPartition > 0) { int dnSize = tableConf.getDataNodes().size(); if (dnSize < nPartition) { return -1; } else if (dnSize > nPartition) { return 1; } } return 0; }
private void setFunctionAlias() { Map<AbstractPartitionAlgorithm, String> funcAlias = new HashMap<>(); int i = 0; for (AbstractPartitionAlgorithm function : functions.values()) { String alias = funcAlias.get(function); if (alias != null) { function.setAlias(alias); } else { alias = "function" + i; i++; function.setAlias(alias); funcAlias.put(function, alias); } } }
for (ColumnRoutePair colPair : colRoutePairSet) { if (colPair.colValue != null) { Integer nodeIndex = algorithm.calculate(colPair.colValue); if (nodeIndex == null) { throw new IllegalArgumentException("can't find datanode for sharding column:" + col + " val:" + colPair.colValue); Integer[] nodeRange = algorithm.calculateRange(String.valueOf(colPair.rangeValue.getBeginValue()), String.valueOf(colPair.rangeValue.getEndValue())); if (nodeRange != null) {
/** * shard table datanode(2) < function count(3) and check failed */ private void checkRuleSuitTable(TableConfig tableConf) { AbstractPartitionAlgorithm function = tableConf.getRule().getRuleAlgorithm(); int suitValue = function.suitableFor(tableConf); if (suitValue < 0) { throw new ConfigException("Illegal table conf : table [ " + tableConf.getName() + " ] rule function [ " + tableConf.getRule().getFunctionName() + " ] partition size : " + tableConf.getRule().getRuleAlgorithm().getPartitionNum() + " > table datanode size : " + tableConf.getDataNodes().size() + ", please make sure table datanode size = function partition size"); } else if (suitValue > 0) { problemReporter.warn("table conf : table [ " + tableConf.getName() + " ] rule function [ " + tableConf.getRule().getFunctionName() + " ] " + "partition size : " + String.valueOf(tableConf.getRule().getRuleAlgorithm().getPartitionNum()) + " < table datanode size : " + String.valueOf(tableConf.getDataNodes().size())); } else { // table data node size == rule function partition size } }
Integer nodeIndex; try { nodeIndex = algorithm.calculate(pair.colValue); } catch (Exception e) { return false;
continue; String key = tc.getRule().getRuleAlgorithm().getName() + "_" + root.getDataNodes().toString(); String column = root.getRule().getColumn(); if (funcNodeERMap == null) { if (tc.getDirectRouteTC() != null) { TableConfig root = tc.getDirectRouteTC(); String key = root.getRule().getRuleAlgorithm().getAlias() + "_" + root.getDataNodes().toString(); if (funcNodeERMap == null) { funcNodeERMap = new HashMap<>();
classRow.add(StringUtil.encode(rule.getRuleAlgorithm().getClass().getName(), charset)); list.add(classRow); for (Map.Entry<String, String> pairs : rule.getRuleAlgorithm().getAllProperties().entrySet()) { RowDataPacket propertyRow = new RowDataPacket(FIELD_COUNT); propertyRow.add(StringUtil.encode(pairs.getKey(), charset));
return; Integer nodeIndex = algorithm.calculate(pair.colValue); if (nodeIndex == null) { String msg = "can't find any valid data node :" + tableConfig.getName() + Integer[] nodeIndexes = algorithm.calculateRange( pair.rangeValue.getBeginValue().toString(), pair.rangeValue.getEndValue().toString()); ArrayList<String> dataNodes = tableConfig.getDataNodes();
Integer nodeIndex = algorithm.calculate(shardingValue);
String shardingValue = shardingValueToSting(expr); checkDefaultValues(shardingValue, tableConfig, schemaInfo.getSchema(), partitionColumn); Integer nodeIndex = algorithm.calculate(shardingValue);
/** * insert single record * * @param schemaInfo SchemaInfo * @param rrs RouteResultset * @param partitionColumn partitionColumn * @param replaceStatement SQLReplaceStatement * @throws SQLNonTransientException if not find a valid data node */ private void parserSingleInsert(SchemaInfo schemaInfo, RouteResultset rrs, String partitionColumn, SQLReplaceStatement replaceStatement) throws SQLNonTransientException { int shardingColIndex = tryGetShardingColIndex(schemaInfo, replaceStatement, partitionColumn); SQLExpr valueExpr = replaceStatement.getValuesList().get(0).getValues().get(shardingColIndex); String shardingValue = shardingValueToSting(valueExpr); TableConfig tableConfig = schemaInfo.getSchemaConfig().getTables().get(schemaInfo.getTable()); AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm(); Integer nodeIndex = algorithm.calculate(shardingValue); if (nodeIndex == null) { String msg = "can't find any valid data node :" + schemaInfo.getTable() + " -> " + partitionColumn + " -> " + shardingValue; LOGGER.info(msg); throw new SQLNonTransientException(msg); } RouteResultsetNode[] nodes = new RouteResultsetNode[1]; nodes[0] = new RouteResultsetNode(tableConfig.getDataNodes().get(nodeIndex), rrs.getSqlType(), RouterUtil.removeSchema(statementToString(replaceStatement), schemaInfo.getSchema())); rrs.setNodes(nodes); rrs.setFinishedRoute(true); } }
AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm(); checkDefaultValues(shardingValue, tableConfig, schemaInfo.getSchema(), partitionColumn); Integer nodeIndex = algorithm.calculate(shardingValue); if (nodeIndex == null || nodeIndex >= tableConfig.getDataNodes().size()) { String msg = "can't find any valid data node :" + schemaInfo.getTable() + " -> " + partitionColumn + " -> " + shardingValue;