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.

339 lines
9.1 KiB

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