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.

331 lines
8.8 KiB

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