打印慢sql日志

This commit is contained in:
Redkale
2022-12-22 21:49:11 +08:00
parent 2d06fd6ea9
commit 989f1cddf8
4 changed files with 56 additions and 121 deletions

View File

@@ -16,6 +16,7 @@ import org.redkale.annotation.*;
import org.redkale.annotation.ResourceListener; import org.redkale.annotation.ResourceListener;
import static org.redkale.boot.Application.*; import static org.redkale.boot.Application.*;
import org.redkale.boot.*; import org.redkale.boot.*;
import static org.redkale.boot.Application.*;
import org.redkale.convert.ConvertDisabled; import org.redkale.convert.ConvertDisabled;
import org.redkale.convert.json.JsonConvert; import org.redkale.convert.json.JsonConvert;
import org.redkale.mq.MessageMultiConsumer; import org.redkale.mq.MessageMultiConsumer;
@@ -58,6 +59,8 @@ public abstract class ClusterAgent {
protected AnyValue config; protected AnyValue config;
protected Set<String> tags;
protected TransportFactory transportFactory; protected TransportFactory transportFactory;
protected final ConcurrentHashMap<String, ClusterEntry> localEntrys = new ConcurrentHashMap<>(); protected final ConcurrentHashMap<String, ClusterEntry> localEntrys = new ConcurrentHashMap<>();
@@ -81,7 +84,18 @@ public abstract class ClusterAgent {
if (str.trim().isEmpty()) continue; if (str.trim().isEmpty()) continue;
list.add(Integer.parseInt(str.trim())); list.add(Integer.parseInt(str.trim()));
} }
if (!list.isEmpty()) this.ports = list.stream().mapToInt(x -> x).toArray(); if (!list.isEmpty()) {
this.ports = list.stream().mapToInt(x -> x).toArray();
}
}
Set<String> tags0 = new HashSet<>();
for (String str : config.getValue("tags", "").split(";|,")) {
if (!str.trim().isEmpty()) {
tags0.add(str.trim());
}
}
if (!tags0.isEmpty()) {
this.tags = tags0;
} }
} }

View File

@@ -69,8 +69,11 @@ public abstract class AbstractDataSource extends AbstractService implements Data
//@since 2.7.0 //@since 2.7.0
public static final String DATA_SOURCE_PIPELINES = "pipelines"; public static final String DATA_SOURCE_PIPELINES = "pipelines";
//@since 2.7.0 //超过多少毫秒视为慢, 会打印警告级别的日志, 默认值: 3000 //@since 2.8.0 //超过多少毫秒视为慢, 会打印警告级别的日志, 默认值: 2000
public static final String DATA_SOURCE_SLOWMS = "slowms"; public static final String DATA_SOURCE_SLOWMS_WARN = "warnslowms";
//@since 2.8.0 //超过多少毫秒视为较慢, 会打印警告级别的日志, 默认值: 3000
public static final String DATA_SOURCE_SLOWMS_ERROR = "errorslowms";
//@since 2.7.0 //@since 2.7.0
public static final String DATA_SOURCE_AUTOMAPPING = "auto-mapping"; public static final String DATA_SOURCE_AUTOMAPPING = "auto-mapping";

View File

@@ -272,12 +272,7 @@ public class DataJdbcSource extends DataSqlSource {
if (info.isLoggable(logger, Level.FINEST, debugsql)) logger.finest(info.getType().getSimpleName() + " insert sql=" + debugsql.replaceAll("(\r|\n)", "\\n")); if (info.isLoggable(logger, Level.FINEST, debugsql)) logger.finest(info.getType().getSimpleName() + " insert sql=" + debugsql.replaceAll("(\r|\n)", "\\n"));
} }
} //打印结束 } //打印结束
if (slowms > 0) { slowLog(s, sql);
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
return CompletableFuture.completedFuture(c); return CompletableFuture.completedFuture(c);
} catch (SQLException e) { } catch (SQLException e) {
return CompletableFuture.failedFuture(e); return CompletableFuture.failedFuture(e);
@@ -332,12 +327,7 @@ public class DataJdbcSource extends DataSqlSource {
final Statement stmt = conn.createStatement(); final Statement stmt = conn.createStatement();
int c = stmt.executeUpdate(sql); int c = stmt.executeUpdate(sql);
stmt.close(); stmt.close();
if (slowms > 0) { slowLog(s, sql);
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
return CompletableFuture.completedFuture(c); return CompletableFuture.completedFuture(c);
} catch (SQLException e) { } catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) { if (isTableNotExist(info, e.getSQLState())) {
@@ -355,12 +345,7 @@ public class DataJdbcSource extends DataSqlSource {
st.executeBatch(); st.executeBatch();
} }
st.close(); st.close();
if (slowms > 0) { slowLog(s, sql);
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
return CompletableFuture.completedFuture(0); return CompletableFuture.completedFuture(0);
} catch (SQLException e2) { } catch (SQLException e2) {
return CompletableFuture.failedFuture(e2); return CompletableFuture.failedFuture(e2);
@@ -385,12 +370,7 @@ public class DataJdbcSource extends DataSqlSource {
final Statement stmt = conn.createStatement(); final Statement stmt = conn.createStatement();
int c = stmt.executeUpdate(sql); int c = stmt.executeUpdate(sql);
stmt.close(); stmt.close();
if (slowms > 0) { slowLog(s, sql);
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
return CompletableFuture.completedFuture(c); return CompletableFuture.completedFuture(c);
} catch (SQLException e) { } catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) return CompletableFuture.completedFuture(-1); if (isTableNotExist(info, e.getSQLState())) return CompletableFuture.completedFuture(-1);
@@ -415,12 +395,7 @@ public class DataJdbcSource extends DataSqlSource {
String tablekey = table.indexOf('.') > 0 ? table : (conn.getCatalog() + '.' + table); String tablekey = table.indexOf('.') > 0 ? table : (conn.getCatalog() + '.' + table);
info.removeDisTable(tablekey); info.removeDisTable(tablekey);
} }
if (slowms > 0) { slowLog(s, sql);
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
return CompletableFuture.completedFuture(c); return CompletableFuture.completedFuture(c);
} catch (SQLException e) { } catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) return CompletableFuture.completedFuture(-1); if (isTableNotExist(info, e.getSQLState())) return CompletableFuture.completedFuture(-1);
@@ -474,12 +449,7 @@ public class DataJdbcSource extends DataSqlSource {
if (p >= 0) c += p; if (p >= 0) c += p;
} }
prestmt.close(); prestmt.close();
if (slowms > 0) { slowLog(s, updateSQL);
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + updateSQL);
}
}
return CompletableFuture.completedFuture(c); return CompletableFuture.completedFuture(c);
} catch (SQLException e) { } catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) { if (isTableNotExist(info, e.getSQLState())) {
@@ -527,24 +497,14 @@ public class DataJdbcSource extends DataSqlSource {
} }
int c = prestmt.executeUpdate(); int c = prestmt.executeUpdate();
prestmt.close(); prestmt.close();
if (slowms > 0) { slowLog(s, sql);
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
return CompletableFuture.completedFuture(c); return CompletableFuture.completedFuture(c);
} else { } else {
if (info.isLoggable(logger, Level.FINEST, sql)) logger.finest(info.getType().getSimpleName() + " update sql=" + sql); if (info.isLoggable(logger, Level.FINEST, sql)) logger.finest(info.getType().getSimpleName() + " update sql=" + sql);
final Statement stmt = conn.createStatement(); final Statement stmt = conn.createStatement();
int c = stmt.executeUpdate(sql); int c = stmt.executeUpdate(sql);
stmt.close(); stmt.close();
if (slowms > 0) { slowLog(s, sql);
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
return CompletableFuture.completedFuture(c); return CompletableFuture.completedFuture(c);
} }
} catch (SQLException e) { } catch (SQLException e) {
@@ -598,12 +558,7 @@ public class DataJdbcSource extends DataSqlSource {
} }
set.close(); set.close();
stmt.close(); stmt.close();
if (slowms > 0) { slowLog(s, sql);
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
return CompletableFuture.completedFuture(map); return CompletableFuture.completedFuture(map);
} catch (SQLException e) { } catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) { if (isTableNotExist(info, e.getSQLState())) {
@@ -649,12 +604,7 @@ public class DataJdbcSource extends DataSqlSource {
} }
set.close(); set.close();
stmt.close(); stmt.close();
if (slowms > 0) { slowLog(s, sql);
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
return CompletableFuture.completedFuture(rs); return CompletableFuture.completedFuture(rs);
} catch (SQLException e) { } catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) { if (isTableNotExist(info, e.getSQLState())) {
@@ -701,12 +651,7 @@ public class DataJdbcSource extends DataSqlSource {
} }
set.close(); set.close();
stmt.close(); stmt.close();
if (slowms > 0) { slowLog(s, sql);
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
return CompletableFuture.completedFuture(rs); return CompletableFuture.completedFuture(rs);
} catch (SQLException e) { } catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) { if (isTableNotExist(info, e.getSQLState())) {
@@ -768,12 +713,7 @@ public class DataJdbcSource extends DataSqlSource {
} }
set.close(); set.close();
stmt.close(); stmt.close();
if (slowms > 0) { slowLog(s, sql);
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
return CompletableFuture.completedFuture(rs); return CompletableFuture.completedFuture(rs);
} catch (SQLException e) { } catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) { if (isTableNotExist(info, e.getSQLState())) {
@@ -816,12 +756,7 @@ public class DataJdbcSource extends DataSqlSource {
T rs = set.next() ? selects == null ? info.getFullEntityValue(set) : info.getEntityValue(selects, set) : null; T rs = set.next() ? selects == null ? info.getFullEntityValue(set) : info.getEntityValue(selects, set) : null;
set.close(); set.close();
ps.close(); ps.close();
if (slowms > 0) { slowLog(s, sql);
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
return CompletableFuture.completedFuture(rs); return CompletableFuture.completedFuture(rs);
} catch (SQLException e) { } catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) { if (isTableNotExist(info, e.getSQLState())) {
@@ -868,12 +803,7 @@ public class DataJdbcSource extends DataSqlSource {
} }
set.close(); set.close();
ps.close(); ps.close();
if (slowms > 0) { slowLog(s, sql);
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
return CompletableFuture.completedFuture(val == null ? defValue : val); return CompletableFuture.completedFuture(val == null ? defValue : val);
} catch (SQLException e) { } catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) { if (isTableNotExist(info, e.getSQLState())) {
@@ -916,12 +846,7 @@ public class DataJdbcSource extends DataSqlSource {
set.close(); set.close();
ps.close(); ps.close();
if (info.isLoggable(logger, Level.FINEST, sql)) logger.finest(info.getType().getSimpleName() + " exists (" + rs + ") sql=" + sql); if (info.isLoggable(logger, Level.FINEST, sql)) logger.finest(info.getType().getSimpleName() + " exists (" + rs + ") sql=" + sql);
if (slowms > 0) { slowLog(s, sql);
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
return CompletableFuture.completedFuture(rs); return CompletableFuture.completedFuture(rs);
} catch (SQLException e) { } catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) { if (isTableNotExist(info, e.getSQLState())) {
@@ -990,12 +915,7 @@ public class DataJdbcSource extends DataSqlSource {
set.close(); set.close();
ps.close(); ps.close();
} }
if (slowms > 0) { slowLog(s, listsql);
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, sheet-content: " + listsql);
}
}
return CompletableFuture.completedFuture(new Sheet<>(total, list)); return CompletableFuture.completedFuture(new Sheet<>(total, list));
} }
final String listsql = "SELECT " + (distinct ? "DISTINCT " : "") + info.getFullQueryColumns("a", selects) + " FROM " + info.getTable(node) + " a" + (join == null ? "" : join) final String listsql = "SELECT " + (distinct ? "DISTINCT " : "") + info.getFullQueryColumns("a", selects) + " FROM " + info.getTable(node) + " a" + (join == null ? "" : join)
@@ -1031,12 +951,7 @@ public class DataJdbcSource extends DataSqlSource {
} }
set.close(); set.close();
ps.close(); ps.close();
if (slowms > 0) { slowLog(s, listsql);
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + listsql);
}
}
return CompletableFuture.completedFuture(new Sheet<>(total, list)); return CompletableFuture.completedFuture(new Sheet<>(total, list));
} catch (SQLException e) { } catch (SQLException e) {
if (isTableNotExist(info, e.getSQLState())) { if (isTableNotExist(info, e.getSQLState())) {
@@ -1103,12 +1018,7 @@ public class DataJdbcSource extends DataSqlSource {
rs[++i] = stmt.execute(sql) ? 1 : 0; rs[++i] = stmt.execute(sql) ? 1 : 0;
} }
stmt.close(); stmt.close();
if (slowms > 0) { slowLog(s, sqls);
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + Arrays.toString(sqls));
}
}
return rs; return rs;
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@@ -1141,12 +1051,7 @@ public class DataJdbcSource extends DataSqlSource {
V rs = handler.apply(createDataResultSet(null, set)); V rs = handler.apply(createDataResultSet(null, set));
set.close(); set.close();
statement.close(); statement.close();
if (slowms > 0) { slowLog(s, sql);
long cost = System.currentTimeMillis() - s;
if (cost > slowms) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + sql);
}
}
return rs; return rs;
} catch (Exception ex) { } catch (Exception ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);

