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.

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