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.

214 lines
5.5 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
5 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. for {
  98. chunk, err = pages.saveToStorage(maxList.ToReader(), maxList.Offset(), maxList.Size())
  99. if err == nil {
  100. hasSavedData = true
  101. glog.V(3).Infof("%s saveToStorage [%d,%d) %s", pages.f.fullpath(), maxList.Offset(), maxList.Offset()+maxList.Size(), chunk.FileId)
  102. return
  103. } else {
  104. glog.V(0).Infof("%s saveToStorage [%d,%d): %v", pages.f.fullpath(), maxList.Offset(), maxList.Offset()+maxList.Size(), err)
  105. time.Sleep(5 * time.Second)
  106. }
  107. }
  108. }
  109. func (pages *ContinuousDirtyPages) saveToStorage(reader io.Reader, offset int64, size int64) (*filer_pb.FileChunk, error) {
  110. var fileId, host string
  111. var auth security.EncodedJwt
  112. dir, _ := pages.f.fullpath().DirAndName()
  113. if err := pages.f.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  114. request := &filer_pb.AssignVolumeRequest{
  115. Count: 1,
  116. Replication: pages.f.wfs.option.Replication,
  117. Collection: pages.f.wfs.option.Collection,
  118. TtlSec: pages.f.wfs.option.TtlSec,
  119. DataCenter: pages.f.wfs.option.DataCenter,
  120. ParentPath: dir,
  121. }
  122. resp, err := client.AssignVolume(context.Background(), request)
  123. if err != nil {
  124. glog.V(0).Infof("assign volume failure %v: %v", request, err)
  125. return err
  126. }
  127. if resp.Error != "" {
  128. return fmt.Errorf("assign volume failure %v: %v", request, resp.Error)
  129. }
  130. fileId, host, auth = resp.FileId, resp.Url, security.EncodedJwt(resp.Auth)
  131. host = pages.f.wfs.AdjustedUrl(host)
  132. pages.collection, pages.replication = resp.Collection, resp.Replication
  133. return nil
  134. }); err != nil {
  135. return nil, fmt.Errorf("filerGrpcAddress assign volume: %v", err)
  136. }
  137. fileUrl := fmt.Sprintf("http://%s/%s", host, fileId)
  138. uploadResult, err, data := operation.Upload(fileUrl, pages.f.Name, pages.f.wfs.option.Cipher, reader, false, "", nil, auth)
  139. if err != nil {
  140. glog.V(0).Infof("upload data %v to %s: %v", pages.f.Name, fileUrl, err)
  141. return nil, fmt.Errorf("upload data: %v", err)
  142. }
  143. if uploadResult.Error != "" {
  144. glog.V(0).Infof("upload failure %v to %s: %v", pages.f.Name, fileUrl, err)
  145. return nil, fmt.Errorf("upload result: %v", uploadResult.Error)
  146. }
  147. pages.f.wfs.chunkCache.SetChunk(fileId, data)
  148. return uploadResult.ToPbFileChunk(fileId, offset), nil
  149. }
  150. func max(x, y int64) int64 {
  151. if x > y {
  152. return x
  153. }
  154. return y
  155. }
  156. func min(x, y int64) int64 {
  157. if x < y {
  158. return x
  159. }
  160. return y
  161. }
  162. func (pages *ContinuousDirtyPages) ReadDirtyData(data []byte, startOffset int64) (offset int64, size int) {
  163. pages.lock.Lock()
  164. defer pages.lock.Unlock()
  165. return pages.intervals.ReadData(data, startOffset)
  166. }