-
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.
- Loading branch information
1 parent
75bbe4e
commit 18eb5f7
Showing
6 changed files
with
306 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
table_types "github.com/ydb-platform/ydb-go-sdk/v3/table/types" | ||
"go.uber.org/zap" | ||
"ydbcp/internal/connectors/db" | ||
"ydbcp/internal/connectors/db/yql/queries" | ||
"ydbcp/internal/connectors/s3" | ||
"ydbcp/internal/types" | ||
"ydbcp/internal/util/xlog" | ||
) | ||
|
||
func NewDBOperationHandler( | ||
db db.DBConnector, s3 s3.S3Connector, getQueryBuilder func(ctx context.Context) queries.WriteTableQuery, | ||
) types.OperationHandler { | ||
return func(ctx context.Context, op types.Operation) error { | ||
return DBOperationHandler(ctx, op, db, s3, getQueryBuilder) | ||
} | ||
} | ||
|
||
func DBOperationHandler( | ||
ctx context.Context, | ||
operation types.Operation, | ||
db db.DBConnector, | ||
s3 s3.S3Connector, | ||
getQueryBuilder func(ctx context.Context) queries.WriteTableQuery, | ||
) error { | ||
xlog.Info( | ||
ctx, "received operation", | ||
zap.String("id", operation.GetID()), | ||
zap.String("type", string(operation.GetType())), | ||
zap.String("state", string(operation.GetState())), | ||
zap.String("message", operation.GetMessage()), | ||
) | ||
|
||
if operation.GetType() != types.OperationTypeDB { | ||
return fmt.Errorf( | ||
"wrong type %s != %s for operation %s", | ||
operation.GetType(), types.OperationTypeDB, types.OperationToString(operation), | ||
) | ||
} | ||
|
||
dbOp, ok := operation.(*types.DeleteBackupOperation) | ||
if !ok { | ||
return fmt.Errorf("can't cast operation to DeleteBackupOperation %s", types.OperationToString(operation)) | ||
} | ||
|
||
backups, err := db.SelectBackups( | ||
ctx, queries.NewReadTableQuery( | ||
queries.WithTableName("Backups"), | ||
queries.WithSelectFields(queries.AllBackupFields...), | ||
queries.WithQueryFilters( | ||
queries.QueryFilter{ | ||
Field: "id", | ||
Values: []table_types.Value{table_types.StringValueFromString(dbOp.BackupID)}, | ||
}, | ||
), | ||
), | ||
) | ||
|
||
if err != nil { | ||
return fmt.Errorf("can't select backups: %v", err) | ||
} | ||
|
||
if len(backups) == 0 { | ||
return fmt.Errorf("backup not found") | ||
} | ||
|
||
backupToWrite := types.Backup{ | ||
ID: dbOp.BackupID, | ||
Status: types.BackupStateUnknown, | ||
} | ||
|
||
deleteBackup := func(pathPrefix string, bucket string) error { | ||
objects, err := s3.ListObjects(pathPrefix, bucket) | ||
if err != nil { | ||
return fmt.Errorf("failed to list S3 objects: %v", err) | ||
} | ||
|
||
if len(objects) != 0 { | ||
err = s3.DeleteObjects(objects, bucket) | ||
if err != nil { | ||
return fmt.Errorf("failed to delete S3 objects: %v", err) | ||
} | ||
} | ||
|
||
operation.SetState(types.OperationStateDone) | ||
backupToWrite.Status = types.BackupStateDeleted | ||
return nil | ||
} | ||
|
||
switch dbOp.State { | ||
case types.OperationStatePending: | ||
{ | ||
operation.SetState(types.OperationStateRunning) | ||
err := db.UpdateOperation(ctx, operation) | ||
if err != nil { | ||
return fmt.Errorf("can't update operation: %v", err) | ||
} | ||
|
||
err = deleteBackup(backups[0].S3PathPrefix, backups[0].S3Bucket) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
case types.OperationStateRunning: | ||
{ | ||
err = deleteBackup(backups[0].S3PathPrefix, backups[0].S3Bucket) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
default: | ||
return fmt.Errorf("unexpected operation state %s", dbOp.State) | ||
} | ||
|
||
return db.ExecuteUpsert( | ||
ctx, getQueryBuilder(ctx).WithUpdateOperation(operation).WithUpdateBackup(backupToWrite), | ||
) | ||
} |
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