aboutsummaryrefslogtreecommitdiff
path: root/weed/worker/tasks/vacuum/vacuum.go
blob: dbfe35cf80cfbcf95337e6dde5c9312cb3931a10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package vacuum

import (
	"fmt"
	"time"

	"github.com/seaweedfs/seaweedfs/weed/glog"
	"github.com/seaweedfs/seaweedfs/weed/worker/tasks"
	"github.com/seaweedfs/seaweedfs/weed/worker/types"
)

// Task implements vacuum operation to reclaim disk space
type Task struct {
	*tasks.BaseTask
	server   string
	volumeID uint32
}

// NewTask creates a new vacuum task instance
func NewTask(server string, volumeID uint32) *Task {
	task := &Task{
		BaseTask: tasks.NewBaseTask(types.TaskTypeVacuum),
		server:   server,
		volumeID: volumeID,
	}
	return task
}

// Execute executes the vacuum task
func (t *Task) Execute(params types.TaskParams) error {
	glog.Infof("Starting vacuum task for volume %d on server %s", t.volumeID, t.server)

	// Simulate vacuum operation with progress updates
	steps := []struct {
		name     string
		duration time.Duration
		progress float64
	}{
		{"Scanning volume", 1 * time.Second, 20},
		{"Identifying deleted files", 2 * time.Second, 50},
		{"Compacting data", 3 * time.Second, 80},
		{"Finalizing vacuum", 1 * time.Second, 100},
	}

	for _, step := range steps {
		if t.IsCancelled() {
			return fmt.Errorf("vacuum task cancelled")
		}

		glog.V(1).Infof("Vacuum task step: %s", step.name)
		t.SetProgress(step.progress)

		// Simulate work
		time.Sleep(step.duration)
	}

	glog.Infof("Vacuum task completed for volume %d on server %s", t.volumeID, t.server)
	return nil
}

// Validate validates the task parameters
func (t *Task) Validate(params types.TaskParams) error {
	if params.VolumeID == 0 {
		return fmt.Errorf("volume_id is required")
	}
	if params.Server == "" {
		return fmt.Errorf("server is required")
	}
	return nil
}

// EstimateTime estimates the time needed for the task
func (t *Task) EstimateTime(params types.TaskParams) time.Duration {
	// Base time for vacuum operation
	baseTime := 25 * time.Second

	// Could adjust based on volume size or usage patterns
	return baseTime
}