remove sqlInterceptor

This commit is contained in:
yanhuqing666
2017-04-15 17:23:40 +08:00
parent 9eb70aac3f
commit 79945e8015
15 changed files with 12 additions and 343 deletions
+1 -14
View File
@@ -92,8 +92,7 @@ import io.mycat.route.sequence.handler.IncrSequenceTimeHandler;
import io.mycat.route.sequence.handler.IncrSequenceZKHandler;
import io.mycat.route.sequence.handler.SequenceHandler;
import io.mycat.server.ServerConnectionFactory;
import io.mycat.server.interceptor.SQLInterceptor;
import io.mycat.server.interceptor.impl.GlobalTableUtil;
import io.mycat.server.util.GlobalTableUtil;
import io.mycat.sqlengine.OneRawSQLQueryResultHandler;
import io.mycat.sqlengine.SQLJob;
import io.mycat.statistic.stat.SqlResultSizeRecorder;
@@ -131,7 +130,6 @@ public class MycatServer {
private volatile int channelIndex = 0;
private final DynaClassLoader catletClassLoader;
private final SQLInterceptor sqlInterceptor;
private volatile int nextProcessor;
// System Buffer Pool Instance
@@ -187,13 +185,6 @@ public class MycatServer {
// load datanode active index from properties
dnIndexProperties = loadDnIndexProps();
try {
//SQL解析器
sqlInterceptor = (SQLInterceptor) Class.forName(
config.getSystem().getSqlInterceptor()).newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
//catlet加载器
catletClassLoader = new DynaClassLoader(SystemConfig.getHomePath()
@@ -229,10 +220,6 @@ public class MycatServer {
return catletClassLoader;
}
public SQLInterceptor getSqlInterceptor() {
return sqlInterceptor;
}
public String genXATXID() {
long seq = this.xaIDInc.incrementAndGet();
if (seq < 0) {
@@ -35,7 +35,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.mycat.backend.mysql.nio.MySQLDataSource;
import io.mycat.server.interceptor.impl.GlobalTableUtil;
import io.mycat.server.util.GlobalTableUtil;
import io.mycat.sqlengine.OneRawSQLQueryResultHandler;
import io.mycat.sqlengine.SQLJob;
import io.mycat.sqlengine.SQLQueryResult;
@@ -33,7 +33,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import io.mycat.server.interceptor.impl.GlobalTableUtil;
import io.mycat.server.util.GlobalTableUtil;
import io.mycat.sqlengine.SQLJob;
import io.mycat.sqlengine.SQLQueryResult;
import io.mycat.sqlengine.SQLQueryResultListener;
@@ -124,9 +124,6 @@ public final class SystemConfig {
//清理大结果集记录周期
private long clearBigSqLResultSetMapMs=10*60*1000;
private int sequnceHandlerType = SEQUENCEHANDLER_LOCAL_TIME;
private String sqlInterceptor = "io.mycat.server.interceptor.impl.DefaultSqlInterceptor";
private String sqlInterceptorType = "select";
private String sqlInterceptorFile = System.getProperty("user.dir")+"/logs/sql.txt";
private String defaultSqlParser = DEFAULT_SQL_PARSER;
private int usingAIO = 0;
private int packetHeaderSize = 4;
@@ -370,14 +367,6 @@ public final class SystemConfig {
this.glableTableCheckPeriod = glableTableCheckPeriod;
}
public String getSqlInterceptor() {
return sqlInterceptor;
}
public void setSqlInterceptor(String sqlInterceptor) {
this.sqlInterceptor = sqlInterceptor;
}
public int getSequnceHandlerType() {
return sequnceHandlerType;
}
@@ -745,22 +734,6 @@ public final class SystemConfig {
this.backSocketNoDelay = backSocketNoDelay;
}
public String getSqlInterceptorType() {
return sqlInterceptorType;
}
public void setSqlInterceptorType(String sqlInterceptorType) {
this.sqlInterceptorType = sqlInterceptorType;
}
public String getSqlInterceptorFile() {
return sqlInterceptorFile;
}
public void setSqlInterceptorFile(String sqlInterceptorFile) {
this.sqlInterceptorFile = sqlInterceptorFile;
}
public int getUsingAIO() {
return usingAIO;
}
@@ -814,9 +787,6 @@ public final class SystemConfig {
+ ", bufferUsagePercent="+bufferUsagePercent
+ ", clearBigSqLResultSetMapMs="+clearBigSqLResultSetMapMs
+ ", sequnceHandlerType=" + sequnceHandlerType
+ ", sqlInterceptor=" + sqlInterceptor
+ ", sqlInterceptorType=" + sqlInterceptorType
+ ", sqlInterceptorFile=" + sqlInterceptorFile
+ ", defaultSqlParser=" + defaultSqlParser
+ ", usingAIO=" + usingAIO
+ ", packetHeaderSize=" + packetHeaderSize
@@ -6,7 +6,6 @@ import java.sql.SQLSyntaxErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.mycat.MycatServer;
import io.mycat.cache.LayerCachePool;
import io.mycat.config.model.SchemaConfig;
import io.mycat.config.model.SystemConfig;
@@ -22,16 +21,8 @@ public abstract class AbstractRouteStrategy implements RouteStrategy {
@Override
public RouteResultset route(SystemConfig sysConfig, SchemaConfig schema, int sqlType, String origSQL,
String charset, ServerConnection sc, LayerCachePool cachePool) throws SQLNonTransientException {
/**
* 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, sc.getSession2());
RouteResultset rrs = new RouteResultset(origSQL, sqlType, sc.getSession2());
/**
* 优化debug loaddata输出cache的日志会极大降低性能
@@ -49,11 +40,11 @@ public abstract class AbstractRouteStrategy implements RouteStrategy {
}
if (schema == null) {
rrs = routeNormalSqlWithAST(schema, stmt, rrs, charset, cachePool);
rrs = routeNormalSqlWithAST(schema, origSQL, rrs, charset, cachePool);
} else {
RouteResultset returnedSet = routeSystemInfo(schema, sqlType, stmt, rrs);
RouteResultset returnedSet = routeSystemInfo(schema, sqlType, origSQL, rrs);
if (returnedSet == null) {
rrs = routeNormalSqlWithAST(schema, stmt, rrs, charset, cachePool);
rrs = routeNormalSqlWithAST(schema, origSQL, rrs, charset, cachePool);
}
}
@@ -33,7 +33,7 @@ import io.mycat.route.RouteResultsetNode;
import io.mycat.route.function.AbstractPartitionAlgorithm;
import io.mycat.route.parser.druid.MycatSchemaStatVisitor;
import io.mycat.route.util.RouterUtil;
import io.mycat.server.interceptor.impl.GlobalTableUtil;
import io.mycat.server.util.GlobalTableUtil;
import io.mycat.server.util.SchemaUtil;
import io.mycat.server.util.SchemaUtil.SchemaInfo;
import io.mycat.sqlengine.mpp.ColumnRoutePair;
@@ -32,7 +32,7 @@ import io.mycat.meta.protocol.MyCatMeta.TableMeta;
import io.mycat.route.RouteResultset;
import io.mycat.route.parser.druid.MycatSchemaStatVisitor;
import io.mycat.route.util.RouterUtil;
import io.mycat.server.interceptor.impl.GlobalTableUtil;
import io.mycat.server.util.GlobalTableUtil;
import io.mycat.server.util.SchemaUtil;
import io.mycat.server.util.SchemaUtil.SchemaInfo;
import io.mycat.util.StringUtil;
@@ -33,7 +33,7 @@ import io.mycat.route.RouteResultset;
import io.mycat.route.parser.druid.MycatSchemaStatVisitor;
import io.mycat.route.parser.druid.impl.DefaultDruidParser;
import io.mycat.route.util.RouterUtil;
import io.mycat.server.interceptor.impl.GlobalTableUtil;
import io.mycat.server.util.GlobalTableUtil;
import io.mycat.server.util.SchemaUtil;
import io.mycat.server.util.SchemaUtil.SchemaInfo;
import io.mycat.util.StringUtil;
@@ -17,7 +17,7 @@ import io.mycat.route.RouteResultset;
import io.mycat.route.parser.druid.MycatSchemaStatVisitor;
import io.mycat.route.parser.druid.impl.DefaultDruidParser;
import io.mycat.route.util.RouterUtil;
import io.mycat.server.interceptor.impl.GlobalTableUtil;
import io.mycat.server.util.GlobalTableUtil;
import io.mycat.server.util.SchemaUtil;
import io.mycat.server.util.SchemaUtil.SchemaInfo;
import io.mycat.util.StringUtil;
@@ -1,16 +0,0 @@
package io.mycat.server.interceptor;
/**
* used for interceptor sql before execute ,can modify sql befor execute
* @author wuzhih
*
*/
public interface SQLInterceptor {
/**
* return new sql to handler,ca't modify sql's type
* @param sql
* @param sqlType
* @return new sql
*/
String interceptSQL(String sql ,int sqlType);
}
@@ -1,57 +0,0 @@
package io.mycat.server.interceptor.impl;
import io.mycat.MycatServer;
import io.mycat.server.interceptor.SQLInterceptor;
public class DefaultSqlInterceptor implements SQLInterceptor {
private static final char ESCAPE_CHAR = '\\';
private static final int TARGET_STRING_LENGTH = 2;
/**
* mysql driver对'转义与\',解析前改为foundationdb parser支持的'' add by sky
*
* @param sql
* @update by jason@dayima.com replace regex with general string walking
* avoid sql being destroyed in case of some mismatch
* maybe some performance enchanced
* @return
*/
public static String processEscape(String sql) {
int firstIndex = -1;
if ((sql == null) || ((firstIndex = sql.indexOf(ESCAPE_CHAR)) == -1)) {
return sql;
} else {
int lastIndex = sql.lastIndexOf(ESCAPE_CHAR, sql.length() - 2) + TARGET_STRING_LENGTH;
StringBuilder sb = new StringBuilder(sql);
for (int i = firstIndex; i < lastIndex; i ++) {
if (sb.charAt(i) == '\\') {
if (i + 1 < lastIndex
&& sb.charAt(i + 1) == '\'') {
//replace
sb.setCharAt(i, '\'');
}
//roll over
i ++;
}
}
return sb.toString();
}
}
/**
* escape mysql escape letter sql type ServerParse.UPDATE,ServerParse.INSERT
* etc
*/
@Override
public String interceptSQL(String sql, int sqlType) {
if("fdbparser".equals(MycatServer.getInstance().getConfig().getSystem().getDefaultSqlParser())) {
sql = processEscape(sql);
}
// other interceptors put in here ....
return sql;
}
}
@@ -1,15 +0,0 @@
package io.mycat.server.interceptor.impl;
import io.mycat.server.interceptor.SQLInterceptor;
public class StatSqlInterceptor implements SQLInterceptor {
@Override
public String interceptSQL(String sql, int sqlType) {
// TODO Auto-generated method stub
final int atype = sqlType;
final String sqls = DefaultSqlInterceptor.processEscape(sql);
return sql;
}
}
@@ -1,141 +0,0 @@
package io.mycat.server.interceptor.impl;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.mycat.MycatServer;
import io.mycat.config.model.SystemConfig;
import io.mycat.server.interceptor.SQLInterceptor;
import io.mycat.server.parser.ServerParse;
public class StatisticsSqlInterceptor implements SQLInterceptor {
private final class StatisticsSqlRunner implements Runnable {
private int sqltype = 0;
private String sqls = "";
public StatisticsSqlRunner(int sqltype, String sqls) {
this.sqltype = sqltype;
this.sqls = sqls;
}
public void run() {
try {
SystemConfig sysconfig = MycatServer.getInstance().getConfig().getSystem();
String sqlInterceptorType = sysconfig.getSqlInterceptorType();
String sqlInterceptorFile = sysconfig.getSqlInterceptorFile();
String[] sqlInterceptorTypes = sqlInterceptorType.split(",");
for (String type : sqlInterceptorTypes) {
if (StatisticsSqlInterceptor.parseType(type.toUpperCase()) == sqltype) {
switch (sqltype) {
case ServerParse.SELECT:
StatisticsSqlInterceptor.appendFile(sqlInterceptorFile, "SELECT:"
+ sqls + "");
break;
case ServerParse.UPDATE:
StatisticsSqlInterceptor.appendFile(sqlInterceptorFile, "UPDATE:"
+ sqls);
break;
case ServerParse.INSERT:
StatisticsSqlInterceptor.appendFile(sqlInterceptorFile, "INSERT:"
+ sqls);
break;
case ServerParse.DELETE:
StatisticsSqlInterceptor.appendFile(sqlInterceptorFile, "DELETE:"
+ sqls);
break;
default:
break;
}
}
}
} catch (Exception e) {
LOGGER.error("interceptSQL error:" + e.getMessage(),e);
}
}
}
private static final Logger LOGGER = LoggerFactory.getLogger(StatisticsSqlInterceptor.class);
private static Map<String, Integer> typeMap = new HashMap<String, Integer>();
static {
typeMap.put("SELECT", 7);
typeMap.put("UPDATE", 11);
typeMap.put("INSERT", 4);
typeMap.put("DELETE", 3);
}
public static int parseType(String type) {
return typeMap.get(type);
}
/**
* 方法追加文件:使用FileWriter
*/
private static synchronized void appendFile(String fileName, String content) {
Calendar calendar = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dayFile = dateFormat.format(calendar.getTime());
FileWriter writer = null;
try {
String newFileName = fileName;
//打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
String[] title = newFileName.split("\\.");
if (title.length == 2) {
newFileName = title[0] + dayFile + "." + title[1];
}
File file = new File(newFileName);
if (!file.exists()) {
file.createNewFile();
}
writer = new FileWriter(file, true);
String newContent = content.replaceAll("[\\t\\n\\r]", "")
+ System.getProperty("line.separator");
writer.write(newContent);
writer.flush();
} catch (IOException e) {
LOGGER.error("appendFile error:" + e.getMessage(),e);
} finally {
if(writer != null ){
try {
writer.close();
} catch (IOException e) {
LOGGER.error("close file error:" + e.getMessage(),e);
}
}
}
}
/**
* interceptSQL ,
* type :insert,delete,update,select
* exectime:xxx ms
* log content : select:select 1 from table,exectime:100ms,shared:1
* etc
*/
@Override
public String interceptSQL(String sql, int sqlType) {
LOGGER.debug("sql interceptSQL:");
final int sqltype = sqlType;
final String sqls = DefaultSqlInterceptor.processEscape(sql);
MycatServer.getInstance().getBusinessExecutor()
.execute(new StatisticsSqlRunner(sqltype, sqls));
return sql;
}
}
@@ -1,4 +1,4 @@
package io.mycat.server.interceptor.impl;
package io.mycat.server.util;
import java.util.ArrayList;
import java.util.Collection;
@@ -1,50 +0,0 @@
/*
* Copyright (c) 2013, OpenCloudDB/MyCAT and/or its affiliates. All rights
* reserved. DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. This
* code is free software;Designed and Developed mainly by many Chinese
* opensource volunteers. you can redistribute it and/or modify it under the
* terms of the GNU General Public License version 2 only, as published by the
* Free Software Foundation. This code is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License version 2 for more details (a copy is included in the LICENSE
* file that accompanied this code). You should have received a copy of the GNU
* General Public License version 2 along with this work; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA. Any questions about this component can be directed to it's
* project Web address https://code.google.com/p/opencloudb/.
*/
package io.mycat.parser;
import static org.junit.Assert.assertEquals;
import io.mycat.MycatServer;
import io.mycat.server.interceptor.impl.DefaultSqlInterceptor;
import org.junit.Test;
public class TestEscapeProcess {
String sql = "insert into t_uud_user_account(USER_ID,USER_NAME,PASSWORD,CREATE_TIME,STATUS,NICK_NAME,USER_ICON_URL,USER_ICON_URL2,USER_ICON_URL3,ACCOUNT_TYPE) "
+ "values (2488899998,'u\\'aa\\'\\'a''aa','af8f9dffa5d420fbc249141645b962ee','2013-12-01 00:00:00',0,NULL,NULL,NULL,NULL,1)";
String sqlret = "insert into t_uud_user_account(USER_ID,USER_NAME,PASSWORD,CREATE_TIME,STATUS,NICK_NAME,USER_ICON_URL,USER_ICON_URL2,USER_ICON_URL3,ACCOUNT_TYPE) "
+ "values (2488899998,'u''aa''''a''aa','af8f9dffa5d420fbc249141645b962ee','2013-12-01 00:00:00',0,NULL,NULL,NULL,NULL,1)";
String starWithEscapeSql = "\\insert into t_uud_user_account(USER_ID,USER_NAME,PASSWORD,CREATE_TIME,STATUS,NICK_NAME,USER_ICON_URL,USER_ICON_URL2,USER_ICON_URL3,ACCOUNT_TYPE) "
+ "values (2488899998,'u\\'aa\\'\\'a''aa','af8f9dffa5d420fbc249141645b962ee','2013-12-01 00:00:00',0,NULL,NULL,NULL,NULL,1)\\";
String starWithEscapeSqlret = "\\insert into t_uud_user_account(USER_ID,USER_NAME,PASSWORD,CREATE_TIME,STATUS,NICK_NAME,USER_ICON_URL,USER_ICON_URL2,USER_ICON_URL3,ACCOUNT_TYPE) "
+ "values (2488899998,'u''aa''''a''aa','af8f9dffa5d420fbc249141645b962ee','2013-12-01 00:00:00',0,NULL,NULL,NULL,NULL,1)\\";
@Test
public void testEscapeProcess() {
MycatServer.getInstance().getConfig().getSystem().setDefaultSqlParser("fdbparser");
String sqlProcessed = DefaultSqlInterceptor.processEscape(sql);
assertEquals(sqlProcessed, sqlret);
String sqlProcessed1 = DefaultSqlInterceptor
.processEscape(starWithEscapeSql);
assertEquals(sqlProcessed1, starWithEscapeSqlret);
}
}