Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[warehouse] support iotdb connection available check when init #384

Merged
merged 1 commit into from
Oct 23, 2022
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 @@ -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