-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmover.go
193 lines (153 loc) · 4.49 KB
/
mover.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
// Copyright (c) 2018 DDN. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"io"
"net/url"
"time"
"cloud.google.com/go/storage"
"github.com/pkg/errors"
"github.com/intel-hpdd/lemur/dmplugin"
"github.com/intel-hpdd/lemur/dmplugin/dmio"
"github.com/intel-hpdd/logging/debug"
"github.com/pborman/uuid"
)
// Mover is a GCS data mover
type Mover struct {
Name string
Ctx context.Context
GcsClient *storage.Client
Bucket string
}
// NewMover returns a new *Mover
func GcsMover(config *archiveConfig, ctx context.Context, gcsClient *storage.Client) *Mover {
return &Mover{
Name: config.Name,
Ctx: ctx,
GcsClient: gcsClient,
Bucket: config.Bucket,
}
}
func newFileID() string {
return uuid.New()
}
// Destination returns the path to archived file.
// Exported for testin
func (m *Mover) destination(id string) string {
return id
}
func (m *Mover) newWriter(object string) *storage.Writer {
return m.GcsClient.Bucket(m.Bucket).Object(object).NewWriter(m.Ctx)
}
func (m *Mover) newReader(object string) (*storage.Reader, error) {
return m.GcsClient.Bucket(m.Bucket).Object(object).NewReader(m.Ctx)
}
func upload(wc *storage.Writer, f io.Reader) error {
if _, err := io.Copy(wc, f); err != nil {
return err
}
if err := wc.Close(); err != nil {
return err
}
return nil
}
func download(wc io.Writer, f *storage.Reader) (int64, error) {
if _, err := io.Copy(wc, f); err != nil {
return f.Size(), err
}
if err := f.Close(); err != nil {
return f.Size(), err
}
return f.Size(), nil
}
// Start signals the mover to begin any asynchronous processing (e.g. stats)
func (m *Mover) Start() {
debug.Printf("%s started", m.Name)
}
func (m *Mover) Archive(action dmplugin.Action) error {
debug.Printf("%s id:%d ARCHIVE %s", m.Name, action.ID(), action.PrimaryPath())
rate.Mark(1)
start := time.Now()
fileID := newFileID()
fileKey := m.destination(fileID)
rdr, total, err := dmio.NewActionReader(action)
if err != nil {
return errors.Wrapf(err, "Could not create archive reader for %s", action)
}
defer rdr.Close()
progressFunc := func(offset, length int64) error {
return action.Update(offset, length, total)
}
progressReader := dmio.NewProgressReader(rdr, updateInterval, progressFunc)
defer progressReader.StopUpdates()
writer := m.newWriter(fileKey)
upload(writer, progressReader)
if err != nil {
return errors.Wrap(err, "upload failed")
}
debug.Printf("%s id:%d Archived %d bytes in %v from %s to", m.Name, action.ID(), total,
time.Since(start),
action.PrimaryPath())
u := url.URL{
Scheme: "gs",
Host: m.Bucket,
Path: fileKey,
}
action.SetUUID(fileID)
action.SetURL(u.String())
action.SetActualLength(total)
return nil
}
// Restore fulfills an HSM Restore request
func (m *Mover) Restore(action dmplugin.Action) error {
debug.Printf("%s id:%d restore %s %s", m.Name, action.ID(), action.PrimaryPath(), action.UUID())
rate.Mark(1)
start := time.Now()
if action.UUID() == "" {
return errors.Errorf("Missing file_id on action %d", action.ID())
}
srcObj := string(action.UUID())
objAttrs, err := m.GcsClient.Bucket(m.Bucket).Object(srcObj).Attrs(m.Ctx)
if err != nil {
return err
}
debug.Printf("obj %s, size %d", srcObj, objAttrs.Size)
dstSize := objAttrs.Size
dst, err := dmio.NewActionWriter(action)
if err != nil {
return errors.Wrapf(err, "Couldn't create ActionWriter for %s", action)
}
defer dst.Close()
progressFunc := func(offset, length int64) error {
return action.Update(offset, length, dstSize)
}
progressWriter := dmio.NewProgressWriter(dst, updateInterval, progressFunc)
defer progressWriter.StopUpdates()
reader, err := m.newReader(srcObj)
n, err := download(progressWriter, reader)
if err != nil {
return errors.Errorf("gcs.Download() of %s failed: %s", srcObj, err)
}
debug.Printf("%s id:%d Restored %d bytes in %v from %s to %s", m.Name, action.ID(), n,
time.Since(start),
srcObj,
action.PrimaryPath())
action.SetActualLength(n)
return nil
}
// Remove fulfills an HSM Remove request
func (m *Mover) Remove(action dmplugin.Action) error {
debug.Printf("%s id:%d remove %s %s", m.Name, action.ID(), action.PrimaryPath(), action.UUID())
rate.Mark(1)
if action.UUID() == "" {
return errors.New("Missing file_id")
}
o := m.GcsClient.Bucket(m.Bucket).Object(string(action.UUID()))
err := o.Delete(m.Ctx)
if err != nil {
return err
}
return errors.Wrap(err, "delete object failed")
}