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.

186 lines
5.6 KiB

5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
5 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "io"
  6. "io/ioutil"
  7. "net/http"
  8. "path"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "github.com/chrislusf/seaweedfs/weed/filer2"
  13. "github.com/chrislusf/seaweedfs/weed/glog"
  14. "github.com/chrislusf/seaweedfs/weed/operation"
  15. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  16. "github.com/chrislusf/seaweedfs/weed/security"
  17. "github.com/chrislusf/seaweedfs/weed/stats"
  18. "github.com/chrislusf/seaweedfs/weed/util"
  19. )
  20. func (fs *FilerServer) autoChunk(ctx context.Context, w http.ResponseWriter, r *http.Request,
  21. replication string, collection string, dataCenter string, ttlSec int32, ttlString string, fsync bool) bool {
  22. if r.Method != "POST" {
  23. glog.V(4).Infoln("AutoChunking not supported for method", r.Method)
  24. return false
  25. }
  26. // autoChunking can be set at the command-line level or as a query param. Query param overrides command-line
  27. query := r.URL.Query()
  28. parsedMaxMB, _ := strconv.ParseInt(query.Get("maxMB"), 10, 32)
  29. maxMB := int32(parsedMaxMB)
  30. if maxMB <= 0 && fs.option.MaxMB > 0 {
  31. maxMB = int32(fs.option.MaxMB)
  32. }
  33. if maxMB <= 0 {
  34. glog.V(4).Infoln("AutoChunking not enabled")
  35. return false
  36. }
  37. glog.V(4).Infoln("AutoChunking level set to", maxMB, "(MB)")
  38. chunkSize := 1024 * 1024 * maxMB
  39. contentLength := int64(0)
  40. if contentLengthHeader := r.Header["Content-Length"]; len(contentLengthHeader) == 1 {
  41. contentLength, _ = strconv.ParseInt(contentLengthHeader[0], 10, 64)
  42. if contentLength <= int64(chunkSize) {
  43. glog.V(4).Infoln("Content-Length of", contentLength, "is less than the chunk size of", chunkSize, "so autoChunking will be skipped.")
  44. return false
  45. }
  46. }
  47. if contentLength <= 0 {
  48. glog.V(4).Infoln("Content-Length value is missing or unexpected so autoChunking will be skipped.")
  49. return false
  50. }
  51. reply, err := fs.doAutoChunk(ctx, w, r, contentLength, chunkSize, replication, collection, dataCenter, ttlSec, ttlString, fsync)
  52. if err != nil {
  53. writeJsonError(w, r, http.StatusInternalServerError, err)
  54. } else if reply != nil {
  55. writeJsonQuiet(w, r, http.StatusCreated, reply)
  56. }
  57. return true
  58. }
  59. func (fs *FilerServer) doAutoChunk(ctx context.Context, w http.ResponseWriter, r *http.Request,
  60. contentLength int64, chunkSize int32, replication string, collection string, dataCenter string, ttlSec int32, ttlString string, fsync bool) (filerResult *FilerPostResult, replyerr error) {
  61. stats.FilerRequestCounter.WithLabelValues("postAutoChunk").Inc()
  62. start := time.Now()
  63. defer func() {
  64. stats.FilerRequestHistogram.WithLabelValues("postAutoChunk").Observe(time.Since(start).Seconds())
  65. }()
  66. multipartReader, multipartReaderErr := r.MultipartReader()
  67. if multipartReaderErr != nil {
  68. return nil, multipartReaderErr
  69. }
  70. part1, part1Err := multipartReader.NextPart()
  71. if part1Err != nil {
  72. return nil, part1Err
  73. }
  74. fileName := part1.FileName()
  75. if fileName != "" {
  76. fileName = path.Base(fileName)
  77. }
  78. contentType := part1.Header.Get("Content-Type")
  79. var fileChunks []*filer_pb.FileChunk
  80. md5Hash := md5.New()
  81. var partReader = ioutil.NopCloser(io.TeeReader(part1, md5Hash))
  82. chunkOffset := int64(0)
  83. for chunkOffset < contentLength {
  84. limitedReader := io.LimitReader(partReader, int64(chunkSize))
  85. // assign one file id for one chunk
  86. fileId, urlLocation, auth, assignErr := fs.assignNewFileInfo(w, r, replication, collection, dataCenter, ttlString, fsync)
  87. if assignErr != nil {
  88. return nil, assignErr
  89. }
  90. // upload the chunk to the volume server
  91. uploadResult, uploadErr := fs.doUpload(urlLocation, w, r, limitedReader, fileName, contentType, nil, auth)
  92. if uploadErr != nil {
  93. return nil, uploadErr
  94. }
  95. // if last chunk exhausted the reader exactly at the border
  96. if uploadResult.Size == 0 {
  97. break
  98. }
  99. // Save to chunk manifest structure
  100. fileChunks = append(fileChunks, uploadResult.ToPbFileChunk(fileId, chunkOffset))
  101. glog.V(4).Infof("uploaded %s chunk %d to %s [%d,%d) of %d", fileName, len(fileChunks), fileId, chunkOffset, chunkOffset+int64(uploadResult.Size), contentLength)
  102. // reset variables for the next chunk
  103. chunkOffset = chunkOffset + int64(uploadResult.Size)
  104. // if last chunk was not at full chunk size, but already exhausted the reader
  105. if int64(uploadResult.Size) < int64(chunkSize) {
  106. break
  107. }
  108. }
  109. path := r.URL.Path
  110. if strings.HasSuffix(path, "/") {
  111. if fileName != "" {
  112. path += fileName
  113. }
  114. }
  115. glog.V(4).Infoln("saving", path)
  116. entry := &filer2.Entry{
  117. FullPath: util.FullPath(path),
  118. Attr: filer2.Attr{
  119. Mtime: time.Now(),
  120. Crtime: time.Now(),
  121. Mode: 0660,
  122. Uid: OS_UID,
  123. Gid: OS_GID,
  124. Replication: replication,
  125. Collection: collection,
  126. TtlSec: ttlSec,
  127. Mime: contentType,
  128. Md5: md5Hash.Sum(nil),
  129. },
  130. Chunks: fileChunks,
  131. }
  132. filerResult = &FilerPostResult{
  133. Name: fileName,
  134. Size: chunkOffset,
  135. }
  136. if dbErr := fs.filer.CreateEntry(ctx, entry, false, false); dbErr != nil {
  137. fs.filer.DeleteChunks(entry.Chunks)
  138. replyerr = dbErr
  139. filerResult.Error = dbErr.Error()
  140. glog.V(0).Infof("failing to write %s to filer server : %v", path, dbErr)
  141. return
  142. }
  143. return
  144. }
  145. func (fs *FilerServer) doUpload(urlLocation string, w http.ResponseWriter, r *http.Request, limitedReader io.Reader, fileName string, contentType string, pairMap map[string]string, auth security.EncodedJwt) (*operation.UploadResult, error) {
  146. stats.FilerRequestCounter.WithLabelValues("postAutoChunkUpload").Inc()
  147. start := time.Now()
  148. defer func() {
  149. stats.FilerRequestHistogram.WithLabelValues("postAutoChunkUpload").Observe(time.Since(start).Seconds())
  150. }()
  151. uploadResult, err, _ := operation.Upload(urlLocation, fileName, fs.option.Cipher, limitedReader, false, contentType, pairMap, auth)
  152. return uploadResult, err
  153. }