-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathsync_file.go
1172 lines (1012 loc) · 36.9 KB
/
sync_file.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package sync
import (
"context"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
butil "github.com/longhorn/backupstore/util"
lhns "github.com/longhorn/go-common-libs/ns"
lhtypes "github.com/longhorn/go-common-libs/types"
sparserest "github.com/longhorn/sparse-tools/sparse/rest"
"github.com/longhorn/backing-image-manager/api"
"github.com/longhorn/backing-image-manager/pkg/backup"
"github.com/longhorn/backing-image-manager/pkg/crypto"
"github.com/longhorn/backing-image-manager/pkg/types"
"github.com/longhorn/backing-image-manager/pkg/util"
)
// SyncFile state machine:
//
// +---------+ Try to reuse file (best effort), +-----------+
// +--+ pending +----------------------------------------------->+ starting +-----+
// | +----+----+ do cleanup & preparation if reuse failed. +-----+-----+ |
// | | | |
// | | | |
// | | Sender doesn't start sending | |
// | | +---------------------------------+ |
// | | | data after an interval. |
// | | | |
// | | | Start syncing |
// | | | the file. |
// | | v |
// | | +------+------+ |
// | +----------------->+ failed +<--------------------------+ |
// | Fail at cleanup +------+------+ | |
// | or preparation. ^ | |
// | | Fail at file handling, | |
// | Reuse success. | data transport, | |
// | | or the final validation. | |
// | | | |
// | +-------------+ | | |
// | + unknown + --------------+ | |
// | +------+------+ Checksum is changed after modification. | |
// | ^ | |
// | | File is suddenly modified. | |
// | | Need to re-calculate the checksum. | |
// | v | |
// | +-------------+ +------+------+ |
// +-->+ ready + <-----------------------------------------+ in-progress +<--+
// +------+------+ +------+------+
//
const (
RetryInterval = 1 * time.Second
RetryCount = 60
LargeRetryCount = 10 * 60
PeriodicRefreshIntervalInSeconds = 2
TmpFileSuffix = ".tmp"
)
type SyncingFile struct {
lock *sync.RWMutex
log logrus.FieldLogger
ctx context.Context
cancel context.CancelFunc
filePath string
tmpFilePath string
uuid string
diskUUID string
size int64
virtualSize int64
realSize int64
state types.State
progress int
processedSize int64
expectedChecksum string
currentChecksum string
modificationTime string
message string
sendingReference int
// for unit test
handler Handler
}
func (sf *SyncingFile) UpdateRestoreProgress(processedSize int, err error) {
sf.lock.Lock()
defer sf.lock.Unlock()
if sf.state == types.StateStarting {
sf.state = types.StateInProgress
}
if sf.state == types.StateReady {
return
}
sf.processedSize = int64(processedSize)
if sf.size > 0 {
sf.progress = int((float32(sf.processedSize) / float32(sf.size)) * 100)
}
if err != nil {
sf.message = errors.Wrapf(err, "failed to restore backing image").Error()
}
}
func (sf *SyncingFile) UpdateProgress(processedSize int64) {
sf.updateProgress(processedSize)
}
func (sf *SyncingFile) UpdateSyncFileProgress(size int64) {
sf.updateProgress(size)
}
func (sf *SyncingFile) updateProgress(processedSize int64) {
sf.lock.Lock()
defer sf.lock.Unlock()
if sf.state == types.StateStarting {
sf.state = types.StateInProgress
}
if sf.state == types.StateReady {
return
}
sf.processedSize = sf.processedSize + processedSize
if sf.size > 0 {
sf.progress = int((float32(sf.processedSize) / float32(sf.size)) * 100)
}
}
func NewSyncingFile(parentCtx context.Context, filePath, uuid, diskUUID, expectedChecksum string, size int64, handler Handler) *SyncingFile {
ctx, cancel := context.WithCancel(parentCtx)
sf := &SyncingFile{
lock: &sync.RWMutex{},
log: logrus.StandardLogger().WithFields(
logrus.Fields{
"component": "sync-file",
"filePath": filePath,
"uuid": uuid,
},
),
ctx: ctx,
cancel: cancel,
filePath: filePath,
tmpFilePath: fmt.Sprintf("%s%s", filePath, TmpFileSuffix),
uuid: uuid,
diskUUID: diskUUID,
size: size,
expectedChecksum: expectedChecksum,
state: types.StatePending,
handler: handler,
}
if size > 0 {
sf.log.WithField("size", size)
}
go func() {
// This may be time-consuming.
reuseErr := sf.checkAndReuseFile()
if reuseErr == nil {
return
}
sf.log.Infof("SyncingFile: cannot to reuse the existing file, will clean it up then start processing: %v", reuseErr)
var err error
defer func() {
sf.lock.Lock()
defer sf.lock.Unlock()
if err != nil {
sf.state = types.StateFailed
sf.log.Infof("SyncingFile: failed to init file syncing: %v", err)
} else {
sf.state = types.StateStarting
go sf.waitForProcessingBeginWithTimeout()
}
}()
dir := filepath.Dir(sf.filePath)
if mkErr := os.MkdirAll(dir, 0666); mkErr != nil && !os.IsExist(mkErr) {
err = mkErr
return
}
if err = os.RemoveAll(sf.tmpFilePath); err != nil {
return
}
if err = os.RemoveAll(sf.filePath); err != nil {
return
}
}()
return sf
}
func (sf *SyncingFile) checkAndReuseFile() (err error) {
sf.lock.RLock()
expectedChecksum := sf.expectedChecksum
filePath := sf.filePath
size := sf.size
sf.lock.RUnlock()
configFilePath := util.GetSyncingFileConfigFilePath(filePath)
defer func() {
if err == nil {
return
}
if rmErr := os.RemoveAll(configFilePath); rmErr != nil {
sf.log.Warnf("SyncingFile: failed to clean up config file %v after reuse failure: %v", configFilePath, rmErr)
}
}()
info, err := os.Stat(filePath)
if err != nil {
return err
}
if size > 0 && size != info.Size() {
return fmt.Errorf("sync file expected size %v doesn't match the existing file size %v", size, info.Size())
}
var currentChecksum string
config, err := util.ReadSyncingFileConfig(configFilePath)
if config != nil && config.ModificationTime == info.ModTime().UTC().String() {
logrus.Debugf("SyncingFile: directly get the checksum from a valid config during file reusage: %v", config.CurrentChecksum)
currentChecksum = config.CurrentChecksum
} else {
logrus.Debugf("SyncingFile: failed to get the checksum from a valid config during file reusage, will directly calculated it then")
currentChecksum, err = util.GetFileChecksum(filePath)
if err != nil {
return errors.Wrapf(err, "failed to calculate checksum for the existing file during init")
}
}
if expectedChecksum != "" && expectedChecksum != currentChecksum {
return fmt.Errorf("file expected checksum %v doesn't match the existing file checksum %v", expectedChecksum, currentChecksum)
}
sf.lock.Lock()
sf.cancel()
sf.currentChecksum = currentChecksum
sf.processedSize = info.Size()
sf.modificationTime = info.ModTime().UTC().String()
sf.updateSyncReadyNoLock()
sf.updateVirtualSizeNoLock(sf.filePath)
sf.updateRealSizeNoLock(sf.filePath)
sf.writeConfigNoLock()
sf.lock.Unlock()
sf.log.Infof("SyncingFile: directly reuse/introduce the existing file in path %v", filePath)
return nil
}
func (sf *SyncingFile) waitForProcessingBeginWithTimeout() {
count := 0
ticker := time.NewTicker(RetryInterval)
defer ticker.Stop()
for {
<-ticker.C
count++
sf.lock.Lock()
notBeginYet := sf.state == "" || sf.state == types.StatePending || sf.state == types.StateStarting
if !notBeginYet {
sf.lock.Unlock()
return
}
if count >= RetryCount {
sf.handleFailureNoLock(fmt.Errorf("failed to wait for processing begin in %v seconds, current state %v", RetryCount, sf.state))
sf.lock.Unlock()
return
}
sf.lock.Unlock()
}
}
func (sf *SyncingFile) WaitForStateNonPending() error {
count := 0
ticker := time.NewTicker(RetryInterval)
defer ticker.Stop()
for {
<-ticker.C
count++
sf.lock.RLock()
state := sf.state
sf.lock.RUnlock()
if state != types.StatePending {
return nil
}
if count >= LargeRetryCount {
return fmt.Errorf("sync file is still in empty state after %v second", LargeRetryCount)
}
}
}
func (sf *SyncingFile) Get() api.FileInfo {
sf.lock.Lock()
defer sf.lock.Unlock()
// make sure the file data is intact when the state is ready
if sf.state == types.StateReady {
sf.validateReadyFileNoLock()
}
return sf.getNoLock()
}
func (sf *SyncingFile) validateReadyFileNoLock() {
if sf.state != types.StateReady {
return
}
if modificationTime := util.FileModificationTime(sf.filePath); modificationTime != sf.modificationTime {
sf.state = types.StateUnknown
sf.modificationTime = modificationTime
sf.message = fmt.Sprintf("ready file is suddenly modified at %v, need to re-check the checksum", modificationTime)
go func() {
var err error
if checksum, cksumErr := util.GetFileChecksum(sf.filePath); cksumErr != nil {
err = errors.Wrapf(cksumErr, "failed to re-calculate checksum after ready file being modified, will mark the file as failed")
} else if checksum != sf.currentChecksum {
err = fmt.Errorf("ready file is modified at %v with a different checksum %v, previous checksum %v", modificationTime, checksum, sf.currentChecksum)
}
sf.lock.Lock()
defer sf.lock.Unlock()
if err != nil {
sf.state = types.StateFailed
sf.message = err.Error()
} else {
sf.state = types.StateReady
sf.message = ""
sf.writeConfigNoLock()
}
}()
}
}
func (sf *SyncingFile) getNoLock() api.FileInfo {
return api.FileInfo{
DiskUUID: sf.diskUUID,
ExpectedChecksum: sf.expectedChecksum,
FilePath: sf.filePath,
UUID: sf.uuid,
Size: sf.size,
VirtualSize: sf.virtualSize,
RealSize: sf.realSize,
State: string(sf.state),
Progress: sf.progress,
ProcessedSize: sf.processedSize,
CurrentChecksum: sf.currentChecksum,
ModificationTime: sf.modificationTime,
Message: sf.message,
SendingReference: sf.sendingReference,
}
}
func (sf *SyncingFile) Delete() {
sf.log.Infof("SyncingFile: start to delete sync file")
sf.lock.RLock()
defer sf.lock.RUnlock()
sf.cancel()
if err := os.RemoveAll(sf.tmpFilePath); err != nil {
sf.log.Warnf("SyncingFile: failed to delete tmp sync file %v: %v", sf.tmpFilePath, err)
}
if err := os.RemoveAll(sf.filePath); err != nil {
sf.log.Warnf("SyncingFile: failed to delete sync file %v: %v", sf.filePath, err)
}
configFilePath := util.GetSyncingFileConfigFilePath(sf.filePath)
if err := os.RemoveAll(configFilePath); err != nil {
sf.log.Warnf("SyncingFile: failed to delete sync file config file %v: %v", configFilePath, err)
}
}
func (sf *SyncingFile) GetFileReader() (io.ReadCloser, error) {
sf.log.Infof("SyncingFile: prepare a reader for the sync file")
sf.lock.RLock()
if sf.state != types.StateReady {
sf.lock.RUnlock()
return nil, fmt.Errorf("cannot get the reader for a non-ready file, current state %v", sf.state)
}
sf.lock.RUnlock()
return os.Open(sf.filePath)
}
func (sf *SyncingFile) isProcessingRequired() (bool, error) {
sf.lock.RLock()
defer sf.lock.RUnlock()
if sf.state == types.StateReady {
sf.log.Infof("SyncingFile: file is already state %v, no need to process it", types.StateReady)
return false, nil
}
if sf.state != types.StateStarting {
return false, fmt.Errorf("invalid state %v for actual processing", sf.state)
}
return true, nil
}
func (sf *SyncingFile) Fetch(srcFilePath string) (err error) {
sf.log.Infof("SyncingFile: start to fetch sync file from %v", srcFilePath)
shouldReuseFile := srcFilePath == sf.filePath
srcConfigFilePath := util.GetSyncingFileConfigFilePath(srcFilePath)
defer func() {
// cleanup the src file
if !shouldReuseFile {
sf.log.Infof("SyncingFile: try to clean up src file %v and its config file %v after fetch", srcFilePath, srcConfigFilePath)
if err := os.RemoveAll(srcFilePath); err != nil {
sf.log.Errorf("SyncingFile: failed to clean up src file %v after fetch: %v", srcFilePath, err)
}
if err := os.RemoveAll(srcConfigFilePath); err != nil {
sf.log.Errorf("SyncingFile: failed to clean up src config file %v after fetch: %v", srcConfigFilePath, err)
}
}
}()
needProcessing, err := sf.isProcessingRequired()
if err != nil {
return err
}
if !needProcessing {
return nil
}
defer func() {
if finalErr := sf.finishProcessing(err, types.DataEnginev1); finalErr != nil {
err = finalErr
}
}()
if shouldReuseFile {
return fmt.Errorf("syncing file should be directly reused but failed")
}
sf.log.Debugf("SyncingFile: need to fetch the syncing file from %v", srcFilePath)
srcFileStat, err := os.Stat(srcFilePath)
if err != nil {
return err
}
if srcFileStat.IsDir() {
return fmt.Errorf("the src file %v of the fetch call should not be dir", srcFilePath)
}
if err = os.Rename(srcFilePath, sf.tmpFilePath); err != nil {
return err
}
if srcConfigFileStat, err := os.Stat(srcConfigFilePath); err == nil && !srcConfigFileStat.IsDir() {
sf.log.Debugf("SyncingFile: found the corresponding src config file %v", srcConfigFilePath)
if err = os.Rename(srcConfigFilePath, util.GetSyncingFileConfigFilePath(sf.filePath)); err != nil {
return err
}
}
sf.lock.Lock()
sf.state = types.StateInProgress
sf.processedSize = srcFileStat.Size()
sf.lock.Unlock()
return nil
}
func (sf *SyncingFile) DownloadFromURL(url, dataEngine string) (written int64, err error) {
sf.log.Infof("SyncingFile: start to download sync file from URL %v", url)
needProcessing, err := sf.isProcessingRequired()
if err != nil {
return 0, err
}
if !needProcessing {
return 0, nil
}
defer func() {
if finalErr := sf.finishProcessing(err, dataEngine); finalErr != nil {
err = finalErr
}
}()
size, err := sf.handler.GetSizeFromURL(url)
if err != nil {
return 0, err
}
sf.lock.Lock()
sf.size = size
sf.log.WithField("size", size)
sf.lock.Unlock()
return sf.handler.DownloadFromURL(sf.ctx, url, sf.tmpFilePath, sf)
}
func (sf *SyncingFile) RestoreFromBackupURL(backupURL string, credential map[string]string, concurrentLimit int, dataEngine string) (err error) {
sf.log.Infof("SyncingFile: start to restore sync file from backup URL %v", backupURL)
needProcessing, err := sf.isProcessingRequired()
if err != nil {
return err
}
if !needProcessing {
return nil
}
defer func() {
if finalErr := sf.finishProcessing(err, dataEngine); finalErr != nil {
err = finalErr
}
}()
backupType, err := util.CheckBackupType(backupURL)
if err != nil {
return errors.Wrapf(err, "failed to check the type for backup %v", backupURL)
}
if err := butil.SetupCredential(backupType, credential); err != nil {
return err
}
backupURL = butil.UnescapeURL(backupURL)
info, err := backup.GetBackupInfo(backupURL)
if err != nil {
return err
}
sf.lock.Lock()
sf.size = info.Size
sf.lock.Unlock()
// async call to start restoration
if err := backup.DoBackupRestore(backupURL, sf.tmpFilePath, concurrentLimit, sf); err != nil {
return err
}
// wait until restoration is complete or failed
err = sf.waitForRestoreComplete()
return err
}
func (sf *SyncingFile) waitForRestoreComplete() (err error) {
var (
restoreProgress int
restoreError string
)
periodicChecker := time.NewTicker(PeriodicRefreshIntervalInSeconds * time.Second)
for range periodicChecker.C {
sf.lock.Lock()
restoreProgress = sf.progress
restoreError = sf.message
sf.lock.Unlock()
if restoreProgress == 100 {
periodicChecker.Stop()
return nil
}
if restoreError != "" {
periodicChecker.Stop()
return fmt.Errorf("%v", restoreError)
}
}
return nil
}
// CloneToFileWithEncryption clone the backing file on the same node to another backing file with the given encryption operation.
// when doing encryption, it creates a loop device from the target backing file, setup the encrypted device from the loop device and then dump the data from the source file to the target encrypted device.
// When doing decryption, it creates a loop device from the source backing file, setup the encrypted device from the loop device and then dump the data from the source encrypted device to the target file.
// When doing ignore clone, it directly dumps the data from the source backing file to the target backing file.
func (sf *SyncingFile) CloneToFileWithEncryption(sourceBackingImage, sourceBackingImageUUID string, encryption types.EncryptionType, credential map[string]string, dataEngine string) (copied int64, err error) {
sf.log.Infof("SyncingFile: start to clone the file")
defer func() {
if err != nil {
sf.log.Errorf("SyncingFile: failed CloneToFileWithEncryption: %v", err)
}
}()
needProcessing, err := sf.isProcessingRequired()
if err != nil {
return 0, err
}
if !needProcessing {
return 0, nil
}
defer func() {
if finalErr := sf.finishProcessing(err, dataEngine); finalErr != nil {
err = finalErr
}
}()
sourceFile, tmpRawFile, writeZero, err := sf.prepareCloneSourceFile(sourceBackingImage, sourceBackingImageUUID, encryption)
defer func() {
if tmpRawFile != "" {
os.RemoveAll(tmpRawFile)
}
}()
if err != nil {
return 0, errors.Wrapf(err, "failed to prepare source file")
}
err = sf.prepareCloneTargetFile(sourceFile, encryption)
if err != nil {
return 0, errors.Wrapf(err, "failed to prepare target file")
}
sourceFileReader, sourceLoopDevicePath, err := sf.openCloneSourceFile(sourceFile, encryption, credential)
defer func() {
if sourceFileReader != nil {
sourceFileReader.Close()
}
if sourceLoopDevicePath == "" {
sf.closeCryptoDevice(sourceLoopDevicePath)
}
}()
if err != nil {
return 0, errors.Wrapf(err, "failed to open clone source file")
}
targetFileWriter, targetLoopDevicePath, err := sf.openCloneTargetFile(encryption, credential)
defer func() {
if targetFileWriter != nil {
targetFileWriter.Close()
}
if targetLoopDevicePath == "" {
sf.closeCryptoDevice(targetLoopDevicePath)
}
}()
if err != nil {
return 0, errors.Wrapf(err, "failed to open clone source file")
}
nw, err := IdleTimeoutCopy(sf.ctx, sf.cancel, sourceFileReader, targetFileWriter, sf, writeZero)
if err != nil {
err = errors.Wrapf(err, "failed to copy the data with timeout")
}
return nw, err
}
func (sf *SyncingFile) openCloneSourceFile(sourceFile string, encryption types.EncryptionType, credential map[string]string) (*os.File, string, error) {
loopDevicePath := ""
if encryption == types.EncryptionTypeDecrypt {
loopDevicePath, err := sf.setupCryptoDevice(sourceFile, false, credential)
if err != nil {
return nil, loopDevicePath, errors.Wrapf(err, "failed to setup the crypto device with the file %v during cloning", sourceFile)
}
sourceFile = types.BackingImageMapper(sf.uuid)
}
if _, err := os.Stat(sourceFile); err != nil {
return nil, loopDevicePath, errors.Wrapf(err, "%v not found during cloning", sourceFile)
}
sourceFileReader, err := os.Open(sourceFile)
if err != nil {
return nil, loopDevicePath, errors.Wrapf(err, "Error opening file %v", sourceFile)
}
return sourceFileReader, loopDevicePath, nil
}
func (sf *SyncingFile) openCloneTargetFile(encryption types.EncryptionType, credential map[string]string) (*os.File, string, error) {
loopDevicePath := ""
targetFile := sf.tmpFilePath
if encryption == types.EncryptionTypeEncrypt {
loopDevicePath, err := sf.setupCryptoDevice(targetFile, true, credential)
if err != nil {
return nil, loopDevicePath, errors.Wrapf(err, "failed to setup the crypto device with the file %v during cloning", targetFile)
}
targetFile = types.BackingImageMapper(sf.uuid)
}
if _, err := os.Stat(targetFile); err != nil {
return nil, loopDevicePath, errors.Wrapf(err, "%v not found during cloning", targetFile)
}
targetFileWriter, err := os.OpenFile(targetFile, os.O_RDWR, 0666)
if err != nil {
return nil, loopDevicePath, errors.Wrapf(err, "Error opening file %v", targetFile)
}
return targetFileWriter, loopDevicePath, nil
}
func (sf *SyncingFile) prepareCloneSourceFile(sourceBackingImage, sourceBackingImageUUID string, encryption types.EncryptionType) (string, string, bool, error) {
writeZero := false
sourceFile := types.GetBackingImageFilePath(types.DiskPathInContainer, sourceBackingImage, sourceBackingImageUUID)
tmpRawFile := ""
if _, err := os.Stat(sourceFile); err != nil {
return "", tmpRawFile, writeZero, errors.Wrapf(err, "source file %v not found ", sourceFile)
}
if encryption == types.EncryptionTypeEncrypt {
// we should write zero when doing encryption so the zero can be encrypted as well
writeZero = true
// If the source file is qcow2 when encrypting we need to convert and use its raw image
imgInfo, err := util.GetQemuImgInfo(sourceFile)
if err != nil {
return "", tmpRawFile, writeZero, errors.Wrapf(err, "failed to get source backing file %v qemu info", sourceFile)
}
if imgInfo.Format == "qcow2" {
tmpRawFile := fmt.Sprintf("%v-raw.tmp", sourceFile)
if err := util.ConvertFromQcow2ToRaw(sourceFile, tmpRawFile); err != nil {
return "", tmpRawFile, writeZero, errors.Wrapf(err, "failed to create raw image from qcow2 image %v", sourceFile)
}
// use the raw image as source when doing encryption
sourceFile = tmpRawFile
}
}
return sourceFile, tmpRawFile, writeZero, nil
}
func (sf *SyncingFile) prepareCloneTargetFile(sourceFile string, encryption types.EncryptionType) error {
info, err := os.Stat(sourceFile)
if err != nil {
return err
}
sourceFileSize := info.Size()
if err := sf.setFileSizeForEncryption(sourceFileSize, encryption); err != nil {
return errors.Wrap(err, "failed to set size for the target file")
}
f, err := os.OpenFile(sf.tmpFilePath, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
return err
}
if err = f.Truncate(sf.size); err != nil {
return err
}
f.Close()
return nil
}
func (sf *SyncingFile) IdleTimeoutCopyToFile(src io.ReadCloser, dataEngine string) (copied int64, err error) {
sf.log.Infof("SyncingFile: start to copy data to sync file")
defer func() {
if err != nil {
sf.log.Errorf("SyncingFile: failed IdleTimeoutCopyToFile: %v", err)
}
}()
needProcessing, err := sf.isProcessingRequired()
if err != nil {
return 0, err
}
if !needProcessing {
return 0, nil
}
f, err := os.OpenFile(sf.tmpFilePath, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
return 0, err
}
defer f.Close()
if err = f.Truncate(sf.size); err != nil {
return 0, err
}
defer func() {
if finalErr := sf.finishProcessing(err, dataEngine); finalErr != nil {
err = finalErr
}
}()
nw, err := IdleTimeoutCopy(sf.ctx, sf.cancel, src, f, sf, false)
if err != nil {
err = errors.Wrapf(err, "failed to copy the data with timeout")
}
return nw, err
}
func (sf *SyncingFile) Receive(port int, fileType, dataEngine string) (err error) {
sf.log.Infof("SyncingFile: start to launch a receiver at port %v", port)
needProcessing, err := sf.isProcessingRequired()
if err != nil {
return err
}
if !needProcessing {
return nil
}
defer func() {
if finalErr := sf.finishProcessing(err, dataEngine); finalErr != nil {
err = finalErr
}
}()
// TODO: After merging the sparse tool repo into this sync service, we don't need to launch a separate server here.
// Instead, this SyncingFile is responsible for punching hole, reading/writing data, and computing checksum.
if serverErr := sparserest.Server(sf.ctx, strconv.Itoa(port), sf.tmpFilePath, sf); serverErr != nil && serverErr != http.ErrServerClosed {
err = serverErr
return err
}
// For sparse files, holes may not be calculated into s.processedSize.
// And if the whole file is empty, the state would be starting rather than in-progress.
// To avoid s.finishProcessing() failure, we need to update s.processedSize in advance.
sf.lock.Lock()
if sf.state == types.StateFailed || sf.state == types.StateReady {
sf.lock.Unlock()
return nil
}
if sf.state == types.StateStarting {
sf.state = types.StateInProgress
}
sf.processedSize = sf.size
sf.lock.Unlock()
// The file size will change after conversion.
if fileType == types.SyncingFileTypeQcow2 {
sf.log.Infof("SyncingFile: converting the file type from raw to qcow2")
if err = util.ConvertFromRawToQcow2(sf.tmpFilePath); err != nil {
return err
}
}
return nil
}
func (sf *SyncingFile) Send(toAddress string, sender Sender) (err error) {
sf.lock.Lock()
defer sf.lock.Unlock()
if sf.sendingReference >= types.SendingLimit {
return fmt.Errorf("syncing file %v reaches to the simultaneous sending limit %v", sf.filePath, types.SendingLimit)
}
sf.validateReadyFileNoLock()
if sf.state != types.StateReady {
return fmt.Errorf("invalid state %v for file sending", sf.state)
}
sf.sendingReference++
go func() {
defer func() {
sf.lock.Lock()
sf.sendingReference--
sf.lock.Unlock()
}()
if err := sender(sf.filePath, toAddress); err != nil {
sf.log.Errorf("SyncingFile: failed to send file: %v", err)
}
}()
return nil
}
func (sf *SyncingFile) finishProcessing(err error, dataEngine string) (finalErr error) {
sf.lock.Lock()
defer sf.lock.Unlock()
sf.cancel()
defer func() {
sf.handleFailureNoLock(finalErr)
}()
if err != nil {
finalErr = err
return
}
if sf.state != types.StateInProgress {
sf.log.Warnf("SyncingFile: invalid state %v after processing", sf.state)
return
}
if sf.size > 0 && sf.processedSize != sf.size {
finalErr = fmt.Errorf("processed data size %v does not match the expected file size %v", sf.processedSize, sf.size)
return
}
stat, statErr := os.Stat(sf.tmpFilePath)
if statErr != nil {
finalErr = errors.Wrapf(statErr, "failed to stat tmp file %v after getting the file from source", sf.tmpFilePath)
return
}
if stat.Size() != sf.processedSize {
sf.log.Debugf("SyncingFile: processed size %v is not equal to the actual file size %v after processing", sf.processedSize, stat.Size())
sf.processedSize = stat.Size()
}
sf.modificationTime = stat.ModTime().UTC().String()
// If the file is qcow2, we need to convert it to raw for dumping the data to the spdk lvol
// This will only happen when preparing the first backing image in data source.
if dataEngine == types.DataEnginev2 {
imgInfo, qemuErr := util.GetQemuImgInfo(sf.tmpFilePath)
if qemuErr != nil {
finalErr = errors.Wrapf(qemuErr, "failed to detect if file %v is qcow2", sf.tmpFilePath)
return
}
tmpRawFile := fmt.Sprintf("%v-raw.tmp", sf.tmpFilePath)
if imgInfo.Format == "qcow2" {
if convertErr := util.ConvertFromQcow2ToRaw(sf.tmpFilePath, tmpRawFile); convertErr != nil {
finalErr = errors.Wrapf(convertErr, "failed to create raw image from qcow2 image %v", sf.tmpFilePath)
return
}
if removeErr := os.RemoveAll(sf.tmpFilePath); removeErr != nil {
sf.log.Warnf("SyncingFile: failed to remove the qcow2 file %v after converting to raw file", sf.tmpFilePath)
}
if renameErr := os.Rename(tmpRawFile, sf.tmpFilePath); renameErr != nil {
finalErr = errors.Wrapf(renameErr, "failed to rename tmp raw file %v to file %v", tmpRawFile, sf.tmpFilePath)
return
}
}
stat, statErr := os.Stat(sf.tmpFilePath)
if statErr != nil {
finalErr = errors.Wrapf(statErr, "failed to stat tmp file %v after converting from qcow2 to raw file", sf.tmpFilePath)
return
}
sf.size = stat.Size()
sf.processedSize = stat.Size()
sf.modificationTime = stat.ModTime().UTC().String()
}
// Check if there is an existing config file then try to load the checksum
configFilePath := util.GetSyncingFileConfigFilePath(sf.filePath)
config, confReadErr := util.ReadSyncingFileConfig(configFilePath)
if confReadErr != nil {
if err = os.RemoveAll(configFilePath); err != nil {
finalErr = errors.Wrapf(err, "failed to clean up the config file %v after read failure", configFilePath)
return
}
}
if config != nil && config.ModificationTime == sf.modificationTime {
logrus.Debugf("SyncingFile: directly get the checksum from the valid config during processing wrap-up: %v", config.CurrentChecksum)
sf.currentChecksum = config.CurrentChecksum
sf.updateSyncReadyNoLock()
sf.updateVirtualSizeNoLock(sf.tmpFilePath)
sf.updateRealSizeNoLock(sf.tmpFilePath)
sf.writeConfigNoLock()
// Renaming won't change the file modification time.
if err := os.Rename(sf.tmpFilePath, sf.filePath); err != nil {
finalErr = errors.Wrapf(err, "failed to rename tmp file %v to file %v", sf.tmpFilePath, sf.filePath)
return
}
sf.log.Info("SyncingFile: succeeded processing file")
return
}
logrus.Debug("SyncingFile: failed to get the checksum from a valid config during processing wrap-up, will directly calculated it then")
// GetFileChecksum is time consuming, so we make it async as post process function to prevent client timeout,
go sf.postProcessSyncFile()
return
}
func (sf *SyncingFile) postProcessSyncFile() {
var finalErr error
defer func() {
sf.lock.Lock()
sf.handleFailureNoLock(finalErr)
sf.lock.Unlock()
}()
currentChecksum, err := util.GetFileChecksum(sf.tmpFilePath)
if err != nil {
finalErr = errors.Wrapf(err, "failed to calculate checksum for tmp file %v", sf.tmpFilePath)
return
}
sf.lock.Lock()
defer sf.lock.Unlock()
if modificationTime := util.FileModificationTime(sf.tmpFilePath); modificationTime != sf.modificationTime {
finalErr = fmt.Errorf("tmp file %v has been modified while calculaing checksum", sf.tmpFilePath)
return
}
sf.currentChecksum = currentChecksum
if sf.expectedChecksum != "" && sf.expectedChecksum != sf.currentChecksum {
finalErr = fmt.Errorf("the expected checksum %v doesn't match the file actual checksum %v", sf.expectedChecksum, sf.currentChecksum)
return
}
// If the state is already failed, there is no need to update the state