Skip to content

Commit

Permalink
[warehouse] support iotdb connection available check when init (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsun28 committed Oct 23, 2022
1 parent 3a4d2fc commit d35084d
Showing 1 changed file with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@ public class HistoryIotDbDataStorage extends AbstractHistoryDataStorage {

private static final String SHOW_DEVICES = "SHOW DEVICES %s";

private static final String SHOW_STORAGE_GROUP = "show storage group";

private static final String QUERY_HISTORY_SQL
= "SELECT %s FROM %s WHERE Time >= now() - %s order by Time desc";
private static final String QUERY_HISTORY_INTERVAL_WITH_INSTANCE_SQL
= "SELECT FIRST_VALUE(%s), AVG(%s), MIN_VALUE(%s), MAX_VALUE(%s) FROM %s GROUP BY ([now() - %s, now()), 4h) WITHOUT NULL ANY";

private SessionPool sessionPool;

// Session有这两个字段的set方法,sessionPool暂未发现,目前存储在此类中
/**
* Session有这两个字段的set方法,sessionPool暂未发现,目前存储在此类中
* version: ioTDb version
* <p>用来区分不同版本的ioTDb</p>
*/
Expand Down Expand Up @@ -104,9 +106,22 @@ private boolean initIotDbSession(WarehouseProperties.StoreProperties.IotDbProper
}
this.queryTimeoutInMs = properties.getQueryTimeoutInMs();
this.sessionPool = builder.build();
this.initTtl(properties.getExpireTime());
log.info("IotDB session pool init success");
return true;
boolean available = checkConnection();
if (available) {
this.initTtl(properties.getExpireTime());
log.info("IotDB session pool init success");
}
return available;
}

private boolean checkConnection() {
try {
this.sessionPool.executeNonQueryStatement(SHOW_STORAGE_GROUP);
return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
}
}

private void initTtl(String expireTime) {
Expand All @@ -124,7 +139,7 @@ private void initTtl(String expireTime) {
}
} catch (IoTDBConnectionException | StatementExecutionException e) {
// 失败不影响主业务
log.error("IoTDB init ttl error, expireTime: {}", expireTime);
log.error("IoTDB init ttl error, expireTime: {}, error: {}", expireTime, e.getMessage());
}
}

Expand Down Expand Up @@ -194,6 +209,9 @@ void saveData(CollectRep.MetricsData metricsData) {
public Map<String, List<Value>> getHistoryMetricData(Long monitorId, String app, String metrics, String metric, String instance, String history) {
Map<String, List<Value>> instanceValuesMap = new HashMap<>(8);
if (!isServerAvailable()) {
log.error("\n\t---------------IotDb Init Failed---------------\n" +
"\t--------------Please Config IotDb--------------\n" +
"\t----------Can Not Use Metric History Now----------\n");
return instanceValuesMap;
}
String deviceId = getDeviceId(app, metrics, monitorId, instance, true);
Expand Down Expand Up @@ -243,6 +261,9 @@ private void handleHistorySelect(String selectSql, String instanceId, Map<String
public Map<String, List<Value>> getHistoryIntervalMetricData(Long monitorId, String app, String metrics, String metric, String instance, String history) {
Map<String, List<Value>> instanceValuesMap = new HashMap<>(8);
if (!isServerAvailable()) {
log.error("\n\t---------------IotDb Init Failed---------------\n" +
"\t--------------Please Config IotDb--------------\n" +
"\t----------Can Not Use Metric History Now----------\n");
return instanceValuesMap;
}
String deviceId = getDeviceId(app, metrics, monitorId, instance, true);
Expand Down

0 comments on commit d35084d

Please sign in to comment.