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.

313 lines
8.4 KiB

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