-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
159 lines (142 loc) · 3.19 KB
/
job.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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"sync"
"time"
)
func bytes(b int64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB",
float64(b)/float64(div), "KMGTPE"[exp])
}
type jobStats struct {
sync.RWMutex
Size int64
Num int
}
func (js *jobStats) GetSize() int64 {
js.RLock()
defer js.RUnlock()
return js.Size
}
func (js *jobStats) Add(size int64) {
js.Lock()
defer js.Unlock()
js.Num++
js.Size += size
}
func (js *jobStats) GetNum() int {
js.RLock()
defer js.RUnlock()
return js.Num
}
type job struct {
source *Bucket
destination *Bucket
copyList []string
copyTotalSize int64
copyChan chan string
stats jobStats
}
func (j *job) copyObject(key string) {
tmpfile, err := ioutil.TempFile("", "lopata_tmp")
defer os.Remove(tmpfile.Name())
if err != nil {
log.Fatal("Failed to create tmp file: ", err)
}
_, err = j.source.DownloadObject(key, tmpfile)
if err != nil {
log.Fatal("Failed to download object ", key, ": ", err)
}
destKey := path.Join(j.destination.Prefix, key)
err = j.destination.UploadObject(destKey, tmpfile)
if err != nil {
log.Fatal("Failed to upload object ", destKey, ": ", err)
}
j.stats.Add(*j.source.Objects[key].Size)
}
func (j *job) runConsumers(wg *sync.WaitGroup) {
for i := 0; i < cap(j.copyChan); i++ {
wg.Add(1)
go func() {
for k := range j.copyChan {
j.copyObject(k)
}
wg.Done()
}()
}
}
func (j *job) copy() {
var wg = sync.WaitGroup{}
j.runConsumers(&wg)
for _, v := range j.copyList {
j.copyChan <- v
}
close(j.copyChan)
wg.Wait()
j.status()
}
func (j *job) prepare() {
j.collectKeys()
// copy all files that are either nonexistent
// or whose size differs
for k, o := range j.source.Objects {
destKey := path.Join(j.destination.Prefix, k)
dest, found := j.destination.Objects[destKey]
if !found || *o.Size != *dest.Size {
j.copyList = append(j.copyList, k)
j.copyTotalSize += *o.Size
}
}
}
func (j *job) collectKeys() {
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
err := j.source.CollectKeys()
if err != nil {
log.Fatal("Failed to list source bucket objects: ", err)
}
log.Println(j.name(), "source: found", j.source.TotalCount, "objects (", bytes(j.source.TotalSize), ")")
}()
wg.Add(1)
go func() {
defer wg.Done()
err := j.destination.CollectKeys()
if err != nil {
log.Fatal("Failed to list destination bucket objects: ", err)
}
log.Println(j.name(), "destination: found", j.destination.TotalCount, "objects (", bytes(j.destination.TotalSize), ")")
}()
wg.Wait()
}
func (j *job) name() string {
return fmt.Sprintf("[%s->%s]", j.source.Name, j.destination.Name)
}
func (j *job) status() {
log.Println(j.name(), "status update: copied", j.stats.GetNum(), "objects (", bytes(j.stats.GetSize()), ")")
}
func (j *job) Start() {
j.prepare()
log.Println(j.name(), "starting, must copy", len(j.copyList), "new objects (", bytes(j.copyTotalSize), ")")
go func() {
for {
time.Sleep(time.Second)
j.status()
}
}()
j.copy()
}