View File

@@ -71,8 +71,11 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi
protected final BiFunction<DataSource, EntityInfo, CompletableFuture<List>> fullloader = (s, i) protected final BiFunction<DataSource, EntityInfo, CompletableFuture<List>> fullloader = (s, i)
-> ((CompletableFuture<Sheet>) querySheetDB(i, false, false, false, null, null, (FilterNode) null)).thenApply(e -> e == null ? new ArrayList() : e.list(true)); -> ((CompletableFuture<Sheet>) querySheetDB(i, false, false, false, null, null, (FilterNode) null)).thenApply(e -> e == null ? new ArrayList() : e.list(true));
//超过多少毫秒视为慢, 会打印警告级别的日志, 默认值: 3000 //超过多少毫秒视为慢, 会打印警告级别的日志, 默认值: 2000
protected long slowms; protected long slowmsWarn;
//超过多少毫秒视为很慢, 会打印错误级别的日志, 默认值: 3000
protected long slowmsError;
//用于反向LIKE使用 //用于反向LIKE使用
protected String containSQL; protected String containSQL;
@@ -127,7 +130,8 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi
this.tablecopySQL = readConfProps.getProperty(DATA_SOURCE_TABLECOPY_SQLTEMPLATE, "CREATE TABLE IF NOT EXISTS ${newtable} LIKE ${oldtable}"); this.tablecopySQL = readConfProps.getProperty(DATA_SOURCE_TABLECOPY_SQLTEMPLATE, "CREATE TABLE IF NOT EXISTS ${newtable} LIKE ${oldtable}");
this.cacheForbidden = "NONE".equalsIgnoreCase(readConfProps.getProperty(DATA_SOURCE_CACHEMODE)); this.cacheForbidden = "NONE".equalsIgnoreCase(readConfProps.getProperty(DATA_SOURCE_CACHEMODE));
this.slowms = Integer.parseInt(readConfProps.getProperty(DATA_SOURCE_SLOWMS, "3000").trim()); this.slowmsWarn = Integer.parseInt(readConfProps.getProperty(DATA_SOURCE_SLOWMS_WARN, "2000").trim());
this.slowmsError = Integer.parseInt(readConfProps.getProperty(DATA_SOURCE_SLOWMS_ERROR, "3000").trim());
} }
@Override @Override
@@ -216,6 +220,15 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi
throw new UnsupportedOperationException("Not supported yet."); throw new UnsupportedOperationException("Not supported yet.");
} }
protected void slowLog(long startTime, String... sqls) {
long cost = System.currentTimeMillis() - startTime;
if (slowmsError > 0 && cost > slowmsError) {
logger.log(Level.SEVERE, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') slow sql cost " + cost + " ms, content: " + Arrays.toString(sqls));
} else if (slowmsWarn > 0 && cost > slowmsWarn) {
logger.log(Level.WARNING, DataSource.class.getSimpleName() + "(name='" + resourceName() + "') very slow sql cost " + cost + " ms, content: " + Arrays.toString(sqls));
}
}
//解密可能存在的加密字段, 可重载 //解密可能存在的加密字段, 可重载
protected String decryptProperty(String key, String value) { protected String decryptProperty(String key, String value) {
return value; return value;