This is an automated email from the ASF dual-hosted git repository.

gongchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git


The following commit(s) were added to refs/heads/master by this push:
     new a32757ea7 [feat] supports TTL for greptimedb data storage (#2101)
a32757ea7 is described below

commit a32757ea7ab4f3075301562d1b322863ad6e1c5f
Author: dennis zhuang <[email protected]>
AuthorDate: Wed Jun 19 17:57:06 2024 -0700

    [feat] supports TTL for greptimedb data storage (#2101)
---
 home/docs/start/greptime-init.md                          |  3 ++-
 .../current/start/greptime-init.md                        |  3 ++-
 manager/src/main/resources/application.yml                |  1 +
 .../store/history/greptime/GreptimeDbDataStorage.java     | 15 +++++++++++----
 .../store/history/greptime/GreptimeProperties.java        |  4 +++-
 5 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/home/docs/start/greptime-init.md b/home/docs/start/greptime-init.md
index f4a0ccbdd..92dec8f6d 100644
--- a/home/docs/start/greptime-init.md
+++ b/home/docs/start/greptime-init.md
@@ -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
 
diff --git 
a/home/i18n/zh-cn/docusaurus-plugin-content-docs/current/start/greptime-init.md 
b/home/i18n/zh-cn/docusaurus-plugin-content-docs/current/start/greptime-init.md
index ab9997786..57cf7603a 100644
--- 
a/home/i18n/zh-cn/docusaurus-plugin-content-docs/current/start/greptime-init.md
+++ 
b/home/i18n/zh-cn/docusaurus-plugin-content-docs/current/start/greptime-init.md
@@ -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
 
diff --git a/manager/src/main/resources/application.yml 
b/manager/src/main/resources/application.yml
index 053008a81..ab9004bda 100644
--- a/manager/src/main/resources/application.yml
+++ b/manager/src/main/resources/application.yml
@@ -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
diff --git 
a/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/greptime/GreptimeDbDataStorage.java
 
b/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/greptime/GreptimeDbDataStorage.java
index a44417121..aefd45e09 100644
--- 
a/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/greptime/GreptimeDbDataStorage.java
+++ 
b/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/greptime/GreptimeDbDataStorage.java
@@ -64,6 +64,8 @@ import org.springframework.stereotype.Component;
 @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")
@@ -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()) {
@@ -97,7 +99,7 @@ public class GreptimeDbDataStorage extends 
AbstractHistoryDataStorage {
            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);
     }
 
@@ -107,10 +109,15 @@ public class GreptimeDbDataStorage extends 
AbstractHistoryDataStorage {
        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();
        }
@@ -121,7 +128,7 @@ public class GreptimeDbDataStorage extends 
AbstractHistoryDataStorage {
        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()))
diff --git 
a/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/greptime/GreptimeProperties.java
 
b/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/greptime/GreptimeProperties.java
index ac8e29f9f..bd02be75c 100644
--- 
a/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/greptime/GreptimeProperties.java
+++ 
b/warehouse/src/main/java/org/apache/hertzbeat/warehouse/store/history/greptime/GreptimeProperties.java
@@ -27,5 +27,7 @@ import 
org.springframework.boot.context.properties.bind.DefaultValue;
 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) {
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to