You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

362 lines
11 KiB

3 years ago
3 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
5 years ago
3 years ago
5 years ago
3 years ago
3 years ago
4 years ago
2 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
5 years ago
3 years ago
3 years ago
3 years ago
5 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
12 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package operation
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/seaweedfs/seaweedfs/weed/glog"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  9. "github.com/seaweedfs/seaweedfs/weed/security"
  10. "github.com/seaweedfs/seaweedfs/weed/stats"
  11. "github.com/seaweedfs/seaweedfs/weed/util"
  12. "io"
  13. "mime"
  14. "mime/multipart"
  15. "net"
  16. "net/http"
  17. "net/textproto"
  18. "path/filepath"
  19. "strings"
  20. "time"
  21. )
  22. type UploadOption struct {
  23. UploadUrl string
  24. Filename string
  25. Cipher bool
  26. IsInputCompressed bool
  27. MimeType string
  28. PairMap map[string]string
  29. Jwt security.EncodedJwt
  30. RetryForever bool
  31. }
  32. type UploadResult struct {
  33. Name string `json:"name,omitempty"`
  34. Size uint32 `json:"size,omitempty"`
  35. Error string `json:"error,omitempty"`
  36. ETag string `json:"eTag,omitempty"`
  37. CipherKey []byte `json:"cipherKey,omitempty"`
  38. Mime string `json:"mime,omitempty"`
  39. Gzip uint32 `json:"gzip,omitempty"`
  40. ContentMd5 string `json:"contentMd5,omitempty"`
  41. RetryCount int `json:"-"`
  42. }
  43. func (uploadResult *UploadResult) ToPbFileChunk(fileId string, offset int64) *filer_pb.FileChunk {
  44. fid, _ := filer_pb.ToFileIdObject(fileId)
  45. return &filer_pb.FileChunk{
  46. FileId: fileId,
  47. Offset: offset,
  48. Size: uint64(uploadResult.Size),
  49. Mtime: time.Now().UnixNano(),
  50. ETag: uploadResult.ContentMd5,
  51. CipherKey: uploadResult.CipherKey,
  52. IsCompressed: uploadResult.Gzip > 0,
  53. Fid: fid,
  54. }
  55. }
  56. // HTTPClient interface for testing
  57. type HTTPClient interface {
  58. Do(req *http.Request) (*http.Response, error)
  59. }
  60. var (
  61. HttpClient HTTPClient
  62. )
  63. func init() {
  64. HttpClient = &http.Client{Transport: &http.Transport{
  65. DialContext: (&net.Dialer{
  66. Timeout: 10 * time.Second,
  67. KeepAlive: 10 * time.Second,
  68. }).DialContext,
  69. MaxIdleConns: 1024,
  70. MaxIdleConnsPerHost: 1024,
  71. }}
  72. }
  73. // UploadWithRetry will retry both assigning volume request and uploading content
  74. // The option parameter does not need to specify UploadUrl and Jwt, which will come from assigning volume.
  75. func UploadWithRetry(filerClient filer_pb.FilerClient, assignRequest *filer_pb.AssignVolumeRequest, uploadOption *UploadOption, genFileUrlFn func(host, fileId string) string, reader io.Reader) (fileId string, uploadResult *UploadResult, err error, data []byte) {
  76. doUploadFunc := func() error {
  77. var host string
  78. var auth security.EncodedJwt
  79. // grpc assign volume
  80. if grpcAssignErr := filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  81. resp, assignErr := client.AssignVolume(context.Background(), assignRequest)
  82. if assignErr != nil {
  83. glog.V(0).Infof("assign volume failure %v: %v", assignRequest, assignErr)
  84. return assignErr
  85. }
  86. if resp.Error != "" {
  87. return fmt.Errorf("assign volume failure %v: %v", assignRequest, resp.Error)
  88. }
  89. fileId, auth = resp.FileId, security.EncodedJwt(resp.Auth)
  90. loc := resp.Location
  91. host = filerClient.AdjustedUrl(loc)
  92. return nil
  93. }); grpcAssignErr != nil {
  94. return fmt.Errorf("filerGrpcAddress assign volume: %v", grpcAssignErr)
  95. }
  96. uploadOption.UploadUrl = genFileUrlFn(host, fileId)
  97. uploadOption.Jwt = auth
  98. var uploadErr error
  99. uploadResult, uploadErr, data = doUpload(reader, uploadOption)
  100. return uploadErr
  101. }
  102. if uploadOption.RetryForever {
  103. util.RetryForever("uploadWithRetryForever", doUploadFunc, func(err error) (shouldContinue bool) {
  104. glog.V(0).Infof("upload content: %v", err)
  105. return true
  106. })
  107. } else {
  108. err = util.Retry("uploadWithRetry", doUploadFunc)
  109. }
  110. return
  111. }
  112. var fileNameEscaper = strings.NewReplacer(`\`, `\\`, `"`, `\"`, "\n", "")
  113. // Upload sends a POST request to a volume server to upload the content with adjustable compression level
  114. func UploadData(data []byte, option *UploadOption) (uploadResult *UploadResult, err error) {
  115. uploadResult, err = retriedUploadData(data, option)
  116. return
  117. }
  118. // Upload sends a POST request to a volume server to upload the content with fast compression
  119. func Upload(reader io.Reader, option *UploadOption) (uploadResult *UploadResult, err error, data []byte) {
  120. uploadResult, err, data = doUpload(reader, option)
  121. return
  122. }
  123. func doUpload(reader io.Reader, option *UploadOption) (uploadResult *UploadResult, err error, data []byte) {
  124. bytesReader, ok := reader.(*util.BytesReader)
  125. if ok {
  126. data = bytesReader.Bytes
  127. } else {
  128. data, err = io.ReadAll(reader)
  129. if err != nil {
  130. err = fmt.Errorf("read input: %v", err)
  131. return
  132. }
  133. }
  134. uploadResult, uploadErr := retriedUploadData(data, option)
  135. return uploadResult, uploadErr, data
  136. }
  137. func retriedUploadData(data []byte, option *UploadOption) (uploadResult *UploadResult, err error) {
  138. for i := 0; i < 3; i++ {
  139. uploadResult, err = doUploadData(data, option)
  140. if err == nil {
  141. uploadResult.RetryCount = i
  142. return
  143. } else {
  144. glog.Warningf("uploading %d to %s: %v", i, option.UploadUrl, err)
  145. }
  146. time.Sleep(time.Millisecond * time.Duration(237*(i+1)))
  147. }
  148. return
  149. }
  150. func doUploadData(data []byte, option *UploadOption) (uploadResult *UploadResult, err error) {
  151. contentIsGzipped := option.IsInputCompressed
  152. shouldGzipNow := false
  153. if !option.IsInputCompressed {
  154. if option.MimeType == "" {
  155. option.MimeType = http.DetectContentType(data)
  156. // println("detect1 mimetype to", MimeType)
  157. if option.MimeType == "application/octet-stream" {
  158. option.MimeType = ""
  159. }
  160. }
  161. if shouldBeCompressed, iAmSure := util.IsCompressableFileType(filepath.Base(option.Filename), option.MimeType); iAmSure && shouldBeCompressed {
  162. shouldGzipNow = true
  163. } else if !iAmSure && option.MimeType == "" && len(data) > 16*1024 {
  164. var compressed []byte
  165. compressed, err = util.GzipData(data[0:128])
  166. shouldGzipNow = len(compressed)*10 < 128*9 // can not compress to less than 90%
  167. }
  168. }
  169. var clearDataLen int
  170. // gzip if possible
  171. // this could be double copying
  172. clearDataLen = len(data)
  173. clearData := data
  174. if shouldGzipNow && !option.Cipher {
  175. compressed, compressErr := util.GzipData(data)
  176. // fmt.Printf("data is compressed from %d ==> %d\n", len(data), len(compressed))
  177. if compressErr == nil {
  178. data = compressed
  179. contentIsGzipped = true
  180. }
  181. } else if option.IsInputCompressed {
  182. // just to get the clear data length
  183. clearData, err = util.DecompressData(data)
  184. if err == nil {
  185. clearDataLen = len(clearData)
  186. }
  187. }
  188. if option.Cipher {
  189. // encrypt(gzip(data))
  190. // encrypt
  191. cipherKey := util.GenCipherKey()
  192. encryptedData, encryptionErr := util.Encrypt(clearData, cipherKey)
  193. if encryptionErr != nil {
  194. err = fmt.Errorf("encrypt input: %v", encryptionErr)
  195. return
  196. }
  197. // upload data
  198. uploadResult, err = upload_content(func(w io.Writer) (err error) {
  199. _, err = w.Write(encryptedData)
  200. return
  201. }, len(encryptedData), &UploadOption{
  202. UploadUrl: option.UploadUrl,
  203. Filename: "",
  204. Cipher: false,
  205. IsInputCompressed: false,
  206. MimeType: "",
  207. PairMap: nil,
  208. Jwt: option.Jwt,
  209. })
  210. if uploadResult == nil {
  211. return
  212. }
  213. uploadResult.Name = option.Filename
  214. uploadResult.Mime = option.MimeType
  215. uploadResult.CipherKey = cipherKey
  216. uploadResult.Size = uint32(clearDataLen)
  217. } else {
  218. // upload data
  219. uploadResult, err = upload_content(func(w io.Writer) (err error) {
  220. _, err = w.Write(data)
  221. return
  222. }, len(data), &UploadOption{
  223. UploadUrl: option.UploadUrl,
  224. Filename: option.Filename,
  225. Cipher: false,
  226. IsInputCompressed: contentIsGzipped,
  227. MimeType: option.MimeType,
  228. PairMap: option.PairMap,
  229. Jwt: option.Jwt,
  230. })
  231. if uploadResult == nil {
  232. return
  233. }
  234. uploadResult.Size = uint32(clearDataLen)
  235. if contentIsGzipped {
  236. uploadResult.Gzip = 1
  237. }
  238. }
  239. return uploadResult, err
  240. }
  241. func upload_content(fillBufferFunction func(w io.Writer) error, originalDataSize int, option *UploadOption) (*UploadResult, error) {
  242. buf := GetBuffer()
  243. defer PutBuffer(buf)
  244. body_writer := multipart.NewWriter(buf)
  245. h := make(textproto.MIMEHeader)
  246. filename := fileNameEscaper.Replace(option.Filename)
  247. h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, filename))
  248. h.Set("Idempotency-Key", option.UploadUrl)
  249. if option.MimeType == "" {
  250. option.MimeType = mime.TypeByExtension(strings.ToLower(filepath.Ext(option.Filename)))
  251. }
  252. if option.MimeType != "" {
  253. h.Set("Content-Type", option.MimeType)
  254. }
  255. if option.IsInputCompressed {
  256. h.Set("Content-Encoding", "gzip")
  257. }
  258. file_writer, cp_err := body_writer.CreatePart(h)
  259. if cp_err != nil {
  260. glog.V(0).Infoln("error creating form file", cp_err.Error())
  261. return nil, cp_err
  262. }
  263. if err := fillBufferFunction(file_writer); err != nil {
  264. glog.V(0).Infoln("error copying data", err)
  265. return nil, err
  266. }
  267. content_type := body_writer.FormDataContentType()
  268. if err := body_writer.Close(); err != nil {
  269. glog.V(0).Infoln("error closing body", err)
  270. return nil, err
  271. }
  272. req, postErr := http.NewRequest("POST", option.UploadUrl, bytes.NewReader(buf.Bytes()))
  273. if postErr != nil {
  274. glog.V(1).Infof("create upload request %s: %v", option.UploadUrl, postErr)
  275. return nil, fmt.Errorf("create upload request %s: %v", option.UploadUrl, postErr)
  276. }
  277. req.Header.Set("Content-Type", content_type)
  278. for k, v := range option.PairMap {
  279. req.Header.Set(k, v)
  280. }
  281. if option.Jwt != "" {
  282. req.Header.Set("Authorization", "BEARER "+string(option.Jwt))
  283. }
  284. // print("+")
  285. resp, post_err := HttpClient.Do(req)
  286. defer util.CloseResponse(resp)
  287. if post_err != nil {
  288. if strings.Contains(post_err.Error(), "connection reset by peer") ||
  289. strings.Contains(post_err.Error(), "use of closed network connection") {
  290. glog.V(1).Infof("repeat error upload request %s: %v", option.UploadUrl, postErr)
  291. stats.FilerRequestCounter.WithLabelValues(stats.RepeatErrorUploadContent).Inc()
  292. resp, post_err = HttpClient.Do(req)
  293. defer util.CloseResponse(resp)
  294. }
  295. }
  296. if post_err != nil {
  297. return nil, fmt.Errorf("upload %s %d bytes to %v: %v", option.Filename, originalDataSize, option.UploadUrl, post_err)
  298. }
  299. // print("-")
  300. var ret UploadResult
  301. etag := getEtag(resp)
  302. if resp.StatusCode == http.StatusNoContent {
  303. ret.ETag = etag
  304. return &ret, nil
  305. }
  306. resp_body, ra_err := io.ReadAll(resp.Body)
  307. if ra_err != nil {
  308. return nil, fmt.Errorf("read response body %v: %v", option.UploadUrl, ra_err)
  309. }
  310. unmarshal_err := json.Unmarshal(resp_body, &ret)
  311. if unmarshal_err != nil {
  312. glog.Errorf("unmarshal %s: %v", option.UploadUrl, string(resp_body))
  313. return nil, fmt.Errorf("unmarshal %v: %v", option.UploadUrl, unmarshal_err)
  314. }
  315. if ret.Error != "" {
  316. return nil, fmt.Errorf("unmarshalled error %v: %v", option.UploadUrl, ret.Error)
  317. }
  318. ret.ETag = etag
  319. ret.ContentMd5 = resp.Header.Get("Content-MD5")
  320. return &ret, nil
  321. }
  322. func getEtag(r *http.Response) (etag string) {
  323. etag = r.Header.Get("ETag")
  324. if strings.HasPrefix(etag, "\"") && strings.HasSuffix(etag, "\"") {
  325. etag = etag[1 : len(etag)-1]
  326. }
  327. return
  328. }