In order to achieve high concurrency, SeaweedFS tries to read and write the whole file into memory. But this would not work for large files.

The following is implemented in "weed upload" command. For 3rd party clients, here is the spec.

To support large files, SeaweedFS supports these two kinds of files:

  • Chunk File. Each chunk file is actually just normal files to SeaweedFS.
  • Chunk Manifest. A simple json file with the list of all the chunks.

This piece of code shows the json file structure:

https://github.com/chrislusf/seaweedfs/blob/master/weed/operation/chunked_file.go#L24

  1. type ChunkInfo struct {
  2. Fid string `json:"fid"`
  3. Offset int64 `json:"offset"`
  4. Size int64 `json:"size"`
  5. }
  6. type ChunkList []*ChunkInfo
  7. type ChunkManifest struct {
  8. Name string `json:"name,omitempty"`
  9. Mime string `json:"mime,omitempty"`
  10. Size int64 `json:"size,omitempty"`
  11. Chunks ChunkList `json:"chunks,omitempty"`
  12. }

When reading Chunk Manifest files, the SeaweedFS will find and send the data file based on the list of ChunkInfo.

Create new large file

SeaweedFS delegates the effort to the client side. The steps are:

  • split large files into chunks
  • upload each file chunk as usual. Save the related info into ChunkInfo struct. Each chunk can be spread onto different volumes, possibly giving faster parallel access.
  • upload the manifest file with mime type "application/json", and add url parameter "cm=true". The FileId to store the manifest file is the entry point of the large file.

Update large file

Usually we just append large files. Updating a specific chunk of file is almost the same.

The steps to append a large file:

  • upload the new file chunks as usual. Save the related info into ChunkInfo struct.
  • update the updated manifest file with mime type "application/json", and add url parameter "cm=true".

Example

  1. # split -n 2 sy.jpg // split the file into two: xxa and xxb
  2. # curl -X POST -F "file=@xaa" http://localhost:9333/submit?pretty=yes
  3. {
  4. "eTag": "809b2add",
  5. "fid": "6,1b70e99bcd",
  6. "fileName": "xaa",
  7. "fileUrl": "10.34.254.62:8080/6,1b70e99bcd",
  8. "size": 73433
  9. }
  10. # curl -X POST -F "file=@xab" http://localhost:9333/submit?pretty=yes
  11. {
  12. "eTag": "9c6ca661",
  13. "fid": "3,1c863b4563",
  14. "fileName": "xab",
  15. "fileUrl": "10.34.254.62:8080/3,1c863b4563",
  16. "size": 73433
  17. }
  18. // get one fid for manifest file
  19. # curl "10.34.254.62:9333/dir/assign?pretty=yes"
  20. {
  21. "fid": "5,1ea9c7d93e",
  22. "url": "10.34.254.62:8080",
  23. "publicUrl": "10.34.254.62:8080",
  24. "count": 1
  25. }
  26. # cat manifest.json
  27. {
  28. "name": "sy.jpg",
  29. "mime": "image/jpeg",
  30. "size": 146866,
  31. "chunks": [{
  32. "fid": "6,0100711ab7",
  33. "offset": 0,
  34. "size": 73433
  35. }, {
  36. "fid": "3,1c863b4563",
  37. "offset": 73433,
  38. "size": 73433
  39. }]
  40. }
  41. // upload the manifest file
  42. # curl -v -F "file=@manifest.json" "http://10.34.254.62:8080/5,1ea9c7d93e?cm=true&pretty=yes"
  43. * Trying 10.34.254.62...
  44. * Connected to 10.34.254.62 (10.34.254.62) port 8080 (#0)
  45. > POST /5,1ea9c7d93e?cm=true&pretty=yes HTTP/1.1
  46. > Host: 10.34.254.62:8080
  47. > User-Agent: curl/7.47.0
  48. > Accept: */*
  49. > Content-Length: 418
  50. > Expect: 100-continue
  51. > Content-Type: multipart/form-data; boundary=------------------------a872064c8f40903c
  52. >
  53. < HTTP/1.1 100 Continue
  54. < HTTP/1.1 201 Created
  55. < Content-Type: application/json
  56. < Etag: "2229f9b4"
  57. < Date: Wed, 15 Jan 2020 03:12:18 GMT
  58. < Content-Length: 66
  59. <
  60. {
  61. "name": "manifest.json",
  62. "size": 213,
  63. "eTag": "2229f9b4"
  64. }
  65. * Connection #0 to host 10.34.254.62 left intact
  66. // download the full file
  67. # curl -v "http://10.34.254.62:8080/5,1ff0fb46c9" -o out.data
  68. * Trying 10.34.254.62...
  69. % Total % Received % Xferd Average Speed Time Time Time Current
  70. Dload Upload Total Spent Left Speed
  71. 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Connected to 10.34.254.62 (10.34.254.62) port 8080 (#0)
  72. > GET /5,1ff0fb46c9 HTTP/1.1
  73. > Host: 10.34.254.62:8080
  74. > User-Agent: curl/7.47.0
  75. > Accept: */*
  76. >
  77. < HTTP/1.1 200 OK
  78. < Accept-Ranges: bytes
  79. < Content-Disposition: inline; filename="sy.jpg"
  80. < Content-Length: 146866
  81. < Content-Type: image/jpeg
  82. < Etag: "3e8ef528"
  83. < Last-Modified: Wed, 15 Jan 2020 03:32:47 GMT
  84. < X-File-Store: chunked
  85. < Date: Wed, 15 Jan 2020 03:33:04 GMT
  86. <
  87. { [7929 bytes data]
  88. 100 143k 100 143k 0 0 47.2M 0 --:--:-- --:--:-- --:--:-- 70.0M
  89. * Connection #0 to host 10.34.254.62 left intact
  90. // check md5 of the downloaded files
  91. # md5sum out.data
  92. 836eababc392419580641a7b65370e82 out.data
  93. # md5sum sy.jpg
  94. 836eababc392419580641a7b65370e82 sy.jpg

Notes

There are no particular limit in terms of chunk file size. Each chunk size does not need to be the same, even in the same file. The rule of thumb is to just being able to keep the whole chunk file in memory, and not to have too many small chunk files.

weed filer and weed mount

The filer server and the FUSE implementation that uses filer server are automatically chunking large files into smaller chunks.

The list of chunks are stored in filer storage, and managed by filer or weed mount client.