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.

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