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.

345 lines
11 KiB

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