filer_server_handlers_write_autochunk.go 8.6 KB

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