From 839cccc1f2fd66dee1e5983c3e142f39854308fd Mon Sep 17 00:00:00 2001 From: Aleksei Pleshakov Date: Tue, 19 Nov 2024 15:33:14 +0400 Subject: [PATCH] fix select in ttl watcher --- internal/connectors/db/yql/queries/queries.go | 4 ++++ .../connectors/db/yql/schema/create_tables.yql | 1 + internal/watchers/ttl_watcher/ttl_watcher.go | 15 ++------------- internal/watchers/ttl_watcher/ttl_watcher_test.go | 2 +- 4 files changed, 8 insertions(+), 14 deletions(-) diff --git a/internal/connectors/db/yql/queries/queries.go b/internal/connectors/db/yql/queries/queries.go index f36b4aa8..a54b349e 100644 --- a/internal/connectors/db/yql/queries/queries.go +++ b/internal/connectors/db/yql/queries/queries.go @@ -27,4 +27,8 @@ $last_backup_id = SELECT MAX_BY(b.id, b.completed_at) AS last_backup_id FROM Bac SELECT s.*, $last_backup_id AS last_backup_id, $rpo_info.recovery_point AS recovery_point, $rpo_info.last_successful_backup_id AS last_successful_backup_id FROM BackupSchedules AS s WHERE s.id = $schedule_id `, types.BackupStateAvailable, ) + GetBackupsToDeleteQuery = fmt.Sprintf( + `SELECT * FROM Backups VIEW idx_expire_at WHERE status != '%s' and status != '%s' AND expire_at < CurrentUtcTimestamp() LIMIT 100`, + types.BackupStateDeleted, + types.BackupStateDeleting) ) diff --git a/internal/connectors/db/yql/schema/create_tables.yql b/internal/connectors/db/yql/schema/create_tables.yql index 775db996..416ea3bc 100644 --- a/internal/connectors/db/yql/schema/create_tables.yql +++ b/internal/connectors/db/yql/schema/create_tables.yql @@ -24,6 +24,7 @@ CREATE TABLE Backups ( INDEX idx_container_id GLOBAL ON (container_id), INDEX idx_created_at GLOBAL ON (created_at), + INDEX idx_expire_at GLOBAL ON (status, expire_at), PRIMARY KEY (id) ); diff --git a/internal/watchers/ttl_watcher/ttl_watcher.go b/internal/watchers/ttl_watcher/ttl_watcher.go index eb30a6a8..8e198f84 100644 --- a/internal/watchers/ttl_watcher/ttl_watcher.go +++ b/internal/watchers/ttl_watcher/ttl_watcher.go @@ -2,7 +2,6 @@ package ttl_watcher import ( "context" - table_types "github.com/ydb-platform/ydb-go-sdk/v3/table/types" "go.uber.org/zap" "google.golang.org/protobuf/types/known/timestamppb" "sync" @@ -28,7 +27,7 @@ func NewTtlWatcher( func(ctx context.Context, period time.Duration) { TtlWatcherAction(ctx, period, db, queryBuilderFactory) }, - time.Hour, + time.Minute, "Ttl", options..., ) @@ -45,17 +44,7 @@ func TtlWatcherAction( backups, err := db.SelectBackups( ctx, queries.NewReadTableQuery( - queries.WithTableName("Backups"), - queries.WithQueryFilters( - queries.QueryFilter{ - Field: "status", - Values: []table_types.Value{ - table_types.StringValueFromString(types.BackupStateAvailable), - table_types.StringValueFromString(types.BackupStateError), - table_types.StringValueFromString(types.BackupStateCancelled), - }, - }, - ), + queries.WithRawQuery(queries.GetBackupsToDeleteQuery), ), ) diff --git a/internal/watchers/ttl_watcher/ttl_watcher_test.go b/internal/watchers/ttl_watcher/ttl_watcher_test.go index e2e5a913..33f83531 100644 --- a/internal/watchers/ttl_watcher/ttl_watcher_test.go +++ b/internal/watchers/ttl_watcher/ttl_watcher_test.go @@ -61,7 +61,7 @@ func TestTtlWatcher(t *testing.T) { case <-ctx.Done(): t.Error("ticker not initialized") case <-tickerInitialized: - assert.Equal(t, fakeTicker.Period, time.Hour, "incorrect period") + assert.Equal(t, fakeTicker.Period, time.Minute, "incorrect period") } // Send a tick to the fake ticker