Skip to content

Commit

Permalink
[feat] supports TTL for greptimedb data storage (#2101)
Browse files Browse the repository at this point in the history
  • Loading branch information
killme2008 authored Jun 20, 2024
1 parent bd52e05 commit a32757e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
3 changes: 2 additions & 1 deletion home/docs/start/greptime-init.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ warehouse:
driver-class-name: com.mysql.cj.jdbc.Driver
username: greptime
password: greptime
expire-time: 30d
```
The default database is `hertzbeat` in the `url`.
The default database is `hertzbeat` in the `url`, and it will be created automatically. The `expire-time` specifies the TTL(time-to-live) of the auto-created database, it's 30 days by default.

2. Restart HertzBeat

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ warehouse:
driver-class-name: com.mysql.cj.jdbc.Driver
username: greptime
password: greptime
expire-time: 30d
```
默认数据库是 URL 中配置的 `hertzbeat` 。
默认数据库是 URL 中配置的 `hertzbeat` ,将自动创建。 `expire-time` 是自动创建的数据库的 TTL (数据过期)时间,默认为 30 天

2. 重启 HertzBeat

Expand Down
1 change: 1 addition & 0 deletions manager/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ warehouse:
driver-class-name: com.mysql.cj.jdbc.Driver
username: greptime
password: greptime
expire-time: 30d
iot-db:
enabled: false
host: 127.0.0.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
@Slf4j
public class GreptimeDbDataStorage extends AbstractHistoryDataStorage {

private static final String CONSTANT_DB_TTL = "30d";

private static final String QUERY_HISTORY_SQL = "SELECT CAST (ts AS Int64) ts, instance, `%s` FROM `%s` WHERE ts >= now() - interval '%s' and monitor_id = %s order by ts desc;";

@SuppressWarnings("checkstyle:LineLength")
Expand All @@ -76,7 +78,7 @@ public class GreptimeDbDataStorage extends AbstractHistoryDataStorage {

private static final String TABLE_NOT_EXIST = "not found";

private static final String CONSTANTS_CREATE_DATABASE = "CREATE DATABASE IF NOT EXISTS `%s`";
private static final String CONSTANTS_CREATE_DATABASE = "CREATE DATABASE IF NOT EXISTS `%s` WITH(ttl='%s')";

private static final Runnable INSTANCE_EXCEPTION_PRINT = () -> {
if (log.isErrorEnabled()) {
Expand All @@ -97,7 +99,7 @@ public GreptimeDbDataStorage(GreptimeProperties greptimeProperties) {
log.error("init error, please config Warehouse GreptimeDB props in application.yml");
throw new IllegalArgumentException("please config Warehouse GreptimeDB props");
}

serverAvailable = initGreptimeDbClient(greptimeProperties) && initGreptimeDbDataSource(greptimeProperties);
}

Expand All @@ -107,10 +109,15 @@ private void initGreptimeDb(final GreptimeProperties greptimeProperties) throws
final String port = ObjectUtils.requireNonEmpty(properties[1].value);
final String dbName = ObjectUtils.requireNonEmpty(properties[2].value);

String ttl = greptimeProperties.expireTime();
if (ttl == null || "".equals(ttl.trim())) {
ttl = CONSTANT_DB_TTL;
}

try (final Connection tempConnection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port,
greptimeProperties.username(), greptimeProperties.password());
final PreparedStatement pstmt = tempConnection
.prepareStatement(String.format(CONSTANTS_CREATE_DATABASE, dbName))) {
.prepareStatement(String.format(CONSTANTS_CREATE_DATABASE, dbName, ttl))) {
log.info("[warehouse greptime] try to create database `{}` if not exists", dbName);
pstmt.execute();
}
Expand All @@ -121,7 +128,7 @@ private boolean initGreptimeDbClient(GreptimeProperties greptimeProperties) {
try {
final DriverPropertyInfo[] properties = new Driver().getPropertyInfo(greptimeProperties.url(), null);
final String dbName = ObjectUtils.requireNonEmpty(properties[2].value);

GreptimeOptions opts = GreptimeOptions.newBuilder(endpoints.split(","), dbName) //
.writeMaxRetries(3) //
.authInfo(new AuthInfo(greptimeProperties.username(), greptimeProperties.password()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@
public record GreptimeProperties(@DefaultValue("false") boolean enabled,
@DefaultValue("127.0.0.1:4001") String grpcEndpoints,
@DefaultValue("jdbc:mysql://127.0.0.1:4002/hertzbeat?connectionTimeZone=Asia/Shanghai&forceConnectionTimeZoneToSession=true") String url,
@DefaultValue("com.mysql.cj.jdbc.Driver") String driverClassName, String username, String password) {
@DefaultValue("com.mysql.cj.jdbc.Driver") String driverClassName, String username, String password,
// Database TTL, default is 30 days.
@DefaultValue("30d") String expireTime) {
}

0 comments on commit a32757e

Please sign in to comment.