Exemple #1
0
 /**
  * 根据 ER分片规则获取路由集合
  *
  * @param stmt 执行的语句
  * @param rrs 数据路由集合
  * @param tc 表实体
  * @param joinKeyVal 连接属性
  * @return RouteResultset(数据路由集合)
  * @throws SQLNonTransientException
  * @author mycat
  */
 public static RouteResultset routeByERParentKey(
     MySQLFrontConnection sc,
     SchemaConfig schema,
     int sqlType,
     String stmt,
     RouteResultset rrs,
     TableConfig tc,
     String joinKeyVal)
     throws SQLNonTransientException {
   // only has one parent level and ER parent key is parent
   // 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);
     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;
 }
Exemple #2
0
  /** @return dataNodeIndex -&gt; [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 it = colRoutePairSet.iterator();
      ColumnRoutePair joinCol = (ColumnRoutePair) it.next();
      joinValue = joinCol.colValue;
    }

    Set<String> retNodeSet = new LinkedHashSet<String>();

    Set<String> nodeSet = new LinkedHashSet<String>();
    if (tc.isSecondLevel()
        && tc.getParentTC().getPartitionColumn().equals(tc.getParentKey())) { // using
      // parent
      // rule to
      // find
      // datanode

      nodeSet = ruleCalculate(tc.getParentTC(), colRoutePairSet);
      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);

      //			for(ColumnRoutePair pair : colRoutePairSet) {
      //				nodeSet = ruleCalculate(tc.getParentTC(),colRoutePairSet);
      //				if (nodeSet.isEmpty() || nodeSet.size() > 1) {//an exception would be thrown, if sql was
      // executed on more than on sharding
      //					throw new SQLNonTransientException(
      //							"parent key can't find  valid datanode ,expect 1 but found: "
      //									+ nodeSet.size());
      //				}
      //				String dn = nodeSet.iterator().next();
      //				if (LOGGER.isDebugEnabled()) {
      //					LOGGER.debug("found partion node (using parent partion rule directly) for child table
      // to insert  "
      //							+ dn + " sql :" + rrs.getStatement());
      //				}
      //				retNodeSet.addAll(nodeSet);
      //			}
      return retNodeSet;
    } else {
      retNodeSet.addAll(tc.getParentTC().getDataNodes());
    }

    return retNodeSet;
  }