[inner-1948] adjust code

This commit is contained in:
wenyh1
2022-11-07 16:24:47 +08:00
parent 48170f79ff
commit f45487be3a
6 changed files with 63 additions and 75 deletions

View File

@@ -2,12 +2,13 @@ package com.actiontech.dble.log.sqldump;
import com.actiontech.dble.backend.mysql.ByteUtil;
import com.actiontech.dble.net.mysql.MySQLPacket;
import com.actiontech.dble.server.parser.RwSplitServerParse;
import com.actiontech.dble.server.parser.ServerParseFactory;
import com.actiontech.dble.server.parser.ShardingServerParse;
import com.actiontech.dble.server.status.SqlDumpLog;
import com.actiontech.dble.services.mysqlsharding.MySQLResponseService;
import com.actiontech.dble.services.rwsplit.RWSplitService;
import com.actiontech.dble.services.rwsplit.handle.PreparedStatementHolder;
import com.actiontech.dble.util.SqlStringUtil;
import com.actiontech.dble.util.StringUtil;
import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils;
@@ -37,7 +38,7 @@ public final class SqlDumpLogHelper {
private volatile boolean isOpen = false;
private volatile ExtendedLogger logger;
private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock();
private static final RwSplitServerParse PARSER = ServerParseFactory.getRwSplitParser();
private static final ShardingServerParse PARSER = ServerParseFactory.getShardingParser(); // the uniform use of ShardingServerParse
private SqlDumpLogHelper() {
}
@@ -147,7 +148,7 @@ public final class SqlDumpLogHelper {
try {
final ExtendedLogger log = INSTANCE.logger;
if (log != null) {
sqlDigest = sqlDigest.length() > 100 ? sqlDigest.substring(0, 100) : sqlDigest;
sqlDigest = sqlDigest.length() > 1024 ? sqlDigest.substring(0, 1024) : sqlDigest;
log.info("[{}][{}][{}][{}][{}][{}:{}][{}:{}][{}] {}",
digestHash, sqlType, transactionId, affectRows, userName,
clientHost, clientPort, backHost, backPort, dura, sqlDigest);
@@ -179,26 +180,7 @@ public final class SqlDumpLogHelper {
String[] arr = new String[2];
int rs = PARSER.parse(originSql);
int sqlType = rs & 0xff;
switch (sqlType) {
case RwSplitServerParse.SELECT:
arr[0] = "SELECT";
break;
case RwSplitServerParse.INSERT:
arr[0] = "INSERT";
break;
case RwSplitServerParse.DELETE:
arr[0] = "DELETE";
break;
case RwSplitServerParse.UPDATE:
arr[0] = "UPDATE";
break;
case RwSplitServerParse.DDL:
arr[0] = "DDL";
break;
default:
arr[0] = "OTHER";
break;
}
arr[0] = SqlStringUtil.getSqlType(sqlType);
arr[1] = originSql;
return arr;
}

View File

@@ -69,13 +69,16 @@ public class SqlDumpLog {
problemReporter.warn(String.format(WARNING_FORMAT, "sqlDumpLogOnStartupRotate", sqlDumpLogOnStartupRotate0, this.sqlDumpLogOnStartupRotate + ""));
}
} else {
this.sqlDumpLogOnStartupRotate = -1;
if (!StringUtil.isBlank(sqlDumpLogOnStartupRotate0)) { // -1、null
this.sqlDumpLogOnStartupRotate = -1;
} // else, use default
}
String sqlDumpLogSizeBasedRotate0 = SystemConfig.getInstance().getSqlDumpLogSizeBasedRotate();
if (isConfig(sqlDumpLogSizeBasedRotate0)) {
this.sqlDumpLogSizeBasedRotate = FileSize.parse(sqlDumpLogSizeBasedRotate0, 52428800L) + ""; // default: 50 MB
if (!isConfig(sqlDumpLogSizeBasedRotate0)) {
sqlDumpLogSizeBasedRotate0 = sqlDumpLogSizeBasedRotate;
} // else, use default
this.sqlDumpLogSizeBasedRotate = FileSize.parse(sqlDumpLogSizeBasedRotate0, 52428800L) + ""; // default: 50 MB
String sqlDumpLogTimeBasedRotate0 = SystemConfig.getInstance().getSqlDumpLogTimeBasedRotate();
if (isConfig(sqlDumpLogTimeBasedRotate0)) {
@@ -88,7 +91,9 @@ public class SqlDumpLog {
problemReporter.warn(String.format(WARNING_FORMAT, "sqlDumpLogTimeBasedRotate", sqlDumpLogTimeBasedRotate0, this.sqlDumpLogTimeBasedRotate + ""));
}
} else {
this.sqlDumpLogTimeBasedRotate = -1;
if (!StringUtil.isBlank(sqlDumpLogTimeBasedRotate0)) { // -1、null
this.sqlDumpLogTimeBasedRotate = -1;
} // else, use default
}
String sqlDumpLogDeleteFileAge0 = SystemConfig.getInstance().getSqlDumpLogDeleteFileAge();

View File

@@ -7,11 +7,11 @@ package com.actiontech.dble.services.manager.information.tables.statistic;
import com.actiontech.dble.config.Fields;
import com.actiontech.dble.meta.ColumnMeta;
import com.actiontech.dble.server.parser.ServerParse;
import com.actiontech.dble.services.manager.information.ManagerBaseTable;
import com.actiontech.dble.statistic.sql.StatisticManager;
import com.actiontech.dble.statistic.sql.handler.SqlStatisticHandler;
import com.actiontech.dble.statistic.sql.handler.StatisticDataHandler;
import com.actiontech.dble.util.SqlStringUtil;
import com.google.common.collect.Maps;
import java.time.Instant;
@@ -108,7 +108,7 @@ public class SqlLog extends ManagerBaseTable {
} else {
map.put(COLUMN_SQL_DIGEST, sqlRecord.getSqlDigest());
}
map.put(COLUMN_SQL_TYPE, getSqlType(sqlRecord.getSqlType()));
map.put(COLUMN_SQL_TYPE, SqlStringUtil.getSqlType(sqlRecord.getSqlType()));
map.put(COLUMN_TX_ID, sqlRecord.getTxId() + "");
map.put(COLUMN_ENTRY, sqlRecord.getEntry() + "");
map.put(COLUMN_USER, sqlRecord.getUser());
@@ -122,48 +122,4 @@ public class SqlLog extends ManagerBaseTable {
}));
return list;
}
public String getSqlType(int sqlType) {
String type;
switch (sqlType) {
case ServerParse.DDL:
type = "DDL";
break;
case ServerParse.INSERT:
type = "Insert";
break;
case ServerParse.SELECT:
type = "Select";
break;
case ServerParse.UPDATE:
type = "Update";
break;
case ServerParse.DELETE:
type = "Delete";
break;
case ServerParse.LOAD_DATA_INFILE_SQL:
type = "Loaddata";
break;
case ServerParse.BEGIN:
type = "Begin";
break;
case ServerParse.COMMIT:
type = "Commit";
break;
case ServerParse.ROLLBACK:
type = "Rollback";
break;
case ServerParse.SET:
type = "Set";
break;
case ServerParse.SHOW:
type = "Show";
break;
default:
type = "Other";
}
return type;
}
}

