forked from Azure/azure-storage-azcopy
/
xfer.go
178 lines (154 loc) · 6.01 KB
/
xfer.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
// Copyright © 2017 Microsoft <wastore@microsoft.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package ste
import (
"path/filepath"
"strings"
"sync"
"time"
"github.com/Azure/azure-pipeline-go/pipeline"
"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/nitin-deamon/azure-storage-azcopy/v10/common"
)
// upload related
const UploadMaxTries = 20
const UploadRetryDelay = time.Second * 1
const UploadMaxRetryDelay = time.Second * 60
var UploadTryTimeout = time.Minute * 15
var ADLSFlushThreshold uint32 = 7500 // The # of blocks to flush at a time-- Implemented only for CI.
// download related
const MaxRetryPerDownloadBody = 5
// TODO: consider to unify the retry options.
const DownloadTryTimeout = time.Minute * 15
const DownloadRetryDelay = time.Second * 1
const DownloadMaxRetryDelay = time.Second * 60
// pacer related
const PacerTimeToWaitInMs = 50
// CPK logging related.
// Sync.Once is used so we only log a CPK error once and prevent gumming up stdout
var cpkAccessFailureLogGLCM sync.Once
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// These types are define the STE Coordinator
type newJobXfer func(jptm IJobPartTransferMgr, pipeline pipeline.Pipeline, pacer pacer)
// same as newJobXfer, but with an extra parameter
type newJobXferWithDownloaderFactory = func(jptm IJobPartTransferMgr, pipeline pipeline.Pipeline, pacer pacer, df downloaderFactory)
type newJobXferWithSenderFactory = func(jptm IJobPartTransferMgr, pipeline pipeline.Pipeline, pacer pacer, sf senderFactory, sipf sourceInfoProviderFactory)
// Takes a multi-purpose download function, and makes it ready to user with a specific type of downloader
func parameterizeDownload(targetFunction newJobXferWithDownloaderFactory, df downloaderFactory) newJobXfer {
return func(jptm IJobPartTransferMgr, pipeline pipeline.Pipeline, pacer pacer) {
targetFunction(jptm, pipeline, pacer, df)
}
}
// Takes a multi-purpose send function, and makes it ready to use with a specific type of sender
func parameterizeSend(targetFunction newJobXferWithSenderFactory, sf senderFactory, sipf sourceInfoProviderFactory) newJobXfer {
return func(jptm IJobPartTransferMgr, pipeline pipeline.Pipeline, pacer pacer) {
targetFunction(jptm, pipeline, pacer, sf, sipf)
}
}
// the xfer factory is generated based on the type of source and destination
func computeJobXfer(fromTo common.FromTo, blobType common.BlobType) newJobXfer {
const blobFSNotS2S = "blobFS not supported as S2S source"
//local helper functions
getDownloader := func(sourceType common.Location) downloaderFactory {
switch sourceType {
case common.ELocation.Blob():
return newBlobDownloader
case common.ELocation.File():
return newAzureFilesDownloader
case common.ELocation.BlobFS():
return newBlobFSDownloader
default:
panic("unexpected source type")
}
}
getSenderFactory := func(fromTo common.FromTo) senderFactory {
isFromRemote := fromTo.From().IsRemote()
if isFromRemote {
// sending from remote = doing an S2S copy
switch fromTo.To() {
case common.ELocation.Blob(),
common.ELocation.S3(), common.ELocation.GCP():
return newURLToBlobCopier
case common.ELocation.File():
return newURLToAzureFileCopier
case common.ELocation.BlobFS():
panic(blobFSNotS2S)
default:
panic("unexpected target location type")
}
} else {
// we are uploading
switch fromTo.To() {
case common.ELocation.Blob():
return newBlobUploader
case common.ELocation.File():
return newAzureFilesUploader
case common.ELocation.BlobFS():
return newBlobFSUploader
default:
panic("unexpected target location type")
}
}
}
getSipFactory := func(sourceType common.Location) sourceInfoProviderFactory {
switch sourceType {
case common.ELocation.Local():
return newLocalSourceInfoProvider
case common.ELocation.Benchmark():
return newBenchmarkSourceInfoProvider
case common.ELocation.Blob():
return newBlobSourceInfoProvider
case common.ELocation.File():
return newFileSourceInfoProvider
case common.ELocation.BlobFS():
panic(blobFSNotS2S)
case common.ELocation.S3():
return newS3SourceInfoProvider
case common.ELocation.GCP():
return newGCPSourceInfoProvider
default:
panic("unexpected source type")
}
}
// main computeJobXfer logic
switch {
case fromTo == common.EFromTo.BlobTrash():
return DeleteBlob
case fromTo == common.EFromTo.FileTrash():
return DeleteFile
default:
if fromTo.IsDownload() {
return parameterizeDownload(remoteToLocal, getDownloader(fromTo.From()))
} else {
return parameterizeSend(anyToRemote, getSenderFactory(fromTo), getSipFactory(fromTo.From()))
}
}
}
var inferExtensions = map[string]azblob.BlobType{
".vhd": azblob.BlobPageBlob,
".vhdx": azblob.BlobPageBlob,
}
// infers a blob type from the extension specified.
func inferBlobType(filename string, defaultBlobType azblob.BlobType) azblob.BlobType {
if b, ok := inferExtensions[strings.ToLower(filepath.Ext(filename))]; ok {
return b
}
return defaultBlobType
}