You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2021/10/25 14:55:55 UTC

[skywalking] branch master updated: Fix unexpected deleting due to TTL mechanism bug (#8000)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new a68503b  Fix unexpected deleting due to TTL mechanism bug (#8000)
a68503b is described below

commit a68503b39f638809c6f61372f777e4892cb09cf7
Author: 吴晟 Wu Sheng <wu...@foxmail.com>
AuthorDate: Mon Oct 25 22:55:42 2021 +0800

    Fix unexpected deleting due to TTL mechanism bug (#8000)
---
 CHANGES.md                                                    |  1 +
 .../server/storage/plugin/jdbc/h2/dao/H2HistoryDeleteDAO.java | 11 +++++++++--
 .../server/storage/plugin/jdbc/tidb/TiDBHistoryDeleteDAO.java |  9 ++++++++-
 3 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index b9d95b3..55601ce 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -50,6 +50,7 @@ Release Notes.
 * Support search browser service.
 * Add `getProfileTaskLogs` to profile query protocol.
 * Set `SW_KAFKA_FETCHER_ENABLE_NATIVE_PROTO_LOG`, `SW_KAFKA_FETCHER_ENABLE_NATIVE_JSON_LOG` default `true`.
+* Fix unexpected deleting due to TTL mechanism bug for H2, MySQL, TiDB and PostgreSQL.
 
 #### UI
 
diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2HistoryDeleteDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2HistoryDeleteDAO.java
index d3367f7..bac373d 100644
--- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2HistoryDeleteDAO.java
+++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2HistoryDeleteDAO.java
@@ -39,28 +39,35 @@ public class H2HistoryDeleteDAO implements IHistoryDeleteDAO {
     @Override
     public void deleteHistory(Model model, String timeBucketColumnName, int ttl) throws IOException {
         SQLBuilder dataDeleteSQL = new SQLBuilder("delete from " + model.getName() + " where ")
-            .append(timeBucketColumnName).append("<= ? ");
+            .append(timeBucketColumnName).append("<= ? ")
+            .append(" and ")
+            .append(timeBucketColumnName).append(">= ? ");
 
         try (Connection connection = client.getConnection()) {
             long deadline;
+            long minTime;
             if (model.isRecord()) {
                 deadline = Long.parseLong(new DateTime().plusDays(-ttl).toString("yyyyMMddHHmmss"));
+                minTime = 1000_00_00_00_00_00L;
             } else {
                 switch (model.getDownsampling()) {
                     case Minute:
                         deadline = Long.parseLong(new DateTime().plusDays(-ttl).toString("yyyyMMddHHmm"));
+                        minTime = 1000_00_00_00_00L;
                         break;
                     case Hour:
                         deadline = Long.parseLong(new DateTime().plusDays(-ttl).toString("yyyyMMddHH"));
+                        minTime = 1000_00_00_00L;
                         break;
                     case Day:
                         deadline = Long.parseLong(new DateTime().plusDays(-ttl).toString("yyyyMMdd"));
+                        minTime = 1000_00_00L;
                         break;
                     default:
                         return;
                 }
             }
-            client.executeUpdate(connection, dataDeleteSQL.toString(), deadline);
+            client.executeUpdate(connection, dataDeleteSQL.toString(), deadline, minTime);
         } catch (JDBCClientException | SQLException e) {
             throw new IOException(e.getMessage(), e);
         }
diff --git a/oap-server/server-storage-plugin/storage-tidb-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/tidb/TiDBHistoryDeleteDAO.java b/oap-server/server-storage-plugin/storage-tidb-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/tidb/TiDBHistoryDeleteDAO.java
index be3208b..5c0ea26 100644
--- a/oap-server/server-storage-plugin/storage-tidb-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/tidb/TiDBHistoryDeleteDAO.java
+++ b/oap-server/server-storage-plugin/storage-tidb-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/tidb/TiDBHistoryDeleteDAO.java
@@ -40,28 +40,35 @@ public class TiDBHistoryDeleteDAO implements IHistoryDeleteDAO {
     public void deleteHistory(Model model, String timeBucketColumnName, int ttl) throws IOException {
         SQLBuilder dataDeleteSQL = new SQLBuilder("delete from " + model.getName() + " where ")
             .append(timeBucketColumnName).append("<= ? ")
+            .append(" and ")
+            .append(timeBucketColumnName).append(">= ? ")
             .append(" limit 10000");
 
         try (Connection connection = client.getConnection()) {
             long deadline;
+            long minTime;
             if (model.isRecord()) {
                 deadline = Long.parseLong(new DateTime().plusDays(-ttl).toString("yyyyMMddHHmmss"));
+                minTime = 1000_00_00_00_00_00L;
             } else {
                 switch (model.getDownsampling()) {
                     case Minute:
                         deadline = Long.parseLong(new DateTime().plusDays(-ttl).toString("yyyyMMddHHmm"));
+                        minTime = 1000_00_00_00_00L;
                         break;
                     case Hour:
                         deadline = Long.parseLong(new DateTime().plusDays(-ttl).toString("yyyyMMddHH"));
+                        minTime = 1000_00_00_00L;
                         break;
                     case Day:
                         deadline = Long.parseLong(new DateTime().plusDays(-ttl).toString("yyyyMMdd"));
+                        minTime = 1000_00_00L;
                         break;
                     default:
                         return;
                 }
             }
-            while (client.executeUpdate(connection, dataDeleteSQL.toString(), deadline) > 0) {
+            while (client.executeUpdate(connection, dataDeleteSQL.toString(), deadline, minTime) > 0) {
             }
         } catch (JDBCClientException | SQLException e) {
             throw new IOException(e.getMessage(), e);