Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip]fix(region): delete overdued snapshots on cleanup snapshot task only #21948

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions pkg/compute/models/disks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2744,7 +2744,6 @@ func (manager *SDiskManager) AutoDiskSnapshot(ctx context.Context, userCred mccl
return
}
log.Debugf("auto snapshot %d disks", len(disks))
now := time.Now()
for i := 0; i < len(disks); i++ {
disk, err := disks[i].GetDisk()
if err != nil {
Expand All @@ -2770,9 +2769,6 @@ func (manager *SDiskManager) AutoDiskSnapshot(ctx context.Context, userCred mccl
return errors.Wrapf(err, "CreateSnapshotAuto")
}

if err = disk.CleanOverduedSnapshots(ctx, userCred, policy, now); err != nil {
log.Errorf("failed clean overdued snapshots %s", err)
}
db.OpsLog.LogEvent(disk, db.ACT_DISK_AUTO_SNAPSHOT, snapshot.Name, userCred)
policy.ExecuteNotify(ctx, userCred, disk.GetName())
return nil
Expand Down
11 changes: 11 additions & 0 deletions pkg/compute/models/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"database/sql"
"fmt"
"sync/atomic"
"time"

"yunion.io/x/cloudmux/pkg/cloudprovider"
Expand Down Expand Up @@ -1154,7 +1155,17 @@ func (manager *SSnapshotManager) GetResourceCount() ([]db.SScopeResourceCount, e
return db.CalculateResourceCount(virts, "tenant_id")
}

var SnapshotCleanupTaskRunning int32 = 0

func SnapshotCleanupTaskIsRunning() bool {
return atomic.LoadInt32(&SnapshotCleanupTaskRunning) == 1
}

func (manager *SSnapshotManager) CleanupSnapshots(ctx context.Context, userCred mcclient.TokenCredential, isStart bool) {
if SnapshotCleanupTaskIsRunning() {
log.Errorf("Previous CleanupSnapshots tasks still running !!!")
return
}
var now = time.Now()
var snapshot = new(SSnapshot)
err := manager.Query().
Expand Down
28 changes: 21 additions & 7 deletions pkg/compute/tasks/disk_clean_overdued_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"database/sql"
"fmt"
"sync/atomic"

"yunion.io/x/jsonutils"
"yunion.io/x/log"
Expand All @@ -40,10 +41,23 @@ type SnapshotCleanupTask struct {
taskman.STask
}

func (self *SnapshotCleanupTask) taskFailed(ctx context.Context, reason *jsonutils.JSONString) {
log.Infof("SnapshotCleanupTask failed %s", reason)
atomic.CompareAndSwapInt32(&models.SnapshotCleanupTaskRunning, 1, 0)
self.SetStageFailed(ctx, reason)
}

func (self *SnapshotCleanupTask) taskCompleted(ctx context.Context, data jsonutils.JSONObject) {
log.Infof("SnapshotCleanupTask completed %s", data)
atomic.CompareAndSwapInt32(&models.SnapshotCleanupTaskRunning, 1, 0)
self.SetStageComplete(ctx, nil)
}

func (self *SnapshotCleanupTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
atomic.CompareAndSwapInt32(&models.SnapshotCleanupTaskRunning, 0, 1)
now, err := self.Params.GetTime("tick")
if err != nil {
self.SetStageFailed(ctx, jsonutils.NewString("failed get tick"))
self.taskFailed(ctx, jsonutils.NewString("failed get tick"))
return
}
var snapshots = make([]models.SSnapshot, 0)
Expand All @@ -52,10 +66,10 @@ func (self *SnapshotCleanupTask) OnInit(ctx context.Context, obj db.IStandaloneM
Equals("created_by", compute.SNAPSHOT_AUTO).
LE("expired_at", now).All(&snapshots)
if err == sql.ErrNoRows {
self.SetStageComplete(ctx, nil)
self.taskCompleted(ctx, nil)
return
} else if err != nil {
self.SetStageFailed(ctx, jsonutils.NewString(fmt.Sprintf("failed get snapshot %s", err)))
self.taskFailed(ctx, jsonutils.NewString(fmt.Sprintf("failed get snapshot %s", err)))
return
}
self.StartSnapshotsDelete(ctx, snapshots)
Expand Down Expand Up @@ -84,17 +98,17 @@ func (self *SnapshotCleanupTask) OnDeleteSnapshot(ctx context.Context, obj db.IS
var snapshots = make([]models.SSnapshot, 0)
err := self.Params.Unmarshal(&snapshots, "snapshots")
if err != nil {
self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
self.taskFailed(ctx, jsonutils.NewString(err.Error()))
return
}
if len(snapshots) > 0 {
self.StartSnapshotsDelete(ctx, snapshots)
} else {
self.SetStageComplete(ctx, nil)
self.taskCompleted(ctx, nil)
}
}

func (self *SnapshotCleanupTask) OnDeleteSnapshotFailed(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
log.Errorf("snapshot delete faield %s", data)
self.SetStageFailed(ctx, data)
reason := fmt.Sprintf("snapshot delete faield %s", data)
self.taskFailed(ctx, jsonutils.NewString(reason))
}
Loading