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.

219 lines
5.6 KiB

7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 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.Path, 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.Path, 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 := 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. return &filer_pb.FileChunk{
  146. FileId: fileId,
  147. Offset: offset,
  148. Size: uint64(size),
  149. Mtime: time.Now().UnixNano(),
  150. ETag: uploadResult.ETag,
  151. CipherKey: uploadResult.CipherKey,
  152. IsGzipped: uploadResult.Gzip > 0,
  153. }, nil
  154. }
  155. func max(x, y int64) int64 {
  156. if x > y {
  157. return x
  158. }
  159. return y
  160. }
  161. func min(x, y int64) int64 {
  162. if x < y {
  163. return x
  164. }
  165. return y
  166. }
  167. func (pages *ContinuousDirtyPages) ReadDirtyData(data []byte, startOffset int64) (offset int64, size int) {
  168. pages.lock.Lock()
  169. defer pages.lock.Unlock()
  170. return pages.intervals.ReadData(data, startOffset)
  171. }