filechunks.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package filer
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "fmt"
  6. "math"
  7. "sort"
  8. "sync"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. )
  12. func TotalSize(chunks []*filer_pb.FileChunk) (size uint64) {
  13. for _, c := range chunks {
  14. t := uint64(c.Offset + int64(c.Size))
  15. if size < t {
  16. size = t
  17. }
  18. }
  19. return
  20. }
  21. func FileSize(entry *filer_pb.Entry) (size uint64) {
  22. return maxUint64(TotalSize(entry.Chunks), entry.Attributes.FileSize)
  23. }
  24. func ETag(entry *filer_pb.Entry) (etag string) {
  25. if entry.Attributes == nil || entry.Attributes.Md5 == nil {
  26. return ETagChunks(entry.Chunks)
  27. }
  28. return fmt.Sprintf("%x", entry.Attributes.Md5)
  29. }
  30. func ETagEntry(entry *Entry) (etag string) {
  31. if entry.Attr.Md5 == nil {
  32. return ETagChunks(entry.Chunks)
  33. }
  34. return fmt.Sprintf("%x", entry.Attr.Md5)
  35. }
  36. func ETagChunks(chunks []*filer_pb.FileChunk) (etag string) {
  37. if len(chunks) == 1 {
  38. return chunks[0].ETag
  39. }
  40. md5_digests := [][]byte{}
  41. for _, c := range chunks {
  42. md5_decoded, _ := hex.DecodeString(c.ETag)
  43. md5_digests = append(md5_digests, md5_decoded)
  44. }
  45. return fmt.Sprintf("%x-%d", util.Md5(bytes.Join(md5_digests, nil)), len(chunks))
  46. }
  47. func CompactFileChunks(lookupFileIdFn LookupFileIdFunctionType, chunks []*filer_pb.FileChunk) (compacted, garbage []*filer_pb.FileChunk) {
  48. visibles, _ := NonOverlappingVisibleIntervals(lookupFileIdFn, chunks)
  49. fileIds := make(map[string]bool)
  50. for _, interval := range visibles {
  51. fileIds[interval.fileId] = true
  52. }
  53. for _, chunk := range chunks {
  54. if _, found := fileIds[chunk.GetFileIdString()]; found {
  55. compacted = append(compacted, chunk)
  56. } else {
  57. garbage = append(garbage, chunk)
  58. }
  59. }
  60. return
  61. }
  62. func MinusChunks(lookupFileIdFn LookupFileIdFunctionType, as, bs []*filer_pb.FileChunk) (delta []*filer_pb.FileChunk, err error) {
  63. aData, aMeta, aErr := ResolveChunkManifest(lookupFileIdFn, as)
  64. if aErr != nil {
  65. return nil, aErr
  66. }
  67. bData, bMeta, bErr := ResolveChunkManifest(lookupFileIdFn, bs)
  68. if bErr != nil {
  69. return nil, bErr
  70. }
  71. delta = append(delta, DoMinusChunks(aData, bData)...)
  72. delta = append(delta, DoMinusChunks(aMeta, bMeta)...)
  73. return
  74. }
  75. func DoMinusChunks(as, bs []*filer_pb.FileChunk) (delta []*filer_pb.FileChunk) {
  76. fileIds := make(map[string]bool)
  77. for _, interval := range bs {
  78. fileIds[interval.GetFileIdString()] = true
  79. }
  80. for _, chunk := range as {
  81. if _, found := fileIds[chunk.GetFileIdString()]; !found {
  82. delta = append(delta, chunk)
  83. }
  84. }
  85. return
  86. }
  87. type ChunkView struct {
  88. FileId string
  89. Offset int64
  90. Size uint64
  91. LogicOffset int64 // actual offset in the file, for the data specified via [offset, offset+size) in current chunk
  92. ChunkSize uint64
  93. CipherKey []byte
  94. IsGzipped bool
  95. }
  96. func (cv *ChunkView) IsFullChunk() bool {
  97. return cv.Size == cv.ChunkSize
  98. }
  99. func ViewFromChunks(lookupFileIdFn LookupFileIdFunctionType, chunks []*filer_pb.FileChunk, offset int64, size int64) (views []*ChunkView) {
  100. visibles, _ := NonOverlappingVisibleIntervals(lookupFileIdFn, chunks)
  101. return ViewFromVisibleIntervals(visibles, offset, size)
  102. }
  103. func ViewFromVisibleIntervals(visibles []VisibleInterval, offset int64, size int64) (views []*ChunkView) {
  104. stop := offset + size
  105. if size == math.MaxInt64 {
  106. stop = math.MaxInt64
  107. }
  108. if stop < offset {
  109. stop = math.MaxInt64
  110. }
  111. for _, chunk := range visibles {
  112. chunkStart, chunkStop := max(offset, chunk.start), min(stop, chunk.stop)
  113. if chunkStart < chunkStop {
  114. views = append(views, &ChunkView{
  115. FileId: chunk.fileId,
  116. Offset: chunkStart - chunk.start + chunk.chunkOffset,
  117. Size: uint64(chunkStop - chunkStart),
  118. LogicOffset: chunkStart,
  119. ChunkSize: chunk.chunkSize,
  120. CipherKey: chunk.cipherKey,
  121. IsGzipped: chunk.isGzipped,
  122. })
  123. }
  124. }
  125. return views
  126. }
  127. func logPrintf(name string, visibles []VisibleInterval) {
  128. /*
  129. log.Infof("%s len %d", name, len(visibles))
  130. for _, v := range visibles {
  131. log.Infof("%s: [%d,%d) %s %d", name, v.start, v.stop, v.fileId, v.chunkOffset)
  132. }
  133. */
  134. }
  135. var bufPool = sync.Pool{
  136. New: func() interface{} {
  137. return new(VisibleInterval)
  138. },
  139. }
  140. func MergeIntoVisibles(visibles []VisibleInterval, chunk *filer_pb.FileChunk) (newVisibles []VisibleInterval) {
  141. newV := newVisibleInterval(chunk.Offset, chunk.Offset+int64(chunk.Size), chunk.GetFileIdString(), chunk.Mtime, 0, chunk.Size, chunk.CipherKey, chunk.IsCompressed)
  142. length := len(visibles)
  143. if length == 0 {
  144. return append(visibles, newV)
  145. }
  146. last := visibles[length-1]
  147. if last.stop <= chunk.Offset {
  148. return append(visibles, newV)
  149. }
  150. logPrintf(" before", visibles)
  151. // log.Infof("newVisibles %d adding chunk [%d,%d) %s size:%d", len(newVisibles), chunk.Offset, chunk.Offset+int64(chunk.Size), chunk.GetFileIdString(), chunk.Size)
  152. chunkStop := chunk.Offset + int64(chunk.Size)
  153. for _, v := range visibles {
  154. if v.start < chunk.Offset && chunk.Offset < v.stop {
  155. t := newVisibleInterval(v.start, chunk.Offset, v.fileId, v.modifiedTime, v.chunkOffset, v.chunkSize, v.cipherKey, v.isGzipped)
  156. newVisibles = append(newVisibles, t)
  157. // log.Infof("visible %d [%d,%d) =1> [%d,%d)", i, v.start, v.stop, t.start, t.stop)
  158. }
  159. if v.start < chunkStop && chunkStop < v.stop {
  160. t := newVisibleInterval(chunkStop, v.stop, v.fileId, v.modifiedTime, v.chunkOffset+(chunkStop-v.start), v.chunkSize, v.cipherKey, v.isGzipped)
  161. newVisibles = append(newVisibles, t)
  162. // log.Infof("visible %d [%d,%d) =2> [%d,%d)", i, v.start, v.stop, t.start, t.stop)
  163. }
  164. if chunkStop <= v.start || v.stop <= chunk.Offset {
  165. newVisibles = append(newVisibles, v)
  166. // log.Infof("visible %d [%d,%d) =3> [%d,%d)", i, v.start, v.stop, v.start, v.stop)
  167. }
  168. }
  169. newVisibles = append(newVisibles, newV)
  170. logPrintf(" append", newVisibles)
  171. for i := len(newVisibles) - 1; i >= 0; i-- {
  172. if i > 0 && newV.start < newVisibles[i-1].start {
  173. newVisibles[i] = newVisibles[i-1]
  174. } else {
  175. newVisibles[i] = newV
  176. break
  177. }
  178. }
  179. logPrintf(" sorted", newVisibles)
  180. return newVisibles
  181. }
  182. // NonOverlappingVisibleIntervals translates the file chunk into VisibleInterval in memory
  183. // If the file chunk content is a chunk manifest
  184. func NonOverlappingVisibleIntervals(lookupFileIdFn LookupFileIdFunctionType, chunks []*filer_pb.FileChunk) (visibles []VisibleInterval, err error) {
  185. chunks, _, err = ResolveChunkManifest(lookupFileIdFn, chunks)
  186. sort.Slice(chunks, func(i, j int) bool {
  187. if chunks[i].Mtime == chunks[j].Mtime {
  188. filer_pb.EnsureFid(chunks[i])
  189. filer_pb.EnsureFid(chunks[j])
  190. if chunks[i].Fid == nil || chunks[j].Fid == nil {
  191. return true
  192. }
  193. return chunks[i].Fid.FileKey < chunks[j].Fid.FileKey
  194. }
  195. return chunks[i].Mtime < chunks[j].Mtime // keep this to make tests run
  196. })
  197. for _, chunk := range chunks {
  198. // log.Infof("merge [%d,%d)", chunk.Offset, chunk.Offset+int64(chunk.Size))
  199. visibles = MergeIntoVisibles(visibles, chunk)
  200. logPrintf("add", visibles)
  201. }
  202. return
  203. }
  204. // find non-overlapping visible intervals
  205. // visible interval map to one file chunk
  206. type VisibleInterval struct {
  207. start int64
  208. stop int64
  209. modifiedTime int64
  210. fileId string
  211. chunkOffset int64
  212. chunkSize uint64
  213. cipherKey []byte
  214. isGzipped bool
  215. }
  216. func newVisibleInterval(start, stop int64, fileId string, modifiedTime int64, chunkOffset int64, chunkSize uint64, cipherKey []byte, isGzipped bool) VisibleInterval {
  217. return VisibleInterval{
  218. start: start,
  219. stop: stop,
  220. fileId: fileId,
  221. modifiedTime: modifiedTime,
  222. chunkOffset: chunkOffset, // the starting position in the chunk
  223. chunkSize: chunkSize,
  224. cipherKey: cipherKey,
  225. isGzipped: isGzipped,
  226. }
  227. }
  228. func min(x, y int64) int64 {
  229. if x <= y {
  230. return x
  231. }
  232. return y
  233. }
  234. func max(x, y int64) int64 {
  235. if x <= y {
  236. return y
  237. }
  238. return x
  239. }