Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor tdengine store code, auto close resource (#1513)
Browse files Browse the repository at this point in the history
Co-authored-by: Carpe-Wang <[email protected]>
2 people authored and tomsun28 committed Mar 10, 2024
1 parent 39b8421 commit 80df54d
Showing 1 changed file with 14 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -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) {
@@ -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;
}

0 comments on commit 80df54d

Please sign in to comment.