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.

404 lines
12 KiB

5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 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. 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 := ""
  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. Mime: contentType,
  163. Md5: md5bytes,
  164. FileSize: uint64(chunkOffset),
  165. },
  166. Content: content,
  167. }
  168. }
  169. // maybe compact entry chunks
  170. mergedChunks, replyerr = filer.MaybeManifestize(fs.saveAsChunk(so), mergedChunks)
  171. if replyerr != nil {
  172. glog.V(0).Infof("manifestize %s: %v", r.RequestURI, replyerr)
  173. return
  174. }
  175. entry.Chunks = mergedChunks
  176. filerResult = &FilerPostResult{
  177. Name: fileName,
  178. Size: int64(entry.FileSize),
  179. }
  180. if entry.Extended == nil {
  181. entry.Extended = make(map[string][]byte)
  182. }
  183. fs.saveAmzMetaData(r, entry)
  184. for k, v := range r.Header {
  185. if len(v) > 0 && strings.HasPrefix(k, needle.PairNamePrefix) {
  186. entry.Extended[k] = []byte(v[0])
  187. }
  188. }
  189. if dbErr := fs.filer.CreateEntry(ctx, entry, false, false, nil); dbErr != nil {
  190. fs.filer.DeleteChunks(fileChunks)
  191. replyerr = dbErr
  192. filerResult.Error = dbErr.Error()
  193. glog.V(0).Infof("failing to write %s to filer server : %v", path, dbErr)
  194. }
  195. return filerResult, replyerr
  196. }
  197. 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) {
  198. var fileChunks []*filer_pb.FileChunk
  199. md5Hash := md5.New()
  200. var partReader = ioutil.NopCloser(io.TeeReader(reader, md5Hash))
  201. chunkOffset := int64(0)
  202. var smallContent []byte
  203. for {
  204. limitedReader := io.LimitReader(partReader, int64(chunkSize))
  205. data, err := ioutil.ReadAll(limitedReader)
  206. if err != nil {
  207. return nil, nil, 0, err, nil
  208. }
  209. if chunkOffset == 0 && !isAppend(r) {
  210. if len(data) < fs.option.SaveToFilerLimit || strings.HasPrefix(r.URL.Path, filer.DirectoryEtcRoot) && len(data) < 4*1024 {
  211. smallContent = data
  212. chunkOffset += int64(len(data))
  213. break
  214. }
  215. }
  216. dataReader := util.NewBytesReader(data)
  217. // retry to assign a different file id
  218. var fileId, urlLocation string
  219. var auth security.EncodedJwt
  220. var assignErr, uploadErr error
  221. var uploadResult *operation.UploadResult
  222. for i := 0; i < 3; i++ {
  223. // assign one file id for one chunk
  224. fileId, urlLocation, auth, assignErr = fs.assignNewFileInfo(so)
  225. if assignErr != nil {
  226. return nil, nil, 0, assignErr, nil
  227. }
  228. // upload the chunk to the volume server
  229. uploadResult, uploadErr, _ = fs.doUpload(urlLocation, w, r, dataReader, fileName, contentType, nil, auth)
  230. if uploadErr != nil {
  231. time.Sleep(251 * time.Millisecond)
  232. continue
  233. }
  234. break
  235. }
  236. if uploadErr != nil {
  237. return nil, nil, 0, uploadErr, nil
  238. }
  239. // if last chunk exhausted the reader exactly at the border
  240. if uploadResult.Size == 0 {
  241. break
  242. }
  243. // Save to chunk manifest structure
  244. fileChunks = append(fileChunks, uploadResult.ToPbFileChunk(fileId, chunkOffset))
  245. glog.V(4).Infof("uploaded %s chunk %d to %s [%d,%d)", fileName, len(fileChunks), fileId, chunkOffset, chunkOffset+int64(uploadResult.Size))
  246. // reset variables for the next chunk
  247. chunkOffset = chunkOffset + int64(uploadResult.Size)
  248. // if last chunk was not at full chunk size, but already exhausted the reader
  249. if int64(uploadResult.Size) < int64(chunkSize) {
  250. break
  251. }
  252. }
  253. return fileChunks, md5Hash, chunkOffset, nil, smallContent
  254. }
  255. 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) {
  256. stats.FilerRequestCounter.WithLabelValues("postAutoChunkUpload").Inc()
  257. start := time.Now()
  258. defer func() {
  259. stats.FilerRequestHistogram.WithLabelValues("postAutoChunkUpload").Observe(time.Since(start).Seconds())
  260. }()
  261. uploadResult, err, data := operation.Upload(urlLocation, fileName, fs.option.Cipher, limitedReader, false, contentType, pairMap, auth)
  262. return uploadResult, err, data
  263. }
  264. func (fs *FilerServer) saveAsChunk(so *operation.StorageOption) filer.SaveDataAsChunkFunctionType {
  265. return func(reader io.Reader, name string, offset int64) (*filer_pb.FileChunk, string, string, error) {
  266. // assign one file id for one chunk
  267. fileId, urlLocation, auth, assignErr := fs.assignNewFileInfo(so)
  268. if assignErr != nil {
  269. return nil, "", "", assignErr
  270. }
  271. // upload the chunk to the volume server
  272. uploadResult, uploadErr, _ := operation.Upload(urlLocation, name, fs.option.Cipher, reader, false, "", nil, auth)
  273. if uploadErr != nil {
  274. return nil, "", "", uploadErr
  275. }
  276. return uploadResult.ToPbFileChunk(fileId, offset), so.Collection, so.Replication, nil
  277. }
  278. }
  279. func (fs *FilerServer) mkdir(ctx context.Context, w http.ResponseWriter, r *http.Request) (filerResult *FilerPostResult, replyerr error) {
  280. // detect file mode
  281. modeStr := r.URL.Query().Get("mode")
  282. if modeStr == "" {
  283. modeStr = "0660"
  284. }
  285. mode, err := strconv.ParseUint(modeStr, 8, 32)
  286. if err != nil {
  287. glog.Errorf("Invalid mode format: %s, use 0660 by default", modeStr)
  288. mode = 0660
  289. }
  290. // fix the path
  291. path := r.URL.Path
  292. if strings.HasSuffix(path, "/") {
  293. path = path[:len(path)-1]
  294. }
  295. existingEntry, err := fs.filer.FindEntry(ctx, util.FullPath(path))
  296. if err == nil && existingEntry != nil {
  297. replyerr = fmt.Errorf("dir %s already exists", path)
  298. return
  299. }
  300. glog.V(4).Infoln("mkdir", path)
  301. entry := &filer.Entry{
  302. FullPath: util.FullPath(path),
  303. Attr: filer.Attr{
  304. Mtime: time.Now(),
  305. Crtime: time.Now(),
  306. Mode: os.FileMode(mode) | os.ModeDir,
  307. Uid: OS_UID,
  308. Gid: OS_GID,
  309. },
  310. }
  311. filerResult = &FilerPostResult{
  312. Name: util.FullPath(path).Name(),
  313. }
  314. if dbErr := fs.filer.CreateEntry(ctx, entry, false, false, nil); dbErr != nil {
  315. replyerr = dbErr
  316. filerResult.Error = dbErr.Error()
  317. glog.V(0).Infof("failing to create dir %s on filer server : %v", path, dbErr)
  318. }
  319. return filerResult, replyerr
  320. }
  321. func (fs *FilerServer) saveAmzMetaData(r *http.Request, entry *filer.Entry) {
  322. if sc := r.Header.Get(xhttp.AmzStorageClass); sc != "" {
  323. entry.Extended[xhttp.AmzStorageClass] = []byte(sc)
  324. }
  325. if tags := r.Header.Get(xhttp.AmzObjectTagging); tags != "" {
  326. for _, v := range strings.Split(tags, "&") {
  327. tag := strings.Split(v, "=")
  328. if len(tag) == 2 {
  329. entry.Extended[xhttp.AmzObjectTagging+"-"+tag[0]] = []byte(tag[1])
  330. }
  331. }
  332. }
  333. for header, values := range r.Header {
  334. if strings.HasPrefix(header, xhttp.AmzUserMetaPrefix) {
  335. for _, value := range values {
  336. entry.Extended[header] = []byte(value)
  337. }
  338. }
  339. }
  340. }