-
Notifications
You must be signed in to change notification settings - Fork 351
/
sync.go
334 lines (308 loc) · 8.81 KB
/
sync.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
package local
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync/atomic"
"syscall"
"time"
"github.com/go-openapi/swag"
"github.com/treeverse/lakefs/pkg/api/apigen"
"github.com/treeverse/lakefs/pkg/api/apiutil"
"github.com/treeverse/lakefs/pkg/api/helpers"
"github.com/treeverse/lakefs/pkg/fileutil"
"github.com/treeverse/lakefs/pkg/uri"
"golang.org/x/sync/errgroup"
)
const (
DefaultDirectoryMask = 0o755
ClientMtimeMetadataKey = apiutil.LakeFSMetadataPrefix + "client-mtime"
)
type SyncFlags struct {
Parallelism int
Presign bool
PresignMultipart bool
}
func getMtimeFromStats(stats apigen.ObjectStats) (int64, error) {
if stats.Metadata == nil {
return stats.Mtime, nil
}
clientMtime, hasClientMtime := stats.Metadata.Get(ClientMtimeMetadataKey)
if hasClientMtime {
// parse
return strconv.ParseInt(clientMtime, 10, 64)
}
return stats.Mtime, nil
}
type Tasks struct {
Downloaded uint64
Uploaded uint64
Removed uint64
}
type SyncManager struct {
ctx context.Context
client *apigen.ClientWithResponses
httpClient *http.Client
progressBar *ProgressPool
flags SyncFlags
tasks Tasks
}
func NewSyncManager(ctx context.Context, client *apigen.ClientWithResponses, flags SyncFlags) *SyncManager {
return &SyncManager{
ctx: ctx,
client: client,
httpClient: http.DefaultClient,
progressBar: NewProgressPool(),
flags: flags,
}
}
// Sync - sync changes between remote and local directory given the Changes channel.
// For each change, will apply download, upload or delete according to the change type and change source
func (s *SyncManager) Sync(rootPath string, remote *uri.URI, changeSet <-chan *Change) error {
s.progressBar.Start()
defer s.progressBar.Stop()
wg, ctx := errgroup.WithContext(s.ctx)
for i := 0; i < s.flags.Parallelism; i++ {
wg.Go(func() error {
for change := range changeSet {
if err := s.apply(ctx, rootPath, remote, change); err != nil {
return err
}
}
return nil
})
}
if err := wg.Wait(); err != nil {
return err
}
_, err := fileutil.PruneEmptyDirectories(rootPath)
return err
}
func (s *SyncManager) apply(ctx context.Context, rootPath string, remote *uri.URI, change *Change) error {
switch change.Type {
case ChangeTypeAdded, ChangeTypeModified:
switch change.Source {
case ChangeSourceRemote:
// remotely changed something, download it!
if err := s.download(ctx, rootPath, remote, change.Path); err != nil {
return fmt.Errorf("download %s failed: %w", change.Path, err)
}
case ChangeSourceLocal:
// we wrote something, upload it!
if err := s.upload(ctx, rootPath, remote, change.Path); err != nil {
return fmt.Errorf("upload %s failed: %w", change.Path, err)
}
default:
panic("invalid change source")
}
case ChangeTypeRemoved:
if change.Source == ChangeSourceRemote {
// remote deleted something, delete it locally!
if err := s.deleteLocal(rootPath, change); err != nil {
return fmt.Errorf("delete local %s failed: %w", change.Path, err)
}
} else {
// we deleted something, delete it on remote!
if err := s.deleteRemote(ctx, remote, change); err != nil {
return fmt.Errorf("delete remote %s failed: %w", change.Path, err)
}
}
case ChangeTypeConflict:
return ErrConflict
default:
panic("invalid change type")
}
return nil
}
func (s *SyncManager) download(ctx context.Context, rootPath string, remote *uri.URI, path string) error {
if err := fileutil.VerifyRelPath(strings.TrimPrefix(path, uri.PathSeparator), rootPath); err != nil {
return err
}
destination := filepath.Join(rootPath, path)
destinationDirectory := filepath.Dir(destination)
if err := os.MkdirAll(destinationDirectory, DefaultDirectoryMask); err != nil {
return err
}
statResp, err := s.client.StatObjectWithResponse(ctx, remote.Repository, remote.Ref, &apigen.StatObjectParams{
Path: filepath.ToSlash(filepath.Join(remote.GetPath(), path)),
Presign: swag.Bool(s.flags.Presign),
UserMetadata: swag.Bool(true),
})
if err != nil {
return err
}
if statResp.StatusCode() != http.StatusOK {
httpErr := apigen.Error{Message: "no content"}
_ = json.Unmarshal(statResp.Body, &httpErr)
return fmt.Errorf("(stat: HTTP %d, message: %s): %w", statResp.StatusCode(), httpErr.Message, ErrDownloadingFile)
}
// get mtime
mtimeSecs, err := getMtimeFromStats(*statResp.JSON200)
if err != nil {
return err
}
if strings.HasSuffix(path, uri.PathSeparator) {
// Directory marker - skip
return nil
}
lastModified := time.Unix(mtimeSecs, 0)
sizeBytes := swag.Int64Value(statResp.JSON200.SizeBytes)
f, err := os.Create(destination)
if err != nil {
// Sometimes we get a file that is actually a directory marker (Spark loves writing those).
// If we already have the directory, we can skip it.
if errors.Is(err, syscall.EISDIR) && sizeBytes == 0 {
return nil // no further action required!
}
return fmt.Errorf("could not create file '%s': %w", destination, err)
}
defer func() {
err = f.Close()
}()
if sizeBytes == 0 { // if size is empty just create file
spinner := s.progressBar.AddSpinner("download " + path)
atomic.AddUint64(&s.tasks.Downloaded, 1)
defer spinner.Done()
} else { // Download file
// make request
var body io.Reader
if s.flags.Presign {
resp, err := s.httpClient.Get(statResp.JSON200.PhysicalAddress)
if err != nil {
return err
}
defer func() {
_ = resp.Body.Close()
}()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%s (pre-signed GET: HTTP %d): %w", path, resp.StatusCode, ErrDownloadingFile)
}
body = resp.Body
} else {
resp, err := s.client.GetObject(ctx, remote.Repository, remote.Ref, &apigen.GetObjectParams{
Path: filepath.ToSlash(filepath.Join(remote.GetPath(), path)),
})
if err != nil {
return err
}
defer func() {
_ = resp.Body.Close()
}()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%s (GetObject: HTTP %d): %w", path, resp.StatusCode, ErrDownloadingFile)
}
body = resp.Body
}
b := s.progressBar.AddReader(fmt.Sprintf("download %s", path), sizeBytes)
barReader := b.Reader(body)
defer func() {
if err != nil {
b.Error()
} else {
atomic.AddUint64(&s.tasks.Downloaded, 1)
b.Done()
}
}()
_, err = io.Copy(f, barReader)
if err != nil {
return fmt.Errorf("could not write file '%s': %w", destination, err)
}
}
// set mtime to the server returned one
err = os.Chtimes(destination, time.Now(), lastModified) // Explicit to catch in deferred func
return err
}
func (s *SyncManager) upload(ctx context.Context, rootPath string, remote *uri.URI, path string) error {
source := filepath.Join(rootPath, path)
if err := fileutil.VerifySafeFilename(source); err != nil {
return err
}
dest := filepath.ToSlash(filepath.Join(remote.GetPath(), path))
f, err := os.Open(source)
if err != nil {
return err
}
defer func() {
_ = f.Close()
}()
fileStat, err := f.Stat()
if err != nil {
return err
}
b := s.progressBar.AddReader(fmt.Sprintf("upload %s", path), fileStat.Size())
defer func() {
if err != nil {
b.Error()
} else {
atomic.AddUint64(&s.tasks.Uploaded, 1)
b.Done()
}
}()
metadata := map[string]string{
ClientMtimeMetadataKey: strconv.FormatInt(fileStat.ModTime().Unix(), 10),
}
reader := fileWrapper{
file: f,
reader: b.Reader(f),
}
if s.flags.Presign {
_, err = helpers.ClientUploadPreSign(
ctx, s.client, remote.Repository, remote.Ref, dest, metadata, "", reader, s.flags.PresignMultipart)
return err
}
// not pre-signed
_, err = helpers.ClientUpload(
ctx, s.client, remote.Repository, remote.Ref, dest, metadata, "", reader)
return err
}
func (s *SyncManager) deleteLocal(rootPath string, change *Change) (err error) {
b := s.progressBar.AddSpinner("delete local: " + change.Path)
defer func() {
defer func() {
if err != nil {
b.Error()
} else {
atomic.AddUint64(&s.tasks.Removed, 1)
b.Done()
}
}()
}()
source := filepath.Join(rootPath, change.Path)
err = fileutil.RemoveFile(source)
if err != nil {
return err
}
return nil
}
func (s *SyncManager) deleteRemote(ctx context.Context, remote *uri.URI, change *Change) (err error) {
b := s.progressBar.AddSpinner("delete remote path: " + change.Path)
defer func() {
if err != nil {
b.Error()
} else {
atomic.AddUint64(&s.tasks.Removed, 1)
b.Done()
}
}()
dest := filepath.ToSlash(filepath.Join(remote.GetPath(), change.Path))
resp, err := s.client.DeleteObjectWithResponse(ctx, remote.Repository, remote.Ref, &apigen.DeleteObjectParams{
Path: dest,
})
if err != nil {
return
}
if resp.StatusCode() != http.StatusNoContent {
return fmt.Errorf("could not delete object: HTTP %d: %w", resp.StatusCode(), helpers.ErrRequestFailed)
}
return
}
func (s *SyncManager) Summary() Tasks {
return s.tasks
}