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.

253 lines
8.7 KiB

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