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.

139 lines
4.1 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 ResolveChunkManifest(lookupFileIdFn LookupFileIdFunctionType, chunks []*filer_pb.FileChunk) (dataChunks, manifestChunks []*filer_pb.FileChunk, manefestResolveErr error) {
  24. // TODO maybe parallel this
  25. for _, chunk := range chunks {
  26. if !chunk.IsChunkManifest {
  27. dataChunks = append(dataChunks, chunk)
  28. continue
  29. }
  30. // IsChunkManifest
  31. data, err := fetchChunk(lookupFileIdFn, chunk.FileId, chunk.CipherKey, chunk.IsCompressed)
  32. if err != nil {
  33. return chunks, nil, fmt.Errorf("fail to read manifest %s: %v", chunk.FileId, err)
  34. }
  35. m := &filer_pb.FileChunkManifest{}
  36. if err := proto.Unmarshal(data, m); err != nil {
  37. return chunks, nil, fmt.Errorf("fail to unmarshal manifest %s: %v", chunk.FileId, err)
  38. }
  39. manifestChunks = append(manifestChunks, chunk)
  40. // recursive
  41. dchunks, mchunks, subErr := ResolveChunkManifest(lookupFileIdFn, m.Chunks)
  42. if subErr != nil {
  43. return chunks, nil, subErr
  44. }
  45. dataChunks = append(dataChunks, dchunks...)
  46. manifestChunks = append(manifestChunks, mchunks...)
  47. }
  48. return
  49. }
  50. // TODO fetch from cache for weed mount?
  51. func fetchChunk(lookupFileIdFn LookupFileIdFunctionType, fileId string, cipherKey []byte, isGzipped bool) ([]byte, error) {
  52. urlString, err := lookupFileIdFn(fileId)
  53. if err != nil {
  54. glog.Errorf("operation LookupFileId %s failed, err: %v", fileId, err)
  55. return nil, err
  56. }
  57. var buffer bytes.Buffer
  58. err = util.ReadUrlAsStream(urlString, cipherKey, isGzipped, true, 0, 0, func(data []byte) {
  59. buffer.Write(data)
  60. })
  61. if err != nil {
  62. glog.V(0).Infof("read %s failed, err: %v", fileId, err)
  63. return nil, err
  64. }
  65. return buffer.Bytes(), nil
  66. }
  67. func MaybeManifestize(saveFunc SaveDataAsChunkFunctionType, inputChunks []*filer_pb.FileChunk) (chunks []*filer_pb.FileChunk, err error) {
  68. return doMaybeManifestize(saveFunc, inputChunks, ManifestBatch, mergeIntoManifest)
  69. }
  70. 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) {
  71. var dataChunks []*filer_pb.FileChunk
  72. for _, chunk := range inputChunks {
  73. if !chunk.IsChunkManifest {
  74. dataChunks = append(dataChunks, chunk)
  75. } else {
  76. chunks = append(chunks, chunk)
  77. }
  78. }
  79. remaining := len(dataChunks)
  80. for i := 0; i+mergeFactor <= len(dataChunks); i += mergeFactor {
  81. chunk, err := mergefn(saveFunc, dataChunks[i:i+mergeFactor])
  82. if err != nil {
  83. return dataChunks, err
  84. }
  85. chunks = append(chunks, chunk)
  86. remaining -= mergeFactor
  87. }
  88. // remaining
  89. for i := len(dataChunks) - remaining; i < len(dataChunks); i++ {
  90. chunks = append(chunks, dataChunks[i])
  91. }
  92. return
  93. }
  94. func mergeIntoManifest(saveFunc SaveDataAsChunkFunctionType, dataChunks []*filer_pb.FileChunk) (manifestChunk *filer_pb.FileChunk, err error) {
  95. // create and serialize the manifest
  96. data, serErr := proto.Marshal(&filer_pb.FileChunkManifest{
  97. Chunks: dataChunks,
  98. })
  99. if serErr != nil {
  100. return nil, fmt.Errorf("serializing manifest: %v", serErr)
  101. }
  102. minOffset, maxOffset := int64(math.MaxInt64), int64(math.MinInt64)
  103. for _, chunk := range dataChunks {
  104. if minOffset > int64(chunk.Offset) {
  105. minOffset = chunk.Offset
  106. }
  107. if maxOffset < int64(chunk.Size)+chunk.Offset {
  108. maxOffset = int64(chunk.Size) + chunk.Offset
  109. }
  110. }
  111. manifestChunk, _, _, err = saveFunc(bytes.NewReader(data), "", 0)
  112. if err != nil {
  113. return nil, err
  114. }
  115. manifestChunk.IsChunkManifest = true
  116. manifestChunk.Offset = minOffset
  117. manifestChunk.Size = uint64(maxOffset - minOffset)
  118. return
  119. }
  120. type SaveDataAsChunkFunctionType func(reader io.Reader, name string, offset int64) (chunk *filer_pb.FileChunk, collection, replication string, err error)