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.

188 lines
5.4 KiB

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