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.

220 lines
5.6 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
  1. package filesys
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io"
  7. "sync"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/operation"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/chrislusf/seaweedfs/weed/security"
  13. )
  14. type ContinuousDirtyPages struct {
  15. intervals *ContinuousIntervals
  16. f *File
  17. lock sync.Mutex
  18. collection string
  19. replication string
  20. }
  21. func newDirtyPages(file *File) *ContinuousDirtyPages {
  22. return &ContinuousDirtyPages{
  23. intervals: &ContinuousIntervals{},
  24. f: file,
  25. }
  26. }
  27. func (pages *ContinuousDirtyPages) releaseResource() {
  28. }
  29. var counter = int32(0)
  30. func (pages *ContinuousDirtyPages) AddPage(offset int64, data []byte) (chunks []*filer_pb.FileChunk, err error) {
  31. pages.lock.Lock()
  32. defer pages.lock.Unlock()
  33. glog.V(3).Infof("%s AddPage [%d,%d)", pages.f.fullpath(), offset, offset+int64(len(data)))
  34. if len(data) > int(pages.f.wfs.option.ChunkSizeLimit) {
  35. // this is more than what buffer can hold.
  36. return pages.flushAndSave(offset, data)
  37. }
  38. pages.intervals.AddInterval(data, offset)
  39. var chunk *filer_pb.FileChunk
  40. var hasSavedData bool
  41. if pages.intervals.TotalSize() > pages.f.wfs.option.ChunkSizeLimit {
  42. chunk, hasSavedData, err = pages.saveExistingLargestPageToStorage()
  43. if hasSavedData {
  44. chunks = append(chunks, chunk)
  45. }
  46. }
  47. return
  48. }
  49. func (pages *ContinuousDirtyPages) flushAndSave(offset int64, data []byte) (chunks []*filer_pb.FileChunk, err error) {
  50. var chunk *filer_pb.FileChunk
  51. var newChunks []*filer_pb.FileChunk
  52. // flush existing
  53. if newChunks, err = pages.saveExistingPagesToStorage(); err == nil {
  54. if newChunks != nil {
  55. chunks = append(chunks, newChunks...)
  56. }
  57. } else {
  58. return
  59. }
  60. // flush the new page
  61. if chunk, err = pages.saveToStorage(bytes.NewReader(data), offset, int64(len(data))); err == nil {
  62. if chunk != nil {
  63. glog.V(4).Infof("%s/%s flush big request [%d,%d) to %s", pages.f.dir.FullPath(), pages.f.Name, chunk.Offset, chunk.Offset+int64(chunk.Size), chunk.FileId)
  64. chunks = append(chunks, chunk)
  65. }
  66. } else {
  67. glog.V(0).Infof("%s/%s failed to flush2 [%d,%d): %v", pages.f.dir.FullPath(), pages.f.Name, chunk.Offset, chunk.Offset+int64(chunk.Size), err)
  68. return
  69. }
  70. return
  71. }
  72. func (pages *ContinuousDirtyPages) FlushToStorage() (chunks []*filer_pb.FileChunk, err error) {
  73. pages.lock.Lock()
  74. defer pages.lock.Unlock()
  75. return pages.saveExistingPagesToStorage()
  76. }
  77. func (pages *ContinuousDirtyPages) saveExistingPagesToStorage() (chunks []*filer_pb.FileChunk, err error) {
  78. var hasSavedData bool
  79. var chunk *filer_pb.FileChunk
  80. for {
  81. chunk, hasSavedData, err = pages.saveExistingLargestPageToStorage()
  82. if !hasSavedData {
  83. return chunks, err
  84. }
  85. if err == nil {
  86. chunks = append(chunks, chunk)
  87. } else {
  88. return
  89. }
  90. }
  91. }
  92. func (pages *ContinuousDirtyPages) saveExistingLargestPageToStorage() (chunk *filer_pb.FileChunk, hasSavedData bool, err error) {
  93. maxList := pages.intervals.RemoveLargestIntervalLinkedList()
  94. if maxList == nil {
  95. return nil, false, nil
  96. }
  97. chunk, err = pages.saveToStorage(maxList.ToReader(), maxList.Offset(), maxList.Size())
  98. if err == nil {
  99. hasSavedData = true
  100. glog.V(3).Infof("%s saveToStorage [%d,%d) %s", pages.f.fullpath(), maxList.Offset(), maxList.Offset()+maxList.Size(), chunk.FileId)
  101. } else {
  102. glog.V(0).Infof("%s saveToStorage [%d,%d): %v", pages.f.fullpath(), maxList.Offset(), maxList.Offset()+maxList.Size(), err)
  103. return
  104. }
  105. return
  106. }
  107. func (pages *ContinuousDirtyPages) saveToStorage(reader io.Reader, offset int64, size int64) (*filer_pb.FileChunk, error) {
  108. var fileId, host string
  109. var auth security.EncodedJwt
  110. dir, _ := pages.f.fullpath().DirAndName()
  111. if err := pages.f.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  112. request := &filer_pb.AssignVolumeRequest{
  113. Count: 1,
  114. Replication: pages.f.wfs.option.Replication,
  115. Collection: pages.f.wfs.option.Collection,
  116. TtlSec: pages.f.wfs.option.TtlSec,
  117. DataCenter: pages.f.wfs.option.DataCenter,
  118. ParentPath: dir,
  119. }
  120. resp, err := client.AssignVolume(context.Background(), request)
  121. if err != nil {
  122. glog.V(0).Infof("assign volume failure %v: %v", request, err)
  123. return err
  124. }
  125. if resp.Error != "" {
  126. return fmt.Errorf("assign volume failure %v: %v", request, resp.Error)
  127. }
  128. fileId, host, auth = resp.FileId, resp.Url, security.EncodedJwt(resp.Auth)
  129. host = pages.f.wfs.AdjustedUrl(host)
  130. pages.collection, pages.replication = resp.Collection, resp.Replication
  131. return nil
  132. }); err != nil {
  133. return nil, fmt.Errorf("filerGrpcAddress assign volume: %v", err)
  134. }
  135. fileUrl := fmt.Sprintf("http://%s/%s", host, fileId)
  136. uploadResult, err, data := operation.Upload(fileUrl, pages.f.Name, pages.f.wfs.option.Cipher, reader, false, "", nil, auth)
  137. if err != nil {
  138. glog.V(0).Infof("upload data %v to %s: %v", pages.f.Name, fileUrl, err)
  139. return nil, fmt.Errorf("upload data: %v", err)
  140. }
  141. if uploadResult.Error != "" {
  142. glog.V(0).Infof("upload failure %v to %s: %v", pages.f.Name, fileUrl, err)
  143. return nil, fmt.Errorf("upload result: %v", uploadResult.Error)
  144. }
  145. pages.f.wfs.chunkCache.SetChunk(fileId, data)
  146. return &filer_pb.FileChunk{
  147. FileId: fileId,
  148. Offset: offset,
  149. Size: uint64(size),
  150. Mtime: time.Now().UnixNano(),
  151. ETag: uploadResult.ETag,
  152. CipherKey: uploadResult.CipherKey,
  153. IsGzipped: uploadResult.Gzip > 0,
  154. }, nil
  155. }
  156. func max(x, y int64) int64 {
  157. if x > y {
  158. return x
  159. }
  160. return y
  161. }
  162. func min(x, y int64) int64 {
  163. if x < y {
  164. return x
  165. }
  166. return y
  167. }
  168. func (pages *ContinuousDirtyPages) ReadDirtyData(data []byte, startOffset int64) (offset int64, size int) {
  169. pages.lock.Lock()
  170. defer pages.lock.Unlock()
  171. return pages.intervals.ReadData(data, startOffset)
  172. }