/
lifecycle_sched.go
408 lines (364 loc) · 13.3 KB
/
lifecycle_sched.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Copyright 2019 The OpenSDS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package lifecycle
import (
"encoding/json"
"fmt"
"sort"
"strconv"
"sync"
"time"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/metadata"
"github.com/opensds/multi-cloud/api/pkg/common"
"github.com/opensds/multi-cloud/api/pkg/utils/constants"
"github.com/opensds/multi-cloud/dataflow/pkg/db"
"github.com/opensds/multi-cloud/dataflow/pkg/kafka"
. "github.com/opensds/multi-cloud/dataflow/pkg/model"
. "github.com/opensds/multi-cloud/dataflow/pkg/utils"
"github.com/opensds/multi-cloud/datamover/proto"
"github.com/opensds/multi-cloud/s3/error"
s3utils "github.com/opensds/multi-cloud/s3/pkg/utils"
"github.com/opensds/multi-cloud/s3/proto"
osdss3 "github.com/opensds/multi-cloud/s3/proto"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
)
var topicLifecycle = "lifecycle"
var s3client = osdss3.NewS3Service("s3", client.DefaultClient)
const TIME_LAYOUT_TIDB = "2006-01-02 15:04:05"
type InterRules []*InternalLifecycleRule
// map from a specific tier to an array of tiers, that means transition can happens from the specific tier to those tiers in the array.
var TransitionMap map[string]struct{}
var mutext sync.Mutex
func loadStorageClassDefinition() error {
res, _ := s3client.GetTierMap(context.Background(), &s3.BaseRequest{})
if len(res.Transition) == 0 {
log.Info("get transition map failed")
return fmt.Errorf("get tier definition failed")
} else {
log.Infof("res.Transition:%v, res.Tier2Name:%+v", res.Transition, res.Tier2Name)
}
TransitionMap = make(map[string]struct{})
for _, v := range res.Transition {
TransitionMap[v] = struct{}{}
}
return nil
}
// Get liecycle rules for each bucket from db, and schedule according to those rules.
func ScheduleLifecycle() {
log.Info("[ScheduleLifecycle] begin ...")
// Load transition map.
{
mutext.Lock()
defer mutext.Unlock()
if len(TransitionMap) == 0 {
err := loadStorageClassDefinition()
if err != nil {
log.Errorf("[ScheduleLifecycle]load storage classes failed: %v.\n", err)
return
}
}
}
// List buckets with lifecycle configured.
ctx := metadata.NewContext(context.Background(), map[string]string{common.CTX_KEY_IS_ADMIN: strconv.FormatBool(true)})
listRsp, err := s3client.ListBucketLifecycle(ctx, &s3.BaseRequest{})
if err != nil {
log.Errorf("[ScheduleLifecycle]list buckets failed: %v.\n", err)
return
}
for _, v := range listRsp.Buckets {
// For each bucket, get the lifecycle rules, and schedule each rule.
if v.LifecycleConfiguration == nil {
log.Infof("[ScheduleLifecycle]bucket[%s] has no lifecycle rule.\n", v.Name)
continue
}
log.Infof("[ScheduleLifecycle]bucket[%s] has lifecycle rule.\n", v.Name)
err = handleBucketLifecyle(v.Name, v.LifecycleConfiguration)
if err != nil {
log.Errorf("[ScheduleLifecycle]handle bucket lifecycle for bucket[%s] failed, err:%v.\n", v.Name, err)
continue
}
}
log.Info("[ScheduleLifecycle] end ...")
}
// Need to lock the bucket, incase the schedule period is too short and the bucket is scheduled at the same time.
// Need to consider confliction between rules.
func handleBucketLifecyle(bucket string, rules []*osdss3.LifecycleRule) error {
// Translate rules set by user to internal rules which can be sorted.
// Lifecycle scheduling must be mutual excluded among several schedulers, so get lock first.
ret := db.DbAdapter.LockBucketLifecycleSched(bucket)
for i := 0; i < 3; i++ {
if ret == LockSuccess {
break
} else if ret == LockBusy {
return fmt.Errorf("lifecycle scheduling of bucket[%s] is in progress", bucket)
} else {
// Try to lock again, try three times at most
ret = db.DbAdapter.LockBucketLifecycleSched(bucket)
}
}
if ret != LockSuccess {
log.Errorf("lock scheduling failed.\n")
return fmt.Errorf("internal error: lock failed")
}
// Make sure unlock before return
defer db.DbAdapter.UnlockBucketLifecycleSched(bucket)
var inRules, abortRules InterRules
for _, rule := range rules {
if rule.Status == RuleStatusDisabled {
continue
}
// actions
for _, ac := range rule.Actions {
var acType int
if ac.Name == ActionNameExpiration {
// Expiration
acType = ActionExpiration
} else if ac.Backend == "" {
acType = ActionIncloudTransition
} else {
acType = ActionCrosscloudTransition
}
v := InternalLifecycleRule{Bucket: bucket, Days: ac.GetDays(), ActionType: acType, Tier: ac.GetTier(), Backend: ac.GetBackend()}
if rule.GetFilter() != nil {
v.Filter = InternalLifecycleFilter{Prefix: rule.Filter.Prefix}
}
inRules = append(inRules, &v)
}
if rule.AbortIncompleteMultipartUpload.DaysAfterInitiation > 0 {
// abort incomplete multipart uploads
abortRule := InternalLifecycleRule{Bucket: bucket, Days: rule.AbortIncompleteMultipartUpload.DaysAfterInitiation, ActionType: AbortIncompleteMultipartUpload}
if rule.GetFilter() != nil {
abortRule.Filter = InternalLifecycleFilter{Prefix: rule.Filter.Prefix}
}
abortRules = append(abortRules, &abortRule)
}
}
// Sort rules, in case different actions exist for an object at the same time, for example, expiration after 30 days
// and transition to azure after 60 days, we need to make sure only one action will be taken, and that needs the
// sorting be stable.
sort.Stable(inRules)
// Begin: Log for debug
for _, v := range inRules {
log.Debugf("action rule: %+v\n", *v)
}
// End: Log for debug
schedSortedActionsRules(&inRules)
sort.Stable(abortRules)
// Begin: Log for debug
for _, v := range abortRules {
log.Debugf("abort rule: %+v\n", *v)
}
// End: Log for debug
schedSortedAbortRules(&abortRules)
return nil
}
func checkTransitionValidation(source int32, destination int32) bool {
key := fmt.Sprintf("%d:%d", source, destination)
if _, ok := TransitionMap[key]; !ok {
return false
}
return true
}
func getObjects(r *InternalLifecycleRule, marker string, limit int32) ([]*osdss3.Object, error) {
// Get objects by communicating with s3 service.
filt := make(map[string]string)
timeFilt := fmt.Sprintf("{\"lte\":\"%s\"}",
time.Now().AddDate(0, 0, int(0-r.Days)).Format(TIME_LAYOUT_TIDB))
filt[KLastModified] = timeFilt
if r.ActionType != ActionExpiration {
filt[KStorageTier] = strconv.Itoa(int(r.Tier))
}
log.Infof("The filter: %+v\n", filt)
s3req := osdss3.ListObjectsRequest{
Version: constants.ListObjectsType2Int,
Bucket: r.Bucket,
Filter: filt,
StartAfter: marker,
MaxKeys: limit,
}
if len(r.Filter.Prefix) > 0 {
s3req.Prefix = r.Filter.Prefix
}
ctx := metadata.NewContext(context.Background(), map[string]string{common.CTX_KEY_IS_ADMIN: strconv.FormatBool(true)})
log.Debugf("ListObjectsRequest:%+v\n", s3req)
s3rsp, err := s3client.ListObjects(ctx, &s3req)
if err != nil || s3rsp.ErrorCode != int32(s3error.ErrNoErr) {
log.Errorf("list objects failed, req:%+v, err:%v.\n", s3req, err)
return nil, err
}
return s3rsp.Objects, nil
}
func schedSortedAbortRules(inRules *InterRules) {
log.Debugln("schedSortedAbortRules begin ...")
ctx := metadata.NewContext(context.Background(), map[string]string{common.CTX_KEY_IS_ADMIN: strconv.FormatBool(true)})
for _, r := range *inRules {
var uploadIdMarker = ""
for {
log.Debugf("list upload reqest, bucket:%s, UploadIdMarker:%s\n", r.Bucket, uploadIdMarker)
req := osdss3.ListBucketUploadRequest{BucketName: r.Bucket, MaxUploads: 1000, UploadIdMarker: uploadIdMarker}
s3rsp, err := s3client.ListBucketUploadRecords(ctx, &req)
if err != nil {
log.Errorf("schedule for rule[id=%s,bucket=%s] failed, err:%v\n", r.Id, r.Bucket, err)
break
}
records := s3rsp.Result.Uploads
if len(records) == 0 {
log.Debugln("break because no upload record exist")
break
}
for _, rc := range records {
log.Debugf("abort upload part, ObjKey=%s, UploadId=%s\n", rc.Key, rc.UploadId)
req := datamover.LifecycleActionRequest{
ObjKey: rc.Key,
BucketName: r.Bucket,
UploadId: rc.UploadId,
Action: AbortIncompleteMultipartUpload,
}
// If send failed, then ignore it, because it will be re-sent in the next schedule round.
sendActionRequest(&req)
}
if s3rsp.Result.IsTruncated == false {
log.Debugln("break because result is truncated")
break
}
uploadIdMarker = s3rsp.Result.NextUploadIdMarker
}
}
log.Debugln("schedSortedAbortRules end ...")
}
func schedSortedActionsRules(inRules *InterRules) {
log.Info("schedSortedActionsRules begin ...")
dupCheck := map[string]struct{}{}
for _, r := range *inRules {
log.Debugf("rule: %v\n", r)
var marker string
var limit int32 = 1000
for {
objs, err := getObjects(r, marker, limit)
if err != nil {
break
}
log.Debugf("objects count: %d\n", len(objs))
for _, obj := range objs {
log.Debugf("obj: %v\n", obj)
if obj.DeleteMarker == true {
log.Infof("deleteMarker of object[%s] is set, no lifecycle action need.\n", obj.ObjectKey)
continue
}
if r.ActionType != ActionExpiration && obj.Tier == s3utils.Tier999 {
// archived object cannot be transit
log.Infof("object[%s] is already archived.\n", obj.ObjectKey)
continue
}
// Check if the object exist in the dupCheck map.
if _, ok := dupCheck[obj.ObjectKey]; !ok {
// Not exist means this object has not processed in this round of scheduling.
if r.ActionType != ActionExpiration && obj.Tier == r.Tier &&
(obj.Location == r.Backend || r.Backend == "") {
// For transition, if target backend and storage class is the same as source backend and storage
// class, then no transition is need.
log.Infof("no need transition for object[%s], backend=%s, tier=%d\n", obj.ObjectKey, r.Backend, r.Tier)
// in case different actions exist for an object at the same time, for example transition to aws after 30 days
// and transition to azure after 30 days, we need to make sure only one action will be taken.
dupCheck[obj.ObjectKey] = struct{}{}
continue
}
//Send request.
var action int32
if r.ActionType == ActionExpiration {
action = int32(ActionExpiration)
} else if r.Backend == "" || obj.Location == r.Backend {
r.Backend = obj.Location
action = int32(ActionIncloudTransition)
} else {
action = int32(ActionCrosscloudTransition)
}
if r.ActionType != ActionExpiration && checkTransitionValidation(obj.Tier, r.Tier) != true {
log.Infof("transition object[%s] from tier[%d] to tier[%d] is invalid.\n", obj.ObjectKey, obj.Tier, r.Tier)
// in case different actions exist for an object at the same time, for example transition to aws after 30 days
// and transition to azure after 30 days, we need to make sure only one action will be taken.
dupCheck[obj.ObjectKey] = struct{}{}
continue
}
log.Infof("lifecycle action: object=[%s] type=[%d] source-tier=[%d] target-tier=[%d] "+
"source-backend=[%s] target-backend=[%s].\n", obj.ObjectKey, r.ActionType, obj.Tier, r.Tier,
obj.Location, r.Backend)
acreq := datamover.LifecycleActionRequest{
ObjKey: obj.ObjectKey,
BucketName: obj.BucketName,
Action: action,
SourceTier: obj.Tier,
TargetTier: r.Tier,
SourceBackend: obj.Location,
TargetBackend: r.Backend,
ObjSize: obj.Size,
VersionId: obj.VersionId,
StorageMeta: obj.StorageMeta,
ObjectId: obj.ObjectId,
}
// If send failed, then ignore it, because it will be re-sent in the next schedule period.
sendActionRequest(&acreq)
// Add object key to dupCheck so it will not be processed repeatedly in this round or scheduling.
dupCheck[obj.ObjectKey] = struct{}{}
} else {
log.Infof("object[%s] is already handled in this schedule time.\n", obj.ObjectKey)
}
marker = obj.ObjectKey
}
if int32(len(objs)) < limit {
break
}
}
}
log.Info("schedSortedActionsRules end ...")
}
func sendActionRequest(req *datamover.LifecycleActionRequest) error {
log.Infof("Send lifecycle request to datamover: %v\n", req)
data, err := json.Marshal(*req)
if err != nil {
log.Errorf("marshal run job request failed, err:%v\n", data)
return err
}
return kafka.ProduceMsg(topicLifecycle, data)
}
func (r InterRules) Len() int {
return len(r)
}
/*
Less reports whether the element with index i should sort before the element with index j.
Expiration action has higher priority than transition action.
For the same action type, bigger Days has higher priority.
*/
func (r InterRules) Less(i, j int) bool {
var ret bool
if r[i].ActionType == ActionExpiration && r[i].ActionType < r[j].ActionType {
ret = true
} else if r[i].ActionType > r[j].ActionType && r[j].ActionType == ActionExpiration {
ret = false
} else {
if r[i].Days >= r[j].Days {
ret = true
} else {
ret = false
}
}
return ret
}
func (r InterRules) Swap(i, j int) {
tmp := r[i]
r[i] = r[j]
r[j] = tmp
}