Skip to content

Commit

Permalink
fix(OpaerationService): set DELETING backup status in DeleteBackup
Browse files Browse the repository at this point in the history
  • Loading branch information
ulya-sidorina committed Oct 3, 2024
1 parent 6d7d6c4 commit 4b8d8ab
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
38 changes: 38 additions & 0 deletions cmd/integration/make_backup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,44 @@ func main() {
if !done {
log.Panicln("failed to complete a restore in 30 seconds")
}
deleteOperation, err := client.DeleteBackup(
context.Background(), &pb.DeleteBackupRequest{
BackupId: backupOperation.BackupId,
},
)
if err != nil {
log.Panicf("failed to delete backup: %v", err)
}
done = false
for range 30 {
op, err := opClient.GetOperation(
context.Background(), &pb.GetOperationRequest{
Id: deleteOperation.Id,
},
)
if err != nil {
log.Panicf("failed to get operation: %v", err)
}
if op.GetStatus().String() == types.OperationStateDone.String() {
done = true
break
}
time.Sleep(time.Second)
}
if !done {
log.Panicln("failed to complete a delete backup in 30 seconds")
}
backup, err := client.GetBackup(
context.Background(),
&pb.GetBackupRequest{Id: backupOperation.BackupId},
)
if err != nil {
log.Panicf("failed to get backup: %v", err)
}
if backup.GetStatus().String() != types.BackupStateDeleted {
log.Panicf("expected DELETED backup status^ but received: %s", backup.GetStatus().String())
}

scheduleClient := pb.NewBackupScheduleServiceClient(conn)
schedules, err := scheduleClient.ListBackupSchedules(
context.Background(), &pb.ListBackupSchedulesRequest{
Expand Down
9 changes: 5 additions & 4 deletions internal/handlers/delete_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func DBOperationHandler(
return fmt.Errorf("backup not found")
}

if backups[0].Status != types.BackupStateDeleting {
return fmt.Errorf("unexpected backup status: %s", backups[0].Status)
}

deleteBackup := func(pathPrefix string, bucket string) error {
objects, err := s3.ListObjects(pathPrefix, bucket)
if err != nil {
Expand All @@ -107,11 +111,8 @@ func DBOperationHandler(
switch dbOp.State {
case types.OperationStatePending:
{
backupToWrite.Status = types.BackupStateDeleting
operation.SetState(types.OperationStateRunning)
err := db.ExecuteUpsert(
ctx, queryBulderFactory().WithUpdateOperation(operation).WithUpdateBackup(backupToWrite),
)
err := db.UpdateOperation(ctx, operation)
if err != nil {
return fmt.Errorf("can't update operation: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/handlers/delete_backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestDBOperationHandlerDeadlineExceededForRunningOperation(t *testing.T) {
}
backup := types.Backup{
ID: backupID,
Status: types.BackupStateAvailable,
Status: types.BackupStateDeleting,
}

opMap := make(map[string]types.Operation)
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestDBOperationHandlerPendingOperationCompletedSuccessfully(t *testing.T) {
}
backup := types.Backup{
ID: backupID,
Status: types.BackupStateAvailable,
Status: types.BackupStateDeleting,
S3PathPrefix: "pathPrefix",
}

Expand Down Expand Up @@ -162,7 +162,7 @@ func TestDBOperationHandlerRunningOperationCompletedSuccessfully(t *testing.T) {
}
backup := types.Backup{
ID: backupID,
Status: types.BackupStateAvailable,
Status: types.BackupStateDeleting,
S3PathPrefix: "pathPrefix",
}

Expand Down
12 changes: 9 additions & 3 deletions internal/server/services/backup/backup_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func (s *BackupService) DeleteBackup(ctx context.Context, req *pb.DeleteBackupRe

now := timestamppb.Now()
op := &types.DeleteBackupOperation{
ID: types.GenerateObjectID(),
ContainerID: backup.ContainerID,
BackupID: req.GetBackupId(),
State: types.OperationStatePending,
Expand All @@ -166,14 +167,19 @@ func (s *BackupService) DeleteBackup(ctx context.Context, req *pb.DeleteBackupRe
UpdatedAt: now,
}

operationID, err := s.driver.CreateOperation(ctx, op)
backupToWrite := types.Backup{
ID: req.GetBackupId(),
Status: types.BackupStateDeleting,
}

err = s.driver.ExecuteUpsert(
ctx, queries.NewWriteTableQuery().WithCreateOperation(op).WithUpdateBackup(backupToWrite),
)
if err != nil {
xlog.Error(ctx, "can't create operation", zap.Error(err))
return nil, status.Error(codes.Internal, "can't create operation")
}
ctx = xlog.With(ctx, zap.String("OperationID", operationID))

op.ID = operationID
xlog.Debug(ctx, "DeleteBackup was started successfully", zap.String("operation", types.OperationToString(op)))
return op.Proto(), nil
}
Expand Down

0 comments on commit 4b8d8ab

Please sign in to comment.