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
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. "math"
  6. "sync"
  7. "github.com/chrislusf/seaweedfs/weed/wdclient"
  8. "golang.org/x/exp/slices"
  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. if err != nil {
  207. return
  208. }
  209. visibles2 := readResolvedChunks(chunks)
  210. if true {
  211. return visibles2, err
  212. }
  213. slices.SortFunc(chunks, func(a, b *filer_pb.FileChunk) bool {
  214. if a.Mtime == b.Mtime {
  215. filer_pb.EnsureFid(a)
  216. filer_pb.EnsureFid(b)
  217. if a.Fid == nil || b.Fid == nil {
  218. return true
  219. }
  220. return a.Fid.FileKey < b.Fid.FileKey
  221. }
  222. return a.Mtime < b.Mtime
  223. })
  224. for _, chunk := range chunks {
  225. // glog.V(0).Infof("merge [%d,%d)", chunk.Offset, chunk.Offset+int64(chunk.Size))
  226. visibles = MergeIntoVisibles(visibles, chunk)
  227. logPrintf("add", visibles)
  228. }
  229. if len(visibles) != len(visibles2) {
  230. fmt.Printf("different visibles size %d : %d\n", len(visibles), len(visibles2))
  231. } else {
  232. for i := 0; i < len(visibles); i++ {
  233. checkDifference(visibles[i], visibles2[i])
  234. }
  235. }
  236. return
  237. }
  238. func checkDifference(x, y VisibleInterval) {
  239. if x.start != y.start ||
  240. x.stop != y.stop ||
  241. x.fileId != y.fileId ||
  242. x.modifiedTime != y.modifiedTime {
  243. fmt.Printf("different visible %+v : %+v\n", x, y)
  244. }
  245. }
  246. // find non-overlapping visible intervals
  247. // visible interval map to one file chunk
  248. type VisibleInterval struct {
  249. start int64
  250. stop int64
  251. modifiedTime int64
  252. fileId string
  253. chunkOffset int64
  254. chunkSize uint64
  255. cipherKey []byte
  256. isGzipped bool
  257. }
  258. func newVisibleInterval(start, stop int64, fileId string, modifiedTime int64, chunkOffset int64, chunkSize uint64, cipherKey []byte, isGzipped bool) VisibleInterval {
  259. return VisibleInterval{
  260. start: start,
  261. stop: stop,
  262. fileId: fileId,
  263. modifiedTime: modifiedTime,
  264. chunkOffset: chunkOffset, // the starting position in the chunk
  265. chunkSize: chunkSize,
  266. cipherKey: cipherKey,
  267. isGzipped: isGzipped,
  268. }
  269. }
  270. func min(x, y int64) int64 {
  271. if x <= y {
  272. return x
  273. }
  274. return y
  275. }
  276. func max(x, y int64) int64 {
  277. if x <= y {
  278. return y
  279. }
  280. return x
  281. }