filechunks.go 7.8 KB

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