-
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
02af8fd
commit f972b64
Showing
10 changed files
with
276 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
table_types "github.com/ydb-platform/ydb-go-sdk/v3/table/types" | ||
"go.uber.org/zap" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
"ydbcp/internal/connectors/db" | ||
"ydbcp/internal/connectors/db/yql/queries" | ||
"ydbcp/internal/types" | ||
"ydbcp/internal/util/xlog" | ||
pb "ydbcp/pkg/proto/ydbcp/v1alpha1" | ||
) | ||
|
||
func NewDOBOperationHandler( | ||
db db.DBConnector, | ||
queryBuilderFactory queries.WriteQueryBulderFactory, | ||
) types.OperationHandler { | ||
return func(ctx context.Context, op types.Operation) error { | ||
return DOBOperationHandler(ctx, op, db, queryBuilderFactory) | ||
} | ||
} | ||
|
||
func DOBOperationHandler( | ||
ctx context.Context, | ||
operation types.Operation, | ||
db db.DBConnector, | ||
queryBuilderFactory queries.WriteQueryBulderFactory, | ||
) error { | ||
xlog.Info(ctx, "DOBOperationHandler", zap.String("OperationMessage", operation.GetMessage())) | ||
|
||
if operation.GetType() != types.OperationTypeDOB { | ||
return fmt.Errorf( | ||
"wrong type %s != %s for operation %s", | ||
operation.GetType(), types.OperationTypeDOB, types.OperationToString(operation), | ||
) | ||
} | ||
|
||
_, ok := operation.(*types.DeleteOutdatedBackupsOperation) | ||
if !ok { | ||
return fmt.Errorf("can't cast operation to DeleteOutadatedBackupsOperation %s", types.OperationToString(operation)) | ||
} | ||
|
||
backups, err := 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 { | ||
return fmt.Errorf("can't select backups: %v", err) | ||
} | ||
|
||
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 := db.ExecuteUpsert( | ||
ctx, queryBuilderFactory().WithCreateOperation(dbOp).WithUpdateBackup(backupToWrite), | ||
) | ||
|
||
if err != nil { | ||
return fmt.Errorf("can't create DeleteBackup operation: %v", err) | ||
} | ||
} | ||
} | ||
|
||
return db.UpdateOperation(ctx, operation) | ||
} |
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 handlers | ||
|
||
// TODO: add tests |
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
Oops, something went wrong.