Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,14 @@ public class TdEngineDataStorage extends AbstractHistoryDataStorage {
private static final String INSTANCE_NULL = "''";
private static final String CONSTANTS_CREATE_DATABASE = "CREATE DATABASE IF NOT EXISTS %s";
private static final String INSERT_TABLE_DATA_SQL = "INSERT INTO `%s` USING `%s` TAGS (%s) VALUES %s";
private static final String CREATE_SUPER_TABLE_SQL = "CREATE STABLE IF NOT EXISTS `%s` %s TAGS (monitor BIGINT)";
private static final String CREATE_SUPER_TABLE_SQL = "CREATE STABLE IF NOT EXISTS `%s` %s TAGS (instance NCHAR(128))";
private static final String NO_SUPER_TABLE_ERROR = "Table does not exist";
/**
* schema version suffix - increment when TAG definition changes.
* v1: instance BIGINT (original monitorId like 123456789)
* v2: instance NCHAR(128) (supports string instance values like 127.0.0.1:8080)
*/
private static final String SUPER_TABLE_VERSION = "_v2";

private static final String QUERY_HISTORY_WITH_INSTANCE_SQL =
"SELECT ts, metric_labels, `%s` FROM `%s` WHERE metric_labels = '%s' AND ts >= now - %s order by ts desc";
Expand Down Expand Up @@ -185,7 +191,7 @@ public void saveData(CollectRep.MetricsData metricsData) {
String instance = metricsData.getInstance();
String app = metricsData.getApp();
String metrics = metricsData.getMetrics();
String superTable = getTable(app, metrics, "_super");
String superTable = getSuperTable(app, metrics);
String table = getTable(app, metrics, instance);
StringBuilder sqlBuffer = new StringBuilder();
int i = 0;
Expand Down Expand Up @@ -246,7 +252,7 @@ public void saveData(CollectRep.MetricsData metricsData) {
sqlBuffer.append(" ").append(String.format(sqlRowBuffer.toString(), formatStringValue(JsonUtil.toJson(labels))));
}

String insertDataSql = String.format(INSERT_TABLE_DATA_SQL, table, superTable, instance, sqlBuffer);
String insertDataSql = String.format(INSERT_TABLE_DATA_SQL, table, superTable, "'" + formatStringValue(instance) + "'", sqlBuffer);

if (log.isDebugEnabled()) {
log.debug(insertDataSql);
Expand Down Expand Up @@ -325,7 +331,12 @@ private static String getTable(String app, String metrics, String instance) {
.replace("[", "_")
.replace("]", "_");
}
return app + "_" + metrics + "_" + instance;
return app + "_" + metrics + "_" + instance + SUPER_TABLE_VERSION;
}

@NotNull
private static String getSuperTable(String app, String metrics) {
return app + "_" + metrics + "_super" + SUPER_TABLE_VERSION;
}

private String formatStringValue(String value) {
Expand Down
Loading