feat: add admin function to discard unflushed data (#8768)

* feat: add admin function to discard unflushed data

Signed-off-by: evenyag <realevenyag@gmail.com>

* test: cover discarding unflushed data by table

Signed-off-by: evenyag <realevenyag@gmail.com>

* chore: fix license header

Signed-off-by: evenyag <realevenyag@gmail.com>

* fix: reject discarding logical metric table data

Signed-off-by: evenyag <realevenyag@gmail.com>

* refactor: defer table name formatting in error paths

Signed-off-by: evenyag <realevenyag@gmail.com>

* chore(deps): update greptime-proto revision

Signed-off-by: evenyag <realevenyag@gmail.com>

* refactor: rename discard unflushed admin function

Signed-off-by: evenyag <realevenyag@gmail.com>

---------

Signed-off-by: evenyag <realevenyag@gmail.com>
(cherry picked from commit 68605aa64d25a24e14b3bbf0b62e453d97179947)
This commit is contained in:
Yingwen
2026-08-20 14:44:57 +08:00
committed by discord9
parent 1ad6c2a1cd
commit cf69ee5df9
12 changed files with 696 additions and 4 deletions
@@ -0,0 +1,62 @@
CREATE TABLE discard_unflushed_data_test (
host STRING PRIMARY KEY,
val DOUBLE,
ts TIMESTAMP TIME INDEX
) ENGINE = mito;
INSERT INTO discard_unflushed_data_test VALUES ('persisted', 1, 1);
ADMIN FLUSH_TABLE('discard_unflushed_data_test');
INSERT INTO discard_unflushed_data_test VALUES ('unflushed', 2, 2);
ADMIN discard_unflushed('discard_unflushed_data_test');
-- Repeating the operation is idempotent.
ADMIN discard_unflushed('discard_unflushed_data_test');
SELECT host, val FROM discard_unflushed_data_test ORDER BY host;
-- The function must only be available through ADMIN.
-- SQLNESS REPLACE \nDid\syou\smean.*
SELECT discard_unflushed(0);
DROP TABLE discard_unflushed_data_test;
CREATE TABLE discard_unflushed_data_test (
host STRING PRIMARY KEY,
val DOUBLE,
ts TIMESTAMP TIME INDEX
) ENGINE = mito;
INSERT INTO discard_unflushed_data_test VALUES ('unflushed', 1, 1);
-- The table has no persisted data, so discarding leaves it empty.
ADMIN discard_unflushed('discard_unflushed_data_test');
SELECT COUNT(*) FROM discard_unflushed_data_test;
DROP TABLE discard_unflushed_data_test;
CREATE TABLE discard_unflushed_data_physical (
ts TIMESTAMP TIME INDEX,
val DOUBLE
) ENGINE = metric WITH ("physical_metric_table" = "");
CREATE TABLE discard_unflushed_data_logical (
host STRING PRIMARY KEY,
val DOUBLE,
ts TIMESTAMP TIME INDEX
) ENGINE = metric WITH ("on_physical_table" = "discard_unflushed_data_physical");
INSERT INTO discard_unflushed_data_logical VALUES ('unflushed', 1, 1);
-- Logical Metric Engine tables share their physical regions with other logical tables.
-- Discarding by logical table name must not truncate those shared regions.
ADMIN discard_unflushed('discard_unflushed_data_logical');
SELECT host, val FROM discard_unflushed_data_logical;
DROP TABLE discard_unflushed_data_logical;
DROP TABLE discard_unflushed_data_physical;