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.

208 lines
6.1 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package weed_server
  2. import (
  3. "bytes"
  4. "context"
  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) 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)
  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) (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. var fileChunks []*filer_pb.FileChunk
  79. totalBytesRead := int64(0)
  80. tmpBufferSize := int32(1024 * 1024)
  81. tmpBuffer := bytes.NewBuffer(make([]byte, 0, tmpBufferSize))
  82. chunkBuf := make([]byte, chunkSize+tmpBufferSize, chunkSize+tmpBufferSize) // chunk size plus a little overflow
  83. chunkBufOffset := int32(0)
  84. chunkOffset := int64(0)
  85. writtenChunks := 0
  86. filerResult = &FilerPostResult{
  87. Name: fileName,
  88. }
  89. for totalBytesRead < contentLength {
  90. tmpBuffer.Reset()
  91. bytesRead, readErr := io.CopyN(tmpBuffer, part1, int64(tmpBufferSize))
  92. readFully := readErr != nil && readErr == io.EOF
  93. tmpBuf := tmpBuffer.Bytes()
  94. bytesToCopy := tmpBuf[0:int(bytesRead)]
  95. copy(chunkBuf[chunkBufOffset:chunkBufOffset+int32(bytesRead)], bytesToCopy)
  96. chunkBufOffset = chunkBufOffset + int32(bytesRead)
  97. if chunkBufOffset >= chunkSize || readFully || (chunkBufOffset > 0 && bytesRead == 0) {
  98. writtenChunks = writtenChunks + 1
  99. fileId, urlLocation, auth, assignErr := fs.assignNewFileInfo(w, r, replication, collection, dataCenter)
  100. if assignErr != nil {
  101. return nil, assignErr
  102. }
  103. // upload the chunk to the volume server
  104. chunkName := fileName + "_chunk_" + strconv.FormatInt(int64(len(fileChunks)+1), 10)
  105. uploadErr := fs.doUpload(urlLocation, w, r, chunkBuf[0:chunkBufOffset], chunkName, "application/octet-stream", fileId, auth)
  106. if uploadErr != nil {
  107. return nil, uploadErr
  108. }
  109. // Save to chunk manifest structure
  110. fileChunks = append(fileChunks,
  111. &filer_pb.FileChunk{
  112. FileId: fileId,
  113. Offset: chunkOffset,
  114. Size: uint64(chunkBufOffset),
  115. Mtime: time.Now().UnixNano(),
  116. },
  117. )
  118. // reset variables for the next chunk
  119. chunkBufOffset = 0
  120. chunkOffset = totalBytesRead + int64(bytesRead)
  121. }
  122. totalBytesRead = totalBytesRead + int64(bytesRead)
  123. if bytesRead == 0 || readFully {
  124. break
  125. }
  126. if readErr != nil {
  127. return nil, readErr
  128. }
  129. }
  130. path := r.URL.Path
  131. if strings.HasSuffix(path, "/") {
  132. if fileName != "" {
  133. path += fileName
  134. }
  135. }
  136. glog.V(4).Infoln("saving", path)
  137. entry := &filer2.Entry{
  138. FullPath: filer2.FullPath(path),
  139. Attr: filer2.Attr{
  140. Mtime: time.Now(),
  141. Crtime: time.Now(),
  142. Mode: 0660,
  143. Uid: OS_UID,
  144. Gid: OS_GID,
  145. Replication: replication,
  146. Collection: collection,
  147. TtlSec: int32(util.ParseInt(r.URL.Query().Get("ttl"), 0)),
  148. },
  149. Chunks: fileChunks,
  150. }
  151. if dbErr := fs.filer.CreateEntry(ctx, entry); dbErr != nil {
  152. fs.filer.DeleteChunks(entry.FullPath, entry.Chunks)
  153. replyerr = dbErr
  154. filerResult.Error = dbErr.Error()
  155. glog.V(0).Infof("failing to write %s to filer server : %v", path, dbErr)
  156. return
  157. }
  158. return
  159. }
  160. func (fs *FilerServer) doUpload(urlLocation string, w http.ResponseWriter, r *http.Request,
  161. chunkBuf []byte, fileName string, contentType string, fileId string, auth security.EncodedJwt) (err error) {
  162. stats.FilerRequestCounter.WithLabelValues("postAutoChunkUpload").Inc()
  163. start := time.Now()
  164. defer func() {
  165. stats.FilerRequestHistogram.WithLabelValues("postAutoChunkUpload").Observe(time.Since(start).Seconds())
  166. }()
  167. ioReader := ioutil.NopCloser(bytes.NewBuffer(chunkBuf))
  168. uploadResult, uploadError := operation.Upload(urlLocation, fileName, ioReader, false, contentType, nil, auth)
  169. if uploadResult != nil {
  170. glog.V(0).Infoln("Chunk upload result. Name:", uploadResult.Name, "Fid:", fileId, "Size:", uploadResult.Size)
  171. }
  172. if uploadError != nil {
  173. err = uploadError
  174. }
  175. return
  176. }