-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdxda.go
1245 lines (1074 loc) · 33.5 KB
/
dxda.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 dxda
// Some inspiration + code snippets taken from https://github.com/dnanexus/precision-fda/blob/master/go/pfda.go
//
// TODO: add more unit tests, setup deeper integration tests
//
import (
"bytes"
"context"
"crypto/md5"
"database/sql"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"runtime"
"sync"
"time"
_ "github.com/mattn/go-sqlite3" // Following canonical example on go-sqlite3 'simple.go'
)
const (
// Range for the number of threads we want to use
minNumThreads = 2
maxNumThreads = 32
numRetries = 10
numRetriesChecksumMismatch = 10
secondsInYear int = 60 * 60 * 24 * 365
)
var err error
// DXDownloadURL ...
type DXDownloadURL struct {
URL string `json:"url"`
Headers map[string]string `json:"headers"`
}
// a part to be downloaded. Can be:
// 1) part of a regular file
// 2) part of symbolic link (a web address)
type DBPart interface {
folder() string
fileId() string
fileName() string
offset() int64
project() string
size() int
}
// Part of a dnanexus file
type DBPartRegular struct {
FileId string
Project string
FileName string
Folder string
PartId int
Offset int64
Size int
MD5 string
BytesFetched int
DownloadDoneTime int64 // The time when it completed downloading
}
func (reg DBPartRegular) folder() string { return reg.Folder }
func (reg DBPartRegular) fileName() string { return reg.FileName }
func (reg DBPartRegular) fileId() string { return reg.FileId }
func (reg DBPartRegular) project() string { return reg.Project }
func (reg DBPartRegular) offset() int64 { return reg.Offset }
func (reg DBPartRegular) size() int { return reg.Size }
// symlink parts do not have checksum. There is only a
// global MD5 checksum on the entire file. There is also
// no need to get a pre-auth URL for the file
type DBPartSymlink struct {
FileId string
Project string
FileName string
Folder string
PartId int
Offset int64
Size int
BytesFetched int
DownloadDoneTime int64 // The time when it completed downloading
Url string
}
func (slnk DBPartSymlink) folder() string { return slnk.Folder }
func (slnk DBPartSymlink) fileName() string { return slnk.FileName }
func (slnk DBPartSymlink) fileId() string { return slnk.FileId }
func (slnk DBPartSymlink) project() string { return slnk.Project }
func (slnk DBPartSymlink) offset() int64 { return slnk.Offset }
func (slnk DBPartSymlink) size() int { return slnk.Size }
// JobInfo ...
type JobInfo struct {
part DBPart
url *DXDownloadURL
completeNs int64
}
// DownloadStatus ...
type DownloadStatus struct {
NumParts int64
NumBytes int64
NumPartsComplete int64
NumBytesComplete int64
// periodicity of progress report
ProgressInterval time.Duration
// Size of window in nanoseconds where to look for
// completed downloads
MaxWindowSize int64
}
type State struct {
dxEnv DXEnvironment
opts Opts
mutex sync.Mutex
db *sql.DB
ds *DownloadStatus // only the progress report thread accesses this field
timeOfLastError int
maxChunkSize int64
}
//-----------------------------------------------------------------
// Utilities to interact with the DNAnexus API
// TODO: Create automatic API wrappers for the dx toolkit
// e.g. via: https://github.com/dnanexus/dx-toolkit/tree/master/src/api_wrappers
// The user didn't specify how many threads to use, so we can
// figure out the optimal number on our own.
//
// Constraints:
// 1. Don't use more than twice the number of cores
// 2. Leave at least 1GiB of RAM for the machine
func calcNumThreads(maxChunkSize int64) int {
numCPUs := runtime.NumCPU()
hwMemoryBytes := memorySizeBytes()
fmt.Printf("number of machine cores: %d\n", numCPUs)
fmt.Printf("memory size: %d GiB\n", hwMemoryBytes/GiB)
numThreads := MinInt(numCPUs, maxNumThreads)
memoryCostPerThread := 3 * int64(maxChunkSize)
for numThreads > minNumThreads {
projectedMemoryUseBytes := memoryCostPerThread * int64(numThreads)
if projectedMemoryUseBytes < (hwMemoryBytes - GiB) {
return numThreads
}
numThreads -= 2
}
return minNumThreads
}
// Initialize the state
func NewDxDa(dxEnv DXEnvironment, fname string, optsRaw Opts) *State {
statsFname := fname + ".stats.db?_busy_timeout=60000&cache=shared&mode=rwc"
db, err := sql.Open("sqlite3", statsFname)
check(err)
db.SetMaxOpenConns(1)
// Constraints:
// 1) The chunk should be large enough to optimize the http(s) network stack
// 2) The chunk should be small enough to be able to take it in one request,
// causing minimal retries.
maxChunkSize := int64(0)
if dxEnv.DxJobId == "" {
// a remote machine, with a lower bandwith network connection
maxChunkSize = 16 * MiB
} else {
// A cloud worker with good bandwitdh
maxChunkSize = 64 * MiB
//maxChunkSize = 128 * MiB
}
// if the number of threads isn't set we
// need to calculate it and modify the options
opts := optsRaw
if opts.NumThreads == 0 {
opts.NumThreads = calcNumThreads(maxChunkSize)
}
// Limit the number of threads
fmt.Printf("Downloading files using %d threads\n", opts.NumThreads)
fmt.Printf("maximal memory chunk size: %d MiB\n", maxChunkSize/MiB)
// runtime.GOMAXPROCS(st.opts.NumThreads + 2)
return &State{
dxEnv: dxEnv,
opts: opts,
mutex: sync.Mutex{},
db: db,
ds: nil,
timeOfLastError: 0,
maxChunkSize: maxChunkSize,
}
}
func (st *State) Close() {
st.db.Close()
}
// Probably a better way to do this :)
func (st *State) queryDBIntegerResult(query string) int64 {
st.mutex.Lock()
defer st.mutex.Unlock()
rows, err := st.db.Query(query)
check(err)
var cnt int64
rows.Next()
rows.Scan(&cnt)
rows.Close()
return cnt
}
// DiskSpaceString ...
// write the number of bytes as a human readable string
func diskSpaceString(numBytes int64) string {
const KB = 1024
const MB = 1024 * KB
const GB = 1024 * MB
const TB = 1024 * GB
tb := float64(numBytes) / float64(TB)
if tb >= 1.0 {
return fmt.Sprintf("%.1fTB", tb)
}
gb := float64(numBytes) / float64(GB)
if gb >= 1.0 {
return fmt.Sprintf("%.1fGB", gb)
}
mb := float64(numBytes) / float64(MB)
if mb >= 1.0 {
return fmt.Sprintf("%.1fMB", mb)
}
return fmt.Sprintf("%dBytes", numBytes)
}
// CheckDiskSpace ...
// Check that we have enough disk space for all downloaded files
func (st *State) CheckDiskSpace() error {
// Calculate total disk space required. To get an accurate number,
// query the database, and sum the space for missing pieces.
//
totalSizeBytes :=
st.queryDBIntegerResult("SELECT SUM(size) FROM manifest_regular_stats WHERE bytes_fetched != size") +
st.queryDBIntegerResult("SELECT SUM(size) FROM manifest_symlink_stats WHERE bytes_fetched != size")
// Find how much local disk space is available
wd, err := os.Getwd()
if err != nil {
return err
}
// Get available bytes on the system
availableBytes, err := getAvailableBytes(wd)
if err != nil {
return err
}
if availableBytes < totalSizeBytes {
desc := fmt.Sprintf("Not enough disk space, available = %s, required = %s",
diskSpaceString(availableBytes),
diskSpaceString(totalSizeBytes))
return errors.New(desc)
}
diskSpaceStr := fmt.Sprintf("Required disk space = %s, available = %s\n",
diskSpaceString(totalSizeBytes),
diskSpaceString(availableBytes))
PrintLogAndOut(diskSpaceStr)
return nil
}
func (st *State) addRegularFileToTable(txn *sql.Tx, f DXFileRegular) {
offset := int64(0)
for _, p := range f.Parts {
sqlStmt := fmt.Sprintf(`
INSERT INTO manifest_regular_stats
VALUES ('%s', '%s', '%s', '%s', %d, '%d', '%d', '%s', '%d', '%d');
`,
f.Id, f.ProjId, f.Name, f.Folder, p.Id, offset, p.Size, p.MD5, 0, 0)
_, err := txn.Exec(sqlStmt)
check(err)
offset += int64(p.Size)
}
}
func (st *State) addSymlinkToTable(txn *sql.Tx, slnk DXFileSymlink) {
// split the symbolic link into chunks, and download several in parallel
offset := int64(0)
pId := 1
for offset < slnk.Size {
// make sure we don't go over the file size
endOfs := MinInt64(offset+st.maxChunkSize, slnk.Size)
partLen := endOfs - offset
if partLen <= 0 {
panic(fmt.Sprintf("part length could not be zero or less (%d)", partLen))
}
sqlStmt := fmt.Sprintf(`
INSERT INTO manifest_symlink_stats
VALUES ('%s', '%s', '%s', '%s', '%d', '%d', '%d', '%d', '%d');
`,
slnk.Id, slnk.ProjId, slnk.Name, slnk.Folder, pId, offset, partLen, 0, 0)
_, err := txn.Exec(sqlStmt)
check(err)
offset += partLen
pId += 1
}
// add to global table
sqlStmt := fmt.Sprintf(`
INSERT INTO symlinks
VALUES ('%s', '%s', '%s', '%s', '%d', '%s');
`,
slnk.Folder, slnk.Id, slnk.ProjId, slnk.Name, slnk.Size, slnk.MD5)
_, err := txn.Exec(sqlStmt)
check(err)
}
// Read the manifest file, and build a database with an empty state
// for each part in each file.
func (st *State) CreateManifestDB(manifest Manifest, fname string) {
statsFname := fname + ".stats.db?_busy_timeout=60000&cache=shared&mode=rwc"
os.Remove(statsFname)
// db, err := sql.Open("sqlite3", statsFname)
// check(err)
// defer db.Close()
sqlStmt := `
CREATE TABLE manifest_regular_stats (
file_id text,
project text,
name text,
folder text,
part_id integer,
offset integer,
size integer,
md5 text,
bytes_fetched integer,
download_done_time integer
);
`
_, err = st.db.Exec(sqlStmt)
check(err)
// symbolic link parts do not have md5 checksums
sqlStmt = `
CREATE TABLE manifest_symlink_stats (
file_id text,
project text,
name text,
folder text,
part_id integer,
offset integer,
size integer,
bytes_fetched integer,
download_done_time integer
);
`
_, err = st.db.Exec(sqlStmt)
check(err)
// create a table for all the symlinks. This is the place
// to record their overall file checksum. We can't put it
// in the per-chunk table.
sqlStmt = `
CREATE TABLE symlinks (
folder text,
id text,
proj_id text,
name text,
size integer,
md5 text
);
`
_, err = st.db.Exec(sqlStmt)
check(err)
txn, err := st.db.Begin()
check(err)
// Regular files
for _, f := range manifest.Files {
switch f.(type) {
case DXFileRegular:
st.addRegularFileToTable(txn, f.(DXFileRegular))
case DXFileSymlink:
st.addSymlinkToTable(txn, f.(DXFileSymlink))
}
}
err = txn.Commit()
check(err)
// TODO Log network settings and other helpful info for debugging
PrintLogAndOut("Preparing files for download\n")
st.PrepareFilesForDownload(manifest)
}
// create an empty file for each download filepath.
//
// TODO: Optimize this for only files that need to be downloaded
func (st *State) PrepareFilesForDownload(m Manifest) {
for _, f := range m.Files {
// Create directory structure and initialize file if it doesn't exist
wd, err := os.Getwd()
check(err)
folder := filepath.Join(wd, f.folder())
fname := filepath.Join(folder, f.name())
if _, err := os.Stat(fname); os.IsNotExist(err) {
err := os.MkdirAll(folder, 0777)
check(err)
localf, err := os.Create(fname)
check(err)
localf.Close()
}
}
}
// InitDownloadStatus ...
func (st *State) InitDownloadStatus() {
// total amounts to download, calculated once
numParts :=
st.queryDBIntegerResult("SELECT COUNT(*) FROM manifest_regular_stats") +
st.queryDBIntegerResult("SELECT COUNT(*) FROM manifest_symlink_stats")
numBytes :=
st.queryDBIntegerResult("SELECT SUM(size) FROM manifest_regular_stats") +
st.queryDBIntegerResult("SELECT SUM(size) FROM manifest_symlink_stats")
progressIntervalSec := 0
if st.dxEnv.DxJobId == "" {
// interactive use, we want to see frequent updates
progressIntervalSec = 5
} else {
// Non interactive use: we don't want to fill the logs with
// too many messages
progressIntervalSec = 15
}
st.ds = &DownloadStatus{
NumParts: numParts,
NumBytes: numBytes,
NumPartsComplete: 0,
NumBytesComplete: 0,
ProgressInterval: time.Duration(progressIntervalSec) * time.Second,
MaxWindowSize: int64(2 * 60 * 1000 * 1000 * 1000),
}
if st.opts.Verbose {
log.Printf("init download status %v\n", st.ds)
}
}
// Calculate bandwidth in MB/sec. Query the database, and find
// all recently completed downloaded parts.
func (st *State) calcBandwidth(timeWindowNanoSec int64) float64 {
if timeWindowNanoSec < 1000 {
// sanity check; if the interval is very short, just return zero.
return 0.0
}
// Time and time window measured in nano seconds
now := time.Now().UnixNano()
lowerBound := now - timeWindowNanoSec
queryReg := fmt.Sprintf(
"SELECT SUM(bytes_fetched) FROM manifest_regular_stats WHERE download_done_time > %d",
lowerBound)
regBytesDownloadedInTimeWindow := st.queryDBIntegerResult(queryReg)
querySlnk := fmt.Sprintf(
"SELECT SUM(bytes_fetched) FROM manifest_symlink_stats WHERE download_done_time > %d",
lowerBound)
slnkBytesDownloadedInTimeWindow := st.queryDBIntegerResult(querySlnk)
bytesDownloadedInTimeWindow := regBytesDownloadedInTimeWindow + slnkBytesDownloadedInTimeWindow
// convert to megabytes downloaded divided by seconds
mbDownloaded := float64(bytesDownloadedInTimeWindow) / float64(1024*1024)
timeDeltaSec := float64(timeWindowNanoSec) / float64(1000*1000*1000)
return mbDownloaded / timeDeltaSec
}
func bytes2MiB(bytes int64) int64 {
return bytes / MiB
}
// DownloadProgressOneTime ...
// Report on progress so far
func (st *State) DownloadProgressOneTime(timeWindowNanoSec int64) string {
// query the current progress
st.ds.NumBytesComplete =
st.queryDBIntegerResult("SELECT SUM(bytes_fetched) FROM manifest_regular_stats WHERE bytes_fetched = size") +
st.queryDBIntegerResult("SELECT SUM(bytes_fetched) FROM manifest_symlink_stats WHERE bytes_fetched = size")
st.ds.NumPartsComplete =
st.queryDBIntegerResult("SELECT COUNT(*) FROM manifest_regular_stats WHERE bytes_fetched = size") +
st.queryDBIntegerResult("SELECT COUNT(*) FROM manifest_symlink_stats WHERE bytes_fetched = size")
// calculate bandwitdh
bandwidthMBSec := st.calcBandwidth(timeWindowNanoSec)
// report on GC statistics
gcReport := ""
if st.opts.GcInfo {
var gcStats runtime.MemStats
runtime.ReadMemStats(&gcStats)
crntAlloc := int64(gcStats.Alloc)
totalAlloc := int64(gcStats.TotalAlloc)
pauseNs := gcStats.PauseTotalNs
numGcCycles := gcStats.NumGC
gcReport = fmt.Sprintf(" GC (alloc=%d/%d MB, pause=%d ms, #cycles=%d)",
bytes2MiB(crntAlloc), bytes2MiB(totalAlloc),
pauseNs/1e6, numGcCycles)
}
desc := fmt.Sprintf("Downloaded %d/%d MB\t%d/%d Parts (~%.1f MB/s written to disk estimated over the last %ds)%s",
bytes2MiB(st.ds.NumBytesComplete), bytes2MiB(st.ds.NumBytes),
st.ds.NumPartsComplete, st.ds.NumParts,
bandwidthMBSec,
timeWindowNanoSec/1e9,
gcReport)
return desc
}
// A loop that reports on download progress periodically.
func (st *State) downloadProgressContinuous(wg *sync.WaitGroup) {
// Start time of the measurements, in nano seconds
startTime := time.Now()
lastReportTs := startTime
// only the progress-report thread has access to the "ds" state
// field. That is why no locking is required here.
for true {
// Sleep for a number of seconds, so as to not flood the screen
// with messages. This also substantially limits the number
// of database queries.
time.Sleep(1 * time.Second)
if st.ds.NumPartsComplete >= st.ds.NumParts {
// signal that the thread is done
wg.Done()
return
}
now := time.Now()
if now.Before(lastReportTs.Add(st.ds.ProgressInterval)) {
continue
}
lastReportTs = now
// If we just started the measurements, we have a short
// history to examine. Limit the window size accordingly.
deltaNanoSec := now.UnixNano() - startTime.UnixNano()
if deltaNanoSec > st.ds.MaxWindowSize {
deltaNanoSec = st.ds.MaxWindowSize
}
desc := st.DownloadProgressOneTime(deltaNanoSec)
if st.dxEnv.DxJobId == "" {
// running on a console, erase the previous line
// TODO: Get rid of this temporary space-padding fix for carriage returns
fmt.Printf(" \r")
fmt.Printf("%s\r", desc)
} else {
// We are on a dx-job, and we want to see the history of printouts
// Note: the "\r" character causes problems in job logs, so do not use it.
fmt.Printf("%s\n", desc)
}
}
}
// Download part of a symlink
func (st *State) downloadSymlinkPart(
httpClient *http.Client,
p DBPartSymlink,
u DXDownloadURL,
memoryBuf []byte) error {
if st.opts.Verbose {
log.Printf("downloadSymlinkPart %v %v\n", p, u)
}
fname := fmt.Sprintf(".%s/%s", p.folder(), p.fileName())
localf, err := os.OpenFile(fname, os.O_WRONLY, 0777)
check(err)
defer localf.Close()
headers := make(map[string]string)
headers["Range"] = fmt.Sprintf("bytes=%d-%d", p.offset(), p.offset()+int64(p.size())-1)
for k, v := range u.Headers {
headers[k] = v
}
err = DxHttpRequestData(context.TODO(), httpClient, "GET", u.URL, headers, []byte("{}"), p.Size, memoryBuf)
check(err)
body := memoryBuf[:p.Size]
_, err = localf.WriteAt(body, p.offset())
check(err)
return nil
}
// Download part of a file and verify its checksum in memory
//
func (st *State) downloadRegPartCheckSum(
httpClient *http.Client,
p DBPartRegular,
u DXDownloadURL,
memoryBuf []byte) (bool, error) {
if st.opts.Verbose {
log.Printf("downloadRegPart %v %v\n", p, u)
}
fname := fmt.Sprintf(".%s/%s", p.folder(), p.fileName())
localf, err := os.OpenFile(fname, os.O_WRONLY, 0777)
check(err)
defer localf.Close()
// compute the checksum as we go
hasher := md5.New()
// loop through the part, reading in chunk pieces
endPart := p.Offset + int64(p.Size) - 1
for ofs := p.Offset; ofs <= endPart; ofs += st.maxChunkSize {
chunkEnd := MinInt64(ofs+st.maxChunkSize-1, endPart)
chunkSize := int(chunkEnd - ofs + 1)
headers := make(map[string]string)
headers["Range"] = fmt.Sprintf("bytes=%d-%d", ofs, chunkEnd)
for k, v := range u.Headers {
headers[k] = v
}
err := DxHttpRequestData(context.TODO(), httpClient, "GET", u.URL, headers, []byte("{}"), chunkSize, memoryBuf)
check(err)
body := memoryBuf[:chunkSize]
// write to disk
_, err = localf.WriteAt(body, ofs)
check(err)
// update the checksum
_, err = io.Copy(hasher, bytes.NewReader(body))
check(err)
}
// verify the checksum
diskSum := hex.EncodeToString(hasher.Sum(nil))
if diskSum != p.MD5 {
return false, nil
}
return true, nil
}
func (st *State) downloadRegPart(
httpClient *http.Client,
p DBPartRegular,
u DXDownloadURL,
memoryBuf []byte) error {
for i := 0; i < numRetriesChecksumMismatch; i++ {
ok, err := st.downloadRegPartCheckSum(httpClient, p, u, memoryBuf)
if err != nil {
return err
}
if ok {
return nil
}
log.Printf("MD5 string of part Id %d does not match stored MD5sum. Retrying.", p.PartId)
}
return fmt.Errorf("MD5 checksum mismatch for part %d url=%s. Gave up after %d attempts",
p.PartId, u.URL, numRetriesChecksumMismatch)
}
// create a download url if one doesn't exist
func (st *State) createURL(p DBPart, urls map[string]DXDownloadURL, httpClient *http.Client) *DXDownloadURL {
var u DXDownloadURL
// check if we already have it
u, ok := urls[p.fileId()]
if ok {
return &u
}
// a regular DNAx file. Requires generating a pre-authenticated download URL.
payload := fmt.Sprintf("{\"project\": \"%s\", \"duration\": %d}",
p.project(), secondsInYear)
body, err := DxAPI(
context.TODO(),
httpClient,
numRetries,
&st.dxEnv,
fmt.Sprintf("%s/download", p.fileId()),
payload)
check(err)
if err := json.Unmarshal(body, &u); err != nil {
log.Printf(err.Error())
panic("Could not unmarshal response from dnanexus for download URL")
}
// record the pre-auth URL so we don't have to create it again
urls[p.fileId()] = u
return &u
}
// A thread that adds pre-authenticated urls to each jobs.
//
func (st *State) preauthUrlsWorker(jobs <-chan JobInfo, jobsWithUrls chan JobInfo, dxEnv *DXEnvironment) {
httpClient := NewHttpClient()
urls := make(map[string]DXDownloadURL)
for j := range jobs {
switch j.part.(type) {
case DBPartRegular:
p := j.part.(DBPartRegular)
// If running in a job and a download URI has been set in the execution environment
// skip file-xxxx/download call to reduce system load
if dxEnv.DxJobId != "" && os.Getenv("DX_DXDA_DOWNLOAD_URI") != "" {
var u DXDownloadURL
u.URL = os.Getenv("DX_DXDA_DOWNLOAD_URI") + p.fileId() + "/" + p.project()
headers := make(map[string]string)
headers["X-Authorization"] = dxEnv.Token
u.Headers = headers
j.url = &u
} else {
j.url = st.createURL(p, urls, httpClient)
}
case DBPartSymlink:
pLnk := j.part.(DBPartSymlink)
j.url = st.createURL(pLnk, urls, httpClient)
}
jobsWithUrls <- j
}
// we are done adding URLs to each job.
close(jobsWithUrls)
}
func (st *State) worker(id int, jobsWithUrls <-chan JobInfo, jobsDbUpdate chan JobInfo, wg *sync.WaitGroup) {
// Create one http client per worker. This should, hopefully, allow
// caching open TCP/HTTP connections, reducing startup times.
httpClient := NewHttpClient()
memoryBuf := make([]byte, st.maxChunkSize)
for j := range jobsWithUrls {
switch j.part.(type) {
case DBPartRegular:
p := j.part.(DBPartRegular)
st.downloadRegPart(httpClient, p, *j.url, memoryBuf)
case DBPartSymlink:
pLnk := j.part.(DBPartSymlink)
st.downloadSymlinkPart(httpClient, pLnk, *j.url, memoryBuf)
}
// move the jobs to the next phase, which is updating the database
j.completeNs = time.Now().UnixNano()
jobsDbUpdate <- j
}
wg.Done()
}
func (st *State) dbApplyBulkUpdates(completedJobs []JobInfo) {
if len(completedJobs) == 0 {
return
}
st.mutex.Lock()
defer st.mutex.Unlock()
txn, err := st.db.Begin()
check(err)
defer txn.Commit()
for _, j := range completedJobs {
st.updateDBPart(txn, j.part, j.completeNs)
}
}
// update the database when a job completes
// Do this in bulk
func (st *State) dbUpdateWorker(jobsDbUpdate <-chan JobInfo, wg *sync.WaitGroup) {
var accu []JobInfo
for j := range jobsDbUpdate {
accu = append(accu, j)
if len(accu) == 10 {
st.dbApplyBulkUpdates(accu)
accu = make([]JobInfo, 0)
}
}
st.dbApplyBulkUpdates(accu)
wg.Done()
}
// Download all the files that are mentioned in the manifest.
func (st *State) DownloadManifestDB(fname string) {
if st.opts.Verbose {
log.Printf("DownloadManifestDB %s\n", fname)
}
st.timeOfLastError = time.Now().Second()
// build a job-channel that will hold all the parts. If we make it too small,
// we will block before creating the worker threads.
cntReg := st.queryDBIntegerResult("SELECT COUNT(*) FROM manifest_regular_stats WHERE bytes_fetched != size")
cntSlnk := st.queryDBIntegerResult("SELECT COUNT(*) FROM manifest_symlink_stats WHERE bytes_fetched != size")
totNumJobs := cntReg + cntSlnk
jobs := make(chan JobInfo, totNumJobs)
// create a job for each incomplete data file part
rows, err := st.db.Query("SELECT * FROM manifest_regular_stats WHERE bytes_fetched != size")
check(err)
numRows := 0
for rows.Next() {
var p DBPartRegular
err := rows.Scan(&p.FileId, &p.Project, &p.FileName, &p.Folder, &p.PartId, &p.Offset,
&p.Size, &p.MD5, &p.BytesFetched, &p.DownloadDoneTime)
check(err)
j := JobInfo{
part: p,
url: nil,
}
jobs <- j
numRows++
}
rows.Close()
if st.opts.Verbose {
log.Printf("There are %d regular file pieces\n", numRows)
}
// create a job for each imcomplete data symlink part
rows, err = st.db.Query("SELECT * FROM manifest_symlink_stats WHERE bytes_fetched != size")
check(err)
for rows.Next() {
var p DBPartSymlink
err = rows.Scan(&p.FileId, &p.Project, &p.FileName, &p.Folder, &p.PartId, &p.Offset,
&p.Size, &p.BytesFetched, &p.DownloadDoneTime)
check(err)
j := JobInfo{
part: p,
url: nil,
}
jobs <- j
}
rows.Close()
// Close the job channel, there will be no more jobs.
close(jobs)
// the preauth thread adds a valid URL to each job.
jobsWithUrls := make(chan JobInfo, totNumJobs)
go st.preauthUrlsWorker(jobs, jobsWithUrls, &st.dxEnv)
// the db-update thread updates the database when jobs
// complete.
var wgDb sync.WaitGroup
jobsDbUpdate := make(chan JobInfo, totNumJobs)
wgDb.Add(1)
go st.dbUpdateWorker(jobsDbUpdate, &wgDb)
// start concurrent workers to download the file parts
var wgDownload sync.WaitGroup
for w := 1; w <= st.opts.NumThreads; w++ {
wgDownload.Add(1)
go st.worker(w, jobsWithUrls, jobsDbUpdate, &wgDownload)
}
var wgProgressReport sync.WaitGroup
wgProgressReport.Add(1)
st.InitDownloadStatus()
go st.downloadProgressContinuous(&wgProgressReport)
// wait for downloads to complete
wgDownload.Wait()
close(jobsDbUpdate)
// wait for database updates to complete
wgDb.Wait()
// wait for progress report thread
wgProgressReport.Wait()
// completed all downloads
PrintLogAndOut(st.DownloadProgressOneTime(60*1000*1000*1000) + "\n")
PrintLogAndOut("Download completed successfully.\n")
PrintLogAndOut("To perform additional post-download integrity checks, please use the 'inspect' subcommand.\n")
}
// UpdateDBPart.
func (st *State) updateDBPart(txn *sql.Tx, p DBPart, tsNanoSec int64) {
switch p.(type) {
case DBPartRegular:
reg := p.(DBPartRegular)
_, err := txn.Exec(fmt.Sprintf(
"UPDATE manifest_regular_stats SET bytes_fetched = %d, download_done_time = %d WHERE file_id = '%s' AND part_id = '%d'",
reg.Size, tsNanoSec, reg.FileId, reg.PartId))
check(err)
case DBPartSymlink:
slnk := p.(DBPartSymlink)
_, err := txn.Exec(fmt.Sprintf(
"UPDATE manifest_symlink_stats SET bytes_fetched = %d, download_done_time = %d WHERE file_id = '%s' AND part_id = '%d'",
slnk.Size, tsNanoSec, slnk.FileId, slnk.PartId))
check(err)
}
}
// There was an error in downloading a part of a file. Reset it in the
// database, so we will download it again.
func (st *State) resetDBPart(p DBPartRegular) {
st.mutex.Lock()
defer st.mutex.Unlock()
tx, err := st.db.Begin()
check(err)
defer tx.Commit()
_, err = tx.Exec(fmt.Sprintf(
"UPDATE manifest_regular_stats SET bytes_fetched = 0, download_done_time = 0 WHERE file_id = '%s' AND part_id = '%d'",
p.FileId, p.PartId))
check(err)
}
// Remove the local file, and zero out all the parts in the
// database.
func (st *State) resetRegularFile(p DBPartRegular) {
// zero out the file
wd, err := os.Getwd()
check(err)
folder := filepath.Join(wd, p.Folder)
fname := filepath.Join(folder, p.FileName)
err = os.Truncate(fname, 0)
if !os.IsNotExist(err) {
check(err)
}
st.mutex.Lock()
defer st.mutex.Unlock()
tx, err := st.db.Begin()
check(err)
defer tx.Commit()
_, err = tx.Exec(fmt.Sprintf(
"UPDATE manifest_regular_stats SET bytes_fetched = 0, download_done_time = 0 WHERE file_id = '%s'",
p.FileId))
check(err)
}
// Remove the local file, and zero out all the parts in the
// database.
func (st *State) resetSymlinkFile(slnk DXFileSymlink) {
// zero out the file
wd, err := os.Getwd()
check(err)
folder := filepath.Join(wd, slnk.Folder)
fname := filepath.Join(folder, slnk.Name)
err = os.Truncate(fname, 0)
if !os.IsNotExist(err) {
check(err)
}
st.mutex.Lock()
defer st.mutex.Unlock()
tx, err := st.db.Begin()
check(err)
defer tx.Commit()
_, err = tx.Exec(fmt.Sprintf(
"UPDATE manifest_symlink_stats SET bytes_fetched = 0, download_done_time = 0 WHERE file_id = '%s'",
slnk.Id))
check(err)
}