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.

196 lines
5.8 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,
  101. &filer_pb.FileChunk{
  102. FileId: fileId,
  103. Offset: chunkOffset,
  104. Size: uint64(uploadResult.Size),
  105. Mtime: time.Now().UnixNano(),
  106. ETag: uploadResult.ETag,
  107. CipherKey: uploadResult.CipherKey,
  108. IsGzipped: uploadResult.Gzip > 0,
  109. },
  110. )
  111. glog.V(4).Infof("uploaded %s chunk %d to %s [%d,%d) of %d", fileName, len(fileChunks), fileId, chunkOffset, chunkOffset+int64(uploadResult.Size), contentLength)
  112. // reset variables for the next chunk
  113. chunkOffset = chunkOffset + int64(uploadResult.Size)
  114. // if last chunk was not at full chunk size, but already exhausted the reader
  115. if int64(uploadResult.Size) < int64(chunkSize) {
  116. break
  117. }
  118. }
  119. path := r.URL.Path
  120. if strings.HasSuffix(path, "/") {
  121. if fileName != "" {
  122. path += fileName
  123. }
  124. }
  125. glog.V(4).Infoln("saving", path)
  126. entry := &filer2.Entry{
  127. FullPath: util.FullPath(path),
  128. Attr: filer2.Attr{
  129. Mtime: time.Now(),
  130. Crtime: time.Now(),
  131. Mode: 0660,
  132. Uid: OS_UID,
  133. Gid: OS_GID,
  134. Replication: replication,
  135. Collection: collection,
  136. TtlSec: ttlSec,
  137. Mime: contentType,
  138. Md5: md5Hash.Sum(nil),
  139. },
  140. Chunks: fileChunks,
  141. }
  142. filerResult = &FilerPostResult{
  143. Name: fileName,
  144. Size: chunkOffset,
  145. }
  146. if dbErr := fs.filer.CreateEntry(ctx, entry, false); dbErr != nil {
  147. fs.filer.DeleteChunks(entry.Chunks)
  148. replyerr = dbErr
  149. filerResult.Error = dbErr.Error()
  150. glog.V(0).Infof("failing to write %s to filer server : %v", path, dbErr)
  151. return
  152. }
  153. return
  154. }
  155. 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) {
  156. stats.FilerRequestCounter.WithLabelValues("postAutoChunkUpload").Inc()
  157. start := time.Now()
  158. defer func() {
  159. stats.FilerRequestHistogram.WithLabelValues("postAutoChunkUpload").Observe(time.Since(start).Seconds())
  160. }()
  161. uploadResult, err, _ := operation.Upload(urlLocation, fileName, fs.option.Cipher, limitedReader, false, contentType, pairMap, auth)
  162. return uploadResult, err
  163. }