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.

371 lines
11 KiB

5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
6 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "fmt"
  6. "hash"
  7. "io"
  8. "io/ioutil"
  9. "net/http"
  10. "os"
  11. "path"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "github.com/chrislusf/seaweedfs/weed/filer"
  16. "github.com/chrislusf/seaweedfs/weed/glog"
  17. "github.com/chrislusf/seaweedfs/weed/operation"
  18. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  19. xhttp "github.com/chrislusf/seaweedfs/weed/s3api/http"
  20. "github.com/chrislusf/seaweedfs/weed/security"
  21. "github.com/chrislusf/seaweedfs/weed/stats"
  22. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  23. "github.com/chrislusf/seaweedfs/weed/util"
  24. )
  25. func (fs *FilerServer) autoChunk(ctx context.Context, w http.ResponseWriter, r *http.Request, so *operation.StorageOption) {
  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. chunkSize := 1024 * 1024 * maxMB
  34. stats.FilerRequestCounter.WithLabelValues("postAutoChunk").Inc()
  35. start := time.Now()
  36. defer func() {
  37. stats.FilerRequestHistogram.WithLabelValues("postAutoChunk").Observe(time.Since(start).Seconds())
  38. }()
  39. var reply *FilerPostResult
  40. var err error
  41. var md5bytes []byte
  42. if r.Method == "POST" {
  43. if r.Header.Get("Content-Type") == "" && strings.HasSuffix(r.URL.Path, "/") {
  44. reply, err = fs.mkdir(ctx, w, r)
  45. } else {
  46. reply, md5bytes, err = fs.doPostAutoChunk(ctx, w, r, chunkSize, so)
  47. }
  48. } else {
  49. reply, md5bytes, err = fs.doPutAutoChunk(ctx, w, r, chunkSize, so)
  50. }
  51. if err != nil {
  52. if strings.HasPrefix(err.Error(), "read input:") {
  53. writeJsonError(w, r, 499, err)
  54. } else {
  55. writeJsonError(w, r, http.StatusInternalServerError, err)
  56. }
  57. } else if reply != nil {
  58. if len(md5bytes) > 0 {
  59. w.Header().Set("Content-MD5", util.Base64Encode(md5bytes))
  60. }
  61. writeJsonQuiet(w, r, http.StatusCreated, reply)
  62. }
  63. }
  64. func (fs *FilerServer) doPostAutoChunk(ctx context.Context, w http.ResponseWriter, r *http.Request, chunkSize int32, so *operation.StorageOption) (filerResult *FilerPostResult, md5bytes []byte, replyerr error) {
  65. multipartReader, multipartReaderErr := r.MultipartReader()
  66. if multipartReaderErr != nil {
  67. return nil, nil, multipartReaderErr
  68. }
  69. part1, part1Err := multipartReader.NextPart()
  70. if part1Err != nil {
  71. return nil, nil, part1Err
  72. }
  73. fileName := part1.FileName()
  74. if fileName != "" {
  75. fileName = path.Base(fileName)
  76. }
  77. contentType := part1.Header.Get("Content-Type")
  78. if contentType == "application/octet-stream" {
  79. contentType = ""
  80. }
  81. fileChunks, md5Hash, chunkOffset, err, smallContent := fs.uploadReaderToChunks(w, r, part1, chunkSize, fileName, contentType, so)
  82. if err != nil {
  83. return nil, nil, err
  84. }
  85. fileChunks, replyerr = filer.MaybeManifestize(fs.saveAsChunk(so), fileChunks)
  86. if replyerr != nil {
  87. glog.V(0).Infof("manifestize %s: %v", r.RequestURI, replyerr)
  88. return
  89. }
  90. md5bytes = md5Hash.Sum(nil)
  91. filerResult, replyerr = fs.saveMetaData(ctx, r, fileName, contentType, so, md5bytes, fileChunks, chunkOffset, smallContent)
  92. return
  93. }
  94. func (fs *FilerServer) doPutAutoChunk(ctx context.Context, w http.ResponseWriter, r *http.Request, chunkSize int32, so *operation.StorageOption) (filerResult *FilerPostResult, md5bytes []byte, replyerr error) {
  95. fileName := ""
  96. contentType := ""
  97. fileChunks, md5Hash, chunkOffset, err, smallContent := fs.uploadReaderToChunks(w, r, r.Body, chunkSize, fileName, contentType, so)
  98. if err != nil {
  99. return nil, nil, err
  100. }
  101. fileChunks, replyerr = filer.MaybeManifestize(fs.saveAsChunk(so), fileChunks)
  102. if replyerr != nil {
  103. glog.V(0).Infof("manifestize %s: %v", r.RequestURI, replyerr)
  104. return
  105. }
  106. md5bytes = md5Hash.Sum(nil)
  107. filerResult, replyerr = fs.saveMetaData(ctx, r, fileName, contentType, so, md5bytes, fileChunks, chunkOffset, smallContent)
  108. return
  109. }
  110. func (fs *FilerServer) saveMetaData(ctx context.Context, r *http.Request, fileName string, contentType string, so *operation.StorageOption, md5bytes []byte, fileChunks []*filer_pb.FileChunk, chunkOffset int64, content []byte) (filerResult *FilerPostResult, replyerr error) {
  111. // detect file mode
  112. modeStr := r.URL.Query().Get("mode")
  113. if modeStr == "" {
  114. modeStr = "0660"
  115. }
  116. mode, err := strconv.ParseUint(modeStr, 8, 32)
  117. if err != nil {
  118. glog.Errorf("Invalid mode format: %s, use 0660 by default", modeStr)
  119. mode = 0660
  120. }
  121. // fix the path
  122. path := r.URL.Path
  123. if strings.HasSuffix(path, "/") {
  124. if fileName != "" {
  125. path += fileName
  126. }
  127. }
  128. glog.V(4).Infoln("saving", path)
  129. entry := &filer.Entry{
  130. FullPath: util.FullPath(path),
  131. Attr: filer.Attr{
  132. Mtime: time.Now(),
  133. Crtime: time.Now(),
  134. Mode: os.FileMode(mode),
  135. Uid: OS_UID,
  136. Gid: OS_GID,
  137. Replication: so.Replication,
  138. Collection: so.Collection,
  139. TtlSec: so.TtlSeconds,
  140. Mime: contentType,
  141. Md5: md5bytes,
  142. FileSize: uint64(chunkOffset),
  143. },
  144. Chunks: fileChunks,
  145. Content: content,
  146. }
  147. filerResult = &FilerPostResult{
  148. Name: fileName,
  149. Size: chunkOffset,
  150. }
  151. if entry.Extended == nil {
  152. entry.Extended = make(map[string][]byte)
  153. }
  154. fs.saveAmzMetaData(r, entry)
  155. for k, v := range r.Header {
  156. if len(v) > 0 && strings.HasPrefix(k, needle.PairNamePrefix) {
  157. entry.Extended[k] = []byte(v[0])
  158. }
  159. }
  160. if dbErr := fs.filer.CreateEntry(ctx, entry, false, false, nil); dbErr != nil {
  161. fs.filer.DeleteChunks(entry.Chunks)
  162. replyerr = dbErr
  163. filerResult.Error = dbErr.Error()
  164. glog.V(0).Infof("failing to write %s to filer server : %v", path, dbErr)
  165. }
  166. return filerResult, replyerr
  167. }
  168. func (fs *FilerServer) uploadReaderToChunks(w http.ResponseWriter, r *http.Request, reader io.Reader, chunkSize int32, fileName, contentType string, so *operation.StorageOption) ([]*filer_pb.FileChunk, hash.Hash, int64, error, []byte) {
  169. var fileChunks []*filer_pb.FileChunk
  170. md5Hash := md5.New()
  171. var partReader = ioutil.NopCloser(io.TeeReader(reader, md5Hash))
  172. chunkOffset := int64(0)
  173. var smallContent, content []byte
  174. for {
  175. limitedReader := io.LimitReader(partReader, int64(chunkSize))
  176. data, err := ioutil.ReadAll(limitedReader)
  177. if err != nil {
  178. return nil, nil, 0, err, nil
  179. }
  180. dataReader := util.NewBytesReader(data)
  181. // retry to assign a different file id
  182. var fileId, urlLocation string
  183. var auth security.EncodedJwt
  184. var assignErr, uploadErr error
  185. var uploadResult *operation.UploadResult
  186. for i := 0; i < 3; i++ {
  187. // assign one file id for one chunk
  188. fileId, urlLocation, auth, assignErr = fs.assignNewFileInfo(so)
  189. if assignErr != nil {
  190. return nil, nil, 0, assignErr, nil
  191. }
  192. // upload the chunk to the volume server
  193. uploadResult, uploadErr, _ = fs.doUpload(urlLocation, w, r, dataReader, fileName, contentType, nil, auth)
  194. if uploadErr != nil {
  195. time.Sleep(251 * time.Millisecond)
  196. continue
  197. }
  198. break
  199. }
  200. if uploadErr != nil {
  201. return nil, nil, 0, uploadErr, nil
  202. }
  203. content = data
  204. // if last chunk exhausted the reader exactly at the border
  205. if uploadResult.Size == 0 {
  206. break
  207. }
  208. // Save to chunk manifest structure
  209. fileChunks = append(fileChunks, uploadResult.ToPbFileChunk(fileId, chunkOffset))
  210. glog.V(4).Infof("uploaded %s chunk %d to %s [%d,%d)", fileName, len(fileChunks), fileId, chunkOffset, chunkOffset+int64(uploadResult.Size))
  211. // reset variables for the next chunk
  212. chunkOffset = chunkOffset + int64(uploadResult.Size)
  213. // if last chunk was not at full chunk size, but already exhausted the reader
  214. if int64(uploadResult.Size) < int64(chunkSize) {
  215. break
  216. }
  217. }
  218. if chunkOffset < fs.option.CacheToFilerLimit || strings.HasPrefix(r.URL.Path, filer.DirectoryEtcRoot) && chunkOffset < 4*1024 {
  219. smallContent = content
  220. }
  221. return fileChunks, md5Hash, chunkOffset, nil, smallContent
  222. }
  223. 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, []byte) {
  224. stats.FilerRequestCounter.WithLabelValues("postAutoChunkUpload").Inc()
  225. start := time.Now()
  226. defer func() {
  227. stats.FilerRequestHistogram.WithLabelValues("postAutoChunkUpload").Observe(time.Since(start).Seconds())
  228. }()
  229. uploadResult, err, data := operation.Upload(urlLocation, fileName, fs.option.Cipher, limitedReader, false, contentType, pairMap, auth)
  230. return uploadResult, err, data
  231. }
  232. func (fs *FilerServer) saveAsChunk(so *operation.StorageOption) filer.SaveDataAsChunkFunctionType {
  233. return func(reader io.Reader, name string, offset int64) (*filer_pb.FileChunk, string, string, error) {
  234. // assign one file id for one chunk
  235. fileId, urlLocation, auth, assignErr := fs.assignNewFileInfo(so)
  236. if assignErr != nil {
  237. return nil, "", "", assignErr
  238. }
  239. // upload the chunk to the volume server
  240. uploadResult, uploadErr, _ := operation.Upload(urlLocation, name, fs.option.Cipher, reader, false, "", nil, auth)
  241. if uploadErr != nil {
  242. return nil, "", "", uploadErr
  243. }
  244. return uploadResult.ToPbFileChunk(fileId, offset), so.Collection, so.Replication, nil
  245. }
  246. }
  247. func (fs *FilerServer) mkdir(ctx context.Context, w http.ResponseWriter, r *http.Request) (filerResult *FilerPostResult, replyerr error) {
  248. // detect file mode
  249. modeStr := r.URL.Query().Get("mode")
  250. if modeStr == "" {
  251. modeStr = "0660"
  252. }
  253. mode, err := strconv.ParseUint(modeStr, 8, 32)
  254. if err != nil {
  255. glog.Errorf("Invalid mode format: %s, use 0660 by default", modeStr)
  256. mode = 0660
  257. }
  258. // fix the path
  259. path := r.URL.Path
  260. if strings.HasSuffix(path, "/") {
  261. path = path[:len(path)-1]
  262. }
  263. existingEntry, err := fs.filer.FindEntry(ctx, util.FullPath(path))
  264. if err == nil && existingEntry != nil {
  265. replyerr = fmt.Errorf("dir %s already exists", path)
  266. return
  267. }
  268. glog.V(4).Infoln("mkdir", path)
  269. entry := &filer.Entry{
  270. FullPath: util.FullPath(path),
  271. Attr: filer.Attr{
  272. Mtime: time.Now(),
  273. Crtime: time.Now(),
  274. Mode: os.FileMode(mode) | os.ModeDir,
  275. Uid: OS_UID,
  276. Gid: OS_GID,
  277. },
  278. }
  279. filerResult = &FilerPostResult{
  280. Name: util.FullPath(path).Name(),
  281. }
  282. if dbErr := fs.filer.CreateEntry(ctx, entry, false, false, nil); dbErr != nil {
  283. replyerr = dbErr
  284. filerResult.Error = dbErr.Error()
  285. glog.V(0).Infof("failing to create dir %s on filer server : %v", path, dbErr)
  286. }
  287. return filerResult, replyerr
  288. }
  289. func (fs *FilerServer) saveAmzMetaData(r *http.Request, entry *filer.Entry) {
  290. if sc := r.Header.Get(xhttp.AmzStorageClass); sc != "" {
  291. entry.Extended[xhttp.AmzStorageClass] = []byte(sc)
  292. }
  293. if tags := r.Header.Get(xhttp.AmzObjectTagging); tags != "" {
  294. for _, v := range strings.Split(tags, "&") {
  295. tag := strings.Split(v, "=")
  296. if len(tag) == 2 {
  297. entry.Extended[xhttp.AmzObjectTagging+"-"+tag[0]] = []byte(tag[1])
  298. }
  299. }
  300. }
  301. for header, values := range r.Header {
  302. if strings.HasPrefix(header, xhttp.AmzUserMetaPrefix) {
  303. for _, value := range values {
  304. entry.Extended[header] = []byte(value)
  305. }
  306. }
  307. }
  308. }