Skip to content

Commit

Permalink
refactor tdengine store code, auto close resource (#1513)
Browse files Browse the repository at this point in the history
Co-authored-by: Carpe-Wang <wangcarpe@126.com>
  • Loading branch information
Carpe-Wang and Carpe-Wang authored Jan 24, 2024
1 parent 293bfba commit f7e4ffe
Showing 1 changed file with 14 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,25 +231,23 @@ public void destroy() {
hikariDataSource.close();
}
}

@Override
public Map<String, List<Value>> getHistoryMetricData(Long monitorId, String app, String metrics, String metric, String label, String history) {
String table = app + "_" + metrics + "_" + monitorId;
String selectSql = label == null ? String.format(QUERY_HISTORY_SQL, metric, table, history) :
String.format(QUERY_HISTORY_WITH_INSTANCE_SQL, metric, table, label, history);
log.debug(selectSql);
Connection connection = null;
Map<String, List<Value>> instanceValuesMap = new HashMap<>(8);
try {
if (!serverAvailable) {
log.error("\n\t---------------TdEngine Init Failed---------------\n" +
"\t--------------Please Config Tdengine--------------\n" +
"\t----------Can Not Use Metric History Now----------\n");
return instanceValuesMap;
}
connection = hikariDataSource.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(selectSql);
if (!serverAvailable) {
log.error("\n\t---------------TdEngine Init Failed---------------\n" +
"\t--------------Please Config Tdengine--------------\n" +
"\t----------Can Not Use Metric History Now----------\n");
return instanceValuesMap;
}
try (Connection connection = hikariDataSource.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(selectSql)) {
while (resultSet.next()) {
Timestamp ts = resultSet.getTimestamp(1);
if (ts == null) {
Expand All @@ -265,22 +263,14 @@ public Map<String, List<Value>> getHistoryMetricData(Long monitorId, String app,
List<Value> valueList = instanceValuesMap.computeIfAbsent(instanceValue, k -> new LinkedList<>());
valueList.add(new Value(strValue, ts.getTime() / 100 * 100));
}
resultSet.close();
return instanceValuesMap;
} catch (SQLException sqlException) {
String msg = sqlException.getMessage();
if (msg != null && !msg.contains(TABLE_NOT_EXIST)) {
log.warn(sqlException.getMessage());
}
String msg = sqlException.getMessage();
if (msg != null && !msg.contains(TABLE_NOT_EXIST)) {
log.warn(sqlException.getMessage());
}
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
try {
assert connection != null;
connection.close();
} catch (Exception e) {
log.error(e.getMessage());
}
}
return instanceValuesMap;
}
Expand Down

0 comments on commit f7e4ffe

Please sign in to comment.