-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ydbcp): support ttl for backups
- Loading branch information
1 parent
1e9293a
commit aa60231
Showing
15 changed files
with
167 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
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" | ||
"time" | ||
"ydbcp/internal/connectors/db" | ||
"ydbcp/internal/connectors/db/yql/queries" | ||
"ydbcp/internal/types" | ||
"ydbcp/internal/util/ticker" | ||
"ydbcp/internal/util/xlog" | ||
pb "ydbcp/pkg/proto/ydbcp/v1alpha1" | ||
) | ||
|
||
type TtlWatcherImpl struct { | ||
ctx context.Context | ||
period time.Duration | ||
handleOperationTimeout time.Duration | ||
tickerProvider ticker.TickerProvider | ||
db db.DBConnector | ||
queryBuilderFactory queries.WriteQueryBulderFactory | ||
} | ||
|
||
func NewTtlWatcher( | ||
ctx context.Context, | ||
wg *sync.WaitGroup, | ||
db db.DBConnector, | ||
queryBuilderFactory queries.WriteQueryBulderFactory, | ||
) *TtlWatcherImpl { | ||
w := &TtlWatcherImpl{ | ||
ctx: ctx, | ||
period: time.Hour, | ||
db: db, | ||
tickerProvider: ticker.NewRealTicker, | ||
queryBuilderFactory: queryBuilderFactory, | ||
} | ||
|
||
wg.Add(1) | ||
go w.run(wg) | ||
return w | ||
} | ||
|
||
func (o *TtlWatcherImpl) run(wg *sync.WaitGroup) { | ||
defer wg.Done() | ||
xlog.Debug(o.ctx, "Ttl watcher started", zap.Duration("period", o.period)) | ||
ticker := o.tickerProvider(o.period) | ||
for { | ||
select { | ||
case <-o.ctx.Done(): | ||
ticker.Stop() | ||
xlog.Debug(o.ctx, "Ttl watcher stopped") | ||
return | ||
case <-ticker.Chan(): | ||
o.deleteOutdatedBackups() | ||
} | ||
} | ||
} | ||
|
||
func (o *TtlWatcherImpl) deleteOutdatedBackups() { | ||
ctx, cancel := context.WithTimeout(o.ctx, o.period) | ||
defer cancel() | ||
|
||
backups, err := o.db.SelectBackups( | ||
ctx, queries.NewReadTableQuery( | ||
queries.WithTableName("Backups"), | ||
queries.WithSelectFields(queries.AllBackupFields...), | ||
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), | ||
}, | ||
}, | ||
), | ||
), | ||
) | ||
|
||
if err != nil { | ||
xlog.Error(ctx, "can't select backups", zap.Error(err)) | ||
return | ||
} | ||
|
||
for _, backup := range backups { | ||
if backup.ExpireAt != nil && backup.ExpireAt.AsTime().Before(timestamppb.Now().AsTime()) { | ||
now := timestamppb.Now() | ||
dbOp := &types.DeleteBackupOperation{ | ||
ContainerID: backup.ContainerID, | ||
BackupID: backup.ID, | ||
State: types.OperationStatePending, | ||
YdbConnectionParams: types.YdbConnectionParams{ | ||
DatabaseName: backup.DatabaseName, | ||
Endpoint: backup.DatabaseEndpoint, | ||
}, | ||
Audit: &pb.AuditInfo{ | ||
CreatedAt: now, | ||
Creator: "ydbcp", | ||
}, | ||
PathPrefix: backup.S3PathPrefix, | ||
UpdatedAt: now, | ||
} | ||
|
||
backupToWrite := types.Backup{ | ||
ID: backup.ID, | ||
Status: types.BackupStateDeleting, | ||
} | ||
|
||
err := o.db.ExecuteUpsert( | ||
ctx, o.queryBuilderFactory().WithCreateOperation(dbOp).WithUpdateBackup(backupToWrite), | ||
) | ||
|
||
if err != nil { | ||
xlog.Error(ctx, "can't create DeleteBackup operation", zap.String("BackupID", backup.ID), zap.Error(err)) | ||
} | ||
|
||
xlog.Debug(ctx, "DeleteBackup operation was created successfully", zap.String("BackupID", backup.ID)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package ttl_watcher | ||
|
||
// TODO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters