From 5f4b566c485f00031ae95c81129a83b96be3d6ce Mon Sep 17 00:00:00 2001 From: mrproliu <741550557@qq.com> Date: Fri, 14 Feb 2020 16:03:17 +0800 Subject: [PATCH] Fix missing mysql index for profile entities (#4360) * fix profile entities missed mysql index * add data length config --- .../jdbc/mysql/MySQLTableInstaller.java | 76 ++++++++++++++++++- 1 file changed, 74 insertions(+), 2 deletions(-) diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/mysql/MySQLTableInstaller.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/mysql/MySQLTableInstaller.java index 69608833b..21c2f8e30 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/mysql/MySQLTableInstaller.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/mysql/MySQLTableInstaller.java @@ -23,8 +23,9 @@ import java.sql.SQLException; import lombok.extern.slf4j.Slf4j; import org.apache.skywalking.oap.server.core.analysis.manual.segment.SegmentRecord; import org.apache.skywalking.oap.server.core.analysis.metrics.IntKeyLongValueHashMap; +import org.apache.skywalking.oap.server.core.profile.ProfileTaskLogRecord; +import org.apache.skywalking.oap.server.core.profile.ProfileThreadSnapshotRecord; import org.apache.skywalking.oap.server.core.register.RegisterSource; -import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine; import org.apache.skywalking.oap.server.core.storage.StorageException; import org.apache.skywalking.oap.server.core.storage.model.ColumnName; import org.apache.skywalking.oap.server.core.storage.model.Model; @@ -41,6 +42,8 @@ import static org.apache.skywalking.oap.server.core.source.DefaultScopeDefine.NE import static org.apache.skywalking.oap.server.core.source.DefaultScopeDefine.SEGMENT; import static org.apache.skywalking.oap.server.core.source.DefaultScopeDefine.SERVICE_INSTANCE_INVENTORY; import static org.apache.skywalking.oap.server.core.source.DefaultScopeDefine.SERVICE_INVENTORY; +import static org.apache.skywalking.oap.server.core.source.DefaultScopeDefine.PROFILE_TASK_LOG; +import static org.apache.skywalking.oap.server.core.source.DefaultScopeDefine.PROFILE_TASK_SEGMENT_SNAPSHOT; /** * Extend H2TableInstaller but match MySQL SQL syntax. @@ -75,13 +78,18 @@ public class MySQLTableInstaller extends H2TableInstaller { } else if (Double.class.equals(type) || double.class.equals(type)) { return "DOUBLE"; } else if (String.class.equals(type)) { - if (DefaultScopeDefine.SEGMENT == model.getScopeId()) { + if (SEGMENT == model.getScopeId() || PROFILE_TASK_SEGMENT_SNAPSHOT == model.getScopeId()) { if (name.getName().equals(SegmentRecord.TRACE_ID) || name.getName().equals(SegmentRecord.SEGMENT_ID)) return "VARCHAR(300)"; if (name.getName().equals(SegmentRecord.DATA_BINARY)) { return "MEDIUMTEXT"; } } + if (PROFILE_TASK_LOG == model.getScopeId() || PROFILE_TASK_SEGMENT_SNAPSHOT == model.getScopeId()) { + if (name.getName().equals(ProfileTaskLogRecord.TASK_ID)) { + return "VARCHAR(300)"; + } + } return "VARCHAR(2000)"; } else if (IntKeyLongValueHashMap.class.equals(type)) { return "MEDIUMTEXT"; @@ -112,11 +120,75 @@ public class MySQLTableInstaller extends H2TableInstaller { case ALARM: createAlarmIndexes(client, model); return; + case PROFILE_TASK_LOG: + createProfileLogIndexes(client, model); + return; + case PROFILE_TASK_SEGMENT_SNAPSHOT: + createProfileThreadSnapshotIndexes(client, model); + return; default: createIndexesForAllMetrics(client, model); } } + private void createProfileThreadSnapshotIndexes(JDBCHikariCPClient client, Model model) throws StorageException { + try (Connection connection = client.getConnection()) { + // query by task id, sequence + SQLBuilder tableIndexSQL = new SQLBuilder("CREATE INDEX "); + tableIndexSQL.append(model.getName().toUpperCase()).append("_TASK_ID_SEQUENCE "); + tableIndexSQL.append("ON ") + .append(model.getName()) + .append("(") + .append(ProfileThreadSnapshotRecord.TASK_ID) + .append(", ") + .append(ProfileThreadSnapshotRecord.SEQUENCE) + .append(")"); + createIndex(client, connection, model, tableIndexSQL); + + // query by segment id, sequence + tableIndexSQL = new SQLBuilder("CREATE INDEX "); + tableIndexSQL.append(model.getName().toUpperCase()).append("_SEGMENT_ID_SEQUENCE "); + tableIndexSQL.append("ON ") + .append(model.getName()) + .append("(") + .append(ProfileThreadSnapshotRecord.SEGMENT_ID) + .append(", ") + .append(ProfileThreadSnapshotRecord.SEQUENCE) + .append(")"); + createIndex(client, connection, model, tableIndexSQL); + + // query by segment id, dump time + tableIndexSQL = new SQLBuilder("CREATE INDEX "); + tableIndexSQL.append(model.getName().toUpperCase()).append("_SEGMENT_ID_DUMP_TIME "); + tableIndexSQL.append("ON ") + .append(model.getName()) + .append("(") + .append(ProfileThreadSnapshotRecord.SEGMENT_ID) + .append(", ") + .append(ProfileThreadSnapshotRecord.DUMP_TIME) + .append(")"); + createIndex(client, connection, model, tableIndexSQL); + } catch (JDBCClientException | SQLException e) { + throw new StorageException(e.getMessage(), e); + } + } + + private void createProfileLogIndexes(JDBCHikariCPClient client, Model model) throws StorageException { + try (Connection connection = client.getConnection()) { + // query by task id + SQLBuilder tableIndexSQL = new SQLBuilder("CREATE INDEX "); + tableIndexSQL.append(model.getName().toUpperCase()).append("_TASK_ID "); + tableIndexSQL.append("ON ") + .append(model.getName()) + .append("(") + .append(ProfileTaskLogRecord.TASK_ID) + .append(")"); + createIndex(client, connection, model, tableIndexSQL); + } catch (JDBCClientException | SQLException e) { + throw new StorageException(e.getMessage(), e); + } + } + private void createIndexesForAllMetrics(JDBCHikariCPClient client, Model model) throws StorageException { try (Connection connection = client.getConnection()) { SQLBuilder tableIndexSQL = new SQLBuilder("CREATE INDEX ");