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.

154 lines
4.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package filer2
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "math"
  7. "github.com/golang/protobuf/proto"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. )
  12. const (
  13. ManifestBatch = 1000
  14. )
  15. func HasChunkManifest(chunks []*filer_pb.FileChunk) bool {
  16. for _, chunk := range chunks {
  17. if chunk.IsChunkManifest {
  18. return true
  19. }
  20. }
  21. return false
  22. }
  23. func SeparateManifestChunks(chunks []*filer_pb.FileChunk) (manifestChunks, nonManifestChunks []*filer_pb.FileChunk) {
  24. for _, c := range chunks {
  25. if !c.IsChunkManifest {
  26. manifestChunks = append(manifestChunks, c)
  27. } else {
  28. nonManifestChunks = append(nonManifestChunks, c)
  29. }
  30. }
  31. return
  32. }
  33. func ResolveChunkManifest(lookupFileIdFn LookupFileIdFunctionType, chunks []*filer_pb.FileChunk) (dataChunks, manifestChunks []*filer_pb.FileChunk, manefestResolveErr error) {
  34. // TODO maybe parallel this
  35. for _, chunk := range chunks {
  36. if !chunk.IsChunkManifest {
  37. dataChunks = append(dataChunks, chunk)
  38. continue
  39. }
  40. // IsChunkManifest
  41. data, err := fetchChunk(lookupFileIdFn, chunk.FileId, chunk.CipherKey, chunk.IsCompressed)
  42. if err != nil {
  43. return chunks, nil, fmt.Errorf("fail to read manifest %s: %v", chunk.FileId, err)
  44. }
  45. m := &filer_pb.FileChunkManifest{}
  46. if err := proto.Unmarshal(data, m); err != nil {
  47. return chunks, nil, fmt.Errorf("fail to unmarshal manifest %s: %v", chunk.FileId, err)
  48. }
  49. manifestChunks = append(manifestChunks, chunk)
  50. // recursive
  51. filer_pb.AfterEntryDeserialization(m.Chunks)
  52. dchunks, mchunks, subErr := ResolveChunkManifest(lookupFileIdFn, m.Chunks)
  53. if subErr != nil {
  54. return chunks, nil, subErr
  55. }
  56. dataChunks = append(dataChunks, dchunks...)
  57. manifestChunks = append(manifestChunks, mchunks...)
  58. }
  59. return
  60. }
  61. // TODO fetch from cache for weed mount?
  62. func fetchChunk(lookupFileIdFn LookupFileIdFunctionType, fileId string, cipherKey []byte, isGzipped bool) ([]byte, error) {
  63. urlString, err := lookupFileIdFn(fileId)
  64. if err != nil {
  65. glog.Errorf("operation LookupFileId %s failed, err: %v", fileId, err)
  66. return nil, err
  67. }
  68. var buffer bytes.Buffer
  69. err = util.ReadUrlAsStream(urlString+"?readDeleted=true", cipherKey, isGzipped, true, 0, 0, func(data []byte) {
  70. buffer.Write(data)
  71. })
  72. if err != nil {
  73. glog.V(0).Infof("read %s failed, err: %v", fileId, err)
  74. return nil, err
  75. }
  76. return buffer.Bytes(), nil
  77. }
  78. func MaybeManifestize(saveFunc SaveDataAsChunkFunctionType, inputChunks []*filer_pb.FileChunk) (chunks []*filer_pb.FileChunk, err error) {
  79. return doMaybeManifestize(saveFunc, inputChunks, ManifestBatch, mergeIntoManifest)
  80. }
  81. func doMaybeManifestize(saveFunc SaveDataAsChunkFunctionType, inputChunks []*filer_pb.FileChunk, mergeFactor int, mergefn func(saveFunc SaveDataAsChunkFunctionType, dataChunks []*filer_pb.FileChunk) (manifestChunk *filer_pb.FileChunk, err error)) (chunks []*filer_pb.FileChunk, err error) {
  82. var dataChunks []*filer_pb.FileChunk
  83. for _, chunk := range inputChunks {
  84. if !chunk.IsChunkManifest {
  85. dataChunks = append(dataChunks, chunk)
  86. } else {
  87. chunks = append(chunks, chunk)
  88. }
  89. }
  90. remaining := len(dataChunks)
  91. for i := 0; i+mergeFactor <= len(dataChunks); i += mergeFactor {
  92. chunk, err := mergefn(saveFunc, dataChunks[i:i+mergeFactor])
  93. if err != nil {
  94. return dataChunks, err
  95. }
  96. chunks = append(chunks, chunk)
  97. remaining -= mergeFactor
  98. }
  99. // remaining
  100. for i := len(dataChunks) - remaining; i < len(dataChunks); i++ {
  101. chunks = append(chunks, dataChunks[i])
  102. }
  103. return
  104. }
  105. func mergeIntoManifest(saveFunc SaveDataAsChunkFunctionType, dataChunks []*filer_pb.FileChunk) (manifestChunk *filer_pb.FileChunk, err error) {
  106. filer_pb.BeforeEntrySerialization(dataChunks)
  107. // create and serialize the manifest
  108. data, serErr := proto.Marshal(&filer_pb.FileChunkManifest{
  109. Chunks: dataChunks,
  110. })
  111. if serErr != nil {
  112. return nil, fmt.Errorf("serializing manifest: %v", serErr)
  113. }
  114. minOffset, maxOffset := int64(math.MaxInt64), int64(math.MinInt64)
  115. for _, chunk := range dataChunks {
  116. if minOffset > int64(chunk.Offset) {
  117. minOffset = chunk.Offset
  118. }
  119. if maxOffset < int64(chunk.Size)+chunk.Offset {
  120. maxOffset = int64(chunk.Size) + chunk.Offset
  121. }
  122. }
  123. manifestChunk, _, _, err = saveFunc(bytes.NewReader(data), "", 0)
  124. if err != nil {
  125. return nil, err
  126. }
  127. manifestChunk.IsChunkManifest = true
  128. manifestChunk.Offset = minOffset
  129. manifestChunk.Size = uint64(maxOffset - minOffset)
  130. return
  131. }
  132. type SaveDataAsChunkFunctionType func(reader io.Reader, name string, offset int64) (chunk *filer_pb.FileChunk, collection, replication string, err error)