|
| 1 | +/******************************************************************************* |
| 2 | +* |
| 3 | +* Copyright 2018 SAP SE |
| 4 | +* |
| 5 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +* you may not use this file except in compliance with the License. |
| 7 | +* You should have received a copy of the License along with this |
| 8 | +* program. If not, you may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +* |
| 18 | +*******************************************************************************/ |
| 19 | + |
| 20 | +package actors |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "sort" |
| 25 | + |
| 26 | + "github.com/majewsky/schwift" |
| 27 | + "github.com/sapcc/swift-http-import/pkg/objects" |
| 28 | + "github.com/sapcc/swift-http-import/pkg/util" |
| 29 | +) |
| 30 | + |
| 31 | +//FileInfoForCleaner contains information about a transferred file for the Cleaner actor. |
| 32 | +type FileInfoForCleaner struct { |
| 33 | + objects.File |
| 34 | + Failed bool |
| 35 | +} |
| 36 | + |
| 37 | +//Cleaner is an actor that cleans up unknown objects on the target side (i.e. |
| 38 | +//those objects which do not exist on the source side). |
| 39 | +type Cleaner struct { |
| 40 | + Context context.Context |
| 41 | + Input <-chan FileInfoForCleaner |
| 42 | + Report chan<- ReportEvent |
| 43 | +} |
| 44 | + |
| 45 | +//Run implements the Actor interface. |
| 46 | +func (c *Cleaner) Run() { |
| 47 | + isJobFailed := make(map[*objects.Job]bool) |
| 48 | + isFileTransferred := make(map[*objects.Job]map[string]bool) //string = object name incl. prefix (if any) |
| 49 | + |
| 50 | + //collect information about transferred files from the transferors |
| 51 | + //(we don't need to check Context.Done in the loop; when the process is |
| 52 | + //interrupted, main() will close our Input and we will move on) |
| 53 | + for info := range c.Input { |
| 54 | + //ignore all files in jobs where no cleanup is configured |
| 55 | + job := info.File.Job |
| 56 | + if job.Cleanup.Strategy == objects.KeepUnknownFiles { |
| 57 | + continue |
| 58 | + } |
| 59 | + |
| 60 | + if info.Failed { |
| 61 | + isJobFailed[job] = true |
| 62 | + } |
| 63 | + |
| 64 | + m, exists := isFileTransferred[job] |
| 65 | + if !exists { |
| 66 | + m = make(map[string]bool) |
| 67 | + isFileTransferred[job] = m |
| 68 | + } |
| 69 | + m[info.File.TargetObject().Name()] = true |
| 70 | + } |
| 71 | + if c.Context.Err() != nil { |
| 72 | + util.Log(util.LogInfo, "skipping cleanup phase: interrupt was received") |
| 73 | + return |
| 74 | + } |
| 75 | + if len(isJobFailed) > 0 { |
| 76 | + util.Log(util.LogInfo, |
| 77 | + "skipping cleanup phase for %d job(s) because of failed file transfers", |
| 78 | + len(isJobFailed)) |
| 79 | + } |
| 80 | + |
| 81 | + //perform cleanup if it is safe to do so |
| 82 | + for job, transferred := range isFileTransferred { |
| 83 | + if c.Context.Err() != nil { |
| 84 | + //interrupt received |
| 85 | + return |
| 86 | + } |
| 87 | + if !isJobFailed[job] { |
| 88 | + c.performCleanup(job, transferred) |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func (c *Cleaner) performCleanup(job *objects.Job, isFileTransferred map[string]bool) { |
| 94 | + //collect objects to cleanup |
| 95 | + var objs []*schwift.Object |
| 96 | + for objectName := range job.Target.FileExists { |
| 97 | + if isFileTransferred[objectName] { |
| 98 | + continue |
| 99 | + } |
| 100 | + objs = append(objs, job.Target.Container.Object(objectName)) |
| 101 | + } |
| 102 | + sort.Slice(objs, func(i, j int) bool { |
| 103 | + return objs[i].Name() < objs[j].Name() |
| 104 | + }) |
| 105 | + |
| 106 | + //perform cleanup according to selected strategy |
| 107 | + switch job.Cleanup.Strategy { |
| 108 | + case objects.ReportUnknownFiles: |
| 109 | + for _, obj := range objs { |
| 110 | + util.Log(util.LogInfo, "found unknown object on target side: %s", obj.FullName()) |
| 111 | + } |
| 112 | + |
| 113 | + case objects.DeleteUnknownFiles: |
| 114 | + numDeleted, _, err := job.Target.Container.Account().BulkDelete(objs, nil, nil) |
| 115 | + c.Report <- ReportEvent{IsCleanup: true, CleanedUpObjectCount: int64(numDeleted)} |
| 116 | + if err != nil { |
| 117 | + util.Log(util.LogError, "cleanup of %d objects on target side failed: %s", len(objs), err.Error()) |
| 118 | + if berr, ok := err.(schwift.BulkError); ok { |
| 119 | + for _, oerr := range berr.ObjectErrors { |
| 120 | + util.Log(util.LogError, "DELETE "+oerr.Error()) |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments