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.

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