aboutsummaryrefslogtreecommitdiff
path: root/weed/worker/tasks/erasure_coding/ec.go
blob: 641dfc6b5d614a56c2a4a8cbc980482754ebc391 (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 erasure_coding

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 erasure coding operation to convert volumes to EC format
type Task struct {
	*tasks.BaseTask
	server   string
	volumeID uint32
}

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

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

	// Simulate erasure coding operation with progress updates
	steps := []struct {
		name     string
		duration time.Duration
		progress float64
	}{
		{"Analyzing volume", 2 * time.Second, 15},
		{"Creating EC shards", 5 * time.Second, 50},
		{"Verifying shards", 2 * time.Second, 75},
		{"Finalizing EC volume", 1 * time.Second, 100},
	}

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

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

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

	glog.Infof("Erasure coding 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 erasure coding operation
	baseTime := 30 * time.Second

	// Could adjust based on volume size or other factors
	return baseTime
}