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.

335 lines
8.9 KiB

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