Fix missing mysql index for profile entities (#4360)

* fix profile entities missed mysql index

* add data length config
This commit is contained in:
mrproliu 2020-02-14 16:03:17 +08:00 committed by GitHub
parent 6be3e9909a
commit 5f4b566c48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 74 additions and 2 deletions

View File

@ -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 ");