]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Fix query used to find old partitions
authorAnton Yuzhaninov <citrin+git@citrin.ru>
Wed, 29 Jul 2020 13:30:23 +0000 (14:30 +0100)
committerAnton Yuzhaninov <citrin+git@citrin.ru>
Tue, 4 Aug 2020 16:58:13 +0000 (17:58 +0100)
In ClickHouse a partition includes one or more parts.  Typically
a partition has at least a few parts.  For each part there is a row in
system.parts table and max_date can be different for different parts.

For example if we have following parts:
┌─partition──┬─table──┬───max_date─┐
│ 2020-06-01 │ rspamd │ 2020-06-07 │
│ 2020-06-08 │ rspamd │ 2020-06-10 │
│ 2020-06-08 │ rspamd │ 2020-06-14 │
│ 2020-06-15 │ rspamd │ 2020-06-21 │
└────────────┴────────┴────────────┘
and want to delete parts which don't contain data written
before 2020-06-12.

Old query will return partition which contain a part with
max_date=2020-06-14:
┌─partition──┬─table──┐
│ 2020-06-01 │ rspamd │
│ 2020-06-08 │ rspamd │
└────────────┴────────┘
and new query will return
┌─partition──┬─table──┐
│ 2020-06-01 │ rspamd │
└────────────┴────────┘

While here change <= to < so we will store at least given number of full
months.

src/plugins/lua/clickhouse.lua

index bede3f50b65543134b0ac041ced6d35e5e1a6b73..3a4b1f802831fc6deb35d4b9dbcd02f8cdbeb805 100644 (file)
@@ -1010,8 +1010,10 @@ local function clickhouse_remove_old_partitions(cfg, ev_base)
   end
 
   local upstream = settings.upstream:get_upstream_round_robin()
-  local partition_to_remove_sql = "SELECT distinct partition, table FROM system.parts WHERE " ..
-      "table in ('${tables}') and max_date <= toDate(now() - interval ${month} month);"
+  local partition_to_remove_sql = "SELECT partition, table " ..
+      "FROM system.parts WHERE table IN ('${tables}') " ..
+      "GROUP BY partition, table " ..
+      "HAVING max(max_date) < toDate(now() - interval ${month} month)"
 
   local table_names = {'rspamd'}
   local tables = table.concat(table_names, "', '")