dirty_page.go 5.3 KB

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