View File

@@ -56,6 +56,7 @@ public class RWSplitQueryHandler implements FrontendQueryHandler {
int sqlType = rs & 0xff;
if (hintInfo != null) {
session.executeHint(hintInfo, sqlType, sql, null);
session.getService().controlTx(TransactionOperate.QUERY);
} else {
if (!RwSplitServerParse.isTCL(sqlType) &&
!RwSplitServerParse.isImplicitlyCommitSql(sqlType) &&

View File

@@ -167,9 +167,9 @@ public final class SystemParams {
readOnlyParams.add(new ParamInfo("sqlDumpLogBasePath", SqlDumpLog.getInstance().getSqlDumpLogBasePath() + "", "The base path of sqldump log, the default value is 'sqldump'"));
readOnlyParams.add(new ParamInfo("sqlDumpLogFileName", SqlDumpLog.getInstance().getSqlDumpLogFileName() + "", "The sqldump log file name, the default value is 'sqldump.log'"));
readOnlyParams.add(new ParamInfo("sqlDumpLogCompressFilePattern", SqlDumpLog.getInstance().getSqlDumpLogCompressFilePattern() + "", "The compression of sqldump log file, the default value is '${date:yyyy-MM}/sqldump-%d{MM-dd}-%i.log.gz'"));
readOnlyParams.add(new ParamInfo("sqlDumpLogOnStartupRotate", SqlDumpLog.getInstance().getSqlDumpLogOnStartupRotate() + "", "The onStartup of rotate policy, the default value is 1"));
readOnlyParams.add(new ParamInfo("sqlDumpLogOnStartupRotate", SqlDumpLog.getInstance().getSqlDumpLogOnStartupRotate() + "", "The onStartup of rotate policy, the default value is 1; -1 said not to participate in the strategy"));
readOnlyParams.add(new ParamInfo("sqlDumpLogSizeBasedRotate", SqlDumpLog.getInstance().getSqlDumpLogSizeBasedRotate() + "", "The sizeBased of rotate policy, the default value is '50 MB'; default unit is byte"));
readOnlyParams.add(new ParamInfo("sqlDumpLogTimeBasedRotate", SqlDumpLog.getInstance().getSqlDumpLogTimeBasedRotate() + "", "The timeBased of rotate policy, the default value is 1"));
readOnlyParams.add(new ParamInfo("sqlDumpLogTimeBasedRotate", SqlDumpLog.getInstance().getSqlDumpLogTimeBasedRotate() + "", "The timeBased of rotate policy, the default value is 1; -1 said not to participate in the strategy"));
readOnlyParams.add(new ParamInfo("sqlDumpLogDeleteFileAge", SqlDumpLog.getInstance().getSqlDumpLogDeleteFileAge() + "", "The expiration time deletion strategy, the default value is '90d'"));
readOnlyParams.add(new ParamInfo("sqlDumpLogCompressFilePath", SqlDumpLog.getInstance().getSqlDumpLogCompressFilePath() + "", "The compression of sqldump log file path, the default value is '*/sqldump-*.log.gz'"));
}

View File

@@ -6,6 +6,7 @@
package com.actiontech.dble.util;
import com.actiontech.dble.server.parser.DbleOutputVisitor;
import com.actiontech.dble.server.parser.ServerParse;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
@@ -30,4 +31,47 @@ public final class SqlStringUtil {
String sql = out.toString();
return sql;
}
public static String getSqlType(int sqlType) {
String type;
switch (sqlType) {
case ServerParse.DDL:
type = "DDL";
break;
case ServerParse.INSERT:
type = "Insert";
break;
case ServerParse.SELECT:
type = "Select";
break;
case ServerParse.UPDATE:
type = "Update";
break;
case ServerParse.DELETE:
type = "Delete";
break;
case ServerParse.LOAD_DATA_INFILE_SQL:
type = "Loaddata";
break;
case ServerParse.BEGIN:
type = "Begin";
break;
case ServerParse.COMMIT:
type = "Commit";
break;
case ServerParse.ROLLBACK:
type = "Rollback";
break;
case ServerParse.SET:
type = "Set";
break;
case ServerParse.SHOW:
type = "Show";
break;
default:
type = "Other";
}
return type;
}
}