-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance(controller): modify datasource param and add index for slowsql (
- Loading branch information
1 parent
52368b4
commit ce807f3
Showing
5 changed files
with
279 additions
and
1 deletion.
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
server/controller/src/main/java/ai/starwhale/mlops/configuration/SlowSqlLogInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
* Copyright 2022 Starwhale, Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package ai.starwhale.mlops.configuration; | ||
|
||
import java.sql.Statement; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.List; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.ibatis.executor.statement.StatementHandler; | ||
import org.apache.ibatis.mapping.BoundSql; | ||
import org.apache.ibatis.mapping.ParameterMapping; | ||
import org.apache.ibatis.mapping.ParameterMode; | ||
import org.apache.ibatis.plugin.Interceptor; | ||
import org.apache.ibatis.plugin.Intercepts; | ||
import org.apache.ibatis.plugin.Invocation; | ||
import org.apache.ibatis.plugin.Signature; | ||
import org.apache.ibatis.reflection.MetaObject; | ||
import org.apache.ibatis.scripting.defaults.DefaultParameterHandler; | ||
import org.apache.ibatis.session.Configuration; | ||
import org.apache.ibatis.session.ResultHandler; | ||
import org.apache.ibatis.type.TypeHandlerRegistry; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.ReflectionUtils; | ||
|
||
|
||
@Slf4j | ||
@Component | ||
@Intercepts({ | ||
@Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class}), | ||
@Signature(type = StatementHandler.class, method = "update", args = {Statement.class}) | ||
}) | ||
@ConditionalOnProperty(prefix = "sw.db.sql.log-slow-sql", name = "enable", havingValue = "true", matchIfMissing = true) | ||
public class SlowSqlLogInterceptor implements Interceptor { | ||
|
||
private Configuration configuration = null; | ||
private static final ThreadLocal<SimpleDateFormat> DATE_FORMAT_THREAD_LOCAL = | ||
ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")); | ||
|
||
private final int slowSqlMillis; | ||
|
||
private final int maxSqlLength; | ||
|
||
public SlowSqlLogInterceptor( | ||
@Value("${sw.db.sql.slow-sql-millis:100}") int slowSqlMillis, | ||
@Value("${sw.db.sql.max-print-length:200}") int maxSqlLength | ||
) { | ||
this.slowSqlMillis = slowSqlMillis; | ||
this.maxSqlLength = maxSqlLength; | ||
} | ||
|
||
|
||
@Override | ||
public Object intercept(Invocation invocation) throws Throwable { | ||
Object target = invocation.getTarget(); | ||
long startTime = System.currentTimeMillis(); | ||
StatementHandler statementHandler = (StatementHandler) target; | ||
try { | ||
return invocation.proceed(); | ||
} finally { | ||
long endTime = System.currentTimeMillis(); | ||
long cost = endTime - startTime; | ||
if (cost >= slowSqlMillis) { | ||
BoundSql boundSql = statementHandler.getBoundSql(); | ||
|
||
if (configuration == null) { | ||
var parameterHandler = (DefaultParameterHandler) statementHandler.getParameterHandler(); | ||
var configurationField = ReflectionUtils.findField(parameterHandler.getClass(), "configuration"); | ||
if (configurationField != null) { | ||
ReflectionUtils.makeAccessible(configurationField); | ||
this.configuration = (Configuration) configurationField.get(parameterHandler); | ||
} | ||
} | ||
if (configuration != null) { | ||
var sql = formatSql(boundSql, configuration); | ||
log.info("Execute SQL:[ {} ] cost[ {} ms]", sql, cost); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Get param list | ||
* | ||
* @param configuration the configuration | ||
* @param boundSql the bound sql | ||
* | ||
* @return the param list | ||
*/ | ||
public String formatSql(BoundSql boundSql, Configuration configuration) { | ||
String sql = boundSql.getSql(); | ||
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings(); | ||
Object parameterObject = boundSql.getParameterObject(); | ||
|
||
if (sql == null || sql.length() == 0) { | ||
return ""; | ||
} | ||
if (configuration == null) { | ||
return ""; | ||
} | ||
TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry(); | ||
sql = sql.replaceAll("\\s+", " "); | ||
// DefaultParameterHandler | ||
if (parameterMappings != null) { | ||
for (ParameterMapping parameterMapping : parameterMappings) { | ||
if (parameterMapping.getMode() != ParameterMode.OUT) { | ||
Object value; | ||
String propertyName = parameterMapping.getProperty(); | ||
if (boundSql.hasAdditionalParameter(propertyName)) { | ||
value = boundSql.getAdditionalParameter(propertyName); | ||
} else if (parameterObject == null) { | ||
value = null; | ||
} else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) { | ||
value = parameterObject; | ||
} else { | ||
MetaObject metaObject = configuration.newMetaObject(parameterObject); | ||
value = metaObject.getValue(propertyName); | ||
} | ||
String paramValueStr = ""; | ||
if (value instanceof String) { | ||
paramValueStr = "'" + value + "'"; | ||
} else if (value instanceof Date) { | ||
paramValueStr = "'" + DATE_FORMAT_THREAD_LOCAL.get().format(value) + "'"; | ||
} else { | ||
paramValueStr = String.valueOf(value); | ||
} | ||
sql = sql.replaceFirst("\\?", paramValueStr); | ||
} | ||
} | ||
} | ||
if (sql.length() > maxSqlLength) { | ||
return sql.substring(0, maxSqlLength); | ||
} else { | ||
return sql; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...ler/src/main/resources/db/migration/v0_4_0/V0_4_0_026__add_index_for_dataset_read_log.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright 2022 Starwhale, Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
|
||
CREATE INDEX dataset_read_log_session_id_IDX USING BTREE ON dataset_read_log (session_id,consumer_id,status); |
56 changes: 56 additions & 0 deletions
56
server/controller/src/test/java/ai/starwhale/mlops/configuration/sql/SlowSqlTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2022 Starwhale, Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package ai.starwhale.mlops.configuration.sql; | ||
|
||
import ai.starwhale.mlops.configuration.SlowSqlLogInterceptor; | ||
import java.util.Map; | ||
import org.apache.ibatis.session.Configuration; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
|
||
public class SlowSqlTest { | ||
|
||
@Test | ||
public void sqlFormatTest() { | ||
var interceptor = new SlowSqlLogInterceptor(100, 200); | ||
|
||
var configuration = new Configuration(); | ||
configuration.addMapper(SqlMapper.class); | ||
|
||
var statement = configuration.getMappedStatement("ai.starwhale.mlops.configuration.sql.SqlMapper.insert"); | ||
var boundSql = statement.getBoundSql("sw"); | ||
Assertions.assertEquals("INSERT INTO sw_user (name) VALUES ('sw')", | ||
interceptor.formatSql(boundSql, configuration)); | ||
|
||
statement = configuration.getMappedStatement("ai.starwhale.mlops.configuration.sql.SqlMapper.selectById"); | ||
boundSql = statement.getBoundSql(Map.of("id", 1, "name", "sw")); | ||
Assertions.assertEquals("SELECT name FROM sw_user where id = 1 and name = 'sw'", | ||
interceptor.formatSql(boundSql, configuration)); | ||
|
||
statement = configuration.getMappedStatement( | ||
"ai.starwhale.mlops.configuration.sql.SqlMapper.selectByIdAndName"); | ||
boundSql = statement.getBoundSql(Map.of("id", 2, "name", "sw2")); | ||
Assertions.assertEquals("SELECT name FROM sw_user where id = 2 and name = 'sw2'", | ||
interceptor.formatSql(boundSql, configuration)); | ||
|
||
statement = configuration.getMappedStatement("ai.starwhale.mlops.configuration.sql.SqlMapper.selectAll"); | ||
boundSql = statement.getBoundSql(null); | ||
Assertions.assertEquals("SELECT name FROM sw_user", interceptor.formatSql(boundSql, configuration)); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
server/controller/src/test/java/ai/starwhale/mlops/configuration/sql/SqlMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2022 Starwhale, Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package ai.starwhale.mlops.configuration.sql; | ||
|
||
import java.util.List; | ||
import org.apache.ibatis.annotations.Insert; | ||
import org.apache.ibatis.annotations.Mapper; | ||
import org.apache.ibatis.annotations.Select; | ||
|
||
|
||
@Mapper | ||
public interface SqlMapper { | ||
@Insert("INSERT INTO sw_user (name) VALUES (#{name})") | ||
int insert(String name); | ||
|
||
@Select("SELECT name FROM sw_user") | ||
List<String> selectAll(); | ||
|
||
@Select("SELECT name FROM sw_user where id = #{id} and name = #{name}") | ||
String selectById(int id, String name); | ||
|
||
@Select("SELECT name FROM sw_user \n" | ||
+ " where id = #{id} and name = #{name}") | ||
String selectByIdAndName(int id, String name); | ||
} |