-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathcollect_snapshots_task.go
179 lines (144 loc) · 4.26 KB
/
collect_snapshots_task.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package dataplane
import (
"context"
"fmt"
"time"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/empty"
"github.com/ydb-platform/nbs/cloud/disk_manager/internal/pkg/dataplane/protos"
"github.com/ydb-platform/nbs/cloud/disk_manager/internal/pkg/dataplane/snapshot/storage"
storage_protos "github.com/ydb-platform/nbs/cloud/disk_manager/internal/pkg/dataplane/snapshot/storage/protos"
"github.com/ydb-platform/nbs/cloud/tasks"
"github.com/ydb-platform/nbs/cloud/tasks/errors"
"github.com/ydb-platform/nbs/cloud/tasks/headers"
)
////////////////////////////////////////////////////////////////////////////////
type collectSnapshotsTask struct {
scheduler tasks.Scheduler
storage storage.Storage
snapshotCollectionTimeout time.Duration
snapshotCollectionInflightLimit int
state *protos.CollectSnapshotsTaskState
}
func (t *collectSnapshotsTask) Save() ([]byte, error) {
return proto.Marshal(t.state)
}
func (t *collectSnapshotsTask) Load(_, state []byte) error {
t.state = &protos.CollectSnapshotsTaskState{}
return proto.Unmarshal(state, t.state)
}
func (t *collectSnapshotsTask) Run(
ctx context.Context,
execCtx tasks.ExecutionContext,
) error {
selfTaskID := execCtx.GetTaskID()
snapshotIDs := make(map[string]bool)
for _, snapshot := range t.state.Snapshots {
snapshotIDs[snapshot.SnapshotId] = true
}
var taskIDs []string
taskIDToSnapshot := make(map[string]*storage_protos.DeletingSnapshotKey)
for {
deletingBefore := time.Now().Add(-t.snapshotCollectionTimeout)
var snapshotsToScheduleDeletionFor []*storage_protos.DeletingSnapshotKey
if len(taskIDs) == 0 {
snapshotsToScheduleDeletionFor = t.state.Snapshots
}
inflightLimit := t.snapshotCollectionInflightLimit
if len(t.state.Snapshots) < inflightLimit {
snapshots, err := t.storage.GetSnapshotsToDelete(
ctx,
deletingBefore,
inflightLimit,
)
if err != nil {
return err
}
for _, snapshot := range snapshots {
if !snapshotIDs[snapshot.SnapshotId] {
snapshotIDs[snapshot.SnapshotId] = true
t.state.Snapshots = append(t.state.Snapshots, snapshot)
snapshotsToScheduleDeletionFor = append(
snapshotsToScheduleDeletionFor,
snapshot,
)
}
}
err = execCtx.SaveState(ctx)
if err != nil {
return err
}
}
for _, snapshot := range snapshotsToScheduleDeletionFor {
idempotencyKey := fmt.Sprintf(
"%v_%v",
selfTaskID,
snapshot.SnapshotId,
)
taskID, err := t.scheduler.ScheduleTask(
headers.SetIncomingIdempotencyKey(ctx, idempotencyKey),
"dataplane.DeleteSnapshotData",
"",
&protos.DeleteSnapshotDataRequest{
SnapshotId: snapshot.SnapshotId,
},
)
if err != nil {
return err
}
taskIDToSnapshot[taskID] = snapshot
taskIDs = append(taskIDs, taskID)
}
if len(taskIDs) == 0 {
// Nothing to collect.
return errors.NewInterruptExecutionError()
}
finishedTaskIDs, err := t.scheduler.WaitAnyTasks(ctx, taskIDs)
if err != nil {
return err
}
deletedSnapshotIDs := make(map[string]bool)
var deletedSnapshots []*storage_protos.DeletingSnapshotKey
for _, taskID := range finishedTaskIDs {
snapshot := taskIDToSnapshot[taskID]
delete(taskIDToSnapshot, taskID)
deletedSnapshotIDs[snapshot.SnapshotId] = true
deletedSnapshots = append(deletedSnapshots, snapshot)
}
taskIDs = nil
for taskID := range taskIDToSnapshot {
taskIDs = append(taskIDs, taskID)
}
err = t.storage.ClearDeletingSnapshots(ctx, deletedSnapshots)
if err != nil {
return err
}
snapshots := t.state.Snapshots
t.state.Snapshots = nil
snapshotIDs = make(map[string]bool)
for _, snapshot := range snapshots {
if !deletedSnapshotIDs[snapshot.SnapshotId] {
snapshotIDs[snapshot.SnapshotId] = true
t.state.Snapshots = append(t.state.Snapshots, snapshot)
}
}
err = execCtx.SaveState(ctx)
if err != nil {
return err
}
}
}
func (t *collectSnapshotsTask) Cancel(
ctx context.Context,
execCtx tasks.ExecutionContext,
) error {
return nil
}
func (t *collectSnapshotsTask) GetMetadata(
ctx context.Context,
) (proto.Message, error) {
return &empty.Empty{}, nil
}
func (t *collectSnapshotsTask) GetResponse() proto.Message {
return &empty.Empty{}
}