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.

191 lines
5.6 KiB

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