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.

408 lines
12 KiB

5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
6 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("chunk").Inc()
  35. start := time.Now()
  36. defer func() {
  37. stats.FilerRequestHistogram.WithLabelValues("chunk").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. md5bytes = md5Hash.Sum(nil)
  86. filerResult, replyerr = fs.saveMetaData(ctx, r, fileName, contentType, so, md5bytes, fileChunks, chunkOffset, smallContent)
  87. return
  88. }
  89. func (fs *FilerServer) doPutAutoChunk(ctx context.Context, w http.ResponseWriter, r *http.Request, chunkSize int32, so *operation.StorageOption) (filerResult *FilerPostResult, md5bytes []byte, replyerr error) {
  90. fileName := path.Base(r.URL.Path)
  91. contentType := r.Header.Get("Content-Type")
  92. if contentType == "application/octet-stream" {
  93. contentType = ""
  94. }
  95. fileChunks, md5Hash, chunkOffset, err, smallContent := fs.uploadReaderToChunks(w, r, r.Body, chunkSize, fileName, contentType, so)
  96. if err != nil {
  97. return nil, nil, err
  98. }
  99. md5bytes = md5Hash.Sum(nil)
  100. filerResult, replyerr = fs.saveMetaData(ctx, r, fileName, contentType, so, md5bytes, fileChunks, chunkOffset, smallContent)
  101. return
  102. }
  103. func isAppend(r *http.Request) bool {
  104. return r.URL.Query().Get("op") == "append"
  105. }
  106. 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) {
  107. // detect file mode
  108. modeStr := r.URL.Query().Get("mode")
  109. if modeStr == "" {
  110. modeStr = "0660"
  111. }
  112. mode, err := strconv.ParseUint(modeStr, 8, 32)
  113. if err != nil {
  114. glog.Errorf("Invalid mode format: %s, use 0660 by default", modeStr)
  115. mode = 0660
  116. }
  117. // fix the path
  118. path := r.URL.Path
  119. if strings.HasSuffix(path, "/") {
  120. if fileName != "" {
  121. path += fileName
  122. }
  123. }
  124. var entry *filer.Entry
  125. var mergedChunks []*filer_pb.FileChunk
  126. // when it is an append
  127. if isAppend(r) {
  128. existingEntry, findErr := fs.filer.FindEntry(ctx, util.FullPath(path))
  129. if findErr != nil && findErr != filer_pb.ErrNotFound {
  130. glog.V(0).Infof("failing to find %s: %v", path, findErr)
  131. }
  132. entry = existingEntry
  133. }
  134. if entry != nil {
  135. entry.Mtime = time.Now()
  136. entry.Md5 = nil
  137. // adjust chunk offsets
  138. for _, chunk := range fileChunks {
  139. chunk.Offset += int64(entry.FileSize)
  140. }
  141. mergedChunks = append(entry.Chunks, fileChunks...)
  142. entry.FileSize += uint64(chunkOffset)
  143. // TODO
  144. if len(entry.Content) > 0 {
  145. replyerr = fmt.Errorf("append to small file is not supported yet")
  146. return
  147. }
  148. } else {
  149. glog.V(4).Infoln("saving", path)
  150. mergedChunks = fileChunks
  151. entry = &filer.Entry{
  152. FullPath: util.FullPath(path),
  153. Attr: filer.Attr{
  154. Mtime: time.Now(),
  155. Crtime: time.Now(),
  156. Mode: os.FileMode(mode),
  157. Uid: OS_UID,
  158. Gid: OS_GID,
  159. Replication: so.Replication,
  160. Collection: so.Collection,
  161. TtlSec: so.TtlSeconds,
  162. DiskType: so.DiskType,
  163. Mime: contentType,
  164. Md5: md5bytes,
  165. FileSize: uint64(chunkOffset),
  166. },
  167. Content: content,
  168. }
  169. }
  170. // maybe compact entry chunks
  171. mergedChunks, replyerr = filer.MaybeManifestize(fs.saveAsChunk(so), mergedChunks)
  172. if replyerr != nil {
  173. glog.V(0).Infof("manifestize %s: %v", r.RequestURI, replyerr)
  174. return
  175. }
  176. entry.Chunks = mergedChunks
  177. filerResult = &FilerPostResult{
  178. Name: fileName,
  179. Size: int64(entry.FileSize),
  180. }
  181. if entry.Extended == nil {
  182. entry.Extended = make(map[string][]byte)
  183. }
  184. fs.saveAmzMetaData(r, entry)
  185. for k, v := range r.Header {
  186. if len(v) > 0 && strings.HasPrefix(k, needle.PairNamePrefix) {
  187. entry.Extended[k] = []byte(v[0])
  188. }
  189. }
  190. if dbErr := fs.filer.CreateEntry(ctx, entry, false, false, nil); dbErr != nil {
  191. fs.filer.DeleteChunks(fileChunks)
  192. replyerr = dbErr
  193. filerResult.Error = dbErr.Error()
  194. glog.V(0).Infof("failing to write %s to filer server : %v", path, dbErr)
  195. }
  196. return filerResult, replyerr
  197. }
  198. 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) {
  199. var fileChunks []*filer_pb.FileChunk
  200. md5Hash := md5.New()
  201. var partReader = ioutil.NopCloser(io.TeeReader(reader, md5Hash))
  202. chunkOffset := int64(0)
  203. var smallContent []byte
  204. for {
  205. limitedReader := io.LimitReader(partReader, int64(chunkSize))
  206. data, err := ioutil.ReadAll(limitedReader)
  207. if err != nil {
  208. return nil, nil, 0, err, nil
  209. }
  210. if chunkOffset == 0 && !isAppend(r) {
  211. if len(data) < fs.option.SaveToFilerLimit || strings.HasPrefix(r.URL.Path, filer.DirectoryEtcRoot) && len(data) < 4*1024 {
  212. smallContent = data
  213. chunkOffset += int64(len(data))
  214. break
  215. }
  216. }
  217. dataReader := util.NewBytesReader(data)
  218. // retry to assign a different file id
  219. var fileId, urlLocation string
  220. var auth security.EncodedJwt
  221. var assignErr, uploadErr error
  222. var uploadResult *operation.UploadResult
  223. for i := 0; i < 3; i++ {
  224. // assign one file id for one chunk
  225. fileId, urlLocation, auth, assignErr = fs.assignNewFileInfo(so)
  226. if assignErr != nil {
  227. return nil, nil, 0, assignErr, nil
  228. }
  229. // upload the chunk to the volume server
  230. uploadResult, uploadErr, _ = fs.doUpload(urlLocation, w, r, dataReader, fileName, contentType, nil, auth)
  231. if uploadErr != nil {
  232. time.Sleep(251 * time.Millisecond)
  233. continue
  234. }
  235. break
  236. }
  237. if uploadErr != nil {
  238. return nil, nil, 0, uploadErr, nil
  239. }
  240. // if last chunk exhausted the reader exactly at the border
  241. if uploadResult.Size == 0 {
  242. break
  243. }
  244. // Save to chunk manifest structure
  245. fileChunks = append(fileChunks, uploadResult.ToPbFileChunk(fileId, chunkOffset))
  246. glog.V(4).Infof("uploaded %s chunk %d to %s [%d,%d)", fileName, len(fileChunks), fileId, chunkOffset, chunkOffset+int64(uploadResult.Size))
  247. // reset variables for the next chunk
  248. chunkOffset = chunkOffset + int64(uploadResult.Size)
  249. // if last chunk was not at full chunk size, but already exhausted the reader
  250. if int64(uploadResult.Size) < int64(chunkSize) {
  251. break
  252. }
  253. }
  254. return fileChunks, md5Hash, chunkOffset, nil, smallContent
  255. }
  256. 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) {
  257. stats.FilerRequestCounter.WithLabelValues("chunkUpload").Inc()
  258. start := time.Now()
  259. defer func() {
  260. stats.FilerRequestHistogram.WithLabelValues("chunkUpload").Observe(time.Since(start).Seconds())
  261. }()
  262. uploadResult, err, data := operation.Upload(urlLocation, fileName, fs.option.Cipher, limitedReader, false, contentType, pairMap, auth)
  263. if uploadResult != nil && uploadResult.RetryCount > 0 {
  264. stats.FilerRequestCounter.WithLabelValues("chunkUploadRetry").Add(float64(uploadResult.RetryCount))
  265. }
  266. return uploadResult, err, data
  267. }
  268. func (fs *FilerServer) saveAsChunk(so *operation.StorageOption) filer.SaveDataAsChunkFunctionType {
  269. return func(reader io.Reader, name string, offset int64) (*filer_pb.FileChunk, string, string, error) {
  270. // assign one file id for one chunk
  271. fileId, urlLocation, auth, assignErr := fs.assignNewFileInfo(so)
  272. if assignErr != nil {
  273. return nil, "", "", assignErr
  274. }
  275. // upload the chunk to the volume server
  276. uploadResult, uploadErr, _ := operation.Upload(urlLocation, name, fs.option.Cipher, reader, false, "", nil, auth)
  277. if uploadErr != nil {
  278. return nil, "", "", uploadErr
  279. }
  280. return uploadResult.ToPbFileChunk(fileId, offset), so.Collection, so.Replication, nil
  281. }
  282. }
  283. func (fs *FilerServer) mkdir(ctx context.Context, w http.ResponseWriter, r *http.Request) (filerResult *FilerPostResult, replyerr error) {
  284. // detect file mode
  285. modeStr := r.URL.Query().Get("mode")
  286. if modeStr == "" {
  287. modeStr = "0660"
  288. }
  289. mode, err := strconv.ParseUint(modeStr, 8, 32)
  290. if err != nil {
  291. glog.Errorf("Invalid mode format: %s, use 0660 by default", modeStr)
  292. mode = 0660
  293. }
  294. // fix the path
  295. path := r.URL.Path
  296. if strings.HasSuffix(path, "/") {
  297. path = path[:len(path)-1]
  298. }
  299. existingEntry, err := fs.filer.FindEntry(ctx, util.FullPath(path))
  300. if err == nil && existingEntry != nil {
  301. replyerr = fmt.Errorf("dir %s already exists", path)
  302. return
  303. }
  304. glog.V(4).Infoln("mkdir", path)
  305. entry := &filer.Entry{
  306. FullPath: util.FullPath(path),
  307. Attr: filer.Attr{
  308. Mtime: time.Now(),
  309. Crtime: time.Now(),
  310. Mode: os.FileMode(mode) | os.ModeDir,
  311. Uid: OS_UID,
  312. Gid: OS_GID,
  313. },
  314. }
  315. filerResult = &FilerPostResult{
  316. Name: util.FullPath(path).Name(),
  317. }
  318. if dbErr := fs.filer.CreateEntry(ctx, entry, false, false, nil); dbErr != nil {
  319. replyerr = dbErr
  320. filerResult.Error = dbErr.Error()
  321. glog.V(0).Infof("failing to create dir %s on filer server : %v", path, dbErr)
  322. }
  323. return filerResult, replyerr
  324. }
  325. func (fs *FilerServer) saveAmzMetaData(r *http.Request, entry *filer.Entry) {
  326. if sc := r.Header.Get(xhttp.AmzStorageClass); sc != "" {
  327. entry.Extended[xhttp.AmzStorageClass] = []byte(sc)
  328. }
  329. if tags := r.Header.Get(xhttp.AmzObjectTagging); tags != "" {
  330. for _, v := range strings.Split(tags, "&") {
  331. tag := strings.Split(v, "=")
  332. if len(tag) == 2 {
  333. entry.Extended[xhttp.AmzObjectTagging+"-"+tag[0]] = []byte(tag[1])
  334. }
  335. }
  336. }
  337. for header, values := range r.Header {
  338. if strings.HasPrefix(header, xhttp.AmzUserMetaPrefix) {
  339. for _, value := range values {
  340. entry.Extended[header] = []byte(value)
  341. }
  342. }
  343. }
  344. }