3.獲取路由的結果
-
【 1 - 2 】【 11】獲取路由的主流程
【RouteService.java】 public RouteResultset route(SystemConfig sysconf, SchemaConfig schema, int sqlType, String stmt, String charset, ServerConnection sc) throws SQLNonTransientException { RouteResultset rrs = null; String cacheKey = null; //......省略代碼 int hintLength = RouteService.isHintSql(stmt); if(hintLength != -1){ //......省略代碼 } else { stmt = stmt.trim(); //策略模式 策略工廠?? rrs = RouteStrategyFactory.getRouteStrategy().route(sysconf, schema, sqlType, stmt, charset, sc, tableId2DataNodeCache); } if (rrs != null && sqlType == ServerParse.SELECT && rrs.isCacheAble()) { sqlRouteCache.putIfAbsent(cacheKey, rrs); } //TODO 檢查遷移路由? checkMigrateRule(schema.getName(),rrs,sqlType); return rrs; } 【AbstractRouteStrategy.java】 @Override public RouteResultset route(SystemConfig sysConfig, SchemaConfig schema, int sqlType, String origSQL, String charset, ServerConnection sc, LayerCachePool cachePool) throws SQLNonTransientException { //對應schema標籤checkSQLschema屬性,把表示schema的字符去掉 if (schema.isCheckSQLSchema()) { origSQL = RouterUtil.removeSchema(origSQL, schema.getName()); } /** * 處理一些路由之前的邏輯 * 全局序列號,父子表插入 */ if ( beforeRouteProcess(schema, sqlType, origSQL, sc) ) { return null; } /** * SQL 語句攔截 */ String stmt = MycatServer.getInstance().getSqlInterceptor().interceptSQL(origSQL, sqlType); if (!origSQL.equals(stmt) && LOGGER.isDebugEnabled()) { LOGGER.debug("sql intercepted to " + stmt + " from " + origSQL); } RouteResultset rrs = new RouteResultset(stmt, sqlType); //......省略代碼 /** * 檢查是否有分片 */ if (schema.isNoSharding() && ServerParse.SHOW != sqlType) { rrs = RouterUtil.routeToSingleNode(rrs, schema.getDataNode(), stmt); } else { RouteResultset returnedSet = routeSystemInfo(schema, sqlType, stmt, rrs); if (returnedSet == null) { rrs = routeNormalSqlWithAST(schema, stmt, rrs, charset, cachePool,sqlType,sc); } } return rrs; } /**
-
【3-6】路由的前置處理
【AbstractRouteStrategy.java】 /** * 路由之前必要的處理 * 主要是全局序列號插入,還有子表插入 */ private boolean beforeRouteProcess(SchemaConfig schema, int sqlType, String origSQL, ServerConnection sc) throws SQLNonTransientException { return // 處理 id 使用 全局序列號 RouterUtil.processWithMycatSeq(schema, sqlType, origSQL, sc) //處理ER子表 || (sqlType == ServerParse.INSERT && RouterUtil.processERChildTable(schema, origSQL, sc)) //處理ID自增 || (sqlType == ServerParse.INSERT && RouterUtil.processInsert(schema, sqlType, origSQL, sc)); }
4.獲取mysql連接,執行sql
- 1-8獲取mysql的執行過程
- 9-13發送sql到mysql的服務器,執行sql語句
5.響應sql結果
- 1到 4 處理mysql server數據響應
- 5到8發送插入成功結果給mysql